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