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

Add ability to create deterministic user-defined functions #100

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
7 changes: 6 additions & 1 deletion doc/sphinx/sqlite3.rst
Expand Up @@ -280,13 +280,18 @@ Connection Objects
given.


.. method:: Connection.create_function(name, num_params, func)
.. method:: Connection.create_function(name, num_params, func, [deterministic])

Creates a user-defined function that you can later use from within SQL
statements under the function name *name*. *num_params* is the number of
parameters the function accepts, and *func* is a Python callable that is called
as the SQL function.

*deterministic* defaults to :const:`False`, when set to :const:`True` this
indicates the function always returns the same output for any given
input. This allows SQLite to use the function in indices and
avoid redundantly evaluating the function when executing queries.

The function can return any of the types supported by SQLite: unicode, str, int,
long, float, buffer and None.

Expand Down
23 changes: 19 additions & 4 deletions src/connection.c
Expand Up @@ -805,24 +805,39 @@ static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)

PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
{
static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
static char *kwlist[] = {"name", "narg", "func", "deterministic", NULL, NULL};

PyObject* func;
char* name;
int narg;
int rc;
int deterministic = 0;
int eTextRep = SQLITE_UTF8;

if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
return NULL;
}

if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
&name, &narg, &func))
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|p", kwlist,
&name, &narg, &func, &deterministic))
{
return NULL;
}

rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
#ifndef SQLITE_DETERMINISTIC
#define SQLITE_DETERMINISTIC 0x800
#endif

if (deterministic) {
if (sqlite3_libversion() < 3008003) {
PyErr_SetString(pysqlite_OperationalError, "deterministic not supported in this version of SQLite, need 3.8.3 or higher");
return NULL;
}

eTextRep = eTextRep | SQLITE_DETERMINISTIC;
}

rc = sqlite3_create_function(self->db, name, narg, eTextRep, (void*)func, _pysqlite_func_callback, NULL, NULL);

if (rc != SQLITE_OK) {
/* Workaround for SQLite bug: no error code or string is available here */
Expand Down