2006-03-31 20:57:31 -04:00
|
|
|
|
/* connection.c - the connection type
|
|
|
|
|
*
|
2010-03-05 05:12:37 -04:00
|
|
|
|
* Copyright (C) 2004-2010 Gerhard H<EFBFBD>ring <gh@ghaering.de>
|
2006-03-31 20:57:31 -04:00
|
|
|
|
*
|
|
|
|
|
* This file is part of pysqlite.
|
2006-04-04 03:29:05 -03:00
|
|
|
|
*
|
2006-03-31 20:57:31 -04:00
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
|
* warranty. In no event will the authors be held liable for any damages
|
|
|
|
|
* arising from the use of this software.
|
|
|
|
|
*
|
|
|
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
|
|
|
* including commercial applications, and to alter it and redistribute it
|
|
|
|
|
* freely, subject to the following restrictions:
|
|
|
|
|
*
|
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
|
* claim that you wrote the original software. If you use this software
|
|
|
|
|
* in a product, an acknowledgment in the product documentation would be
|
|
|
|
|
* appreciated but is not required.
|
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
|
* misrepresented as being the original software.
|
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "cache.h"
|
|
|
|
|
#include "module.h"
|
|
|
|
|
#include "connection.h"
|
|
|
|
|
#include "statement.h"
|
|
|
|
|
#include "cursor.h"
|
|
|
|
|
#include "prepare_protocol.h"
|
|
|
|
|
#include "util.h"
|
2006-04-04 03:29:05 -03:00
|
|
|
|
#include "sqlitecompat.h"
|
|
|
|
|
|
2006-03-31 20:57:31 -04:00
|
|
|
|
#include "pythread.h"
|
|
|
|
|
|
2008-02-29 18:08:41 -04:00
|
|
|
|
#define ACTION_FINALIZE 1
|
|
|
|
|
#define ACTION_RESET 2
|
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
#if SQLITE_VERSION_NUMBER >= 3003008
|
|
|
|
|
#ifndef SQLITE_OMIT_LOAD_EXTENSION
|
|
|
|
|
#define HAVE_LOAD_EXTENSION
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
|
2010-03-05 05:12:37 -04:00
|
|
|
|
static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
2006-06-14 19:28:37 -03:00
|
|
|
|
|
2008-09-12 15:58:57 -03:00
|
|
|
|
static void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
|
2006-06-14 19:28:37 -03:00
|
|
|
|
{
|
|
|
|
|
/* in older SQLite versions, calling sqlite3_result_error in callbacks
|
|
|
|
|
* triggers a bug in SQLite that leads either to irritating results or
|
|
|
|
|
* segfaults, depending on the SQLite version */
|
|
|
|
|
#if SQLITE_VERSION_NUMBER >= 3003003
|
|
|
|
|
sqlite3_result_error(ctx, errmsg, len);
|
2006-06-15 01:54:29 -03:00
|
|
|
|
#else
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyErr_SetString(pysqlite_OperationalError, errmsg);
|
2006-06-14 19:28:37 -03:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL};
|
|
|
|
|
|
2008-02-29 18:08:41 -04:00
|
|
|
|
PyObject* database;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
int detect_types = 0;
|
|
|
|
|
PyObject* isolation_level = NULL;
|
|
|
|
|
PyObject* factory = NULL;
|
|
|
|
|
int check_same_thread = 1;
|
|
|
|
|
int cached_statements = 100;
|
|
|
|
|
double timeout = 5.0;
|
|
|
|
|
int rc;
|
2008-02-29 18:08:41 -04:00
|
|
|
|
PyObject* class_attr = NULL;
|
|
|
|
|
PyObject* class_attr_str = NULL;
|
|
|
|
|
int is_apsw_connection = 0;
|
|
|
|
|
PyObject* database_utf8;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
2008-02-29 18:08:41 -04:00
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOi", kwlist,
|
2006-03-31 20:57:31 -04:00
|
|
|
|
&database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements))
|
|
|
|
|
{
|
2008-02-29 18:08:41 -04:00
|
|
|
|
return -1;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
self->initialized = 1;
|
|
|
|
|
|
2006-03-31 20:57:31 -04:00
|
|
|
|
self->begin_statement = NULL;
|
|
|
|
|
|
|
|
|
|
self->statement_cache = NULL;
|
2006-04-23 12:24:26 -03:00
|
|
|
|
self->statements = NULL;
|
2010-03-05 05:12:37 -04:00
|
|
|
|
self->cursors = NULL;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
self->row_factory = Py_None;
|
|
|
|
|
|
|
|
|
|
Py_INCREF(&PyUnicode_Type);
|
|
|
|
|
self->text_factory = (PyObject*)&PyUnicode_Type;
|
|
|
|
|
|
2008-06-09 01:58:54 -03:00
|
|
|
|
if (PyString_Check(database) || PyUnicode_Check(database)) {
|
|
|
|
|
if (PyString_Check(database)) {
|
2008-02-29 18:08:41 -04:00
|
|
|
|
database_utf8 = database;
|
|
|
|
|
Py_INCREF(database_utf8);
|
|
|
|
|
} else {
|
|
|
|
|
database_utf8 = PyUnicode_AsUTF8String(database);
|
|
|
|
|
if (!database_utf8) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
2008-02-29 18:08:41 -04:00
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2008-06-09 01:58:54 -03:00
|
|
|
|
rc = sqlite3_open(PyString_AsString(database_utf8), &self->db);
|
2008-02-29 18:08:41 -04:00
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
|
|
Py_DECREF(database_utf8);
|
|
|
|
|
|
|
|
|
|
if (rc != SQLITE_OK) {
|
|
|
|
|
_pysqlite_seterror(self->db, NULL);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* Create a pysqlite connection from a APSW connection */
|
|
|
|
|
class_attr = PyObject_GetAttrString(database, "__class__");
|
|
|
|
|
if (class_attr) {
|
|
|
|
|
class_attr_str = PyObject_Str(class_attr);
|
|
|
|
|
if (class_attr_str) {
|
2008-06-09 01:58:54 -03:00
|
|
|
|
if (strcmp(PyString_AsString(class_attr_str), "<type 'apsw.Connection'>") == 0) {
|
2008-02-29 18:08:41 -04:00
|
|
|
|
/* In the APSW Connection object, the first entry after
|
|
|
|
|
* PyObject_HEAD is the sqlite3* we want to get hold of.
|
|
|
|
|
* Luckily, this is the same layout as we have in our
|
|
|
|
|
* pysqlite_Connection */
|
|
|
|
|
self->db = ((pysqlite_Connection*)database)->db;
|
|
|
|
|
|
|
|
|
|
Py_INCREF(database);
|
|
|
|
|
self->apsw_connection = database;
|
|
|
|
|
is_apsw_connection = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Py_XDECREF(class_attr_str);
|
|
|
|
|
Py_XDECREF(class_attr);
|
|
|
|
|
|
|
|
|
|
if (!is_apsw_connection) {
|
|
|
|
|
PyErr_SetString(PyExc_ValueError, "database parameter must be string or APSW Connection object");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isolation_level) {
|
2008-06-09 01:58:54 -03:00
|
|
|
|
isolation_level = PyString_FromString("");
|
2006-04-23 12:24:26 -03:00
|
|
|
|
if (!isolation_level) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2006-03-31 20:57:31 -04:00
|
|
|
|
} else {
|
|
|
|
|
Py_INCREF(isolation_level);
|
|
|
|
|
}
|
|
|
|
|
self->isolation_level = NULL;
|
2007-01-13 21:43:50 -04:00
|
|
|
|
pysqlite_connection_set_isolation_level(self, isolation_level);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
Py_DECREF(isolation_level);
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
self->created_statements = 0;
|
|
|
|
|
self->created_cursors = 0;
|
|
|
|
|
|
|
|
|
|
/* Create lists of weak references to statements/cursors */
|
2006-04-23 12:24:26 -03:00
|
|
|
|
self->statements = PyList_New(0);
|
2010-03-05 05:12:37 -04:00
|
|
|
|
self->cursors = PyList_New(0);
|
|
|
|
|
if (!self->statements || !self->cursors) {
|
2006-04-23 12:24:26 -03:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-31 20:57:31 -04:00
|
|
|
|
/* By default, the Cache class INCREFs the factory in its initializer, and
|
|
|
|
|
* decrefs it in its deallocator method. Since this would create a circular
|
|
|
|
|
* reference here, we're breaking it by decrementing self, and telling the
|
|
|
|
|
* cache class to not decref the factory (self) in its deallocator.
|
|
|
|
|
*/
|
|
|
|
|
self->statement_cache->decref_factory = 0;
|
|
|
|
|
Py_DECREF(self);
|
|
|
|
|
|
|
|
|
|
self->inTransaction = 0;
|
|
|
|
|
self->detect_types = detect_types;
|
|
|
|
|
self->timeout = timeout;
|
|
|
|
|
(void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#ifdef WITH_THREAD
|
2006-03-31 20:57:31 -04:00
|
|
|
|
self->thread_ident = PyThread_get_thread_ident();
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#endif
|
2006-03-31 20:57:31 -04:00
|
|
|
|
self->check_same_thread = check_same_thread;
|
|
|
|
|
|
|
|
|
|
self->function_pinboard = PyDict_New();
|
2006-04-04 03:29:05 -03:00
|
|
|
|
if (!self->function_pinboard) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self->collations = PyDict_New();
|
|
|
|
|
if (!self->collations) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
self->Warning = pysqlite_Warning;
|
|
|
|
|
self->Error = pysqlite_Error;
|
|
|
|
|
self->InterfaceError = pysqlite_InterfaceError;
|
|
|
|
|
self->DatabaseError = pysqlite_DatabaseError;
|
|
|
|
|
self->DataError = pysqlite_DataError;
|
|
|
|
|
self->OperationalError = pysqlite_OperationalError;
|
|
|
|
|
self->IntegrityError = pysqlite_IntegrityError;
|
|
|
|
|
self->InternalError = pysqlite_InternalError;
|
|
|
|
|
self->ProgrammingError = pysqlite_ProgrammingError;
|
|
|
|
|
self->NotSupportedError = pysqlite_NotSupportedError;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-23 12:24:26 -03:00
|
|
|
|
/* Empty the entire statement cache of this connection */
|
2007-01-13 21:43:50 -04:00
|
|
|
|
void pysqlite_flush_statement_cache(pysqlite_Connection* self)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
2007-01-13 21:43:50 -04:00
|
|
|
|
pysqlite_Node* node;
|
|
|
|
|
pysqlite_Statement* statement;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
|
|
|
|
node = self->statement_cache->first;
|
|
|
|
|
|
|
|
|
|
while (node) {
|
2007-01-13 21:43:50 -04:00
|
|
|
|
statement = (pysqlite_Statement*)(node->data);
|
|
|
|
|
(void)pysqlite_statement_finalize(statement);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
node = node->next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Py_DECREF(self->statement_cache);
|
2007-01-13 21:43:50 -04:00
|
|
|
|
self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
Py_DECREF(self);
|
|
|
|
|
self->statement_cache->decref_factory = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 18:08:41 -04:00
|
|
|
|
/* action in (ACTION_RESET, ACTION_FINALIZE) */
|
2010-03-05 05:12:37 -04:00
|
|
|
|
void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
2006-04-23 12:24:26 -03:00
|
|
|
|
int i;
|
|
|
|
|
PyObject* weakref;
|
|
|
|
|
PyObject* statement;
|
2010-03-05 05:12:37 -04:00
|
|
|
|
pysqlite_Cursor* cursor;
|
2006-04-23 12:24:26 -03:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < PyList_Size(self->statements); i++) {
|
|
|
|
|
weakref = PyList_GetItem(self->statements, i);
|
|
|
|
|
statement = PyWeakref_GetObject(weakref);
|
|
|
|
|
if (statement != Py_None) {
|
2008-02-29 18:08:41 -04:00
|
|
|
|
if (action == ACTION_RESET) {
|
|
|
|
|
(void)pysqlite_statement_reset((pysqlite_Statement*)statement);
|
|
|
|
|
} else {
|
|
|
|
|
(void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
|
|
|
|
|
}
|
2006-04-23 12:24:26 -03:00
|
|
|
|
}
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
2010-03-05 05:12:37 -04:00
|
|
|
|
|
|
|
|
|
if (reset_cursors) {
|
|
|
|
|
for (i = 0; i < PyList_Size(self->cursors); i++) {
|
|
|
|
|
weakref = PyList_GetItem(self->cursors, i);
|
|
|
|
|
cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
|
|
|
|
|
if ((PyObject*)cursor != Py_None) {
|
|
|
|
|
cursor->reset = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
void pysqlite_connection_dealloc(pysqlite_Connection* self)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
2008-02-29 18:08:41 -04:00
|
|
|
|
PyObject* ret = NULL;
|
|
|
|
|
|
2006-03-31 20:57:31 -04:00
|
|
|
|
Py_XDECREF(self->statement_cache);
|
|
|
|
|
|
|
|
|
|
/* Clean up if user has not called .close() explicitly. */
|
|
|
|
|
if (self->db) {
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
|
sqlite3_close(self->db);
|
|
|
|
|
Py_END_ALLOW_THREADS
|
2008-02-29 18:08:41 -04:00
|
|
|
|
} else if (self->apsw_connection) {
|
|
|
|
|
ret = PyObject_CallMethod(self->apsw_connection, "close", "");
|
|
|
|
|
Py_XDECREF(ret);
|
|
|
|
|
Py_XDECREF(self->apsw_connection);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (self->begin_statement) {
|
|
|
|
|
PyMem_Free(self->begin_statement);
|
|
|
|
|
}
|
|
|
|
|
Py_XDECREF(self->isolation_level);
|
|
|
|
|
Py_XDECREF(self->function_pinboard);
|
|
|
|
|
Py_XDECREF(self->row_factory);
|
|
|
|
|
Py_XDECREF(self->text_factory);
|
2006-04-04 03:29:05 -03:00
|
|
|
|
Py_XDECREF(self->collations);
|
2006-04-23 12:24:26 -03:00
|
|
|
|
Py_XDECREF(self->statements);
|
2010-03-05 05:12:37 -04:00
|
|
|
|
Py_XDECREF(self->cursors);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
2008-02-29 18:08:41 -04:00
|
|
|
|
self->ob_type->tp_free((PyObject*)self);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
/*
|
|
|
|
|
* Registers a cursor with the connection.
|
|
|
|
|
*
|
|
|
|
|
* 0 => error; 1 => ok
|
|
|
|
|
*/
|
|
|
|
|
int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
|
|
|
|
|
{
|
|
|
|
|
PyObject* weakref;
|
|
|
|
|
|
|
|
|
|
weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
|
|
|
|
|
if (!weakref) {
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (PyList_Append(connection->cursors, weakref) != 0) {
|
|
|
|
|
Py_CLEAR(weakref);
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Py_DECREF(weakref);
|
|
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
|
error:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
static char *kwlist[] = {"factory", NULL, NULL};
|
|
|
|
|
PyObject* factory = NULL;
|
|
|
|
|
PyObject* cursor;
|
|
|
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
|
|
|
|
|
&factory)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
|
2006-03-31 20:57:31 -04:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (factory == NULL) {
|
2007-01-13 21:43:50 -04:00
|
|
|
|
factory = (PyObject*)&pysqlite_CursorType;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cursor = PyObject_CallFunction(factory, "O", self);
|
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
_pysqlite_drop_unused_cursor_references(self);
|
|
|
|
|
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (cursor && self->row_factory != Py_None) {
|
2007-01-13 21:43:50 -04:00
|
|
|
|
Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
Py_INCREF(self->row_factory);
|
2007-01-13 21:43:50 -04:00
|
|
|
|
((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cursor;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
2008-02-29 18:08:41 -04:00
|
|
|
|
PyObject* ret;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
int rc;
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
if (!pysqlite_check_thread(self)) {
|
2006-03-31 20:57:31 -04:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
|
|
|
|
if (self->db) {
|
2008-02-29 18:08:41 -04:00
|
|
|
|
if (self->apsw_connection) {
|
|
|
|
|
ret = PyObject_CallMethod(self->apsw_connection, "close", "");
|
|
|
|
|
Py_XDECREF(ret);
|
|
|
|
|
Py_XDECREF(self->apsw_connection);
|
|
|
|
|
self->apsw_connection = NULL;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
self->db = NULL;
|
2008-02-29 18:08:41 -04:00
|
|
|
|
} else {
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
|
rc = sqlite3_close(self->db);
|
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
|
|
if (rc != SQLITE_OK) {
|
|
|
|
|
_pysqlite_seterror(self->db, NULL);
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
self->db = NULL;
|
|
|
|
|
}
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Checks if a connection object is usable (i. e. not closed).
|
|
|
|
|
*
|
|
|
|
|
* 0 => error; 1 => ok
|
|
|
|
|
*/
|
2007-01-13 21:43:50 -04:00
|
|
|
|
int pysqlite_check_connection(pysqlite_Connection* con)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
2010-03-05 05:12:37 -04:00
|
|
|
|
if (!con->initialized) {
|
|
|
|
|
PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (!con->db) {
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
|
2006-03-31 20:57:31 -04:00
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
const char* tail;
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
|
rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
|
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
|
|
if (rc != SQLITE_OK) {
|
2008-02-29 18:08:41 -04:00
|
|
|
|
_pysqlite_seterror(self->db, statement);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-12 15:58:57 -03:00
|
|
|
|
rc = pysqlite_step(statement, self);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (rc == SQLITE_DONE) {
|
|
|
|
|
self->inTransaction = 1;
|
|
|
|
|
} else {
|
2008-02-29 18:08:41 -04:00
|
|
|
|
_pysqlite_seterror(self->db, statement);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
|
rc = sqlite3_finalize(statement);
|
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
|
|
if (rc != SQLITE_OK && !PyErr_Occurred()) {
|
2008-02-29 18:08:41 -04:00
|
|
|
|
_pysqlite_seterror(self->db, NULL);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
const char* tail;
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
|
2006-03-31 20:57:31 -04:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (self->inTransaction) {
|
2010-03-05 05:12:37 -04:00
|
|
|
|
pysqlite_do_all_statements(self, ACTION_RESET, 0);
|
|
|
|
|
|
2006-03-31 20:57:31 -04:00
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
|
rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
|
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
if (rc != SQLITE_OK) {
|
2008-02-29 18:08:41 -04:00
|
|
|
|
_pysqlite_seterror(self->db, NULL);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-12 15:58:57 -03:00
|
|
|
|
rc = pysqlite_step(statement, self);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (rc == SQLITE_DONE) {
|
|
|
|
|
self->inTransaction = 0;
|
|
|
|
|
} else {
|
2008-02-29 18:08:41 -04:00
|
|
|
|
_pysqlite_seterror(self->db, statement);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
|
rc = sqlite3_finalize(statement);
|
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
if (rc != SQLITE_OK && !PyErr_Occurred()) {
|
2008-02-29 18:08:41 -04:00
|
|
|
|
_pysqlite_seterror(self->db, NULL);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
const char* tail;
|
|
|
|
|
sqlite3_stmt* statement;
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
|
2006-03-31 20:57:31 -04:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (self->inTransaction) {
|
2010-03-05 05:12:37 -04:00
|
|
|
|
pysqlite_do_all_statements(self, ACTION_RESET, 1);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
|
rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
|
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
if (rc != SQLITE_OK) {
|
2008-02-29 18:08:41 -04:00
|
|
|
|
_pysqlite_seterror(self->db, NULL);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-12 15:58:57 -03:00
|
|
|
|
rc = pysqlite_step(statement, self);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (rc == SQLITE_DONE) {
|
|
|
|
|
self->inTransaction = 0;
|
|
|
|
|
} else {
|
2008-02-29 18:08:41 -04:00
|
|
|
|
_pysqlite_seterror(self->db, statement);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
|
rc = sqlite3_finalize(statement);
|
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
if (rc != SQLITE_OK && !PyErr_Occurred()) {
|
2008-02-29 18:08:41 -04:00
|
|
|
|
_pysqlite_seterror(self->db, NULL);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 10:59:34 -04:00
|
|
|
|
static int
|
|
|
|
|
_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
2013-02-07 10:59:34 -04:00
|
|
|
|
if (py_val == Py_None) {
|
2006-03-31 20:57:31 -04:00
|
|
|
|
sqlite3_result_null(context);
|
|
|
|
|
} else if (PyInt_Check(py_val)) {
|
2012-05-20 03:35:05 -03:00
|
|
|
|
sqlite3_result_int64(context, (sqlite_int64)PyInt_AsLong(py_val));
|
2012-02-21 07:58:40 -04:00
|
|
|
|
} else if (PyLong_Check(py_val)) {
|
2013-02-07 10:59:34 -04:00
|
|
|
|
sqlite_int64 value = _pysqlite_long_as_int64(py_val);
|
|
|
|
|
if (value == -1 && PyErr_Occurred())
|
|
|
|
|
return -1;
|
|
|
|
|
sqlite3_result_int64(context, value);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
} else if (PyFloat_Check(py_val)) {
|
|
|
|
|
sqlite3_result_double(context, PyFloat_AsDouble(py_val));
|
|
|
|
|
} else if (PyBuffer_Check(py_val)) {
|
2013-02-07 10:59:34 -04:00
|
|
|
|
const char* buffer;
|
|
|
|
|
Py_ssize_t buflen;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
|
|
|
|
|
PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
|
2013-02-07 10:59:34 -04:00
|
|
|
|
return -1;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
2013-02-07 10:59:34 -04:00
|
|
|
|
sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
|
2008-06-09 01:58:54 -03:00
|
|
|
|
} else if (PyString_Check(py_val)) {
|
|
|
|
|
sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
} else if (PyUnicode_Check(py_val)) {
|
2013-02-07 10:59:34 -04:00
|
|
|
|
PyObject * stringval = PyUnicode_AsUTF8String(py_val);
|
|
|
|
|
if (!stringval)
|
|
|
|
|
return -1;
|
|
|
|
|
sqlite3_result_text(context, PyString_AsString(stringval), -1, SQLITE_TRANSIENT);
|
|
|
|
|
Py_DECREF(stringval);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
} else {
|
2013-02-07 10:59:34 -04:00
|
|
|
|
return -1;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
2013-02-07 10:59:34 -04:00
|
|
|
|
return 0;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
PyObject* args;
|
|
|
|
|
int i;
|
|
|
|
|
sqlite3_value* cur_value;
|
|
|
|
|
PyObject* cur_py_value;
|
|
|
|
|
const char* val_str;
|
2006-04-01 05:08:06 -04:00
|
|
|
|
Py_ssize_t buflen;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
void* raw_buffer;
|
|
|
|
|
|
|
|
|
|
args = PyTuple_New(argc);
|
2006-04-04 03:29:05 -03:00
|
|
|
|
if (!args) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
|
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
|
cur_value = argv[i];
|
|
|
|
|
switch (sqlite3_value_type(argv[i])) {
|
|
|
|
|
case SQLITE_INTEGER:
|
2013-02-07 10:59:34 -04:00
|
|
|
|
cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
|
2006-03-31 20:57:31 -04:00
|
|
|
|
break;
|
|
|
|
|
case SQLITE_FLOAT:
|
|
|
|
|
cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
|
|
|
|
|
break;
|
|
|
|
|
case SQLITE_TEXT:
|
|
|
|
|
val_str = (const char*)sqlite3_value_text(cur_value);
|
|
|
|
|
cur_py_value = PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL);
|
|
|
|
|
/* TODO: have a way to show errors here */
|
|
|
|
|
if (!cur_py_value) {
|
2006-04-23 12:24:26 -03:00
|
|
|
|
PyErr_Clear();
|
2006-03-31 20:57:31 -04:00
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
cur_py_value = Py_None;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case SQLITE_BLOB:
|
|
|
|
|
buflen = sqlite3_value_bytes(cur_value);
|
|
|
|
|
cur_py_value = PyBuffer_New(buflen);
|
|
|
|
|
if (!cur_py_value) {
|
2006-04-23 12:24:26 -03:00
|
|
|
|
break;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
if (PyObject_AsWriteBuffer(cur_py_value, &raw_buffer, &buflen)) {
|
2006-04-23 12:24:26 -03:00
|
|
|
|
Py_DECREF(cur_py_value);
|
|
|
|
|
cur_py_value = NULL;
|
|
|
|
|
break;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
memcpy(raw_buffer, sqlite3_value_blob(cur_value), buflen);
|
|
|
|
|
break;
|
|
|
|
|
case SQLITE_NULL:
|
|
|
|
|
default:
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
cur_py_value = Py_None;
|
|
|
|
|
}
|
2006-04-23 12:24:26 -03:00
|
|
|
|
|
|
|
|
|
if (!cur_py_value) {
|
|
|
|
|
Py_DECREF(args);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyTuple_SetItem(args, i, cur_py_value);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
PyObject* args;
|
|
|
|
|
PyObject* py_func;
|
2006-04-23 12:24:26 -03:00
|
|
|
|
PyObject* py_retval = NULL;
|
2013-02-07 10:59:34 -04:00
|
|
|
|
int ok;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#ifdef WITH_THREAD
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyGILState_STATE threadstate;
|
|
|
|
|
|
|
|
|
|
threadstate = PyGILState_Ensure();
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#endif
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
|
|
|
|
py_func = (PyObject*)sqlite3_user_data(context);
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
args = _pysqlite_build_py_params(context, argc, argv);
|
2006-04-23 12:24:26 -03:00
|
|
|
|
if (args) {
|
|
|
|
|
py_retval = PyObject_CallObject(py_func, args);
|
|
|
|
|
Py_DECREF(args);
|
|
|
|
|
}
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
2013-02-07 10:59:34 -04:00
|
|
|
|
ok = 0;
|
2006-06-13 19:24:47 -03:00
|
|
|
|
if (py_retval) {
|
2013-02-07 10:59:34 -04:00
|
|
|
|
ok = _pysqlite_set_result(context, py_retval) == 0;
|
2006-06-13 19:24:47 -03:00
|
|
|
|
Py_DECREF(py_retval);
|
2013-02-07 10:59:34 -04:00
|
|
|
|
}
|
|
|
|
|
if (!ok) {
|
2006-06-13 19:24:47 -03:00
|
|
|
|
if (_enable_callback_tracebacks) {
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
} else {
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
}
|
2006-06-14 19:28:37 -03:00
|
|
|
|
_sqlite3_result_error(context, "user-defined function raised exception", -1);
|
2006-06-13 19:24:47 -03:00
|
|
|
|
}
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#ifdef WITH_THREAD
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyGILState_Release(threadstate);
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#endif
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
PyObject* args;
|
2006-04-23 12:24:26 -03:00
|
|
|
|
PyObject* function_result = NULL;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyObject* aggregate_class;
|
|
|
|
|
PyObject** aggregate_instance;
|
2006-04-23 12:24:26 -03:00
|
|
|
|
PyObject* stepmethod = NULL;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#ifdef WITH_THREAD
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyGILState_STATE threadstate;
|
|
|
|
|
|
|
|
|
|
threadstate = PyGILState_Ensure();
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#endif
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
|
|
|
|
aggregate_class = (PyObject*)sqlite3_user_data(context);
|
|
|
|
|
|
|
|
|
|
aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
|
|
|
|
|
|
|
|
|
|
if (*aggregate_instance == 0) {
|
|
|
|
|
*aggregate_instance = PyObject_CallFunction(aggregate_class, "");
|
|
|
|
|
|
2006-04-23 12:24:26 -03:00
|
|
|
|
if (PyErr_Occurred()) {
|
2006-03-31 20:57:31 -04:00
|
|
|
|
*aggregate_instance = 0;
|
2006-06-13 19:24:47 -03:00
|
|
|
|
if (_enable_callback_tracebacks) {
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
} else {
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
}
|
2006-06-14 19:28:37 -03:00
|
|
|
|
_sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
|
2006-04-23 12:24:26 -03:00
|
|
|
|
goto error;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
|
2006-04-23 12:24:26 -03:00
|
|
|
|
if (!stepmethod) {
|
|
|
|
|
goto error;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
args = _pysqlite_build_py_params(context, argc, params);
|
2006-04-23 12:24:26 -03:00
|
|
|
|
if (!args) {
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
|
|
|
|
function_result = PyObject_CallObject(stepmethod, args);
|
|
|
|
|
Py_DECREF(args);
|
|
|
|
|
|
2006-04-23 12:24:26 -03:00
|
|
|
|
if (!function_result) {
|
2006-06-13 19:24:47 -03:00
|
|
|
|
if (_enable_callback_tracebacks) {
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
} else {
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
}
|
2006-06-14 19:28:37 -03:00
|
|
|
|
_sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2006-04-23 12:24:26 -03:00
|
|
|
|
error:
|
|
|
|
|
Py_XDECREF(stepmethod);
|
|
|
|
|
Py_XDECREF(function_result);
|
|
|
|
|
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#ifdef WITH_THREAD
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyGILState_Release(threadstate);
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#endif
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
void _pysqlite_final_callback(sqlite3_context* context)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
2013-02-07 10:59:34 -04:00
|
|
|
|
PyObject* function_result;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyObject** aggregate_instance;
|
2013-02-07 10:59:34 -04:00
|
|
|
|
int ok;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#ifdef WITH_THREAD
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyGILState_STATE threadstate;
|
|
|
|
|
|
|
|
|
|
threadstate = PyGILState_Ensure();
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#endif
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
|
|
|
|
aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
|
|
|
|
|
if (!*aggregate_instance) {
|
|
|
|
|
/* this branch is executed if there was an exception in the aggregate's
|
|
|
|
|
* __init__ */
|
|
|
|
|
|
2006-04-23 12:24:26 -03:00
|
|
|
|
goto error;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2006-04-23 12:24:26 -03:00
|
|
|
|
function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
|
2013-02-07 10:59:34 -04:00
|
|
|
|
Py_DECREF(*aggregate_instance);
|
|
|
|
|
|
|
|
|
|
ok = 0;
|
|
|
|
|
if (function_result) {
|
|
|
|
|
ok = _pysqlite_set_result(context, function_result) == 0;
|
|
|
|
|
Py_DECREF(function_result);
|
|
|
|
|
}
|
|
|
|
|
if (!ok) {
|
2006-06-13 19:24:47 -03:00
|
|
|
|
if (_enable_callback_tracebacks) {
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
} else {
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
}
|
2006-06-14 19:28:37 -03:00
|
|
|
|
_sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2006-04-23 12:24:26 -03:00
|
|
|
|
error:
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#ifdef WITH_THREAD
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyGILState_Release(threadstate);
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#endif
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
|
2006-04-23 12:24:26 -03:00
|
|
|
|
{
|
|
|
|
|
PyObject* new_list;
|
|
|
|
|
PyObject* weakref;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
/* we only need to do this once in a while */
|
|
|
|
|
if (self->created_statements++ < 200) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self->created_statements = 0;
|
|
|
|
|
|
|
|
|
|
new_list = PyList_New(0);
|
|
|
|
|
if (!new_list) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < PyList_Size(self->statements); i++) {
|
|
|
|
|
weakref = PyList_GetItem(self->statements, i);
|
2006-06-19 18:17:35 -03:00
|
|
|
|
if (PyWeakref_GetObject(weakref) != Py_None) {
|
2006-04-23 12:24:26 -03:00
|
|
|
|
if (PyList_Append(new_list, weakref) != 0) {
|
|
|
|
|
Py_DECREF(new_list);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Py_DECREF(self->statements);
|
|
|
|
|
self->statements = new_list;
|
|
|
|
|
}
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
|
|
|
|
|
{
|
|
|
|
|
PyObject* new_list;
|
|
|
|
|
PyObject* weakref;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
/* we only need to do this once in a while */
|
|
|
|
|
if (self->created_cursors++ < 200) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self->created_cursors = 0;
|
|
|
|
|
|
|
|
|
|
new_list = PyList_New(0);
|
|
|
|
|
if (!new_list) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < PyList_Size(self->cursors); i++) {
|
|
|
|
|
weakref = PyList_GetItem(self->cursors, i);
|
|
|
|
|
if (PyWeakref_GetObject(weakref) != Py_None) {
|
|
|
|
|
if (PyList_Append(new_list, weakref) != 0) {
|
|
|
|
|
Py_DECREF(new_list);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Py_DECREF(self->cursors);
|
|
|
|
|
self->cursors = new_list;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
|
|
|
|
|
|
|
|
|
|
PyObject* func;
|
|
|
|
|
char* name;
|
|
|
|
|
int narg;
|
|
|
|
|
int rc;
|
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
|
|
|
|
|
&name, &narg, &func))
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
2006-04-23 12:24:26 -03:00
|
|
|
|
if (rc != SQLITE_OK) {
|
|
|
|
|
/* Workaround for SQLite bug: no error code or string is available here */
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyErr_SetString(pysqlite_OperationalError, "Error creating function");
|
2006-04-23 12:24:26 -03:00
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
2010-03-05 05:12:37 -04:00
|
|
|
|
if (PyDict_SetItem(self->function_pinboard, func, Py_None) == -1)
|
|
|
|
|
return NULL;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
2006-04-23 12:24:26 -03:00
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
PyObject* aggregate_class;
|
|
|
|
|
|
|
|
|
|
int n_arg;
|
|
|
|
|
char* name;
|
|
|
|
|
static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
|
|
|
|
|
int rc;
|
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
|
|
|
|
|
kwlist, &name, &n_arg, &aggregate_class)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
rc = sqlite3_create_function(self->db, name, n_arg, SQLITE_UTF8, (void*)aggregate_class, 0, &_pysqlite_step_callback, &_pysqlite_final_callback);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (rc != SQLITE_OK) {
|
2006-04-23 12:24:26 -03:00
|
|
|
|
/* Workaround for SQLite bug: no error code or string is available here */
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
|
2006-03-31 20:57:31 -04:00
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
2010-03-05 05:12:37 -04:00
|
|
|
|
if (PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None) == -1)
|
|
|
|
|
return NULL;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
static int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source)
|
2006-06-13 19:24:47 -03:00
|
|
|
|
{
|
|
|
|
|
PyObject *ret;
|
|
|
|
|
int rc;
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#ifdef WITH_THREAD
|
2006-06-13 19:24:47 -03:00
|
|
|
|
PyGILState_STATE gilstate;
|
|
|
|
|
|
|
|
|
|
gilstate = PyGILState_Ensure();
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#endif
|
2006-06-13 19:24:47 -03:00
|
|
|
|
ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
|
|
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
|
if (_enable_callback_tracebacks) {
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
} else {
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = SQLITE_DENY;
|
|
|
|
|
} else {
|
|
|
|
|
if (PyInt_Check(ret)) {
|
2013-02-07 10:59:34 -04:00
|
|
|
|
rc = _PyInt_AsInt(ret);
|
|
|
|
|
if (rc == -1 && PyErr_Occurred())
|
|
|
|
|
rc = SQLITE_DENY;
|
2006-06-13 19:24:47 -03:00
|
|
|
|
} else {
|
|
|
|
|
rc = SQLITE_DENY;
|
|
|
|
|
}
|
|
|
|
|
Py_DECREF(ret);
|
|
|
|
|
}
|
|
|
|
|
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#ifdef WITH_THREAD
|
2006-06-13 19:24:47 -03:00
|
|
|
|
PyGILState_Release(gilstate);
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#endif
|
2006-06-13 19:24:47 -03:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 18:08:41 -04:00
|
|
|
|
static int _progress_handler(void* user_arg)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
PyObject *ret;
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#ifdef WITH_THREAD
|
2008-02-29 18:08:41 -04:00
|
|
|
|
PyGILState_STATE gilstate;
|
|
|
|
|
|
|
|
|
|
gilstate = PyGILState_Ensure();
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#endif
|
2008-02-29 18:08:41 -04:00
|
|
|
|
ret = PyObject_CallFunction((PyObject*)user_arg, "");
|
|
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
|
if (_enable_callback_tracebacks) {
|
|
|
|
|
PyErr_Print();
|
|
|
|
|
} else {
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-21 16:27:01 -04:00
|
|
|
|
/* abort query if error occurred */
|
2013-02-07 10:59:34 -04:00
|
|
|
|
rc = 1;
|
2008-02-29 18:08:41 -04:00
|
|
|
|
} else {
|
|
|
|
|
rc = (int)PyObject_IsTrue(ret);
|
2008-03-03 00:37:45 -04:00
|
|
|
|
Py_DECREF(ret);
|
2008-02-29 18:08:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#ifdef WITH_THREAD
|
2008-02-29 18:08:41 -04:00
|
|
|
|
PyGILState_Release(gilstate);
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#endif
|
2008-02-29 18:08:41 -04:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
|
2006-06-13 19:24:47 -03:00
|
|
|
|
{
|
|
|
|
|
PyObject* authorizer_cb;
|
|
|
|
|
|
|
|
|
|
static char *kwlist[] = { "authorizer_callback", NULL };
|
|
|
|
|
int rc;
|
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-13 19:24:47 -03:00
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
|
|
|
|
|
kwlist, &authorizer_cb)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
|
|
|
|
|
|
|
|
|
|
if (rc != SQLITE_OK) {
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
|
2006-06-13 19:24:47 -03:00
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
2010-03-05 05:12:37 -04:00
|
|
|
|
if (PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None) == -1)
|
|
|
|
|
return NULL;
|
2006-06-13 19:24:47 -03:00
|
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
|
2008-02-29 18:08:41 -04:00
|
|
|
|
{
|
|
|
|
|
PyObject* progress_handler;
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
static char *kwlist[] = { "progress_handler", "n", NULL };
|
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 18:08:41 -04:00
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
|
|
|
|
|
kwlist, &progress_handler, &n)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (progress_handler == Py_None) {
|
|
|
|
|
/* None clears the progress handler previously set */
|
|
|
|
|
sqlite3_progress_handler(self->db, 0, 0, (void*)0);
|
|
|
|
|
} else {
|
|
|
|
|
sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
|
2010-03-05 05:12:37 -04:00
|
|
|
|
if (PyDict_SetItem(self->function_pinboard, progress_handler, Py_None) == -1)
|
|
|
|
|
return NULL;
|
2008-02-29 18:08:41 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
#ifdef HAVE_LOAD_EXTENSION
|
|
|
|
|
static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
int onoff;
|
|
|
|
|
|
|
|
|
|
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "i", &onoff)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = sqlite3_enable_load_extension(self->db, onoff);
|
|
|
|
|
|
|
|
|
|
if (rc != SQLITE_OK) {
|
|
|
|
|
PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
char* extension_name;
|
|
|
|
|
char* errmsg;
|
|
|
|
|
|
|
|
|
|
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "s", &extension_name)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
|
|
|
|
|
if (rc != 0) {
|
|
|
|
|
PyErr_SetString(pysqlite_OperationalError, errmsg);
|
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
return Py_None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
int pysqlite_check_thread(pysqlite_Connection* self)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#ifdef WITH_THREAD
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (self->check_same_thread) {
|
|
|
|
|
if (PyThread_get_thread_ident() != self->thread_ident) {
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyErr_Format(pysqlite_ProgrammingError,
|
2006-03-31 20:57:31 -04:00
|
|
|
|
"SQLite objects created in a thread can only be used in that same thread."
|
|
|
|
|
"The object was created in thread id %ld and this is thread id %ld",
|
|
|
|
|
self->thread_ident, PyThread_get_thread_ident());
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#endif
|
2006-03-31 20:57:31 -04:00
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
Py_INCREF(self->isolation_level);
|
|
|
|
|
return self->isolation_level;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
|
2006-04-04 03:29:05 -03:00
|
|
|
|
{
|
2007-01-13 21:43:50 -04:00
|
|
|
|
if (!pysqlite_check_connection(self)) {
|
2006-04-04 03:29:05 -03:00
|
|
|
|
return NULL;
|
|
|
|
|
} else {
|
|
|
|
|
return Py_BuildValue("i", sqlite3_total_changes(self->db));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
PyObject* res;
|
|
|
|
|
PyObject* begin_statement;
|
2008-07-16 19:33:18 -03:00
|
|
|
|
char* begin_statement_str;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
|
|
|
|
Py_XDECREF(self->isolation_level);
|
|
|
|
|
|
2006-04-16 00:28:17 -03:00
|
|
|
|
if (self->begin_statement) {
|
|
|
|
|
PyMem_Free(self->begin_statement);
|
|
|
|
|
self->begin_statement = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (isolation_level == Py_None) {
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
self->isolation_level = Py_None;
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
res = pysqlite_connection_commit(self, NULL);
|
2006-04-04 03:29:05 -03:00
|
|
|
|
if (!res) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2006-03-31 20:57:31 -04:00
|
|
|
|
Py_DECREF(res);
|
|
|
|
|
|
|
|
|
|
self->inTransaction = 0;
|
|
|
|
|
} else {
|
|
|
|
|
Py_INCREF(isolation_level);
|
|
|
|
|
self->isolation_level = isolation_level;
|
|
|
|
|
|
2008-06-09 01:58:54 -03:00
|
|
|
|
begin_statement = PyString_FromString("BEGIN ");
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (!begin_statement) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2008-06-09 01:58:54 -03:00
|
|
|
|
PyString_Concat(&begin_statement, isolation_level);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (!begin_statement) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-16 19:33:18 -03:00
|
|
|
|
begin_statement_str = PyString_AsString(begin_statement);
|
|
|
|
|
if (!begin_statement_str) {
|
|
|
|
|
Py_DECREF(begin_statement);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
self->begin_statement = PyMem_Malloc(strlen(begin_statement_str) + 2);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (!self->begin_statement) {
|
2008-07-16 19:33:18 -03:00
|
|
|
|
Py_DECREF(begin_statement);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-16 19:33:18 -03:00
|
|
|
|
strcpy(self->begin_statement, begin_statement_str);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
Py_DECREF(begin_statement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
PyObject* sql;
|
2007-01-13 21:43:50 -04:00
|
|
|
|
pysqlite_Statement* statement;
|
2006-04-23 12:24:26 -03:00
|
|
|
|
PyObject* weakref;
|
2006-03-31 20:57:31 -04:00
|
|
|
|
int rc;
|
|
|
|
|
|
2010-03-05 05:12:37 -04:00
|
|
|
|
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (!PyArg_ParseTuple(args, "O", &sql)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
_pysqlite_drop_unused_statement_references(self);
|
2006-04-23 12:24:26 -03:00
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
if (!statement) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-12 23:27:07 -04:00
|
|
|
|
statement->db = NULL;
|
|
|
|
|
statement->st = NULL;
|
|
|
|
|
statement->sql = NULL;
|
|
|
|
|
statement->in_use = 0;
|
|
|
|
|
statement->in_weakreflist = NULL;
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
rc = pysqlite_statement_create(statement, self, sql);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
|
|
|
|
if (rc != SQLITE_OK) {
|
|
|
|
|
if (rc == PYSQLITE_TOO_MUCH_SQL) {
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
|
2006-03-31 20:57:31 -04:00
|
|
|
|
} else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
|
2006-03-31 20:57:31 -04:00
|
|
|
|
} else {
|
2008-02-29 18:08:41 -04:00
|
|
|
|
(void)pysqlite_statement_reset(statement);
|
|
|
|
|
_pysqlite_seterror(self->db, NULL);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2008-07-13 18:47:59 -03:00
|
|
|
|
Py_CLEAR(statement);
|
2006-04-23 12:24:26 -03:00
|
|
|
|
} else {
|
|
|
|
|
weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
|
|
|
|
|
if (!weakref) {
|
2008-07-13 18:47:59 -03:00
|
|
|
|
Py_CLEAR(statement);
|
2006-04-23 12:24:26 -03:00
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (PyList_Append(self->statements, weakref) != 0) {
|
2008-07-13 18:47:59 -03:00
|
|
|
|
Py_CLEAR(weakref);
|
2006-04-23 12:24:26 -03:00
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Py_DECREF(weakref);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
2006-04-23 12:24:26 -03:00
|
|
|
|
error:
|
2006-03-31 20:57:31 -04:00
|
|
|
|
return (PyObject*)statement;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
PyObject* cursor = 0;
|
|
|
|
|
PyObject* result = 0;
|
|
|
|
|
PyObject* method = 0;
|
|
|
|
|
|
|
|
|
|
cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
|
|
|
|
|
if (!cursor) {
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
method = PyObject_GetAttrString(cursor, "execute");
|
|
|
|
|
if (!method) {
|
2008-07-13 18:47:59 -03:00
|
|
|
|
Py_CLEAR(cursor);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = PyObject_CallObject(method, args);
|
|
|
|
|
if (!result) {
|
2008-07-13 18:47:59 -03:00
|
|
|
|
Py_CLEAR(cursor);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
Py_XDECREF(result);
|
|
|
|
|
Py_XDECREF(method);
|
|
|
|
|
|
|
|
|
|
return cursor;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
PyObject* cursor = 0;
|
|
|
|
|
PyObject* result = 0;
|
|
|
|
|
PyObject* method = 0;
|
|
|
|
|
|
|
|
|
|
cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
|
|
|
|
|
if (!cursor) {
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
method = PyObject_GetAttrString(cursor, "executemany");
|
|
|
|
|
if (!method) {
|
2008-07-13 18:47:59 -03:00
|
|
|
|
Py_CLEAR(cursor);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = PyObject_CallObject(method, args);
|
|
|
|
|
if (!result) {
|
2008-07-13 18:47:59 -03:00
|
|
|
|
Py_CLEAR(cursor);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
Py_XDECREF(result);
|
|
|
|
|
Py_XDECREF(method);
|
|
|
|
|
|
|
|
|
|
return cursor;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
|
|
|
|
PyObject* cursor = 0;
|
|
|
|
|
PyObject* result = 0;
|
|
|
|
|
PyObject* method = 0;
|
|
|
|
|
|
|
|
|
|
cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
|
|
|
|
|
if (!cursor) {
|
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
method = PyObject_GetAttrString(cursor, "executescript");
|
|
|
|
|
if (!method) {
|
2008-07-13 18:47:59 -03:00
|
|
|
|
Py_CLEAR(cursor);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
goto error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = PyObject_CallObject(method, args);
|
|
|
|
|
if (!result) {
|
2008-07-13 18:47:59 -03:00
|
|
|
|
Py_CLEAR(cursor);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
Py_XDECREF(result);
|
|
|
|
|
Py_XDECREF(method);
|
|
|
|
|
|
|
|
|
|
return cursor;
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-04 03:29:05 -03:00
|
|
|
|
/* ------------------------- COLLATION CODE ------------------------ */
|
|
|
|
|
|
|
|
|
|
static int
|
2007-01-13 21:43:50 -04:00
|
|
|
|
pysqlite_collation_callback(
|
2006-04-04 03:29:05 -03:00
|
|
|
|
void* context,
|
|
|
|
|
int text1_length, const void* text1_data,
|
|
|
|
|
int text2_length, const void* text2_data)
|
|
|
|
|
{
|
|
|
|
|
PyObject* callback = (PyObject*)context;
|
|
|
|
|
PyObject* string1 = 0;
|
|
|
|
|
PyObject* string2 = 0;
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#ifdef WITH_THREAD
|
2006-04-04 03:29:05 -03:00
|
|
|
|
PyGILState_STATE gilstate;
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#endif
|
2006-04-04 03:29:05 -03:00
|
|
|
|
PyObject* retval = NULL;
|
2013-02-07 10:59:34 -04:00
|
|
|
|
long longval;
|
2006-04-04 03:29:05 -03:00
|
|
|
|
int result = 0;
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#ifdef WITH_THREAD
|
2006-04-04 03:29:05 -03:00
|
|
|
|
gilstate = PyGILState_Ensure();
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#endif
|
2006-04-04 03:29:05 -03:00
|
|
|
|
|
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
|
goto finally;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-09 01:58:54 -03:00
|
|
|
|
string1 = PyString_FromStringAndSize((const char*)text1_data, text1_length);
|
|
|
|
|
string2 = PyString_FromStringAndSize((const char*)text2_data, text2_length);
|
2006-04-04 03:29:05 -03:00
|
|
|
|
|
|
|
|
|
if (!string1 || !string2) {
|
|
|
|
|
goto finally; /* failed to allocate strings */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
|
|
|
|
|
|
|
|
|
|
if (!retval) {
|
|
|
|
|
/* execution failed */
|
|
|
|
|
goto finally;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 10:59:34 -04:00
|
|
|
|
longval = PyLong_AsLongAndOverflow(retval, &result);
|
|
|
|
|
if (longval == -1 && PyErr_Occurred()) {
|
|
|
|
|
PyErr_Clear();
|
2006-04-04 03:29:05 -03:00
|
|
|
|
result = 0;
|
|
|
|
|
}
|
2013-02-07 10:59:34 -04:00
|
|
|
|
else if (!result) {
|
|
|
|
|
if (longval > 0)
|
|
|
|
|
result = 1;
|
|
|
|
|
else if (longval < 0)
|
|
|
|
|
result = -1;
|
|
|
|
|
}
|
2006-04-04 03:29:05 -03:00
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
Py_XDECREF(string1);
|
|
|
|
|
Py_XDECREF(string2);
|
|
|
|
|
Py_XDECREF(retval);
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#ifdef WITH_THREAD
|
2006-04-04 03:29:05 -03:00
|
|
|
|
PyGILState_Release(gilstate);
|
2009-04-05 08:07:14 -03:00
|
|
|
|
#endif
|
2006-04-04 03:29:05 -03:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-13 19:24:47 -03:00
|
|
|
|
static PyObject *
|
2007-01-13 21:43:50 -04:00
|
|
|
|
pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
|
2006-06-13 19:24:47 -03:00
|
|
|
|
{
|
|
|
|
|
PyObject* retval = NULL;
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
if (!pysqlite_check_connection(self)) {
|
2006-06-13 19:24:47 -03:00
|
|
|
|
goto finally;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sqlite3_interrupt(self->db);
|
|
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
retval = Py_None;
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-28 05:32:09 -03:00
|
|
|
|
/* Function author: Paul Kippes <kippesp@gmail.com>
|
|
|
|
|
* Class method of Connection to call the Python function _iterdump
|
|
|
|
|
* of the sqlite3 module.
|
|
|
|
|
*/
|
|
|
|
|
static PyObject *
|
|
|
|
|
pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
|
|
|
|
|
{
|
|
|
|
|
PyObject* retval = NULL;
|
|
|
|
|
PyObject* module = NULL;
|
|
|
|
|
PyObject* module_dict;
|
|
|
|
|
PyObject* pyfn_iterdump;
|
|
|
|
|
|
|
|
|
|
if (!pysqlite_check_connection(self)) {
|
|
|
|
|
goto finally;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module = PyImport_ImportModule(MODULE_NAME ".dump");
|
|
|
|
|
if (!module) {
|
|
|
|
|
goto finally;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module_dict = PyModule_GetDict(module);
|
|
|
|
|
if (!module_dict) {
|
|
|
|
|
goto finally;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
|
|
|
|
|
if (!pyfn_iterdump) {
|
|
|
|
|
PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
|
|
|
|
|
goto finally;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
args = PyTuple_New(1);
|
|
|
|
|
if (!args) {
|
|
|
|
|
goto finally;
|
|
|
|
|
}
|
|
|
|
|
Py_INCREF(self);
|
|
|
|
|
PyTuple_SetItem(args, 0, (PyObject*)self);
|
|
|
|
|
retval = PyObject_CallObject(pyfn_iterdump, args);
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
Py_XDECREF(args);
|
|
|
|
|
Py_XDECREF(module);
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-04 03:29:05 -03:00
|
|
|
|
static PyObject *
|
2007-01-13 21:43:50 -04:00
|
|
|
|
pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
|
2006-04-04 03:29:05 -03:00
|
|
|
|
{
|
|
|
|
|
PyObject* callable;
|
|
|
|
|
PyObject* uppercase_name = 0;
|
|
|
|
|
PyObject* name;
|
|
|
|
|
PyObject* retval;
|
|
|
|
|
char* chk;
|
|
|
|
|
int rc;
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
|
2006-04-04 03:29:05 -03:00
|
|
|
|
goto finally;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-09 01:58:54 -03:00
|
|
|
|
if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyString_Type, &name, &callable)) {
|
2006-04-04 03:29:05 -03:00
|
|
|
|
goto finally;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uppercase_name = PyObject_CallMethod(name, "upper", "");
|
|
|
|
|
if (!uppercase_name) {
|
|
|
|
|
goto finally;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-09 01:58:54 -03:00
|
|
|
|
chk = PyString_AsString(uppercase_name);
|
2006-04-04 03:29:05 -03:00
|
|
|
|
while (*chk) {
|
|
|
|
|
if ((*chk >= '0' && *chk <= '9')
|
|
|
|
|
|| (*chk >= 'A' && *chk <= 'Z')
|
|
|
|
|
|| (*chk == '_'))
|
|
|
|
|
{
|
|
|
|
|
chk++;
|
|
|
|
|
} else {
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
|
2006-04-04 03:29:05 -03:00
|
|
|
|
goto finally;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (callable != Py_None && !PyCallable_Check(callable)) {
|
|
|
|
|
PyErr_SetString(PyExc_TypeError, "parameter must be callable");
|
|
|
|
|
goto finally;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (callable != Py_None) {
|
2010-03-05 05:12:37 -04:00
|
|
|
|
if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
|
|
|
|
|
goto finally;
|
2006-04-04 03:29:05 -03:00
|
|
|
|
} else {
|
2010-03-05 05:12:37 -04:00
|
|
|
|
if (PyDict_DelItem(self->collations, uppercase_name) == -1)
|
|
|
|
|
goto finally;
|
2006-04-04 03:29:05 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = sqlite3_create_collation(self->db,
|
2008-06-09 01:58:54 -03:00
|
|
|
|
PyString_AsString(uppercase_name),
|
2006-04-04 03:29:05 -03:00
|
|
|
|
SQLITE_UTF8,
|
|
|
|
|
(callable != Py_None) ? callable : NULL,
|
2007-01-13 21:43:50 -04:00
|
|
|
|
(callable != Py_None) ? pysqlite_collation_callback : NULL);
|
2006-04-04 03:29:05 -03:00
|
|
|
|
if (rc != SQLITE_OK) {
|
|
|
|
|
PyDict_DelItem(self->collations, uppercase_name);
|
2008-02-29 18:08:41 -04:00
|
|
|
|
_pysqlite_seterror(self->db, NULL);
|
2006-04-04 03:29:05 -03:00
|
|
|
|
goto finally;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
Py_XDECREF(uppercase_name);
|
|
|
|
|
|
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
|
retval = NULL;
|
|
|
|
|
} else {
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
|
retval = Py_None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-29 18:08:41 -04:00
|
|
|
|
/* Called when the connection is used as a context manager. Returns itself as a
|
|
|
|
|
* convenience to the caller. */
|
|
|
|
|
static PyObject *
|
|
|
|
|
pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
|
|
|
|
|
{
|
|
|
|
|
Py_INCREF(self);
|
|
|
|
|
return (PyObject*)self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Called when the connection is used as a context manager. If there was any
|
|
|
|
|
* exception, a rollback takes place; otherwise we commit. */
|
|
|
|
|
static PyObject *
|
|
|
|
|
pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
|
|
|
|
|
{
|
|
|
|
|
PyObject* exc_type, *exc_value, *exc_tb;
|
|
|
|
|
char* method_name;
|
|
|
|
|
PyObject* result;
|
|
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
|
|
|
|
|
method_name = "commit";
|
|
|
|
|
} else {
|
|
|
|
|
method_name = "rollback";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = PyObject_CallMethod((PyObject*)self, method_name, "");
|
|
|
|
|
if (!result) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
Py_DECREF(result);
|
|
|
|
|
|
|
|
|
|
Py_INCREF(Py_False);
|
|
|
|
|
return Py_False;
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-31 20:57:31 -04:00
|
|
|
|
static char connection_doc[] =
|
2006-04-23 12:24:26 -03:00
|
|
|
|
PyDoc_STR("SQLite database connection object.");
|
2006-03-31 20:57:31 -04:00
|
|
|
|
|
|
|
|
|
static PyGetSetDef connection_getset[] = {
|
2007-01-13 21:43:50 -04:00
|
|
|
|
{"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
|
|
|
|
|
{"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{NULL}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static PyMethodDef connection_methods[] = {
|
2007-01-13 21:43:50 -04:00
|
|
|
|
{"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyDoc_STR("Return a cursor for the connection.")},
|
2007-01-13 21:43:50 -04:00
|
|
|
|
{"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyDoc_STR("Closes the connection.")},
|
2007-01-13 21:43:50 -04:00
|
|
|
|
{"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyDoc_STR("Commit the current transaction.")},
|
2007-01-13 21:43:50 -04:00
|
|
|
|
{"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyDoc_STR("Roll back the current transaction.")},
|
2007-01-13 21:43:50 -04:00
|
|
|
|
{"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyDoc_STR("Creates a new function. Non-standard.")},
|
2007-01-13 21:43:50 -04:00
|
|
|
|
{"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyDoc_STR("Creates a new aggregate. Non-standard.")},
|
2007-01-13 21:43:50 -04:00
|
|
|
|
{"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
|
2006-06-13 19:24:47 -03:00
|
|
|
|
PyDoc_STR("Sets authorizer callback. Non-standard.")},
|
2010-03-05 05:12:37 -04:00
|
|
|
|
#ifdef HAVE_LOAD_EXTENSION
|
|
|
|
|
{"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
|
|
|
|
|
PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
|
|
|
|
|
{"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
|
|
|
|
|
PyDoc_STR("Load SQLite extension module. Non-standard.")},
|
|
|
|
|
#endif
|
2008-02-29 18:08:41 -04:00
|
|
|
|
{"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
|
|
|
|
|
PyDoc_STR("Sets progress handler callback. Non-standard.")},
|
2007-01-13 21:43:50 -04:00
|
|
|
|
{"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyDoc_STR("Executes a SQL statement. Non-standard.")},
|
2007-01-13 21:43:50 -04:00
|
|
|
|
{"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
|
2007-01-13 21:43:50 -04:00
|
|
|
|
{"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
|
2006-03-31 20:57:31 -04:00
|
|
|
|
PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
|
2007-01-13 21:43:50 -04:00
|
|
|
|
{"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
|
2006-04-23 12:24:26 -03:00
|
|
|
|
PyDoc_STR("Creates a collation function. Non-standard.")},
|
2007-01-13 21:43:50 -04:00
|
|
|
|
{"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
|
2006-06-13 19:24:47 -03:00
|
|
|
|
PyDoc_STR("Abort any pending database operation. Non-standard.")},
|
2008-03-28 05:32:09 -03:00
|
|
|
|
{"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
|
2008-09-12 10:54:06 -03:00
|
|
|
|
PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
|
2008-02-29 18:08:41 -04:00
|
|
|
|
{"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
|
|
|
|
|
PyDoc_STR("For context manager. Non-standard.")},
|
|
|
|
|
{"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
|
|
|
|
|
PyDoc_STR("For context manager. Non-standard.")},
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{NULL, NULL}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct PyMemberDef connection_members[] =
|
|
|
|
|
{
|
2007-01-13 21:43:50 -04:00
|
|
|
|
{"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), RO},
|
|
|
|
|
{"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), RO},
|
|
|
|
|
{"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), RO},
|
|
|
|
|
{"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), RO},
|
|
|
|
|
{"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), RO},
|
|
|
|
|
{"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), RO},
|
|
|
|
|
{"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), RO},
|
|
|
|
|
{"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), RO},
|
|
|
|
|
{"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), RO},
|
|
|
|
|
{"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), RO},
|
|
|
|
|
{"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
|
|
|
|
|
{"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{NULL}
|
|
|
|
|
};
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
PyTypeObject pysqlite_ConnectionType = {
|
2007-07-21 03:55:02 -03:00
|
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
2006-04-05 15:25:33 -03:00
|
|
|
|
MODULE_NAME ".Connection", /* tp_name */
|
2007-01-13 21:43:50 -04:00
|
|
|
|
sizeof(pysqlite_Connection), /* tp_basicsize */
|
2006-03-31 20:57:31 -04:00
|
|
|
|
0, /* tp_itemsize */
|
2007-01-13 21:43:50 -04:00
|
|
|
|
(destructor)pysqlite_connection_dealloc, /* tp_dealloc */
|
2006-03-31 20:57:31 -04:00
|
|
|
|
0, /* tp_print */
|
|
|
|
|
0, /* tp_getattr */
|
|
|
|
|
0, /* tp_setattr */
|
|
|
|
|
0, /* tp_compare */
|
|
|
|
|
0, /* tp_repr */
|
|
|
|
|
0, /* tp_as_number */
|
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
|
0, /* tp_hash */
|
2007-01-13 21:43:50 -04:00
|
|
|
|
(ternaryfunc)pysqlite_connection_call, /* tp_call */
|
2006-03-31 20:57:31 -04:00
|
|
|
|
0, /* tp_str */
|
|
|
|
|
0, /* tp_getattro */
|
|
|
|
|
0, /* tp_setattro */
|
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
|
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
|
|
|
|
|
connection_doc, /* tp_doc */
|
|
|
|
|
0, /* tp_traverse */
|
|
|
|
|
0, /* tp_clear */
|
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
|
0, /* tp_iter */
|
|
|
|
|
0, /* tp_iternext */
|
|
|
|
|
connection_methods, /* tp_methods */
|
|
|
|
|
connection_members, /* tp_members */
|
|
|
|
|
connection_getset, /* tp_getset */
|
|
|
|
|
0, /* tp_base */
|
|
|
|
|
0, /* tp_dict */
|
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
|
0, /* tp_dictoffset */
|
2007-01-13 21:43:50 -04:00
|
|
|
|
(initproc)pysqlite_connection_init, /* tp_init */
|
2006-03-31 20:57:31 -04:00
|
|
|
|
0, /* tp_alloc */
|
|
|
|
|
0, /* tp_new */
|
|
|
|
|
0 /* tp_free */
|
|
|
|
|
};
|
|
|
|
|
|
2007-01-13 21:43:50 -04:00
|
|
|
|
extern int pysqlite_connection_setup_types(void)
|
2006-03-31 20:57:31 -04:00
|
|
|
|
{
|
2007-01-13 21:43:50 -04:00
|
|
|
|
pysqlite_ConnectionType.tp_new = PyType_GenericNew;
|
|
|
|
|
return PyType_Ready(&pysqlite_ConnectionType);
|
2006-03-31 20:57:31 -04:00
|
|
|
|
}
|