blob: 2071c332127ebefa1a738ce2c29f1688fc32aee5 [file] [log] [blame]
Anthony Baxterc51ee692006-04-01 00:57:31 +00001/* connection.c - the connection type
2 *
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00003 * Copyright (C) 2004-2007 Gerhard Häring <gh@ghaering.de>
Anthony Baxterc51ee692006-04-01 00:57:31 +00004 *
5 * This file is part of pysqlite.
Anthony Baxter72289a62006-04-04 06:29:05 +00006 *
Anthony Baxterc51ee692006-04-01 00:57:31 +00007 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
10 *
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not
16 * claim that you wrote the original software. If you use this software
17 * in a product, an acknowledgment in the product documentation would be
18 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#include "cache.h"
25#include "module.h"
26#include "connection.h"
27#include "statement.h"
28#include "cursor.h"
29#include "prepare_protocol.h"
30#include "util.h"
Anthony Baxter72289a62006-04-04 06:29:05 +000031#include "sqlitecompat.h"
32
Anthony Baxterc51ee692006-04-01 00:57:31 +000033#include "pythread.h"
34
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000035#define ACTION_FINALIZE 1
36#define ACTION_RESET 2
37
Gerhard Häring0741a602007-01-14 01:43:50 +000038static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level);
Anthony Baxterc51ee692006-04-01 00:57:31 +000039
Gerhard Häringb2e88162006-06-14 22:28:37 +000040
41void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
42{
43 /* in older SQLite versions, calling sqlite3_result_error in callbacks
44 * triggers a bug in SQLite that leads either to irritating results or
45 * segfaults, depending on the SQLite version */
46#if SQLITE_VERSION_NUMBER >= 3003003
47 sqlite3_result_error(ctx, errmsg, len);
Neal Norwitzfe7d0c32006-06-15 04:54:29 +000048#else
Gerhard Häring0741a602007-01-14 01:43:50 +000049 PyErr_SetString(pysqlite_OperationalError, errmsg);
Gerhard Häringb2e88162006-06-14 22:28:37 +000050#endif
51}
52
Gerhard Häring0741a602007-01-14 01:43:50 +000053int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +000054{
55 static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL};
56
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000057 PyObject* database;
Anthony Baxterc51ee692006-04-01 00:57:31 +000058 int detect_types = 0;
59 PyObject* isolation_level = NULL;
60 PyObject* factory = NULL;
61 int check_same_thread = 1;
62 int cached_statements = 100;
63 double timeout = 5.0;
64 int rc;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000065 PyObject* class_attr = NULL;
66 PyObject* class_attr_str = NULL;
67 int is_apsw_connection = 0;
68 PyObject* database_utf8;
Anthony Baxterc51ee692006-04-01 00:57:31 +000069
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000070 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOi", kwlist,
Anthony Baxterc51ee692006-04-01 00:57:31 +000071 &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements))
72 {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000073 return -1;
Anthony Baxterc51ee692006-04-01 00:57:31 +000074 }
75
76 self->begin_statement = NULL;
77
78 self->statement_cache = NULL;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +000079 self->statements = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +000080
81 Py_INCREF(Py_None);
82 self->row_factory = Py_None;
83
84 Py_INCREF(&PyUnicode_Type);
85 self->text_factory = (PyObject*)&PyUnicode_Type;
86
Gregory P. Smithdd96db62008-06-09 04:58:54 +000087 if (PyString_Check(database) || PyUnicode_Check(database)) {
88 if (PyString_Check(database)) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000089 database_utf8 = database;
90 Py_INCREF(database_utf8);
91 } else {
92 database_utf8 = PyUnicode_AsUTF8String(database);
93 if (!database_utf8) {
94 return -1;
95 }
96 }
Anthony Baxterc51ee692006-04-01 00:57:31 +000097
Gerhard Häring1cc60ed2008-02-29 22:08:41 +000098 Py_BEGIN_ALLOW_THREADS
Gregory P. Smithdd96db62008-06-09 04:58:54 +000099 rc = sqlite3_open(PyString_AsString(database_utf8), &self->db);
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000100 Py_END_ALLOW_THREADS
101
102 Py_DECREF(database_utf8);
103
104 if (rc != SQLITE_OK) {
105 _pysqlite_seterror(self->db, NULL);
106 return -1;
107 }
108 } else {
109 /* Create a pysqlite connection from a APSW connection */
110 class_attr = PyObject_GetAttrString(database, "__class__");
111 if (class_attr) {
112 class_attr_str = PyObject_Str(class_attr);
113 if (class_attr_str) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000114 if (strcmp(PyString_AsString(class_attr_str), "<type 'apsw.Connection'>") == 0) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000115 /* In the APSW Connection object, the first entry after
116 * PyObject_HEAD is the sqlite3* we want to get hold of.
117 * Luckily, this is the same layout as we have in our
118 * pysqlite_Connection */
119 self->db = ((pysqlite_Connection*)database)->db;
120
121 Py_INCREF(database);
122 self->apsw_connection = database;
123 is_apsw_connection = 1;
124 }
125 }
126 }
127 Py_XDECREF(class_attr_str);
128 Py_XDECREF(class_attr);
129
130 if (!is_apsw_connection) {
131 PyErr_SetString(PyExc_ValueError, "database parameter must be string or APSW Connection object");
132 return -1;
133 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000134 }
135
136 if (!isolation_level) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000137 isolation_level = PyString_FromString("");
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000138 if (!isolation_level) {
139 return -1;
140 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000141 } else {
142 Py_INCREF(isolation_level);
143 }
144 self->isolation_level = NULL;
Gerhard Häring0741a602007-01-14 01:43:50 +0000145 pysqlite_connection_set_isolation_level(self, isolation_level);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000146 Py_DECREF(isolation_level);
147
Gerhard Häring0741a602007-01-14 01:43:50 +0000148 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000149 if (PyErr_Occurred()) {
150 return -1;
151 }
152
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000153 self->statements = PyList_New(0);
154 if (!self->statements) {
155 return -1;
156 }
157 self->created_statements = 0;
158
Anthony Baxterc51ee692006-04-01 00:57:31 +0000159 /* By default, the Cache class INCREFs the factory in its initializer, and
160 * decrefs it in its deallocator method. Since this would create a circular
161 * reference here, we're breaking it by decrementing self, and telling the
162 * cache class to not decref the factory (self) in its deallocator.
163 */
164 self->statement_cache->decref_factory = 0;
165 Py_DECREF(self);
166
167 self->inTransaction = 0;
168 self->detect_types = detect_types;
169 self->timeout = timeout;
170 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
171
172 self->thread_ident = PyThread_get_thread_ident();
173 self->check_same_thread = check_same_thread;
174
175 self->function_pinboard = PyDict_New();
Anthony Baxter72289a62006-04-04 06:29:05 +0000176 if (!self->function_pinboard) {
177 return -1;
178 }
179
180 self->collations = PyDict_New();
181 if (!self->collations) {
182 return -1;
183 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000184
Gerhard Häring0741a602007-01-14 01:43:50 +0000185 self->Warning = pysqlite_Warning;
186 self->Error = pysqlite_Error;
187 self->InterfaceError = pysqlite_InterfaceError;
188 self->DatabaseError = pysqlite_DatabaseError;
189 self->DataError = pysqlite_DataError;
190 self->OperationalError = pysqlite_OperationalError;
191 self->IntegrityError = pysqlite_IntegrityError;
192 self->InternalError = pysqlite_InternalError;
193 self->ProgrammingError = pysqlite_ProgrammingError;
194 self->NotSupportedError = pysqlite_NotSupportedError;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000195
196 return 0;
197}
198
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000199/* Empty the entire statement cache of this connection */
Gerhard Häring0741a602007-01-14 01:43:50 +0000200void pysqlite_flush_statement_cache(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000201{
Gerhard Häring0741a602007-01-14 01:43:50 +0000202 pysqlite_Node* node;
203 pysqlite_Statement* statement;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000204
205 node = self->statement_cache->first;
206
207 while (node) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000208 statement = (pysqlite_Statement*)(node->data);
209 (void)pysqlite_statement_finalize(statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000210 node = node->next;
211 }
212
213 Py_DECREF(self->statement_cache);
Gerhard Häring0741a602007-01-14 01:43:50 +0000214 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "O", self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000215 Py_DECREF(self);
216 self->statement_cache->decref_factory = 0;
217}
218
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000219/* action in (ACTION_RESET, ACTION_FINALIZE) */
220void pysqlite_do_all_statements(pysqlite_Connection* self, int action)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000221{
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000222 int i;
223 PyObject* weakref;
224 PyObject* statement;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000225
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000226 for (i = 0; i < PyList_Size(self->statements); i++) {
227 weakref = PyList_GetItem(self->statements, i);
228 statement = PyWeakref_GetObject(weakref);
229 if (statement != Py_None) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000230 if (action == ACTION_RESET) {
231 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
232 } else {
233 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
234 }
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000235 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000236 }
237}
238
Gerhard Häring0741a602007-01-14 01:43:50 +0000239void pysqlite_connection_dealloc(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000240{
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000241 PyObject* ret = NULL;
242
Anthony Baxterc51ee692006-04-01 00:57:31 +0000243 Py_XDECREF(self->statement_cache);
244
245 /* Clean up if user has not called .close() explicitly. */
246 if (self->db) {
247 Py_BEGIN_ALLOW_THREADS
248 sqlite3_close(self->db);
249 Py_END_ALLOW_THREADS
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000250 } else if (self->apsw_connection) {
251 ret = PyObject_CallMethod(self->apsw_connection, "close", "");
252 Py_XDECREF(ret);
253 Py_XDECREF(self->apsw_connection);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000254 }
255
256 if (self->begin_statement) {
257 PyMem_Free(self->begin_statement);
258 }
259 Py_XDECREF(self->isolation_level);
260 Py_XDECREF(self->function_pinboard);
261 Py_XDECREF(self->row_factory);
262 Py_XDECREF(self->text_factory);
Anthony Baxter72289a62006-04-04 06:29:05 +0000263 Py_XDECREF(self->collations);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000264 Py_XDECREF(self->statements);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000265
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000266 self->ob_type->tp_free((PyObject*)self);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000267}
268
Gerhard Häring0741a602007-01-14 01:43:50 +0000269PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000270{
271 static char *kwlist[] = {"factory", NULL, NULL};
272 PyObject* factory = NULL;
273 PyObject* cursor;
274
275
276 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
277 &factory)) {
278 return NULL;
279 }
280
Gerhard Häring0741a602007-01-14 01:43:50 +0000281 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000282 return NULL;
283 }
284
285 if (factory == NULL) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000286 factory = (PyObject*)&pysqlite_CursorType;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000287 }
288
289 cursor = PyObject_CallFunction(factory, "O", self);
290
291 if (cursor && self->row_factory != Py_None) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000292 Py_XDECREF(((pysqlite_Cursor*)cursor)->row_factory);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000293 Py_INCREF(self->row_factory);
Gerhard Häring0741a602007-01-14 01:43:50 +0000294 ((pysqlite_Cursor*)cursor)->row_factory = self->row_factory;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000295 }
296
297 return cursor;
298}
299
Gerhard Häring0741a602007-01-14 01:43:50 +0000300PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000301{
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000302 PyObject* ret;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000303 int rc;
304
Gerhard Häring0741a602007-01-14 01:43:50 +0000305 if (!pysqlite_check_thread(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000306 return NULL;
307 }
308
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000309 pysqlite_do_all_statements(self, ACTION_FINALIZE);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000310
311 if (self->db) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000312 if (self->apsw_connection) {
313 ret = PyObject_CallMethod(self->apsw_connection, "close", "");
314 Py_XDECREF(ret);
315 Py_XDECREF(self->apsw_connection);
316 self->apsw_connection = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000317 self->db = NULL;
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000318 } else {
319 Py_BEGIN_ALLOW_THREADS
320 rc = sqlite3_close(self->db);
321 Py_END_ALLOW_THREADS
322
323 if (rc != SQLITE_OK) {
324 _pysqlite_seterror(self->db, NULL);
325 return NULL;
326 } else {
327 self->db = NULL;
328 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000329 }
330 }
331
332 Py_INCREF(Py_None);
333 return Py_None;
334}
335
336/*
337 * Checks if a connection object is usable (i. e. not closed).
338 *
339 * 0 => error; 1 => ok
340 */
Gerhard Häring0741a602007-01-14 01:43:50 +0000341int pysqlite_check_connection(pysqlite_Connection* con)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000342{
343 if (!con->db) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000344 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Anthony Baxterc51ee692006-04-01 00:57:31 +0000345 return 0;
346 } else {
347 return 1;
348 }
349}
350
Gerhard Häring0741a602007-01-14 01:43:50 +0000351PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000352{
353 int rc;
354 const char* tail;
355 sqlite3_stmt* statement;
356
357 Py_BEGIN_ALLOW_THREADS
358 rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail);
359 Py_END_ALLOW_THREADS
360
361 if (rc != SQLITE_OK) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000362 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000363 goto error;
364 }
365
366 rc = _sqlite_step_with_busyhandler(statement, self);
367 if (rc == SQLITE_DONE) {
368 self->inTransaction = 1;
369 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000370 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000371 }
372
373 Py_BEGIN_ALLOW_THREADS
374 rc = sqlite3_finalize(statement);
375 Py_END_ALLOW_THREADS
376
377 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000378 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000379 }
380
381error:
382 if (PyErr_Occurred()) {
383 return NULL;
384 } else {
385 Py_INCREF(Py_None);
386 return Py_None;
387 }
388}
389
Gerhard Häring0741a602007-01-14 01:43:50 +0000390PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000391{
392 int rc;
393 const char* tail;
394 sqlite3_stmt* statement;
395
Gerhard Häring0741a602007-01-14 01:43:50 +0000396 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000397 return NULL;
398 }
399
400 if (self->inTransaction) {
401 Py_BEGIN_ALLOW_THREADS
402 rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail);
403 Py_END_ALLOW_THREADS
404 if (rc != SQLITE_OK) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000405 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000406 goto error;
407 }
408
409 rc = _sqlite_step_with_busyhandler(statement, self);
410 if (rc == SQLITE_DONE) {
411 self->inTransaction = 0;
412 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000413 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000414 }
415
416 Py_BEGIN_ALLOW_THREADS
417 rc = sqlite3_finalize(statement);
418 Py_END_ALLOW_THREADS
419 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000420 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000421 }
422
423 }
424
425error:
426 if (PyErr_Occurred()) {
427 return NULL;
428 } else {
429 Py_INCREF(Py_None);
430 return Py_None;
431 }
432}
433
Gerhard Häring0741a602007-01-14 01:43:50 +0000434PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000435{
436 int rc;
437 const char* tail;
438 sqlite3_stmt* statement;
439
Gerhard Häring0741a602007-01-14 01:43:50 +0000440 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000441 return NULL;
442 }
443
444 if (self->inTransaction) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000445 pysqlite_do_all_statements(self, ACTION_RESET);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000446
447 Py_BEGIN_ALLOW_THREADS
448 rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail);
449 Py_END_ALLOW_THREADS
450 if (rc != SQLITE_OK) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000451 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000452 goto error;
453 }
454
455 rc = _sqlite_step_with_busyhandler(statement, self);
456 if (rc == SQLITE_DONE) {
457 self->inTransaction = 0;
458 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000459 _pysqlite_seterror(self->db, statement);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000460 }
461
462 Py_BEGIN_ALLOW_THREADS
463 rc = sqlite3_finalize(statement);
464 Py_END_ALLOW_THREADS
465 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000466 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000467 }
468
469 }
470
471error:
472 if (PyErr_Occurred()) {
473 return NULL;
474 } else {
475 Py_INCREF(Py_None);
476 return Py_None;
477 }
478}
479
Gerhard Häring0741a602007-01-14 01:43:50 +0000480void _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000481{
482 long longval;
483 const char* buffer;
Neal Norwitz95f0e4c2006-04-01 09:08:06 +0000484 Py_ssize_t buflen;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000485 PyObject* stringval;
486
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000487 if ((!py_val) || PyErr_Occurred()) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000488 sqlite3_result_null(context);
489 } else if (py_val == Py_None) {
490 sqlite3_result_null(context);
491 } else if (PyInt_Check(py_val)) {
492 longval = PyInt_AsLong(py_val);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000493 sqlite3_result_int64(context, (PY_LONG_LONG)longval);
494 } else if (PyFloat_Check(py_val)) {
495 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
496 } else if (PyBuffer_Check(py_val)) {
497 if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) {
498 PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer");
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000499 } else {
500 sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000501 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000502 } else if (PyString_Check(py_val)) {
503 sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000504 } else if (PyUnicode_Check(py_val)) {
505 stringval = PyUnicode_AsUTF8String(py_val);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000506 if (stringval) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000507 sqlite3_result_text(context, PyString_AsString(stringval), -1, SQLITE_TRANSIENT);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000508 Py_DECREF(stringval);
509 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000510 } else {
511 /* TODO: raise error */
512 }
513}
514
Gerhard Häring0741a602007-01-14 01:43:50 +0000515PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000516{
517 PyObject* args;
518 int i;
519 sqlite3_value* cur_value;
520 PyObject* cur_py_value;
521 const char* val_str;
522 PY_LONG_LONG val_int;
Neal Norwitz95f0e4c2006-04-01 09:08:06 +0000523 Py_ssize_t buflen;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000524 void* raw_buffer;
525
526 args = PyTuple_New(argc);
Anthony Baxter72289a62006-04-04 06:29:05 +0000527 if (!args) {
528 return NULL;
529 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000530
531 for (i = 0; i < argc; i++) {
532 cur_value = argv[i];
533 switch (sqlite3_value_type(argv[i])) {
534 case SQLITE_INTEGER:
535 val_int = sqlite3_value_int64(cur_value);
536 cur_py_value = PyInt_FromLong((long)val_int);
537 break;
538 case SQLITE_FLOAT:
539 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
540 break;
541 case SQLITE_TEXT:
542 val_str = (const char*)sqlite3_value_text(cur_value);
543 cur_py_value = PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL);
544 /* TODO: have a way to show errors here */
545 if (!cur_py_value) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000546 PyErr_Clear();
Anthony Baxterc51ee692006-04-01 00:57:31 +0000547 Py_INCREF(Py_None);
548 cur_py_value = Py_None;
549 }
550 break;
551 case SQLITE_BLOB:
552 buflen = sqlite3_value_bytes(cur_value);
553 cur_py_value = PyBuffer_New(buflen);
554 if (!cur_py_value) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000555 break;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000556 }
557 if (PyObject_AsWriteBuffer(cur_py_value, &raw_buffer, &buflen)) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000558 Py_DECREF(cur_py_value);
559 cur_py_value = NULL;
560 break;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000561 }
562 memcpy(raw_buffer, sqlite3_value_blob(cur_value), buflen);
563 break;
564 case SQLITE_NULL:
565 default:
566 Py_INCREF(Py_None);
567 cur_py_value = Py_None;
568 }
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000569
570 if (!cur_py_value) {
571 Py_DECREF(args);
572 return NULL;
573 }
574
Anthony Baxterc51ee692006-04-01 00:57:31 +0000575 PyTuple_SetItem(args, i, cur_py_value);
576
577 }
578
579 return args;
580}
581
Gerhard Häring0741a602007-01-14 01:43:50 +0000582void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000583{
584 PyObject* args;
585 PyObject* py_func;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000586 PyObject* py_retval = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000587
588 PyGILState_STATE threadstate;
589
590 threadstate = PyGILState_Ensure();
591
592 py_func = (PyObject*)sqlite3_user_data(context);
593
Gerhard Häring0741a602007-01-14 01:43:50 +0000594 args = _pysqlite_build_py_params(context, argc, argv);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000595 if (args) {
596 py_retval = PyObject_CallObject(py_func, args);
597 Py_DECREF(args);
598 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000599
Gerhard Häring1541ef02006-06-13 22:24:47 +0000600 if (py_retval) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000601 _pysqlite_set_result(context, py_retval);
Gerhard Häring1541ef02006-06-13 22:24:47 +0000602 Py_DECREF(py_retval);
603 } else {
604 if (_enable_callback_tracebacks) {
605 PyErr_Print();
606 } else {
607 PyErr_Clear();
608 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000609 _sqlite3_result_error(context, "user-defined function raised exception", -1);
Gerhard Häring1541ef02006-06-13 22:24:47 +0000610 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000611
612 PyGILState_Release(threadstate);
613}
614
Gerhard Häring0741a602007-01-14 01:43:50 +0000615static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000616{
617 PyObject* args;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000618 PyObject* function_result = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000619 PyObject* aggregate_class;
620 PyObject** aggregate_instance;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000621 PyObject* stepmethod = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000622
623 PyGILState_STATE threadstate;
624
625 threadstate = PyGILState_Ensure();
626
627 aggregate_class = (PyObject*)sqlite3_user_data(context);
628
629 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
630
631 if (*aggregate_instance == 0) {
632 *aggregate_instance = PyObject_CallFunction(aggregate_class, "");
633
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000634 if (PyErr_Occurred()) {
Anthony Baxterc51ee692006-04-01 00:57:31 +0000635 *aggregate_instance = 0;
Gerhard Häring1541ef02006-06-13 22:24:47 +0000636 if (_enable_callback_tracebacks) {
637 PyErr_Print();
638 } else {
639 PyErr_Clear();
640 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000641 _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000642 goto error;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000643 }
644 }
645
646 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000647 if (!stepmethod) {
648 goto error;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000649 }
650
Gerhard Häring0741a602007-01-14 01:43:50 +0000651 args = _pysqlite_build_py_params(context, argc, params);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000652 if (!args) {
653 goto error;
654 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000655
656 function_result = PyObject_CallObject(stepmethod, args);
657 Py_DECREF(args);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000658
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000659 if (!function_result) {
Gerhard Häring1541ef02006-06-13 22:24:47 +0000660 if (_enable_callback_tracebacks) {
661 PyErr_Print();
662 } else {
663 PyErr_Clear();
664 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000665 _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000666 }
667
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000668error:
669 Py_XDECREF(stepmethod);
670 Py_XDECREF(function_result);
671
Anthony Baxterc51ee692006-04-01 00:57:31 +0000672 PyGILState_Release(threadstate);
673}
674
Gerhard Häring0741a602007-01-14 01:43:50 +0000675void _pysqlite_final_callback(sqlite3_context* context)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000676{
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000677 PyObject* function_result = NULL;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000678 PyObject** aggregate_instance;
679 PyObject* aggregate_class;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000680
681 PyGILState_STATE threadstate;
682
683 threadstate = PyGILState_Ensure();
684
685 aggregate_class = (PyObject*)sqlite3_user_data(context);
686
687 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
688 if (!*aggregate_instance) {
689 /* this branch is executed if there was an exception in the aggregate's
690 * __init__ */
691
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000692 goto error;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000693 }
694
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000695 function_result = PyObject_CallMethod(*aggregate_instance, "finalize", "");
696 if (!function_result) {
Gerhard Häring1541ef02006-06-13 22:24:47 +0000697 if (_enable_callback_tracebacks) {
698 PyErr_Print();
699 } else {
700 PyErr_Clear();
701 }
Gerhard Häringb2e88162006-06-14 22:28:37 +0000702 _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Gerhard Häring1541ef02006-06-13 22:24:47 +0000703 } else {
Gerhard Häring0741a602007-01-14 01:43:50 +0000704 _pysqlite_set_result(context, function_result);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000705 }
706
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000707error:
Anthony Baxterc51ee692006-04-01 00:57:31 +0000708 Py_XDECREF(*aggregate_instance);
709 Py_XDECREF(function_result);
710
711 PyGILState_Release(threadstate);
712}
713
Gerhard Häring0741a602007-01-14 01:43:50 +0000714void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000715{
716 PyObject* new_list;
717 PyObject* weakref;
718 int i;
719
720 /* we only need to do this once in a while */
721 if (self->created_statements++ < 200) {
722 return;
723 }
724
725 self->created_statements = 0;
726
727 new_list = PyList_New(0);
728 if (!new_list) {
729 return;
730 }
731
732 for (i = 0; i < PyList_Size(self->statements); i++) {
733 weakref = PyList_GetItem(self->statements, i);
Gerhard Häringecd20102006-06-19 21:17:35 +0000734 if (PyWeakref_GetObject(weakref) != Py_None) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000735 if (PyList_Append(new_list, weakref) != 0) {
736 Py_DECREF(new_list);
737 return;
738 }
739 }
740 }
741
742 Py_DECREF(self->statements);
743 self->statements = new_list;
744}
Anthony Baxterc51ee692006-04-01 00:57:31 +0000745
Gerhard Häring0741a602007-01-14 01:43:50 +0000746PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000747{
748 static char *kwlist[] = {"name", "narg", "func", NULL, NULL};
749
750 PyObject* func;
751 char* name;
752 int narg;
753 int rc;
754
755 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist,
756 &name, &narg, &func))
757 {
758 return NULL;
759 }
760
Gerhard Häring0741a602007-01-14 01:43:50 +0000761 rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _pysqlite_func_callback, NULL, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000762
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000763 if (rc != SQLITE_OK) {
764 /* Workaround for SQLite bug: no error code or string is available here */
Gerhard Häring0741a602007-01-14 01:43:50 +0000765 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000766 return NULL;
767 } else {
768 PyDict_SetItem(self->function_pinboard, func, Py_None);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000769
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000770 Py_INCREF(Py_None);
771 return Py_None;
772 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000773}
774
Gerhard Häring0741a602007-01-14 01:43:50 +0000775PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000776{
777 PyObject* aggregate_class;
778
779 int n_arg;
780 char* name;
781 static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
782 int rc;
783
784 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
785 kwlist, &name, &n_arg, &aggregate_class)) {
786 return NULL;
787 }
788
Gerhard Häring0741a602007-01-14 01:43:50 +0000789 rc = sqlite3_create_function(self->db, name, n_arg, SQLITE_UTF8, (void*)aggregate_class, 0, &_pysqlite_step_callback, &_pysqlite_final_callback);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000790 if (rc != SQLITE_OK) {
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000791 /* Workaround for SQLite bug: no error code or string is available here */
Gerhard Häring0741a602007-01-14 01:43:50 +0000792 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Anthony Baxterc51ee692006-04-01 00:57:31 +0000793 return NULL;
794 } else {
795 PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None);
796
797 Py_INCREF(Py_None);
798 return Py_None;
799 }
800}
801
Gerhard Häring0741a602007-01-14 01:43:50 +0000802static int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source)
Gerhard Häring1541ef02006-06-13 22:24:47 +0000803{
804 PyObject *ret;
805 int rc;
806 PyGILState_STATE gilstate;
807
808 gilstate = PyGILState_Ensure();
809 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
810
811 if (!ret) {
812 if (_enable_callback_tracebacks) {
813 PyErr_Print();
814 } else {
815 PyErr_Clear();
816 }
817
818 rc = SQLITE_DENY;
819 } else {
820 if (PyInt_Check(ret)) {
821 rc = (int)PyInt_AsLong(ret);
822 } else {
823 rc = SQLITE_DENY;
824 }
825 Py_DECREF(ret);
826 }
827
828 PyGILState_Release(gilstate);
829 return rc;
830}
831
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000832static int _progress_handler(void* user_arg)
833{
834 int rc;
835 PyObject *ret;
836 PyGILState_STATE gilstate;
837
838 gilstate = PyGILState_Ensure();
839 ret = PyObject_CallFunction((PyObject*)user_arg, "");
840
841 if (!ret) {
842 if (_enable_callback_tracebacks) {
843 PyErr_Print();
844 } else {
845 PyErr_Clear();
846 }
847
848 /* abort query if error occured */
849 rc = 1;
850 } else {
851 rc = (int)PyObject_IsTrue(ret);
Neal Norwitzca752f32008-03-03 04:37:45 +0000852 Py_DECREF(ret);
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000853 }
854
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000855 PyGILState_Release(gilstate);
856 return rc;
857}
858
Gerhard Häring0741a602007-01-14 01:43:50 +0000859PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Gerhard Häring1541ef02006-06-13 22:24:47 +0000860{
861 PyObject* authorizer_cb;
862
863 static char *kwlist[] = { "authorizer_callback", NULL };
864 int rc;
865
866 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
867 kwlist, &authorizer_cb)) {
868 return NULL;
869 }
870
871 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
872
873 if (rc != SQLITE_OK) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000874 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
Gerhard Häring1541ef02006-06-13 22:24:47 +0000875 return NULL;
876 } else {
877 PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None);
878
879 Py_INCREF(Py_None);
880 return Py_None;
881 }
882}
883
Gerhard Häring1cc60ed2008-02-29 22:08:41 +0000884PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
885{
886 PyObject* progress_handler;
887 int n;
888
889 static char *kwlist[] = { "progress_handler", "n", NULL };
890
891 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
892 kwlist, &progress_handler, &n)) {
893 return NULL;
894 }
895
896 if (progress_handler == Py_None) {
897 /* None clears the progress handler previously set */
898 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
899 } else {
900 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
901 PyDict_SetItem(self->function_pinboard, progress_handler, Py_None);
902 }
903
904 Py_INCREF(Py_None);
905 return Py_None;
906}
907
Gerhard Häring0741a602007-01-14 01:43:50 +0000908int pysqlite_check_thread(pysqlite_Connection* self)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000909{
910 if (self->check_same_thread) {
911 if (PyThread_get_thread_ident() != self->thread_ident) {
Gerhard Häring0741a602007-01-14 01:43:50 +0000912 PyErr_Format(pysqlite_ProgrammingError,
Anthony Baxterc51ee692006-04-01 00:57:31 +0000913 "SQLite objects created in a thread can only be used in that same thread."
914 "The object was created in thread id %ld and this is thread id %ld",
915 self->thread_ident, PyThread_get_thread_ident());
916 return 0;
917 }
918
919 }
920
921 return 1;
922}
923
Gerhard Häring0741a602007-01-14 01:43:50 +0000924static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000925{
926 Py_INCREF(self->isolation_level);
927 return self->isolation_level;
928}
929
Gerhard Häring0741a602007-01-14 01:43:50 +0000930static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Anthony Baxter72289a62006-04-04 06:29:05 +0000931{
Gerhard Häring0741a602007-01-14 01:43:50 +0000932 if (!pysqlite_check_connection(self)) {
Anthony Baxter72289a62006-04-04 06:29:05 +0000933 return NULL;
934 } else {
935 return Py_BuildValue("i", sqlite3_total_changes(self->db));
936 }
937}
938
Gerhard Häring0741a602007-01-14 01:43:50 +0000939static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000940{
Anthony Baxterc51ee692006-04-01 00:57:31 +0000941 PyObject* res;
942 PyObject* begin_statement;
943
944 Py_XDECREF(self->isolation_level);
945
Neal Norwitz5b030652006-04-16 03:28:17 +0000946 if (self->begin_statement) {
947 PyMem_Free(self->begin_statement);
948 self->begin_statement = NULL;
949 }
950
Anthony Baxterc51ee692006-04-01 00:57:31 +0000951 if (isolation_level == Py_None) {
952 Py_INCREF(Py_None);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000953 self->isolation_level = Py_None;
954
Gerhard Häring0741a602007-01-14 01:43:50 +0000955 res = pysqlite_connection_commit(self, NULL);
Anthony Baxter72289a62006-04-04 06:29:05 +0000956 if (!res) {
957 return -1;
958 }
Anthony Baxterc51ee692006-04-01 00:57:31 +0000959 Py_DECREF(res);
960
961 self->inTransaction = 0;
962 } else {
963 Py_INCREF(isolation_level);
964 self->isolation_level = isolation_level;
965
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000966 begin_statement = PyString_FromString("BEGIN ");
Anthony Baxterc51ee692006-04-01 00:57:31 +0000967 if (!begin_statement) {
968 return -1;
969 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000970 PyString_Concat(&begin_statement, isolation_level);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000971 if (!begin_statement) {
972 return -1;
973 }
974
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000975 self->begin_statement = PyMem_Malloc(PyString_Size(begin_statement) + 2);
Anthony Baxterc51ee692006-04-01 00:57:31 +0000976 if (!self->begin_statement) {
977 return -1;
978 }
979
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000980 strcpy(self->begin_statement, PyString_AsString(begin_statement));
Anthony Baxterc51ee692006-04-01 00:57:31 +0000981 Py_DECREF(begin_statement);
982 }
983
984 return 0;
985}
986
Gerhard Häring0741a602007-01-14 01:43:50 +0000987PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +0000988{
989 PyObject* sql;
Gerhard Häring0741a602007-01-14 01:43:50 +0000990 pysqlite_Statement* statement;
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000991 PyObject* weakref;
Anthony Baxterc51ee692006-04-01 00:57:31 +0000992 int rc;
993
994 if (!PyArg_ParseTuple(args, "O", &sql)) {
995 return NULL;
996 }
997
Gerhard Häring0741a602007-01-14 01:43:50 +0000998 _pysqlite_drop_unused_statement_references(self);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +0000999
Gerhard Häring0741a602007-01-14 01:43:50 +00001000 statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001001 if (!statement) {
1002 return NULL;
1003 }
1004
Gerhard Häring0741a602007-01-14 01:43:50 +00001005 rc = pysqlite_statement_create(statement, self, sql);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001006
1007 if (rc != SQLITE_OK) {
1008 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Gerhard Häring0741a602007-01-14 01:43:50 +00001009 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001010 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Gerhard Häring0741a602007-01-14 01:43:50 +00001011 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string or unicode.");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001012 } else {
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001013 (void)pysqlite_statement_reset(statement);
1014 _pysqlite_seterror(self->db, NULL);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001015 }
1016
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001017 Py_CLEAR(statement);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001018 } else {
1019 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1020 if (!weakref) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001021 Py_CLEAR(statement);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001022 goto error;
1023 }
1024
1025 if (PyList_Append(self->statements, weakref) != 0) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001026 Py_CLEAR(weakref);
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001027 goto error;
1028 }
1029
1030 Py_DECREF(weakref);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001031 }
1032
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001033error:
Anthony Baxterc51ee692006-04-01 00:57:31 +00001034 return (PyObject*)statement;
1035}
1036
Gerhard Häring0741a602007-01-14 01:43:50 +00001037PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001038{
1039 PyObject* cursor = 0;
1040 PyObject* result = 0;
1041 PyObject* method = 0;
1042
1043 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1044 if (!cursor) {
1045 goto error;
1046 }
1047
1048 method = PyObject_GetAttrString(cursor, "execute");
1049 if (!method) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001050 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001051 goto error;
1052 }
1053
1054 result = PyObject_CallObject(method, args);
1055 if (!result) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001056 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001057 }
1058
1059error:
1060 Py_XDECREF(result);
1061 Py_XDECREF(method);
1062
1063 return cursor;
1064}
1065
Gerhard Häring0741a602007-01-14 01:43:50 +00001066PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001067{
1068 PyObject* cursor = 0;
1069 PyObject* result = 0;
1070 PyObject* method = 0;
1071
1072 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1073 if (!cursor) {
1074 goto error;
1075 }
1076
1077 method = PyObject_GetAttrString(cursor, "executemany");
1078 if (!method) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001079 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001080 goto error;
1081 }
1082
1083 result = PyObject_CallObject(method, args);
1084 if (!result) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001085 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001086 }
1087
1088error:
1089 Py_XDECREF(result);
1090 Py_XDECREF(method);
1091
1092 return cursor;
1093}
1094
Gerhard Häring0741a602007-01-14 01:43:50 +00001095PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001096{
1097 PyObject* cursor = 0;
1098 PyObject* result = 0;
1099 PyObject* method = 0;
1100
1101 cursor = PyObject_CallMethod((PyObject*)self, "cursor", "");
1102 if (!cursor) {
1103 goto error;
1104 }
1105
1106 method = PyObject_GetAttrString(cursor, "executescript");
1107 if (!method) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001108 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001109 goto error;
1110 }
1111
1112 result = PyObject_CallObject(method, args);
1113 if (!result) {
Alexandre Vassalottibd704762008-07-13 21:47:59 +00001114 Py_CLEAR(cursor);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001115 }
1116
1117error:
1118 Py_XDECREF(result);
1119 Py_XDECREF(method);
1120
1121 return cursor;
1122}
1123
Anthony Baxter72289a62006-04-04 06:29:05 +00001124/* ------------------------- COLLATION CODE ------------------------ */
1125
1126static int
Gerhard Häring0741a602007-01-14 01:43:50 +00001127pysqlite_collation_callback(
Anthony Baxter72289a62006-04-04 06:29:05 +00001128 void* context,
1129 int text1_length, const void* text1_data,
1130 int text2_length, const void* text2_data)
1131{
1132 PyObject* callback = (PyObject*)context;
1133 PyObject* string1 = 0;
1134 PyObject* string2 = 0;
1135 PyGILState_STATE gilstate;
1136
1137 PyObject* retval = NULL;
1138 int result = 0;
1139
1140 gilstate = PyGILState_Ensure();
1141
1142 if (PyErr_Occurred()) {
1143 goto finally;
1144 }
1145
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001146 string1 = PyString_FromStringAndSize((const char*)text1_data, text1_length);
1147 string2 = PyString_FromStringAndSize((const char*)text2_data, text2_length);
Anthony Baxter72289a62006-04-04 06:29:05 +00001148
1149 if (!string1 || !string2) {
1150 goto finally; /* failed to allocate strings */
1151 }
1152
1153 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1154
1155 if (!retval) {
1156 /* execution failed */
1157 goto finally;
1158 }
1159
1160 result = PyInt_AsLong(retval);
1161 if (PyErr_Occurred()) {
1162 result = 0;
1163 }
1164
1165finally:
1166 Py_XDECREF(string1);
1167 Py_XDECREF(string2);
1168 Py_XDECREF(retval);
1169
1170 PyGILState_Release(gilstate);
1171
1172 return result;
1173}
1174
1175static PyObject *
Gerhard Häring0741a602007-01-14 01:43:50 +00001176pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
Gerhard Häring1541ef02006-06-13 22:24:47 +00001177{
1178 PyObject* retval = NULL;
1179
Gerhard Häring0741a602007-01-14 01:43:50 +00001180 if (!pysqlite_check_connection(self)) {
Gerhard Häring1541ef02006-06-13 22:24:47 +00001181 goto finally;
1182 }
1183
1184 sqlite3_interrupt(self->db);
1185
1186 Py_INCREF(Py_None);
1187 retval = Py_None;
1188
1189finally:
1190 return retval;
1191}
1192
Gregory P. Smithb9803422008-03-28 08:32:09 +00001193/* Function author: Paul Kippes <kippesp@gmail.com>
1194 * Class method of Connection to call the Python function _iterdump
1195 * of the sqlite3 module.
1196 */
1197static PyObject *
1198pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1199{
1200 PyObject* retval = NULL;
1201 PyObject* module = NULL;
1202 PyObject* module_dict;
1203 PyObject* pyfn_iterdump;
1204
1205 if (!pysqlite_check_connection(self)) {
1206 goto finally;
1207 }
1208
1209 module = PyImport_ImportModule(MODULE_NAME ".dump");
1210 if (!module) {
1211 goto finally;
1212 }
1213
1214 module_dict = PyModule_GetDict(module);
1215 if (!module_dict) {
1216 goto finally;
1217 }
1218
1219 pyfn_iterdump = PyDict_GetItemString(module_dict, "_iterdump");
1220 if (!pyfn_iterdump) {
1221 PyErr_SetString(pysqlite_OperationalError, "Failed to obtain _iterdump() reference");
1222 goto finally;
1223 }
1224
1225 args = PyTuple_New(1);
1226 if (!args) {
1227 goto finally;
1228 }
1229 Py_INCREF(self);
1230 PyTuple_SetItem(args, 0, (PyObject*)self);
1231 retval = PyObject_CallObject(pyfn_iterdump, args);
1232
1233finally:
1234 Py_XDECREF(args);
1235 Py_XDECREF(module);
1236 return retval;
1237}
1238
Gerhard Häring1541ef02006-06-13 22:24:47 +00001239static PyObject *
Gerhard Häring0741a602007-01-14 01:43:50 +00001240pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
Anthony Baxter72289a62006-04-04 06:29:05 +00001241{
1242 PyObject* callable;
1243 PyObject* uppercase_name = 0;
1244 PyObject* name;
1245 PyObject* retval;
1246 char* chk;
1247 int rc;
1248
Gerhard Häring0741a602007-01-14 01:43:50 +00001249 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Anthony Baxter72289a62006-04-04 06:29:05 +00001250 goto finally;
1251 }
1252
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001253 if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyString_Type, &name, &callable)) {
Anthony Baxter72289a62006-04-04 06:29:05 +00001254 goto finally;
1255 }
1256
1257 uppercase_name = PyObject_CallMethod(name, "upper", "");
1258 if (!uppercase_name) {
1259 goto finally;
1260 }
1261
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001262 chk = PyString_AsString(uppercase_name);
Anthony Baxter72289a62006-04-04 06:29:05 +00001263 while (*chk) {
1264 if ((*chk >= '0' && *chk <= '9')
1265 || (*chk >= 'A' && *chk <= 'Z')
1266 || (*chk == '_'))
1267 {
1268 chk++;
1269 } else {
Gerhard Häring0741a602007-01-14 01:43:50 +00001270 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Anthony Baxter72289a62006-04-04 06:29:05 +00001271 goto finally;
1272 }
1273 }
1274
1275 if (callable != Py_None && !PyCallable_Check(callable)) {
1276 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1277 goto finally;
1278 }
1279
1280 if (callable != Py_None) {
1281 PyDict_SetItem(self->collations, uppercase_name, callable);
1282 } else {
1283 PyDict_DelItem(self->collations, uppercase_name);
1284 }
1285
1286 rc = sqlite3_create_collation(self->db,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001287 PyString_AsString(uppercase_name),
Anthony Baxter72289a62006-04-04 06:29:05 +00001288 SQLITE_UTF8,
1289 (callable != Py_None) ? callable : NULL,
Gerhard Häring0741a602007-01-14 01:43:50 +00001290 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Anthony Baxter72289a62006-04-04 06:29:05 +00001291 if (rc != SQLITE_OK) {
1292 PyDict_DelItem(self->collations, uppercase_name);
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001293 _pysqlite_seterror(self->db, NULL);
Anthony Baxter72289a62006-04-04 06:29:05 +00001294 goto finally;
1295 }
1296
1297finally:
1298 Py_XDECREF(uppercase_name);
1299
1300 if (PyErr_Occurred()) {
1301 retval = NULL;
1302 } else {
1303 Py_INCREF(Py_None);
1304 retval = Py_None;
1305 }
1306
1307 return retval;
1308}
1309
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001310/* Called when the connection is used as a context manager. Returns itself as a
1311 * convenience to the caller. */
1312static PyObject *
1313pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1314{
1315 Py_INCREF(self);
1316 return (PyObject*)self;
1317}
1318
1319/** Called when the connection is used as a context manager. If there was any
1320 * exception, a rollback takes place; otherwise we commit. */
1321static PyObject *
1322pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1323{
1324 PyObject* exc_type, *exc_value, *exc_tb;
1325 char* method_name;
1326 PyObject* result;
1327
1328 if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1329 return NULL;
1330 }
1331
1332 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1333 method_name = "commit";
1334 } else {
1335 method_name = "rollback";
1336 }
1337
1338 result = PyObject_CallMethod((PyObject*)self, method_name, "");
1339 if (!result) {
1340 return NULL;
1341 }
1342 Py_DECREF(result);
1343
1344 Py_INCREF(Py_False);
1345 return Py_False;
1346}
1347
Anthony Baxterc51ee692006-04-01 00:57:31 +00001348static char connection_doc[] =
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001349PyDoc_STR("SQLite database connection object.");
Anthony Baxterc51ee692006-04-01 00:57:31 +00001350
1351static PyGetSetDef connection_getset[] = {
Gerhard Häring0741a602007-01-14 01:43:50 +00001352 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1353 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Anthony Baxterc51ee692006-04-01 00:57:31 +00001354 {NULL}
1355};
1356
1357static PyMethodDef connection_methods[] = {
Gerhard Häring0741a602007-01-14 01:43:50 +00001358 {"cursor", (PyCFunction)pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001359 PyDoc_STR("Return a cursor for the connection.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001360 {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001361 PyDoc_STR("Closes the connection.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001362 {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001363 PyDoc_STR("Commit the current transaction.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001364 {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001365 PyDoc_STR("Roll back the current transaction.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001366 {"create_function", (PyCFunction)pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001367 PyDoc_STR("Creates a new function. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001368 {"create_aggregate", (PyCFunction)pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001369 PyDoc_STR("Creates a new aggregate. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001370 {"set_authorizer", (PyCFunction)pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
Gerhard Häring1541ef02006-06-13 22:24:47 +00001371 PyDoc_STR("Sets authorizer callback. Non-standard.")},
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001372 {"set_progress_handler", (PyCFunction)pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1373 PyDoc_STR("Sets progress handler callback. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001374 {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001375 PyDoc_STR("Executes a SQL statement. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001376 {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001377 PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001378 {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
Anthony Baxterc51ee692006-04-01 00:57:31 +00001379 PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001380 {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
Gerhard Häring3e99c0a2006-04-23 15:24:26 +00001381 PyDoc_STR("Creates a collation function. Non-standard.")},
Gerhard Häring0741a602007-01-14 01:43:50 +00001382 {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
Gerhard Häring1541ef02006-06-13 22:24:47 +00001383 PyDoc_STR("Abort any pending database operation. Non-standard.")},
Gregory P. Smithb9803422008-03-28 08:32:09 +00001384 {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
1385 PyDoc_STR("Returns iterator to the dump of the database in an SQL text format.")},
Gerhard Häring1cc60ed2008-02-29 22:08:41 +00001386 {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1387 PyDoc_STR("For context manager. Non-standard.")},
1388 {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1389 PyDoc_STR("For context manager. Non-standard.")},
Anthony Baxterc51ee692006-04-01 00:57:31 +00001390 {NULL, NULL}
1391};
1392
1393static struct PyMemberDef connection_members[] =
1394{
Gerhard Häring0741a602007-01-14 01:43:50 +00001395 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), RO},
1396 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), RO},
1397 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), RO},
1398 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), RO},
1399 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), RO},
1400 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), RO},
1401 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), RO},
1402 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), RO},
1403 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), RO},
1404 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), RO},
1405 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1406 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Anthony Baxterc51ee692006-04-01 00:57:31 +00001407 {NULL}
1408};
1409
Gerhard Häring0741a602007-01-14 01:43:50 +00001410PyTypeObject pysqlite_ConnectionType = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001411 PyVarObject_HEAD_INIT(NULL, 0)
Anthony Baxter8e7b4902006-04-05 18:25:33 +00001412 MODULE_NAME ".Connection", /* tp_name */
Gerhard Häring0741a602007-01-14 01:43:50 +00001413 sizeof(pysqlite_Connection), /* tp_basicsize */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001414 0, /* tp_itemsize */
Gerhard Häring0741a602007-01-14 01:43:50 +00001415 (destructor)pysqlite_connection_dealloc, /* tp_dealloc */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001416 0, /* tp_print */
1417 0, /* tp_getattr */
1418 0, /* tp_setattr */
1419 0, /* tp_compare */
1420 0, /* tp_repr */
1421 0, /* tp_as_number */
1422 0, /* tp_as_sequence */
1423 0, /* tp_as_mapping */
1424 0, /* tp_hash */
Gerhard Häring0741a602007-01-14 01:43:50 +00001425 (ternaryfunc)pysqlite_connection_call, /* tp_call */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001426 0, /* tp_str */
1427 0, /* tp_getattro */
1428 0, /* tp_setattro */
1429 0, /* tp_as_buffer */
1430 Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
1431 connection_doc, /* tp_doc */
1432 0, /* tp_traverse */
1433 0, /* tp_clear */
1434 0, /* tp_richcompare */
1435 0, /* tp_weaklistoffset */
1436 0, /* tp_iter */
1437 0, /* tp_iternext */
1438 connection_methods, /* tp_methods */
1439 connection_members, /* tp_members */
1440 connection_getset, /* tp_getset */
1441 0, /* tp_base */
1442 0, /* tp_dict */
1443 0, /* tp_descr_get */
1444 0, /* tp_descr_set */
1445 0, /* tp_dictoffset */
Gerhard Häring0741a602007-01-14 01:43:50 +00001446 (initproc)pysqlite_connection_init, /* tp_init */
Anthony Baxterc51ee692006-04-01 00:57:31 +00001447 0, /* tp_alloc */
1448 0, /* tp_new */
1449 0 /* tp_free */
1450};
1451
Gerhard Häring0741a602007-01-14 01:43:50 +00001452extern int pysqlite_connection_setup_types(void)
Anthony Baxterc51ee692006-04-01 00:57:31 +00001453{
Gerhard Häring0741a602007-01-14 01:43:50 +00001454 pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1455 return PyType_Ready(&pysqlite_ConnectionType);
Anthony Baxterc51ee692006-04-01 00:57:31 +00001456}