From: Billy G. A. <bal...@us...> - 2003-10-24 11:34:29
|
Update of /cvsroot/pypgsql/pypgsql/pyPgSQL In directory sc8-pr-cvs1:/tmp/cvs-serv5431/pyPgSQL Modified Files: PgSQL.py Log Message: 22OCT2003 bga - Completed the cleanup of parameter handling in the cursor.execute() method. - Cleaned up the parameter handling in the cursor.callproc() method. Index: PgSQL.py =================================================================== RCS file: /cvsroot/pypgsql/pypgsql/pyPgSQL/PgSQL.py,v retrieving revision 1.36 retrieving revision 1.37 diff -C2 -d -r1.36 -r1.37 *** PgSQL.py 22 Oct 2003 05:27:49 -0000 1.36 --- PgSQL.py 23 Oct 2003 17:24:51 -0000 1.37 *************** *** 30,33 **** --- 30,37 ---- # Date Ini Description | # --------- --- ------------------------------------------------------- | + # 22OCT2003 bga - Completed the cleanup of parameter handling in the | + # cursor.execute() method. | + # - Cleaned up the parameter handling in the | + # cursor.callproc() method. | # 20OCT2003 bga - Fixed the quoting of lists/tuples passed in to the | # cursor.execute method for use with the SQL 'IN' oper- | *************** *** 2244,2248 **** return _j[:-1] + "}'" ! def _quote(value): """ _quote(value) -> string --- 2248,2252 ---- return _j[:-1] + "}'" ! def _quote(value, forProc=False): """ _quote(value) -> string *************** *** 2268,2275 **** elif isinstance(value, StringType): return PgQuoteString(value) ! elif type(value) in [DictType, ListType, TupleType]: ! return _quoteall(value) ! elif isinstance(value, PgResultSet): ! return _quoteall(value) elif isinstance(value, LongType): return str(value) --- 2272,2283 ---- elif isinstance(value, StringType): return PgQuoteString(value) ! elif type(value) is DictType or isinstance(value, PgResultSet): ! raise TypeError, "dictionary or PgResultSet not allowed here" ! elif type(value) in [ListType, TupleType]: ! if forProc: ! raise TypeError, "lists or tuples not allowed here" ! return "(" + \ ! reduce(lambda x, y: x + ", " + y, tuple(map(_quote, value))) + \ ! ")" elif isinstance(value, LongType): return str(value) *************** *** 2277,2280 **** --- 2285,2291 ---- return repr(value) + def _procQuote(value): + return _quote(value, True) + def _quoteall(vdict): """ *************** *** 2292,2298 **** t = (_quote(vdict), ) elif type(vdict) in [ListType, TupleType]: ! t = "(" + \ ! reduce(lambda x, y: x + ", " + y, tuple(map(_quote, vdict))) + \ ! ")" else: raise TypeError, \ --- 2303,2307 ---- t = (_quote(vdict), ) elif type(vdict) in [ListType, TupleType]: ! t = tuple(map(_quote, vdict)) else: raise TypeError, \ *************** *** 2929,2941 **** proc = self.__unicodeConvert(proc) ! args = self.__unicodeConvert(args) ! _qstr = "select %s(" % proc ! for _i in range(len(args)): ! _qstr = '%s%s, ' % (_qstr, _quote(args[_i])) ! if len(args) == 0: ! _qstr = '%s)' % _qstr ! else: ! _qstr = '%s)' % _qstr[:-2] _nl = len(self.conn.notices) --- 2938,2950 ---- proc = self.__unicodeConvert(proc) ! if len(args) == 0: ! args = "()" ! else: ! args = "(" + \ ! reduce(lambda x, y: x + ", " + y, ! tuple(map(_procQuote, ! self.__unicodeConvert(args)))) + ")" ! _qstr = "select %s%s" % (proc, args) _nl = len(self.conn.notices) *************** *** 3002,3012 **** self.closed = 1 ! def execute(self, *parms): ! if len(parms) == 0: ! raise TypeError, "execute() takes at least 2 arguments (1 given)" ! else: ! query = parms[0] # Get the query from the list of parameters ! parms = parms[1:] # and adjust the parameter list. ! if self.closed: raise InterfaceError, "execute failed - the cursor is closed." --- 3011,3015 ---- self.closed = 1 ! def execute(self, query, *parms): if self.closed: raise InterfaceError, "execute failed - the cursor is closed." *************** *** 3057,3063 **** # If there are no paramters, just execute the query. self.res = self.conn.conn.query(_qstr) ! else: ! parms = self.__unicodeConvert(parms) ! parms = tuple(map(_quote, parms)); self.res = self.conn.conn.query(_qstr % parms) self._rows_ = self.res.ntuples --- 3060,3072 ---- # If there are no paramters, just execute the query. self.res = self.conn.conn.query(_qstr) ! else: ! if len(parms) == 1 and \ ! (type(parms[0]) in [DictType, ListType, TupleType] or \ ! isinstance(parms[0], PgResultSet)): ! parms = _quoteall(self.__unicodeConvert(parms[0])) ! self.res = self.conn.conn.query(_qstr % parms) ! else: ! parms = self.__unicodeConvert(parms) ! parms = tuple(map(_quote, self.__unicodeConvert(parms))); self.res = self.conn.conn.query(_qstr % parms) self._rows_ = self.res.ntuples |