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