blob: 04e308fa1c350c7ee54a4d8fd42e73c6cbb86ca0 [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"
Erlend Egeberg Aasland84d79cf2020-12-29 14:22:55 +010026#include "clinic/row.c.h"
27
28/*[clinic input]
29module _sqlite3
30class _sqlite3.Row "pysqlite_Row *" "pysqlite_RowType"
31[clinic start generated code]*/
32/*[clinic end generated code: output=da39a3ee5e6b4b0d input=384227da65f250fd]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000033
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000034void pysqlite_row_dealloc(pysqlite_Row* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000035{
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +020036 PyTypeObject *tp = Py_TYPE(self);
37
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038 Py_XDECREF(self->data);
39 Py_XDECREF(self->description);
40
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +020041 tp->tp_free(self);
42 Py_DECREF(tp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000043}
44
Erlend Egeberg Aasland84d79cf2020-12-29 14:22:55 +010045/*[clinic input]
46@classmethod
47_sqlite3.Row.__new__ as pysqlite_row_new
48
49 cursor: object(type='pysqlite_Cursor *', subclass_of='pysqlite_CursorType')
50 data: object(subclass_of='&PyTuple_Type')
51 /
52
53[clinic start generated code]*/
54
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030055static PyObject *
Erlend Egeberg Aasland84d79cf2020-12-29 14:22:55 +010056pysqlite_row_new_impl(PyTypeObject *type, pysqlite_Cursor *cursor,
57 PyObject *data)
58/*[clinic end generated code: output=10d58b09a819a4c1 input=f6cd7e6e0935828d]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000059{
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030060 pysqlite_Row *self;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000061
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030062 assert(type != NULL && type->tp_alloc != NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000063
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030064 self = (pysqlite_Row *) type->tp_alloc(type, 0);
65 if (self == NULL)
66 return NULL;
67
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +010068 self->data = Py_NewRef(data);
69 self->description = Py_NewRef(cursor->description);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000070
Serhiy Storchaka3d4b2d42014-08-06 17:50:39 +030071 return (PyObject *) self;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000072}
73
Serhiy Storchaka47a98132014-05-28 12:58:34 +030074PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx)
75{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +010076 PyObject *item = PyTuple_GetItem(self->data, idx);
77 return Py_XNewRef(item);
Serhiy Storchaka47a98132014-05-28 12:58:34 +030078}
79
Serhiy Storchakaf6695812019-09-17 09:20:56 +030080static int
81equal_ignore_case(PyObject *left, PyObject *right)
82{
83 int eq = PyObject_RichCompareBool(left, right, Py_EQ);
84 if (eq) { /* equal or error */
85 return eq;
86 }
87 if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) {
88 return 0;
89 }
90 if (!PyUnicode_IS_ASCII(left) || !PyUnicode_IS_ASCII(right)) {
91 return 0;
92 }
93
94 Py_ssize_t len = PyUnicode_GET_LENGTH(left);
95 if (PyUnicode_GET_LENGTH(right) != len) {
96 return 0;
97 }
98 const Py_UCS1 *p1 = PyUnicode_1BYTE_DATA(left);
99 const Py_UCS1 *p2 = PyUnicode_1BYTE_DATA(right);
100 for (; len; len--, p1++, p2++) {
101 if (Py_TOLOWER(*p1) != Py_TOLOWER(*p2)) {
102 return 0;
103 }
104 }
105 return 1;
106}
107
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000108PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000109{
Serhiy Storchaka47a98132014-05-28 12:58:34 +0300110 Py_ssize_t _idx;
Victor Stinner83e30bf2013-11-18 01:27:30 +0100111 Py_ssize_t nitems, i;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000112
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);
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100119
120 PyObject *item = PyTuple_GetItem(self->data, _idx);
121 return Py_XNewRef(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 */
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100135 PyObject *item = PyTuple_GetItem(self->data, i);
136 return Py_XNewRef(item);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000137 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000138 }
139
140 PyErr_SetString(PyExc_IndexError, "No item with that key");
141 return NULL;
Victor Stinner83e30bf2013-11-18 01:27:30 +0100142 }
143 else if (PySlice_Check(idx)) {
Serhiy Storchaka72e731c2015-03-31 13:33:11 +0300144 return PyObject_GetItem(self->data, idx);
Victor Stinner83e30bf2013-11-18 01:27:30 +0100145 }
146 else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000147 PyErr_SetString(PyExc_IndexError, "Index must be int or string");
148 return NULL;
149 }
150}
151
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200152static Py_ssize_t
153pysqlite_row_length(pysqlite_Row* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000154{
155 return PyTuple_GET_SIZE(self->data);
156}
157
Erlend Egeberg Aasland84d79cf2020-12-29 14:22:55 +0100158/*[clinic input]
159_sqlite3.Row.keys as pysqlite_row_keys
160
161Returns the keys of the row.
162[clinic start generated code]*/
163
164static PyObject *
165pysqlite_row_keys_impl(pysqlite_Row *self)
166/*[clinic end generated code: output=efe3dfb3af6edc07 input=7549a122827c5563]*/
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000167{
168 PyObject* list;
Serhiy Storchaka26861b02015-02-16 20:52:17 +0200169 Py_ssize_t nitems, i;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000170
171 list = PyList_New(0);
172 if (!list) {
173 return NULL;
174 }
175 nitems = PyTuple_Size(self->description);
176
177 for (i = 0; i < nitems; i++) {
178 if (PyList_Append(list, PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0)) != 0) {
179 Py_DECREF(list);
180 return NULL;
181 }
182 }
183
184 return list;
185}
186
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000187static PyObject* pysqlite_iter(pysqlite_Row* self)
188{
189 return PyObject_GetIter(self->data);
190}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000191
Georg Brandl016cec72010-10-18 12:24:53 +0000192static Py_hash_t pysqlite_row_hash(pysqlite_Row *self)
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000193{
194 return PyObject_Hash(self->description) ^ PyObject_Hash(self->data);
195}
196
197static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other, int opid)
198{
Brian Curtindfc80e32011-08-10 20:28:54 -0500199 if (opid != Py_EQ && opid != Py_NE)
200 Py_RETURN_NOTIMPLEMENTED;
201
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200202 if (PyObject_TypeCheck(_other, pysqlite_RowType)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000203 pysqlite_Row *other = (pysqlite_Row *)_other;
Serhiy Storchaka8debfa52019-09-16 20:15:18 +0300204 int eq = PyObject_RichCompareBool(self->description, other->description, Py_EQ);
205 if (eq < 0) {
206 return NULL;
207 }
208 if (eq) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000209 return PyObject_RichCompare(self->data, other->data, opid);
210 }
Serhiy Storchaka8debfa52019-09-16 20:15:18 +0300211 return PyBool_FromLong(opid != Py_EQ);
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000212 }
Brian Curtindfc80e32011-08-10 20:28:54 -0500213 Py_RETURN_NOTIMPLEMENTED;
Christian Heimes81ee3ef2008-05-04 22:42:01 +0000214}
215
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200216static PyMethodDef row_methods[] = {
Erlend Egeberg Aasland84d79cf2020-12-29 14:22:55 +0100217 PYSQLITE_ROW_KEYS_METHODDEF
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000218 {NULL, NULL}
219};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000220
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200221static PyType_Slot row_slots[] = {
222 {Py_tp_dealloc, pysqlite_row_dealloc},
223 {Py_tp_hash, pysqlite_row_hash},
224 {Py_tp_methods, row_methods},
225 {Py_tp_richcompare, pysqlite_row_richcompare},
226 {Py_tp_iter, pysqlite_iter},
227 {Py_mp_length, pysqlite_row_length},
228 {Py_mp_subscript, pysqlite_row_subscript},
229 {Py_sq_length, pysqlite_row_length},
230 {Py_sq_item, pysqlite_row_item},
231 {Py_tp_new, pysqlite_row_new},
232 {0, NULL},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000233};
234
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200235static PyType_Spec row_spec = {
236 .name = MODULE_NAME ".Row",
237 .basicsize = sizeof(pysqlite_Row),
238 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
239 .slots = row_slots,
240};
241
242PyTypeObject *pysqlite_RowType = NULL;
243
244extern int pysqlite_row_setup_types(PyObject *module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000245{
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +0200246 pysqlite_RowType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &row_spec, NULL);
247 if (pysqlite_RowType == NULL) {
248 return -1;
249 }
250 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000251}