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