1
/* adapter_qstring.c - QuotedString objects
3
* Copyright (C) 2003-2004 Federico Di Gregorio <fog@debian.org>
5
* This file is part of psycopg.
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 2,
10
* or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
#include <structmember.h>
24
#include <stringobject.h>
29
#define PSYCOPG_MODULE
30
#include "psycopg/config.h"
31
#include "psycopg/python.h"
32
#include "psycopg/psycopg.h"
33
#include "psycopg/connection.h"
34
#include "psycopg/adapter_qstring.h"
35
#include "psycopg/microprotocols_proto.h"
38
/** the quoting code */
40
#ifndef PSYCOPG_OWN_QUOTING
42
qstring_escape(char *to, char *from, size_t len, PGconn *conn)
44
#if PG_MAJOR_VERSION > 8 || \
45
(PG_MAJOR_VERSION == 8 && PG_MINOR_VERSION > 1) || \
46
(PG_MAJOR_VERSION == 8 && PG_MINOR_VERSION == 1 && PG_PATCH_VERSION >= 4)
49
return PQescapeStringConn(conn, to, from, len, &err);
52
return PQescapeString(to, from, len);
56
qstring_escape(char *to, char *from, size_t len, PGconn *conn)
60
for (i=0, j=0; i<len; i++) {
74
/* do nothing, embedded \0 are discarded */
83
Dprintf("qstring_quote: to = %s", to);
88
/* qstring_quote - do the quote process on plain and unicode strings */
91
qstring_quote(qstringObject *self)
97
/* if the wrapped object is an unicode object we can encode it to match
98
self->encoding but if the encoding is not specified we don't know what
99
to do and we raise an exception */
101
/* TODO: we need a real translation table from postgres encoding names to
104
if (PyUnicode_Check(self->wrapped) && self->encoding) {
105
PyObject *enc = PyDict_GetItemString(psycoEncodings, self->encoding);
106
/* note that pgenc is a borrowed reference */
109
char *s = PyString_AsString(enc);
110
Dprintf("qstring_quote: encoding unicode object to %s", s);
111
str = PyUnicode_AsEncodedString(self->wrapped, s, NULL);
112
Dprintf("qstring_quote: got encoded object at %p", str);
113
if (str == NULL) return NULL;
116
/* can't find the right encoder, raise exception */
117
PyErr_Format(InterfaceError,
118
"can't encode unicode string to %s", self->encoding);
123
/* if the wrapped object is a simple string, we don't know how to
124
(re)encode it, so we pass it as-is */
125
else if (PyString_Check(self->wrapped)) {
127
/* INCREF to make it ref-wise identical to unicode one */
131
/* if the wrapped object is not a string, this is an error */
133
PyErr_SetString(PyExc_TypeError,
134
"can't quote non-string object (or missing encoding)");
138
/* encode the string into buffer */
139
PyString_AsStringAndSize(str, &s, &len);
141
buffer = (char *)PyMem_Malloc((len*2+3) * sizeof(char));
142
if (buffer == NULL) {
148
Py_BEGIN_ALLOW_THREADS;
149
len = qstring_escape(buffer+1, s, len,
150
self->conn ? ((connectionObject*)self->conn)->pgconn : NULL);
151
buffer[0] = '\'' ; buffer[len+1] = '\'';
152
Py_END_ALLOW_THREADS;
154
self->buffer = PyString_FromStringAndSize(buffer, len+2);
161
/* qstring_str, qstring_getquoted - return result of quoting */
164
qstring_str(qstringObject *self)
166
if (self->buffer == NULL) {
169
Py_XINCREF(self->buffer);
174
qstring_getquoted(qstringObject *self, PyObject *args)
176
if (!PyArg_ParseTuple(args, "")) return NULL;
177
return qstring_str(self);
181
qstring_prepare(qstringObject *self, PyObject *args)
183
connectionObject *conn;
185
if (!PyArg_ParseTuple(args, "O", &conn))
188
/* we bother copying the encoding only if the wrapped string is unicode,
189
we don't need the encoding if that's not the case */
190
if (PyUnicode_Check(self->wrapped)) {
191
if (self->encoding) free(self->encoding);
192
self->encoding = strdup(conn->encoding);
193
Dprintf("qstring_prepare: set encoding to %s", conn->encoding);
196
Py_XDECREF(self->conn);
198
self->conn = (PyObject*)conn;
199
Py_INCREF(self->conn);
207
qstring_conform(qstringObject *self, PyObject *args)
209
PyObject *res, *proto;
211
if (!PyArg_ParseTuple(args, "O", &proto)) return NULL;
213
if (proto == (PyObject*)&isqlquoteType)
214
res = (PyObject*)self;
222
/** the QuotedString object **/
224
/* object member list */
226
static struct PyMemberDef qstringObject_members[] = {
227
{"adapted", T_OBJECT, offsetof(qstringObject, wrapped), RO},
228
{"buffer", T_OBJECT, offsetof(qstringObject, buffer), RO},
229
{"encoding", T_STRING, offsetof(qstringObject, encoding), RO},
233
/* object method table */
235
static PyMethodDef qstringObject_methods[] = {
236
{"getquoted", (PyCFunction)qstring_getquoted, METH_VARARGS,
237
"getquoted() -> wrapped object value as SQL-quoted string"},
238
{"prepare", (PyCFunction)qstring_prepare, METH_VARARGS,
239
"prepare(conn) -> set encoding to conn->encoding and store conn"},
240
{"__conform__", (PyCFunction)qstring_conform, METH_VARARGS, NULL},
241
{NULL} /* Sentinel */
244
/* initialization and finalization methods */
247
qstring_setup(qstringObject *self, PyObject *str, char *enc)
249
Dprintf("qstring_setup: init qstring object at %p, refcnt = %d",
250
self, ((PyObject *)self)->ob_refcnt);
255
/* FIXME: remove this orrible strdup */
256
if (enc) self->encoding = strdup(enc);
259
Py_INCREF(self->wrapped);
261
Dprintf("qstring_setup: good qstring object at %p, refcnt = %d",
262
self, ((PyObject *)self)->ob_refcnt);
267
qstring_dealloc(PyObject* obj)
269
qstringObject *self = (qstringObject *)obj;
271
Py_XDECREF(self->wrapped);
272
Py_XDECREF(self->buffer);
273
Py_XDECREF(self->conn);
275
if (self->encoding) free(self->encoding);
277
Dprintf("qstring_dealloc: deleted qstring object at %p, refcnt = %d",
278
obj, obj->ob_refcnt);
280
obj->ob_type->tp_free(obj);
284
qstring_init(PyObject *obj, PyObject *args, PyObject *kwds)
287
char *enc = "latin-1"; /* default encoding as in Python */
289
if (!PyArg_ParseTuple(args, "O|s", &str, &enc))
292
return qstring_setup((qstringObject *)obj, str, enc);
296
qstring_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
298
return type->tp_alloc(type, 0);
302
qstring_del(PyObject* self)
308
qstring_repr(qstringObject *self)
310
return PyString_FromFormat("<psycopg2._psycopg.QuotedString object at %p>",
316
#define qstringType_doc \
317
"QuotedString(str, enc) -> new quoted object with 'enc' encoding"
319
PyTypeObject qstringType = {
320
PyObject_HEAD_INIT(NULL)
322
"psycopg2._psycopg.QuotedString",
323
sizeof(qstringObject),
325
qstring_dealloc, /*tp_dealloc*/
331
(reprfunc)qstring_repr, /*tp_repr*/
333
0, /*tp_as_sequence*/
338
(reprfunc)qstring_str, /*tp_str*/
343
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
345
qstringType_doc, /*tp_doc*/
350
0, /*tp_richcompare*/
351
0, /*tp_weaklistoffset*/
356
/* Attribute descriptor and subclassing stuff */
358
qstringObject_methods, /*tp_methods*/
359
qstringObject_members, /*tp_members*/
368
qstring_init, /*tp_init*/
369
0, /*tp_alloc will be set to PyType_GenericAlloc in module init*/
370
qstring_new, /*tp_new*/
371
(freefunc)qstring_del, /*tp_free Low-level free-memory routine */
372
0, /*tp_is_gc For PyObject_IS_GC */
374
0, /*tp_mro method resolution order */
381
/** module-level functions **/
384
psyco_QuotedString(PyObject *module, PyObject *args)
387
char *enc = "latin-1"; /* default encoding as in Python */
389
if (!PyArg_ParseTuple(args, "O|s", &str, &enc))
392
return PyObject_CallFunction((PyObject *)&qstringType, "Os", str, enc);