blob: b371aed99dbe4dbbf37ad4ef1ffeae999147d2a2 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* util.c - various utility functions
2 *
Florent Xiclunac934f322010-09-03 23:47:32 +00003 * Copyright (C) 2005-2010 Gerhard Häring <gh@ghaering.de>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004 *
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
Benjamin Petersond7b03282008-09-13 15:58:53 +000027int pysqlite_step(sqlite3_stmt* statement, pysqlite_Connection* connection)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000028{
29 int rc;
30
Christian Heimes38053212007-12-14 01:24:44 +000031 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 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000040
41 return rc;
42}
43
44/**
45 * Checks the SQLite error code and sets the appropriate DB-API exception.
Thomas Wouters0e3f5912006-08-11 14:57:12 +000046 * Returns the error code (0 means no error occurred).
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000047 */
Gerhard Häringe7ea7452008-03-29 00:45:29 +000048int _pysqlite_seterror(sqlite3* db, sqlite3_stmt* st)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000049{
50 int errorcode;
51
Aviv Palivoda86a67052017-03-03 12:58:17 +020052#if SQLITE_VERSION_NUMBER < 3003009
53 /* SQLite often doesn't report anything useful, unless you reset the statement first.
54 When using sqlite3_prepare_v2 this is not needed. */
Gerhard Häringe7ea7452008-03-29 00:45:29 +000055 if (st != NULL) {
56 (void)sqlite3_reset(st);
57 }
Aviv Palivoda86a67052017-03-03 12:58:17 +020058#endif
Gerhard Häringe7ea7452008-03-29 00:45:29 +000059
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000060 errorcode = sqlite3_errcode(db);
61
62 switch (errorcode)
63 {
64 case SQLITE_OK:
65 PyErr_Clear();
66 break;
67 case SQLITE_INTERNAL:
68 case SQLITE_NOTFOUND:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000069 PyErr_SetString(pysqlite_InternalError, sqlite3_errmsg(db));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000070 break;
71 case SQLITE_NOMEM:
72 (void)PyErr_NoMemory();
73 break;
74 case SQLITE_ERROR:
75 case SQLITE_PERM:
76 case SQLITE_ABORT:
77 case SQLITE_BUSY:
78 case SQLITE_LOCKED:
79 case SQLITE_READONLY:
80 case SQLITE_INTERRUPT:
81 case SQLITE_IOERR:
82 case SQLITE_FULL:
83 case SQLITE_CANTOPEN:
84 case SQLITE_PROTOCOL:
85 case SQLITE_EMPTY:
86 case SQLITE_SCHEMA:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000087 PyErr_SetString(pysqlite_OperationalError, sqlite3_errmsg(db));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000088 break;
89 case SQLITE_CORRUPT:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000090 PyErr_SetString(pysqlite_DatabaseError, sqlite3_errmsg(db));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000091 break;
92 case SQLITE_TOOBIG:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000093 PyErr_SetString(pysqlite_DataError, sqlite3_errmsg(db));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000094 break;
95 case SQLITE_CONSTRAINT:
96 case SQLITE_MISMATCH:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +000097 PyErr_SetString(pysqlite_IntegrityError, sqlite3_errmsg(db));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000098 break;
99 case SQLITE_MISUSE:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000100 PyErr_SetString(pysqlite_ProgrammingError, sqlite3_errmsg(db));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000101 break;
102 default:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000103 PyErr_SetString(pysqlite_DatabaseError, sqlite3_errmsg(db));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000104 break;
105 }
106
107 return errorcode;
108}
109
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200110#ifdef WORDS_BIGENDIAN
111# define IS_LITTLE_ENDIAN 0
112#else
113# define IS_LITTLE_ENDIAN 1
114#endif
115
116PyObject *
Serhiy Storchakad160b122013-04-28 14:10:27 +0300117_pysqlite_long_from_int64(sqlite_int64 value)
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200118{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200119# if SIZEOF_LONG_LONG < 8
120 if (value > PY_LLONG_MAX || value < PY_LLONG_MIN) {
121 return _PyLong_FromByteArray(&value, sizeof(value),
122 IS_LITTLE_ENDIAN, 1 /* signed */);
123 }
124# endif
125# if SIZEOF_LONG < SIZEOF_LONG_LONG
126 if (value > LONG_MAX || value < LONG_MIN)
127 return PyLong_FromLongLong(value);
128# endif
Victor Stinner725e4212013-05-17 00:19:59 +0200129 return PyLong_FromLong(Py_SAFE_DOWNCAST(value, sqlite_int64, long));
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200130}
131
Serhiy Storchakad160b122013-04-28 14:10:27 +0300132sqlite_int64
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200133_pysqlite_long_as_int64(PyObject * py_val)
134{
135 int overflow;
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700136 long long value = PyLong_AsLongLongAndOverflow(py_val, &overflow);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200137 if (value == -1 && PyErr_Occurred())
138 return -1;
139 if (!overflow) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200140# if SIZEOF_LONG_LONG > 8
141 if (-0x8000000000000000LL <= value && value <= 0x7FFFFFFFFFFFFFFFLL)
142# endif
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200143 return value;
144 }
Serhiy Storchakad160b122013-04-28 14:10:27 +0300145 else if (sizeof(value) < sizeof(sqlite_int64)) {
146 sqlite_int64 int64val;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200147 if (_PyLong_AsByteArray((PyLongObject *)py_val,
148 (unsigned char *)&int64val, sizeof(int64val),
149 IS_LITTLE_ENDIAN, 1 /* signed */) >= 0) {
150 return int64val;
151 }
152 }
153 PyErr_SetString(PyExc_OverflowError,
154 "Python int too large to convert to SQLite INTEGER");
155 return -1;
156}