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