Back to index

python3.2  3.2.2
Classes | Defines | Functions | Variables
statement.h File Reference
#include "Python.h"
#include "connection.h"
#include "sqlite3.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  pysqlite_Statement

Defines

#define PYSQLITE_TOO_MUCH_SQL   (-100)
#define PYSQLITE_SQL_WRONG_TYPE   (-101)

Functions

int pysqlite_statement_create (pysqlite_Statement *self, pysqlite_Connection *connection, PyObject *sql)
void pysqlite_statement_dealloc (pysqlite_Statement *self)
int pysqlite_statement_bind_parameter (pysqlite_Statement *self, int pos, PyObject *parameter, int allow_8bit_chars)
void pysqlite_statement_bind_parameters (pysqlite_Statement *self, PyObject *parameters, int allow_8bit_chars)
int pysqlite_statement_recompile (pysqlite_Statement *self, PyObject *parameters)
int pysqlite_statement_finalize (pysqlite_Statement *self)
int pysqlite_statement_reset (pysqlite_Statement *self)
void pysqlite_statement_mark_dirty (pysqlite_Statement *self)
int pysqlite_statement_setup_types (void)

Variables

PyTypeObject pysqlite_StatementType

Class Documentation

struct pysqlite_Statement

Definition at line 34 of file statement.h.

Collaboration diagram for pysqlite_Statement:
Class Members
PyObject_HEAD sqlite3 * db
int in_use
PyObject * in_weakreflist
PyObject * sql
sqlite3_stmt * st

Define Documentation

#define PYSQLITE_SQL_WRONG_TYPE   (-101)

Definition at line 32 of file statement.h.

#define PYSQLITE_TOO_MUCH_SQL   (-100)

Definition at line 31 of file statement.h.


Function Documentation

int pysqlite_statement_bind_parameter ( pysqlite_Statement self,
int  pos,
PyObject parameter,
int  allow_8bit_chars 
)

Definition at line 90 of file statement.c.

