blob: 309b16852cd082ee799cac9fa376c8d310abdc22 [file] [log] [blame]
Anthony Baxterc51ee692006-04-01 00:57:31 +00001/* connection.c - the connection type
2 *
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00003 * Copyright (C) 2004-2007 Gerhard Häring <gh@ghaering.de>
Anthony Baxterc51ee692006-04-01 00:57:31 +00004 *
5 * This file is part of pysqlite.
Anthony Baxter72289a62006-04-04 06:29:05 +00006 *
Anthony Baxterc51ee692006-04-01 00:57:31 +00007 * 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.
10 *
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:
14 *
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.
22 */
23
24#include "cache.h"
25#include "module.h"
26#include "connection.h"
27#include "statement.h"
28#include "cursor.h"
29#include "prepare_protocol.h"
30#include "util.h"
Anthony Baxter72289a62006-04-04 06:29:05 +000031#include "sqlitecompat.h"
32
Anthony Baxterc51ee692006-04-01 00:57:31 +000033#include "pythread.h"
34
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000035#define ACTION_FINALIZE 1
36#define ACTION_RESET 2
37
Gerhard Häring0741a602007-01-14 01:43:50 +000038static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
Anthony Baxterc51ee692006-04-01 00:57:31 +000039
Gerhard Häringb2e88162006-06-14 22:28:37 +000040
Gerhard Häring6e1afcf2008-09-12 18:58:57 +000041static void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
Gerhard Häringb2e88162006-06-14 22:28:37 +000042{
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);
Neal Norwitzfe7d0c32006-06-15 04:54:29 +000048#else
Gerhard Häring0741a602007-01-14 01:43:50 +000049 PyErr_SetString(pysqlite_OperationalError, errmsg);
Gerhard Häringb2e88162006-06-14 22:28:37 +000050#endif
51}
52
Gerhard Häring0741a602007-01-14 01:43:50 +000053int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +000054{
55 static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL};
56
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000057 PyObject* database;
Anthony Baxterc51ee692006-04-01 00:57:31 +000058 int detect_types = 0;
59 PyObject* isolation_level = NULL;
60 PyObject* factory = NULL;
61 int check_same_thread = 1;
62 int cached_statements = 100;
63 double timeout = 5.0;
64 int rc;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000065 PyObject* class_attr = NULL;
66 PyObject* class_attr_str = NULL;
67 int is_apsw_connection = 0;
68 PyObject* database_utf8;
Anthony Baxterc51ee692006-04-01 00:57:31 +000069
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000070 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOi", kwlist,
Anthony Baxterc51ee692006-04-01 00:57:31 +000071 &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements))
72 {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000073 return -1;
Anthony Baxterc51ee692006-04-01 00:57:31 +000074 }
75
76 self->begin_statement = NULL;
77
78 self->statement_cache = NULL;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +000079 self->statements = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +000080
81 Py_INCREF(Py_None);
82 self->row_factory = Py_None;
83
84 Py_INCREF(&PyUnicode_Type);
85 self->text_factory = (PyObject*)&PyUnicode_Type;
86
Gregory P. Smithdd96db62008-06-09 04:58:54 +000087 if (PyString_Check(database) || PyUnicode_Check(database)) {
88 if (PyString_Check(database)) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000089 database_utf8 = database;
90 Py_INCREF(database_utf8);
91 } else {
92 database_utf8 = PyUnicode_AsUTF8String(database);
93 if (!database_utf8) {
94 return -1;
95 }
96 }
Anthony Baxterc51ee692006-04-01 00:57:31 +000097
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000098 Py_BEGIN_ALLOW_THREADS
Gregory P. Smithdd96db62008-06-09 04:58:54 +000099 rc = sqlite3_open(PyString_AsString(database_utf8), &self->db);
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000100 Py_END_ALLOW_THREADS
101
102 Py_DECREF(database_utf8);
103
104 if (rc != SQLITE_OK) {
105 _pysqlite_seterror(self->db, NULL);
106 return -1;
107 }
108 } else {
109 /* Create a pysqlite connection from a APSW connection */
110 class_attr = PyObject_GetAttrString(database, "__class__");
111 if (class_attr) {
112 class_attr_str = PyObject_Str(class_attr);
113 if (class_attr_str) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000114 if (strcmp(PyString_AsString(class_attr_str), "<type 'apsw.Connection'>") == 0) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000115 /* 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;
120
121 Py_INCREF(database);
122 self->apsw_connection = database;
123 is_apsw_connection = 1;
124 }
125 }
126 }
127 Py_XDECREF(class_attr_str);
128 Py_XDECREF(class_attr);
129
130 if (!is_apsw_connection) {
131 PyErr_SetString(PyExc_ValueError, "database parameter must be string or APSW Connection object");
132 return -1;
133 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000134 }
135
136 if (!isolation_level) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000137 isolation_level = PyString_FromString("");
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000138 if (!isolation_level) {
139 return -1;
140 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000141 } else {
142 Py_INCREF(isolation_level);
143 }
144 self->isolation_level = NULL;
Gerhard Häring0741a602007-01-14 01:43:50 +0000145 pysqlite_connection_set_isolation_level(self, isolation_level);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000146 Py_DECREF(isolation_level);
147
Gerhard Häring0741a602007-01-14 01:43:50 +0000148 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000149 if (PyErr_Occurred()) {
150 return -1;
151 }
152
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000153 self->statements = PyList_New(0);
154 if (!self->statements) {
155 return -1;
156 }
157 self->created_statements = 0;
158
Anthony Baxterc51ee692006-04-01 00:57:31 +0000159 /* 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.
163 */
164 self->statement_cache->decref_factory = 0;
165 Py_DECREF(self);
166
167 self->inTransaction = 0;
168 self->detect_types = detect_types;
169 self->timeout = timeout;
170 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Georg Brandle9b91212009-04-05 21:26:31 +0000171#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000172 self->thread_ident = PyThread_get_thread_ident();
Georg Brandle9b91212009-04-05 21:26:31 +0000173#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000174 self->check_same_thread = check_same_thread;
175
176 self->function_pinboard = PyDict_New();
Anthony Baxter72289a62006-04-04 06:29:05 +0000177 if (!self->function_pinboard) {
178 return -1;
179 }
180
181 self->collations = PyDict_New();
182 if (!self->collations) {
183 return -1;
184 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000185
Gerhard Häring0741a602007-01-14 01:43:50 +0000186 self->Warning = pysqlite_Warning;
187 self->Error = pysqlite_Error;
188 self->InterfaceError = pysqlite_InterfaceError;
189 self->DatabaseError = pysqlite_DatabaseError;
190 self->DataError = pysqlite_DataError;
191 self->OperationalError = pysqlite_OperationalError;
192 self->IntegrityError = pysqlite_IntegrityError;
193 self->InternalError = pysqlite_InternalError;
194 self->ProgrammingError = pysqlite_ProgrammingError;
195 self->NotSupportedError = pysqlite_NotSupportedError;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000196
197 return 0;
198}
199
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000200/* Empty the entire statement cache of this connection */
Gerhard Häring0741a602007-01-14 01:43:50 +0000201void pysqlite_flush_statement_cache(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000202{
Gerhard Häring0741a602007-01-14 01:43:50 +0000203 pysqlite_Node* node;
204 pysqlite_Statement* statement;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000205
206 node = self->statement_cache->first;
207
208 while (node) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000209 statement = (pysqlite_Statement*)(node->data);
210 (void)pysqlite_statement_finalize(statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000211 node = node->next;
212 }
213
214 Py_DECREF(self->statement_cache);
Gerhard Häring0741a602007-01-14 01:43:50 +0000215 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000216 Py_DECREF(self);
217 self->statement_cache->decref_factory = 0;
218}
219
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000220/* action in (ACTION_RESET, ACTION_FINALIZE) */
221void pysqlite_do_all_statements(pysqlite_Connection* self, int action)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000222{
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000223 int i;
224 PyObject* weakref;
225 PyObject* statement;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000226
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000227 for (i = 0; i < PyList_Size(self->statements); i++) {
228 weakref = PyList_GetItem(self->statements, i);
229 statement = PyWeakref_GetObject(weakref);
230 if (statement != Py_None) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000231 if (action == ACTION_RESET) {
232 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
233 } else {
234 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
235 }
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000236 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000237 }
238}
239
Gerhard Häring0741a602007-01-14 01:43:50 +0000240void pysqlite_connection_dealloc(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000241{
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000242 PyObject* ret = NULL;
243
Anthony Baxterc51ee692006-04-01 00:57:31 +0000244 Py_XDECREF(self->statement_cache);
245
246 /* Clean up if user has not called .close() explicitly. */
247 if (self->db) {
248 Py_BEGIN_ALLOW_THREADS
249 sqlite3_close(self->db);
250 Py_END_ALLOW_THREADS
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000251 } else if (self->apsw_connection) {
252 ret = PyObject_CallMethod(self->apsw_connection, "close", "");
253 Py_XDECREF(ret);
254 Py_XDECREF(self->apsw_connection);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000255 }
256
257 if (self->begin_statement) {
258 PyMem_Free(self->begin_statement);
259 }
260 Py_XDECREF(self->isolation_level);
261 Py_XDECREF(self->function_pinboard);
262 Py_XDECREF(self->row_factory);
263 Py_XDECREF(self->text_factory);
Anthony Baxter72289a62006-04-04 06:29:05 +0000264 Py_XDECREF(self->collations);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000265 Py_XDECREF(self->statements);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000266
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000267 self->ob_type->tp_free((PyObject*)self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000268}
269
Gerhard Häring0741a602007-01-14 01:43:50 +0000270PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000271{
272 static char *kwlist[] = {"factory", NULL, NULL};
273 PyObject* factory = NULL;
274 PyObject* cursor;
275
276
277 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
278 &factory)) {
279 return NULL;
280 }
281
Gerhard Häring0741a602007-01-14 01:43:50 +0000282 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000283 return NULL;
284 }
285
286 if (factory == NULL) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000287 factory = (PyObject*)&pysqlite_CursorType;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000288 }
289
290 cursor = PyObject_CallFunction(factory, "O", self);
291
292 if (cursor && self->row_factory != Py_None) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000293 Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000294 Py_INCREF(self->row_factory);
Gerhard Häring0741a602007-01-14 01:43:50 +0000295 ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000296 }
297
298 return cursor;
299}
300
Gerhard Häring0741a602007-01-14 01:43:50 +0000301PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000302{
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000303 PyObject* ret;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000304 int rc;
305
Gerhard Häring0741a602007-01-14 01:43:50 +0000306 if (!pysqlite_check_thread(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000307 return NULL;
308 }
309
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000310 pysqlite_do_all_statements(self, ACTION_FINALIZE);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000311
312 if (self->db) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000313 if (self->apsw_connection) {
314 ret = PyObject_CallMethod(self->apsw_connection, "close", "");
315 Py_XDECREF(ret);
316 Py_XDECREF(self->apsw_connection);
317 self->apsw_connection = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000318 self->db = NULL;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000319 } else {
320 Py_BEGIN_ALLOW_THREADS
321 rc = sqlite3_close(self->db);
322 Py_END_ALLOW_THREADS
323
324 if (rc != SQLITE_OK) {
325 _pysqlite_seterror(self->db, NULL);
326 return NULL;
327 } else {
328 self->db = NULL;
329 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000330 }
331 }
332
333 Py_INCREF(Py_None);
334 return Py_None;
335}
336
337/*
338 * Checks if a connection object is usable (i. e. not closed).
339 *
340 * 0 => error; 1 => ok
341 */
Gerhard Häring0741a602007-01-14 01:43:50 +0000342int pysqlite_check_connection(pysqlite_Connection* con)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000343{
344 if (!con->db) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000345 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Anthony Baxterc51ee692006-04-01 00:57:31 +0000346 return 0;
347 } else {
348 return 1;
349 }
350}
351
Gerhard Häring0741a602007-01-14 01:43:50 +0000352PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000353{
354 int rc;
355 const char* tail;
356 sqlite3_stmt* statement;
357
358 Py_BEGIN_ALLOW_THREADS
359 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
360 Py_END_ALLOW_THREADS
361
362 if (rc != SQLITE_OK) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000363 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000364 goto error;
365 }
366
Gerhard Häring6e1afcf2008-09-12 18:58:57 +0000367 rc = pysqlite_step(statement, self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000368 if (rc == SQLITE_DONE) {
369 self->inTransaction = 1;
370 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000371 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000372 }
373
374 Py_BEGIN_ALLOW_THREADS
375 rc = sqlite3_finalize(statement);
376 Py_END_ALLOW_THREADS
377
378 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000379 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000380 }
381
382error:
383 if (PyErr_Occurred()) {
384 return NULL;
385 } else {
386 Py_INCREF(Py_None);
387 return Py_None;
388 }
389}
390
Gerhard Häring0741a602007-01-14 01:43:50 +0000391PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000392{
393 int rc;
394 const char* tail;
395 sqlite3_stmt* statement;
396
Gerhard Häring0741a602007-01-14 01:43:50 +0000397 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000398 return NULL;
399 }
400
401 if (self->inTransaction) {
402 Py_BEGIN_ALLOW_THREADS
403 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
404 Py_END_ALLOW_THREADS
405 if (rc != SQLITE_OK) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000406 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000407 goto error;
408 }
409
Gerhard Häring6e1afcf2008-09-12 18:58:57 +0000410 rc = pysqlite_step(statement, self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000411 if (rc == SQLITE_DONE) {
412 self->inTransaction = 0;
413 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000414 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000415 }
416
417 Py_BEGIN_ALLOW_THREADS
418 rc = sqlite3_finalize(statement);
419 Py_END_ALLOW_THREADS
420 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000421 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000422 }
423
424 }
425
426error:
427 if (PyErr_Occurred()) {
428 return NULL;
429 } else {
430 Py_INCREF(Py_None);
431 return Py_None;
432 }
433}
434
Gerhard Häring0741a602007-01-14 01:43:50 +0000435PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000436{
437 int rc;
438 const char* tail;
439 sqlite3_stmt* statement;
440
Gerhard Häring0741a602007-01-14 01:43:50 +0000441 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000442 return NULL;
443 }
444
445 if (self->inTransaction) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000446 pysqlite_do_all_statements(self, ACTION_RESET);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000447
448 Py_BEGIN_ALLOW_THREADS
449 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
450 Py_END_ALLOW_THREADS
451 if (rc != SQLITE_OK) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000452 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000453 goto error;
454 }
455
Gerhard Häring6e1afcf2008-09-12 18:58:57 +0000456 rc = pysqlite_step(statement, self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000457 if (rc == SQLITE_DONE) {
458 self->inTransaction = 0;
459 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000460 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000461 }
462
463 Py_BEGIN_ALLOW_THREADS
464 rc = sqlite3_finalize(statement);
465 Py_END_ALLOW_THREADS
466 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000467 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000468 }
469
470 }
471
472error:
473 if (PyErr_Occurred()) {
474 return NULL;
475 } else {
476 Py_INCREF(Py_None);
477 return Py_None;
478 }
479}
480
Gerhard Häring0741a602007-01-14 01:43:50 +0000481void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000482{
483 long longval;
484 const char* buffer;
Neal Norwitz95f0e4c2006-04-01 09:08:06 +0000485 Py_ssize_t buflen;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000486 PyObject* stringval;
487
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000488 if ((!py_val) || PyErr_Occurred()) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000489 sqlite3_result_null(context);
490 } else if (py_val == Py_None) {
491 sqlite3_result_null(context);
492 } else if (PyInt_Check(py_val)) {
493 longval = PyInt_AsLong(py_val);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000494 sqlite3_result_int64(context, (PY_LONG_LONG)longval);
495 } else if (PyFloat_Check(py_val)) {
496 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
497 } else if (PyBuffer_Check(py_val)) {
498 if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
499 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000500 } else {
501 sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000502 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000503 } else if (PyString_Check(py_val)) {
504 sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000505 } else if (PyUnicode_Check(py_val)) {
506 stringval = PyUnicode_AsUTF8String(py_val);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000507 if (stringval) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000508 sqlite3_result_text(context, PyString_AsString(stringval), -1, SQLITE_TRANSIENT);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000509 Py_DECREF(stringval);
510 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000511 } else {
512 /* TODO: raise error */
513 }
514}
515
Gerhard Häring0741a602007-01-14 01:43:50 +0000516PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000517{
518 PyObject* args;
519 int i;
520 sqlite3_value* cur_value;
521 PyObject* cur_py_value;
522 const char* val_str;
523 PY_LONG_LONG val_int;
Neal Norwitz95f0e4c2006-04-01 09:08:06 +0000524 Py_ssize_t buflen;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000525 void* raw_buffer;
526
527 args = PyTuple_New(argc);
Anthony Baxter72289a62006-04-04 06:29:05 +0000528 if (!args) {
529 return NULL;
530 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000531
532 for (i = 0; i < argc; i++) {
533 cur_value = argv[i];
534 switch (sqlite3_value_type(argv[i])) {
535 case SQLITE_INTEGER:
536 val_int = sqlite3_value_int64(cur_value);
537 cur_py_value = PyInt_FromLong((long)val_int);
538 break;
539 case SQLITE_FLOAT:
540 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
541 break;
542 case SQLITE_TEXT:
543 val_str = (const char*)sqlite3_value_text(cur_value);
544 cur_py_value = PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL);
545 /* TODO: have a way to show errors here */
546 if (!cur_py_value) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000547 PyErr_Clear();
Anthony Baxterc51ee692006-04-01 00:57:31 +0000548 Py_INCREF(Py_None);
549 cur_py_value = Py_None;
550 }
551 break;
552 case SQLITE_BLOB:
553 buflen = sqlite3_value_bytes(cur_value);
554 cur_py_value = PyBuffer_New(buflen);
555 if (!cur_py_value) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000556 break;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000557 }
558 if (PyObject_AsWriteBuffer(cur_py_value, &raw_buffer, &buflen)) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000559 Py_DECREF(cur_py_value);
560 cur_py_value = NULL;
561 break;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000562 }
563 memcpy(raw_buffer, sqlite3_value_blob(cur_value), buflen);
564 break;
565 case SQLITE_NULL:
566 default:
567 Py_INCREF(Py_None);
568 cur_py_value = Py_None;
569 }
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000570
571 if (!cur_py_value) {
572 Py_DECREF(args);
573 return NULL;
574 }
575
Anthony Baxterc51ee692006-04-01 00:57:31 +0000576 PyTuple_SetItem(args, i, cur_py_value);
577
578 }
579
580 return args;
581}
582
Gerhard Häring0741a602007-01-14 01:43:50 +0000583void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000584{
585 PyObject* args;
586 PyObject* py_func;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000587 PyObject* py_retval = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000588
Georg Brandle9b91212009-04-05 21:26:31 +0000589#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000590 PyGILState_STATE threadstate;
591
592 threadstate = PyGILState_Ensure();
Georg Brandle9b91212009-04-05 21:26:31 +0000593#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000594
595 py_func = (PyObject*)sqlite3_user_data(context);
596
Gerhard Häring0741a602007-01-14 01:43:50 +0000597 args = _pysqlite_build_py_params(context, argc, argv);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000598 if (args) {
599 py_retval = PyObject_CallObject(py_func, args);
600 Py_DECREF(args);
601 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000602
Gerhard Häring1541ef02006-06-13 22:24:47 +0000603 if (py_retval) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000604 _pysqlite_set_result(context, py_retval);
Gerhard Häring1541ef02006-06-13 22:24:47 +0000605 Py_DECREF(py_retval);
606 } else {
607 if (_enable_callback_tracebacks) {
608 PyErr_Print();
609 } else {
610 PyErr_Clear();
611 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000612 _sqlite3_result_error(context, "user-defined function raised exception", -1);
Gerhard Häring1541ef02006-06-13 22:24:47 +0000613 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000614
Georg Brandle9b91212009-04-05 21:26:31 +0000615#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000616 PyGILState_Release(threadstate);
Georg Brandle9b91212009-04-05 21:26:31 +0000617#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000618}
619
Gerhard Häring0741a602007-01-14 01:43:50 +0000620static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000621{
622 PyObject* args;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000623 PyObject* function_result = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000624 PyObject* aggregate_class;
625 PyObject** aggregate_instance;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000626 PyObject* stepmethod = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000627
Georg Brandle9b91212009-04-05 21:26:31 +0000628#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000629 PyGILState_STATE threadstate;
630
631 threadstate = PyGILState_Ensure();
Georg Brandle9b91212009-04-05 21:26:31 +0000632#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000633
634 aggregate_class = (PyObject*)sqlite3_user_data(context);
635
636 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
637
638 if (*aggregate_instance == 0) {
639 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
640
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000641 if (PyErr_Occurred()) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000642 *aggregate_instance = 0;
Gerhard Häring1541ef02006-06-13 22:24:47 +0000643 if (_enable_callback_tracebacks) {
644 PyErr_Print();
645 } else {
646 PyErr_Clear();
647 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000648 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000649 goto error;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000650 }
651 }
652
653 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000654 if (!stepmethod) {
655 goto error;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000656 }
657
Gerhard Häring0741a602007-01-14 01:43:50 +0000658 args = _pysqlite_build_py_params(context, argc, params);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000659 if (!args) {
660 goto error;
661 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000662
663 function_result = PyObject_CallObject(stepmethod, args);
664 Py_DECREF(args);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000665
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000666 if (!function_result) {
Gerhard Häring1541ef02006-06-13 22:24:47 +0000667 if (_enable_callback_tracebacks) {
668 PyErr_Print();
669 } else {
670 PyErr_Clear();
671 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000672 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000673 }
674
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000675error:
676 Py_XDECREF(stepmethod);
677 Py_XDECREF(function_result);
678
Georg Brandle9b91212009-04-05 21:26:31 +0000679#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000680 PyGILState_Release(threadstate);
Georg Brandle9b91212009-04-05 21:26:31 +0000681#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000682}
683
Gerhard Häring0741a602007-01-14 01:43:50 +0000684void _pysqlite_final_callback(sqlite3_context* context)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000685{
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000686 PyObject* function_result = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000687 PyObject** aggregate_instance;
688 PyObject* aggregate_class;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000689
Georg Brandle9b91212009-04-05 21:26:31 +0000690#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000691 PyGILState_STATE threadstate;
692
693 threadstate = PyGILState_Ensure();
Georg Brandle9b91212009-04-05 21:26:31 +0000694#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000695
696 aggregate_class = (PyObject*)sqlite3_user_data(context);
697
698 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
699 if (!*aggregate_instance) {
700 /* this branch is executed if there was an exception in the aggregate's
701 * __init__ */
702
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000703 goto error;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000704 }
705
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000706 function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
707 if (!function_result) {
Gerhard Häring1541ef02006-06-13 22:24:47 +0000708 if (_enable_callback_tracebacks) {
709 PyErr_Print();
710 } else {
711 PyErr_Clear();
712 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000713 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Gerhard Häring1541ef02006-06-13 22:24:47 +0000714 } else {
Gerhard Häring0741a602007-01-14 01:43:50 +0000715 _pysqlite_set_result(context, function_result);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000716 }
717
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000718error:
Anthony Baxterc51ee692006-04-01 00:57:31 +0000719 Py_XDECREF(*aggregate_instance);
720 Py_XDECREF(function_result);
721
Georg Brandle9b91212009-04-05 21:26:31 +0000722#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000723 PyGILState_Release(threadstate);
Georg Brandle9b91212009-04-05 21:26:31 +0000724#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000725}
726
Gerhard Häring0741a602007-01-14 01:43:50 +0000727void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000728{
729 PyObject* new_list;
730 PyObject* weakref;
731 int i;
732
733 /* we only need to do this once in a while */
734 if (self->created_statements++ < 200) {
735 return;
736 }
737
738 self->created_statements = 0;
739
740 new_list = PyList_New(0);
741 if (!new_list) {
742 return;
743 }
744
745 for (i = 0; i < PyList_Size(self->statements); i++) {
746 weakref = PyList_GetItem(self->statements, i);
Gerhard Häringecd20102006-06-19 21:17:35 +0000747 if (PyWeakref_GetObject(weakref) != Py_None) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000748 if (PyList_Append(new_list, weakref) != 0) {
749 Py_DECREF(new_list);
750 return;
751 }
752 }
753 }
754
755 Py_DECREF(self->statements);
756 self->statements = new_list;
757}
Anthony Baxterc51ee692006-04-01 00:57:31 +0000758
Gerhard Häring0741a602007-01-14 01:43:50 +0000759PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000760{
761 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
762
763 PyObject* func;
764 char* name;
765 int narg;
766 int rc;
767
Gerhard Häring924b5712010-03-05 15:50:25 +0000768 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
769 return NULL;
770 }
771
Anthony Baxterc51ee692006-04-01 00:57:31 +0000772 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
773 &name, &narg, &func))
774 {
775 return NULL;
776 }
777
Gerhard Häring0741a602007-01-14 01:43:50 +0000778 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000779
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000780 if (rc != SQLITE_OK) {
781 /* Workaround for SQLite bug: no error code or string is available here */
Gerhard Häring0741a602007-01-14 01:43:50 +0000782 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000783 return NULL;
784 } else {
785 PyDict_SetItem(self->function_pinboard, func, Py_None);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000786
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000787 Py_INCREF(Py_None);
788 return Py_None;
789 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000790}
791
Gerhard Häring0741a602007-01-14 01:43:50 +0000792PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000793{
794 PyObject* aggregate_class;
795
796 int n_arg;
797 char* name;
798 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
799 int rc;
800
Gerhard Häring924b5712010-03-05 15:50:25 +0000801 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
802 return NULL;
803 }
804
Anthony Baxterc51ee692006-04-01 00:57:31 +0000805 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
806 kwlist, &name, &n_arg, &aggregate_class)) {
807 return NULL;
808 }
809
Gerhard Häring0741a602007-01-14 01:43:50 +0000810 rc = sqlite3_create_function(self->db, name, n_arg, SQLITE_UTF8, (void*)aggregate_class, 0, &_pysqlite_step_callback, &_pysqlite_final_callback);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000811 if (rc != SQLITE_OK) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000812 /* Workaround for SQLite bug: no error code or string is available here */
Gerhard Häring0741a602007-01-14 01:43:50 +0000813 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Anthony Baxterc51ee692006-04-01 00:57:31 +0000814 return NULL;
815 } else {
816 PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None);
817
818 Py_INCREF(Py_None);
819 return Py_None;
820 }
821}
822
Gerhard Häring0741a602007-01-14 01:43:50 +0000823static int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source)
Gerhard Häring1541ef02006-06-13 22:24:47 +0000824{
825 PyObject *ret;
826 int rc;
Georg Brandle9b91212009-04-05 21:26:31 +0000827#ifdef WITH_THREAD
Gerhard Häring1541ef02006-06-13 22:24:47 +0000828 PyGILState_STATE gilstate;
829
830 gilstate = PyGILState_Ensure();
Georg Brandle9b91212009-04-05 21:26:31 +0000831#endif
Gerhard Häring1541ef02006-06-13 22:24:47 +0000832 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
833
834 if (!ret) {
835 if (_enable_callback_tracebacks) {
836 PyErr_Print();
837 } else {
838 PyErr_Clear();
839 }
840
841 rc = SQLITE_DENY;
842 } else {
843 if (PyInt_Check(ret)) {
844 rc = (int)PyInt_AsLong(ret);
845 } else {
846 rc = SQLITE_DENY;
847 }
848 Py_DECREF(ret);
849 }
850
Georg Brandle9b91212009-04-05 21:26:31 +0000851#ifdef WITH_THREAD
Gerhard Häring1541ef02006-06-13 22:24:47 +0000852 PyGILState_Release(gilstate);
Georg Brandle9b91212009-04-05 21:26:31 +0000853#endif
Gerhard Häring1541ef02006-06-13 22:24:47 +0000854 return rc;
855}
856
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000857static int _progress_handler(void* user_arg)
858{
859 int rc;
860 PyObject *ret;
Georg Brandle9b91212009-04-05 21:26:31 +0000861#ifdef WITH_THREAD
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000862 PyGILState_STATE gilstate;
863
864 gilstate = PyGILState_Ensure();
Georg Brandle9b91212009-04-05 21:26:31 +0000865#endif
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000866 ret = PyObject_CallFunction((PyObject*)user_arg, "");
867
868 if (!ret) {
869 if (_enable_callback_tracebacks) {
870 PyErr_Print();
871 } else {
872 PyErr_Clear();
873 }
874
Jesus Cea585ad8a2009-07-02 15:37:21 +0000875 /* abort query if error occurred */
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000876 rc = 1;
877 } else {
878 rc = (int)PyObject_IsTrue(ret);
Neal Norwitzca752f32008-03-03 04:37:45 +0000879 Py_DECREF(ret);
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000880 }
881
Georg Brandle9b91212009-04-05 21:26:31 +0000882#ifdef WITH_THREAD
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000883 PyGILState_Release(gilstate);
Georg Brandle9b91212009-04-05 21:26:31 +0000884#endif
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000885 return rc;
886}
887
Gerhard Häring0741a602007-01-14 01:43:50 +0000888PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häring1541ef02006-06-13 22:24:47 +0000889{
890 PyObject* authorizer_cb;
891
892 static char *kwlist[] = { "authorizer_callback", NULL };
893 int rc;
894
Gerhard Häring924b5712010-03-05 15:50:25 +0000895 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
896 return NULL;
897 }
898
Gerhard Häring1541ef02006-06-13 22:24:47 +0000899 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
900 kwlist, &authorizer_cb)) {
901 return NULL;
902 }
903
904 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
905
906 if (rc != SQLITE_OK) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000907 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Gerhard Häring1541ef02006-06-13 22:24:47 +0000908 return NULL;
909 } else {
910 PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None);
911
912 Py_INCREF(Py_None);
913 return Py_None;
914 }
915}
916
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000917PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
918{
919 PyObject* progress_handler;
920 int n;
921
922 static char *kwlist[] = { "progress_handler", "n", NULL };
923
Gerhard Häring924b5712010-03-05 15:50:25 +0000924 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
925 return NULL;
926 }
927
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000928 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
929 kwlist, &progress_handler, &n)) {
930 return NULL;
931 }
932
933 if (progress_handler == Py_None) {
934 /* None clears the progress handler previously set */
935 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
936 } else {
937 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
938 PyDict_SetItem(self->function_pinboard, progress_handler, Py_None);
939 }
940
941 Py_INCREF(Py_None);
942 return Py_None;
943}
944
Gerhard Häring0741a602007-01-14 01:43:50 +0000945int pysqlite_check_thread(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000946{
Georg Brandle9b91212009-04-05 21:26:31 +0000947#ifdef WITH_THREAD
Anthony Baxterc51ee692006-04-01 00:57:31 +0000948 if (self->check_same_thread) {
949 if (PyThread_get_thread_ident() != self->thread_ident) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000950 PyErr_Format(pysqlite_ProgrammingError,
Anthony Baxterc51ee692006-04-01 00:57:31 +0000951 "SQLite objects created in a thread can only be used in that same thread."
952 "The object was created in thread id %ld and this is thread id %ld",
953 self->thread_ident, PyThread_get_thread_ident());
954 return 0;
955 }
956
957 }
Georg Brandle9b91212009-04-05 21:26:31 +0000958#endif
Anthony Baxterc51ee692006-04-01 00:57:31 +0000959 return 1;
960}
961
Gerhard Häring0741a602007-01-14 01:43:50 +0000962static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000963{
964 Py_INCREF(self->isolation_level);
965 return self->isolation_level;
966}
967
Gerhard Häring0741a602007-01-14 01:43:50 +0000968static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Anthony Baxter72289a62006-04-04 06:29:05 +0000969{
Gerhard Häring0741a602007-01-14 01:43:50 +0000970 if (!pysqlite_check_connection(self)) {
Anthony Baxter72289a62006-04-04 06:29:05 +0000971 return NULL;
972 } else {
973 return Py_BuildValue("i", sqlite3_total_changes(self->db));
974 }
975}
976
Gerhard Häring0741a602007-01-14 01:43:50 +0000977static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000978{
Anthony Baxterc51ee692006-04-01 00:57:31 +0000979 PyObject* res;
980 PyObject* begin_statement;
Georg Brandla24869a2008-07-16 22:33:18 +0000981 char* begin_statement_str;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000982
983 Py_XDECREF(self->isolation_level);
984
Neal Norwitz5b030652006-04-16 03:28:17 +0000985 if (self->begin_statement) {
986 PyMem_Free(self->begin_statement);
987 self->begin_statement = NULL;
988 }
989
Anthony Baxterc51ee692006-04-01 00:57:31 +0000990 if (isolation_level == Py_None) {
991 Py_INCREF(Py_None);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000992 self->isolation_level = Py_None;
993
Gerhard Häring0741a602007-01-14 01:43:50 +0000994 res = pysqlite_connection_commit(self, NULL);
Anthony Baxter72289a62006-04-04 06:29:05 +0000995 if (!res) {
996 return -1;
997 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000998 Py_DECREF(res);
999
1000 self->inTransaction = 0;
1001 } else {
1002 Py_INCREF(isolation_level);
1003 self->isolation_level = isolation_level;
1004
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001005 begin_statement = PyString_FromString("BEGIN ");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001006 if (!begin_statement) {
1007 return -1;
1008 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001009 PyString_Concat(&begin_statement, isolation_level);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001010 if (!begin_statement) {
1011 return -1;
1012 }
1013
Georg Brandla24869a2008-07-16 22:33:18 +00001014 begin_statement_str = PyString_AsString(begin_statement);
1015 if (!begin_statement_str) {
1016 Py_DECREF(begin_statement);
1017 return -1;
1018 }
1019 self->begin_statement = PyMem_Malloc(strlen(begin_statement_str) + 2);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001020 if (!self->begin_statement) {
Georg Brandla24869a2008-07-16 22:33:18 +00001021 Py_DECREF(begin_statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001022 return -1;
1023 }
1024
Georg Brandla24869a2008-07-16 22:33:18 +00001025 strcpy(self->begin_statement, begin_statement_str);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001026 Py_DECREF(begin_statement);
1027 }
1028
1029 return 0;
1030}
1031
Gerhard Häring0741a602007-01-14 01:43:50 +00001032PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001033{
1034 PyObject* sql;
Gerhard Häring0741a602007-01-14 01:43:50 +00001035 pysqlite_Statement* statement;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001036 PyObject* weakref;
Anthony Baxterc51ee692006-04-01 00:57:31 +00001037 int rc;
1038
Gerhard Häring924b5712010-03-05 15:50:25 +00001039 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1040 return NULL;
1041 }
1042
Anthony Baxterc51ee692006-04-01 00:57:31 +00001043 if (!PyArg_ParseTuple(args, "O", &sql)) {
1044 return NULL;
1045 }
1046
Gerhard Häring0741a602007-01-14 01:43:50 +00001047 _pysqlite_drop_unused_statement_references(self);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001048
Gerhard Häring0741a602007-01-14 01:43:50 +00001049 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001050 if (!statement) {
1051 return NULL;
1052 }
1053
Gerhard Häring0741a602007-01-14 01:43:50 +00001054 rc = pysqlite_statement_create(statement, self, sql);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001055
1056 if (rc != SQLITE_OK) {
1057 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Gerhard Häring0741a602007-01-14 01:43:50 +00001058 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001059 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Gerhard Häring0741a602007-01-14 01:43:50 +00001060 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001061 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001062 (void)pysqlite_statement_reset(statement);
1063 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001064 }
1065
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001066 Py_CLEAR(statement);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001067 } else {
1068 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1069 if (!weakref) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001070 Py_CLEAR(statement);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001071 goto error;
1072 }
1073
1074 if (PyList_Append(self->statements, weakref) != 0) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001075 Py_CLEAR(weakref);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001076 goto error;
1077 }
1078
1079 Py_DECREF(weakref);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001080 }
1081
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001082error:
Anthony Baxterc51ee692006-04-01 00:57:31 +00001083 return (PyObject*)statement;
1084}
1085
Gerhard Häring0741a602007-01-14 01:43:50 +00001086PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001087{
1088 PyObject* cursor = 0;
1089 PyObject* result = 0;
1090 PyObject* method = 0;
1091
1092 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1093 if (!cursor) {
1094 goto error;
1095 }
1096
1097 method = PyObject_GetAttrString(cursor, "execute");
1098 if (!method) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001099 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001100 goto error;
1101 }
1102
1103 result = PyObject_CallObject(method, args);
1104 if (!result) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001105 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001106 }
1107
1108error:
1109 Py_XDECREF(result);
1110 Py_XDECREF(method);
1111
1112 return cursor;
1113}
1114
Gerhard Häring0741a602007-01-14 01:43:50 +00001115PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001116{
1117 PyObject* cursor = 0;
1118 PyObject* result = 0;
1119 PyObject* method = 0;
1120
1121 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1122 if (!cursor) {
1123 goto error;
1124 }
1125
1126 method = PyObject_GetAttrString(cursor, "executemany");
1127 if (!method) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001128 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001129 goto error;
1130 }
1131
1132 result = PyObject_CallObject(method, args);
1133 if (!result) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001134 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001135 }
1136
1137error:
1138 Py_XDECREF(result);
1139 Py_XDECREF(method);
1140
1141 return cursor;
1142}
1143
Gerhard Häring0741a602007-01-14 01:43:50 +00001144PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001145{
1146 PyObject* cursor = 0;
1147 PyObject* result = 0;
1148 PyObject* method = 0;
1149
1150 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1151 if (!cursor) {
1152 goto error;
1153 }
1154
1155 method = PyObject_GetAttrString(cursor, "executescript");
1156 if (!method) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001157 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001158 goto error;
1159 }
1160
1161 result = PyObject_CallObject(method, args);
1162 if (!result) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001163 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001164 }
1165
1166error:
1167 Py_XDECREF(result);
1168 Py_XDECREF(method);
1169
1170 return cursor;
1171}
1172
Anthony Baxter72289a62006-04-04 06:29:05 +00001173/* ------------------------- COLLATION CODE ------------------------ */
1174
1175static int
Gerhard Häring0741a602007-01-14 01:43:50 +00001176pysqlite_collation_callback(
Anthony Baxter72289a62006-04-04 06:29:05 +00001177 void* context,
1178 int text1_length, const void* text1_data,
1179 int text2_length, const void* text2_data)
1180{
1181 PyObject* callback = (PyObject*)context;
1182 PyObject* string1 = 0;
1183 PyObject* string2 = 0;
Georg Brandle9b91212009-04-05 21:26:31 +00001184#ifdef WITH_THREAD
Anthony Baxter72289a62006-04-04 06:29:05 +00001185 PyGILState_STATE gilstate;
Georg Brandle9b91212009-04-05 21:26:31 +00001186#endif
Anthony Baxter72289a62006-04-04 06:29:05 +00001187 PyObject* retval = NULL;
1188 int result = 0;
Georg Brandle9b91212009-04-05 21:26:31 +00001189#ifdef WITH_THREAD
Anthony Baxter72289a62006-04-04 06:29:05 +00001190 gilstate = PyGILState_Ensure();
Georg Brandle9b91212009-04-05 21:26:31 +00001191#endif
Anthony Baxter72289a62006-04-04 06:29:05 +00001192
1193 if (PyErr_Occurred()) {
1194 goto finally;
1195 }
1196
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001197 string1 = PyString_FromStringAndSize((const char*)text1_data, text1_length);
1198 string2 = PyString_FromStringAndSize((const char*)text2_data, text2_length);
Anthony Baxter72289a62006-04-04 06:29:05 +00001199
1200 if (!string1 || !string2) {
1201 goto finally; /* failed to allocate strings */
1202 }
1203
1204 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1205
1206 if (!retval) {
1207 /* execution failed */
1208 goto finally;
1209 }
1210
1211 result = PyInt_AsLong(retval);
1212 if (PyErr_Occurred()) {
1213 result = 0;
1214 }
1215
1216finally:
1217 Py_XDECREF(string1);
1218 Py_XDECREF(string2);
1219 Py_XDECREF(retval);
Georg Brandle9b91212009-04-05 21:26:31 +00001220#ifdef WITH_THREAD
Anthony Baxter72289a62006-04-04 06:29:05 +00001221 PyGILState_Release(gilstate);
Georg Brandle9b91212009-04-05 21:26:31 +00001222#endif
Anthony Baxter72289a62006-04-04 06:29:05 +00001223 return result;
1224}
1225
1226static PyObject *
Gerhard Häring0741a602007-01-14 01:43:50 +00001227pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Gerhard Häring1541ef02006-06-13 22:24:47 +00001228{
1229 PyObject* retval = NULL;
1230
Gerhard Häring0741a602007-01-14 01:43:50 +00001231 if (!pysqlite_check_connection(self)) {
Gerhard Häring1541ef02006-06-13 22:24:47 +00001232 goto finally;
1233 }
1234
1235 sqlite3_interrupt(self->db);
1236
1237 Py_INCREF(Py_None);
1238 retval = Py_None;
1239
1240finally:
1241 return retval;
1242}
1243
Gregory P. Smithb9803422008-03-28 08:32:09 +00001244/* Function author: Paul Kippes <kippesp@gmail.com>
1245 * Class method of Connection to call the Python function _iterdump
1246 * of the sqlite3 module.
1247 */
1248static PyObject *
1249pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1250{
1251 PyObject* retval = NULL;
1252 PyObject* module = NULL;
1253 PyObject* module_dict;
1254 PyObject* pyfn_iterdump;
1255
1256 if (!pysqlite_check_connection(self)) {
1257 goto finally;
1258 }
1259
1260 module = PyImport_ImportModule(MODULE_NAME ".dump");
1261 if (!module) {
1262 goto finally;
1263 }
1264
1265 module_dict = PyModule_GetDict(module);
1266 if (!module_dict) {
1267 goto finally;
1268 }
1269
1270 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1271 if (!pyfn_iterdump) {
1272 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1273 goto finally;
1274 }
1275
1276 args = PyTuple_New(1);
1277 if (!args) {
1278 goto finally;
1279 }
1280 Py_INCREF(self);
1281 PyTuple_SetItem(args, 0, (PyObject*)self);
1282 retval = PyObject_CallObject(pyfn_iterdump, args);
1283
1284finally:
1285 Py_XDECREF(args);
1286 Py_XDECREF(module);
1287 return retval;
1288}
1289
Gerhard Häring1541ef02006-06-13 22:24:47 +00001290static PyObject *
Gerhard Häring0741a602007-01-14 01:43:50 +00001291pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Anthony Baxter72289a62006-04-04 06:29:05 +00001292{
1293 PyObject* callable;
1294 PyObject* uppercase_name = 0;
1295 PyObject* name;
1296 PyObject* retval;
1297 char* chk;
1298 int rc;
1299
Gerhard Häring0741a602007-01-14 01:43:50 +00001300 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxter72289a62006-04-04 06:29:05 +00001301 goto finally;
1302 }
1303
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001304 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyString_Type, &name, &callable)) {
Anthony Baxter72289a62006-04-04 06:29:05 +00001305 goto finally;
1306 }
1307
1308 uppercase_name = PyObject_CallMethod(name, "upper", "");
1309 if (!uppercase_name) {
1310 goto finally;
1311 }
1312
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001313 chk = PyString_AsString(uppercase_name);
Anthony Baxter72289a62006-04-04 06:29:05 +00001314 while (*chk) {
1315 if ((*chk >= '0' && *chk <= '9')
1316 || (*chk >= 'A' && *chk <= 'Z')
1317 || (*chk == '_'))
1318 {
1319 chk++;
1320 } else {
Gerhard Häring0741a602007-01-14 01:43:50 +00001321 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Anthony Baxter72289a62006-04-04 06:29:05 +00001322 goto finally;
1323 }
1324 }
1325
1326 if (callable != Py_None && !PyCallable_Check(callable)) {
1327 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1328 goto finally;
1329 }
1330
1331 if (callable != Py_None) {
1332 PyDict_SetItem(self->collations, uppercase_name, callable);
1333 } else {
1334 PyDict_DelItem(self->collations, uppercase_name);
1335 }
1336
1337 rc = sqlite3_create_collation(self->db,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001338 PyString_AsString(uppercase_name),
Anthony Baxter72289a62006-04-04 06:29:05 +00001339 SQLITE_UTF8,
1340 (callable != Py_None) ? callable : NULL,
Gerhard Häring0741a602007-01-14 01:43:50 +00001341 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Anthony Baxter72289a62006-04-04 06:29:05 +00001342 if (rc != SQLITE_OK) {
1343 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001344 _pysqlite_seterror(self->db, NULL);
Anthony Baxter72289a62006-04-04 06:29:05 +00001345 goto finally;
1346 }
1347
1348finally:
1349 Py_XDECREF(uppercase_name);
1350
1351 if (PyErr_Occurred()) {
1352 retval = NULL;
1353 } else {
1354 Py_INCREF(Py_None);
1355 retval = Py_None;
1356 }
1357
1358 return retval;
1359}
1360
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001361/* Called when the connection is used as a context manager. Returns itself as a
1362 * convenience to the caller. */
1363static PyObject *
1364pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1365{
1366 Py_INCREF(self);
1367 return (PyObject*)self;
1368}
1369
1370/** Called when the connection is used as a context manager. If there was any
1371 * exception, a rollback takes place; otherwise we commit. */
1372static PyObject *
1373pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1374{
1375 PyObject* exc_type, *exc_value, *exc_tb;
1376 char* method_name;
1377 PyObject* result;
1378
1379 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1380 return NULL;
1381 }
1382
1383 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1384 method_name = "commit";
1385 } else {
1386 method_name = "rollback";
1387 }
1388
1389 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1390 if (!result) {
1391 return NULL;
1392 }
1393 Py_DECREF(result);
1394
1395 Py_INCREF(Py_False);
1396 return Py_False;
1397}
1398
Anthony Baxterc51ee692006-04-01 00:57:31 +00001399static char connection_doc[] =
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001400PyDoc_STR("SQLite database connection object.");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001401
1402static PyGetSetDef connection_getset[] = {
Gerhard Häring0741a602007-01-14 01:43:50 +00001403 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1404 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Anthony Baxterc51ee692006-04-01 00:57:31 +00001405 {NULL}
1406};
1407
1408static PyMethodDef connection_methods[] = {
Gerhard Häring0741a602007-01-14 01:43:50 +00001409 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001410 PyDoc_STR("Return a cursor for the connection.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001411 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001412 PyDoc_STR("Closes the connection.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001413 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001414 PyDoc_STR("Commit the current transaction.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001415 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001416 PyDoc_STR("Roll back the current transaction.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001417 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001418 PyDoc_STR("Creates a new function. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001419 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001420 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001421 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Gerhard Häring1541ef02006-06-13 22:24:47 +00001422 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001423 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1424 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001425 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001426 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001427 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001428 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001429 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001430 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001431 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001432 PyDoc_STR("Creates a collation function. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001433 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Gerhard Häring1541ef02006-06-13 22:24:47 +00001434 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Gregory P. Smithb9803422008-03-28 08:32:09 +00001435 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Gerhard Häringef2276b2008-09-12 13:54:06 +00001436 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001437 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1438 PyDoc_STR("For context manager. Non-standard.")},
1439 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1440 PyDoc_STR("For context manager. Non-standard.")},
Anthony Baxterc51ee692006-04-01 00:57:31 +00001441 {NULL, NULL}
1442};
1443
1444static struct PyMemberDef connection_members[] =
1445{
Gerhard Häring0741a602007-01-14 01:43:50 +00001446 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), RO},
1447 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), RO},
1448 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), RO},
1449 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), RO},
1450 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), RO},
1451 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), RO},
1452 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), RO},
1453 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), RO},
1454 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), RO},
1455 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), RO},
1456 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1457 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Anthony Baxterc51ee692006-04-01 00:57:31 +00001458 {NULL}
1459};
1460
Gerhard Häring0741a602007-01-14 01:43:50 +00001461PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001462 PyVarObject_HEAD_INIT(NULL, 0)
Anthony Baxter8e7b4902006-04-05 18:25:33 +00001463 MODULE_NAME ".Connection", /* tp_name */
Gerhard Häring0741a602007-01-14 01:43:50 +00001464 sizeof(pysqlite_Connection), /* tp_basicsize */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001465 0, /* tp_itemsize */
Gerhard Häring0741a602007-01-14 01:43:50 +00001466 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001467 0, /* tp_print */
1468 0, /* tp_getattr */
1469 0, /* tp_setattr */
1470 0, /* tp_compare */
1471 0, /* tp_repr */
1472 0, /* tp_as_number */
1473 0, /* tp_as_sequence */
1474 0, /* tp_as_mapping */
1475 0, /* tp_hash */
Gerhard Häring0741a602007-01-14 01:43:50 +00001476 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001477 0, /* tp_str */
1478 0, /* tp_getattro */
1479 0, /* tp_setattro */
1480 0, /* tp_as_buffer */
1481 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1482 connection_doc, /* tp_doc */
1483 0, /* tp_traverse */
1484 0, /* tp_clear */
1485 0, /* tp_richcompare */
1486 0, /* tp_weaklistoffset */
1487 0, /* tp_iter */
1488 0, /* tp_iternext */
1489 connection_methods, /* tp_methods */
1490 connection_members, /* tp_members */
1491 connection_getset, /* tp_getset */
1492 0, /* tp_base */
1493 0, /* tp_dict */
1494 0, /* tp_descr_get */
1495 0, /* tp_descr_set */
1496 0, /* tp_dictoffset */
Gerhard Häring0741a602007-01-14 01:43:50 +00001497 (initproc)pysqlite_connection_init, /* tp_init */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001498 0, /* tp_alloc */
1499 0, /* tp_new */
1500 0 /* tp_free */
1501};
1502
Gerhard Häring0741a602007-01-14 01:43:50 +00001503extern int pysqlite_connection_setup_types(void)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001504{
Gerhard Häring0741a602007-01-14 01:43:50 +00001505 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1506 return PyType_Ready(&pysqlite_ConnectionType);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001507}