This commit was manufactured by cvs2svn to create branch
[plcapi.git] / psycopg2 / psycopg / adapter_pboolean.c
1 /* adapter_pboolean.c - psycopg boolean type wrapper implementation
2  *
3  * Copyright (C) 2003-2004 Federico Di Gregorio <fog@debian.org>
4  *
5  * This file is part of psycopg.
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include <Python.h>
23 #include <structmember.h>
24 #include <stringobject.h>
25 #include <string.h>
26
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"
33
34
35 /** the Boolean object **/
36
37 static PyObject *
38 pboolean_str(pbooleanObject *self)
39 {
40 #ifdef PSYCOPG_NEW_BOOLEAN
41     if (PyObject_IsTrue(self->wrapped)) {
42         return PyString_FromString("true");
43     }
44     else {
45         return PyString_FromString("false");
46     }
47 #else
48     if (PyObject_IsTrue(self->wrapped)) {
49         return PyString_FromString("'t'");
50     }
51     else {
52         return PyString_FromString("'f'");
53     }
54 #endif
55 }
56
57 PyObject *
58 pboolean_getquoted(pbooleanObject *self, PyObject *args)
59 {
60     if (!PyArg_ParseTuple(args, "")) return NULL;
61     return pboolean_str(self);
62 }
63
64 PyObject *
65 pboolean_conform(pbooleanObject *self, PyObject *args)
66 {
67     PyObject *res, *proto;
68     
69     if (!PyArg_ParseTuple(args, "O", &proto)) return NULL;
70
71     if (proto == (PyObject*)&isqlquoteType)
72         res = (PyObject*)self;
73     else
74         res = Py_None;
75     
76     Py_INCREF(res);
77     return res;
78 }
79
80 /** the Boolean object */
81
82 /* object member list */
83
84 static struct PyMemberDef pbooleanObject_members[] = {
85     {"adapted", T_OBJECT, offsetof(pbooleanObject, wrapped), RO},
86     {NULL}
87 };
88
89 /* object method table */
90
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},
95     {NULL}  /* Sentinel */
96 };
97
98 /* initialization and finalization methods */
99
100 static int
101 pboolean_setup(pbooleanObject *self, PyObject *obj)
102 {
103     Dprintf("pboolean_setup: init pboolean object at %p, refcnt = %d",
104             self, ((PyObject *)self)->ob_refcnt);
105
106     self->wrapped = obj;
107     Py_INCREF(self->wrapped);
108     
109     Dprintf("pboolean_setup: good pboolean object at %p, refcnt = %d",
110             self, ((PyObject *)self)->ob_refcnt);
111     return 0;
112 }
113
114 static void
115 pboolean_dealloc(PyObject* obj)
116 {
117     pbooleanObject *self = (pbooleanObject *)obj;
118
119     Py_XDECREF(self->wrapped);
120     
121     Dprintf("pboolean_dealloc: deleted pboolean object at %p, refcnt = %d",
122             obj, obj->ob_refcnt);
123     
124     obj->ob_type->tp_free(obj);
125 }
126
127 static int
128 pboolean_init(PyObject *obj, PyObject *args, PyObject *kwds)
129 {
130     PyObject *o;
131     
132     if (!PyArg_ParseTuple(args, "O", &o))
133         return -1;
134
135     return pboolean_setup((pbooleanObject *)obj, o);
136 }
137
138 static PyObject *
139 pboolean_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
140 {    
141     return type->tp_alloc(type, 0);
142 }
143
144 static void
145 pboolean_del(PyObject* self)
146 {
147     PyObject_Del(self);
148 }
149
150 static PyObject *
151 pboolean_repr(pbooleanObject *self)
152 {
153     return PyString_FromFormat("<psycopg2._psycopg.Boolean object at %p>",
154                                 self);
155 }
156
157
158 /* object type */
159
160 #define pbooleanType_doc \
161 "Boolean(str) -> new Boolean adapter object"
162
163 PyTypeObject pbooleanType = {
164     PyObject_HEAD_INIT(NULL)
165     0,
166     "psycopg2._psycopg.Boolean",
167     sizeof(pbooleanObject),
168     0,
169     pboolean_dealloc, /*tp_dealloc*/
170     0,          /*tp_print*/
171
172     0,          /*tp_getattr*/
173     0,          /*tp_setattr*/ 
174
175     0,          /*tp_compare*/
176
177     (reprfunc)pboolean_repr, /*tp_repr*/
178     0,          /*tp_as_number*/
179     0,          /*tp_as_sequence*/
180     0,          /*tp_as_mapping*/
181     0,          /*tp_hash */
182
183     0,          /*tp_call*/
184     (reprfunc)pboolean_str, /*tp_str*/
185     
186     0,          /*tp_getattro*/
187     0,          /*tp_setattro*/
188     0,          /*tp_as_buffer*/
189
190     Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
191     pbooleanType_doc, /*tp_doc*/
192     
193     0,          /*tp_traverse*/
194     0,          /*tp_clear*/
195
196     0,          /*tp_richcompare*/
197     0,          /*tp_weaklistoffset*/
198
199     0,          /*tp_iter*/
200     0,          /*tp_iternext*/
201
202     /* Attribute descriptor and subclassing stuff */
203
204     pbooleanObject_methods, /*tp_methods*/
205     pbooleanObject_members, /*tp_members*/
206     0,          /*tp_getset*/
207     0,          /*tp_base*/
208     0,          /*tp_dict*/
209     
210     0,          /*tp_descr_get*/
211     0,          /*tp_descr_set*/
212     0,          /*tp_dictoffset*/
213     
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 */
219     0,          /*tp_bases*/
220     0,          /*tp_mro method resolution order */
221     0,          /*tp_cache*/
222     0,          /*tp_subclasses*/
223     0           /*tp_weaklist*/
224 };
225
226
227 /** module-level functions **/
228
229 PyObject *
230 psyco_Boolean(PyObject *module, PyObject *args)
231 {
232     PyObject *obj;
233      
234     if (!PyArg_ParseTuple(args, "O", &obj))
235         return NULL;
236   
237     return PyObject_CallFunction((PyObject *)&pbooleanType, "O", obj);
238 }