Debian Codegraph @

Modules/_sqlite/row.c

raw
1 /* row.c - an enhanced tuple for database rows
2 *
3 * Copyright (C) 2005-2010 Gerhard Häring <[email protected]>
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 #ifndef Py_BUILD_CORE_BUILTIN
25 # define Py_BUILD_CORE_MODULE 1
26 #endif
27
28 #include "row.h"
29 #include "cursor.h"
30
31 #define clinic_state() (pysqlite_get_state_by_type(type))
32 #include "clinic/row.c.h"
33 #undef clinic_state
34
35 #define _pysqlite_Row_CAST(op) ((pysqlite_Row *)(op))
36
37 /*[clinic input]
38 module _sqlite3
39 class _sqlite3.Row "pysqlite_Row *" "clinic_state()->RowType"
40 [clinic start generated code]*/
41 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=966c53403d7f3a40]*/
42
43 static int
44 row_clear(PyObject *op)
45 {
46 pysqlite_Row *self = _pysqlite_Row_CAST(op);
47 Py_CLEAR(self->data);
48 Py_CLEAR(self->description);
49 return 0;
50 }
51
52 static int
53 row_traverse(PyObject *op, visitproc visit, void *arg)
54 {
55 pysqlite_Row *self = _pysqlite_Row_CAST(op);
56 Py_VISIT(Py_TYPE(self));
57 Py_VISIT(self->data);
58 Py_VISIT(self->description);
59 return 0;
60 }
61
62 static void
63 pysqlite_row_dealloc(PyObject *self)
64 {
65 PyTypeObject *tp = Py_TYPE(self);
66 PyObject_GC_UnTrack(self);
67 (void)tp->tp_clear(self);
68 tp->tp_free(self);
69 Py_DECREF(tp);
70 }
71
72 /*[clinic input]
73 @classmethod
74 _sqlite3.Row.__new__ as pysqlite_row_new
75
76 cursor: object(type='pysqlite_Cursor *', subclass_of='clinic_state()->CursorType')
77 data: object(subclass_of='&PyTuple_Type')
78 /
79
80 [clinic start generated code]*/
81
82 static PyObject *
83 pysqlite_row_new_impl(PyTypeObject *type, pysqlite_Cursor *cursor,
84 PyObject *data)
85 /*[clinic end generated code: output=10d58b09a819a4c1 input=b9e954ca31345dbf]*/
86 {
87 pysqlite_Row *self;
88
89 assert(type != NULL && type->tp_alloc != NULL);
90
91 self = (pysqlite_Row *) type->tp_alloc(type, 0);
92 if (self == NULL)
93 return NULL;
94
95 self->data = Py_NewRef(data);
96 self->description = Py_NewRef(cursor->description);
97
98 return (PyObject *) self;
99 }
100
101 static PyObject *
102 pysqlite_row_item(PyObject *op, Py_ssize_t idx)
103 {
104 pysqlite_Row *self = _pysqlite_Row_CAST(op);
105 PyObject *item = PyTuple_GetItem(self->data, idx);
106 return Py_XNewRef(item);
107 }
108
109 static int
110 equal_ignore_case(PyObject *left, PyObject *right)
111 {
112 int eq = PyObject_RichCompareBool(left, right, Py_EQ);
113 if (eq) { /* equal or error */
114 return eq;
115 }
116 if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) {
117 return 0;
118 }
119 if (!PyUnicode_IS_ASCII(left) || !PyUnicode_IS_ASCII(right)) {
120 return 0;
121 }
122
123 Py_ssize_t len = PyUnicode_GET_LENGTH(left);
124 if (PyUnicode_GET_LENGTH(right) != len) {
125 return 0;
126 }
127 const Py_UCS1 *p1 = PyUnicode_1BYTE_DATA(left);
128 const Py_UCS1 *p2 = PyUnicode_1BYTE_DATA(right);
129 for (; len; len--, p1++, p2++) {
130 if (Py_TOLOWER(*p1) != Py_TOLOWER(*p2)) {
131 return 0;
132 }
133 }
134 return 1;
135 }
136
137 static PyObject *
138 pysqlite_row_subscript(PyObject *op, PyObject *idx)
139 {
140 Py_ssize_t _idx;
141 pysqlite_Row *self = _pysqlite_Row_CAST(op);
142
143 if (PyLong_Check(idx)) {
144 _idx = PyNumber_AsSsize_t(idx, PyExc_IndexError);
145 if (_idx == -1 && PyErr_Occurred())
146 return NULL;
147 if (_idx < 0)
148 _idx += PyTuple_GET_SIZE(self->data);
149
150 PyObject *item = PyTuple_GetItem(self->data, _idx);
151 return Py_XNewRef(item);
152 } else if (PyUnicode_Check(idx)) {
153 if (Py_IsNone(self->description)) {
154 PyErr_Format(PyExc_IndexError, "No item with key %R", idx);
155 return NULL;
156 }
157 Py_ssize_t nitems = PyTuple_GET_SIZE(self->description);
158
159 for (Py_ssize_t i = 0; i < nitems; i++) {
160 PyObject *obj;
161 obj = PyTuple_GET_ITEM(self->description, i);
162 obj = PyTuple_GET_ITEM(obj, 0);
163 int eq = equal_ignore_case(idx, obj);
164 if (eq < 0) {
165 return NULL;
166 }
167 if (eq) {
168 /* found item */
169 PyObject *item = PyTuple_GetItem(self->data, i);
170 return Py_XNewRef(item);
171 }
172 }
173
174 PyErr_SetString(PyExc_IndexError, "No item with that key");
175 return NULL;
176 }
177 else if (PySlice_Check(idx)) {
178 return PyObject_GetItem(self->data, idx);
179 }
180 else {
181 PyErr_SetString(PyExc_IndexError, "Index must be int or string");
182 return NULL;
183 }
184 }
185
186 static Py_ssize_t
187 pysqlite_row_length(PyObject *op)
188 {
189 pysqlite_Row *self = _pysqlite_Row_CAST(op);
190 return PyTuple_GET_SIZE(self->data);
191 }
192
193 /*[clinic input]
194 _sqlite3.Row.keys as pysqlite_row_keys
195
196 Returns the keys of the row.
197 [clinic start generated code]*/
198
199 static PyObject *
200 pysqlite_row_keys_impl(pysqlite_Row *self)
201 /*[clinic end generated code: output=efe3dfb3af6edc07 input=7549a122827c5563]*/
202 {
203 PyObject *list = PyList_New(0);
204 if (!list) {
205 return NULL;
206 }
207 if (Py_IsNone(self->description)) {
208 return list;
209 }
210
211 Py_ssize_t nitems = PyTuple_GET_SIZE(self->description);
212 for (Py_ssize_t i = 0; i < nitems; i++) {
213 PyObject *descr = PyTuple_GET_ITEM(self->description, i);
214 PyObject *name = PyTuple_GET_ITEM(descr, 0);
215 if (PyList_Append(list, name) < 0) {
216 Py_DECREF(list);
217 return NULL;
218 }
219 }
220
221 return list;
222 }
223
224 static PyObject *
225 pysqlite_iter(PyObject *op)
226 {
227 pysqlite_Row *self = _pysqlite_Row_CAST(op);
228 return PyObject_GetIter(self->data);
229 }
230
231 static Py_hash_t
232 pysqlite_row_hash(PyObject *op)
233 {
234 pysqlite_Row *self = _pysqlite_Row_CAST(op);
235 return PyObject_Hash(self->description) ^ PyObject_Hash(self->data);
236 }
237
238 static PyObject *
239 pysqlite_row_richcompare(PyObject *op, PyObject *opother, int opid)
240 {
241 if (opid != Py_EQ && opid != Py_NE)
242 Py_RETURN_NOTIMPLEMENTED;
243
244 pysqlite_Row *self = _pysqlite_Row_CAST(op);
245 pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(self));
246 if (PyObject_TypeCheck(opother, state->RowType)) {
247 pysqlite_Row *other = (pysqlite_Row *)opother;
248 int eq = PyObject_RichCompareBool(self->description, other->description, Py_EQ);
249 if (eq < 0) {
250 return NULL;
251 }
252 if (eq) {
253 return PyObject_RichCompare(self->data, other->data, opid);
254 }
255 return PyBool_FromLong(opid != Py_EQ);
256 }
257 Py_RETURN_NOTIMPLEMENTED;
258 }
259
260 static PyMethodDef row_methods[] = {
261 PYSQLITE_ROW_KEYS_METHODDEF
262 {NULL, NULL}
263 };
264
265 static PyType_Slot row_slots[] = {
266 {Py_tp_dealloc, pysqlite_row_dealloc},
267 {Py_tp_hash, pysqlite_row_hash},
268 {Py_tp_methods, row_methods},
269 {Py_tp_richcompare, pysqlite_row_richcompare},
270 {Py_tp_iter, pysqlite_iter},
271 {Py_mp_length, pysqlite_row_length},
272 {Py_mp_subscript, pysqlite_row_subscript},
273 {Py_sq_length, pysqlite_row_length},
274 {Py_sq_item, pysqlite_row_item},
275 {Py_tp_new, pysqlite_row_new},
276 {Py_tp_traverse, row_traverse},
277 {Py_tp_clear, row_clear},
278 {0, NULL},
279 };
280
281 static PyType_Spec row_spec = {
282 .name = MODULE_NAME ".Row",
283 .basicsize = sizeof(pysqlite_Row),
284 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
285 Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
286 .slots = row_slots,
287 };
288
289 int
290 pysqlite_row_setup_types(PyObject *module)
291 {
292 PyObject *type = PyType_FromModuleAndSpec(module, &row_spec, NULL);
293 if (type == NULL) {
294 return -1;
295 }
296 pysqlite_state *state = pysqlite_get_state(module);
297 state->RowType = (PyTypeObject *)type;
298 return 0;
299 }