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