blob: d10bd59962468ffc4cfb0e3113e6f6c7cae86c4a [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* row.c - an enhanced tuple for database rows
2 *
Florent Xiclunac934f322010-09-03 23:47:32 +00003 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004 *
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"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000026
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000027void pysqlite_row_dealloc(pysqlite_Row* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000028{
29 Py_XDECREF(self->data);
30 Py_XDECREF(self->description);
31
Christian Heimes90aa7642007-12-19 02:45:37 +000032 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033}
34
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000035int pysqlite_row_init(pysqlite_Row* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036{
37 PyObject* data;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000038 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039
40 self->data = 0;
41 self->description = 0;
42
43 if (!PyArg_ParseTuple(args, "OO", &cursor, &data)) {
44 return -1;
45 }
46
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000047 if (!PyObject_IsInstance((PyObject*)cursor, (PyObject*)&pysqlite_CursorType)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000048 PyErr_SetString(PyExc_TypeError, "instance of cursor required for first argument");
49 return -1;
50 }
51
52 if (!PyTuple_Check(data)) {
53 PyErr_SetString(PyExc_TypeError, "tuple required for second argument");
54 return -1;
55 }
56
57 Py_INCREF(data);
58 self->data = data;
59
60 Py_INCREF(cursor->description);
61 self->description = cursor->description;
62
63 return 0;
64}
65
Serhiy Storchaka47a98132014-05-28 12:58:34 +030066PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx)
67{
68 PyObject* item = PyTuple_GetItem(self->data, idx);
69 Py_XINCREF(item);
70 return item;
71}
72
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000073PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000074{
Serhiy Storchaka47a98132014-05-28 12:58:34 +030075 Py_ssize_t _idx;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000076 char* key;
Victor Stinner83e30bf2013-11-18 01:27:30 +010077 Py_ssize_t nitems, i;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078 char* compare_key;
79
80 char* p1;
81 char* p2;
82
83 PyObject* item;
84
Georg Brandle1a0d112007-10-23 19:24:22 +000085 if (PyLong_Check(idx)) {
Serhiy Storchaka47a98132014-05-28 12:58:34 +030086 _idx = PyNumber_AsSsize_t(idx, PyExc_IndexError);
87 if (_idx == -1 && PyErr_Occurred())
88 return NULL;
89 if (_idx < 0)
90 _idx += PyTuple_GET_SIZE(self->data);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000091 item = PyTuple_GetItem(self->data, _idx);
92 Py_XINCREF(item);
93 return item;
Gerhard Häring6d214562007-08-10 18:15:11 +000094 } else if (PyUnicode_Check(idx)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +000095 key = _PyUnicode_AsString(idx);
Victor Stinner86999502010-05-19 01:27:23 +000096 if (key == NULL)
97 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000098
99 nitems = PyTuple_Size(self->description);
100
101 for (i = 0; i < nitems; i++) {
Victor Stinner83e30bf2013-11-18 01:27:30 +0100102 PyObject *obj;
103 obj = PyTuple_GET_ITEM(self->description, i);
104 obj = PyTuple_GET_ITEM(obj, 0);
105 compare_key = _PyUnicode_AsString(obj);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000106 if (!compare_key) {
107 return NULL;
108 }
109
110 p1 = key;
111 p2 = compare_key;
112
113 while (1) {
114 if ((*p1 == (char)0) || (*p2 == (char)0)) {
115 break;
116 }
117
118 if ((*p1 | 0x20) != (*p2 | 0x20)) {
119 break;
120 }
121
122 p1++;
123 p2++;
124 }
125
126 if ((*p1 == (char)0) && (*p2 == (char)0)) {
127 /* found item */
128 item = PyTuple_GetItem(self->data, i);
129 Py_INCREF(item);
130 return item;
131 }
132
133 }
134
135 PyErr_SetString(PyExc_IndexError, "No item with that key");
136 return NULL;
Victor Stinner83e30bf2013-11-18 01:27:30 +0100137 }
138 else if (PySlice_Check(idx)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000139 PyErr_SetString(PyExc_ValueError, "slices not implemented, yet");
140 return NULL;
Victor Stinner83e30bf2013-11-18 01:27:30 +0100141 }
142 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000143 PyErr_SetString(PyExc_IndexError, "Index must be int or string");
144 return NULL;
145 }
146}
147
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000148Py_ssize_t pysqlite_row_length(pysqlite_Row* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000149{
150 return PyTuple_GET_SIZE(self->data);
151}
152
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000153PyObject* pysqlite_row_keys(pysqlite_Row* self, PyObject* args, PyObject* kwargs)
154{
155 PyObject* list;
156 int nitems, i;
157
158 list = PyList_New(0);
159 if (!list) {
160 return NULL;
161 }
162 nitems = PyTuple_Size(self->description);
163
164 for (i = 0; i < nitems; i++) {
165 if (PyList_Append(list, PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0)) != 0) {
166 Py_DECREF(list);
167 return NULL;
168 }
169 }
170
171 return list;
172}
173
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000174static int pysqlite_row_print(pysqlite_Row* self, FILE *fp, int flags)
175{
176 return (&PyTuple_Type)->tp_print(self->data, fp, flags);
177}
178
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000179static PyObject* pysqlite_iter(pysqlite_Row* self)
180{
181 return PyObject_GetIter(self->data);
182}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000183
Georg Brandl016cec72010-10-18 12:24:53 +0000184static Py_hash_t pysqlite_row_hash(pysqlite_Row *self)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000185{
186 return PyObject_Hash(self->description) ^ PyObject_Hash(self->data);
187}
188
189static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other, int opid)
190{
Brian Curtindfc80e32011-08-10 20:28:54 -0500191 if (opid != Py_EQ && opid != Py_NE)
192 Py_RETURN_NOTIMPLEMENTED;
193
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000194 if (PyType_IsSubtype(Py_TYPE(_other), &pysqlite_RowType)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000195 pysqlite_Row *other = (pysqlite_Row *)_other;
196 PyObject *res = PyObject_RichCompare(self->description, other->description, opid);
197 if ((opid == Py_EQ && res == Py_True)
198 || (opid == Py_NE && res == Py_False)) {
199 Py_DECREF(res);
200 return PyObject_RichCompare(self->data, other->data, opid);
201 }
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000202 }
Brian Curtindfc80e32011-08-10 20:28:54 -0500203 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000204}
205
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000206PyMappingMethods pysqlite_row_as_mapping = {
207 /* mp_length */ (lenfunc)pysqlite_row_length,
208 /* mp_subscript */ (binaryfunc)pysqlite_row_subscript,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000209 /* mp_ass_subscript */ (objobjargproc)0,
210};
211
Serhiy Storchaka47a98132014-05-28 12:58:34 +0300212static PySequenceMethods pysqlite_row_as_sequence = {
213 /* sq_length */ (lenfunc)pysqlite_row_length,
214 /* sq_concat */ 0,
215 /* sq_repeat */ 0,
216 /* sq_item */ (ssizeargfunc)pysqlite_row_item,
217};
218
219
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000220static PyMethodDef pysqlite_row_methods[] = {
221 {"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS,
222 PyDoc_STR("Returns the keys of the row.")},
223 {NULL, NULL}
224};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000225
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000226
227PyTypeObject pysqlite_RowType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000228 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000229 MODULE_NAME ".Row", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000230 sizeof(pysqlite_Row), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000231 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000232 (destructor)pysqlite_row_dealloc, /* tp_dealloc */
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000233 (printfunc)pysqlite_row_print, /* tp_print */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000234 0, /* tp_getattr */
235 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +0000236 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000237 0, /* tp_repr */
238 0, /* tp_as_number */
239 0, /* tp_as_sequence */
240 0, /* tp_as_mapping */
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000241 (hashfunc)pysqlite_row_hash, /* tp_hash */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000242 0, /* tp_call */
243 0, /* tp_str */
244 0, /* tp_getattro */
245 0, /* tp_setattro */
246 0, /* tp_as_buffer */
247 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
248 0, /* tp_doc */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000249 (traverseproc)0, /* tp_traverse */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000250 0, /* tp_clear */
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000251 (richcmpfunc)pysqlite_row_richcompare, /* tp_richcompare */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000252 0, /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000253 (getiterfunc)pysqlite_iter, /* tp_iter */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000254 0, /* tp_iternext */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000255 pysqlite_row_methods, /* tp_methods */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000256 0, /* tp_members */
257 0, /* tp_getset */
258 0, /* tp_base */
259 0, /* tp_dict */
260 0, /* tp_descr_get */
261 0, /* tp_descr_set */
262 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000263 (initproc)pysqlite_row_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000264 0, /* tp_alloc */
265 0, /* tp_new */
266 0 /* tp_free */
267};
268
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000269extern int pysqlite_row_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000270{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000271 pysqlite_RowType.tp_new = PyType_GenericNew;
272 pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping;
Serhiy Storchaka47a98132014-05-28 12:58:34 +0300273 pysqlite_RowType.tp_as_sequence = &pysqlite_row_as_sequence;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000274 return PyType_Ready(&pysqlite_RowType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000275}