Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1 | /* connection.c - the connection type |
| 2 | * |
| 3 | * Copyright (C) 2004-2006 Gerhard Häring <gh@ghaering.de> |
| 4 | * |
| 5 | * This file is part of pysqlite. |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 6 | * |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 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" |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 31 | #include "sqlitecompat.h" |
| 32 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 33 | #include "pythread.h" |
| 34 | |
| 35 | static int connection_set_isolation_level(Connection* self, PyObject* isolation_level); |
| 36 | |
| 37 | int connection_init(Connection* self, PyObject* args, PyObject* kwargs) |
| 38 | { |
| 39 | static char *kwlist[] = {"database", "timeout", "detect_types", "isolation_level", "check_same_thread", "factory", "cached_statements", NULL, NULL}; |
| 40 | |
| 41 | char* database; |
| 42 | int detect_types = 0; |
| 43 | PyObject* isolation_level = NULL; |
| 44 | PyObject* factory = NULL; |
| 45 | int check_same_thread = 1; |
| 46 | int cached_statements = 100; |
| 47 | double timeout = 5.0; |
| 48 | int rc; |
| 49 | |
| 50 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOi", kwlist, |
| 51 | &database, &timeout, &detect_types, &isolation_level, &check_same_thread, &factory, &cached_statements)) |
| 52 | { |
| 53 | return -1; |
| 54 | } |
| 55 | |
| 56 | self->begin_statement = NULL; |
| 57 | |
| 58 | self->statement_cache = NULL; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 59 | self->statements = NULL; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 60 | |
| 61 | Py_INCREF(Py_None); |
| 62 | self->row_factory = Py_None; |
| 63 | |
| 64 | Py_INCREF(&PyUnicode_Type); |
| 65 | self->text_factory = (PyObject*)&PyUnicode_Type; |
| 66 | |
| 67 | Py_BEGIN_ALLOW_THREADS |
| 68 | rc = sqlite3_open(database, &self->db); |
| 69 | Py_END_ALLOW_THREADS |
| 70 | |
| 71 | if (rc != SQLITE_OK) { |
| 72 | _seterror(self->db); |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | if (!isolation_level) { |
| 77 | isolation_level = PyString_FromString(""); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 78 | if (!isolation_level) { |
| 79 | return -1; |
| 80 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 81 | } else { |
| 82 | Py_INCREF(isolation_level); |
| 83 | } |
| 84 | self->isolation_level = NULL; |
| 85 | connection_set_isolation_level(self, isolation_level); |
| 86 | Py_DECREF(isolation_level); |
| 87 | |
| 88 | self->statement_cache = (Cache*)PyObject_CallFunction((PyObject*)&CacheType, "Oi", self, cached_statements); |
| 89 | if (PyErr_Occurred()) { |
| 90 | return -1; |
| 91 | } |
| 92 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 93 | self->statements = PyList_New(0); |
| 94 | if (!self->statements) { |
| 95 | return -1; |
| 96 | } |
| 97 | self->created_statements = 0; |
| 98 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 99 | /* By default, the Cache class INCREFs the factory in its initializer, and |
| 100 | * decrefs it in its deallocator method. Since this would create a circular |
| 101 | * reference here, we're breaking it by decrementing self, and telling the |
| 102 | * cache class to not decref the factory (self) in its deallocator. |
| 103 | */ |
| 104 | self->statement_cache->decref_factory = 0; |
| 105 | Py_DECREF(self); |
| 106 | |
| 107 | self->inTransaction = 0; |
| 108 | self->detect_types = detect_types; |
| 109 | self->timeout = timeout; |
| 110 | (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000)); |
| 111 | |
| 112 | self->thread_ident = PyThread_get_thread_ident(); |
| 113 | self->check_same_thread = check_same_thread; |
| 114 | |
| 115 | self->function_pinboard = PyDict_New(); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 116 | if (!self->function_pinboard) { |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | self->collations = PyDict_New(); |
| 121 | if (!self->collations) { |
| 122 | return -1; |
| 123 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 124 | |
| 125 | self->Warning = Warning; |
| 126 | self->Error = Error; |
| 127 | self->InterfaceError = InterfaceError; |
| 128 | self->DatabaseError = DatabaseError; |
| 129 | self->DataError = DataError; |
| 130 | self->OperationalError = OperationalError; |
| 131 | self->IntegrityError = IntegrityError; |
| 132 | self->InternalError = InternalError; |
| 133 | self->ProgrammingError = ProgrammingError; |
| 134 | self->NotSupportedError = NotSupportedError; |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 139 | /* Empty the entire statement cache of this connection */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 140 | void flush_statement_cache(Connection* self) |
| 141 | { |
| 142 | Node* node; |
| 143 | Statement* statement; |
| 144 | |
| 145 | node = self->statement_cache->first; |
| 146 | |
| 147 | while (node) { |
| 148 | statement = (Statement*)(node->data); |
| 149 | (void)statement_finalize(statement); |
| 150 | node = node->next; |
| 151 | } |
| 152 | |
| 153 | Py_DECREF(self->statement_cache); |
| 154 | self->statement_cache = (Cache*)PyObject_CallFunction((PyObject*)&CacheType, "O", self); |
| 155 | Py_DECREF(self); |
| 156 | self->statement_cache->decref_factory = 0; |
| 157 | } |
| 158 | |
| 159 | void reset_all_statements(Connection* self) |
| 160 | { |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 161 | int i; |
| 162 | PyObject* weakref; |
| 163 | PyObject* statement; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 164 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 165 | for (i = 0; i < PyList_Size(self->statements); i++) { |
| 166 | weakref = PyList_GetItem(self->statements, i); |
| 167 | statement = PyWeakref_GetObject(weakref); |
| 168 | if (statement != Py_None) { |
| 169 | (void)statement_reset((Statement*)statement); |
| 170 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
| 174 | void connection_dealloc(Connection* self) |
| 175 | { |
| 176 | Py_XDECREF(self->statement_cache); |
| 177 | |
| 178 | /* Clean up if user has not called .close() explicitly. */ |
| 179 | if (self->db) { |
| 180 | Py_BEGIN_ALLOW_THREADS |
| 181 | sqlite3_close(self->db); |
| 182 | Py_END_ALLOW_THREADS |
| 183 | } |
| 184 | |
| 185 | if (self->begin_statement) { |
| 186 | PyMem_Free(self->begin_statement); |
| 187 | } |
| 188 | Py_XDECREF(self->isolation_level); |
| 189 | Py_XDECREF(self->function_pinboard); |
| 190 | Py_XDECREF(self->row_factory); |
| 191 | Py_XDECREF(self->text_factory); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 192 | Py_XDECREF(self->collations); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 193 | Py_XDECREF(self->statements); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 194 | |
| 195 | self->ob_type->tp_free((PyObject*)self); |
| 196 | } |
| 197 | |
| 198 | PyObject* connection_cursor(Connection* self, PyObject* args, PyObject* kwargs) |
| 199 | { |
| 200 | static char *kwlist[] = {"factory", NULL, NULL}; |
| 201 | PyObject* factory = NULL; |
| 202 | PyObject* cursor; |
| 203 | |
| 204 | |
| 205 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, |
| 206 | &factory)) { |
| 207 | return NULL; |
| 208 | } |
| 209 | |
| 210 | if (!check_thread(self) || !check_connection(self)) { |
| 211 | return NULL; |
| 212 | } |
| 213 | |
| 214 | if (factory == NULL) { |
| 215 | factory = (PyObject*)&CursorType; |
| 216 | } |
| 217 | |
| 218 | cursor = PyObject_CallFunction(factory, "O", self); |
| 219 | |
| 220 | if (cursor && self->row_factory != Py_None) { |
| 221 | Py_XDECREF(((Cursor*)cursor)->row_factory); |
| 222 | Py_INCREF(self->row_factory); |
| 223 | ((Cursor*)cursor)->row_factory = self->row_factory; |
| 224 | } |
| 225 | |
| 226 | return cursor; |
| 227 | } |
| 228 | |
| 229 | PyObject* connection_close(Connection* self, PyObject* args) |
| 230 | { |
| 231 | int rc; |
| 232 | |
| 233 | if (!check_thread(self)) { |
| 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | flush_statement_cache(self); |
| 238 | |
| 239 | if (self->db) { |
| 240 | Py_BEGIN_ALLOW_THREADS |
| 241 | rc = sqlite3_close(self->db); |
| 242 | Py_END_ALLOW_THREADS |
| 243 | |
| 244 | if (rc != SQLITE_OK) { |
| 245 | _seterror(self->db); |
| 246 | return NULL; |
| 247 | } else { |
| 248 | self->db = NULL; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | Py_INCREF(Py_None); |
| 253 | return Py_None; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Checks if a connection object is usable (i. e. not closed). |
| 258 | * |
| 259 | * 0 => error; 1 => ok |
| 260 | */ |
| 261 | int check_connection(Connection* con) |
| 262 | { |
| 263 | if (!con->db) { |
| 264 | PyErr_SetString(ProgrammingError, "Cannot operate on a closed database."); |
| 265 | return 0; |
| 266 | } else { |
| 267 | return 1; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | PyObject* _connection_begin(Connection* self) |
| 272 | { |
| 273 | int rc; |
| 274 | const char* tail; |
| 275 | sqlite3_stmt* statement; |
| 276 | |
| 277 | Py_BEGIN_ALLOW_THREADS |
| 278 | rc = sqlite3_prepare(self->db, self->begin_statement, -1, &statement, &tail); |
| 279 | Py_END_ALLOW_THREADS |
| 280 | |
| 281 | if (rc != SQLITE_OK) { |
| 282 | _seterror(self->db); |
| 283 | goto error; |
| 284 | } |
| 285 | |
| 286 | rc = _sqlite_step_with_busyhandler(statement, self); |
| 287 | if (rc == SQLITE_DONE) { |
| 288 | self->inTransaction = 1; |
| 289 | } else { |
| 290 | _seterror(self->db); |
| 291 | } |
| 292 | |
| 293 | Py_BEGIN_ALLOW_THREADS |
| 294 | rc = sqlite3_finalize(statement); |
| 295 | Py_END_ALLOW_THREADS |
| 296 | |
| 297 | if (rc != SQLITE_OK && !PyErr_Occurred()) { |
| 298 | _seterror(self->db); |
| 299 | } |
| 300 | |
| 301 | error: |
| 302 | if (PyErr_Occurred()) { |
| 303 | return NULL; |
| 304 | } else { |
| 305 | Py_INCREF(Py_None); |
| 306 | return Py_None; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | PyObject* connection_commit(Connection* self, PyObject* args) |
| 311 | { |
| 312 | int rc; |
| 313 | const char* tail; |
| 314 | sqlite3_stmt* statement; |
| 315 | |
| 316 | if (!check_thread(self) || !check_connection(self)) { |
| 317 | return NULL; |
| 318 | } |
| 319 | |
| 320 | if (self->inTransaction) { |
| 321 | Py_BEGIN_ALLOW_THREADS |
| 322 | rc = sqlite3_prepare(self->db, "COMMIT", -1, &statement, &tail); |
| 323 | Py_END_ALLOW_THREADS |
| 324 | if (rc != SQLITE_OK) { |
| 325 | _seterror(self->db); |
| 326 | goto error; |
| 327 | } |
| 328 | |
| 329 | rc = _sqlite_step_with_busyhandler(statement, self); |
| 330 | if (rc == SQLITE_DONE) { |
| 331 | self->inTransaction = 0; |
| 332 | } else { |
| 333 | _seterror(self->db); |
| 334 | } |
| 335 | |
| 336 | Py_BEGIN_ALLOW_THREADS |
| 337 | rc = sqlite3_finalize(statement); |
| 338 | Py_END_ALLOW_THREADS |
| 339 | if (rc != SQLITE_OK && !PyErr_Occurred()) { |
| 340 | _seterror(self->db); |
| 341 | } |
| 342 | |
| 343 | } |
| 344 | |
| 345 | error: |
| 346 | if (PyErr_Occurred()) { |
| 347 | return NULL; |
| 348 | } else { |
| 349 | Py_INCREF(Py_None); |
| 350 | return Py_None; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | PyObject* connection_rollback(Connection* self, PyObject* args) |
| 355 | { |
| 356 | int rc; |
| 357 | const char* tail; |
| 358 | sqlite3_stmt* statement; |
| 359 | |
| 360 | if (!check_thread(self) || !check_connection(self)) { |
| 361 | return NULL; |
| 362 | } |
| 363 | |
| 364 | if (self->inTransaction) { |
| 365 | reset_all_statements(self); |
| 366 | |
| 367 | Py_BEGIN_ALLOW_THREADS |
| 368 | rc = sqlite3_prepare(self->db, "ROLLBACK", -1, &statement, &tail); |
| 369 | Py_END_ALLOW_THREADS |
| 370 | if (rc != SQLITE_OK) { |
| 371 | _seterror(self->db); |
| 372 | goto error; |
| 373 | } |
| 374 | |
| 375 | rc = _sqlite_step_with_busyhandler(statement, self); |
| 376 | if (rc == SQLITE_DONE) { |
| 377 | self->inTransaction = 0; |
| 378 | } else { |
| 379 | _seterror(self->db); |
| 380 | } |
| 381 | |
| 382 | Py_BEGIN_ALLOW_THREADS |
| 383 | rc = sqlite3_finalize(statement); |
| 384 | Py_END_ALLOW_THREADS |
| 385 | if (rc != SQLITE_OK && !PyErr_Occurred()) { |
| 386 | _seterror(self->db); |
| 387 | } |
| 388 | |
| 389 | } |
| 390 | |
| 391 | error: |
| 392 | if (PyErr_Occurred()) { |
| 393 | return NULL; |
| 394 | } else { |
| 395 | Py_INCREF(Py_None); |
| 396 | return Py_None; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | void _set_result(sqlite3_context* context, PyObject* py_val) |
| 401 | { |
| 402 | long longval; |
| 403 | const char* buffer; |
Neal Norwitz | 95f0e4c | 2006-04-01 09:08:06 +0000 | [diff] [blame] | 404 | Py_ssize_t buflen; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 405 | PyObject* stringval; |
| 406 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 407 | if ((!py_val) || PyErr_Occurred()) { |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 408 | sqlite3_result_null(context); |
| 409 | } else if (py_val == Py_None) { |
| 410 | sqlite3_result_null(context); |
| 411 | } else if (PyInt_Check(py_val)) { |
| 412 | longval = PyInt_AsLong(py_val); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 413 | sqlite3_result_int64(context, (PY_LONG_LONG)longval); |
| 414 | } else if (PyFloat_Check(py_val)) { |
| 415 | sqlite3_result_double(context, PyFloat_AsDouble(py_val)); |
| 416 | } else if (PyBuffer_Check(py_val)) { |
| 417 | if (PyObject_AsCharBuffer(py_val, &buffer, &buflen) != 0) { |
| 418 | PyErr_SetString(PyExc_ValueError, "could not convert BLOB to buffer"); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 419 | } else { |
| 420 | sqlite3_result_blob(context, buffer, buflen, SQLITE_TRANSIENT); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 421 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 422 | } else if (PyString_Check(py_val)) { |
| 423 | sqlite3_result_text(context, PyString_AsString(py_val), -1, SQLITE_TRANSIENT); |
| 424 | } else if (PyUnicode_Check(py_val)) { |
| 425 | stringval = PyUnicode_AsUTF8String(py_val); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 426 | if (stringval) { |
| 427 | sqlite3_result_text(context, PyString_AsString(stringval), -1, SQLITE_TRANSIENT); |
| 428 | Py_DECREF(stringval); |
| 429 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 430 | } else { |
| 431 | /* TODO: raise error */ |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | PyObject* _build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv) |
| 436 | { |
| 437 | PyObject* args; |
| 438 | int i; |
| 439 | sqlite3_value* cur_value; |
| 440 | PyObject* cur_py_value; |
| 441 | const char* val_str; |
| 442 | PY_LONG_LONG val_int; |
Neal Norwitz | 95f0e4c | 2006-04-01 09:08:06 +0000 | [diff] [blame] | 443 | Py_ssize_t buflen; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 444 | void* raw_buffer; |
| 445 | |
| 446 | args = PyTuple_New(argc); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 447 | if (!args) { |
| 448 | return NULL; |
| 449 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 450 | |
| 451 | for (i = 0; i < argc; i++) { |
| 452 | cur_value = argv[i]; |
| 453 | switch (sqlite3_value_type(argv[i])) { |
| 454 | case SQLITE_INTEGER: |
| 455 | val_int = sqlite3_value_int64(cur_value); |
| 456 | cur_py_value = PyInt_FromLong((long)val_int); |
| 457 | break; |
| 458 | case SQLITE_FLOAT: |
| 459 | cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value)); |
| 460 | break; |
| 461 | case SQLITE_TEXT: |
| 462 | val_str = (const char*)sqlite3_value_text(cur_value); |
| 463 | cur_py_value = PyUnicode_DecodeUTF8(val_str, strlen(val_str), NULL); |
| 464 | /* TODO: have a way to show errors here */ |
| 465 | if (!cur_py_value) { |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 466 | PyErr_Clear(); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 467 | Py_INCREF(Py_None); |
| 468 | cur_py_value = Py_None; |
| 469 | } |
| 470 | break; |
| 471 | case SQLITE_BLOB: |
| 472 | buflen = sqlite3_value_bytes(cur_value); |
| 473 | cur_py_value = PyBuffer_New(buflen); |
| 474 | if (!cur_py_value) { |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 475 | break; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 476 | } |
| 477 | if (PyObject_AsWriteBuffer(cur_py_value, &raw_buffer, &buflen)) { |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 478 | Py_DECREF(cur_py_value); |
| 479 | cur_py_value = NULL; |
| 480 | break; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 481 | } |
| 482 | memcpy(raw_buffer, sqlite3_value_blob(cur_value), buflen); |
| 483 | break; |
| 484 | case SQLITE_NULL: |
| 485 | default: |
| 486 | Py_INCREF(Py_None); |
| 487 | cur_py_value = Py_None; |
| 488 | } |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 489 | |
| 490 | if (!cur_py_value) { |
| 491 | Py_DECREF(args); |
| 492 | return NULL; |
| 493 | } |
| 494 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 495 | PyTuple_SetItem(args, i, cur_py_value); |
| 496 | |
| 497 | } |
| 498 | |
| 499 | return args; |
| 500 | } |
| 501 | |
| 502 | void _func_callback(sqlite3_context* context, int argc, sqlite3_value** argv) |
| 503 | { |
| 504 | PyObject* args; |
| 505 | PyObject* py_func; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 506 | PyObject* py_retval = NULL; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 507 | |
| 508 | PyGILState_STATE threadstate; |
| 509 | |
| 510 | threadstate = PyGILState_Ensure(); |
| 511 | |
| 512 | py_func = (PyObject*)sqlite3_user_data(context); |
| 513 | |
| 514 | args = _build_py_params(context, argc, argv); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 515 | if (args) { |
| 516 | py_retval = PyObject_CallObject(py_func, args); |
| 517 | Py_DECREF(args); |
| 518 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 519 | |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame^] | 520 | if (py_retval) { |
| 521 | _set_result(context, py_retval); |
| 522 | Py_DECREF(py_retval); |
| 523 | } else { |
| 524 | if (_enable_callback_tracebacks) { |
| 525 | PyErr_Print(); |
| 526 | } else { |
| 527 | PyErr_Clear(); |
| 528 | } |
| 529 | sqlite3_result_error(context, "user-defined function raised exception", -1); |
| 530 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 531 | |
| 532 | PyGILState_Release(threadstate); |
| 533 | } |
| 534 | |
| 535 | static void _step_callback(sqlite3_context *context, int argc, sqlite3_value** params) |
| 536 | { |
| 537 | PyObject* args; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 538 | PyObject* function_result = NULL; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 539 | PyObject* aggregate_class; |
| 540 | PyObject** aggregate_instance; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 541 | PyObject* stepmethod = NULL; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 542 | |
| 543 | PyGILState_STATE threadstate; |
| 544 | |
| 545 | threadstate = PyGILState_Ensure(); |
| 546 | |
| 547 | aggregate_class = (PyObject*)sqlite3_user_data(context); |
| 548 | |
| 549 | aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); |
| 550 | |
| 551 | if (*aggregate_instance == 0) { |
| 552 | *aggregate_instance = PyObject_CallFunction(aggregate_class, ""); |
| 553 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 554 | if (PyErr_Occurred()) { |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 555 | *aggregate_instance = 0; |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame^] | 556 | if (_enable_callback_tracebacks) { |
| 557 | PyErr_Print(); |
| 558 | } else { |
| 559 | PyErr_Clear(); |
| 560 | } |
| 561 | sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 562 | goto error; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | |
| 566 | stepmethod = PyObject_GetAttrString(*aggregate_instance, "step"); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 567 | if (!stepmethod) { |
| 568 | goto error; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | args = _build_py_params(context, argc, params); |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 572 | if (!args) { |
| 573 | goto error; |
| 574 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 575 | |
| 576 | function_result = PyObject_CallObject(stepmethod, args); |
| 577 | Py_DECREF(args); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 578 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 579 | if (!function_result) { |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame^] | 580 | if (_enable_callback_tracebacks) { |
| 581 | PyErr_Print(); |
| 582 | } else { |
| 583 | PyErr_Clear(); |
| 584 | } |
| 585 | sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 588 | error: |
| 589 | Py_XDECREF(stepmethod); |
| 590 | Py_XDECREF(function_result); |
| 591 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 592 | PyGILState_Release(threadstate); |
| 593 | } |
| 594 | |
| 595 | void _final_callback(sqlite3_context* context) |
| 596 | { |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 597 | PyObject* function_result = NULL; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 598 | PyObject** aggregate_instance; |
| 599 | PyObject* aggregate_class; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 600 | |
| 601 | PyGILState_STATE threadstate; |
| 602 | |
| 603 | threadstate = PyGILState_Ensure(); |
| 604 | |
| 605 | aggregate_class = (PyObject*)sqlite3_user_data(context); |
| 606 | |
| 607 | aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*)); |
| 608 | if (!*aggregate_instance) { |
| 609 | /* this branch is executed if there was an exception in the aggregate's |
| 610 | * __init__ */ |
| 611 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 612 | goto error; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 615 | function_result = PyObject_CallMethod(*aggregate_instance, "finalize", ""); |
| 616 | if (!function_result) { |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame^] | 617 | if (_enable_callback_tracebacks) { |
| 618 | PyErr_Print(); |
| 619 | } else { |
| 620 | PyErr_Clear(); |
| 621 | } |
| 622 | sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1); |
| 623 | } else { |
| 624 | _set_result(context, function_result); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 627 | error: |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 628 | Py_XDECREF(*aggregate_instance); |
| 629 | Py_XDECREF(function_result); |
| 630 | |
| 631 | PyGILState_Release(threadstate); |
| 632 | } |
| 633 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 634 | void _drop_unused_statement_references(Connection* self) |
| 635 | { |
| 636 | PyObject* new_list; |
| 637 | PyObject* weakref; |
| 638 | int i; |
| 639 | |
| 640 | /* we only need to do this once in a while */ |
| 641 | if (self->created_statements++ < 200) { |
| 642 | return; |
| 643 | } |
| 644 | |
| 645 | self->created_statements = 0; |
| 646 | |
| 647 | new_list = PyList_New(0); |
| 648 | if (!new_list) { |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | for (i = 0; i < PyList_Size(self->statements); i++) { |
| 653 | weakref = PyList_GetItem(self->statements, i); |
| 654 | if (weakref != Py_None) { |
| 655 | if (PyList_Append(new_list, weakref) != 0) { |
| 656 | Py_DECREF(new_list); |
| 657 | return; |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | Py_DECREF(self->statements); |
| 663 | self->statements = new_list; |
| 664 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 665 | |
| 666 | PyObject* connection_create_function(Connection* self, PyObject* args, PyObject* kwargs) |
| 667 | { |
| 668 | static char *kwlist[] = {"name", "narg", "func", NULL, NULL}; |
| 669 | |
| 670 | PyObject* func; |
| 671 | char* name; |
| 672 | int narg; |
| 673 | int rc; |
| 674 | |
| 675 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO", kwlist, |
| 676 | &name, &narg, &func)) |
| 677 | { |
| 678 | return NULL; |
| 679 | } |
| 680 | |
| 681 | rc = sqlite3_create_function(self->db, name, narg, SQLITE_UTF8, (void*)func, _func_callback, NULL, NULL); |
| 682 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 683 | if (rc != SQLITE_OK) { |
| 684 | /* Workaround for SQLite bug: no error code or string is available here */ |
| 685 | PyErr_SetString(OperationalError, "Error creating function"); |
| 686 | return NULL; |
| 687 | } else { |
| 688 | PyDict_SetItem(self->function_pinboard, func, Py_None); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 689 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 690 | Py_INCREF(Py_None); |
| 691 | return Py_None; |
| 692 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | PyObject* connection_create_aggregate(Connection* self, PyObject* args, PyObject* kwargs) |
| 696 | { |
| 697 | PyObject* aggregate_class; |
| 698 | |
| 699 | int n_arg; |
| 700 | char* name; |
| 701 | static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL }; |
| 702 | int rc; |
| 703 | |
| 704 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate", |
| 705 | kwlist, &name, &n_arg, &aggregate_class)) { |
| 706 | return NULL; |
| 707 | } |
| 708 | |
| 709 | rc = sqlite3_create_function(self->db, name, n_arg, SQLITE_UTF8, (void*)aggregate_class, 0, &_step_callback, &_final_callback); |
| 710 | if (rc != SQLITE_OK) { |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 711 | /* Workaround for SQLite bug: no error code or string is available here */ |
| 712 | PyErr_SetString(OperationalError, "Error creating aggregate"); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 713 | return NULL; |
| 714 | } else { |
| 715 | PyDict_SetItem(self->function_pinboard, aggregate_class, Py_None); |
| 716 | |
| 717 | Py_INCREF(Py_None); |
| 718 | return Py_None; |
| 719 | } |
| 720 | } |
| 721 | |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame^] | 722 | int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source) |
| 723 | { |
| 724 | PyObject *ret; |
| 725 | int rc; |
| 726 | PyGILState_STATE gilstate; |
| 727 | |
| 728 | gilstate = PyGILState_Ensure(); |
| 729 | ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source); |
| 730 | |
| 731 | if (!ret) { |
| 732 | if (_enable_callback_tracebacks) { |
| 733 | PyErr_Print(); |
| 734 | } else { |
| 735 | PyErr_Clear(); |
| 736 | } |
| 737 | |
| 738 | rc = SQLITE_DENY; |
| 739 | } else { |
| 740 | if (PyInt_Check(ret)) { |
| 741 | rc = (int)PyInt_AsLong(ret); |
| 742 | } else { |
| 743 | rc = SQLITE_DENY; |
| 744 | } |
| 745 | Py_DECREF(ret); |
| 746 | } |
| 747 | |
| 748 | PyGILState_Release(gilstate); |
| 749 | return rc; |
| 750 | } |
| 751 | |
| 752 | PyObject* connection_set_authorizer(Connection* self, PyObject* args, PyObject* kwargs) |
| 753 | { |
| 754 | PyObject* authorizer_cb; |
| 755 | |
| 756 | static char *kwlist[] = { "authorizer_callback", NULL }; |
| 757 | int rc; |
| 758 | |
| 759 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer", |
| 760 | kwlist, &authorizer_cb)) { |
| 761 | return NULL; |
| 762 | } |
| 763 | |
| 764 | rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb); |
| 765 | |
| 766 | if (rc != SQLITE_OK) { |
| 767 | PyErr_SetString(OperationalError, "Error setting authorizer callback"); |
| 768 | return NULL; |
| 769 | } else { |
| 770 | PyDict_SetItem(self->function_pinboard, authorizer_cb, Py_None); |
| 771 | |
| 772 | Py_INCREF(Py_None); |
| 773 | return Py_None; |
| 774 | } |
| 775 | } |
| 776 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 777 | int check_thread(Connection* self) |
| 778 | { |
| 779 | if (self->check_same_thread) { |
| 780 | if (PyThread_get_thread_ident() != self->thread_ident) { |
| 781 | PyErr_Format(ProgrammingError, |
| 782 | "SQLite objects created in a thread can only be used in that same thread." |
| 783 | "The object was created in thread id %ld and this is thread id %ld", |
| 784 | self->thread_ident, PyThread_get_thread_ident()); |
| 785 | return 0; |
| 786 | } |
| 787 | |
| 788 | } |
| 789 | |
| 790 | return 1; |
| 791 | } |
| 792 | |
| 793 | static PyObject* connection_get_isolation_level(Connection* self, void* unused) |
| 794 | { |
| 795 | Py_INCREF(self->isolation_level); |
| 796 | return self->isolation_level; |
| 797 | } |
| 798 | |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 799 | static PyObject* connection_get_total_changes(Connection* self, void* unused) |
| 800 | { |
| 801 | if (!check_connection(self)) { |
| 802 | return NULL; |
| 803 | } else { |
| 804 | return Py_BuildValue("i", sqlite3_total_changes(self->db)); |
| 805 | } |
| 806 | } |
| 807 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 808 | static int connection_set_isolation_level(Connection* self, PyObject* isolation_level) |
| 809 | { |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 810 | PyObject* res; |
| 811 | PyObject* begin_statement; |
| 812 | |
| 813 | Py_XDECREF(self->isolation_level); |
| 814 | |
Neal Norwitz | 5b03065 | 2006-04-16 03:28:17 +0000 | [diff] [blame] | 815 | if (self->begin_statement) { |
| 816 | PyMem_Free(self->begin_statement); |
| 817 | self->begin_statement = NULL; |
| 818 | } |
| 819 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 820 | if (isolation_level == Py_None) { |
| 821 | Py_INCREF(Py_None); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 822 | self->isolation_level = Py_None; |
| 823 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 824 | res = connection_commit(self, NULL); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 825 | if (!res) { |
| 826 | return -1; |
| 827 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 828 | Py_DECREF(res); |
| 829 | |
| 830 | self->inTransaction = 0; |
| 831 | } else { |
| 832 | Py_INCREF(isolation_level); |
| 833 | self->isolation_level = isolation_level; |
| 834 | |
| 835 | begin_statement = PyString_FromString("BEGIN "); |
| 836 | if (!begin_statement) { |
| 837 | return -1; |
| 838 | } |
| 839 | PyString_Concat(&begin_statement, isolation_level); |
| 840 | if (!begin_statement) { |
| 841 | return -1; |
| 842 | } |
| 843 | |
| 844 | self->begin_statement = PyMem_Malloc(PyString_Size(begin_statement) + 2); |
| 845 | if (!self->begin_statement) { |
| 846 | return -1; |
| 847 | } |
| 848 | |
| 849 | strcpy(self->begin_statement, PyString_AsString(begin_statement)); |
| 850 | Py_DECREF(begin_statement); |
| 851 | } |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | PyObject* connection_call(Connection* self, PyObject* args, PyObject* kwargs) |
| 857 | { |
| 858 | PyObject* sql; |
| 859 | Statement* statement; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 860 | PyObject* weakref; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 861 | int rc; |
| 862 | |
| 863 | if (!PyArg_ParseTuple(args, "O", &sql)) { |
| 864 | return NULL; |
| 865 | } |
| 866 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 867 | _drop_unused_statement_references(self); |
| 868 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 869 | statement = PyObject_New(Statement, &StatementType); |
| 870 | if (!statement) { |
| 871 | return NULL; |
| 872 | } |
| 873 | |
| 874 | rc = statement_create(statement, self, sql); |
| 875 | |
| 876 | if (rc != SQLITE_OK) { |
| 877 | if (rc == PYSQLITE_TOO_MUCH_SQL) { |
| 878 | PyErr_SetString(Warning, "You can only execute one statement at a time."); |
| 879 | } else if (rc == PYSQLITE_SQL_WRONG_TYPE) { |
| 880 | PyErr_SetString(Warning, "SQL is of wrong type. Must be string or unicode."); |
| 881 | } else { |
| 882 | _seterror(self->db); |
| 883 | } |
| 884 | |
| 885 | Py_DECREF(statement); |
| 886 | statement = 0; |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 887 | } else { |
| 888 | weakref = PyWeakref_NewRef((PyObject*)statement, NULL); |
| 889 | if (!weakref) { |
| 890 | Py_DECREF(statement); |
| 891 | statement = 0; |
| 892 | goto error; |
| 893 | } |
| 894 | |
| 895 | if (PyList_Append(self->statements, weakref) != 0) { |
| 896 | Py_DECREF(weakref); |
| 897 | statement = 0; |
| 898 | goto error; |
| 899 | } |
| 900 | |
| 901 | Py_DECREF(weakref); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 904 | error: |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 905 | return (PyObject*)statement; |
| 906 | } |
| 907 | |
| 908 | PyObject* connection_execute(Connection* self, PyObject* args, PyObject* kwargs) |
| 909 | { |
| 910 | PyObject* cursor = 0; |
| 911 | PyObject* result = 0; |
| 912 | PyObject* method = 0; |
| 913 | |
| 914 | cursor = PyObject_CallMethod((PyObject*)self, "cursor", ""); |
| 915 | if (!cursor) { |
| 916 | goto error; |
| 917 | } |
| 918 | |
| 919 | method = PyObject_GetAttrString(cursor, "execute"); |
| 920 | if (!method) { |
| 921 | Py_DECREF(cursor); |
| 922 | cursor = 0; |
| 923 | goto error; |
| 924 | } |
| 925 | |
| 926 | result = PyObject_CallObject(method, args); |
| 927 | if (!result) { |
| 928 | Py_DECREF(cursor); |
| 929 | cursor = 0; |
| 930 | } |
| 931 | |
| 932 | error: |
| 933 | Py_XDECREF(result); |
| 934 | Py_XDECREF(method); |
| 935 | |
| 936 | return cursor; |
| 937 | } |
| 938 | |
| 939 | PyObject* connection_executemany(Connection* self, PyObject* args, PyObject* kwargs) |
| 940 | { |
| 941 | PyObject* cursor = 0; |
| 942 | PyObject* result = 0; |
| 943 | PyObject* method = 0; |
| 944 | |
| 945 | cursor = PyObject_CallMethod((PyObject*)self, "cursor", ""); |
| 946 | if (!cursor) { |
| 947 | goto error; |
| 948 | } |
| 949 | |
| 950 | method = PyObject_GetAttrString(cursor, "executemany"); |
| 951 | if (!method) { |
| 952 | Py_DECREF(cursor); |
| 953 | cursor = 0; |
| 954 | goto error; |
| 955 | } |
| 956 | |
| 957 | result = PyObject_CallObject(method, args); |
| 958 | if (!result) { |
| 959 | Py_DECREF(cursor); |
| 960 | cursor = 0; |
| 961 | } |
| 962 | |
| 963 | error: |
| 964 | Py_XDECREF(result); |
| 965 | Py_XDECREF(method); |
| 966 | |
| 967 | return cursor; |
| 968 | } |
| 969 | |
| 970 | PyObject* connection_executescript(Connection* self, PyObject* args, PyObject* kwargs) |
| 971 | { |
| 972 | PyObject* cursor = 0; |
| 973 | PyObject* result = 0; |
| 974 | PyObject* method = 0; |
| 975 | |
| 976 | cursor = PyObject_CallMethod((PyObject*)self, "cursor", ""); |
| 977 | if (!cursor) { |
| 978 | goto error; |
| 979 | } |
| 980 | |
| 981 | method = PyObject_GetAttrString(cursor, "executescript"); |
| 982 | if (!method) { |
| 983 | Py_DECREF(cursor); |
| 984 | cursor = 0; |
| 985 | goto error; |
| 986 | } |
| 987 | |
| 988 | result = PyObject_CallObject(method, args); |
| 989 | if (!result) { |
| 990 | Py_DECREF(cursor); |
| 991 | cursor = 0; |
| 992 | } |
| 993 | |
| 994 | error: |
| 995 | Py_XDECREF(result); |
| 996 | Py_XDECREF(method); |
| 997 | |
| 998 | return cursor; |
| 999 | } |
| 1000 | |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 1001 | /* ------------------------- COLLATION CODE ------------------------ */ |
| 1002 | |
| 1003 | static int |
| 1004 | collation_callback( |
| 1005 | void* context, |
| 1006 | int text1_length, const void* text1_data, |
| 1007 | int text2_length, const void* text2_data) |
| 1008 | { |
| 1009 | PyObject* callback = (PyObject*)context; |
| 1010 | PyObject* string1 = 0; |
| 1011 | PyObject* string2 = 0; |
| 1012 | PyGILState_STATE gilstate; |
| 1013 | |
| 1014 | PyObject* retval = NULL; |
| 1015 | int result = 0; |
| 1016 | |
| 1017 | gilstate = PyGILState_Ensure(); |
| 1018 | |
| 1019 | if (PyErr_Occurred()) { |
| 1020 | goto finally; |
| 1021 | } |
| 1022 | |
| 1023 | string1 = PyString_FromStringAndSize((const char*)text1_data, text1_length); |
| 1024 | string2 = PyString_FromStringAndSize((const char*)text2_data, text2_length); |
| 1025 | |
| 1026 | if (!string1 || !string2) { |
| 1027 | goto finally; /* failed to allocate strings */ |
| 1028 | } |
| 1029 | |
| 1030 | retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL); |
| 1031 | |
| 1032 | if (!retval) { |
| 1033 | /* execution failed */ |
| 1034 | goto finally; |
| 1035 | } |
| 1036 | |
| 1037 | result = PyInt_AsLong(retval); |
| 1038 | if (PyErr_Occurred()) { |
| 1039 | result = 0; |
| 1040 | } |
| 1041 | |
| 1042 | finally: |
| 1043 | Py_XDECREF(string1); |
| 1044 | Py_XDECREF(string2); |
| 1045 | Py_XDECREF(retval); |
| 1046 | |
| 1047 | PyGILState_Release(gilstate); |
| 1048 | |
| 1049 | return result; |
| 1050 | } |
| 1051 | |
| 1052 | static PyObject * |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame^] | 1053 | connection_interrupt(Connection* self, PyObject* args) |
| 1054 | { |
| 1055 | PyObject* retval = NULL; |
| 1056 | |
| 1057 | if (!check_connection(self)) { |
| 1058 | goto finally; |
| 1059 | } |
| 1060 | |
| 1061 | sqlite3_interrupt(self->db); |
| 1062 | |
| 1063 | Py_INCREF(Py_None); |
| 1064 | retval = Py_None; |
| 1065 | |
| 1066 | finally: |
| 1067 | return retval; |
| 1068 | } |
| 1069 | |
| 1070 | static PyObject * |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 1071 | connection_create_collation(Connection* self, PyObject* args) |
| 1072 | { |
| 1073 | PyObject* callable; |
| 1074 | PyObject* uppercase_name = 0; |
| 1075 | PyObject* name; |
| 1076 | PyObject* retval; |
| 1077 | char* chk; |
| 1078 | int rc; |
| 1079 | |
| 1080 | if (!check_thread(self) || !check_connection(self)) { |
| 1081 | goto finally; |
| 1082 | } |
| 1083 | |
| 1084 | if (!PyArg_ParseTuple(args, "O!O:create_collation(name, callback)", &PyString_Type, &name, &callable)) { |
| 1085 | goto finally; |
| 1086 | } |
| 1087 | |
| 1088 | uppercase_name = PyObject_CallMethod(name, "upper", ""); |
| 1089 | if (!uppercase_name) { |
| 1090 | goto finally; |
| 1091 | } |
| 1092 | |
| 1093 | chk = PyString_AsString(uppercase_name); |
| 1094 | while (*chk) { |
| 1095 | if ((*chk >= '0' && *chk <= '9') |
| 1096 | || (*chk >= 'A' && *chk <= 'Z') |
| 1097 | || (*chk == '_')) |
| 1098 | { |
| 1099 | chk++; |
| 1100 | } else { |
| 1101 | PyErr_SetString(ProgrammingError, "invalid character in collation name"); |
| 1102 | goto finally; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | if (callable != Py_None && !PyCallable_Check(callable)) { |
| 1107 | PyErr_SetString(PyExc_TypeError, "parameter must be callable"); |
| 1108 | goto finally; |
| 1109 | } |
| 1110 | |
| 1111 | if (callable != Py_None) { |
| 1112 | PyDict_SetItem(self->collations, uppercase_name, callable); |
| 1113 | } else { |
| 1114 | PyDict_DelItem(self->collations, uppercase_name); |
| 1115 | } |
| 1116 | |
| 1117 | rc = sqlite3_create_collation(self->db, |
| 1118 | PyString_AsString(uppercase_name), |
| 1119 | SQLITE_UTF8, |
| 1120 | (callable != Py_None) ? callable : NULL, |
| 1121 | (callable != Py_None) ? collation_callback : NULL); |
| 1122 | if (rc != SQLITE_OK) { |
| 1123 | PyDict_DelItem(self->collations, uppercase_name); |
| 1124 | _seterror(self->db); |
| 1125 | goto finally; |
| 1126 | } |
| 1127 | |
| 1128 | finally: |
| 1129 | Py_XDECREF(uppercase_name); |
| 1130 | |
| 1131 | if (PyErr_Occurred()) { |
| 1132 | retval = NULL; |
| 1133 | } else { |
| 1134 | Py_INCREF(Py_None); |
| 1135 | retval = Py_None; |
| 1136 | } |
| 1137 | |
| 1138 | return retval; |
| 1139 | } |
| 1140 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1141 | static char connection_doc[] = |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 1142 | PyDoc_STR("SQLite database connection object."); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1143 | |
| 1144 | static PyGetSetDef connection_getset[] = { |
| 1145 | {"isolation_level", (getter)connection_get_isolation_level, (setter)connection_set_isolation_level}, |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 1146 | {"total_changes", (getter)connection_get_total_changes, (setter)0}, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1147 | {NULL} |
| 1148 | }; |
| 1149 | |
| 1150 | static PyMethodDef connection_methods[] = { |
| 1151 | {"cursor", (PyCFunction)connection_cursor, METH_VARARGS|METH_KEYWORDS, |
| 1152 | PyDoc_STR("Return a cursor for the connection.")}, |
| 1153 | {"close", (PyCFunction)connection_close, METH_NOARGS, |
| 1154 | PyDoc_STR("Closes the connection.")}, |
| 1155 | {"commit", (PyCFunction)connection_commit, METH_NOARGS, |
| 1156 | PyDoc_STR("Commit the current transaction.")}, |
| 1157 | {"rollback", (PyCFunction)connection_rollback, METH_NOARGS, |
| 1158 | PyDoc_STR("Roll back the current transaction.")}, |
| 1159 | {"create_function", (PyCFunction)connection_create_function, METH_VARARGS|METH_KEYWORDS, |
| 1160 | PyDoc_STR("Creates a new function. Non-standard.")}, |
| 1161 | {"create_aggregate", (PyCFunction)connection_create_aggregate, METH_VARARGS|METH_KEYWORDS, |
| 1162 | PyDoc_STR("Creates a new aggregate. Non-standard.")}, |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame^] | 1163 | {"set_authorizer", (PyCFunction)connection_set_authorizer, METH_VARARGS|METH_KEYWORDS, |
| 1164 | PyDoc_STR("Sets authorizer callback. Non-standard.")}, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1165 | {"execute", (PyCFunction)connection_execute, METH_VARARGS, |
| 1166 | PyDoc_STR("Executes a SQL statement. Non-standard.")}, |
| 1167 | {"executemany", (PyCFunction)connection_executemany, METH_VARARGS, |
| 1168 | PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")}, |
| 1169 | {"executescript", (PyCFunction)connection_executescript, METH_VARARGS, |
| 1170 | PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")}, |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 1171 | {"create_collation", (PyCFunction)connection_create_collation, METH_VARARGS, |
Gerhard Häring | 3e99c0a | 2006-04-23 15:24:26 +0000 | [diff] [blame] | 1172 | PyDoc_STR("Creates a collation function. Non-standard.")}, |
Gerhard Häring | 1541ef0 | 2006-06-13 22:24:47 +0000 | [diff] [blame^] | 1173 | {"interrupt", (PyCFunction)connection_interrupt, METH_NOARGS, |
| 1174 | PyDoc_STR("Abort any pending database operation. Non-standard.")}, |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1175 | {NULL, NULL} |
| 1176 | }; |
| 1177 | |
| 1178 | static struct PyMemberDef connection_members[] = |
| 1179 | { |
| 1180 | {"Warning", T_OBJECT, offsetof(Connection, Warning), RO}, |
| 1181 | {"Error", T_OBJECT, offsetof(Connection, Error), RO}, |
| 1182 | {"InterfaceError", T_OBJECT, offsetof(Connection, InterfaceError), RO}, |
| 1183 | {"DatabaseError", T_OBJECT, offsetof(Connection, DatabaseError), RO}, |
| 1184 | {"DataError", T_OBJECT, offsetof(Connection, DataError), RO}, |
| 1185 | {"OperationalError", T_OBJECT, offsetof(Connection, OperationalError), RO}, |
| 1186 | {"IntegrityError", T_OBJECT, offsetof(Connection, IntegrityError), RO}, |
| 1187 | {"InternalError", T_OBJECT, offsetof(Connection, InternalError), RO}, |
| 1188 | {"ProgrammingError", T_OBJECT, offsetof(Connection, ProgrammingError), RO}, |
| 1189 | {"NotSupportedError", T_OBJECT, offsetof(Connection, NotSupportedError), RO}, |
| 1190 | {"row_factory", T_OBJECT, offsetof(Connection, row_factory)}, |
| 1191 | {"text_factory", T_OBJECT, offsetof(Connection, text_factory)}, |
| 1192 | {NULL} |
| 1193 | }; |
| 1194 | |
| 1195 | PyTypeObject ConnectionType = { |
| 1196 | PyObject_HEAD_INIT(NULL) |
| 1197 | 0, /* ob_size */ |
Anthony Baxter | 8e7b490 | 2006-04-05 18:25:33 +0000 | [diff] [blame] | 1198 | MODULE_NAME ".Connection", /* tp_name */ |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1199 | sizeof(Connection), /* tp_basicsize */ |
| 1200 | 0, /* tp_itemsize */ |
| 1201 | (destructor)connection_dealloc, /* tp_dealloc */ |
| 1202 | 0, /* tp_print */ |
| 1203 | 0, /* tp_getattr */ |
| 1204 | 0, /* tp_setattr */ |
| 1205 | 0, /* tp_compare */ |
| 1206 | 0, /* tp_repr */ |
| 1207 | 0, /* tp_as_number */ |
| 1208 | 0, /* tp_as_sequence */ |
| 1209 | 0, /* tp_as_mapping */ |
| 1210 | 0, /* tp_hash */ |
| 1211 | (ternaryfunc)connection_call, /* tp_call */ |
| 1212 | 0, /* tp_str */ |
| 1213 | 0, /* tp_getattro */ |
| 1214 | 0, /* tp_setattro */ |
| 1215 | 0, /* tp_as_buffer */ |
| 1216 | Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1217 | connection_doc, /* tp_doc */ |
| 1218 | 0, /* tp_traverse */ |
| 1219 | 0, /* tp_clear */ |
| 1220 | 0, /* tp_richcompare */ |
| 1221 | 0, /* tp_weaklistoffset */ |
| 1222 | 0, /* tp_iter */ |
| 1223 | 0, /* tp_iternext */ |
| 1224 | connection_methods, /* tp_methods */ |
| 1225 | connection_members, /* tp_members */ |
| 1226 | connection_getset, /* tp_getset */ |
| 1227 | 0, /* tp_base */ |
| 1228 | 0, /* tp_dict */ |
| 1229 | 0, /* tp_descr_get */ |
| 1230 | 0, /* tp_descr_set */ |
| 1231 | 0, /* tp_dictoffset */ |
| 1232 | (initproc)connection_init, /* tp_init */ |
| 1233 | 0, /* tp_alloc */ |
| 1234 | 0, /* tp_new */ |
| 1235 | 0 /* tp_free */ |
| 1236 | }; |
| 1237 | |
| 1238 | extern int connection_setup_types(void) |
| 1239 | { |
| 1240 | ConnectionType.tp_new = PyType_GenericNew; |
| 1241 | return PyType_Ready(&ConnectionType); |
| 1242 | } |