Line data Source code
1 : /* row.c - an enhanced tuple for database rows
2 : *
3 : * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
4 : *
5 : * This file is part of pysqlite.
6 : *
7 : * This software is provided 'as-is', without any express or implied
8 : * warranty. In no event will the authors be held liable for any damages
9 : * arising from the use of this software.
10 : *
11 : * Permission is granted to anyone to use this software for any purpose,
12 : * including commercial applications, and to alter it and redistribute it
13 : * freely, subject to the following restrictions:
14 : *
15 : * 1. The origin of this software must not be misrepresented; you must not
16 : * claim that you wrote the original software. If you use this software
17 : * in a product, an acknowledgment in the product documentation would be
18 : * appreciated but is not required.
19 : * 2. Altered source versions must be plainly marked as such, and must not be
20 : * misrepresented as being the original software.
21 : * 3. This notice may not be removed or altered from any source distribution.
22 : */
23 :
24 : #include "row.h"
25 : #include "cursor.h"
26 : #include "sqlitecompat.h"
27 :
28 0 : void pysqlite_row_dealloc(pysqlite_Row* self)
29 : {
30 0 : Py_XDECREF(self->data);
31 0 : Py_XDECREF(self->description);
32 :
33 0 : Py_TYPE(self)->tp_free((PyObject*)self);
34 0 : }
35 :
36 0 : int pysqlite_row_init(pysqlite_Row* self, PyObject* args, PyObject* kwargs)
37 : {
38 : PyObject* data;
39 : pysqlite_Cursor* cursor;
40 :
41 0 : self->data = 0;
42 0 : self->description = 0;
43 :
44 0 : if (!PyArg_ParseTuple(args, "OO", &cursor, &data)) {
45 0 : return -1;
46 : }
47 :
48 0 : if (!PyObject_IsInstance((PyObject*)cursor, (PyObject*)&pysqlite_CursorType)) {
49 0 : PyErr_SetString(PyExc_TypeError, "instance of cursor required for first argument");
50 0 : return -1;
51 : }
52 :
53 0 : if (!PyTuple_Check(data)) {
54 0 : PyErr_SetString(PyExc_TypeError, "tuple required for second argument");
55 0 : return -1;
56 : }
57 :
58 0 : Py_INCREF(data);
59 0 : self->data = data;
60 :
61 0 : Py_INCREF(cursor->description);
62 0 : self->description = cursor->description;
63 :
64 0 : return 0;
65 : }
66 :
67 0 : PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
68 : {
69 : long _idx;
70 : char* key;
71 : int nitems, i;
72 : char* compare_key;
73 :
74 : char* p1;
75 : char* p2;
76 :
77 : PyObject* item;
78 :
79 0 : if (PyLong_Check(idx)) {
80 0 : _idx = PyLong_AsLong(idx);
81 0 : item = PyTuple_GetItem(self->data, _idx);
82 0 : Py_XINCREF(item);
83 0 : return item;
84 0 : } else if (PyUnicode_Check(idx)) {
85 0 : key = _PyUnicode_AsString(idx);
86 0 : if (key == NULL)
87 0 : return NULL;
88 :
89 0 : nitems = PyTuple_Size(self->description);
90 :
91 0 : for (i = 0; i < nitems; i++) {
92 0 : compare_key = _PyUnicode_AsString(PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0));
93 0 : if (!compare_key) {
94 0 : return NULL;
95 : }
96 :
97 0 : p1 = key;
98 0 : p2 = compare_key;
99 :
100 : while (1) {
101 0 : if ((*p1 == (char)0) || (*p2 == (char)0)) {
102 : break;
103 : }
104 :
105 0 : if ((*p1 | 0x20) != (*p2 | 0x20)) {
106 0 : break;
107 : }
108 :
109 0 : p1++;
110 0 : p2++;
111 0 : }
112 :
113 0 : if ((*p1 == (char)0) && (*p2 == (char)0)) {
114 : /* found item */
115 0 : item = PyTuple_GetItem(self->data, i);
116 0 : Py_INCREF(item);
117 0 : return item;
118 : }
119 :
120 : }
121 :
122 0 : PyErr_SetString(PyExc_IndexError, "No item with that key");
123 0 : return NULL;
124 0 : } else if (PySlice_Check(idx)) {
125 0 : PyErr_SetString(PyExc_ValueError, "slices not implemented, yet");
126 0 : return NULL;
127 : } else {
128 0 : PyErr_SetString(PyExc_IndexError, "Index must be int or string");
129 0 : return NULL;
130 : }
131 : }
132 :
133 0 : Py_ssize_t pysqlite_row_length(pysqlite_Row* self, PyObject* args, PyObject* kwargs)
134 : {
135 0 : return PyTuple_GET_SIZE(self->data);
136 : }
137 :
138 0 : PyObject* pysqlite_row_keys(pysqlite_Row* self, PyObject* args, PyObject* kwargs)
139 : {
140 : PyObject* list;
141 : int nitems, i;
142 :
143 0 : list = PyList_New(0);
144 0 : if (!list) {
145 0 : return NULL;
146 : }
147 0 : nitems = PyTuple_Size(self->description);
148 :
149 0 : for (i = 0; i < nitems; i++) {
150 0 : if (PyList_Append(list, PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0)) != 0) {
151 0 : Py_DECREF(list);
152 0 : return NULL;
153 : }
154 : }
155 :
156 0 : return list;
157 : }
158 :
159 0 : static int pysqlite_row_print(pysqlite_Row* self, FILE *fp, int flags)
160 : {
161 0 : return (&PyTuple_Type)->tp_print(self->data, fp, flags);
162 : }
163 :
164 0 : static PyObject* pysqlite_iter(pysqlite_Row* self)
165 : {
166 0 : return PyObject_GetIter(self->data);
167 : }
168 :
169 0 : static Py_hash_t pysqlite_row_hash(pysqlite_Row *self)
170 : {
171 0 : return PyObject_Hash(self->description) ^ PyObject_Hash(self->data);
172 : }
173 :
174 0 : static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other, int opid)
175 : {
176 0 : if (opid != Py_EQ && opid != Py_NE)
177 0 : Py_RETURN_NOTIMPLEMENTED;
178 :
179 0 : if (PyType_IsSubtype(Py_TYPE(_other), &pysqlite_RowType)) {
180 0 : pysqlite_Row *other = (pysqlite_Row *)_other;
181 0 : PyObject *res = PyObject_RichCompare(self->description, other->description, opid);
182 0 : if ((opid == Py_EQ && res == Py_True)
183 0 : || (opid == Py_NE && res == Py_False)) {
184 0 : Py_DECREF(res);
185 0 : return PyObject_RichCompare(self->data, other->data, opid);
186 : }
187 : }
188 0 : Py_RETURN_NOTIMPLEMENTED;
189 : }
190 :
191 : PyMappingMethods pysqlite_row_as_mapping = {
192 : /* mp_length */ (lenfunc)pysqlite_row_length,
193 : /* mp_subscript */ (binaryfunc)pysqlite_row_subscript,
194 : /* mp_ass_subscript */ (objobjargproc)0,
195 : };
196 :
197 : static PyMethodDef pysqlite_row_methods[] = {
198 : {"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS,
199 : PyDoc_STR("Returns the keys of the row.")},
200 : {NULL, NULL}
201 : };
202 :
203 :
204 : PyTypeObject pysqlite_RowType = {
205 : PyVarObject_HEAD_INIT(NULL, 0)
206 : MODULE_NAME ".Row", /* tp_name */
207 : sizeof(pysqlite_Row), /* tp_basicsize */
208 : 0, /* tp_itemsize */
209 : (destructor)pysqlite_row_dealloc, /* tp_dealloc */
210 : (printfunc)pysqlite_row_print, /* tp_print */
211 : 0, /* tp_getattr */
212 : 0, /* tp_setattr */
213 : 0, /* tp_reserved */
214 : 0, /* tp_repr */
215 : 0, /* tp_as_number */
216 : 0, /* tp_as_sequence */
217 : 0, /* tp_as_mapping */
218 : (hashfunc)pysqlite_row_hash, /* tp_hash */
219 : 0, /* tp_call */
220 : 0, /* tp_str */
221 : 0, /* tp_getattro */
222 : 0, /* tp_setattro */
223 : 0, /* tp_as_buffer */
224 : Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
225 : 0, /* tp_doc */
226 : (traverseproc)0, /* tp_traverse */
227 : 0, /* tp_clear */
228 : (richcmpfunc)pysqlite_row_richcompare, /* tp_richcompare */
229 : 0, /* tp_weaklistoffset */
230 : (getiterfunc)pysqlite_iter, /* tp_iter */
231 : 0, /* tp_iternext */
232 : pysqlite_row_methods, /* tp_methods */
233 : 0, /* tp_members */
234 : 0, /* tp_getset */
235 : 0, /* tp_base */
236 : 0, /* tp_dict */
237 : 0, /* tp_descr_get */
238 : 0, /* tp_descr_set */
239 : 0, /* tp_dictoffset */
240 : (initproc)pysqlite_row_init, /* tp_init */
241 : 0, /* tp_alloc */
242 : 0, /* tp_new */
243 : 0 /* tp_free */
244 : };
245 :
246 0 : extern int pysqlite_row_setup_types(void)
247 : {
248 0 : pysqlite_RowType.tp_new = PyType_GenericNew;
249 0 : pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping;
250 0 : return PyType_Ready(&pysqlite_RowType);
251 : }
|