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