blob: 9086c19af73ccf46a02e3129076143c0fbbc7500 [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{
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +020029 PyTypeObject *tp = Py_TYPE(self);
30
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000031 Py_XDECREF(self->data);
32 Py_XDECREF(self->description);
33
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +020034 tp->tp_free(self);
35 Py_DECREF(tp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000036}
37
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030038static PyObject *
39pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000040{
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030041 pysqlite_Row *self;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000042 PyObject* data;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000043 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000044
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030045 assert(type != NULL && type->tp_alloc != NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000046
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +030047 if (!_PyArg_NoKeywords("Row", kwargs))
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030048 return NULL;
49 if (!PyArg_ParseTuple(args, "OO", &cursor, &data))
50 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000051
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +020052 if (!PyObject_TypeCheck((PyObject*)cursor, pysqlite_CursorType)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000053 PyErr_SetString(PyExc_TypeError, "instance of cursor required for first argument");
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030054 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000055 }
56
57 if (!PyTuple_Check(data)) {
58 PyErr_SetString(PyExc_TypeError, "tuple required for second argument");
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030059 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000060 }
61
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030062 self = (pysqlite_Row *) type->tp_alloc(type, 0);
63 if (self == NULL)
64 return NULL;
65
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +010066 self->data = Py_NewRef(data);
67 self->description = Py_NewRef(cursor->description);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000068
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{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +010074 PyObject *item = PyTuple_GetItem(self->data, idx);
75 return Py_XNewRef(item);
Serhiy Storchaka47a98132014-05-28 12:58:34 +030076}
77
Serhiy Storchakaf6695812019-09-17 09:20:56 +030078static int
79equal_ignore_case(PyObject *left, PyObject *right)
80{
81 int eq = PyObject_RichCompareBool(left, right, Py_EQ);
82 if (eq) { /* equal or error */
83 return eq;
84 }
85 if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) {
86 return 0;
87 }
88 if (!PyUnicode_IS_ASCII(left) || !PyUnicode_IS_ASCII(right)) {
89 return 0;
90 }
91
92 Py_ssize_t len = PyUnicode_GET_LENGTH(left);
93 if (PyUnicode_GET_LENGTH(right) != len) {
94 return 0;
95 }
96 const Py_UCS1 *p1 = PyUnicode_1BYTE_DATA(left);
97 const Py_UCS1 *p2 = PyUnicode_1BYTE_DATA(right);
98 for (; len; len--, p1++, p2++) {
99 if (Py_TOLOWER(*p1) != Py_TOLOWER(*p2)) {
100 return 0;
101 }
102 }
103 return 1;
104}
105
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000106PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000107{
Serhiy Storchaka47a98132014-05-28 12:58:34 +0300108 Py_ssize_t _idx;
Victor Stinner83e30bf2013-11-18 01:27:30 +0100109 Py_ssize_t nitems, i;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000110
Georg Brandle1a0d112007-10-23 19:24:22 +0000111 if (PyLong_Check(idx)) {
Serhiy Storchaka47a98132014-05-28 12:58:34 +0300112 _idx = PyNumber_AsSsize_t(idx, PyExc_IndexError);
113 if (_idx == -1 && PyErr_Occurred())
114 return NULL;
115 if (_idx < 0)
116 _idx += PyTuple_GET_SIZE(self->data);
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100117
118 PyObject *item = PyTuple_GetItem(self->data, _idx);
119 return Py_XNewRef(item);
Gerhard Häring6d214562007-08-10 18:15:11 +0000120 } else if (PyUnicode_Check(idx)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000121 nitems = PyTuple_Size(self->description);
122
123 for (i = 0; i < nitems; i++) {
Victor Stinner83e30bf2013-11-18 01:27:30 +0100124 PyObject *obj;
125 obj = PyTuple_GET_ITEM(self->description, i);
126 obj = PyTuple_GET_ITEM(obj, 0);
Serhiy Storchakaf6695812019-09-17 09:20:56 +0300127 int eq = equal_ignore_case(idx, obj);
128 if (eq < 0) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000129 return NULL;
130 }
Serhiy Storchakaf6695812019-09-17 09:20:56 +0300131 if (eq) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000132 /* found item */
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100133 PyObject *item = PyTuple_GetItem(self->data, i);
134 return Py_XNewRef(item);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000135 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000136 }
137
138 PyErr_SetString(PyExc_IndexError, "No item with that key");
139 return NULL;
Victor Stinner83e30bf2013-11-18 01:27:30 +0100140 }
141 else if (PySlice_Check(idx)) {
Serhiy Storchaka72e731c2015-03-31 13:33:11 +0300142 return PyObject_GetItem(self->data, idx);
Victor Stinner83e30bf2013-11-18 01:27:30 +0100143 }
144 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000145 PyErr_SetString(PyExc_IndexError, "Index must be int or string");
146 return NULL;
147 }
148}
149
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200150static Py_ssize_t
151pysqlite_row_length(pysqlite_Row* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152{
153 return PyTuple_GET_SIZE(self->data);
154}
155
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +0530156PyObject* pysqlite_row_keys(pysqlite_Row* self, PyObject *Py_UNUSED(ignored))
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000157{
158 PyObject* list;
Serhiy Storchaka26861b02015-02-16 20:52:17 +0200159 Py_ssize_t nitems, i;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000160
161 list = PyList_New(0);
162 if (!list) {
163 return NULL;
164 }
165 nitems = PyTuple_Size(self->description);
166
167 for (i = 0; i < nitems; i++) {
168 if (PyList_Append(list, PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0)) != 0) {
169 Py_DECREF(list);
170 return NULL;
171 }
172 }
173
174 return list;
175}
176
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000177static PyObject* pysqlite_iter(pysqlite_Row* self)
178{
179 return PyObject_GetIter(self->data);
180}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000181
Georg Brandl016cec72010-10-18 12:24:53 +0000182static Py_hash_t pysqlite_row_hash(pysqlite_Row *self)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000183{
184 return PyObject_Hash(self->description) ^ PyObject_Hash(self->data);
185}
186
187static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other, int opid)
188{
Brian Curtindfc80e32011-08-10 20:28:54 -0500189 if (opid != Py_EQ && opid != Py_NE)
190 Py_RETURN_NOTIMPLEMENTED;
191
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200192 if (PyObject_TypeCheck(_other, pysqlite_RowType)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000193 pysqlite_Row *other = (pysqlite_Row *)_other;
Serhiy Storchaka8debfa52019-09-16 20:15:18 +0300194 int eq = PyObject_RichCompareBool(self->description, other->description, Py_EQ);
195 if (eq < 0) {
196 return NULL;
197 }
198 if (eq) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000199 return PyObject_RichCompare(self->data, other->data, opid);
200 }
Serhiy Storchaka8debfa52019-09-16 20:15:18 +0300201 return PyBool_FromLong(opid != Py_EQ);
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
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200206static PyMethodDef row_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000207 {"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS,
208 PyDoc_STR("Returns the keys of the row.")},
209 {NULL, NULL}
210};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000211
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200212static PyType_Slot row_slots[] = {
213 {Py_tp_dealloc, pysqlite_row_dealloc},
214 {Py_tp_hash, pysqlite_row_hash},
215 {Py_tp_methods, row_methods},
216 {Py_tp_richcompare, pysqlite_row_richcompare},
217 {Py_tp_iter, pysqlite_iter},
218 {Py_mp_length, pysqlite_row_length},
219 {Py_mp_subscript, pysqlite_row_subscript},
220 {Py_sq_length, pysqlite_row_length},
221 {Py_sq_item, pysqlite_row_item},
222 {Py_tp_new, pysqlite_row_new},
223 {0, NULL},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000224};
225
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200226static PyType_Spec row_spec = {
227 .name = MODULE_NAME ".Row",
228 .basicsize = sizeof(pysqlite_Row),
229 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
230 .slots = row_slots,
231};
232
233PyTypeObject *pysqlite_RowType = NULL;
234
235extern int pysqlite_row_setup_types(PyObject *module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000236{
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200237 pysqlite_RowType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &row_spec, NULL);
238 if (pysqlite_RowType == NULL) {
239 return -1;
240 }
241 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000242}