Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1 | /* util.c - various utility functions |
| 2 | * |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 3 | * Copyright (C) 2005-2006 Gerhard Häring <gh@ghaering.de> |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 4 | * |
| 5 | * This file is part of pysqlite. |
| 6 | * |
| 7 | * This software is provided 'as-is', without any express or implied |
| 8 | * warranty. In no event will the authors be held liable for any damages |
| 9 | * arising from the use of this software. |
| 10 | * |
| 11 | * Permission is granted to anyone to use this software for any purpose, |
| 12 | * including commercial applications, and to alter it and redistribute it |
| 13 | * freely, subject to the following restrictions: |
| 14 | * |
| 15 | * 1. The origin of this software must not be misrepresented; you must not |
| 16 | * claim that you wrote the original software. If you use this software |
| 17 | * in a product, an acknowledgment in the product documentation would be |
| 18 | * appreciated but is not required. |
| 19 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 20 | * misrepresented as being the original software. |
| 21 | * 3. This notice may not be removed or altered from any source distribution. |
| 22 | */ |
| 23 | |
| 24 | #include "module.h" |
| 25 | #include "connection.h" |
| 26 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 27 | int _sqlite_step_with_busyhandler(sqlite3_stmt* statement, Connection* connection |
| 28 | ) |
| 29 | { |
| 30 | int rc; |
| 31 | |
Gerhard Häring | 14fbf29 | 2007-11-25 17:40:35 +0000 | [diff] [blame] | 32 | if (statement == NULL) { |
| 33 | /* this is a workaround for SQLite 3.5 and later. it now apparently |
| 34 | * returns NULL for "no-operation" statements */ |
| 35 | rc = SQLITE_OK; |
| 36 | } else { |
| 37 | Py_BEGIN_ALLOW_THREADS |
| 38 | rc = sqlite3_step(statement); |
| 39 | Py_END_ALLOW_THREADS |
| 40 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 41 | |
| 42 | return rc; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Checks the SQLite error code and sets the appropriate DB-API exception. |
Georg Brandl | cddabbf | 2006-07-28 18:36:01 +0000 | [diff] [blame] | 47 | * Returns the error code (0 means no error occurred). |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 48 | */ |
| 49 | int _seterror(sqlite3* db) |
| 50 | { |
| 51 | int errorcode; |
| 52 | |
| 53 | errorcode = sqlite3_errcode(db); |
| 54 | |
| 55 | switch (errorcode) |
| 56 | { |
| 57 | case SQLITE_OK: |
| 58 | PyErr_Clear(); |
| 59 | break; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 60 | case SQLITE_INTERNAL: |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 61 | case SQLITE_NOTFOUND: |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 62 | PyErr_SetString(InternalError, sqlite3_errmsg(db)); |
| 63 | break; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 64 | case SQLITE_NOMEM: |
| 65 | (void)PyErr_NoMemory(); |
| 66 | break; |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 67 | case SQLITE_ERROR: |
| 68 | case SQLITE_PERM: |
| 69 | case SQLITE_ABORT: |
| 70 | case SQLITE_BUSY: |
| 71 | case SQLITE_LOCKED: |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 72 | case SQLITE_READONLY: |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 73 | case SQLITE_INTERRUPT: |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 74 | case SQLITE_IOERR: |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 75 | case SQLITE_FULL: |
| 76 | case SQLITE_CANTOPEN: |
| 77 | case SQLITE_PROTOCOL: |
| 78 | case SQLITE_EMPTY: |
| 79 | case SQLITE_SCHEMA: |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 80 | PyErr_SetString(OperationalError, sqlite3_errmsg(db)); |
| 81 | break; |
| 82 | case SQLITE_CORRUPT: |
| 83 | PyErr_SetString(DatabaseError, sqlite3_errmsg(db)); |
| 84 | break; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 85 | case SQLITE_TOOBIG: |
| 86 | PyErr_SetString(DataError, sqlite3_errmsg(db)); |
| 87 | break; |
| 88 | case SQLITE_CONSTRAINT: |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 89 | case SQLITE_MISMATCH: |
| 90 | PyErr_SetString(IntegrityError, sqlite3_errmsg(db)); |
| 91 | break; |
| 92 | case SQLITE_MISUSE: |
| 93 | PyErr_SetString(ProgrammingError, sqlite3_errmsg(db)); |
| 94 | break; |
| 95 | default: |
| 96 | PyErr_SetString(DatabaseError, sqlite3_errmsg(db)); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 97 | break; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | return errorcode; |
| 101 | } |
| 102 | |