Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return SQLite datatype. Return decltypes if flag is set. #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions doc/sphinx/sqlite3.rst
Expand Up @@ -588,9 +588,20 @@ A :class:`Cursor` instance has the following attributes and methods:

.. attribute:: Cursor.description

This read-only attribute provides the column names of the last query. To
remain compatible with the Python DB API, it returns a 7-tuple for each
column where the last six items of each tuple are :const:`None`.
This read-only attribute provides the column names of the last query, and
the SQLite datatype. To remain compatible with the Python DB API, it returns
a 7-tuple for each column where the last five items of each tuple are
:const:`None`. Please consult the SQLite documentation for datatype
values and information on the handling of column datatypes:
http://www.sqlite.org/c3ref/c_blob.html
http://www.sqlite.org/c3ref/column_blob.html

It is set for ``SELECT`` statements without any matching rows as well.

.. attribute:: Cursor.decltypes

If the :const:`PARSE_DECLTYPES` flag is set, this read-only attribute provides
the declared column types of the last query. Otherwise, it is :const:`None`.

It is set for ``SELECT`` statements without any matching rows as well.

Expand Down
52 changes: 51 additions & 1 deletion src/cursor.c
Expand Up @@ -60,6 +60,9 @@ static int pysqlite_cursor_init(pysqlite_Cursor* self, PyObject* args, PyObject*
Py_INCREF(Py_None);
self->description = Py_None;

Py_INCREF(Py_None);
self->decltypes = Py_None;

Py_INCREF(Py_None);
self->lastrowid= Py_None;

Expand Down Expand Up @@ -93,6 +96,7 @@ static void pysqlite_cursor_dealloc(pysqlite_Cursor* self)
Py_XDECREF(self->connection);
Py_XDECREF(self->row_cast_map);
Py_XDECREF(self->description);
Py_XDECREF(self->decltypes);
Py_XDECREF(self->lastrowid);
Py_XDECREF(self->next_row);

Expand All @@ -119,6 +123,46 @@ PyObject* _pysqlite_get_converter(PyObject* key)
return retval;
}


int pysqlite_build_decltypes(pysqlite_Cursor* self){
int i, numcols;
const char* decltype;
const char* pos;
PyObject* py_decltype;

if(!self->connection->detect_types & PARSE_DECLTYPES){
return 0;
}

numcols = sqlite3_column_count(self->statement->st);
Py_XDECREF(self->decltypes);
self->decltypes = PyTuple_New(numcols);
if (!self->decltypes) {
return -1;
}

for (i = 0; i < sqlite3_column_count(self->statement->st); i++) {
decltype = sqlite3_column_decltype(self->statement->st, i);
for (pos = decltype;;pos++) {
/* Converter names are split at '(' and blanks.
* This allows 'INTEGER NOT NULL' to be treated as 'INTEGER' and
* 'NUMBER(10)' to be treated as 'NUMBER', for example.
* In other words, it will work as people expect it to work.*/
if (*pos == ' ' || *pos == '(' || *pos == 0) {
py_decltype = PyString_FromStringAndSize(decltype, pos - decltype);
if (!py_decltype) {
return -1;
}
break;
}
}

PyTuple_SetItem(self->decltypes, i, py_decltype);

}
return 0;
}

int pysqlite_build_row_cast_map(pysqlite_Cursor* self)
{
int i;
Expand Down Expand Up @@ -574,6 +618,11 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
goto error;
}

if (pysqlite_build_decltypes(self) != 0) {
PyErr_SetString(pysqlite_OperationalError, "Error while building decltypes");
goto error;
}

if (pysqlite_build_row_cast_map(self) != 0) {
PyErr_SetString(pysqlite_OperationalError, "Error while building row_cast_map");
goto error;
Expand All @@ -596,7 +645,7 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject*
goto error;
}
PyTuple_SetItem(descriptor, 0, _pysqlite_build_column_name(sqlite3_column_name(self->statement->st, i)));
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 1, Py_None);
PyTuple_SetItem(descriptor, 1, PyInt_FromLong((long) sqlite3_column_type(self->statement->st, i)));
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 2, Py_None);
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 3, Py_None);
Py_INCREF(Py_None); PyTuple_SetItem(descriptor, 4, Py_None);
Expand Down Expand Up @@ -946,6 +995,7 @@ static struct PyMemberDef cursor_members[] =
{
{"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), RO},
{"description", T_OBJECT, offsetof(pysqlite_Cursor, description), RO},
{"decltypes", T_OBJECT, offsetof(pysqlite_Cursor, decltypes), RO},
{"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
{"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), RO},
{"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), RO},
Expand Down
1 change: 1 addition & 0 deletions src/cursor.h
Expand Up @@ -34,6 +34,7 @@ typedef struct
PyObject_HEAD
pysqlite_Connection* connection;
PyObject* description;
PyObject* decltypes;
PyObject* row_cast_map;
int arraysize;
PyObject* lastrowid;
Expand Down