1 /* adapter_asis.c - adapt types as they are
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>
27 #define PSYCOPG_MODULE
28 #include "psycopg/config.h"
29 #include "psycopg/python.h"
30 #include "psycopg/psycopg.h"
31 #include "psycopg/adapter_asis.h"
32 #include "psycopg/microprotocols_proto.h"
34 /** the AsIs object **/
37 asis_str(asisObject *self)
39 if (self->wrapped == Py_None) {
40 return PyString_FromString("NULL");
43 return PyObject_Str(self->wrapped);
48 asis_getquoted(asisObject *self, PyObject *args)
50 if (!PyArg_ParseTuple(args, "")) return NULL;
51 return asis_str(self);
55 asis_conform(asisObject *self, PyObject *args)
57 PyObject *res, *proto;
59 if (!PyArg_ParseTuple(args, "O", &proto)) return NULL;
61 if (proto == (PyObject*)&isqlquoteType)
62 res = (PyObject*)self;
70 /** the AsIs object */
72 /* object member list */
74 static struct PyMemberDef asisObject_members[] = {
75 {"adapted", T_OBJECT, offsetof(asisObject, wrapped), RO},
79 /* object method table */
81 static PyMethodDef asisObject_methods[] = {
82 {"getquoted", (PyCFunction)asis_getquoted, METH_VARARGS,
83 "getquoted() -> wrapped object value as SQL-quoted string"},
84 {"__conform__", (PyCFunction)asis_conform, METH_VARARGS, NULL},
88 /* initialization and finalization methods */
91 asis_setup(asisObject *self, PyObject *obj)
93 Dprintf("asis_setup: init asis object at %p, refcnt = %d",
94 self, ((PyObject *)self)->ob_refcnt);
97 Py_INCREF(self->wrapped);
99 Dprintf("asis_setup: good asis object at %p, refcnt = %d",
100 self, ((PyObject *)self)->ob_refcnt);
105 asis_dealloc(PyObject* obj)
107 asisObject *self = (asisObject *)obj;
109 Py_XDECREF(self->wrapped);
111 Dprintf("asis_dealloc: deleted asis object at %p, refcnt = %d",
112 obj, obj->ob_refcnt);
114 obj->ob_type->tp_free(obj);
118 asis_init(PyObject *obj, PyObject *args, PyObject *kwds)
122 if (!PyArg_ParseTuple(args, "O", &o))
125 return asis_setup((asisObject *)obj, o);
129 asis_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
131 return type->tp_alloc(type, 0);
135 asis_del(PyObject* self)
141 asis_repr(asisObject *self)
143 return PyString_FromFormat("<psycopg2._psycopg.AsIs object at %p>", self);
149 #define asisType_doc \
150 "AsIs(str) -> new AsIs adapter object"
152 PyTypeObject asisType = {
153 PyObject_HEAD_INIT(NULL)
155 "psycopg2._psycopg.AsIs",
158 asis_dealloc, /*tp_dealloc*/
166 (reprfunc)asis_repr, /*tp_repr*/
168 0, /*tp_as_sequence*/
173 (reprfunc)asis_str, /*tp_str*/
179 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
180 asisType_doc, /*tp_doc*/
185 0, /*tp_richcompare*/
186 0, /*tp_weaklistoffset*/
191 /* Attribute descriptor and subclassing stuff */
193 asisObject_methods, /*tp_methods*/
194 asisObject_members, /*tp_members*/
203 asis_init, /*tp_init*/
204 0, /*tp_alloc will be set to PyType_GenericAlloc in module init*/
206 (freefunc)asis_del, /*tp_free Low-level free-memory routine */
207 0, /*tp_is_gc For PyObject_IS_GC */
209 0, /*tp_mro method resolution order */
216 /** module-level functions **/
219 psyco_AsIs(PyObject *module, PyObject *args)
223 if (!PyArg_ParseTuple(args, "O", &obj))
226 return PyObject_CallFunction((PyObject *)&asisType, "O", obj);