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" |
| 26 | #include "sqlitecompat.h" |
| 27 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 28 | void pysqlite_row_dealloc(pysqlite_Row* self) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 29 | { |
| 30 | Py_XDECREF(self->data); |
| 31 | Py_XDECREF(self->description); |
| 32 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 33 | Py_TYPE(self)->tp_free((PyObject*)self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 36 | int pysqlite_row_init(pysqlite_Row* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 37 | { |
| 38 | PyObject* data; |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 39 | pysqlite_Cursor* cursor; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +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 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 48 | if (!PyObject_IsInstance((PyObject*)cursor, (PyObject*)&pysqlite_CursorType)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +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 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 67 | PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 68 | { |
| 69 | long _idx; |
| 70 | char* key; |
| 71 | int nitems, i; |
| 72 | char* compare_key; |
| 73 | |
| 74 | char* p1; |
| 75 | char* p2; |
| 76 | |
| 77 | PyObject* item; |
| 78 | |
Georg Brandl | e1a0d11 | 2007-10-23 19:24:22 +0000 | [diff] [blame] | 79 | if (PyLong_Check(idx)) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 80 | _idx = PyLong_AsLong(idx); |
| 81 | item = PyTuple_GetItem(self->data, _idx); |
| 82 | Py_XINCREF(item); |
| 83 | return item; |
Gerhard Häring | 6d21456 | 2007-08-10 18:15:11 +0000 | [diff] [blame] | 84 | } else if (PyUnicode_Check(idx)) { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 85 | key = _PyUnicode_AsString(idx); |
Victor Stinner | 8699950 | 2010-05-19 01:27:23 +0000 | [diff] [blame] | 86 | if (key == NULL) |
| 87 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 88 | |
| 89 | nitems = PyTuple_Size(self->description); |
| 90 | |
| 91 | for (i = 0; i < nitems; i++) { |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 92 | compare_key = _PyUnicode_AsString(PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0)); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 93 | if (!compare_key) { |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | p1 = key; |
| 98 | p2 = compare_key; |
| 99 | |
| 100 | while (1) { |
| 101 | if ((*p1 == (char)0) || (*p2 == (char)0)) { |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | if ((*p1 | 0x20) != (*p2 | 0x20)) { |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | p1++; |
| 110 | p2++; |
| 111 | } |
| 112 | |
| 113 | if ((*p1 == (char)0) && (*p2 == (char)0)) { |
| 114 | /* found item */ |
| 115 | item = PyTuple_GetItem(self->data, i); |
| 116 | Py_INCREF(item); |
| 117 | return item; |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | |
| 122 | PyErr_SetString(PyExc_IndexError, "No item with that key"); |
| 123 | return NULL; |
| 124 | } else if (PySlice_Check(idx)) { |
| 125 | PyErr_SetString(PyExc_ValueError, "slices not implemented, yet"); |
| 126 | return NULL; |
| 127 | } else { |
| 128 | PyErr_SetString(PyExc_IndexError, "Index must be int or string"); |
| 129 | return NULL; |
| 130 | } |
| 131 | } |
| 132 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 133 | Py_ssize_t pysqlite_row_length(pysqlite_Row* self, PyObject* args, PyObject* kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 134 | { |
| 135 | return PyTuple_GET_SIZE(self->data); |
| 136 | } |
| 137 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 138 | PyObject* pysqlite_row_keys(pysqlite_Row* self, PyObject* args, PyObject* kwargs) |
| 139 | { |
| 140 | PyObject* list; |
| 141 | int nitems, i; |
| 142 | |
| 143 | list = PyList_New(0); |
| 144 | if (!list) { |
| 145 | return NULL; |
| 146 | } |
| 147 | nitems = PyTuple_Size(self->description); |
| 148 | |
| 149 | for (i = 0; i < nitems; i++) { |
| 150 | if (PyList_Append(list, PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0)) != 0) { |
| 151 | Py_DECREF(list); |
| 152 | return NULL; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return list; |
| 157 | } |
| 158 | |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 159 | static int pysqlite_row_print(pysqlite_Row* self, FILE *fp, int flags) |
| 160 | { |
| 161 | return (&PyTuple_Type)->tp_print(self->data, fp, flags); |
| 162 | } |
| 163 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 164 | static PyObject* pysqlite_iter(pysqlite_Row* self) |
| 165 | { |
| 166 | return PyObject_GetIter(self->data); |
| 167 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 168 | |
Georg Brandl | 016cec7 | 2010-10-18 12:24:53 +0000 | [diff] [blame] | 169 | static Py_hash_t pysqlite_row_hash(pysqlite_Row *self) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 170 | { |
| 171 | return PyObject_Hash(self->description) ^ PyObject_Hash(self->data); |
| 172 | } |
| 173 | |
| 174 | static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other, int opid) |
| 175 | { |
| 176 | if (opid != Py_EQ && opid != Py_NE) { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 177 | Py_INCREF(Py_NotImplemented); |
| 178 | return Py_NotImplemented; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 179 | } |
| 180 | if (PyType_IsSubtype(Py_TYPE(_other), &pysqlite_RowType)) { |
Gerhard Häring | f9cee22 | 2010-03-05 15:20:03 +0000 | [diff] [blame] | 181 | pysqlite_Row *other = (pysqlite_Row *)_other; |
| 182 | PyObject *res = PyObject_RichCompare(self->description, other->description, opid); |
| 183 | if ((opid == Py_EQ && res == Py_True) |
| 184 | || (opid == Py_NE && res == Py_False)) { |
| 185 | Py_DECREF(res); |
| 186 | return PyObject_RichCompare(self->data, other->data, opid); |
| 187 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 188 | } |
| 189 | Py_INCREF(Py_NotImplemented); |
| 190 | return Py_NotImplemented; |
| 191 | } |
| 192 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 193 | PyMappingMethods pysqlite_row_as_mapping = { |
| 194 | /* mp_length */ (lenfunc)pysqlite_row_length, |
| 195 | /* mp_subscript */ (binaryfunc)pysqlite_row_subscript, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 196 | /* mp_ass_subscript */ (objobjargproc)0, |
| 197 | }; |
| 198 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 199 | static PyMethodDef pysqlite_row_methods[] = { |
| 200 | {"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS, |
| 201 | PyDoc_STR("Returns the keys of the row.")}, |
| 202 | {NULL, NULL} |
| 203 | }; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 204 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 205 | |
| 206 | PyTypeObject pysqlite_RowType = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 207 | PyVarObject_HEAD_INIT(NULL, 0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 208 | MODULE_NAME ".Row", /* tp_name */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 209 | sizeof(pysqlite_Row), /* tp_basicsize */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 210 | 0, /* tp_itemsize */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 211 | (destructor)pysqlite_row_dealloc, /* tp_dealloc */ |
Gerhard Häring | e7ea745 | 2008-03-29 00:45:29 +0000 | [diff] [blame] | 212 | (printfunc)pysqlite_row_print, /* tp_print */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 213 | 0, /* tp_getattr */ |
| 214 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 215 | 0, /* tp_reserved */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 216 | 0, /* tp_repr */ |
| 217 | 0, /* tp_as_number */ |
| 218 | 0, /* tp_as_sequence */ |
| 219 | 0, /* tp_as_mapping */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 220 | (hashfunc)pysqlite_row_hash, /* tp_hash */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 221 | 0, /* tp_call */ |
| 222 | 0, /* tp_str */ |
| 223 | 0, /* tp_getattro */ |
| 224 | 0, /* tp_setattro */ |
| 225 | 0, /* tp_as_buffer */ |
| 226 | Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 227 | 0, /* tp_doc */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 228 | (traverseproc)0, /* tp_traverse */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 229 | 0, /* tp_clear */ |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 230 | (richcmpfunc)pysqlite_row_richcompare, /* tp_richcompare */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 231 | 0, /* tp_weaklistoffset */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 232 | (getiterfunc)pysqlite_iter, /* tp_iter */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 233 | 0, /* tp_iternext */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 234 | pysqlite_row_methods, /* tp_methods */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 235 | 0, /* tp_members */ |
| 236 | 0, /* tp_getset */ |
| 237 | 0, /* tp_base */ |
| 238 | 0, /* tp_dict */ |
| 239 | 0, /* tp_descr_get */ |
| 240 | 0, /* tp_descr_set */ |
| 241 | 0, /* tp_dictoffset */ |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 242 | (initproc)pysqlite_row_init, /* tp_init */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 243 | 0, /* tp_alloc */ |
| 244 | 0, /* tp_new */ |
| 245 | 0 /* tp_free */ |
| 246 | }; |
| 247 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 248 | extern int pysqlite_row_setup_types(void) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 249 | { |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 250 | pysqlite_RowType.tp_new = PyType_GenericNew; |
| 251 | pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping; |
| 252 | return PyType_Ready(&pysqlite_RowType); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 253 | } |