1 /* adapter_pboolean.c - psycopg boolean type wrapper implementation
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_pboolean.h"
32 #include "psycopg/microprotocols_proto.h"
35 /** the Boolean object **/
38 pboolean_str(pbooleanObject *self)
40 #ifdef PSYCOPG_NEW_BOOLEAN
41 if (PyObject_IsTrue(self->wrapped)) {
42 return PyString_FromString("true");
45 return PyString_FromString("false");
48 if (PyObject_IsTrue(self->wrapped)) {
49 return PyString_FromString("'t'");
52 return PyString_FromString("'f'");
58 pboolean_getquoted(pbooleanObject *self, PyObject *args)
60 if (!PyArg_ParseTuple(args, "")) return NULL;
61 return pboolean_str(self);
65 pboolean_conform(pbooleanObject *self, PyObject *args)
67 PyObject *res, *proto;
69 if (!PyArg_ParseTuple(args, "O", &proto)) return NULL;
71 if (proto == (PyObject*)&isqlquoteType)
72 res = (PyObject*)self;
80 /** the Boolean object */
82 /* object member list */
84 static struct PyMemberDef pbooleanObject_members[] = {
85 {"adapted", T_OBJECT, offsetof(pbooleanObject, wrapped), RO},
89 /* object method table */
91 static PyMethodDef pbooleanObject_methods[] = {
92 {"getquoted", (PyCFunction)pboolean_getquoted, METH_VARARGS,
93 "getquoted() -> wrapped object value as SQL-quoted string"},
94 {"__conform__", (PyCFunction)pboolean_conform, METH_VARARGS, NULL},
98 /* initialization and finalization methods */
101 pboolean_setup(pbooleanObject *self, PyObject *obj)
103 Dprintf("pboolean_setup: init pboolean object at %p, refcnt = %d",
104 self, ((PyObject *)self)->ob_refcnt);
107 Py_INCREF(self->wrapped);
109 Dprintf("pboolean_setup: good pboolean object at %p, refcnt = %d",
110 self, ((PyObject *)self)->ob_refcnt);
115 pboolean_dealloc(PyObject* obj)
117 pbooleanObject *self = (pbooleanObject *)obj;
119 Py_XDECREF(self->wrapped);
121 Dprintf("pboolean_dealloc: deleted pboolean object at %p, refcnt = %d",
122 obj, obj->ob_refcnt);
124 obj->ob_type->tp_free(obj);
128 pboolean_init(PyObject *obj, PyObject *args, PyObject *kwds)
132 if (!PyArg_ParseTuple(args, "O", &o))
135 return pboolean_setup((pbooleanObject *)obj, o);
139 pboolean_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
141 return type->tp_alloc(type, 0);
145 pboolean_del(PyObject* self)
151 pboolean_repr(pbooleanObject *self)
153 return PyString_FromFormat("<psycopg2._psycopg.Boolean object at %p>",
160 #define pbooleanType_doc \
161 "Boolean(str) -> new Boolean adapter object"
163 PyTypeObject pbooleanType = {
164 PyObject_HEAD_INIT(NULL)
166 "psycopg2._psycopg.Boolean",
167 sizeof(pbooleanObject),
169 pboolean_dealloc, /*tp_dealloc*/
177 (reprfunc)pboolean_repr, /*tp_repr*/
179 0, /*tp_as_sequence*/
184 (reprfunc)pboolean_str, /*tp_str*/
190 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
191 pbooleanType_doc, /*tp_doc*/
196 0, /*tp_richcompare*/
197 0, /*tp_weaklistoffset*/
202 /* Attribute descriptor and subclassing stuff */
204 pbooleanObject_methods, /*tp_methods*/
205 pbooleanObject_members, /*tp_members*/
214 pboolean_init, /*tp_init*/
215 0, /*tp_alloc will be set to PyType_GenericAlloc in module init*/
216 pboolean_new, /*tp_new*/
217 (freefunc)pboolean_del, /*tp_free Low-level free-memory routine */
218 0, /*tp_is_gc For PyObject_IS_GC */
220 0, /*tp_mro method resolution order */
227 /** module-level functions **/
230 psyco_Boolean(PyObject *module, PyObject *args)
234 if (!PyArg_ParseTuple(args, "O", &obj))
237 return PyObject_CallFunction((PyObject *)&pbooleanType, "O", obj);