From: Gerhard H?r. <gha...@us...> - 2002-10-27 04:01:43
|
Update of /cvsroot/pypgsql/pypgsql/pyPgSQL In directory usw-pr-cvs1:/tmp/cvs-serv2112/pyPgSQL Modified Files: PgSQL.py Log Message: 27OCT2002 gh - Merged the Unicode patch. Closes #484468. - Convert ROWID to PgInt8 instead of PgInt4 (the origi- nal behaviour led to overflow errors.) Index: PgSQL.py =================================================================== RCS file: /cvsroot/pypgsql/pypgsql/pyPgSQL/PgSQL.py,v retrieving revision 1.17 retrieving revision 1.18 diff -C2 -d -r1.17 -r1.18 *** PgSQL.py 2 Oct 2002 05:16:44 -0000 1.17 --- PgSQL.py 27 Oct 2002 04:01:41 -0000 1.18 *************** *** 30,33 **** --- 30,36 ---- # Date Ini Description | # --------- --- ------------------------------------------------------- | + # 27OCT2002 gh - Merged the Unicode patch. Closes #484468. | + # - Convert ROWID to PgInt8 instead of PgInt4 (the origi- | + # nal behaviour led to overflow errors.) | # 02OCT2002 gh - Only support mxDateTime 2.x and give useful error | # message if import fails. | *************** *** 189,193 **** 'database', 'user', 'password', 'options', and 'tty'. The port may also be passed in as part of the host keyword parameter, ! ie. host='localhost:5432'. connection.cursor() -> cursor --- 192,207 ---- 'database', 'user', 'password', 'options', and 'tty'. The port may also be passed in as part of the host keyword parameter, ! ie. host='localhost:5432'. Other optional parameters are ! client_encoding and unicode_results. If unicode_results is true, ! all strings from the backend are returned as Unicode strings. ! ! client_encoding accepts the same parameters as the encode method ! of Unicode strings. If you also want to set a policy for encoding ! errors, set client_encoding to a tuple, like ("koi8-r", "replace") ! ! Note that you still must make sure that the PostgreSQL client is ! using the same encoding as set with the client_encoding parameter. ! This is typically done by issuing a "SET CLIENT_ENCODING TO ..." ! SQL statement immediately after creating the connection. connection.cursor() -> cursor *************** *** 347,351 **** from types import * ! from sys import getrefcount import string import re --- 361,366 ---- from types import * ! from sys import getrefcount, getdefaultencoding ! import copy import string import re *************** *** 714,723 **** else: return PgInt2(value) ! elif _ftv == PG_INT4 or _ftv == ROWID: if type(value) is IntType: return value else: return int(value) ! elif _ftv == PG_INT8: if type(PgInt8) is ClassType: if isinstance(value, PgInt8): --- 729,738 ---- else: return PgInt2(value) ! elif _ftv == PG_INT4: if type(value) is IntType: return value else: return int(value) ! elif _ftv == PG_INT8 or _ftv == ROWID: if type(PgInt8) is ClassType: if isinstance(value, PgInt8): *************** *** 757,761 **** else: return PgOther(value) ! # Other typecasting is not needed. It will be once support for # the other built-in types (ie. box, line, inet, cidr, etc) are added. --- 772,778 ---- else: return PgOther(value) ! elif self.__conn.unicode_results \ ! and _ftv in (PG_CHAR, PG_BPCHAR, PG_TEXT, PG_VARCHAR, PG_NAME): ! return unicode(value, *self.__conn.client_encoding) # Other typecasting is not needed. It will be once support for # the other built-in types (ie. box, line, inet, cidr, etc) are added. *************** *** 808,812 **** except KeyError: _nl = len(self.__conn.notices) ! _res = self.__conn.conn.query("SELECT typname, typprtlen, typelem " "FROM pg_type " "WHERE oid = %s" % pgtype.value) --- 825,829 ---- except KeyError: _nl = len(self.__conn.notices) ! _res = self.__conn.conn.query("SELECT typname, -1 , typelem " "FROM pg_type " "WHERE oid = %s" % pgtype.value) *************** *** 1746,1753 **** def connect(dsn=None, user=None, password=None, host=None, database=None, ! port=None, options=None, tty=None): """ connection = PgSQL.connect(dsn[, user, password, host, database, port, ! options, tty]) Opens a connection to a PostgreSQL database.""" --- 1763,1772 ---- def connect(dsn=None, user=None, password=None, host=None, database=None, ! port=None, options=None, tty=None, client_encoding=None, ! unicode_results=None): """ connection = PgSQL.connect(dsn[, user, password, host, database, port, ! options, tty] [, client_encoding] ! [, unicode_results]) Opens a connection to a PostgreSQL database.""" *************** *** 1791,1795 **** connInfo = "%s%s=%s " % (connInfo, i, _d[i]) ! return Connection(connInfo) def _handleArray(value): --- 1810,1814 ---- connInfo = "%s%s=%s " % (connInfo, i, _d[i]) ! return Connection(connInfo, client_encoding, unicode_results) def _handleArray(value): *************** *** 1861,1865 **** for k, v in vdict.items(): t[k]=_quote(v) ! elif type(vdict) is StringType: # Note: a string is a SequenceType, but is treated as a single # entity, not a sequence of characters. --- 1880,1884 ---- for k, v in vdict.items(): t[k]=_quote(v) ! elif type(vdict) in (StringType, UnicodeType): # Note: a string is a SequenceType, but is treated as a single # entity, not a sequence of characters. *************** *** 1883,1887 **** """Python DB-API 2.0 Connection Object.""" ! def __init__(self, connInfo): try: self.__dict__["conn"] = PQconnectdb(connInfo) --- 1902,1906 ---- """Python DB-API 2.0 Connection Object.""" ! def __init__(self, connInfo, client_encoding=None, unicode_results=None): try: self.__dict__["conn"] = PQconnectdb(connInfo) *************** *** 1906,1909 **** --- 1925,1934 ---- self.__dict__["cursors"] = weakref.WeakValueDictionary() + self.unicode_results = unicode_results + if type(client_encoding) in (TupleType, ListType): + self.client_encoding = client_encoding + else: + self.client_encoding = (client_encoding or getdefaultencoding(),) + def __del__(self): if self._isOpen: *************** *** 1977,1980 **** --- 2002,2007 ---- raise ValueError, \ 'TransactionLevel must be: "", "READ COMMITTED", or "SERIALIZABLE"' + elif name in ('unicode_results', 'client_encoding'): + self.__dict__[name] = value elif self.__dict__.has_key(name): raise AttributeError, "%s is read-only." % name *************** *** 2277,2281 **** else: raise AttributeError, name ! def __fetchOneRow(self): if self._idx_ >= self._rows_: --- 2304,2338 ---- else: raise AttributeError, name ! ! def __unicodeConvert(self, obj): ! if type(obj) is StringType: ! return obj ! elif type(obj) is UnicodeType: ! return obj.encode(*self.conn.client_encoding) ! elif type(obj) in (ListType, TupleType): ! converted_obj = [] ! for item in obj: ! if type(item) is UnicodeType: ! converted_obj.append(item.encode(*self.conn.client_encoding)) ! else: ! converted_obj.append(item) ! return converted_obj ! elif type(obj) is DictType: ! converted_obj = {} ! for k, v in obj.items(): ! if type(v) is UnicodeType: ! converted_obj[k] = v.encode(*self.conn.client_encoding) ! else: ! converted_obj[k] = v ! return converted_obj ! elif isinstance(obj, PgResultSet): ! obj = copy.copy(obj) ! for k, v in obj.items(): ! if type(v) is UnicodeType: ! obj[k] = v.encode(*self.conn.client_encoding) ! return obj ! else: ! return obj ! def __fetchOneRow(self): if self._idx_ >= self._rows_: *************** *** 2435,2438 **** --- 2492,2498 ---- self.conn.__dict__["inTransaction"] = 1 + proc = self.__unicodeConvert(proc) + args = self.__unicodeConvert(args) + _qstr = "select %s(" % proc for _i in range(len(args)): *************** *** 2567,2570 **** --- 2627,2631 ---- try: + _qstr = self.__unicodeConvert(_qstr) if len(parms) == 0: # If there are no paramters, just execute the query. *************** *** 2574,2579 **** --- 2635,2642 ---- (type(parms[0]) in [DictType, ListType, TupleType] or \ isinstance(parms[0], PgResultSet)): + parms = (self.__unicodeConvert(parms[0]),) parms = _quoteall(parms[0]) else: + parms = self.__unicodeConvert(parms) parms = tuple(map(_quote, parms)); self.res = self.conn.conn.query(_qstr % parms) |