{
    int rc = SQLITE_OK;
    PY_LONG_LONG longlongval;
    const char* buffer;
    char* string;
    Py_ssize_t buflen;
    parameter_type paramtype;

    if (parameter == Py_None) {
        rc = sqlite3_bind_null(self->st, pos);
        goto final;
    }

    if (PyLong_CheckExact(parameter)) {
        paramtype = TYPE_LONG;
    } else if (PyFloat_CheckExact(parameter)) {
        paramtype = TYPE_FLOAT;
    } else if (PyUnicode_CheckExact(parameter)) {
        paramtype = TYPE_UNICODE;
    } else if (PyLong_Check(parameter)) {
        paramtype = TYPE_LONG;
    } else if (PyFloat_Check(parameter)) {
        paramtype = TYPE_FLOAT;
    } else if (PyUnicode_Check(parameter)) {
        paramtype = TYPE_UNICODE;
    } else if (PyObject_CheckBuffer(parameter)) {
        paramtype = TYPE_BUFFER;
    } else {
        paramtype = TYPE_UNKNOWN;
    }

    switch (paramtype) {
        case TYPE_LONG:
            /* in the overflow error case, longval/longlongval is -1, and an exception is set */
            longlongval = PyLong_AsLongLong(parameter);
            rc = sqlite3_bind_int64(self->st, pos, (sqlite_int64)longlongval);
            break;
        case TYPE_FLOAT:
            rc = sqlite3_bind_double(self->st, pos, PyFloat_AsDouble(parameter));
            break;
        case TYPE_UNICODE:
            string = _PyUnicode_AsString(parameter);
            if (string != NULL)
                rc = sqlite3_bind_text(self->st, pos, string, -1, SQLITE_TRANSIENT);
            else
                rc = -1;
            break;
        case TYPE_BUFFER:
            if (PyObject_AsCharBuffer(parameter, &buffer, &buflen) == 0) {
                rc = sqlite3_bind_blob(self->st, pos, buffer, buflen, SQLITE_TRANSIENT);
            } else {
                PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
                rc = -1;
            }
            break;
        case TYPE_UNKNOWN:
            rc = -1;
    }

final:
    return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void pysqlite_statement_bind_parameters ( pysqlite_Statement self,
PyObject parameters,
int  allow_8bit_chars 
)

Definition at line 169 of file statement.c.

{
    PyObject* current_param;
    PyObject* adapted;
    const char* binding_name;
    int i;
    int rc;
    int num_params_needed;
    int num_params;

    Py_BEGIN_ALLOW_THREADS
    num_params_needed = sqlite3_bind_parameter_count(self->st);
    Py_END_ALLOW_THREADS

    if (PyTuple_CheckExact(parameters) || PyList_CheckExact(parameters) || (!PyDict_Check(parameters) && PySequence_Check(parameters))) {
        /* parameters passed as sequence */
        if (PyTuple_CheckExact(parameters)) {
            num_params = PyTuple_GET_SIZE(parameters);
        } else if (PyList_CheckExact(parameters)) {
            num_params = PyList_GET_SIZE(parameters);
        } else {
            num_params = PySequence_Size(parameters);
        }
        if (num_params != num_params_needed) {
            PyErr_Format(pysqlite_ProgrammingError, "Incorrect number of bindings supplied. The current statement uses %d, and there are %d supplied.",
                         num_params_needed, num_params);
            return;
        }
        for (i = 0; i < num_params; i++) {
            if (PyTuple_CheckExact(parameters)) {
                current_param = PyTuple_GET_ITEM(parameters, i);
                Py_XINCREF(current_param);
            } else if (PyList_CheckExact(parameters)) {
                current_param = PyList_GET_ITEM(parameters, i);
                Py_XINCREF(current_param);
            } else {
                current_param = PySequence_GetItem(parameters, i);
            }
            if (!current_param) {
                return;
            }

            if (!_need_adapt(current_param)) {
                adapted = current_param;
            } else {
                adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
                if (adapted) {
                    Py_DECREF(current_param);
                } else {
                    PyErr_Clear();
                    adapted = current_param;
                }
            }

            rc = pysqlite_statement_bind_parameter(self, i + 1, adapted, allow_8bit_chars);
            Py_DECREF(adapted);

            if (rc != SQLITE_OK) {
                if (!PyErr_Occurred()) {
                    PyErr_Format(pysqlite_InterfaceError, "Error binding parameter %d - probably unsupported type.", i);
                }
                return;
            }
        }
    } else if (PyDict_Check(parameters)) {
        /* parameters passed as dictionary */
        for (i = 1; i <= num_params_needed; i++) {
            Py_BEGIN_ALLOW_THREADS
            binding_name = sqlite3_bind_parameter_name(self->st, i);
            Py_END_ALLOW_THREADS
            if (!binding_name) {
                PyErr_Format(pysqlite_ProgrammingError, "Binding %d has no name, but you supplied a dictionary (which has only names).", i);
                return;
            }

            binding_name++; /* skip first char (the colon) */
            if (PyDict_CheckExact(parameters)) {
                current_param = PyDict_GetItemString(parameters, binding_name);
                Py_XINCREF(current_param);
            } else {
                current_param = PyMapping_GetItemString(parameters, (char*)binding_name);
            }
            if (!current_param) {
                PyErr_Format(pysqlite_ProgrammingError, "You did not supply a value for binding %d.", i);
                return;
            }

            if (!_need_adapt(current_param)) {
                adapted = current_param;
            } else {
                adapted = pysqlite_microprotocols_adapt(current_param, (PyObject*)&pysqlite_PrepareProtocolType, NULL);
                if (adapted) {
                    Py_DECREF(current_param);
                } else {
                    PyErr_Clear();
                    adapted = current_param;
                }
            }

            rc = pysqlite_statement_bind_parameter(self, i, adapted, allow_8bit_chars);
            Py_DECREF(adapted);

            if (rc != SQLITE_OK) {
                if (!PyErr_Occurred()) {
                    PyErr_Format(pysqlite_InterfaceError, "Error binding parameter :%s - probably unsupported type.", binding_name);
                }
                return;
           }
        }
    } else {
        PyErr_SetString(PyExc_ValueError, "parameters are of unsupported type");
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 51 of file statement.c.

{
    const char* tail;
    int rc;
    const char* sql_cstr;
    Py_ssize_t sql_cstr_len;

    self->st = NULL;
    self->in_use = 0;

    sql_cstr = _PyUnicode_AsStringAndSize(sql, &sql_cstr_len);
    if (sql_cstr == NULL) {
        rc = PYSQLITE_SQL_WRONG_TYPE;
        return rc;
    }

    self->in_weakreflist = NULL;
    Py_INCREF(sql);
    self->sql = sql;

    Py_BEGIN_ALLOW_THREADS
    rc = sqlite3_prepare(connection->db,
                         sql_cstr,
                         -1,
                         &self->st,
                         &tail);
    Py_END_ALLOW_THREADS

    self->db = connection->db;

    if (rc == SQLITE_OK && pysqlite_check_remaining_sql(tail)) {
        (void)sqlite3_finalize(self->st);
        self->st = NULL;
        rc = PYSQLITE_TOO_MUCH_SQL;
    }

    return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 370 of file statement.c.

{
    int rc;

    if (self->st) {
        Py_BEGIN_ALLOW_THREADS
        rc = sqlite3_finalize(self->st);
        Py_END_ALLOW_THREADS
    }

    self->st = NULL;

    Py_XDECREF(self->sql);

    if (self->in_weakreflist != NULL) {
        PyObject_ClearWeakRefs((PyObject*)self);
    }

    Py_TYPE(self)->tp_free((PyObject*)self);
}

Here is the call graph for this function:

Definition at line 329 of file statement.c.

{
    int rc;

    rc = SQLITE_OK;
    if (self->st) {
        Py_BEGIN_ALLOW_THREADS
        rc = sqlite3_finalize(self->st);
        Py_END_ALLOW_THREADS
        self->st = NULL;
    }

    self->in_use = 0;

    return rc;
}

Here is the caller graph for this function:

Definition at line 365 of file statement.c.

{
    self->in_use = 1;
}

Here is the caller graph for this function:

Definition at line 283 of file statement.c.

{
    const char* tail;
    int rc;
    const char* sql_cstr;
    Py_ssize_t sql_len;
    sqlite3_stmt* new_st;

    sql_cstr = _PyUnicode_AsStringAndSize(self->sql, &sql_len);
    if (sql_cstr == NULL) {
        rc = PYSQLITE_SQL_WRONG_TYPE;
        return rc;
    }

    Py_BEGIN_ALLOW_THREADS
    rc = sqlite3_prepare(self->db,
                         sql_cstr,
                         -1,
                         &new_st,
                         &tail);
    Py_END_ALLOW_THREADS

    if (rc == SQLITE_OK) {
        /* The efficient sqlite3_transfer_bindings is only available in SQLite
         * version 3.2.2 or later. For older SQLite releases, that might not
         * even define SQLITE_VERSION_NUMBER, we do it the manual way.
         */
        #ifdef SQLITE_VERSION_NUMBER
        #if SQLITE_VERSION_NUMBER >= 3002002
        /* The check for the number of parameters is necessary to not trigger a
         * bug in certain SQLite versions (experienced in 3.2.8 and 3.3.4). */
        if (sqlite3_bind_parameter_count(self->st) > 0) {
            (void)sqlite3_transfer_bindings(self->st, new_st);
        }
        #endif
        #else
        statement_bind_parameters(self, params);
        #endif

        (void)sqlite3_finalize(self->st);
        self->st = new_st;
    }

    return rc;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 346 of file statement.c.

{
    int rc;

    rc = SQLITE_OK;

    if (self->in_use && self->st) {
        Py_BEGIN_ALLOW_THREADS
        rc = sqlite3_reset(self->st);
        Py_END_ALLOW_THREADS

        if (rc == SQLITE_OK) {
            self->in_use = 0;
        }
    }

    return rc;
}

Here is the caller graph for this function:

Definition at line 503 of file statement.c.

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 461 of file statement.c.