1 /* connection.c - the connection type
3 * Copyright (C) 2004-2007 Gerhard Häring <gh@ghaering.de>
5 * This file is part of pysqlite.
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.
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:
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.
26 #include "connection.h"
27 #include "statement.h"
29 #include "prepare_protocol.h"
31 #include "sqlitecompat.h"
35 #define ACTION_FINALIZE 1
36 #define ACTION_RESET 2
38 static int pysqlite_connection_set_isolation_level(pysqlite_Connection
* self
, PyObject
* isolation_level
);
41 static void _sqlite3_result_error(sqlite3_context
* ctx
, const char* errmsg
, int len
)
43 /* in older SQLite versions, calling sqlite3_result_error in callbacks
44 * triggers a bug in SQLite that leads either to irritating results or
45 * segfaults, depending on the SQLite version */
46 #if SQLITE_VERSION_NUMBER >= 3003003
47 sqlite3_result_error(ctx
, errmsg
, len
);
49 PyErr_SetString(pysqlite_OperationalError
, errmsg
);
53 int pysqlite_connection_init(pysqlite_Connection
* self
, PyObject
* args
, PyObject
* kwargs
)
55 static char *kwlist
[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL
, NULL
};
59 PyObject
* isolation_level
= NULL
;
60 PyObject
* factory
= NULL
;
61 int check_same_thread
= 1;
62 int cached_statements
= 100;
65 PyObject
* class_attr
= NULL
;
66 PyObject
* class_attr_str
= NULL
;
67 int is_apsw_connection
= 0;
68 PyObject
* database_utf8
;
70 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|diOiOi", kwlist
,
71 &database
, &timeout
, &detect_types
, &isolation_level
, &check_same_thread
, &factory
, &cached_statements
))
76 self
->begin_statement
= NULL
;
78 self
->statement_cache
= NULL
;
79 self
->statements
= NULL
;
82 self
->row_factory
= Py_None
;
84 Py_INCREF(&PyUnicode_Type
);
85 self
->text_factory
= (PyObject
*)&PyUnicode_Type
;
87 if (PyString_Check(database
) || PyUnicode_Check(database
)) {
88 if (PyString_Check(database
)) {
89 database_utf8
= database
;
90 Py_INCREF(database_utf8
);
92 database_utf8
= PyUnicode_AsUTF8String(database
);
98 Py_BEGIN_ALLOW_THREADS
99 rc
= sqlite3_open(PyString_AsString(database_utf8
), &self
->db
);
102 Py_DECREF(database_utf8
);
104 if (rc
!= SQLITE_OK
) {
105 _pysqlite_seterror(self
->db
, NULL
);
109 /* Create a pysqlite connection from a APSW connection */
110 class_attr
= PyObject_GetAttrString(database
, "__class__");
112 class_attr_str
= PyObject_Str(class_attr
);
113 if (class_attr_str
) {
114 if (strcmp(PyString_AsString(class_attr_str
), "<type 'apsw.Connection'>") == 0) {
115 /* In the APSW Connection object, the first entry after
116 * PyObject_HEAD is the sqlite3* we want to get hold of.
117 * Luckily, this is the same layout as we have in our
118 * pysqlite_Connection */
119 self
->db
= ((pysqlite_Connection
*)database
)->db
;
122 self
->apsw_connection
= database
;
123 is_apsw_connection
= 1;
127 Py_XDECREF(class_attr_str
);
128 Py_XDECREF(class_attr
);
130 if (!is_apsw_connection
) {
131 PyErr_SetString(PyExc_ValueError
, "database parameter must be string or APSW Connection object");
136 if (!isolation_level
) {
137 isolation_level
= PyString_FromString("");
138 if (!isolation_level
) {
142 Py_INCREF(isolation_level
);
144 self
->isolation_level
= NULL
;
145 pysqlite_connection_set_isolation_level(self
, isolation_level
);
146 Py_DECREF(isolation_level
);
148 self
->statement_cache
= (pysqlite_Cache
*)PyObject_CallFunction((PyObject
*)&pysqlite_CacheType
, "Oi", self
, cached_statements
);
149 if (PyErr_Occurred()) {
153 self
->statements
= PyList_New(0);
154 if (!self
->statements
) {
157 self
->created_statements
= 0;
159 /* By default, the Cache class INCREFs the factory in its initializer, and
160 * decrefs it in its deallocator method. Since this would create a circular
161 * reference here, we're breaking it by decrementing self, and telling the
162 * cache class to not decref the factory (self) in its deallocator.
164 self
->statement_cache
->decref_factory
= 0;
167 self
->inTransaction
= 0;
168 self
->detect_types
= detect_types
;
169 self
->timeout
= timeout
;
170 (void)sqlite3_busy_timeout(self
->db
, (int)(timeout
*1000));
172 self
->thread_ident
= PyThread_get_thread_ident();
173 self
->check_same_thread
= check_same_thread
;
175 self
->function_pinboard
= PyDict_New();
176 if (!self
->function_pinboard
) {
180 self
->collations
= PyDict_New();
181 if (!self
->collations
) {
185 self
->Warning
= pysqlite_Warning
;
186 self
->Error
= pysqlite_Error
;
187 self
->InterfaceError
= pysqlite_InterfaceError
;
188 self
->DatabaseError
= pysqlite_DatabaseError
;
189 self
->DataError
= pysqlite_DataError
;
190 self
->OperationalError
= pysqlite_OperationalError
;
191 self
->IntegrityError
= pysqlite_IntegrityError
;
192 self
->InternalError
= pysqlite_InternalError
;
193 self
->ProgrammingError
= pysqlite_ProgrammingError
;
194 self
->NotSupportedError
= pysqlite_NotSupportedError
;
199 /* Empty the entire statement cache of this connection */
200 void pysqlite_flush_statement_cache(pysqlite_Connection
* self
)
203 pysqlite_Statement
* statement
;
205 node
= self
->statement_cache
->first
;
208 statement
= (pysqlite_Statement
*)(node
->data
);
209 (void)pysqlite_statement_finalize(statement
);
213 Py_DECREF(self
->statement_cache
);
214 self
->statement_cache
= (pysqlite_Cache
*)PyObject_CallFunction((PyObject
*)&pysqlite_CacheType
, "O", self
);
216 self
->statement_cache
->decref_factory
= 0;
219 /* action in (ACTION_RESET, ACTION_FINALIZE) */
220 void pysqlite_do_all_statements(pysqlite_Connection
* self
, int action
)
226 for (i
= 0; i
< PyList_Size(self
->statements
); i
++) {
227 weakref
= PyList_GetItem(self
->statements
, i
);
228 statement
= PyWeakref_GetObject(weakref
);
229 if (statement
!= Py_None
) {
230 if (action
== ACTION_RESET
) {
231 (void)pysqlite_statement_reset((pysqlite_Statement
*)statement
);
233 (void)pysqlite_statement_finalize((pysqlite_Statement
*)statement
);
239 void pysqlite_connection_dealloc(pysqlite_Connection
* self
)
241 PyObject
* ret
= NULL
;
243 Py_XDECREF(self
->statement_cache
);
245 /* Clean up if user has not called .close() explicitly. */
247 Py_BEGIN_ALLOW_THREADS
248 sqlite3_close(self
->db
);
250 } else if (self
->apsw_connection
) {
251 ret
= PyObject_CallMethod(self
->apsw_connection
, "close", "");
253 Py_XDECREF(self
->apsw_connection
);
256 if (self
->begin_statement
) {
257 PyMem_Free(self
->begin_statement
);
259 Py_XDECREF(self
->isolation_level
);
260 Py_XDECREF(self
->function_pinboard
);
261 Py_XDECREF(self
->row_factory
);
262 Py_XDECREF(self
->text_factory
);
263 Py_XDECREF(self
->collations
);
264 Py_XDECREF(self
->statements
);
266 self
->ob_type
->tp_free((PyObject
*)self
);
269 PyObject
* pysqlite_connection_cursor(pysqlite_Connection
* self
, PyObject
* args
, PyObject
* kwargs
)
271 static char *kwlist
[] = {"factory", NULL
, NULL
};
272 PyObject
* factory
= NULL
;
276 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|O", kwlist
,
281 if (!pysqlite_check_thread(self
) || !pysqlite_check_connection(self
)) {
285 if (factory
== NULL
) {
286 factory
= (PyObject
*)&pysqlite_CursorType
;
289 cursor
= PyObject_CallFunction(factory
, "O", self
);
291 if (cursor
&& self
->row_factory
!= Py_None
) {
292 Py_XDECREF(((pysqlite_Cursor
*)cursor
)->row_factory
);
293 Py_INCREF(self
->row_factory
);
294 ((pysqlite_Cursor
*)cursor
)->row_factory
= self
->row_factory
;
300 PyObject
* pysqlite_connection_close(pysqlite_Connection
* self
, PyObject
* args
)
305 if (!pysqlite_check_thread(self
)) {
309 pysqlite_do_all_statements(self
, ACTION_FINALIZE
);
312 if (self
->apsw_connection
) {
313 ret
= PyObject_CallMethod(self
->apsw_connection
, "close", "");
315 Py_XDECREF(self
->apsw_connection
);
316 self
->apsw_connection
= NULL
;
319 Py_BEGIN_ALLOW_THREADS
320 rc
= sqlite3_close(self
->db
);
323 if (rc
!= SQLITE_OK
) {
324 _pysqlite_seterror(self
->db
, NULL
);
337 * Checks if a connection object is usable (i. e. not closed).
339 * 0 => error; 1 => ok
341 int pysqlite_check_connection(pysqlite_Connection
* con
)
344 PyErr_SetString(pysqlite_ProgrammingError
, "Cannot operate on a closed database.");
351 PyObject
* _pysqlite_connection_begin(pysqlite_Connection
* self
)
355 sqlite3_stmt
* statement
;
357 Py_BEGIN_ALLOW_THREADS
358 rc
= sqlite3_prepare(self
->db
, self
->begin_statement
, -1, &statement
, &tail
);
361 if (rc
!= SQLITE_OK
) {
362 _pysqlite_seterror(self
->db
, statement
);
366 rc
= pysqlite_step(statement
, self
);
367 if (rc
== SQLITE_DONE
) {
368 self
->inTransaction
= 1;
370 _pysqlite_seterror(self
->db
, statement
);
373 Py_BEGIN_ALLOW_THREADS
374 rc
= sqlite3_finalize(statement
);
377 if (rc
!= SQLITE_OK
&& !PyErr_Occurred()) {
378 _pysqlite_seterror(self
->db
, NULL
);
382 if (PyErr_Occurred()) {
390 PyObject
* pysqlite_connection_commit(pysqlite_Connection
* self
, PyObject
* args
)
394 sqlite3_stmt
* statement
;
396 if (!pysqlite_check_thread(self
) || !pysqlite_check_connection(self
)) {
400 if (self
->inTransaction
) {
401 Py_BEGIN_ALLOW_THREADS
402 rc
= sqlite3_prepare(self
->db
, "COMMIT", -1, &statement
, &tail
);
404 if (rc
!= SQLITE_OK
) {
405 _pysqlite_seterror(self
->db
, NULL
);
409 rc
= pysqlite_step(statement
, self
);
410 if (rc
== SQLITE_DONE
) {
411 self
->inTransaction
= 0;
413 _pysqlite_seterror(self
->db
, statement
);
416 Py_BEGIN_ALLOW_THREADS
417 rc
= sqlite3_finalize(statement
);
419 if (rc
!= SQLITE_OK
&& !PyErr_Occurred()) {
420 _pysqlite_seterror(self
->db
, NULL
);
426 if (PyErr_Occurred()) {
434 PyObject
* pysqlite_connection_rollback(pysqlite_Connection
* self
, PyObject
* args
)
438 sqlite3_stmt
* statement
;
440 if (!pysqlite_check_thread(self
) || !pysqlite_check_connection(self
)) {
444 if (self
->inTransaction
) {
445 pysqlite_do_all_statements(self
, ACTION_RESET
);
447 Py_BEGIN_ALLOW_THREADS
448 rc
= sqlite3_prepare(self
->db
, "ROLLBACK", -1, &statement
, &tail
);
450 if (rc
!= SQLITE_OK
) {
451 _pysqlite_seterror(self
->db
, NULL
);
455 rc
= pysqlite_step(statement
, self
);
456 if (rc
== SQLITE_DONE
) {
457 self
->inTransaction
= 0;
459 _pysqlite_seterror(self
->db
, statement
);
462 Py_BEGIN_ALLOW_THREADS
463 rc
= sqlite3_finalize(statement
);
465 if (rc
!= SQLITE_OK
&& !PyErr_Occurred()) {
466 _pysqlite_seterror(self
->db
, NULL
);
472 if (PyErr_Occurred()) {
480 void _pysqlite_set_result(sqlite3_context
* context
, PyObject
* py_val
)
487 if ((!py_val
) || PyErr_Occurred()) {
488 sqlite3_result_null(context
);
489 } else if (py_val
== Py_None
) {
490 sqlite3_result_null(context
);
491 } else if (PyInt_Check(py_val
)) {
492 longval
= PyInt_AsLong(py_val
);
493 sqlite3_result_int64(context
, (PY_LONG_LONG
)longval
);
494 } else if (PyFloat_Check(py_val
)) {
495 sqlite3_result_double(context
, PyFloat_AsDouble(py_val
));
496 } else if (PyBuffer_Check(py_val
)) {
497 if (PyObject_AsCharBuffer(py_val
, &buffer
, &buflen
) != 0) {
498 PyErr_SetString(PyExc_ValueError
, "could not convert BLOB to buffer");
500 sqlite3_result_blob(context
, buffer
, buflen
, SQLITE_TRANSIENT
);
502 } else if (PyString_Check(py_val
)) {
503 sqlite3_result_text(context
, PyString_AsString(py_val
), -1, SQLITE_TRANSIENT
);
504 } else if (PyUnicode_Check(py_val
)) {
505 stringval
= PyUnicode_AsUTF8String(py_val
);
507 sqlite3_result_text(context
, PyString_AsString(stringval
), -1, SQLITE_TRANSIENT
);
508 Py_DECREF(stringval
);
511 /* TODO: raise error */
515 PyObject
* _pysqlite_build_py_params(sqlite3_context
*context
, int argc
, sqlite3_value
** argv
)
519 sqlite3_value
* cur_value
;
520 PyObject
* cur_py_value
;
522 PY_LONG_LONG val_int
;
526 args
= PyTuple_New(argc
);
531 for (i
= 0; i
< argc
; i
++) {
533 switch (sqlite3_value_type(argv
[i
])) {
535 val_int
= sqlite3_value_int64(cur_value
);
536 cur_py_value
= PyInt_FromLong((long)val_int
);
539 cur_py_value
= PyFloat_FromDouble(sqlite3_value_double(cur_value
));
542 val_str
= (const char*)sqlite3_value_text(cur_value
);
543 cur_py_value
= PyUnicode_DecodeUTF8(val_str
, strlen(val_str
), NULL
);
544 /* TODO: have a way to show errors here */
548 cur_py_value
= Py_None
;
552 buflen
= sqlite3_value_bytes(cur_value
);
553 cur_py_value
= PyBuffer_New(buflen
);
557 if (PyObject_AsWriteBuffer(cur_py_value
, &raw_buffer
, &buflen
)) {
558 Py_DECREF(cur_py_value
);
562 memcpy(raw_buffer
, sqlite3_value_blob(cur_value
), buflen
);
567 cur_py_value
= Py_None
;
575 PyTuple_SetItem(args
, i
, cur_py_value
);
582 void _pysqlite_func_callback(sqlite3_context
* context
, int argc
, sqlite3_value
** argv
)
586 PyObject
* py_retval
= NULL
;
588 PyGILState_STATE threadstate
;
590 threadstate
= PyGILState_Ensure();
592 py_func
= (PyObject
*)sqlite3_user_data(context
);
594 args
= _pysqlite_build_py_params(context
, argc
, argv
);
596 py_retval
= PyObject_CallObject(py_func
, args
);
601 _pysqlite_set_result(context
, py_retval
);
602 Py_DECREF(py_retval
);
604 if (_enable_callback_tracebacks
) {
609 _sqlite3_result_error(context
, "user-defined function raised exception", -1);
612 PyGILState_Release(threadstate
);
615 static void _pysqlite_step_callback(sqlite3_context
*context
, int argc
, sqlite3_value
** params
)
618 PyObject
* function_result
= NULL
;
619 PyObject
* aggregate_class
;
620 PyObject
** aggregate_instance
;
621 PyObject
* stepmethod
= NULL
;
623 PyGILState_STATE threadstate
;
625 threadstate
= PyGILState_Ensure();
627 aggregate_class
= (PyObject
*)sqlite3_user_data(context
);
629 aggregate_instance
= (PyObject
**)sqlite3_aggregate_context(context
, sizeof(PyObject
*));
631 if (*aggregate_instance
== 0) {
632 *aggregate_instance
= PyObject_CallFunction(aggregate_class
, "");
634 if (PyErr_Occurred()) {
635 *aggregate_instance
= 0;
636 if (_enable_callback_tracebacks
) {
641 _sqlite3_result_error(context
, "user-defined aggregate's '__init__' method raised error", -1);
646 stepmethod
= PyObject_GetAttrString(*aggregate_instance
, "step");
651 args
= _pysqlite_build_py_params(context
, argc
, params
);
656 function_result
= PyObject_CallObject(stepmethod
, args
);
659 if (!function_result
) {
660 if (_enable_callback_tracebacks
) {
665 _sqlite3_result_error(context
, "user-defined aggregate's 'step' method raised error", -1);
669 Py_XDECREF(stepmethod
);
670 Py_XDECREF(function_result
);
672 PyGILState_Release(threadstate
);
675 void _pysqlite_final_callback(sqlite3_context
* context
)
677 PyObject
* function_result
= NULL
;
678 PyObject
** aggregate_instance
;
679 PyObject
* aggregate_class
;
681 PyGILState_STATE threadstate
;
683 threadstate
= PyGILState_Ensure();
685 aggregate_class
= (PyObject
*)sqlite3_user_data(context
);
687 aggregate_instance
= (PyObject
**)sqlite3_aggregate_context(context
, sizeof(PyObject
*));
688 if (!*aggregate_instance
) {
689 /* this branch is executed if there was an exception in the aggregate's
695 function_result
= PyObject_CallMethod(*aggregate_instance
, "finalize", "");
696 if (!function_result
) {
697 if (_enable_callback_tracebacks
) {
702 _sqlite3_result_error(context
, "user-defined aggregate's 'finalize' method raised error", -1);
704 _pysqlite_set_result(context
, function_result
);
708 Py_XDECREF(*aggregate_instance
);
709 Py_XDECREF(function_result
);
711 PyGILState_Release(threadstate
);
714 void _pysqlite_drop_unused_statement_references(pysqlite_Connection
* self
)
720 /* we only need to do this once in a while */
721 if (self
->created_statements
++ < 200) {
725 self
->created_statements
= 0;
727 new_list
= PyList_New(0);
732 for (i
= 0; i
< PyList_Size(self
->statements
); i
++) {
733 weakref
= PyList_GetItem(self
->statements
, i
);
734 if (PyWeakref_GetObject(weakref
) != Py_None
) {
735 if (PyList_Append(new_list
, weakref
) != 0) {
742 Py_DECREF(self
->statements
);
743 self
->statements
= new_list
;
746 PyObject
* pysqlite_connection_create_function(pysqlite_Connection
* self
, PyObject
* args
, PyObject
* kwargs
)
748 static char *kwlist
[] = {"name", "narg", "func", NULL
, NULL
};
755 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "siO", kwlist
,
756 &name
, &narg
, &func
))
761 rc
= sqlite3_create_function(self
->db
, name
, narg
, SQLITE_UTF8
, (void*)func
, _pysqlite_func_callback
, NULL
, NULL
);
763 if (rc
!= SQLITE_OK
) {
764 /* Workaround for SQLite bug: no error code or string is available here */
765 PyErr_SetString(pysqlite_OperationalError
, "Error creating function");
768 PyDict_SetItem(self
->function_pinboard
, func
, Py_None
);
775 PyObject
* pysqlite_connection_create_aggregate(pysqlite_Connection
* self
, PyObject
* args
, PyObject
* kwargs
)
777 PyObject
* aggregate_class
;
781 static char *kwlist
[] = { "name", "n_arg", "aggregate_class", NULL
};
784 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "siO:create_aggregate",
785 kwlist
, &name
, &n_arg
, &aggregate_class
)) {
789 rc
= sqlite3_create_function(self
->db
, name
, n_arg
, SQLITE_UTF8
, (void*)aggregate_class
, 0, &_pysqlite_step_callback
, &_pysqlite_final_callback
);
790 if (rc
!= SQLITE_OK
) {
791 /* Workaround for SQLite bug: no error code or string is available here */
792 PyErr_SetString(pysqlite_OperationalError
, "Error creating aggregate");
795 PyDict_SetItem(self
->function_pinboard
, aggregate_class
, Py_None
);
802 static int _authorizer_callback(void* user_arg
, int action
, const char* arg1
, const char* arg2
, const char* dbname
, const char* access_attempt_source
)
806 PyGILState_STATE gilstate
;
808 gilstate
= PyGILState_Ensure();
809 ret
= PyObject_CallFunction((PyObject
*)user_arg
, "issss", action
, arg1
, arg2
, dbname
, access_attempt_source
);
812 if (_enable_callback_tracebacks
) {
820 if (PyInt_Check(ret
)) {
821 rc
= (int)PyInt_AsLong(ret
);
828 PyGILState_Release(gilstate
);
832 static int _progress_handler(void* user_arg
)
836 PyGILState_STATE gilstate
;
838 gilstate
= PyGILState_Ensure();
839 ret
= PyObject_CallFunction((PyObject
*)user_arg
, "");
842 if (_enable_callback_tracebacks
) {
848 /* abort query if error occured */
851 rc
= (int)PyObject_IsTrue(ret
);
855 PyGILState_Release(gilstate
);
859 PyObject
* pysqlite_connection_set_authorizer(pysqlite_Connection
* self
, PyObject
* args
, PyObject
* kwargs
)
861 PyObject
* authorizer_cb
;
863 static char *kwlist
[] = { "authorizer_callback", NULL
};
866 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O:set_authorizer",
867 kwlist
, &authorizer_cb
)) {
871 rc
= sqlite3_set_authorizer(self
->db
, _authorizer_callback
, (void*)authorizer_cb
);
873 if (rc
!= SQLITE_OK
) {
874 PyErr_SetString(pysqlite_OperationalError
, "Error setting authorizer callback");
877 PyDict_SetItem(self
->function_pinboard
, authorizer_cb
, Py_None
);
884 PyObject
* pysqlite_connection_set_progress_handler(pysqlite_Connection
* self
, PyObject
* args
, PyObject
* kwargs
)
886 PyObject
* progress_handler
;
889 static char *kwlist
[] = { "progress_handler", "n", NULL
};
891 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "Oi:set_progress_handler",
892 kwlist
, &progress_handler
, &n
)) {
896 if (progress_handler
== Py_None
) {
897 /* None clears the progress handler previously set */
898 sqlite3_progress_handler(self
->db
, 0, 0, (void*)0);
900 sqlite3_progress_handler(self
->db
, n
, _progress_handler
, progress_handler
);
901 PyDict_SetItem(self
->function_pinboard
, progress_handler
, Py_None
);
908 int pysqlite_check_thread(pysqlite_Connection
* self
)
910 if (self
->check_same_thread
) {
911 if (PyThread_get_thread_ident() != self
->thread_ident
) {
912 PyErr_Format(pysqlite_ProgrammingError
,
913 "SQLite objects created in a thread can only be used in that same thread."
914 "The object was created in thread id %ld and this is thread id %ld",
915 self
->thread_ident
, PyThread_get_thread_ident());
924 static PyObject
* pysqlite_connection_get_isolation_level(pysqlite_Connection
* self
, void* unused
)
926 Py_INCREF(self
->isolation_level
);
927 return self
->isolation_level
;
930 static PyObject
* pysqlite_connection_get_total_changes(pysqlite_Connection
* self
, void* unused
)
932 if (!pysqlite_check_connection(self
)) {
935 return Py_BuildValue("i", sqlite3_total_changes(self
->db
));
939 static int pysqlite_connection_set_isolation_level(pysqlite_Connection
* self
, PyObject
* isolation_level
)
942 PyObject
* begin_statement
;
943 char* begin_statement_str
;
945 Py_XDECREF(self
->isolation_level
);
947 if (self
->begin_statement
) {
948 PyMem_Free(self
->begin_statement
);
949 self
->begin_statement
= NULL
;
952 if (isolation_level
== Py_None
) {
954 self
->isolation_level
= Py_None
;
956 res
= pysqlite_connection_commit(self
, NULL
);
962 self
->inTransaction
= 0;
964 Py_INCREF(isolation_level
);
965 self
->isolation_level
= isolation_level
;
967 begin_statement
= PyString_FromString("BEGIN ");
968 if (!begin_statement
) {
971 PyString_Concat(&begin_statement
, isolation_level
);
972 if (!begin_statement
) {
976 begin_statement_str
= PyString_AsString(begin_statement
);
977 if (!begin_statement_str
) {
978 Py_DECREF(begin_statement
);
981 self
->begin_statement
= PyMem_Malloc(strlen(begin_statement_str
) + 2);
982 if (!self
->begin_statement
) {
983 Py_DECREF(begin_statement
);
987 strcpy(self
->begin_statement
, begin_statement_str
);
988 Py_DECREF(begin_statement
);
994 PyObject
* pysqlite_connection_call(pysqlite_Connection
* self
, PyObject
* args
, PyObject
* kwargs
)
997 pysqlite_Statement
* statement
;
1001 if (!PyArg_ParseTuple(args
, "O", &sql
)) {
1005 _pysqlite_drop_unused_statement_references(self
);
1007 statement
= PyObject_New(pysqlite_Statement
, &pysqlite_StatementType
);
1012 rc
= pysqlite_statement_create(statement
, self
, sql
);
1014 if (rc
!= SQLITE_OK
) {
1015 if (rc
== PYSQLITE_TOO_MUCH_SQL
) {
1016 PyErr_SetString(pysqlite_Warning
, "You can only execute one statement at a time.");
1017 } else if (rc
== PYSQLITE_SQL_WRONG_TYPE
) {
1018 PyErr_SetString(pysqlite_Warning
, "SQL is of wrong type. Must be string or unicode.");
1020 (void)pysqlite_statement_reset(statement
);
1021 _pysqlite_seterror(self
->db
, NULL
);
1024 Py_CLEAR(statement
);
1026 weakref
= PyWeakref_NewRef((PyObject
*)statement
, NULL
);
1028 Py_CLEAR(statement
);
1032 if (PyList_Append(self
->statements
, weakref
) != 0) {
1041 return (PyObject
*)statement
;
1044 PyObject
* pysqlite_connection_execute(pysqlite_Connection
* self
, PyObject
* args
, PyObject
* kwargs
)
1046 PyObject
* cursor
= 0;
1047 PyObject
* result
= 0;
1048 PyObject
* method
= 0;
1050 cursor
= PyObject_CallMethod((PyObject
*)self
, "cursor", "");
1055 method
= PyObject_GetAttrString(cursor
, "execute");
1061 result
= PyObject_CallObject(method
, args
);
1073 PyObject
* pysqlite_connection_executemany(pysqlite_Connection
* self
, PyObject
* args
, PyObject
* kwargs
)
1075 PyObject
* cursor
= 0;
1076 PyObject
* result
= 0;
1077 PyObject
* method
= 0;
1079 cursor
= PyObject_CallMethod((PyObject
*)self
, "cursor", "");
1084 method
= PyObject_GetAttrString(cursor
, "executemany");
1090 result
= PyObject_CallObject(method
, args
);
1102 PyObject
* pysqlite_connection_executescript(pysqlite_Connection
* self
, PyObject
* args
, PyObject
* kwargs
)
1104 PyObject
* cursor
= 0;
1105 PyObject
* result
= 0;
1106 PyObject
* method
= 0;
1108 cursor
= PyObject_CallMethod((PyObject
*)self
, "cursor", "");
1113 method
= PyObject_GetAttrString(cursor
, "executescript");
1119 result
= PyObject_CallObject(method
, args
);
1131 /* ------------------------- COLLATION CODE ------------------------ */
1134 pysqlite_collation_callback(
1136 int text1_length
, const void* text1_data
,
1137 int text2_length
, const void* text2_data
)
1139 PyObject
* callback
= (PyObject
*)context
;
1140 PyObject
* string1
= 0;
1141 PyObject
* string2
= 0;
1142 PyGILState_STATE gilstate
;
1144 PyObject
* retval
= NULL
;
1147 gilstate
= PyGILState_Ensure();
1149 if (PyErr_Occurred()) {
1153 string1
= PyString_FromStringAndSize((const char*)text1_data
, text1_length
);
1154 string2
= PyString_FromStringAndSize((const char*)text2_data
, text2_length
);
1156 if (!string1
|| !string2
) {
1157 goto finally
; /* failed to allocate strings */
1160 retval
= PyObject_CallFunctionObjArgs(callback
, string1
, string2
, NULL
);
1163 /* execution failed */
1167 result
= PyInt_AsLong(retval
);
1168 if (PyErr_Occurred()) {
1173 Py_XDECREF(string1
);
1174 Py_XDECREF(string2
);
1177 PyGILState_Release(gilstate
);
1183 pysqlite_connection_interrupt(pysqlite_Connection
* self
, PyObject
* args
)
1185 PyObject
* retval
= NULL
;
1187 if (!pysqlite_check_connection(self
)) {
1191 sqlite3_interrupt(self
->db
);
1200 /* Function author: Paul Kippes <kippesp@gmail.com>
1201 * Class method of Connection to call the Python function _iterdump
1202 * of the sqlite3 module.
1205 pysqlite_connection_iterdump(pysqlite_Connection
* self
, PyObject
* args
)
1207 PyObject
* retval
= NULL
;
1208 PyObject
* module
= NULL
;
1209 PyObject
* module_dict
;
1210 PyObject
* pyfn_iterdump
;
1212 if (!pysqlite_check_connection(self
)) {
1216 module
= PyImport_ImportModule(MODULE_NAME
".dump");
1221 module_dict
= PyModule_GetDict(module
);
1226 pyfn_iterdump
= PyDict_GetItemString(module_dict
, "_iterdump");
1227 if (!pyfn_iterdump
) {
1228 PyErr_SetString(pysqlite_OperationalError
, "Failed to obtain _iterdump() reference");
1232 args
= PyTuple_New(1);
1237 PyTuple_SetItem(args
, 0, (PyObject
*)self
);
1238 retval
= PyObject_CallObject(pyfn_iterdump
, args
);
1247 pysqlite_connection_create_collation(pysqlite_Connection
* self
, PyObject
* args
)
1250 PyObject
* uppercase_name
= 0;
1256 if (!pysqlite_check_thread(self
) || !pysqlite_check_connection(self
)) {
1260 if (!PyArg_ParseTuple(args
, "O!O:create_collation(name, callback)", &PyString_Type
, &name
, &callable
)) {
1264 uppercase_name
= PyObject_CallMethod(name
, "upper", "");
1265 if (!uppercase_name
) {
1269 chk
= PyString_AsString(uppercase_name
);
1271 if ((*chk
>= '0' && *chk
<= '9')
1272 || (*chk
>= 'A' && *chk
<= 'Z')
1277 PyErr_SetString(pysqlite_ProgrammingError
, "invalid character in collation name");
1282 if (callable
!= Py_None
&& !PyCallable_Check(callable
)) {
1283 PyErr_SetString(PyExc_TypeError
, "parameter must be callable");
1287 if (callable
!= Py_None
) {
1288 PyDict_SetItem(self
->collations
, uppercase_name
, callable
);
1290 PyDict_DelItem(self
->collations
, uppercase_name
);
1293 rc
= sqlite3_create_collation(self
->db
,
1294 PyString_AsString(uppercase_name
),
1296 (callable
!= Py_None
) ? callable
: NULL
,
1297 (callable
!= Py_None
) ? pysqlite_collation_callback
: NULL
);
1298 if (rc
!= SQLITE_OK
) {
1299 PyDict_DelItem(self
->collations
, uppercase_name
);
1300 _pysqlite_seterror(self
->db
, NULL
);
1305 Py_XDECREF(uppercase_name
);
1307 if (PyErr_Occurred()) {
1317 /* Called when the connection is used as a context manager. Returns itself as a
1318 * convenience to the caller. */
1320 pysqlite_connection_enter(pysqlite_Connection
* self
, PyObject
* args
)
1323 return (PyObject
*)self
;
1326 /** Called when the connection is used as a context manager. If there was any
1327 * exception, a rollback takes place; otherwise we commit. */
1329 pysqlite_connection_exit(pysqlite_Connection
* self
, PyObject
* args
)
1331 PyObject
* exc_type
, *exc_value
, *exc_tb
;
1335 if (!PyArg_ParseTuple(args
, "OOO", &exc_type
, &exc_value
, &exc_tb
)) {
1339 if (exc_type
== Py_None
&& exc_value
== Py_None
&& exc_tb
== Py_None
) {
1340 method_name
= "commit";
1342 method_name
= "rollback";
1345 result
= PyObject_CallMethod((PyObject
*)self
, method_name
, "");
1351 Py_INCREF(Py_False
);
1355 static char connection_doc
[] =
1356 PyDoc_STR("SQLite database connection object.");
1358 static PyGetSetDef connection_getset
[] = {
1359 {"isolation_level", (getter
)pysqlite_connection_get_isolation_level
, (setter
)pysqlite_connection_set_isolation_level
},
1360 {"total_changes", (getter
)pysqlite_connection_get_total_changes
, (setter
)0},
1364 static PyMethodDef connection_methods
[] = {
1365 {"cursor", (PyCFunction
)pysqlite_connection_cursor
, METH_VARARGS
|METH_KEYWORDS
,
1366 PyDoc_STR("Return a cursor for the connection.")},
1367 {"close", (PyCFunction
)pysqlite_connection_close
, METH_NOARGS
,
1368 PyDoc_STR("Closes the connection.")},
1369 {"commit", (PyCFunction
)pysqlite_connection_commit
, METH_NOARGS
,
1370 PyDoc_STR("Commit the current transaction.")},
1371 {"rollback", (PyCFunction
)pysqlite_connection_rollback
, METH_NOARGS
,
1372 PyDoc_STR("Roll back the current transaction.")},
1373 {"create_function", (PyCFunction
)pysqlite_connection_create_function
, METH_VARARGS
|METH_KEYWORDS
,
1374 PyDoc_STR("Creates a new function. Non-standard.")},
1375 {"create_aggregate", (PyCFunction
)pysqlite_connection_create_aggregate
, METH_VARARGS
|METH_KEYWORDS
,
1376 PyDoc_STR("Creates a new aggregate. Non-standard.")},
1377 {"set_authorizer", (PyCFunction
)pysqlite_connection_set_authorizer
, METH_VARARGS
|METH_KEYWORDS
,
1378 PyDoc_STR("Sets authorizer callback. Non-standard.")},
1379 {"set_progress_handler", (PyCFunction
)pysqlite_connection_set_progress_handler
, METH_VARARGS
|METH_KEYWORDS
,
1380 PyDoc_STR("Sets progress handler callback. Non-standard.")},
1381 {"execute", (PyCFunction
)pysqlite_connection_execute
, METH_VARARGS
,
1382 PyDoc_STR("Executes a SQL statement. Non-standard.")},
1383 {"executemany", (PyCFunction
)pysqlite_connection_executemany
, METH_VARARGS
,
1384 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
1385 {"executescript", (PyCFunction
)pysqlite_connection_executescript
, METH_VARARGS
,
1386 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
1387 {"create_collation", (PyCFunction
)pysqlite_connection_create_collation
, METH_VARARGS
,
1388 PyDoc_STR("Creates a collation function. Non-standard.")},
1389 {"interrupt", (PyCFunction
)pysqlite_connection_interrupt
, METH_NOARGS
,
1390 PyDoc_STR("Abort any pending database operation. Non-standard.")},
1391 {"iterdump", (PyCFunction
)pysqlite_connection_iterdump
, METH_NOARGS
,
1392 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
1393 {"__enter__", (PyCFunction
)pysqlite_connection_enter
, METH_NOARGS
,
1394 PyDoc_STR("For context manager. Non-standard.")},
1395 {"__exit__", (PyCFunction
)pysqlite_connection_exit
, METH_VARARGS
,
1396 PyDoc_STR("For context manager. Non-standard.")},
1400 static struct PyMemberDef connection_members
[] =
1402 {"Warning", T_OBJECT
, offsetof(pysqlite_Connection
, Warning
), RO
},
1403 {"Error", T_OBJECT
, offsetof(pysqlite_Connection
, Error
), RO
},
1404 {"InterfaceError", T_OBJECT
, offsetof(pysqlite_Connection
, InterfaceError
), RO
},
1405 {"DatabaseError", T_OBJECT
, offsetof(pysqlite_Connection
, DatabaseError
), RO
},
1406 {"DataError", T_OBJECT
, offsetof(pysqlite_Connection
, DataError
), RO
},
1407 {"OperationalError", T_OBJECT
, offsetof(pysqlite_Connection
, OperationalError
), RO
},
1408 {"IntegrityError", T_OBJECT
, offsetof(pysqlite_Connection
, IntegrityError
), RO
},
1409 {"InternalError", T_OBJECT
, offsetof(pysqlite_Connection
, InternalError
), RO
},
1410 {"ProgrammingError", T_OBJECT
, offsetof(pysqlite_Connection
, ProgrammingError
), RO
},
1411 {"NotSupportedError", T_OBJECT
, offsetof(pysqlite_Connection
, NotSupportedError
), RO
},
1412 {"row_factory", T_OBJECT
, offsetof(pysqlite_Connection
, row_factory
)},
1413 {"text_factory", T_OBJECT
, offsetof(pysqlite_Connection
, text_factory
)},
1417 PyTypeObject pysqlite_ConnectionType
= {
1418 PyVarObject_HEAD_INIT(NULL
, 0)
1419 MODULE_NAME
".Connection", /* tp_name */
1420 sizeof(pysqlite_Connection
), /* tp_basicsize */
1421 0, /* tp_itemsize */
1422 (destructor
)pysqlite_connection_dealloc
, /* tp_dealloc */
1428 0, /* tp_as_number */
1429 0, /* tp_as_sequence */
1430 0, /* tp_as_mapping */
1432 (ternaryfunc
)pysqlite_connection_call
, /* tp_call */
1434 0, /* tp_getattro */
1435 0, /* tp_setattro */
1436 0, /* tp_as_buffer */
1437 Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
, /* tp_flags */
1438 connection_doc
, /* tp_doc */
1439 0, /* tp_traverse */
1441 0, /* tp_richcompare */
1442 0, /* tp_weaklistoffset */
1444 0, /* tp_iternext */
1445 connection_methods
, /* tp_methods */
1446 connection_members
, /* tp_members */
1447 connection_getset
, /* tp_getset */
1450 0, /* tp_descr_get */
1451 0, /* tp_descr_set */
1452 0, /* tp_dictoffset */
1453 (initproc
)pysqlite_connection_init
, /* tp_init */
1459 extern int pysqlite_connection_setup_types(void)
1461 pysqlite_ConnectionType
.tp_new
= PyType_GenericNew
;
1462 return PyType_Ready(&pysqlite_ConnectionType
);