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