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