]>
Commit | Line | Data |
---|---|---|
4a34e4f4 GS |
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 | |
3 | @@ -260,32 +260,38 @@ | |
4 | pass | |
5 | ||
6 | ||
7 | -def _quote(x): | |
8 | - if type(x) == types.StringType: | |
9 | - x = "'" + string.replace( | |
10 | - string.replace(str(x), '\\', '\\\\'), "'", "''") + "'" | |
11 | +try: | |
12 | + _quote = _pg.quote_fast | |
13 | + _quoteparams = _pg.quoteparams_fast | |
14 | +except NameError: | |
15 | + def _quote(x): | |
16 | + if type(x) == types.StringType: | |
17 | + x = "'" + string.replace( | |
18 | + string.replace(str(x), '\\', '\\\\'), "'", "''") + "'" | |
19 | + | |
20 | + elif type(x) in (types.IntType, types.LongType, types.FloatType): | |
21 | + pass | |
22 | + elif x is None: | |
23 | + x = 'NULL' | |
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__'): | |
27 | + x = x.__pg_repr__() | |
28 | + else: | |
29 | + raise InterfaceError, 'do not know how to handle type %s' % type(x) | |
30 | + | |
31 | + return x | |
32 | + | |
33 | + def _quoteparams(s, params): | |
34 | + if hasattr(params, 'has_key'): | |
35 | + x = {} | |
36 | + for k, v in params.items(): | |
37 | + x[k] = _quote(v) | |
38 | + params = x | |
39 | + else: | |
40 | + params = tuple(map(_quote, params)) | |
41 | ||
42 | - elif type(x) in (types.IntType, types.LongType, types.FloatType): | |
43 | - pass | |
44 | - elif x is None: | |
45 | - x = 'NULL' | |
46 | - elif hasattr(x, '__pg_repr__'): | |
47 | - x = x.__pg_repr__() | |
48 | - else: | |
49 | - raise InterfaceError, 'do not know how to handle type %s' % type(x) | |
50 | - | |
51 | - return x | |
52 | - | |
53 | -def _quoteparams(s, params): | |
54 | - if hasattr(params, 'has_key'): | |
55 | - x = {} | |
56 | - for k, v in params.items(): | |
57 | - x[k] = _quote(v) | |
58 | - params = x | |
59 | - else: | |
60 | - params = tuple(map(_quote, params)) | |
61 | - | |
62 | - return s % params | |
63 | + return s % params | |
64 | ||
65 | ### connection object | |
66 | ||
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 @@ | |
70 | } | |
71 | #endif /* DEFAULT_VARS */ | |
72 | ||
73 | +static PyObject *comma_string = NULL; | |
74 | + | |
75 | +static PyObject * | |
76 | +pgpy_quote_fast(PyObject *self, PyObject *args) | |
77 | +{ | |
78 | + PyObject *x, *retval = NULL; | |
79 | + | |
80 | + if(!PyArg_ParseTuple(args, "O:pgpy_quote_fast", &x)) | |
81 | + return NULL; | |
82 | + | |
83 | + if(x->ob_type == &PyInt_Type || x->ob_type == &PyLong_Type || x->ob_type == &PyFloat_Type) | |
84 | + { | |
85 | + Py_INCREF(retval = x); | |
86 | + } | |
87 | + else if(x == Py_None) | |
88 | + retval = PyString_FromString("NULL"); | |
89 | + else if(x->ob_type == &PyString_Type) | |
90 | + { | |
91 | + char *in, *out, *ctmp; | |
92 | + int i, n, ct; | |
93 | + in = PyString_AS_STRING(x); | |
94 | + n = PyString_GET_SIZE(x); | |
95 | + | |
96 | + for(i = ct = 0; i < n; i++) | |
97 | + if(in[i] == '\\' || in[i] == '\'') | |
98 | + ct++; | |
99 | + ctmp = out = alloca(n + ct + 10); | |
100 | + *(ctmp++) = '\''; | |
101 | + for(i = 0; i < n; i++) | |
102 | + { | |
103 | + if(in[i] == '\\') | |
104 | + *(ctmp++) = '\\'; | |
105 | + if(in[i] == '\'') | |
106 | + *(ctmp++) = '\''; | |
107 | + *(ctmp++) = in[i]; | |
108 | + } | |
109 | + *(ctmp++) = '\''; | |
110 | + *(ctmp++) = '\0'; | |
111 | + retval = PyString_FromString(out); | |
112 | + } | |
113 | + else if(PySequence_Check(x)) | |
114 | + { | |
115 | + int i, n = PySequence_Size(x); | |
116 | + PyObject *subout, *subargs, *subjoin = NULL; | |
117 | + | |
118 | + subargs = PyTuple_New(1); | |
119 | + subout = PyTuple_New(n); | |
120 | + | |
121 | + for(i = 0; i < n; i++) | |
122 | + { | |
123 | + PyObject *sub = PySequence_GetItem(x, i), *subres; | |
124 | + | |
125 | + PyTuple_SetItem(subargs, 0, sub); | |
126 | + subres = pgpy_quote_fast(NULL, subargs); | |
127 | + if(!subres) | |
128 | + goto out; | |
129 | + | |
130 | + if(!PyString_Check(subres)) | |
131 | + { | |
132 | + PyObject *subres2 = PyObject_Str(subres); | |
133 | + | |
134 | + if(!subres2) | |
135 | + goto out; | |
136 | + Py_DECREF(subres); | |
137 | + subres = subres2; | |
138 | + } | |
139 | + | |
140 | + PyTuple_SetItem(subout, n, subres); | |
141 | + } | |
142 | + | |
143 | + subjoin = _PyString_Join(comma_string, subout); | |
144 | + if(!subjoin) | |
145 | + goto out; | |
146 | + retval = PyString_FromFormat("(%s)", PyString_AS_STRING(subjoin)); | |
147 | + | |
148 | + out: | |
149 | + Py_INCREF(Py_None); | |
150 | + PyTuple_SetItem(subargs, 0, Py_None); | |
151 | + Py_DECREF(subargs); | |
152 | + Py_DECREF(subout); | |
153 | + Py_XDECREF(subjoin); | |
154 | + } | |
155 | + else | |
156 | + { | |
157 | + retval = PyEval_CallMethod(x, "__pg_repr__", "()"); | |
158 | + if(!retval) | |
159 | + { | |
160 | + PyErr_Format(PyExc_TypeError, "Don't know how to quote type %s", ((PyTypeObject *)x->ob_type)->tp_name); | |
161 | + return NULL; | |
162 | + } | |
163 | + } | |
164 | + | |
165 | + return retval; | |
166 | +} | |
167 | + | |
168 | +static PyObject * | |
169 | +pgpy_quoteparams_fast(PyObject *self, PyObject *args) | |
170 | +{ | |
171 | + PyObject *s, *params, *x = NULL, *retval; | |
172 | + | |
173 | + if(!PyArg_ParseTuple(args, "O!O:pgpy_quoteparams_fast", &PyString_Type, &s, ¶ms)) | |
174 | + return NULL; | |
175 | + | |
176 | + if(PyDict_Check(params)) | |
177 | + { | |
178 | + int i = 0; | |
179 | + PyObject *k, *v, *subargs; | |
180 | + | |
181 | + x = PyDict_New(); | |
182 | + subargs = PyTuple_New(1); | |
183 | + while(PyDict_Next(params, &i, &k, &v)) | |
184 | + { | |
185 | + PyObject *qres; | |
186 | + | |
187 | + PyTuple_SetItem(subargs, 0, v); | |
188 | + qres = pgpy_quote_fast(NULL, subargs); | |
189 | + if(!qres) | |
190 | + { | |
191 | + Py_DECREF(x); | |
192 | + Py_INCREF(Py_None); | |
193 | + PyTuple_SetItem(subargs, 0, Py_None); | |
194 | + Py_DECREF(subargs); | |
195 | + | |
196 | + return NULL; | |
197 | + } | |
198 | + | |
199 | + PyDict_SetItem(x, k, qres); | |
200 | + Py_DECREF(qres); | |
201 | + } | |
202 | + | |
203 | + params = x; | |
204 | + } | |
205 | + | |
206 | + retval = PyString_Format(s, params); | |
207 | + Py_XDECREF(x); | |
208 | + return retval; | |
209 | +} | |
210 | + | |
211 | /* List of functions defined in the module */ | |
212 | ||
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}, | |
217 | ||
218 | #ifdef DEFAULT_VARS | |
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)); | |
223 | ||
224 | + comma_string = PyString_InternFromString(","); | |
225 | + | |
226 | #ifdef LARGE_OBJECTS | |
227 | /* create mode for large objects */ | |
228 | PyDict_SetItemString(dict, "INV_READ", PyInt_FromLong(INV_READ)); |