blob: 11fb4ac864828acfb9001303553af97498ebdf3e [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) {
Victor Stinner8a685f72010-03-21 20:05:51 +0000944 Py_DECREF(begin_statement);
Georg Brandl3dbca812008-07-23 16:10:53 +0000945 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
Victor Stinner09e46972010-03-21 20:29:20 +0000982 statement->db = NULL;
983 statement->st = NULL;
984 statement->sql = NULL;
985 statement->in_use = 0;
986 statement->in_weakreflist = NULL;
987
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000988 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000989
990 if (rc != SQLITE_OK) {
991 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000992 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000993 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000994 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000995 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000996 (void)pysqlite_statement_reset(statement);
997 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000998 }
999
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001000 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001001 } else {
1002 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1003 if (!weakref) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001004 Py_CLEAR(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001005 goto error;
1006 }
1007
1008 if (PyList_Append(self->statements, weakref) != 0) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001009 Py_CLEAR(weakref);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001010 goto error;
1011 }
1012
1013 Py_DECREF(weakref);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001014 }
1015
Thomas Wouters477c8d52006-05-27 19:21:47 +00001016error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001017 return (PyObject*)statement;
1018}
1019
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001020PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001021{
1022 PyObject* cursor = 0;
1023 PyObject* result = 0;
1024 PyObject* method = 0;
1025
1026 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1027 if (!cursor) {
1028 goto error;
1029 }
1030
1031 method = PyObject_GetAttrString(cursor, "execute");
1032 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001033 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001034 goto error;
1035 }
1036
1037 result = PyObject_CallObject(method, args);
1038 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001039 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001040 }
1041
1042error:
1043 Py_XDECREF(result);
1044 Py_XDECREF(method);
1045
1046 return cursor;
1047}
1048
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001049PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001050{
1051 PyObject* cursor = 0;
1052 PyObject* result = 0;
1053 PyObject* method = 0;
1054
1055 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1056 if (!cursor) {
1057 goto error;
1058 }
1059
1060 method = PyObject_GetAttrString(cursor, "executemany");
1061 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001062 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001063 goto error;
1064 }
1065
1066 result = PyObject_CallObject(method, args);
1067 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001068 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001069 }
1070
1071error:
1072 Py_XDECREF(result);
1073 Py_XDECREF(method);
1074
1075 return cursor;
1076}
1077
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001078PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001079{
1080 PyObject* cursor = 0;
1081 PyObject* result = 0;
1082 PyObject* method = 0;
1083
1084 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1085 if (!cursor) {
1086 goto error;
1087 }
1088
1089 method = PyObject_GetAttrString(cursor, "executescript");
1090 if (!method) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001091 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001092 goto error;
1093 }
1094
1095 result = PyObject_CallObject(method, args);
1096 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001097 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001098 }
1099
1100error:
1101 Py_XDECREF(result);
1102 Py_XDECREF(method);
1103
1104 return cursor;
1105}
1106
1107/* ------------------------- COLLATION CODE ------------------------ */
1108
1109static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001110pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001111 void* context,
1112 int text1_length, const void* text1_data,
1113 int text2_length, const void* text2_data)
1114{
1115 PyObject* callback = (PyObject*)context;
1116 PyObject* string1 = 0;
1117 PyObject* string2 = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001118#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001119 PyGILState_STATE gilstate;
Georg Brandldfd73442009-04-05 11:47:34 +00001120#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001121 PyObject* retval = NULL;
1122 int result = 0;
Georg Brandldfd73442009-04-05 11:47:34 +00001123#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001124 gilstate = PyGILState_Ensure();
Georg Brandldfd73442009-04-05 11:47:34 +00001125#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001126
1127 if (PyErr_Occurred()) {
1128 goto finally;
1129 }
1130
Guido van Rossum98297ee2007-11-06 21:34:58 +00001131 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1132 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001133
1134 if (!string1 || !string2) {
1135 goto finally; /* failed to allocate strings */
1136 }
1137
1138 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1139
1140 if (!retval) {
1141 /* execution failed */
1142 goto finally;
1143 }
1144
Christian Heimes217cfd12007-12-02 14:31:20 +00001145 result = PyLong_AsLong(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001146 if (PyErr_Occurred()) {
1147 result = 0;
1148 }
1149
1150finally:
1151 Py_XDECREF(string1);
1152 Py_XDECREF(string2);
1153 Py_XDECREF(retval);
Georg Brandldfd73442009-04-05 11:47:34 +00001154#ifdef WITH_THREAD
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001155 PyGILState_Release(gilstate);
Georg Brandldfd73442009-04-05 11:47:34 +00001156#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001157 return result;
1158}
1159
1160static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001161pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001162{
1163 PyObject* retval = NULL;
1164
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001165 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001166 goto finally;
1167 }
1168
1169 sqlite3_interrupt(self->db);
1170
1171 Py_INCREF(Py_None);
1172 retval = Py_None;
1173
1174finally:
1175 return retval;
1176}
1177
Christian Heimesbbe741d2008-03-28 10:53:29 +00001178/* Function author: Paul Kippes <kippesp@gmail.com>
1179 * Class method of Connection to call the Python function _iterdump
1180 * of the sqlite3 module.
1181 */
1182static PyObject *
1183pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1184{
1185 PyObject* retval = NULL;
1186 PyObject* module = NULL;
1187 PyObject* module_dict;
1188 PyObject* pyfn_iterdump;
1189
1190 if (!pysqlite_check_connection(self)) {
1191 goto finally;
1192 }
1193
1194 module = PyImport_ImportModule(MODULE_NAME ".dump");
1195 if (!module) {
1196 goto finally;
1197 }
1198
1199 module_dict = PyModule_GetDict(module);
1200 if (!module_dict) {
1201 goto finally;
1202 }
1203
1204 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1205 if (!pyfn_iterdump) {
1206 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1207 goto finally;
1208 }
1209
1210 args = PyTuple_New(1);
1211 if (!args) {
1212 goto finally;
1213 }
1214 Py_INCREF(self);
1215 PyTuple_SetItem(args, 0, (PyObject*)self);
1216 retval = PyObject_CallObject(pyfn_iterdump, args);
1217
1218finally:
1219 Py_XDECREF(args);
1220 Py_XDECREF(module);
1221 return retval;
1222}
1223
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001224static PyObject *
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001225pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001226{
1227 PyObject* callable;
1228 PyObject* uppercase_name = 0;
1229 PyObject* name;
1230 PyObject* retval;
Victor Stinner9b057002010-04-22 11:24:50 +00001231 Py_UNICODE* chk;
1232 Py_ssize_t i, len;
1233 char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001234 int rc;
1235
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001236 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001237 goto finally;
1238 }
1239
Gerhard Häring6d214562007-08-10 18:15:11 +00001240 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyUnicode_Type, &name, &callable)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001241 goto finally;
1242 }
1243
1244 uppercase_name = PyObject_CallMethod(name, "upper", "");
1245 if (!uppercase_name) {
1246 goto finally;
1247 }
1248
Victor Stinner9b057002010-04-22 11:24:50 +00001249 len = PyUnicode_GET_SIZE(uppercase_name);
1250 chk = PyUnicode_AS_UNICODE(uppercase_name);
1251 for (i=0; i<len; i++, chk++) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001252 if ((*chk >= '0' && *chk <= '9')
1253 || (*chk >= 'A' && *chk <= 'Z')
1254 || (*chk == '_'))
1255 {
Victor Stinner9b057002010-04-22 11:24:50 +00001256 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001257 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001258 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001259 goto finally;
1260 }
1261 }
1262
Victor Stinner9b057002010-04-22 11:24:50 +00001263 uppercase_name_str = _PyUnicode_AsString(uppercase_name);
1264 if (!uppercase_name_str)
1265 goto finally;
1266
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001267 if (callable != Py_None && !PyCallable_Check(callable)) {
1268 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1269 goto finally;
1270 }
1271
1272 if (callable != Py_None) {
1273 PyDict_SetItem(self->collations, uppercase_name, callable);
1274 } else {
1275 PyDict_DelItem(self->collations, uppercase_name);
1276 }
1277
1278 rc = sqlite3_create_collation(self->db,
Victor Stinner9b057002010-04-22 11:24:50 +00001279 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001280 SQLITE_UTF8,
1281 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001282 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001283 if (rc != SQLITE_OK) {
1284 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001285 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001286 goto finally;
1287 }
1288
1289finally:
1290 Py_XDECREF(uppercase_name);
1291
1292 if (PyErr_Occurred()) {
1293 retval = NULL;
1294 } else {
1295 Py_INCREF(Py_None);
1296 retval = Py_None;
1297 }
1298
1299 return retval;
1300}
1301
Christian Heimesbbe741d2008-03-28 10:53:29 +00001302/* Called when the connection is used as a context manager. Returns itself as a
1303 * convenience to the caller. */
1304static PyObject *
1305pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1306{
1307 Py_INCREF(self);
1308 return (PyObject*)self;
1309}
1310
1311/** Called when the connection is used as a context manager. If there was any
1312 * exception, a rollback takes place; otherwise we commit. */
1313static PyObject *
1314pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1315{
1316 PyObject* exc_type, *exc_value, *exc_tb;
1317 char* method_name;
1318 PyObject* result;
1319
1320 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1321 return NULL;
1322 }
1323
1324 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1325 method_name = "commit";
1326 } else {
1327 method_name = "rollback";
1328 }
1329
1330 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1331 if (!result) {
1332 return NULL;
1333 }
1334 Py_DECREF(result);
1335
1336 Py_RETURN_FALSE;
1337}
1338
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001339static char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001340PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001341
1342static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001343 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1344 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001345 {NULL}
1346};
1347
1348static PyMethodDef connection_methods[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001349 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001350 PyDoc_STR("Return a cursor for the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001351 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001352 PyDoc_STR("Closes the connection.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001353 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001354 PyDoc_STR("Commit the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001355 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001356 PyDoc_STR("Roll back the current transaction.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001357 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001358 PyDoc_STR("Creates a new function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001359 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001360 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001361 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001362 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001363 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1364 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001365 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001366 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001367 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001368 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001369 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001370 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001371 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Thomas Wouters477c8d52006-05-27 19:21:47 +00001372 PyDoc_STR("Creates a collation function. Non-standard.")},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001373 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001374 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001375 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
Benjamin Petersond7b03282008-09-13 15:58:53 +00001376 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
Christian Heimesbbe741d2008-03-28 10:53:29 +00001377 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1378 PyDoc_STR("For context manager. Non-standard.")},
1379 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1380 PyDoc_STR("For context manager. Non-standard.")},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001381 {NULL, NULL}
1382};
1383
1384static struct PyMemberDef connection_members[] =
1385{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001386 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1387 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1388 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1389 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1390 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1391 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1392 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1393 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1394 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1395 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001396 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1397 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001398 {NULL}
1399};
1400
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001401PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +00001402 PyVarObject_HEAD_INIT(NULL, 0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001403 MODULE_NAME ".Connection", /* tp_name */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001404 sizeof(pysqlite_Connection), /* tp_basicsize */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001405 0, /* tp_itemsize */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001406 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001407 0, /* tp_print */
1408 0, /* tp_getattr */
1409 0, /* tp_setattr */
Mark Dickinsone94c6792009-02-02 20:36:42 +00001410 0, /* tp_reserved */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001411 0, /* tp_repr */
1412 0, /* tp_as_number */
1413 0, /* tp_as_sequence */
1414 0, /* tp_as_mapping */
1415 0, /* tp_hash */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001416 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001417 0, /* tp_str */
1418 0, /* tp_getattro */
1419 0, /* tp_setattro */
1420 0, /* tp_as_buffer */
1421 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1422 connection_doc, /* tp_doc */
1423 0, /* tp_traverse */
1424 0, /* tp_clear */
1425 0, /* tp_richcompare */
1426 0, /* tp_weaklistoffset */
1427 0, /* tp_iter */
1428 0, /* tp_iternext */
1429 connection_methods, /* tp_methods */
1430 connection_members, /* tp_members */
1431 connection_getset, /* tp_getset */
1432 0, /* tp_base */
1433 0, /* tp_dict */
1434 0, /* tp_descr_get */
1435 0, /* tp_descr_set */
1436 0, /* tp_dictoffset */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001437 (initproc)pysqlite_connection_init, /* tp_init */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001438 0, /* tp_alloc */
1439 0, /* tp_new */
1440 0 /* tp_free */
1441};
1442
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001443extern int pysqlite_connection_setup_types(void)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001444{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001445 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1446 return PyType_Ready(&pysqlite_ConnectionType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001447}