1 --- postgresql-7.2rc2/src/interfaces/python/pgdb.py.sopwith Thu Jun 14 22:23:18 2001
2 +++ postgresql-7.2rc2/src/interfaces/python/pgdb.py Sun Jan 27 17:37:36 2002
8 - if type(x) == types.StringType:
9 - x = "'" + string.replace(
10 - string.replace(str(x), '\\', '\\\\'), "'", "''") + "'"
12 + _quote = _pg.quote_fast
13 + _quoteparams = _pg.quoteparams_fast
16 + if type(x) == types.StringType:
17 + x = "'" + string.replace(
18 + string.replace(str(x), '\\', '\\\\'), "'", "''") + "'"
20 + elif type(x) in (types.IntType, types.LongType, types.FloatType):
24 + elif type(x) in (types.ListType, types.TupleType):
25 + return '(%s)' % ','.join(map(lambda x: str(_quote(x)), x))
26 + elif hasattr(x, '__pg_repr__'):
29 + raise InterfaceError, 'do not know how to handle type %s' % type(x)
33 + def _quoteparams(s, params):
34 + if hasattr(params, 'has_key'):
36 + for k, v in params.items():
40 + params = tuple(map(_quote, params))
42 - elif type(x) in (types.IntType, types.LongType, types.FloatType):
46 - elif hasattr(x, '__pg_repr__'):
49 - raise InterfaceError, 'do not know how to handle type %s' % type(x)
53 -def _quoteparams(s, params):
54 - if hasattr(params, 'has_key'):
56 - for k, v in params.items():
60 - params = tuple(map(_quote, params))
67 --- postgresql-7.2rc2/src/interfaces/python/pgmodule.c.sopwith Mon Dec 3 07:39:44 2001
68 +++ postgresql-7.2rc2/src/interfaces/python/pgmodule.c Sun Jan 27 17:39:59 2002
69 @@ -3121,10 +3121,150 @@
71 #endif /* DEFAULT_VARS */
73 +static PyObject *comma_string = NULL;
76 +pgpy_quote_fast(PyObject *self, PyObject *args)
78 + PyObject *x, *retval = NULL;
80 + if(!PyArg_ParseTuple(args, "O:pgpy_quote_fast", &x))
83 + if(x->ob_type == &PyInt_Type || x->ob_type == &PyLong_Type || x->ob_type == &PyFloat_Type)
85 + Py_INCREF(retval = x);
87 + else if(x == Py_None)
88 + retval = PyString_FromString("NULL");
89 + else if(x->ob_type == &PyString_Type)
91 + char *in, *out, *ctmp;
93 + in = PyString_AS_STRING(x);
94 + n = PyString_GET_SIZE(x);
96 + for(i = ct = 0; i < n; i++)
97 + if(in[i] == '\\' || in[i] == '\'')
99 + ctmp = out = alloca(n + ct + 10);
101 + for(i = 0; i < n; i++)
111 + retval = PyString_FromString(out);
113 + else if(PySequence_Check(x))
115 + int i, n = PySequence_Size(x);
116 + PyObject *subout, *subargs, *subjoin = NULL;
118 + subargs = PyTuple_New(1);
119 + subout = PyTuple_New(n);
121 + for(i = 0; i < n; i++)
123 + PyObject *sub = PySequence_GetItem(x, i), *subres;
125 + PyTuple_SetItem(subargs, 0, sub);
126 + subres = pgpy_quote_fast(NULL, subargs);
130 + if(!PyString_Check(subres))
132 + PyObject *subres2 = PyObject_Str(subres);
140 + PyTuple_SetItem(subout, n, subres);
143 + subjoin = _PyString_Join(comma_string, subout);
146 + retval = PyString_FromFormat("(%s)", PyString_AS_STRING(subjoin));
149 + Py_INCREF(Py_None);
150 + PyTuple_SetItem(subargs, 0, Py_None);
151 + Py_DECREF(subargs);
153 + Py_XDECREF(subjoin);
157 + retval = PyEval_CallMethod(x, "__pg_repr__", "()");
160 + PyErr_Format(PyExc_TypeError, "Don't know how to quote type %s", ((PyTypeObject *)x->ob_type)->tp_name);
169 +pgpy_quoteparams_fast(PyObject *self, PyObject *args)
171 + PyObject *s, *params, *x = NULL, *retval;
173 + if(!PyArg_ParseTuple(args, "O!O:pgpy_quoteparams_fast", &PyString_Type, &s, ¶ms))
176 + if(PyDict_Check(params))
179 + PyObject *k, *v, *subargs;
182 + subargs = PyTuple_New(1);
183 + while(PyDict_Next(params, &i, &k, &v))
187 + PyTuple_SetItem(subargs, 0, v);
188 + qres = pgpy_quote_fast(NULL, subargs);
192 + Py_INCREF(Py_None);
193 + PyTuple_SetItem(subargs, 0, Py_None);
194 + Py_DECREF(subargs);
199 + PyDict_SetItem(x, k, qres);
206 + retval = PyString_Format(s, params);
211 /* List of functions defined in the module */
213 static struct PyMethodDef pg_methods[] = {
214 {"connect", (PyCFunction) pgconnect, 3, connect__doc__},
215 + {"quote_fast", (PyCFunction) pgpy_quote_fast, METH_VARARGS},
216 + {"quoteparams_fast", (PyCFunction) pgpy_quoteparams_fast, METH_VARARGS},
219 {"get_defhost", pggetdefhost, 1, getdefhost__doc__},
220 @@ -3178,6 +3318,8 @@
221 PyDict_SetItemString(dict, "RESULT_DDL", PyInt_FromLong(RESULT_DDL));
222 PyDict_SetItemString(dict, "RESULT_DQL", PyInt_FromLong(RESULT_DQL));
224 + comma_string = PyString_InternFromString(",");
227 /* create mode for large objects */
228 PyDict_SetItemString(dict, "INV_READ", PyInt_FromLong(INV_READ));