Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | /* row.c - an enhanced tuple for database rows |
| 2 | * |
Florent Xicluna | c934f32 | 2010-09-03 23:47:32 +0000 | [diff] [blame] | 3 | * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de> |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 | #include "row.h" |
| 25 | #include "cursor.h" |
Erlend Egeberg Aasland | 84d79cf | 2020-12-29 14:22:55 +0100 | [diff] [blame] | 26 | #include "clinic/row.c.h" |
| 27 | |
| 28 | /*[clinic input] |
| 29 | module _sqlite3 |
| 30 | class _sqlite3.Row "pysqlite_Row *" "pysqlite_RowType" |
| 31 | [clinic start generated code]*/ |
| 32 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=384227da65f250fd]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 33 | |
Erlend Egeberg Aasland | bf838a6 | 2021-02-21 01:29:19 +0100 | [diff] [blame] | 34 | static void |
| 35 | pysqlite_row_dealloc(pysqlite_Row *self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 36 | { |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 37 | PyTypeObject *tp = Py_TYPE(self); |
| 38 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 39 | Py_XDECREF(self->data); |
| 40 | Py_XDECREF(self->description); |
| 41 | |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 42 | tp->tp_free(self); |
| 43 | Py_DECREF(tp); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Erlend Egeberg Aasland | 84d79cf | 2020-12-29 14:22:55 +0100 | [diff] [blame] | 46 | /*[clinic input] |
| 47 | @classmethod |
| 48 | _sqlite3.Row.__new__ as pysqlite_row_new |
| 49 | |
| 50 | cursor: object(type='pysqlite_Cursor *', subclass_of='pysqlite_CursorType') |
| 51 | data: object(subclass_of='&PyTuple_Type') |
| 52 | / |
| 53 | |
| 54 | [clinic start generated code]*/ |
| 55 | |
Serhiy Storchaka | 3d4b2d4 | 2014-08-06 17:50:39 +0300 | [diff] [blame] | 56 | static PyObject * |
Erlend Egeberg Aasland | 84d79cf | 2020-12-29 14:22:55 +0100 | [diff] [blame] | 57 | pysqlite_row_new_impl(PyTypeObject *type, pysqlite_Cursor *cursor, |
| 58 | PyObject *data) |
| 59 | /*[clinic end generated code: output=10d58b09a819a4c1 input=f6cd7e6e0935828d]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 60 | { |
Serhiy Storchaka | 3d4b2d4 | 2014-08-06 17:50:39 +0300 | [diff] [blame] | 61 | pysqlite_Row *self; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 62 | |
Serhiy Storchaka | 3d4b2d4 | 2014-08-06 17:50:39 +0300 | [diff] [blame] | 63 | assert(type != NULL && type->tp_alloc != NULL); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 64 | |
Serhiy Storchaka | 3d4b2d4 | 2014-08-06 17:50:39 +0300 | [diff] [blame] | 65 | self = (pysqlite_Row *) type->tp_alloc(type, 0); |
| 66 | if (self == NULL) |
| 67 | return NULL; |
| 68 | |
Erlend Egeberg Aasland | bf64d90 | 2020-12-27 12:05:33 +0100 | [diff] [blame] | 69 | self->data = Py_NewRef(data); |
| 70 | self->description = Py_NewRef(cursor->description); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 71 | |
Serhiy Storchaka | 3d4b2d4 | 2014-08-06 17:50:39 +0300 | [diff] [blame] | 72 | return (PyObject *) self; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Serhiy Storchaka | 47a9813 | 2014-05-28 12:58:34 +0300 | [diff] [blame] | 75 | PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx) |
| 76 | { |
Erlend Egeberg Aasland | bf64d90 | 2020-12-27 12:05:33 +0100 | [diff] [blame] | 77 | PyObject *item = PyTuple_GetItem(self->data, idx); |
| 78 | return Py_XNewRef(item); |
Serhiy Storchaka | 47a9813 | 2014-05-28 12:58:34 +0300 | [diff] [blame] | 79 | } |
| 80 | |
Serhiy Storchaka | f669581 | 2019-09-17 09:20:56 +0300 | [diff] [blame] | 81 | static int |
| 82 | equal_ignore_case(PyObject *left, PyObject *right) |
| 83 | { |
| 84 | int eq = PyObject_RichCompareBool(left, right, Py_EQ); |
| 85 | if (eq) { /* equal or error */ |
| 86 | return eq; |
| 87 | } |
| 88 | if (!PyUnicode_Check(left) || !PyUnicode_Check(right)) { |
| 89 | return 0; |
| 90 | } |
| 91 | if (!PyUnicode_IS_ASCII(left) || !PyUnicode_IS_ASCII(right)) { |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | Py_ssize_t len = PyUnicode_GET_LENGTH(left); |
| 96 | if (PyUnicode_GET_LENGTH(right) != len) { |
| 97 | return 0; |
| 98 | } |
| 99 | const Py_UCS1 *p1 = PyUnicode_1BYTE_DATA(left); |
| 100 | const Py_UCS1 *p2 = PyUnicode_1BYTE_DATA(right); |
| 101 | for (; len; len--, p1++, p2++) { |
| 102 | if (Py_TOLOWER(*p1) != Py_TOLOWER(*p2)) { |
| 103 | return 0; |
| 104 | } |
| 105 | } |
| 106 | return 1; |
| 107 | } |
| 108 | |
Erlend Egeberg Aasland | bf838a6 | 2021-02-21 01:29:19 +0100 | [diff] [blame] | 109 | static PyObject * |
| 110 | pysqlite_row_subscript(pysqlite_Row *self, PyObject *idx) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 111 | { |
Serhiy Storchaka | 47a9813 | 2014-05-28 12:58:34 +0300 | [diff] [blame] | 112 | Py_ssize_t _idx; |
Victor Stinner | 83e30bf | 2013-11-18 01:27:30 +0100 | [diff] [blame] | 113 | Py_ssize_t nitems, i; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 114 | |
Georg Brandl | e1a0d11 | 2007-10-23 19:24:22 +0000 | [diff] [blame] | 115 | if (PyLong_Check(idx)) { |
Serhiy Storchaka | 47a9813 | 2014-05-28 12:58:34 +0300 | [diff] [blame] | 116 | _idx = PyNumber_AsSsize_t(idx, PyExc_IndexError); |
| 117 | if (_idx == -1 && PyErr_Occurred()) |
| 118 | return NULL; |
| 119 | if (_idx < 0) |
| 120 | _idx += PyTuple_GET_SIZE(self->data); |
Erlend Egeberg Aasland | bf64d90 | 2020-12-27 12:05:33 +0100 | [diff] [blame] | 121 | |
| 122 | PyObject *item = PyTuple_GetItem(self->data, _idx); |
| 123 | return Py_XNewRef(item); |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 124 | } else if (PyUnicode_Check(idx)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 125 | nitems = PyTuple_Size(self->description); |
| 126 | |
| 127 | for (i = 0; i < nitems; i++) { |
Victor Stinner | 83e30bf | 2013-11-18 01:27:30 +0100 | [diff] [blame] | 128 | PyObject *obj; |
| 129 | obj = PyTuple_GET_ITEM(self->description, i); |
| 130 | obj = PyTuple_GET_ITEM(obj, 0); |
Serhiy Storchaka | f669581 | 2019-09-17 09:20:56 +0300 | [diff] [blame] | 131 | int eq = equal_ignore_case(idx, obj); |
| 132 | if (eq < 0) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 133 | return NULL; |
| 134 | } |
Serhiy Storchaka | f669581 | 2019-09-17 09:20:56 +0300 | [diff] [blame] | 135 | if (eq) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 136 | /* found item */ |
Erlend Egeberg Aasland | bf64d90 | 2020-12-27 12:05:33 +0100 | [diff] [blame] | 137 | PyObject *item = PyTuple_GetItem(self->data, i); |
| 138 | return Py_XNewRef(item); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 139 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | PyErr_SetString(PyExc_IndexError, "No item with that key"); |
| 143 | return NULL; |
Victor Stinner | 83e30bf | 2013-11-18 01:27:30 +0100 | [diff] [blame] | 144 | } |
| 145 | else if (PySlice_Check(idx)) { |
Serhiy Storchaka | 72e731c | 2015-03-31 13:33:11 +0300 | [diff] [blame] | 146 | return PyObject_GetItem(self->data, idx); |
Victor Stinner | 83e30bf | 2013-11-18 01:27:30 +0100 | [diff] [blame] | 147 | } |
| 148 | else { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 149 | PyErr_SetString(PyExc_IndexError, "Index must be int or string"); |
| 150 | return NULL; |
| 151 | } |
| 152 | } |
| 153 | |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 154 | static Py_ssize_t |
| 155 | pysqlite_row_length(pysqlite_Row* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 156 | { |
| 157 | return PyTuple_GET_SIZE(self->data); |
| 158 | } |
| 159 | |
Erlend Egeberg Aasland | 84d79cf | 2020-12-29 14:22:55 +0100 | [diff] [blame] | 160 | /*[clinic input] |
| 161 | _sqlite3.Row.keys as pysqlite_row_keys |
| 162 | |
| 163 | Returns the keys of the row. |
| 164 | [clinic start generated code]*/ |
| 165 | |
| 166 | static PyObject * |
| 167 | pysqlite_row_keys_impl(pysqlite_Row *self) |
| 168 | /*[clinic end generated code: output=efe3dfb3af6edc07 input=7549a122827c5563]*/ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 169 | { |
| 170 | PyObject* list; |
Serhiy Storchaka | 26861b0 | 2015-02-16 20:52:17 +0200 | [diff] [blame] | 171 | Py_ssize_t nitems, i; |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 172 | |
| 173 | list = PyList_New(0); |
| 174 | if (!list) { |
| 175 | return NULL; |
| 176 | } |
| 177 | nitems = PyTuple_Size(self->description); |
| 178 | |
| 179 | for (i = 0; i < nitems; i++) { |
| 180 | if (PyList_Append(list, PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0)) != 0) { |
| 181 | Py_DECREF(list); |
| 182 | return NULL; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return list; |
| 187 | } |
| 188 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 189 | static PyObject* pysqlite_iter(pysqlite_Row* self) |
| 190 | { |
| 191 | return PyObject_GetIter(self->data); |
| 192 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 193 | |
Georg Brandl | 016cec7 | 2010-10-18 12:24:53 +0000 | [diff] [blame] | 194 | static Py_hash_t pysqlite_row_hash(pysqlite_Row *self) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 195 | { |
| 196 | return PyObject_Hash(self->description) ^ PyObject_Hash(self->data); |
| 197 | } |
| 198 | |
| 199 | static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other, int opid) |
| 200 | { |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 201 | if (opid != Py_EQ && opid != Py_NE) |
| 202 | Py_RETURN_NOTIMPLEMENTED; |
| 203 | |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 204 | if (PyObject_TypeCheck(_other, pysqlite_RowType)) { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 205 | pysqlite_Row *other = (pysqlite_Row *)_other; |
Serhiy Storchaka | 8debfa5 | 2019-09-16 20:15:18 +0300 | [diff] [blame] | 206 | int eq = PyObject_RichCompareBool(self->description, other->description, Py_EQ); |
| 207 | if (eq < 0) { |
| 208 | return NULL; |
| 209 | } |
| 210 | if (eq) { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 211 | return PyObject_RichCompare(self->data, other->data, opid); |
| 212 | } |
Serhiy Storchaka | 8debfa5 | 2019-09-16 20:15:18 +0300 | [diff] [blame] | 213 | return PyBool_FromLong(opid != Py_EQ); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 214 | } |
Brian Curtin | dfc80e3 | 2011-08-10 20:28:54 -0500 | [diff] [blame] | 215 | Py_RETURN_NOTIMPLEMENTED; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 218 | static PyMethodDef row_methods[] = { |
Erlend Egeberg Aasland | 84d79cf | 2020-12-29 14:22:55 +0100 | [diff] [blame] | 219 | PYSQLITE_ROW_KEYS_METHODDEF |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 220 | {NULL, NULL} |
| 221 | }; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 222 | |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 223 | static PyType_Slot row_slots[] = { |
| 224 | {Py_tp_dealloc, pysqlite_row_dealloc}, |
| 225 | {Py_tp_hash, pysqlite_row_hash}, |
| 226 | {Py_tp_methods, row_methods}, |
| 227 | {Py_tp_richcompare, pysqlite_row_richcompare}, |
| 228 | {Py_tp_iter, pysqlite_iter}, |
| 229 | {Py_mp_length, pysqlite_row_length}, |
| 230 | {Py_mp_subscript, pysqlite_row_subscript}, |
| 231 | {Py_sq_length, pysqlite_row_length}, |
| 232 | {Py_sq_item, pysqlite_row_item}, |
| 233 | {Py_tp_new, pysqlite_row_new}, |
| 234 | {0, NULL}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 237 | static PyType_Spec row_spec = { |
| 238 | .name = MODULE_NAME ".Row", |
| 239 | .basicsize = sizeof(pysqlite_Row), |
| 240 | .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 241 | .slots = row_slots, |
| 242 | }; |
| 243 | |
| 244 | PyTypeObject *pysqlite_RowType = NULL; |
| 245 | |
Erlend Egeberg Aasland | 38b6c2a | 2021-02-21 11:07:49 +0100 | [diff] [blame] | 246 | int |
| 247 | pysqlite_row_setup_types(PyObject *module) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 248 | { |
Erlend Egeberg Aasland | 9031bd4 | 2020-10-01 15:24:31 +0200 | [diff] [blame] | 249 | pysqlite_RowType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &row_spec, NULL); |
| 250 | if (pysqlite_RowType == NULL) { |
| 251 | return -1; |
| 252 | } |
| 253 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 254 | } |