| 1 | /* row.c - an enhanced tuple for database rows
|
|---|
| 2 | *
|
|---|
| 3 | * Copyright (C) 2005-2006 Gerhard Häring <[email protected]>
|
|---|
| 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 |
|
|---|
| 28 | void row_dealloc(Row* self)
|
|---|
| 29 | {
|
|---|
| 30 | Py_XDECREF(self->data);
|
|---|
| 31 | Py_XDECREF(self->description);
|
|---|
| 32 |
|
|---|
| 33 | self->ob_type->tp_free((PyObject*)self);
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | int row_init(Row* self, PyObject* args, PyObject* kwargs)
|
|---|
| 37 | {
|
|---|
| 38 | PyObject* data;
|
|---|
| 39 | Cursor* cursor;
|
|---|
| 40 |
|
|---|
| 41 | self->data = 0;
|
|---|
| 42 | self->description = 0;
|
|---|
| 43 |
|
|---|
| 44 | if (!PyArg_ParseTuple(args, "OO", &cursor, &data)) {
|
|---|
| 45 | return -1;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | if (!PyObject_IsInstance((PyObject*)cursor, (PyObject*)&CursorType)) {
|
|---|
| 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 |
|
|---|
| 67 | PyObject* row_subscript(Row* self, PyObject* idx)
|
|---|
| 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 |
|
|---|
| 79 | if (PyInt_Check(idx)) {
|
|---|
| 80 | _idx = PyInt_AsLong(idx);
|
|---|
| 81 | item = PyTuple_GetItem(self->data, _idx);
|
|---|
| 82 | Py_XINCREF(item);
|
|---|
| 83 | return item;
|
|---|
| 84 | } else if (PyLong_Check(idx)) {
|
|---|
| 85 | _idx = PyLong_AsLong(idx);
|
|---|
| 86 | item = PyTuple_GetItem(self->data, _idx);
|
|---|
| 87 | Py_XINCREF(item);
|
|---|
| 88 | return item;
|
|---|
| 89 | } else if (PyString_Check(idx)) {
|
|---|
| 90 | key = PyString_AsString(idx);
|
|---|
| 91 |
|
|---|
| 92 | nitems = PyTuple_Size(self->description);
|
|---|
| 93 |
|
|---|
| 94 | for (i = 0; i < nitems; i++) {
|
|---|
| 95 | compare_key = PyString_AsString(PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0));
|
|---|
| 96 | if (!compare_key) {
|
|---|
| 97 | return NULL;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | p1 = key;
|
|---|
| 101 | p2 = compare_key;
|
|---|
| 102 |
|
|---|
| 103 | while (1) {
|
|---|
| 104 | if ((*p1 == (char)0) || (*p2 == (char)0)) {
|
|---|
| 105 | break;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | if ((*p1 | 0x20) != (*p2 | 0x20)) {
|
|---|
| 109 | break;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | p1++;
|
|---|
| 113 | p2++;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | if ((*p1 == (char)0) && (*p2 == (char)0)) {
|
|---|
| 117 | /* found item */
|
|---|
| 118 | item = PyTuple_GetItem(self->data, i);
|
|---|
| 119 | Py_INCREF(item);
|
|---|
| 120 | return item;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | PyErr_SetString(PyExc_IndexError, "No item with that key");
|
|---|
| 126 | return NULL;
|
|---|
| 127 | } else if (PySlice_Check(idx)) {
|
|---|
| 128 | PyErr_SetString(PyExc_ValueError, "slices not implemented, yet");
|
|---|
| 129 | return NULL;
|
|---|
| 130 | } else {
|
|---|
| 131 | PyErr_SetString(PyExc_IndexError, "Index must be int or string");
|
|---|
| 132 | return NULL;
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | Py_ssize_t row_length(Row* self, PyObject* args, PyObject* kwargs)
|
|---|
| 137 | {
|
|---|
| 138 | return PyTuple_GET_SIZE(self->data);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | static int row_print(Row* self, FILE *fp, int flags)
|
|---|
| 142 | {
|
|---|
| 143 | return (&PyTuple_Type)->tp_print(self->data, fp, flags);
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 | PyMappingMethods row_as_mapping = {
|
|---|
| 148 | /* mp_length */ (lenfunc)row_length,
|
|---|
| 149 | /* mp_subscript */ (binaryfunc)row_subscript,
|
|---|
| 150 | /* mp_ass_subscript */ (objobjargproc)0,
|
|---|
| 151 | };
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 | PyTypeObject RowType = {
|
|---|
| 155 | PyObject_HEAD_INIT(NULL)
|
|---|
| 156 | 0, /* ob_size */
|
|---|
| 157 | MODULE_NAME ".Row", /* tp_name */
|
|---|
| 158 | sizeof(Row), /* tp_basicsize */
|
|---|
| 159 | 0, /* tp_itemsize */
|
|---|
| 160 | (destructor)row_dealloc, /* tp_dealloc */
|
|---|
| 161 | (printfunc)row_print, /* tp_print */
|
|---|
| 162 | 0, /* tp_getattr */
|
|---|
| 163 | 0, /* tp_setattr */
|
|---|
| 164 | 0, /* tp_compare */
|
|---|
| 165 | 0, /* tp_repr */
|
|---|
| 166 | 0, /* tp_as_number */
|
|---|
| 167 | 0, /* tp_as_sequence */
|
|---|
| 168 | 0, /* tp_as_mapping */
|
|---|
| 169 | 0, /* tp_hash */
|
|---|
| 170 | 0, /* tp_call */
|
|---|
| 171 | 0, /* tp_str */
|
|---|
| 172 | 0, /* tp_getattro */
|
|---|
| 173 | 0, /* tp_setattro */
|
|---|
| 174 | 0, /* tp_as_buffer */
|
|---|
| 175 | Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|---|
| 176 | 0, /* tp_doc */
|
|---|
| 177 | 0, /* tp_traverse */
|
|---|
| 178 | 0, /* tp_clear */
|
|---|
| 179 | 0, /* tp_richcompare */
|
|---|
| 180 | 0, /* tp_weaklistoffset */
|
|---|
| 181 | 0, /* tp_iter */
|
|---|
| 182 | 0, /* tp_iternext */
|
|---|
| 183 | 0, /* tp_methods */
|
|---|
| 184 | 0, /* tp_members */
|
|---|
| 185 | 0, /* tp_getset */
|
|---|
| 186 | 0, /* tp_base */
|
|---|
| 187 | 0, /* tp_dict */
|
|---|
| 188 | 0, /* tp_descr_get */
|
|---|
| 189 | 0, /* tp_descr_set */
|
|---|
| 190 | 0, /* tp_dictoffset */
|
|---|
| 191 | (initproc)row_init, /* tp_init */
|
|---|
| 192 | 0, /* tp_alloc */
|
|---|
| 193 | 0, /* tp_new */
|
|---|
| 194 | 0 /* tp_free */
|
|---|
| 195 | };
|
|---|
| 196 |
|
|---|
| 197 | extern int row_setup_types(void)
|
|---|
| 198 | {
|
|---|
| 199 | RowType.tp_new = PyType_GenericNew;
|
|---|
| 200 | RowType.tp_as_mapping = &row_as_mapping;
|
|---|
| 201 | return PyType_Ready(&RowType);
|
|---|
| 202 | }
|
|---|