~groldster/ubuntu/maverick/psycopg2/merge-611040

« back to all changes in this revision

Viewing changes to psycopg/adapter_asis.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabio Tranchitella
  • Date: 2006-08-09 10:28:30 UTC
  • Revision ID: james.westby@ubuntu.com-20060809102830-grac1dsp24uyqfp4
Tags: upstream-2.0.4
ImportĀ upstreamĀ versionĀ 2.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* adapter_asis.c - adapt types as they are
 
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_asis.h"
 
32
#include "psycopg/microprotocols_proto.h"
 
33
 
 
34
/** the AsIs object **/
 
35
 
 
36
static PyObject *
 
37
asis_str(asisObject *self)
 
38
{
 
39
    if (self->wrapped == Py_None) {
 
40
        return PyString_FromString("NULL");
 
41
    }
 
42
    else {
 
43
        return PyObject_Str(self->wrapped);
 
44
    }
 
45
}
 
46
 
 
47
PyObject *
 
48
asis_getquoted(asisObject *self, PyObject *args)
 
49
{
 
50
    if (!PyArg_ParseTuple(args, "")) return NULL;
 
51
    return asis_str(self);
 
52
}
 
53
 
 
54
PyObject *
 
55
asis_conform(asisObject *self, PyObject *args)
 
56
{
 
57
    PyObject *res, *proto;
 
58
    
 
59
    if (!PyArg_ParseTuple(args, "O", &proto)) return NULL;
 
60
 
 
61
    if (proto == (PyObject*)&isqlquoteType)
 
62
        res = (PyObject*)self;
 
63
    else
 
64
        res = Py_None;
 
65
    
 
66
    Py_INCREF(res);
 
67
    return res;
 
68
}
 
69
 
 
70
/** the AsIs object */
 
71
 
 
72
/* object member list */
 
73
 
 
74
static struct PyMemberDef asisObject_members[] = {
 
75
    {"adapted", T_OBJECT, offsetof(asisObject, wrapped), RO},
 
76
    {NULL}
 
77
};
 
78
 
 
79
/* object method table */
 
80
 
 
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},
 
85
    {NULL}  /* Sentinel */
 
86
};
 
87
 
 
88
/* initialization and finalization methods */
 
89
 
 
90
static int
 
91
asis_setup(asisObject *self, PyObject *obj)
 
92
{
 
93
    Dprintf("asis_setup: init asis object at %p, refcnt = %d",
 
94
            self, ((PyObject *)self)->ob_refcnt);
 
95
 
 
96
    self->wrapped = obj;
 
97
    Py_INCREF(self->wrapped);
 
98
    
 
99
    Dprintf("asis_setup: good asis object at %p, refcnt = %d",
 
100
            self, ((PyObject *)self)->ob_refcnt);
 
101
    return 0;
 
102
}
 
103
 
 
104
static void
 
105
asis_dealloc(PyObject* obj)
 
106
{
 
107
    asisObject *self = (asisObject *)obj;
 
108
 
 
109
    Py_XDECREF(self->wrapped);
 
110
    
 
111
    Dprintf("asis_dealloc: deleted asis object at %p, refcnt = %d",
 
112
            obj, obj->ob_refcnt);
 
113
    
 
114
    obj->ob_type->tp_free(obj);
 
115
}
 
116
 
 
117
static int
 
118
asis_init(PyObject *obj, PyObject *args, PyObject *kwds)
 
119
{
 
120
    PyObject *o;
 
121
    
 
122
    if (!PyArg_ParseTuple(args, "O", &o))
 
123
        return -1;
 
124
 
 
125
    return asis_setup((asisObject *)obj, o);
 
126
}
 
127
 
 
128
static PyObject *
 
129
asis_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 
130
{    
 
131
    return type->tp_alloc(type, 0);
 
132
}
 
133
 
 
134
static void
 
135
asis_del(PyObject* self)
 
136
{
 
137
    PyObject_Del(self);
 
138
}
 
139
 
 
140
static PyObject *
 
141
asis_repr(asisObject *self)
 
142
{
 
143
    return PyString_FromFormat("<psycopg2._psycopg.AsIs object at %p>", self);
 
144
}
 
145
 
 
146
 
 
147
/* object type */
 
148
 
 
149
#define asisType_doc \
 
150
"AsIs(str) -> new AsIs adapter object"
 
151
 
 
152
PyTypeObject asisType = {
 
153
    PyObject_HEAD_INIT(NULL)
 
154
    0,
 
155
    "psycopg2._psycopg.AsIs",
 
156
    sizeof(asisObject),
 
157
    0,
 
158
    asis_dealloc, /*tp_dealloc*/
 
159
    0,          /*tp_print*/
 
160
 
 
161
    0,          /*tp_getattr*/
 
162
    0,          /*tp_setattr*/ 
 
163
 
 
164
    0,          /*tp_compare*/
 
165
 
 
166
    (reprfunc)asis_repr, /*tp_repr*/
 
167
    0,          /*tp_as_number*/
 
168
    0,          /*tp_as_sequence*/
 
169
    0,          /*tp_as_mapping*/
 
170
    0,          /*tp_hash */
 
171
 
 
172
    0,          /*tp_call*/
 
173
    (reprfunc)asis_str, /*tp_str*/
 
174
    
 
175
    0,          /*tp_getattro*/
 
176
    0,          /*tp_setattro*/
 
177
    0,          /*tp_as_buffer*/
 
178
 
 
179
    Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/
 
180
    asisType_doc, /*tp_doc*/
 
181
    
 
182
    0,          /*tp_traverse*/
 
183
    0,          /*tp_clear*/
 
184
 
 
185
    0,          /*tp_richcompare*/
 
186
    0,          /*tp_weaklistoffset*/
 
187
 
 
188
    0,          /*tp_iter*/
 
189
    0,          /*tp_iternext*/
 
190
 
 
191
    /* Attribute descriptor and subclassing stuff */
 
192
 
 
193
    asisObject_methods, /*tp_methods*/
 
194
    asisObject_members, /*tp_members*/
 
195
    0,          /*tp_getset*/
 
196
    0,          /*tp_base*/
 
197
    0,          /*tp_dict*/
 
198
    
 
199
    0,          /*tp_descr_get*/
 
200
    0,          /*tp_descr_set*/
 
201
    0,          /*tp_dictoffset*/
 
202
    
 
203
    asis_init, /*tp_init*/
 
204
    0, /*tp_alloc  will be set to PyType_GenericAlloc in module init*/
 
205
    asis_new, /*tp_new*/
 
206
    (freefunc)asis_del, /*tp_free  Low-level free-memory routine */
 
207
    0,          /*tp_is_gc For PyObject_IS_GC */
 
208
    0,          /*tp_bases*/
 
209
    0,          /*tp_mro method resolution order */
 
210
    0,          /*tp_cache*/
 
211
    0,          /*tp_subclasses*/
 
212
    0           /*tp_weaklist*/
 
213
};
 
214
 
 
215
 
 
216
/** module-level functions **/
 
217
 
 
218
PyObject *
 
219
psyco_AsIs(PyObject *module, PyObject *args)
 
220
{
 
221
    PyObject *obj;
 
222
     
 
223
    if (!PyArg_ParseTuple(args, "O", &obj))
 
224
        return NULL;
 
225
  
 
226
    return PyObject_CallFunction((PyObject *)&asisType, "O", obj);
 
227
}