| 1 | n/a | /* util.c - various utility functions |
|---|
| 2 | n/a | * |
|---|
| 3 | n/a | * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de> |
|---|
| 4 | n/a | * |
|---|
| 5 | n/a | * This file is part of pysqlite. |
|---|
| 6 | n/a | * |
|---|
| 7 | n/a | * This software is provided 'as-is', without any express or implied |
|---|
| 8 | n/a | * warranty. In no event will the authors be held liable for any damages |
|---|
| 9 | n/a | * arising from the use of this software. |
|---|
| 10 | n/a | * |
|---|
| 11 | n/a | * Permission is granted to anyone to use this software for any purpose, |
|---|
| 12 | n/a | * including commercial applications, and to alter it and redistribute it |
|---|
| 13 | n/a | * freely, subject to the following restrictions: |
|---|
| 14 | n/a | * |
|---|
| 15 | n/a | * 1. The origin of this software must not be misrepresented; you must not |
|---|
| 16 | n/a | * claim that you wrote the original software. If you use this software |
|---|
| 17 | n/a | * in a product, an acknowledgment in the product documentation would be |
|---|
| 18 | n/a | * appreciated but is not required. |
|---|
| 19 | n/a | * 2. Altered source versions must be plainly marked as such, and must not be |
|---|
| 20 | n/a | * misrepresented as being the original software. |
|---|
| 21 | n/a | * 3. This notice may not be removed or altered from any source distribution. |
|---|
| 22 | n/a | */ |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | #include "module.h" |
|---|
| 25 | n/a | #include "connection.h" |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | int pysqlite_step(sqlite3_stmt* statement, pysqlite_Connection* connection) |
|---|
| 28 | n/a | { |
|---|
| 29 | n/a | int rc; |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | if (statement == NULL) { |
|---|
| 32 | n/a | /* this is a workaround for SQLite 3.5 and later. it now apparently |
|---|
| 33 | n/a | * returns NULL for "no-operation" statements */ |
|---|
| 34 | n/a | rc = SQLITE_OK; |
|---|
| 35 | n/a | } else { |
|---|
| 36 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 37 | n/a | rc = sqlite3_step(statement); |
|---|
| 38 | n/a | Py_END_ALLOW_THREADS |
|---|
| 39 | n/a | } |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | return rc; |
|---|
| 42 | n/a | } |
|---|
| 43 | n/a | |
|---|
| 44 | n/a | /** |
|---|
| 45 | n/a | * Checks the SQLite error code and sets the appropriate DB-API exception. |
|---|
| 46 | n/a | * Returns the error code (0 means no error occurred). |
|---|
| 47 | n/a | */ |
|---|
| 48 | n/a | int _pysqlite_seterror(sqlite3* db, sqlite3_stmt* st) |
|---|
| 49 | n/a | { |
|---|
| 50 | n/a | int errorcode; |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | /* SQLite often doesn't report anything useful, unless you reset the statement first */ |
|---|
| 53 | n/a | if (st != NULL) { |
|---|
| 54 | n/a | (void)sqlite3_reset(st); |
|---|
| 55 | n/a | } |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | errorcode = sqlite3_errcode(db); |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | switch (errorcode) |
|---|
| 60 | n/a | { |
|---|
| 61 | n/a | case SQLITE_OK: |
|---|
| 62 | n/a | PyErr_Clear(); |
|---|
| 63 | n/a | break; |
|---|
| 64 | n/a | case SQLITE_INTERNAL: |
|---|
| 65 | n/a | case SQLITE_NOTFOUND: |
|---|
| 66 | n/a | PyErr_SetString(pysqlite_InternalError, sqlite3_errmsg(db)); |
|---|
| 67 | n/a | break; |
|---|
| 68 | n/a | case SQLITE_NOMEM: |
|---|
| 69 | n/a | (void)PyErr_NoMemory(); |
|---|
| 70 | n/a | break; |
|---|
| 71 | n/a | case SQLITE_ERROR: |
|---|
| 72 | n/a | case SQLITE_PERM: |
|---|
| 73 | n/a | case SQLITE_ABORT: |
|---|
| 74 | n/a | case SQLITE_BUSY: |
|---|
| 75 | n/a | case SQLITE_LOCKED: |
|---|
| 76 | n/a | case SQLITE_READONLY: |
|---|
| 77 | n/a | case SQLITE_INTERRUPT: |
|---|
| 78 | n/a | case SQLITE_IOERR: |
|---|
| 79 | n/a | case SQLITE_FULL: |
|---|
| 80 | n/a | case SQLITE_CANTOPEN: |
|---|
| 81 | n/a | case SQLITE_PROTOCOL: |
|---|
| 82 | n/a | case SQLITE_EMPTY: |
|---|
| 83 | n/a | case SQLITE_SCHEMA: |
|---|
| 84 | n/a | PyErr_SetString(pysqlite_OperationalError, sqlite3_errmsg(db)); |
|---|
| 85 | n/a | break; |
|---|
| 86 | n/a | case SQLITE_CORRUPT: |
|---|
| 87 | n/a | PyErr_SetString(pysqlite_DatabaseError, sqlite3_errmsg(db)); |
|---|
| 88 | n/a | break; |
|---|
| 89 | n/a | case SQLITE_TOOBIG: |
|---|
| 90 | n/a | PyErr_SetString(pysqlite_DataError, sqlite3_errmsg(db)); |
|---|
| 91 | n/a | break; |
|---|
| 92 | n/a | case SQLITE_CONSTRAINT: |
|---|
| 93 | n/a | case SQLITE_MISMATCH: |
|---|
| 94 | n/a | PyErr_SetString(pysqlite_IntegrityError, sqlite3_errmsg(db)); |
|---|
| 95 | n/a | break; |
|---|
| 96 | n/a | case SQLITE_MISUSE: |
|---|
| 97 | n/a | PyErr_SetString(pysqlite_ProgrammingError, sqlite3_errmsg(db)); |
|---|
| 98 | n/a | break; |
|---|
| 99 | n/a | default: |
|---|
| 100 | n/a | PyErr_SetString(pysqlite_DatabaseError, sqlite3_errmsg(db)); |
|---|
| 101 | n/a | break; |
|---|
| 102 | n/a | } |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | return errorcode; |
|---|
| 105 | n/a | } |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | #ifdef WORDS_BIGENDIAN |
|---|
| 108 | n/a | # define IS_LITTLE_ENDIAN 0 |
|---|
| 109 | n/a | #else |
|---|
| 110 | n/a | # define IS_LITTLE_ENDIAN 1 |
|---|
| 111 | n/a | #endif |
|---|
| 112 | n/a | |
|---|
| 113 | n/a | PyObject * |
|---|
| 114 | n/a | _pysqlite_long_from_int64(sqlite_int64 value) |
|---|
| 115 | n/a | { |
|---|
| 116 | n/a | # if SIZEOF_LONG_LONG < 8 |
|---|
| 117 | n/a | if (value > PY_LLONG_MAX || value < PY_LLONG_MIN) { |
|---|
| 118 | n/a | return _PyLong_FromByteArray(&value, sizeof(value), |
|---|
| 119 | n/a | IS_LITTLE_ENDIAN, 1 /* signed */); |
|---|
| 120 | n/a | } |
|---|
| 121 | n/a | # endif |
|---|
| 122 | n/a | # if SIZEOF_LONG < SIZEOF_LONG_LONG |
|---|
| 123 | n/a | if (value > LONG_MAX || value < LONG_MIN) |
|---|
| 124 | n/a | return PyLong_FromLongLong(value); |
|---|
| 125 | n/a | # endif |
|---|
| 126 | n/a | return PyLong_FromLong(Py_SAFE_DOWNCAST(value, sqlite_int64, long)); |
|---|
| 127 | n/a | } |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | sqlite_int64 |
|---|
| 130 | n/a | _pysqlite_long_as_int64(PyObject * py_val) |
|---|
| 131 | n/a | { |
|---|
| 132 | n/a | int overflow; |
|---|
| 133 | n/a | long long value = PyLong_AsLongLongAndOverflow(py_val, &overflow); |
|---|
| 134 | n/a | if (value == -1 && PyErr_Occurred()) |
|---|
| 135 | n/a | return -1; |
|---|
| 136 | n/a | if (!overflow) { |
|---|
| 137 | n/a | # if SIZEOF_LONG_LONG > 8 |
|---|
| 138 | n/a | if (-0x8000000000000000LL <= value && value <= 0x7FFFFFFFFFFFFFFFLL) |
|---|
| 139 | n/a | # endif |
|---|
| 140 | n/a | return value; |
|---|
| 141 | n/a | } |
|---|
| 142 | n/a | else if (sizeof(value) < sizeof(sqlite_int64)) { |
|---|
| 143 | n/a | sqlite_int64 int64val; |
|---|
| 144 | n/a | if (_PyLong_AsByteArray((PyLongObject *)py_val, |
|---|
| 145 | n/a | (unsigned char *)&int64val, sizeof(int64val), |
|---|
| 146 | n/a | IS_LITTLE_ENDIAN, 1 /* signed */) >= 0) { |
|---|
| 147 | n/a | return int64val; |
|---|
| 148 | n/a | } |
|---|
| 149 | n/a | } |
|---|
| 150 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 151 | n/a | "Python int too large to convert to SQLite INTEGER"); |
|---|
| 152 | n/a | return -1; |
|---|
| 153 | n/a | } |
|---|