Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 1 | /* util.c - various utility functions |
| 2 | * |
Gerhard Häring | 3bbb672 | 2010-03-05 09:12:37 +0000 | [diff] [blame] | 3 | * Copyright (C) 2005-2010 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 | |
Gerhard Häring | 6e1afcf | 2008-09-12 18:58:57 +0000 | [diff] [blame] | 27 | int pysqlite_step(sqlite3_stmt* statement, pysqlite_Connection* connection) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 28 | { |
| 29 | int rc; |
| 30 | |
Gerhard Häring | 99b9df8 | 2007-12-11 21:07:40 +0000 | [diff] [blame] | 31 | if (statement == NULL) { |
| 32 | /* this is a workaround for SQLite 3.5 and later. it now apparently |
| 33 | * returns NULL for "no-operation" statements */ |
| 34 | rc = SQLITE_OK; |
| 35 | } else { |
| 36 | Py_BEGIN_ALLOW_THREADS |
| 37 | rc = sqlite3_step(statement); |
| 38 | Py_END_ALLOW_THREADS |
| 39 | } |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 40 | |
| 41 | return rc; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Checks the SQLite error code and sets the appropriate DB-API exception. |
Georg Brandl | cddabbf | 2006-07-28 18:36:01 +0000 | [diff] [blame] | 46 | * Returns the error code (0 means no error occurred). |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 47 | */ |
Gerhard Häring | 1cc60ed | 2008-02-29 22:08:41 +0000 | [diff] [blame] | 48 | int _pysqlite_seterror(sqlite3* db, sqlite3_stmt* st) |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 49 | { |
| 50 | int errorcode; |
| 51 | |
Gerhard Häring | 1cc60ed | 2008-02-29 22:08:41 +0000 | [diff] [blame] | 52 | /* SQLite often doesn't report anything useful, unless you reset the statement first */ |
| 53 | if (st != NULL) { |
| 54 | (void)sqlite3_reset(st); |
| 55 | } |
| 56 | |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 57 | errorcode = sqlite3_errcode(db); |
| 58 | |
| 59 | switch (errorcode) |
| 60 | { |
| 61 | case SQLITE_OK: |
| 62 | PyErr_Clear(); |
| 63 | break; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 64 | case SQLITE_INTERNAL: |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 65 | case SQLITE_NOTFOUND: |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 66 | PyErr_SetString(pysqlite_InternalError, sqlite3_errmsg(db)); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 67 | break; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 68 | case SQLITE_NOMEM: |
| 69 | (void)PyErr_NoMemory(); |
| 70 | break; |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 71 | case SQLITE_ERROR: |
| 72 | case SQLITE_PERM: |
| 73 | case SQLITE_ABORT: |
| 74 | case SQLITE_BUSY: |
| 75 | case SQLITE_LOCKED: |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 76 | case SQLITE_READONLY: |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 77 | case SQLITE_INTERRUPT: |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 78 | case SQLITE_IOERR: |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 79 | case SQLITE_FULL: |
| 80 | case SQLITE_CANTOPEN: |
| 81 | case SQLITE_PROTOCOL: |
| 82 | case SQLITE_EMPTY: |
| 83 | case SQLITE_SCHEMA: |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 84 | PyErr_SetString(pysqlite_OperationalError, sqlite3_errmsg(db)); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 85 | break; |
| 86 | case SQLITE_CORRUPT: |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 87 | PyErr_SetString(pysqlite_DatabaseError, sqlite3_errmsg(db)); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 88 | break; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 89 | case SQLITE_TOOBIG: |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 90 | PyErr_SetString(pysqlite_DataError, sqlite3_errmsg(db)); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 91 | break; |
| 92 | case SQLITE_CONSTRAINT: |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 93 | case SQLITE_MISMATCH: |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 94 | PyErr_SetString(pysqlite_IntegrityError, sqlite3_errmsg(db)); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 95 | break; |
| 96 | case SQLITE_MISUSE: |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 97 | PyErr_SetString(pysqlite_ProgrammingError, sqlite3_errmsg(db)); |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 98 | break; |
| 99 | default: |
Gerhard Häring | 0741a60 | 2007-01-14 01:43:50 +0000 | [diff] [blame] | 100 | PyErr_SetString(pysqlite_DatabaseError, sqlite3_errmsg(db)); |
Anthony Baxter | 72289a6 | 2006-04-04 06:29:05 +0000 | [diff] [blame] | 101 | break; |
Anthony Baxter | c51ee69 | 2006-04-01 00:57:31 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | return errorcode; |
| 105 | } |
| 106 | |
Serhiy Storchaka | 35c52b6 | 2013-02-07 16:59:34 +0200 | [diff] [blame] | 107 | #ifdef WORDS_BIGENDIAN |
| 108 | # define IS_LITTLE_ENDIAN 0 |
| 109 | #else |
| 110 | # define IS_LITTLE_ENDIAN 1 |
| 111 | #endif |
| 112 | |
| 113 | PyObject * |
Serhiy Storchaka | 8493a04 | 2013-04-28 14:09:47 +0300 | [diff] [blame] | 114 | _pysqlite_long_from_int64(sqlite_int64 value) |
Serhiy Storchaka | 35c52b6 | 2013-02-07 16:59:34 +0200 | [diff] [blame] | 115 | { |
| 116 | #ifdef HAVE_LONG_LONG |
| 117 | # if SIZEOF_LONG_LONG < 8 |
| 118 | if (value > PY_LLONG_MAX || value < PY_LLONG_MIN) { |
| 119 | return _PyLong_FromByteArray(&value, sizeof(value), |
| 120 | IS_LITTLE_ENDIAN, 1 /* signed */); |
| 121 | } |
| 122 | # endif |
| 123 | # if SIZEOF_LONG < SIZEOF_LONG_LONG |
| 124 | if (value > LONG_MAX || value < LONG_MIN) |
| 125 | return PyLong_FromLongLong(value); |
| 126 | # endif |
| 127 | #else |
| 128 | # if SIZEOF_LONG < 8 |
| 129 | if (value > LONG_MAX || value < LONG_MIN) { |
| 130 | return _PyLong_FromByteArray(&value, sizeof(value), |
| 131 | IS_LITTLE_ENDIAN, 1 /* signed */); |
| 132 | } |
| 133 | # endif |
| 134 | #endif |
| 135 | return PyInt_FromLong(value); |
| 136 | } |
| 137 | |
Serhiy Storchaka | 8493a04 | 2013-04-28 14:09:47 +0300 | [diff] [blame] | 138 | sqlite_int64 |
Serhiy Storchaka | 35c52b6 | 2013-02-07 16:59:34 +0200 | [diff] [blame] | 139 | _pysqlite_long_as_int64(PyObject * py_val) |
| 140 | { |
| 141 | int overflow; |
| 142 | #ifdef HAVE_LONG_LONG |
| 143 | PY_LONG_LONG value = PyLong_AsLongLongAndOverflow(py_val, &overflow); |
| 144 | #else |
| 145 | long value = PyLong_AsLongAndOverflow(py_val, &overflow); |
| 146 | #endif |
| 147 | if (value == -1 && PyErr_Occurred()) |
| 148 | return -1; |
| 149 | if (!overflow) { |
| 150 | #ifdef HAVE_LONG_LONG |
| 151 | # if SIZEOF_LONG_LONG > 8 |
| 152 | if (-0x8000000000000000LL <= value && value <= 0x7FFFFFFFFFFFFFFFLL) |
| 153 | # endif |
| 154 | #else |
| 155 | # if SIZEOF_LONG > 8 |
| 156 | if (-0x8000000000000000L <= value && value <= 0x7FFFFFFFFFFFFFFFL) |
| 157 | # endif |
| 158 | #endif |
| 159 | return value; |
| 160 | } |
Serhiy Storchaka | 8493a04 | 2013-04-28 14:09:47 +0300 | [diff] [blame] | 161 | else if (sizeof(value) < sizeof(sqlite_int64)) { |
| 162 | sqlite_int64 int64val; |
Serhiy Storchaka | 35c52b6 | 2013-02-07 16:59:34 +0200 | [diff] [blame] | 163 | if (_PyLong_AsByteArray((PyLongObject *)py_val, |
| 164 | (unsigned char *)&int64val, sizeof(int64val), |
| 165 | IS_LITTLE_ENDIAN, 1 /* signed */) >= 0) { |
| 166 | return int64val; |
| 167 | } |
| 168 | } |
| 169 | PyErr_SetString(PyExc_OverflowError, |
| 170 | "Python int too large to convert to SQLite INTEGER"); |
| 171 | return -1; |
| 172 | } |