blob: 4b47108278a0ab4d6c1043ae48f7c40aa31e11bf [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
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030035static PyObject *
36pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037{
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030038 pysqlite_Row *self;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039 PyObject* data;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000040 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000041
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030042 assert(type != NULL && type->tp_alloc != NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000043
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +030044 if (!_PyArg_NoKeywords("Row", kwargs))
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030045 return NULL;
46 if (!PyArg_ParseTuple(args, "OO", &cursor, &data))
47 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000048
Serhiy Storchaka08d230a2015-05-22 11:02:49 +030049 if (!PyObject_TypeCheck((PyObject*)cursor, &pysqlite_CursorType)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000050 PyErr_SetString(PyExc_TypeError, "instance of cursor required for first argument");
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030051 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000052 }
53
54 if (!PyTuple_Check(data)) {
55 PyErr_SetString(PyExc_TypeError, "tuple required for second argument");
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030056 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000057 }
58
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030059 self = (pysqlite_Row *) type->tp_alloc(type, 0);
60 if (self == NULL)
61 return NULL;
62
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000063 Py_INCREF(data);
64 self->data = data;
65
66 Py_INCREF(cursor->description);
67 self->description = cursor->description;
68
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030069 return (PyObject *) self;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000070}
71
Serhiy Storchaka47a98132014-05-28 12:58:34 +030072PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx)
73{
74 PyObject* item = PyTuple_GetItem(self->data, idx);
75 Py_XINCREF(item);
76 return item;
77}
78
Serhiy Storchakaf6695812019-09-17 09:20:56 +030079static int
80equal_ignore_case(PyObject *left, PyObject *right)
81{
82 int eq = PyObject_RichCompareBool(left, right, Py_EQ);
83 if (eq) { /* equal or error */
84 return eq;
85 }
86 if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) {
87 return 0;
88 }
89 if (!PyUnicode_IS_ASCII(left) || !PyUnicode_IS_ASCII(right)) {
90 return 0;
91 }
92
93 Py_ssize_t len = PyUnicode_GET_LENGTH(left);
94 if (PyUnicode_GET_LENGTH(right) != len) {
95 return 0;
96 }
97 const Py_UCS1 *p1 = PyUnicode_1BYTE_DATA(left);
98 const Py_UCS1 *p2 = PyUnicode_1BYTE_DATA(right);
99 for (; len; len--, p1++, p2++) {
100 if (Py_TOLOWER(*p1) != Py_TOLOWER(*p2)) {
101 return 0;
102 }
103 }
104 return 1;
105}
106
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000107PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000108{
Serhiy Storchaka47a98132014-05-28 12:58:34 +0300109 Py_ssize_t _idx;
Victor Stinner83e30bf2013-11-18 01:27:30 +0100110 Py_ssize_t nitems, i;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000111 PyObject* item;
112
Georg Brandle1a0d112007-10-23 19:24:22 +0000113 if (PyLong_Check(idx)) {
Serhiy Storchaka47a98132014-05-28 12:58:34 +0300114 _idx = PyNumber_AsSsize_t(idx, PyExc_IndexError);
115 if (_idx == -1 && PyErr_Occurred())
116 return NULL;
117 if (_idx < 0)
118 _idx += PyTuple_GET_SIZE(self->data);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000119 item = PyTuple_GetItem(self->data, _idx);
120 Py_XINCREF(item);
121 return item;
Gerhard Häring6d214562007-08-10 18:15:11 +0000122 } else if (PyUnicode_Check(idx)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000123 nitems = PyTuple_Size(self->description);
124
125 for (i = 0; i < nitems; i++) {
Victor Stinner83e30bf2013-11-18 01:27:30 +0100126 PyObject *obj;
127 obj = PyTuple_GET_ITEM(self->description, i);
128 obj = PyTuple_GET_ITEM(obj, 0);
Serhiy Storchakaf6695812019-09-17 09:20:56 +0300129 int eq = equal_ignore_case(idx, obj);
130 if (eq < 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000131 return NULL;
132 }
Serhiy Storchakaf6695812019-09-17 09:20:56 +0300133 if (eq) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000134 /* found item */
135 item = PyTuple_GetItem(self->data, i);
136 Py_INCREF(item);
137 return item;
138 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000139 }
140
141 PyErr_SetString(PyExc_IndexError, "No item with that key");
142 return NULL;
Victor Stinner83e30bf2013-11-18 01:27:30 +0100143 }
144 else if (PySlice_Check(idx)) {
Serhiy Storchaka72e731c2015-03-31 13:33:11 +0300145 return PyObject_GetItem(self->data, idx);
Victor Stinner83e30bf2013-11-18 01:27:30 +0100146 }
147 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000148 PyErr_SetString(PyExc_IndexError, "Index must be int or string");
149 return NULL;
150 }
151}
152
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200153static Py_ssize_t
154pysqlite_row_length(pysqlite_Row* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000155{
156 return PyTuple_GET_SIZE(self->data);
157}
158
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530159PyObject* pysqlite_row_keys(pysqlite_Row* self, PyObject *Py_UNUSED(ignored))
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000160{
161 PyObject* list;
Serhiy Storchaka26861b02015-02-16 20:52:17 +0200162 Py_ssize_t nitems, i;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000163
164 list = PyList_New(0);
165 if (!list) {
166 return NULL;
167 }
168 nitems = PyTuple_Size(self->description);
169
170 for (i = 0; i < nitems; i++) {
171 if (PyList_Append(list, PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0)) != 0) {
172 Py_DECREF(list);
173 return NULL;
174 }
175 }
176
177 return list;
178}
179
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000180static PyObject* pysqlite_iter(pysqlite_Row* self)
181{
182 return PyObject_GetIter(self->data);
183}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000184
Georg Brandl016cec72010-10-18 12:24:53 +0000185static Py_hash_t pysqlite_row_hash(pysqlite_Row *self)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000186{
187 return PyObject_Hash(self->description) ^ PyObject_Hash(self->data);
188}
189
190static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other, int opid)
191{
Brian Curtindfc80e32011-08-10 20:28:54 -0500192 if (opid != Py_EQ && opid != Py_NE)
193 Py_RETURN_NOTIMPLEMENTED;
194
Serhiy Storchaka8debfa52019-09-16 20:15:18 +0300195 if (PyObject_TypeCheck(_other, &pysqlite_RowType)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000196 pysqlite_Row *other = (pysqlite_Row *)_other;
Serhiy Storchaka8debfa52019-09-16 20:15:18 +0300197 int eq = PyObject_RichCompareBool(self->description, other->description, Py_EQ);
198 if (eq < 0) {
199 return NULL;
200 }
201 if (eq) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000202 return PyObject_RichCompare(self->data, other->data, opid);
203 }
Serhiy Storchaka8debfa52019-09-16 20:15:18 +0300204 return PyBool_FromLong(opid != Py_EQ);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000205 }
Brian Curtindfc80e32011-08-10 20:28:54 -0500206 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000207}
208
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000209PyMappingMethods pysqlite_row_as_mapping = {
210 /* mp_length */ (lenfunc)pysqlite_row_length,
211 /* mp_subscript */ (binaryfunc)pysqlite_row_subscript,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000212 /* mp_ass_subscript */ (objobjargproc)0,
213};
214
Serhiy Storchaka47a98132014-05-28 12:58:34 +0300215static PySequenceMethods pysqlite_row_as_sequence = {
216 /* sq_length */ (lenfunc)pysqlite_row_length,
217 /* sq_concat */ 0,
218 /* sq_repeat */ 0,
219 /* sq_item */ (ssizeargfunc)pysqlite_row_item,
220};
221
222
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000223static PyMethodDef pysqlite_row_methods[] = {
224 {"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS,
225 PyDoc_STR("Returns the keys of the row.")},
226 {NULL, NULL}
227};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000228
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000229
230PyTypeObject pysqlite_RowType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000231 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000232 MODULE_NAME ".Row", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000233 sizeof(pysqlite_Row), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000234 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000235 (destructor)pysqlite_row_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200236 0, /* tp_vectorcall_offset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000237 0, /* tp_getattr */
238 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200239 0, /* tp_as_async */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000240 0, /* tp_repr */
241 0, /* tp_as_number */
242 0, /* tp_as_sequence */
243 0, /* tp_as_mapping */
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000244 (hashfunc)pysqlite_row_hash, /* tp_hash */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000245 0, /* tp_call */
246 0, /* tp_str */
247 0, /* tp_getattro */
248 0, /* tp_setattro */
249 0, /* tp_as_buffer */
250 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
251 0, /* tp_doc */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000252 (traverseproc)0, /* tp_traverse */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000253 0, /* tp_clear */
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000254 (richcmpfunc)pysqlite_row_richcompare, /* tp_richcompare */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000255 0, /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000256 (getiterfunc)pysqlite_iter, /* tp_iter */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000257 0, /* tp_iternext */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000258 pysqlite_row_methods, /* tp_methods */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000259 0, /* tp_members */
260 0, /* tp_getset */
261 0, /* tp_base */
262 0, /* tp_dict */
263 0, /* tp_descr_get */
264 0, /* tp_descr_set */
265 0, /* tp_dictoffset */
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +0300266 0, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000267 0, /* tp_alloc */
268 0, /* tp_new */
269 0 /* tp_free */
270};
271
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000272extern int pysqlite_row_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000273{
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +0300274 pysqlite_RowType.tp_new = pysqlite_row_new;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000275 pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping;
Serhiy Storchaka47a98132014-05-28 12:58:34 +0300276 pysqlite_RowType.tp_as_sequence = &pysqlite_row_as_sequence;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000277 return PyType_Ready(&pysqlite_RowType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000278}