blob: a8515794ff670e1c1194ec1fc5e3b6051d8e1517 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* row.c - an enhanced tuple for database rows
2 *
3 * Copyright (C) 2005-2006 Gerhard Häring <gh@ghaering.de>
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 Woutersfc7bb8c2007-01-15 15:49:28 +000028void pysqlite_row_dealloc(pysqlite_Row* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000029{
30 Py_XDECREF(self->data);
31 Py_XDECREF(self->description);
32
Christian Heimes90aa7642007-12-19 02:45:37 +000033 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000034}
35
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000036int pysqlite_row_init(pysqlite_Row* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000037{
38 PyObject* data;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000039 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000040
41 self->data = 0;
42 self->description = 0;
43
44 if (!PyArg_ParseTuple(args, "OO", &cursor, &data)) {
45 return -1;
46 }
47
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000048 if (!PyObject_IsInstance((PyObject*)cursor, (PyObject*)&pysqlite_CursorType)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000049 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 Woutersfc7bb8c2007-01-15 15:49:28 +000067PyObject* pysqlite_row_subscript(pysqlite_Row* self, PyObject* idx)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000068{
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 Brandle1a0d112007-10-23 19:24:22 +000079 if (PyLong_Check(idx)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000080 _idx = PyLong_AsLong(idx);
81 item = PyTuple_GetItem(self->data, _idx);
82 Py_XINCREF(item);
83 return item;
Gerhard Häring6d214562007-08-10 18:15:11 +000084 } else if (PyUnicode_Check(idx)) {
85 key = PyUnicode_AsString(idx);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000086
87 nitems = PyTuple_Size(self->description);
88
89 for (i = 0; i < nitems; i++) {
Guido van Rossum98297ee2007-11-06 21:34:58 +000090 compare_key = PyUnicode_AsString(PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000091 if (!compare_key) {
92 return NULL;
93 }
94
95 p1 = key;
96 p2 = compare_key;
97
98 while (1) {
99 if ((*p1 == (char)0) || (*p2 == (char)0)) {
100 break;
101 }
102
103 if ((*p1 | 0x20) != (*p2 | 0x20)) {
104 break;
105 }
106
107 p1++;
108 p2++;
109 }
110
111 if ((*p1 == (char)0) && (*p2 == (char)0)) {
112 /* found item */
113 item = PyTuple_GetItem(self->data, i);
114 Py_INCREF(item);
115 return item;
116 }
117
118 }
119
120 PyErr_SetString(PyExc_IndexError, "No item with that key");
121 return NULL;
122 } else if (PySlice_Check(idx)) {
123 PyErr_SetString(PyExc_ValueError, "slices not implemented, yet");
124 return NULL;
125 } else {
126 PyErr_SetString(PyExc_IndexError, "Index must be int or string");
127 return NULL;
128 }
129}
130
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000131Py_ssize_t pysqlite_row_length(pysqlite_Row* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000132{
133 return PyTuple_GET_SIZE(self->data);
134}
135
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000136PyObject* pysqlite_row_keys(pysqlite_Row* self, PyObject* args, PyObject* kwargs)
137{
138 PyObject* list;
139 int nitems, i;
140
141 list = PyList_New(0);
142 if (!list) {
143 return NULL;
144 }
145 nitems = PyTuple_Size(self->description);
146
147 for (i = 0; i < nitems; i++) {
148 if (PyList_Append(list, PyTuple_GET_ITEM(PyTuple_GET_ITEM(self->description, i), 0)) != 0) {
149 Py_DECREF(list);
150 return NULL;
151 }
152 }
153
154 return list;
155}
156
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000157static PyObject* pysqlite_iter(pysqlite_Row* self)
158{
159 return PyObject_GetIter(self->data);
160}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000161
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000162PyMappingMethods pysqlite_row_as_mapping = {
163 /* mp_length */ (lenfunc)pysqlite_row_length,
164 /* mp_subscript */ (binaryfunc)pysqlite_row_subscript,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000165 /* mp_ass_subscript */ (objobjargproc)0,
166};
167
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000168static PyMethodDef pysqlite_row_methods[] = {
169 {"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS,
170 PyDoc_STR("Returns the keys of the row.")},
171 {NULL, NULL}
172};
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000173
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000174
175PyTypeObject pysqlite_RowType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000176 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000177 MODULE_NAME ".Row", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000178 sizeof(pysqlite_Row), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000179 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000180 (destructor)pysqlite_row_dealloc, /* tp_dealloc */
Guido van Rossum346f1a82007-08-07 19:58:47 +0000181 0, /* tp_print */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000182 0, /* tp_getattr */
183 0, /* tp_setattr */
184 0, /* tp_compare */
185 0, /* tp_repr */
186 0, /* tp_as_number */
187 0, /* tp_as_sequence */
188 0, /* tp_as_mapping */
189 0, /* tp_hash */
190 0, /* tp_call */
191 0, /* tp_str */
192 0, /* tp_getattro */
193 0, /* tp_setattro */
194 0, /* tp_as_buffer */
195 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
196 0, /* tp_doc */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000197 (traverseproc)0, /* tp_traverse */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000198 0, /* tp_clear */
199 0, /* tp_richcompare */
200 0, /* tp_weaklistoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000201 (getiterfunc)pysqlite_iter, /* tp_iter */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000202 0, /* tp_iternext */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000203 pysqlite_row_methods, /* tp_methods */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000204 0, /* tp_members */
205 0, /* tp_getset */
206 0, /* tp_base */
207 0, /* tp_dict */
208 0, /* tp_descr_get */
209 0, /* tp_descr_set */
210 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000211 (initproc)pysqlite_row_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000212 0, /* tp_alloc */
213 0, /* tp_new */
214 0 /* tp_free */
215};
216
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000217extern int pysqlite_row_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000218{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000219 pysqlite_RowType.tp_new = PyType_GenericNew;
220 pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping;
221 return PyType_Ready(&pysqlite_RowType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000222}