blob: a3170f31badb97505b4d014fa1417864690406dc [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
693 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
694 &name, &narg, &func))
695 {
696 return NULL;
697 }
698
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000699 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000700
Thomas Wouters477c8d52006-05-27 19:21:47 +0000701 if (rc != SQLITE_OK) {
702 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000703 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000704 return NULL;
705 } else {
706 PyDict_SetItem(self->function_pinboard, func, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000707
Thomas Wouters477c8d52006-05-27 19:21:47 +0000708 Py_INCREF(Py_None);
709 return Py_None;
710 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000711}
712
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000713PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000714{
715 PyObject* aggregate_class;
716
717 int n_arg;
718 char* name;
719 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
720 int rc;
721
722 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
723 kwlist, &name, &n_arg, &aggregate_class)) {
724 return NULL;
725 }
726
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000727 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 +0000728 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000729 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000730 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000731 return NULL;
732 } else {
733 PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None);
734
735 Py_INCREF(Py_None);
736 return Py_None;
737 }
738}
739
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000740static 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 +0000741{
742 PyObject *ret;
743 int rc;
Georg Brandldfd73442009-04-05 11:47:34 +0000744#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000745 PyGILState_STATE gilstate;
746
747 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000748#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000749 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
750
751 if (!ret) {
752 if (_enable_callback_tracebacks) {
753 PyErr_Print();
754 } else {
755 PyErr_Clear();
756 }
757
758 rc = SQLITE_DENY;
759 } else {
Christian Heimes217cfd12007-12-02 14:31:20 +0000760 if (PyLong_Check(ret)) {
761 rc = (int)PyLong_AsLong(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000762 } else {
763 rc = SQLITE_DENY;
764 }
765 Py_DECREF(ret);
766 }
767
Georg Brandldfd73442009-04-05 11:47:34 +0000768#ifdef WITH_THREAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000769 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000770#endif
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000771 return rc;
772}
773
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000774static int _progress_handler(void* user_arg)
775{
776 int rc;
777 PyObject *ret;
Georg Brandldfd73442009-04-05 11:47:34 +0000778#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000779 PyGILState_STATE gilstate;
780
781 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +0000782#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000783 ret = PyObject_CallFunction((PyObject*)user_arg, "");
784
785 if (!ret) {
786 if (_enable_callback_tracebacks) {
787 PyErr_Print();
788 } else {
789 PyErr_Clear();
790 }
791
Mark Dickinson934896d2009-02-21 20:59:32 +0000792 /* abort query if error occurred */
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000793 rc = 1;
794 } else {
795 rc = (int)PyObject_IsTrue(ret);
796 Py_DECREF(ret);
797 }
798
Georg Brandldfd73442009-04-05 11:47:34 +0000799#ifdef WITH_THREAD
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000800 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +0000801#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000802 return rc;
803}
804
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000805PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000806{
807 PyObject* authorizer_cb;
808
809 static char *kwlist[] = { "authorizer_callback", NULL };
810 int rc;
811
812 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
813 kwlist, &authorizer_cb)) {
814 return NULL;
815 }
816
817 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
818
819 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000820 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000821 return NULL;
822 } else {
823 PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None);
824
825 Py_INCREF(Py_None);
826 return Py_None;
827 }
828}
829
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000830PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
831{
832 PyObject* progress_handler;
833 int n;
834
835 static char *kwlist[] = { "progress_handler", "n", NULL };
836
837 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
838 kwlist, &progress_handler, &n)) {
839 return NULL;
840 }
841
842 if (progress_handler == Py_None) {
843 /* None clears the progress handler previously set */
844 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
845 } else {
846 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
847 PyDict_SetItem(self->function_pinboard, progress_handler, Py_None);
848 }
849
850 Py_INCREF(Py_None);
851 return Py_None;
852}
853
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000854int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000855{
Georg Brandldfd73442009-04-05 11:47:34 +0000856#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000857 if (self->check_same_thread) {
858 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000859 PyErr_Format(pysqlite_ProgrammingError,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000860 "SQLite objects created in a thread can only be used in that same thread."
861 "The object was created in thread id %ld and this is thread id %ld",
862 self->thread_ident, PyThread_get_thread_ident());
863 return 0;
864 }
865
866 }
Georg Brandldfd73442009-04-05 11:47:34 +0000867#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000868 return 1;
869}
870
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000871static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000872{
873 Py_INCREF(self->isolation_level);
874 return self->isolation_level;
875}
876
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000877static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000878{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000879 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000880 return NULL;
881 } else {
882 return Py_BuildValue("i", sqlite3_total_changes(self->db));
883 }
884}
885
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000886static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000887{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000888 PyObject* res;
889 PyObject* begin_statement;
Georg Brandlceab6102007-11-25 00:45:05 +0000890 static PyObject* begin_word;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000891
892 Py_XDECREF(self->isolation_level);
893
894 if (self->begin_statement) {
895 PyMem_Free(self->begin_statement);
896 self->begin_statement = NULL;
897 }
898
899 if (isolation_level == Py_None) {
900 Py_INCREF(Py_None);
901 self->isolation_level = Py_None;
902
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000903 res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000904 if (!res) {
905 return -1;
906 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000907 Py_DECREF(res);
908
909 self->inTransaction = 0;
910 } else {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000911 const char *statement;
912 Py_ssize_t size;
913
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000914 Py_INCREF(isolation_level);
915 self->isolation_level = isolation_level;
916
Georg Brandlceab6102007-11-25 00:45:05 +0000917 if (!begin_word) {
918 begin_word = PyUnicode_FromString("BEGIN ");
919 if (!begin_word) return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000920 }
Georg Brandlceab6102007-11-25 00:45:05 +0000921 begin_statement = PyUnicode_Concat(begin_word, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000922 if (!begin_statement) {
923 return -1;
924 }
925
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000926 statement = _PyUnicode_AsStringAndSize(begin_statement, &size);
Georg Brandl3dbca812008-07-23 16:10:53 +0000927 if (!statement) {
928 Py_DECREF(statement);
929 return -1;
930 }
Neal Norwitzefee9f52007-10-27 02:50:52 +0000931 self->begin_statement = PyMem_Malloc(size + 2);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000932 if (!self->begin_statement) {
Georg Brandl3dbca812008-07-23 16:10:53 +0000933 Py_DECREF(begin_statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000934 return -1;
935 }
936
Neal Norwitzefee9f52007-10-27 02:50:52 +0000937 strcpy(self->begin_statement, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000938 Py_DECREF(begin_statement);
939 }
940
941 return 0;
942}
943
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000944PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000945{
946 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000947 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000948 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000949 int rc;
950
951 if (!PyArg_ParseTuple(args, "O", &sql)) {
952 return NULL;
953 }
954
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000955 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000956
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000957 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000958 if (!statement) {
959 return NULL;
960 }
961
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000962 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000963
964 if (rc != SQLITE_OK) {
965 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000966 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000967 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000968 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000969 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000970 (void)pysqlite_statement_reset(statement);
971 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000972 }
973
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000974 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000975 } else {
976 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
977 if (!weakref) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000978 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000979 goto error;
980 }
981
982 if (PyList_Append(self->statements, weakref) != 0) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +0000983 Py_CLEAR(weakref);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000984 goto error;
985 }
986
987 Py_DECREF(weakref);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000988 }
989
Thomas Wouters477c8d52006-05-27 19:21:47 +0000990error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000991 return (PyObject*)statement;
992}
993
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000994PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000995{
996 PyObject* cursor = 0;
997 PyObject* result = 0;
998 PyObject* method = 0;
999
1000 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1001 if (!cursor) {
1002 goto error;
1003 }
1004
1005 method = PyObject_GetAttrString(cursor, "execute");
1006 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001007 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001008 goto error;
1009 }
1010
1011 result = PyObject_CallObject(method, args);
1012 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001013 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001014 }
1015
1016error:
1017 Py_XDECREF(result);
1018 Py_XDECREF(method);
1019
1020 return cursor;
1021}
1022
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001023PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001024{
1025 PyObject* cursor = 0;
1026 PyObject* result = 0;
1027 PyObject* method = 0;
1028
1029 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1030 if (!cursor) {
1031 goto error;
1032 }
1033
1034 method = PyObject_GetAttrString(cursor, "executemany");
1035 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001036 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001037 goto error;
1038 }
1039
1040 result = PyObject_CallObject(method, args);
1041 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001042 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001043 }
1044
1045error:
1046 Py_XDECREF(result);
1047 Py_XDECREF(method);
1048
1049 return cursor;
1050}
1051
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001052PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001053{
1054 PyObject* cursor = 0;
1055 PyObject* result = 0;
1056 PyObject* method = 0;
1057
1058 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1059 if (!cursor) {
1060 goto error;
1061 }
1062
1063 method = PyObject_GetAttrString(cursor, "executescript");
1064 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001065 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001066 goto error;
1067 }
1068
1069 result = PyObject_CallObject(method, args);
1070 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001071 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001072 }
1073
1074error:
1075 Py_XDECREF(result);
1076 Py_XDECREF(method);
1077
1078 return cursor;
1079}
1080
1081/* ------------------------- COLLATION CODE ------------------------ */
1082
1083static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001084pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001085 void* context,
1086 int text1_length, const void* text1_data,
1087 int text2_length, const void* text2_data)
1088{
1089 PyObject* callback = (PyObject*)context;
1090 PyObject* string1 = 0;
1091 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001092#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001093 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001094#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001095 PyObject* retval = NULL;
1096 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001097#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001098 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001099#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001100
1101 if (PyErr_Occurred()) {
1102 goto finally;
1103 }
1104
Guido van Rossum98297ee2007-11-06 21:34:58 +00001105 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1106 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001107
1108 if (!string1 || !string2) {
1109 goto finally; /* failed to allocate strings */
1110 }
1111
1112 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1113
1114 if (!retval) {
1115 /* execution failed */
1116 goto finally;
1117 }
1118
Christian Heimes217cfd12007-12-02 14:31:20 +00001119 result = PyLong_AsLong(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001120 if (PyErr_Occurred()) {
1121 result = 0;
1122 }
1123
1124finally:
1125 Py_XDECREF(string1);
1126 Py_XDECREF(string2);
1127 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001128#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001129 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001130#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001131 return result;
1132}
1133
1134static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001135pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001136{
1137 PyObject* retval = NULL;
1138
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001139 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001140 goto finally;
1141 }
1142
1143 sqlite3_interrupt(self->db);
1144
1145 Py_INCREF(Py_None);
1146 retval = Py_None;
1147
1148finally:
1149 return retval;
1150}
1151
Christian Heimesbbe741d2008-03-28 10:53:29 +00001152/* Function author: Paul Kippes <kippesp@gmail.com>
1153 * Class method of Connection to call the Python function _iterdump
1154 * of the sqlite3 module.
1155 */
1156static PyObject *
1157pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1158{
1159 PyObject* retval = NULL;
1160 PyObject* module = NULL;
1161 PyObject* module_dict;
1162 PyObject* pyfn_iterdump;
1163
1164 if (!pysqlite_check_connection(self)) {
1165 goto finally;
1166 }
1167
1168 module = PyImport_ImportModule(MODULE_NAME ".dump");
1169 if (!module) {
1170 goto finally;
1171 }
1172
1173 module_dict = PyModule_GetDict(module);
1174 if (!module_dict) {
1175 goto finally;
1176 }
1177
1178 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1179 if (!pyfn_iterdump) {
1180 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1181 goto finally;
1182 }
1183
1184 args = PyTuple_New(1);
1185 if (!args) {
1186 goto finally;
1187 }
1188 Py_INCREF(self);
1189 PyTuple_SetItem(args, 0, (PyObject*)self);
1190 retval = PyObject_CallObject(pyfn_iterdump, args);
1191
1192finally:
1193 Py_XDECREF(args);
1194 Py_XDECREF(module);
1195 return retval;
1196}
1197
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001198static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001199pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001200{
1201 PyObject* callable;
1202 PyObject* uppercase_name = 0;
1203 PyObject* name;
1204 PyObject* retval;
1205 char* chk;
1206 int rc;
1207
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001208 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001209 goto finally;
1210 }
1211
Gerhard Häring6d214562007-08-10 18:15:11 +00001212 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001213 goto finally;
1214 }
1215
1216 uppercase_name = PyObject_CallMethod(name, "upper", "");
1217 if (!uppercase_name) {
1218 goto finally;
1219 }
1220
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001221 chk = _PyUnicode_AsString(uppercase_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001222 while (*chk) {
1223 if ((*chk >= '0' && *chk <= '9')
1224 || (*chk >= 'A' && *chk <= 'Z')
1225 || (*chk == '_'))
1226 {
1227 chk++;
1228 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001229 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001230 goto finally;
1231 }
1232 }
1233
1234 if (callable != Py_None && !PyCallable_Check(callable)) {
1235 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1236 goto finally;
1237 }
1238
1239 if (callable != Py_None) {
1240 PyDict_SetItem(self->collations, uppercase_name, callable);
1241 } else {
1242 PyDict_DelItem(self->collations, uppercase_name);
1243 }
1244
1245 rc = sqlite3_create_collation(self->db,
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001246 _PyUnicode_AsString(uppercase_name),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001247 SQLITE_UTF8,
1248 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001249 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001250 if (rc != SQLITE_OK) {
1251 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001252 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001253 goto finally;
1254 }
1255
1256finally:
1257 Py_XDECREF(uppercase_name);
1258
1259 if (PyErr_Occurred()) {
1260 retval = NULL;
1261 } else {
1262 Py_INCREF(Py_None);
1263 retval = Py_None;
1264 }
1265
1266 return retval;
1267}
1268
Christian Heimesbbe741d2008-03-28 10:53:29 +00001269/* Called when the connection is used as a context manager. Returns itself as a
1270 * convenience to the caller. */
1271static PyObject *
1272pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1273{
1274 Py_INCREF(self);
1275 return (PyObject*)self;
1276}
1277
1278/** Called when the connection is used as a context manager. If there was any
1279 * exception, a rollback takes place; otherwise we commit. */
1280static PyObject *
1281pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1282{
1283 PyObject* exc_type, *exc_value, *exc_tb;
1284 char* method_name;
1285 PyObject* result;
1286
1287 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1288 return NULL;
1289 }
1290
1291 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1292 method_name = "commit";
1293 } else {
1294 method_name = "rollback";
1295 }
1296
1297 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1298 if (!result) {
1299 return NULL;
1300 }
1301 Py_DECREF(result);
1302
1303 Py_RETURN_FALSE;
1304}
1305
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001306static char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001307PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001308
1309static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001310 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1311 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001312 {NULL}
1313};
1314
1315static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001316 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001317 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001318 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001319 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001320 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001321 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001322 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001323 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001324 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001325 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001326 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001327 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001328 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001329 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001330 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1331 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001332 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001333 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001334 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001335 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001336 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001337 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001338 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001339 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001340 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001341 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001342 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001343 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001344 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1345 PyDoc_STR("For context manager. Non-standard.")},
1346 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1347 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001348 {NULL, NULL}
1349};
1350
1351static struct PyMemberDef connection_members[] =
1352{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001353 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1354 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1355 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1356 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1357 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1358 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1359 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1360 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1361 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1362 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001363 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1364 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001365 {NULL}
1366};
1367
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001368PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001369 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001370 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001371 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001372 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001373 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001374 0, /* tp_print */
1375 0, /* tp_getattr */
1376 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001377 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001378 0, /* tp_repr */
1379 0, /* tp_as_number */
1380 0, /* tp_as_sequence */
1381 0, /* tp_as_mapping */
1382 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001383 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001384 0, /* tp_str */
1385 0, /* tp_getattro */
1386 0, /* tp_setattro */
1387 0, /* tp_as_buffer */
1388 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1389 connection_doc, /* tp_doc */
1390 0, /* tp_traverse */
1391 0, /* tp_clear */
1392 0, /* tp_richcompare */
1393 0, /* tp_weaklistoffset */
1394 0, /* tp_iter */
1395 0, /* tp_iternext */
1396 connection_methods, /* tp_methods */
1397 connection_members, /* tp_members */
1398 connection_getset, /* tp_getset */
1399 0, /* tp_base */
1400 0, /* tp_dict */
1401 0, /* tp_descr_get */
1402 0, /* tp_descr_set */
1403 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001404 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001405 0, /* tp_alloc */
1406 0, /* tp_new */
1407 0 /* tp_free */
1408};
1409
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001410extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001411{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001412 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1413 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001414}