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