Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1 | /* row.c - an enhanced tuple for database rows |
| 2 | * |
Gerhard Häring | 3bbb672 | 2010-03-05 09:12:37 +0000 | [diff] [blame] | 3 | * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de> |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +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" |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 26 | #include "sqlitecompat.h" |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 27 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 28 | void pysqlite_row_dealloc(pysqlite_Row* self) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 29 | { |
| 30 | Py_XDECREF(self->data); |
| 31 | Py_XDECREF(self->description); |
| 32 | |
Christian Heimes | e93237d | 2007-12-19 02:37:44 +0000 | [diff] [blame] | 33 | Py_TYPE(self)->tp_free((PyObject*)self); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Serhiy Storchaka | 501da1d | 2014-08-06 17:50:22 +0300 | [diff] [blame] | 36 | static PyObject * |
| 37 | pysqlite_row_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 38 | { |
Serhiy Storchaka | 501da1d | 2014-08-06 17:50:22 +0300 | [diff] [blame] | 39 | pysqlite_Row *self; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 40 | PyObject* data; |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 41 | pysqlite_Cursor* cursor; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 42 | |
Serhiy Storchaka | 501da1d | 2014-08-06 17:50:22 +0300 | [diff] [blame] | 43 | assert(type != NULL && type->tp_alloc != NULL); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 44 | |
Serhiy Storchaka | 501da1d | 2014-08-06 17:50:22 +0300 | [diff] [blame] | 45 | if (!_PyArg_NoKeywords("Row()", kwargs)) |
| 46 | return NULL; |
| 47 | if (!PyArg_ParseTuple(args, "OO", &cursor, &data)) |
| 48 | return NULL; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 49 | |
Serhiy Storchaka | 80cb186 | 2015-05-22 11:00:40 +0300 | [diff] [blame] | 50 | if (!PyObject_TypeCheck((PyObject*)cursor, &pysqlite_CursorType)) { |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 51 | PyErr_SetString(PyExc_TypeError, "instance of cursor required for first argument"); |
Serhiy Storchaka | 501da1d | 2014-08-06 17:50:22 +0300 | [diff] [blame] | 52 | return NULL; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | if (!PyTuple_Check(data)) { |
| 56 | PyErr_SetString(PyExc_TypeError, "tuple required for second argument"); |
Serhiy Storchaka | 501da1d | 2014-08-06 17:50:22 +0300 | [diff] [blame] | 57 | return NULL; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Serhiy Storchaka | 501da1d | 2014-08-06 17:50:22 +0300 | [diff] [blame] | 60 | self = (pysqlite_Row *) type->tp_alloc(type, 0); |
| 61 | if (self == NULL) |
| 62 | return NULL; |
| 63 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 64 | Py_INCREF(data); |
| 65 | self->data = data; |
| 66 | |
| 67 | Py_INCREF(cursor->description); |
| 68 | self->description = cursor->description; |
| 69 | |
Serhiy Storchaka | 501da1d | 2014-08-06 17:50:22 +0300 | [diff] [blame] | 70 | return (PyObject *) self; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Serhiy Storchaka | 30080fd | 2014-05-28 12:57:38 +0300 | [diff] [blame] | 73 | PyObject* pysqlite_row_item(pysqlite_Row* self, Py_ssize_t idx) |
| 74 | { |
| 75 | PyObject* item = PyTuple_GetItem(self->data, idx); |
| 76 | Py_XINCREF(item); |
| 77 | return item; |
| 78 | } |
| 79 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 80 | PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 81 | { |
Serhiy Storchaka | 30080fd | 2014-05-28 12:57:38 +0300 | [diff] [blame] | 82 | Py_ssize_t _idx; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 83 | char* key; |
| 84 | int nitems, i; |
| 85 | char* compare_key; |
| 86 | |
| 87 | char* p1; |
| 88 | char* p2; |
| 89 | |
| 90 | PyObject* item; |
| 91 | |
| 92 | if (PyInt_Check(idx)) { |
| 93 | _idx = PyInt_AsLong(idx); |
Serhiy Storchaka | 30080fd | 2014-05-28 12:57:38 +0300 | [diff] [blame] | 94 | if (_idx < 0) |
| 95 | _idx += PyTuple_GET_SIZE(self->data); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 96 | item = PyTuple_GetItem(self->data, _idx); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 97 | Py_XINCREF(item); |
| 98 | return item; |
| 99 | } else if (PyLong_Check(idx)) { |
Serhiy Storchaka | 30080fd | 2014-05-28 12:57:38 +0300 | [diff] [blame] | 100 | _idx = PyNumber_AsSsize_t(idx, PyExc_IndexError); |
| 101 | if (_idx == -1 && PyErr_Occurred()) |
| 102 | return NULL; |
| 103 | if (_idx < 0) |
| 104 | _idx += PyTuple_GET_SIZE(self->data); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 105 | item = PyTuple_GetItem(self->data, _idx); |
| 106 | Py_XINCREF(item); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 107 | return item; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 108 | } else if (PyString_Check(idx)) { |
| 109 | key = PyString_AsString(idx); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 110 | |
| 111 | nitems = PyTuple_Size(self->description); |
| 112 | |
| 113 | for (i = 0; i < nitems; i++) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 114 | compare_key = PyString_AsString(PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0)); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 115 | if (!compare_key) { |
| 116 | return NULL; |
| 117 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 118 | |
| 119 | p1 = key; |
| 120 | p2 = compare_key; |
| 121 | |
| 122 | while (1) { |
| 123 | if ((*p1 == (char)0) || (*p2 == (char)0)) { |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | if ((*p1 | 0x20) != (*p2 | 0x20)) { |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | p1++; |
| 132 | p2++; |
| 133 | } |
| 134 | |
| 135 | if ((*p1 == (char)0) && (*p2 == (char)0)) { |
| 136 | /* found item */ |
| 137 | item = PyTuple_GetItem(self->data, i); |
| 138 | Py_INCREF(item); |
| 139 | return item; |
| 140 | } |
| 141 | |
| 142 | } |
| 143 | |
| 144 | PyErr_SetString(PyExc_IndexError, "No item with that key"); |
| 145 | return NULL; |
| 146 | } else if (PySlice_Check(idx)) { |
| 147 | PyErr_SetString(PyExc_ValueError, "slices not implemented, yet"); |
| 148 | return NULL; |
| 149 | } else { |
| 150 | PyErr_SetString(PyExc_IndexError, "Index must be int or string"); |
| 151 | return NULL; |
| 152 | } |
| 153 | } |
| 154 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 155 | Py_ssize_t pysqlite_row_length(pysqlite_Row* self, PyObject* args, PyObject* kwargs) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 156 | { |
| 157 | return PyTuple_GET_SIZE(self->data); |
| 158 | } |
| 159 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 160 | PyObject* pysqlite_row_keys(pysqlite_Row* self, PyObject* args, PyObject* kwargs) |
| 161 | { |
| 162 | PyObject* list; |
| 163 | int nitems, i; |
| 164 | |
| 165 | list = PyList_New(0); |
| 166 | if (!list) { |
| 167 | return NULL; |
| 168 | } |
| 169 | nitems = PyTuple_Size(self->description); |
| 170 | |
| 171 | for (i = 0; i < nitems; i++) { |
| 172 | if (PyList_Append(list, PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0)) != 0) { |
| 173 | Py_DECREF(list); |
| 174 | return NULL; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return list; |
| 179 | } |
| 180 | |
| 181 | static int pysqlite_row_print(pysqlite_Row* self, FILE *fp, int flags) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 182 | { |
| 183 | return (&PyTuple_Type)->tp_print(self->data, fp, flags); |
| 184 | } |
| 185 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 186 | static PyObject* pysqlite_iter(pysqlite_Row* self) |
| 187 | { |
| 188 | return PyObject_GetIter(self->data); |
| 189 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 190 | |
Gerhard Häring | 5a366c3 | 2008-05-04 13:15:12 +0000 | [diff] [blame] | 191 | static long pysqlite_row_hash(pysqlite_Row *self) |
| 192 | { |
| 193 | return PyObject_Hash(self->description) ^ PyObject_Hash(self->data); |
| 194 | } |
| 195 | |
| 196 | static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other, int opid) |
| 197 | { |
| 198 | if (opid != Py_EQ && opid != Py_NE) { |
Gerhard Häring | 3bbb672 | 2010-03-05 09:12:37 +0000 | [diff] [blame] | 199 | Py_INCREF(Py_NotImplemented); |
| 200 | return Py_NotImplemented; |
Gerhard Häring | 5a366c3 | 2008-05-04 13:15:12 +0000 | [diff] [blame] | 201 | } |
| 202 | if (PyType_IsSubtype(Py_TYPE(_other), &pysqlite_RowType)) { |
Gerhard Häring | 3bbb672 | 2010-03-05 09:12:37 +0000 | [diff] [blame] | 203 | pysqlite_Row *other = (pysqlite_Row *)_other; |
| 204 | PyObject *res = PyObject_RichCompare(self->description, other->description, opid); |
| 205 | if ((opid == Py_EQ && res == Py_True) |
| 206 | || (opid == Py_NE && res == Py_False)) { |
| 207 | Py_DECREF(res); |
| 208 | return PyObject_RichCompare(self->data, other->data, opid); |
| 209 | } |
Gerhard Häring | 5a366c3 | 2008-05-04 13:15:12 +0000 | [diff] [blame] | 210 | } |
| 211 | Py_INCREF(Py_NotImplemented); |
| 212 | return Py_NotImplemented; |
| 213 | } |
| 214 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 215 | PyMappingMethods pysqlite_row_as_mapping = { |
| 216 | /* mp_length */ (lenfunc)pysqlite_row_length, |
| 217 | /* mp_subscript */ (binaryfunc)pysqlite_row_subscript, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 218 | /* mp_ass_subscript */ (objobjargproc)0, |
| 219 | }; |
| 220 | |
Serhiy Storchaka | 30080fd | 2014-05-28 12:57:38 +0300 | [diff] [blame] | 221 | static PySequenceMethods pysqlite_row_as_sequence = { |
| 222 | /* sq_length */ (lenfunc)pysqlite_row_length, |
| 223 | /* sq_concat */ 0, |
| 224 | /* sq_repeat */ 0, |
| 225 | /* sq_item */ (ssizeargfunc)pysqlite_row_item, |
| 226 | }; |
| 227 | |
| 228 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 229 | static PyMethodDef pysqlite_row_methods[] = { |
| 230 | {"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS, |
| 231 | PyDoc_STR("Returns the keys of the row.")}, |
| 232 | {NULL, NULL} |
| 233 | }; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 234 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 235 | |
| 236 | PyTypeObject pysqlite_RowType = { |
Martin v. Löwis | 6819210 | 2007-07-21 06:55:02 +0000 | [diff] [blame] | 237 | PyVarObject_HEAD_INIT(NULL, 0) |
Anthony Baxter | 8e7b490 | 2006-04-05 18:25:33 +0000 | [diff] [blame] | 238 | MODULE_NAME ".Row", /* tp_name */ |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 239 | sizeof(pysqlite_Row), /* tp_basicsize */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 240 | 0, /* tp_itemsize */ |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 241 | (destructor)pysqlite_row_dealloc, /* tp_dealloc */ |
| 242 | (printfunc)pysqlite_row_print, /* tp_print */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 243 | 0, /* tp_getattr */ |
| 244 | 0, /* tp_setattr */ |
| 245 | 0, /* tp_compare */ |
| 246 | 0, /* tp_repr */ |
| 247 | 0, /* tp_as_number */ |
| 248 | 0, /* tp_as_sequence */ |
| 249 | 0, /* tp_as_mapping */ |
Gerhard Häring | 5a366c3 | 2008-05-04 13:15:12 +0000 | [diff] [blame] | 250 | (hashfunc)pysqlite_row_hash, /* tp_hash */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 251 | 0, /* tp_call */ |
| 252 | 0, /* tp_str */ |
| 253 | 0, /* tp_getattro */ |
| 254 | 0, /* tp_setattro */ |
| 255 | 0, /* tp_as_buffer */ |
| 256 | Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 257 | 0, /* tp_doc */ |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 258 | (traverseproc)0, /* tp_traverse */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 259 | 0, /* tp_clear */ |
Gerhard Häring | 5a366c3 | 2008-05-04 13:15:12 +0000 | [diff] [blame] | 260 | (richcmpfunc)pysqlite_row_richcompare, /* tp_richcompare */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 261 | 0, /* tp_weaklistoffset */ |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 262 | (getiterfunc)pysqlite_iter, /* tp_iter */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 263 | 0, /* tp_iternext */ |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 264 | pysqlite_row_methods, /* tp_methods */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 265 | 0, /* tp_members */ |
| 266 | 0, /* tp_getset */ |
| 267 | 0, /* tp_base */ |
| 268 | 0, /* tp_dict */ |
| 269 | 0, /* tp_descr_get */ |
| 270 | 0, /* tp_descr_set */ |
| 271 | 0, /* tp_dictoffset */ |
Serhiy Storchaka | 501da1d | 2014-08-06 17:50:22 +0300 | [diff] [blame] | 272 | 0, /* tp_init */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 273 | 0, /* tp_alloc */ |
| 274 | 0, /* tp_new */ |
| 275 | 0 /* tp_free */ |
| 276 | }; |
| 277 | |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 278 | extern int pysqlite_row_setup_types(void) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 279 | { |
Serhiy Storchaka | 501da1d | 2014-08-06 17:50:22 +0300 | [diff] [blame] | 280 | pysqlite_RowType.tp_new = pysqlite_row_new; |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 281 | pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping; |
Serhiy Storchaka | 30080fd | 2014-05-28 12:57:38 +0300 | [diff] [blame] | 282 | pysqlite_RowType.tp_as_sequence = &pysqlite_row_as_sequence; |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 283 | return PyType_Ready(&pysqlite_RowType); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 284 | } |