Merge from trunk
[plcapi.git] / trunk / psycopg2 / psycopg / microprotocols_proto.c
1 /* microprotocol_proto.c - psycopg protocols
2  *
3  * Copyright (C) 2003 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
26 #include <string.h>
27
28 #define PSYCOPG_MODULE
29 #include "psycopg/config.h"
30 #include "psycopg/python.h"
31 #include "psycopg/psycopg.h"
32 #include "psycopg/microprotocols_proto.h"
33
34
35 /** void protocol implementation **/
36
37
38 /* getquoted - return quoted representation for object */
39
40 #define psyco_isqlquote_getquoted_doc \
41 "getquoted() -- return SQL-quoted representation of this object"
42
43 static PyObject *
44 psyco_isqlquote_getquoted(isqlquoteObject *self, PyObject *args)
45 {
46     if (!PyArg_ParseTuple(args, "")) return NULL;
47     
48     Py_INCREF(Py_None);
49     return Py_None;
50 }
51
52 /* getbinary - return quoted representation for object */
53
54 #define psyco_isqlquote_getbinary_doc \
55 "getbinary() -- return SQL-quoted binary representation of this object"
56
57 static PyObject *
58 psyco_isqlquote_getbinary(isqlquoteObject *self, PyObject *args)
59 {
60     if (!PyArg_ParseTuple(args, "")) return NULL;
61     
62     Py_INCREF(Py_None);
63     return Py_None;
64 }
65
66 /* getbuffer - return quoted representation for object */
67
68 #define psyco_isqlquote_getbuffer_doc \
69 "getbuffer() -- return this object"
70
71 static PyObject *
72 psyco_isqlquote_getbuffer(isqlquoteObject *self, PyObject *args)
73 {
74     if (!PyArg_ParseTuple(args, "")) return NULL;
75     
76     Py_INCREF(Py_None);
77     return Py_None;
78 }
79
80 \f
81
82 /** the ISQLQuote object **/
83
84
85 /* object method list */
86
87 static struct PyMethodDef isqlquoteObject_methods[] = {
88     {"getquoted", (PyCFunction)psyco_isqlquote_getquoted,
89      METH_VARARGS, psyco_isqlquote_getquoted_doc},
90     {"getbinary", (PyCFunction)psyco_isqlquote_getbinary,
91      METH_VARARGS, psyco_isqlquote_getbinary_doc},
92     {"getbuffer", (PyCFunction)psyco_isqlquote_getbuffer,
93      METH_VARARGS, psyco_isqlquote_getbuffer_doc},
94     /*    {"prepare", (PyCFunction)psyco_isqlquote_prepare,
95           METH_VARARGS, psyco_isqlquote_prepare_doc}, */
96     {NULL}
97 };
98
99 /* object member list */
100
101 static struct PyMemberDef isqlquoteObject_members[] = {
102     /* DBAPI-2.0 extensions (exception objects) */
103     {"_wrapped", T_OBJECT, offsetof(isqlquoteObject, wrapped), RO},
104     {NULL}
105 };
106
107 /* initialization and finalization methods */
108
109 static int
110 isqlquote_setup(isqlquoteObject *self, PyObject *wrapped)
111 {
112     self->wrapped = wrapped;
113     Py_INCREF(wrapped);
114
115     return 0;
116 }
117
118 static void
119 isqlquote_dealloc(PyObject* obj)
120 {
121     isqlquoteObject *self = (isqlquoteObject *)obj;
122     
123     Py_XDECREF(self->wrapped);
124     
125     obj->ob_type->tp_free(obj);
126 }
127
128 static int
129 isqlquote_init(PyObject *obj, PyObject *args, PyObject *kwds)
130 {
131     PyObject *wrapped = NULL;
132
133     if (!PyArg_ParseTuple(args, "O", &wrapped))
134         return -1;
135
136     return isqlquote_setup((isqlquoteObject *)obj, wrapped);
137 }
138
139 static PyObject *
140 isqlquote_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
141 {
142     return type->tp_alloc(type, 0);
143 }
144
145 static void
146 isqlquote_del(PyObject* self)
147 {
148     PyObject_Del(self);
149 }
150
151
152 /* object type */
153
154 #define isqlquoteType_doc \
155 "Abstract ISQLQuote protocol\n\n" \
156 "An object conform to this protocol should expose a ``getquoted()`` method\n" \
157 "returning the SQL representation of the object.\n\n"
158
159 PyTypeObject isqlquoteType = {
160     PyObject_HEAD_INIT(NULL)
161     0,
162     "psycopg2._psycopg.ISQLQuote",
163     sizeof(isqlquoteObject),
164     0,
165     isqlquote_dealloc, /*tp_dealloc*/
166     0,          /*tp_print*/ 
167     0,          /*tp_getattr*/
168     0,          /*tp_setattr*/
169     0,          /*tp_compare*/
170     0,          /*tp_repr*/
171     0,          /*tp_as_number*/
172     0,          /*tp_as_sequence*/
173     0,          /*tp_as_mapping*/
174     0,          /*tp_hash */
175
176     0,          /*tp_call*/
177     0,          /*tp_str*/
178     0,          /*tp_getattro*/
179     0,          /*tp_setattro*/
180     0,          /*tp_as_buffer*/
181
182     Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
183     isqlquoteType_doc, /*tp_doc*/
184     
185     0,          /*tp_traverse*/
186     0,          /*tp_clear*/
187
188     0,          /*tp_richcompare*/
189     0,          /*tp_weaklistoffset*/
190
191     0,          /*tp_iter*/
192     0,          /*tp_iternext*/
193
194     /* Attribute descriptor and subclassing stuff */
195
196     isqlquoteObject_methods, /*tp_methods*/
197     isqlquoteObject_members, /*tp_members*/
198     0,          /*tp_getset*/
199     0,          /*tp_base*/
200     0,          /*tp_dict*/
201     
202     0,          /*tp_descr_get*/
203     0,          /*tp_descr_set*/
204     0,          /*tp_dictoffset*/
205     
206     isqlquote_init, /*tp_init*/
207     0, /*tp_alloc  will be set to PyType_GenericAlloc in module init*/
208     isqlquote_new, /*tp_new*/
209     (freefunc)isqlquote_del, /*tp_free  Low-level free-memory routine */
210     0,          /*tp_is_gc For PyObject_IS_GC */
211     0,          /*tp_bases*/
212     0,          /*tp_mro method resolution order */
213     0,          /*tp_cache*/
214     0,          /*tp_subclasses*/
215     0           /*tp_weaklist*/
216 };