blob: b08b07d353e416cb31f734bc86764ce7ec0e4a12 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* connection.c - the connection type
2 *
Gerhard Häringe7ea7452008-03-29 00:45:29 +00003 * Copyright (C) 2004-2007 Gerhard Häring <gh@ghaering.de>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004 *
5 * This file is part of pysqlite.
6 *
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.
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"
31#include "sqlitecompat.h"
32
33#include "pythread.h"
34
Gerhard Häringe7ea7452008-03-29 00:45:29 +000035#define ACTION_FINALIZE 1
36#define ACTION_RESET 2
37
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000038static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000039
Thomas Wouters0e3f5912006-08-11 14:57:12 +000040
Benjamin Petersond7b03282008-09-13 15:58:53 +000041static void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
Thomas Wouters0e3f5912006-08-11 14:57:12 +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);
48#else
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000049 PyErr_SetString(pysqlite_OperationalError, errmsg);
Thomas Wouters0e3f5912006-08-11 14:57:12 +000050#endif
51}
52
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000053int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000054{
55 static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL};
56
57 char* database;
58 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;
65
66 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOi", kwlist,
67 &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements))
68 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000069 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000070 }
71
72 self->begin_statement = NULL;
73
74 self->statement_cache = NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +000075 self->statements = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000076
77 Py_INCREF(Py_None);
78 self->row_factory = Py_None;
79
80 Py_INCREF(&PyUnicode_Type);
81 self->text_factory = (PyObject*)&PyUnicode_Type;
82
83 Py_BEGIN_ALLOW_THREADS
84 rc = sqlite3_open(database, &self->db);
85 Py_END_ALLOW_THREADS
86
87 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000088 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000089 return -1;
90 }
91
92 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +000093 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +000094 if (!isolation_level) {
95 return -1;
96 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000097 } else {
98 Py_INCREF(isolation_level);
99 }
100 self->isolation_level = NULL;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000101 pysqlite_connection_set_isolation_level(self, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000102 Py_DECREF(isolation_level);
103
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000104 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000105 if (PyErr_Occurred()) {
106 return -1;
107 }
108
Thomas Wouters477c8d52006-05-27 19:21:47 +0000109 self->statements = PyList_New(0);
110 if (!self->statements) {
111 return -1;
112 }
113 self->created_statements = 0;
114
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000115 /* By default, the Cache class INCREFs the factory in its initializer, and
116 * decrefs it in its deallocator method. Since this would create a circular
117 * reference here, we're breaking it by decrementing self, and telling the
118 * cache class to not decref the factory (self) in its deallocator.
119 */
120 self->statement_cache->decref_factory = 0;
121 Py_DECREF(self);
122
123 self->inTransaction = 0;
124 self->detect_types = detect_types;
125 self->timeout = timeout;
126 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Georg Brandldfd73442009-04-05 11:47:34 +0000127#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000128 self->thread_ident = PyThread_get_thread_ident();
Georg Brandldfd73442009-04-05 11:47:34 +0000129#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000130 self->check_same_thread = check_same_thread;
131
132 self->function_pinboard = PyDict_New();
133 if (!self->function_pinboard) {
134 return -1;
135 }
136
137 self->collations = PyDict_New();
138 if (!self->collations) {
139 return -1;
140 }
141
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000142 self->Warning = pysqlite_Warning;
143 self->Error = pysqlite_Error;
144 self->InterfaceError = pysqlite_InterfaceError;
145 self->DatabaseError = pysqlite_DatabaseError;
146 self->DataError = pysqlite_DataError;
147 self->OperationalError = pysqlite_OperationalError;
148 self->IntegrityError = pysqlite_IntegrityError;
149 self->InternalError = pysqlite_InternalError;
150 self->ProgrammingError = pysqlite_ProgrammingError;
151 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152
153 return 0;
154}
155
Thomas Wouters477c8d52006-05-27 19:21:47 +0000156/* Empty the entire statement cache of this connection */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000157void pysqlite_flush_statement_cache(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000158{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000159 pysqlite_Node* node;
160 pysqlite_Statement* statement;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000161
162 node = self->statement_cache->first;
163
164 while (node) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000165 statement = (pysqlite_Statement*)(node->data);
166 (void)pysqlite_statement_finalize(statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000167 node = node->next;
168 }
169
170 Py_DECREF(self->statement_cache);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000171 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000172 Py_DECREF(self);
173 self->statement_cache->decref_factory = 0;
174}
175
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000176/* action in (ACTION_RESET, ACTION_FINALIZE) */
177void pysqlite_do_all_statements(pysqlite_Connection* self, int action)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000178{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000179 int i;
180 PyObject* weakref;
181 PyObject* statement;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000182
Thomas Wouters477c8d52006-05-27 19:21:47 +0000183 for (i = 0; i < PyList_Size(self->statements); i++) {
184 weakref = PyList_GetItem(self->statements, i);
185 statement = PyWeakref_GetObject(weakref);
186 if (statement != Py_None) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000187 if (action == ACTION_RESET) {
188 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
189 } else {
190 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
191 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000192 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000193 }
194}
195
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000196void pysqlite_connection_dealloc(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000197{
198 Py_XDECREF(self->statement_cache);
199
200 /* Clean up if user has not called .close() explicitly. */
201 if (self->db) {
202 Py_BEGIN_ALLOW_THREADS
203 sqlite3_close(self->db);
204 Py_END_ALLOW_THREADS
205 }
206
207 if (self->begin_statement) {
208 PyMem_Free(self->begin_statement);
209 }
210 Py_XDECREF(self->isolation_level);
211 Py_XDECREF(self->function_pinboard);
212 Py_XDECREF(self->row_factory);
213 Py_XDECREF(self->text_factory);
214 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000215 Py_XDECREF(self->statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000216
Christian Heimes90aa7642007-12-19 02:45:37 +0000217 Py_TYPE(self)->tp_free((PyObject*)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000218}
219
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000220PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000221{
222 static char *kwlist[] = {"factory", NULL, NULL};
223 PyObject* factory = NULL;
224 PyObject* cursor;
225
226
227 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
228 &factory)) {
229 return NULL;
230 }
231
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000232 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000233 return NULL;
234 }
235
236 if (factory == NULL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000237 factory = (PyObject*)&pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000238 }
239
240 cursor = PyObject_CallFunction(factory, "O", self);
241
242 if (cursor && self->row_factory != Py_None) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000243 Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000244 Py_INCREF(self->row_factory);
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000245 ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000246 }
247
248 return cursor;
249}
250
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000251PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000252{
253 int rc;
254
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000255 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000256 return NULL;
257 }
258
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000259 pysqlite_do_all_statements(self, ACTION_FINALIZE);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000260
261 if (self->db) {
262 Py_BEGIN_ALLOW_THREADS
263 rc = sqlite3_close(self->db);
264 Py_END_ALLOW_THREADS
265
266 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000267 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000268 return NULL;
269 } else {
270 self->db = NULL;
271 }
272 }
273
274 Py_INCREF(Py_None);
275 return Py_None;
276}
277
278/*
279 * Checks if a connection object is usable (i. e. not closed).
280 *
281 * 0 => error; 1 => ok
282 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000283int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000284{
285 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000286 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000287 return 0;
288 } else {
289 return 1;
290 }
291}
292
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000293PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000294{
295 int rc;
296 const char* tail;
297 sqlite3_stmt* statement;
298
299 Py_BEGIN_ALLOW_THREADS
300 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
301 Py_END_ALLOW_THREADS
302
303 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000304 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000305 goto error;
306 }
307
Benjamin Petersond7b03282008-09-13 15:58:53 +0000308 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000309 if (rc == SQLITE_DONE) {
310 self->inTransaction = 1;
311 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000312 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313 }
314
315 Py_BEGIN_ALLOW_THREADS
316 rc = sqlite3_finalize(statement);
317 Py_END_ALLOW_THREADS
318
319 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000320 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000321 }
322
323error:
324 if (PyErr_Occurred()) {
325 return NULL;
326 } else {
327 Py_INCREF(Py_None);
328 return Py_None;
329 }
330}
331
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000332PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000333{
334 int rc;
335 const char* tail;
336 sqlite3_stmt* statement;
337
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000338 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339 return NULL;
340 }
341
342 if (self->inTransaction) {
343 Py_BEGIN_ALLOW_THREADS
344 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
345 Py_END_ALLOW_THREADS
346 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000347 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000348 goto error;
349 }
350
Benjamin Petersond7b03282008-09-13 15:58:53 +0000351 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000352 if (rc == SQLITE_DONE) {
353 self->inTransaction = 0;
354 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000355 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000356 }
357
358 Py_BEGIN_ALLOW_THREADS
359 rc = sqlite3_finalize(statement);
360 Py_END_ALLOW_THREADS
361 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000362 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363 }
364
365 }
366
367error:
368 if (PyErr_Occurred()) {
369 return NULL;
370 } else {
371 Py_INCREF(Py_None);
372 return Py_None;
373 }
374}
375
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000376PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000377{
378 int rc;
379 const char* tail;
380 sqlite3_stmt* statement;
381
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000382 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000383 return NULL;
384 }
385
386 if (self->inTransaction) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000387 pysqlite_do_all_statements(self, ACTION_RESET);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000388
389 Py_BEGIN_ALLOW_THREADS
Georg Brandl0eaa9402007-08-11 15:39:18 +0000390 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000391 Py_END_ALLOW_THREADS
392 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000393 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000394 goto error;
395 }
396
Benjamin Petersond7b03282008-09-13 15:58:53 +0000397 rc = pysqlite_step(statement, self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000398 if (rc == SQLITE_DONE) {
399 self->inTransaction = 0;
400 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000401 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000402 }
403
404 Py_BEGIN_ALLOW_THREADS
405 rc = sqlite3_finalize(statement);
406 Py_END_ALLOW_THREADS
407 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000408 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000409 }
410
411 }
412
413error:
414 if (PyErr_Occurred()) {
415 return NULL;
416 } else {
417 Py_INCREF(Py_None);
418 return Py_None;
419 }
420}
421
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000422void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000423{
424 long longval;
425 const char* buffer;
426 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000427
Thomas Wouters477c8d52006-05-27 19:21:47 +0000428 if ((!py_val) || PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000429 sqlite3_result_null(context);
430 } else if (py_val == Py_None) {
431 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000432 } else if (PyLong_Check(py_val)) {
433 longval = PyLong_AsLong(py_val);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000434 sqlite3_result_int64(context, (PY_LONG_LONG)longval);
435 } else if (PyFloat_Check(py_val)) {
436 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000437 } else if (PyUnicode_Check(py_val)) {
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000438 sqlite3_result_text(context, _PyUnicode_AsString(py_val), -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000439 } else if (PyObject_CheckBuffer(py_val)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000440 if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
441 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000442 } else {
443 sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000444 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000445 } else {
446 /* TODO: raise error */
447 }
448}
449
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000450PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000451{
452 PyObject* args;
453 int i;
454 sqlite3_value* cur_value;
455 PyObject* cur_py_value;
456 const char* val_str;
457 PY_LONG_LONG val_int;
458 Py_ssize_t buflen;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000459
460 args = PyTuple_New(argc);
461 if (!args) {
462 return NULL;
463 }
464
465 for (i = 0; i < argc; i++) {
466 cur_value = argv[i];
467 switch (sqlite3_value_type(argv[i])) {
468 case SQLITE_INTEGER:
469 val_int = sqlite3_value_int64(cur_value);
Christian Heimes217cfd12007-12-02 14:31:20 +0000470 cur_py_value = PyLong_FromLong((long)val_int);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000471 break;
472 case SQLITE_FLOAT:
473 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
474 break;
475 case SQLITE_TEXT:
476 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000477 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000478 /* TODO: have a way to show errors here */
479 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000480 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000481 Py_INCREF(Py_None);
482 cur_py_value = Py_None;
483 }
484 break;
485 case SQLITE_BLOB:
486 buflen = sqlite3_value_bytes(cur_value);
Christian Heimes72b710a2008-05-26 13:28:38 +0000487 cur_py_value = PyBytes_FromStringAndSize(
Guido van Rossumbae07c92007-10-08 02:46:15 +0000488 sqlite3_value_blob(cur_value), buflen);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000489 break;
490 case SQLITE_NULL:
491 default:
492 Py_INCREF(Py_None);
493 cur_py_value = Py_None;
494 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000495
496 if (!cur_py_value) {
497 Py_DECREF(args);
498 return NULL;
499 }
500
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000501 PyTuple_SetItem(args, i, cur_py_value);
502
503 }
504
505 return args;
506}
507
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000508void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000509{
510 PyObject* args;
511 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000512 PyObject* py_retval = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000513
Georg Brandldfd73442009-04-05 11:47:34 +0000514#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000515 PyGILState_STATE threadstate;
516
517 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000518#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000519
520 py_func = (PyObject*)sqlite3_user_data(context);
521
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000522 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000523 if (args) {
524 py_retval = PyObject_CallObject(py_func, args);
525 Py_DECREF(args);
526 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000527
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000528 if (py_retval) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000529 _pysqlite_set_result(context, py_retval);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000530 Py_DECREF(py_retval);
531 } else {
532 if (_enable_callback_tracebacks) {
533 PyErr_Print();
534 } else {
535 PyErr_Clear();
536 }
537 _sqlite3_result_error(context, "user-defined function raised exception", -1);
538 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000539
Georg Brandldfd73442009-04-05 11:47:34 +0000540#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000541 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000542#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543}
544
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000545static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000546{
547 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000548 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000549 PyObject* aggregate_class;
550 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000551 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000552
Georg Brandldfd73442009-04-05 11:47:34 +0000553#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000554 PyGILState_STATE threadstate;
555
556 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000557#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000558
559 aggregate_class = (PyObject*)sqlite3_user_data(context);
560
561 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
562
563 if (*aggregate_instance == 0) {
564 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
565
Thomas Wouters477c8d52006-05-27 19:21:47 +0000566 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000567 *aggregate_instance = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000568 if (_enable_callback_tracebacks) {
569 PyErr_Print();
570 } else {
571 PyErr_Clear();
572 }
573 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000574 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000575 }
576 }
577
578 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000579 if (!stepmethod) {
580 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000581 }
582
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000583 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000584 if (!args) {
585 goto error;
586 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000587
588 function_result = PyObject_CallObject(stepmethod, args);
589 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000590
Thomas Wouters477c8d52006-05-27 19:21:47 +0000591 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000592 if (_enable_callback_tracebacks) {
593 PyErr_Print();
594 } else {
595 PyErr_Clear();
596 }
597 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000598 }
599
Thomas Wouters477c8d52006-05-27 19:21:47 +0000600error:
601 Py_XDECREF(stepmethod);
602 Py_XDECREF(function_result);
603
Georg Brandldfd73442009-04-05 11:47:34 +0000604#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000605 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000606#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000607}
608
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000609void _pysqlite_final_callback(sqlite3_context* context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000610{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000611 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000612 PyObject** aggregate_instance;
613 PyObject* aggregate_class;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000614
Georg Brandldfd73442009-04-05 11:47:34 +0000615#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000616 PyGILState_STATE threadstate;
617
618 threadstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000619#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000620
621 aggregate_class = (PyObject*)sqlite3_user_data(context);
622
623 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
624 if (!*aggregate_instance) {
625 /* this branch is executed if there was an exception in the aggregate's
626 * __init__ */
627
Thomas Wouters477c8d52006-05-27 19:21:47 +0000628 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000629 }
630
Thomas Wouters477c8d52006-05-27 19:21:47 +0000631 function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
632 if (!function_result) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000633 if (_enable_callback_tracebacks) {
634 PyErr_Print();
635 } else {
636 PyErr_Clear();
637 }
638 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
639 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000640 _pysqlite_set_result(context, function_result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000641 }
642
Thomas Wouters477c8d52006-05-27 19:21:47 +0000643error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000644 Py_XDECREF(*aggregate_instance);
645 Py_XDECREF(function_result);
646
Georg Brandldfd73442009-04-05 11:47:34 +0000647#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000648 PyGILState_Release(threadstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000649#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000650}
651
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000652void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000653{
654 PyObject* new_list;
655 PyObject* weakref;
656 int i;
657
658 /* we only need to do this once in a while */
659 if (self->created_statements++ < 200) {
660 return;
661 }
662
663 self->created_statements = 0;
664
665 new_list = PyList_New(0);
666 if (!new_list) {
667 return;
668 }
669
670 for (i = 0; i < PyList_Size(self->statements); i++) {
671 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000672 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000673 if (PyList_Append(new_list, weakref) != 0) {
674 Py_DECREF(new_list);
675 return;
676 }
677 }
678 }
679
680 Py_DECREF(self->statements);
681 self->statements = new_list;
682}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000683
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000684PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000685{
686 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
687
688 PyObject* func;
689 char* name;
690 int narg;
691 int rc;
692
Gerhard Häring78576502010-03-05 15:55:55 +0000693 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
694 return NULL;
695 }
696
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000697 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
698 &name, &narg, &func))
699 {
700 return NULL;
701 }
702
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000703 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000704
Thomas Wouters477c8d52006-05-27 19:21:47 +0000705 if (rc != SQLITE_OK) {
706 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000707 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000708 return NULL;
709 } else {
710 PyDict_SetItem(self->function_pinboard, func, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711
Thomas Wouters477c8d52006-05-27 19:21:47 +0000712 Py_INCREF(Py_None);
713 return Py_None;
714 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000715}
716
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000717PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000718{
719 PyObject* aggregate_class;
720
721 int n_arg;
722 char* name;
723 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
724 int rc;
725
Gerhard Häring78576502010-03-05 15:55:55 +0000726 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
727 return NULL;
728 }
729
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000730 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
731 kwlist, &name, &n_arg, &aggregate_class)) {
732 return NULL;
733 }
734
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000735 rc = sqlite3_create_function(self->db, name, n_arg, SQLITE_UTF8, (void*)aggregate_class, 0, &_pysqlite_step_callback, &_pysqlite_final_callback);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000736 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000737 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000738 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000739 return NULL;
740 } else {
741 PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None);
742
743 Py_INCREF(Py_None);
744 return Py_None;
745 }
746}
747
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000748static int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000749{
750 PyObject *ret;
751 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000752#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000753 PyGILState_STATE gilstate;
754
755 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000756#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000757 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
758
759 if (!ret) {
760 if (_enable_callback_tracebacks) {
761 PyErr_Print();
762 } else {
763 PyErr_Clear();
764 }
765
766 rc = SQLITE_DENY;
767 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000768 if (PyLong_Check(ret)) {
769 rc = (int)PyLong_AsLong(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000770 } else {
771 rc = SQLITE_DENY;
772 }
773 Py_DECREF(ret);
774 }
775
Georg Brandldfd73442009-04-05 11:47:34 +0000776#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000777 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000778#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000779 return rc;
780}
781
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000782static int _progress_handler(void* user_arg)
783{
784 int rc;
785 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000786#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000787 PyGILState_STATE gilstate;
788
789 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000790#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000791 ret = PyObject_CallFunction((PyObject*)user_arg, "");
792
793 if (!ret) {
794 if (_enable_callback_tracebacks) {
795 PyErr_Print();
796 } else {
797 PyErr_Clear();
798 }
799
Mark Dickinson934896d2009-02-21 20:59:32 +0000800 /* abort query if error occurred */
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000801 rc = 1;
802 } else {
803 rc = (int)PyObject_IsTrue(ret);
804 Py_DECREF(ret);
805 }
806
Georg Brandldfd73442009-04-05 11:47:34 +0000807#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000808 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000809#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000810 return rc;
811}
812
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000813PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000814{
815 PyObject* authorizer_cb;
816
817 static char *kwlist[] = { "authorizer_callback", NULL };
818 int rc;
819
Gerhard Häring78576502010-03-05 15:55:55 +0000820 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
821 return NULL;
822 }
823
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000824 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
825 kwlist, &authorizer_cb)) {
826 return NULL;
827 }
828
829 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
830
831 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000832 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000833 return NULL;
834 } else {
835 PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None);
836
837 Py_INCREF(Py_None);
838 return Py_None;
839 }
840}
841
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000842PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
843{
844 PyObject* progress_handler;
845 int n;
846
847 static char *kwlist[] = { "progress_handler", "n", NULL };
848
Gerhard Häring78576502010-03-05 15:55:55 +0000849 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
850 return NULL;
851 }
852
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000853 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
854 kwlist, &progress_handler, &n)) {
855 return NULL;
856 }
857
858 if (progress_handler == Py_None) {
859 /* None clears the progress handler previously set */
860 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
861 } else {
862 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
863 PyDict_SetItem(self->function_pinboard, progress_handler, Py_None);
864 }
865
866 Py_INCREF(Py_None);
867 return Py_None;
868}
869
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000870int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000871{
Georg Brandldfd73442009-04-05 11:47:34 +0000872#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000873 if (self->check_same_thread) {
874 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000875 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000876 "SQLite objects created in a thread can only be used in that same thread."
877 "The object was created in thread id %ld and this is thread id %ld",
878 self->thread_ident, PyThread_get_thread_ident());
879 return 0;
880 }
881
882 }
Georg Brandldfd73442009-04-05 11:47:34 +0000883#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000884 return 1;
885}
886
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000887static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000888{
889 Py_INCREF(self->isolation_level);
890 return self->isolation_level;
891}
892
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000893static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000894{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000895 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000896 return NULL;
897 } else {
898 return Py_BuildValue("i", sqlite3_total_changes(self->db));
899 }
900}
901
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000902static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000903{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000904 PyObject* res;
905 PyObject* begin_statement;
Georg Brandlceab6102007-11-25 00:45:05 +0000906 static PyObject* begin_word;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000907
908 Py_XDECREF(self->isolation_level);
909
910 if (self->begin_statement) {
911 PyMem_Free(self->begin_statement);
912 self->begin_statement = NULL;
913 }
914
915 if (isolation_level == Py_None) {
916 Py_INCREF(Py_None);
917 self->isolation_level = Py_None;
918
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000919 res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000920 if (!res) {
921 return -1;
922 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000923 Py_DECREF(res);
924
925 self->inTransaction = 0;
926 } else {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000927 const char *statement;
928 Py_ssize_t size;
929
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000930 Py_INCREF(isolation_level);
931 self->isolation_level = isolation_level;
932
Georg Brandlceab6102007-11-25 00:45:05 +0000933 if (!begin_word) {
934 begin_word = PyUnicode_FromString("BEGIN ");
935 if (!begin_word) return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000936 }
Georg Brandlceab6102007-11-25 00:45:05 +0000937 begin_statement = PyUnicode_Concat(begin_word, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000938 if (!begin_statement) {
939 return -1;
940 }
941
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000942 statement = _PyUnicode_AsStringAndSize(begin_statement, &size);
Georg Brandl3dbca812008-07-23 16:10:53 +0000943 if (!statement) {
944 Py_DECREF(statement);
945 return -1;
946 }
Neal Norwitzefee9f52007-10-27 02:50:52 +0000947 self->begin_statement = PyMem_Malloc(size + 2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000948 if (!self->begin_statement) {
Georg Brandl3dbca812008-07-23 16:10:53 +0000949 Py_DECREF(begin_statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000950 return -1;
951 }
952
Neal Norwitzefee9f52007-10-27 02:50:52 +0000953 strcpy(self->begin_statement, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000954 Py_DECREF(begin_statement);
955 }
956
957 return 0;
958}
959
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000960PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000961{
962 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000963 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000964 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000965 int rc;
966
Gerhard Häring78576502010-03-05 15:55:55 +0000967 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
968 return NULL;
969 }
970
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000971 if (!PyArg_ParseTuple(args, "O", &sql)) {
972 return NULL;
973 }
974
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000975 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000976
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000977 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000978 if (!statement) {
979 return NULL;
980 }
981
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000982 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000983
984 if (rc != SQLITE_OK) {
985 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000986 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000987 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000988 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000989 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000990 (void)pysqlite_statement_reset(statement);
991 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000992 }
993
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000994 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000995 } else {
996 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
997 if (!weakref) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000998 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000999 goto error;
1000 }
1001
1002 if (PyList_Append(self->statements, weakref) != 0) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001003 Py_CLEAR(weakref);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001004 goto error;
1005 }
1006
1007 Py_DECREF(weakref);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001008 }
1009
Thomas Wouters477c8d52006-05-27 19:21:47 +00001010error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001011 return (PyObject*)statement;
1012}
1013
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001014PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001015{
1016 PyObject* cursor = 0;
1017 PyObject* result = 0;
1018 PyObject* method = 0;
1019
1020 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1021 if (!cursor) {
1022 goto error;
1023 }
1024
1025 method = PyObject_GetAttrString(cursor, "execute");
1026 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001027 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001028 goto error;
1029 }
1030
1031 result = PyObject_CallObject(method, args);
1032 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001033 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001034 }
1035
1036error:
1037 Py_XDECREF(result);
1038 Py_XDECREF(method);
1039
1040 return cursor;
1041}
1042
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001043PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001044{
1045 PyObject* cursor = 0;
1046 PyObject* result = 0;
1047 PyObject* method = 0;
1048
1049 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1050 if (!cursor) {
1051 goto error;
1052 }
1053
1054 method = PyObject_GetAttrString(cursor, "executemany");
1055 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001056 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001057 goto error;
1058 }
1059
1060 result = PyObject_CallObject(method, args);
1061 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001062 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001063 }
1064
1065error:
1066 Py_XDECREF(result);
1067 Py_XDECREF(method);
1068
1069 return cursor;
1070}
1071
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001072PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001073{
1074 PyObject* cursor = 0;
1075 PyObject* result = 0;
1076 PyObject* method = 0;
1077
1078 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1079 if (!cursor) {
1080 goto error;
1081 }
1082
1083 method = PyObject_GetAttrString(cursor, "executescript");
1084 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001085 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001086 goto error;
1087 }
1088
1089 result = PyObject_CallObject(method, args);
1090 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001091 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001092 }
1093
1094error:
1095 Py_XDECREF(result);
1096 Py_XDECREF(method);
1097
1098 return cursor;
1099}
1100
1101/* ------------------------- COLLATION CODE ------------------------ */
1102
1103static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001104pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001105 void* context,
1106 int text1_length, const void* text1_data,
1107 int text2_length, const void* text2_data)
1108{
1109 PyObject* callback = (PyObject*)context;
1110 PyObject* string1 = 0;
1111 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001112#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001113 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001114#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001115 PyObject* retval = NULL;
1116 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001117#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001118 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001119#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001120
1121 if (PyErr_Occurred()) {
1122 goto finally;
1123 }
1124
Guido van Rossum98297ee2007-11-06 21:34:58 +00001125 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1126 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001127
1128 if (!string1 || !string2) {
1129 goto finally; /* failed to allocate strings */
1130 }
1131
1132 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1133
1134 if (!retval) {
1135 /* execution failed */
1136 goto finally;
1137 }
1138
Christian Heimes217cfd12007-12-02 14:31:20 +00001139 result = PyLong_AsLong(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001140 if (PyErr_Occurred()) {
1141 result = 0;
1142 }
1143
1144finally:
1145 Py_XDECREF(string1);
1146 Py_XDECREF(string2);
1147 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001148#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001149 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001150#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001151 return result;
1152}
1153
1154static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001155pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001156{
1157 PyObject* retval = NULL;
1158
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001159 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001160 goto finally;
1161 }
1162
1163 sqlite3_interrupt(self->db);
1164
1165 Py_INCREF(Py_None);
1166 retval = Py_None;
1167
1168finally:
1169 return retval;
1170}
1171
Christian Heimesbbe741d2008-03-28 10:53:29 +00001172/* Function author: Paul Kippes <kippesp@gmail.com>
1173 * Class method of Connection to call the Python function _iterdump
1174 * of the sqlite3 module.
1175 */
1176static PyObject *
1177pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1178{
1179 PyObject* retval = NULL;
1180 PyObject* module = NULL;
1181 PyObject* module_dict;
1182 PyObject* pyfn_iterdump;
1183
1184 if (!pysqlite_check_connection(self)) {
1185 goto finally;
1186 }
1187
1188 module = PyImport_ImportModule(MODULE_NAME ".dump");
1189 if (!module) {
1190 goto finally;
1191 }
1192
1193 module_dict = PyModule_GetDict(module);
1194 if (!module_dict) {
1195 goto finally;
1196 }
1197
1198 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1199 if (!pyfn_iterdump) {
1200 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1201 goto finally;
1202 }
1203
1204 args = PyTuple_New(1);
1205 if (!args) {
1206 goto finally;
1207 }
1208 Py_INCREF(self);
1209 PyTuple_SetItem(args, 0, (PyObject*)self);
1210 retval = PyObject_CallObject(pyfn_iterdump, args);
1211
1212finally:
1213 Py_XDECREF(args);
1214 Py_XDECREF(module);
1215 return retval;
1216}
1217
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001218static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001219pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001220{
1221 PyObject* callable;
1222 PyObject* uppercase_name = 0;
1223 PyObject* name;
1224 PyObject* retval;
1225 char* chk;
1226 int rc;
1227
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001228 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001229 goto finally;
1230 }
1231
Gerhard Häring6d214562007-08-10 18:15:11 +00001232 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001233 goto finally;
1234 }
1235
1236 uppercase_name = PyObject_CallMethod(name, "upper", "");
1237 if (!uppercase_name) {
1238 goto finally;
1239 }
1240
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001241 chk = _PyUnicode_AsString(uppercase_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001242 while (*chk) {
1243 if ((*chk >= '0' && *chk <= '9')
1244 || (*chk >= 'A' && *chk <= 'Z')
1245 || (*chk == '_'))
1246 {
1247 chk++;
1248 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001249 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001250 goto finally;
1251 }
1252 }
1253
1254 if (callable != Py_None && !PyCallable_Check(callable)) {
1255 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1256 goto finally;
1257 }
1258
1259 if (callable != Py_None) {
1260 PyDict_SetItem(self->collations, uppercase_name, callable);
1261 } else {
1262 PyDict_DelItem(self->collations, uppercase_name);
1263 }
1264
1265 rc = sqlite3_create_collation(self->db,
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001266 _PyUnicode_AsString(uppercase_name),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001267 SQLITE_UTF8,
1268 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001269 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001270 if (rc != SQLITE_OK) {
1271 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001272 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001273 goto finally;
1274 }
1275
1276finally:
1277 Py_XDECREF(uppercase_name);
1278
1279 if (PyErr_Occurred()) {
1280 retval = NULL;
1281 } else {
1282 Py_INCREF(Py_None);
1283 retval = Py_None;
1284 }
1285
1286 return retval;
1287}
1288
Christian Heimesbbe741d2008-03-28 10:53:29 +00001289/* Called when the connection is used as a context manager. Returns itself as a
1290 * convenience to the caller. */
1291static PyObject *
1292pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1293{
1294 Py_INCREF(self);
1295 return (PyObject*)self;
1296}
1297
1298/** Called when the connection is used as a context manager. If there was any
1299 * exception, a rollback takes place; otherwise we commit. */
1300static PyObject *
1301pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1302{
1303 PyObject* exc_type, *exc_value, *exc_tb;
1304 char* method_name;
1305 PyObject* result;
1306
1307 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1308 return NULL;
1309 }
1310
1311 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1312 method_name = "commit";
1313 } else {
1314 method_name = "rollback";
1315 }
1316
1317 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1318 if (!result) {
1319 return NULL;
1320 }
1321 Py_DECREF(result);
1322
1323 Py_RETURN_FALSE;
1324}
1325
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001326static char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001327PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001328
1329static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001330 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1331 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001332 {NULL}
1333};
1334
1335static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001336 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001337 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001338 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001339 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001340 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001341 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001342 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001343 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001344 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001345 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001346 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001347 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001348 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001349 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001350 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1351 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001352 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001353 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001354 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001355 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001356 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001357 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001358 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001359 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001360 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001361 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001362 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001363 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001364 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1365 PyDoc_STR("For context manager. Non-standard.")},
1366 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1367 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001368 {NULL, NULL}
1369};
1370
1371static struct PyMemberDef connection_members[] =
1372{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001373 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1374 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1375 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1376 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1377 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1378 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1379 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1380 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1381 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1382 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001383 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1384 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001385 {NULL}
1386};
1387
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001388PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001389 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001390 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001391 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001392 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001393 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001394 0, /* tp_print */
1395 0, /* tp_getattr */
1396 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001397 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001398 0, /* tp_repr */
1399 0, /* tp_as_number */
1400 0, /* tp_as_sequence */
1401 0, /* tp_as_mapping */
1402 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001403 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001404 0, /* tp_str */
1405 0, /* tp_getattro */
1406 0, /* tp_setattro */
1407 0, /* tp_as_buffer */
1408 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1409 connection_doc, /* tp_doc */
1410 0, /* tp_traverse */
1411 0, /* tp_clear */
1412 0, /* tp_richcompare */
1413 0, /* tp_weaklistoffset */
1414 0, /* tp_iter */
1415 0, /* tp_iternext */
1416 connection_methods, /* tp_methods */
1417 connection_members, /* tp_members */
1418 connection_getset, /* tp_getset */
1419 0, /* tp_base */
1420 0, /* tp_dict */
1421 0, /* tp_descr_get */
1422 0, /* tp_descr_set */
1423 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001424 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001425 0, /* tp_alloc */
1426 0, /* tp_new */
1427 0 /* tp_free */
1428};
1429
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001430extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001431{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001432 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1433 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001434}