1/* util.c - various utility functions
2 *
3 * Copyright (C) 2005-2010 Gerhard Häring <[email protected]>
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
27int pysqlite_step(sqlite3_stmt* statement, pysqlite_Connection* connection)
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.
40 * Returns the error code (0 means no error occurred).
41 */
42int _pysqlite_seterror(sqlite3* db, sqlite3_stmt* st)
43{
44 int errorcode = sqlite3_errcode(db);
45
46 switch (errorcode)
47 {
48 case SQLITE_OK:
49 PyErr_Clear();
50 break;
51 case SQLITE_INTERNAL:
52 case SQLITE_NOTFOUND:
53 PyErr_SetString(pysqlite_InternalError, sqlite3_errmsg(db));
54 break;
55 case SQLITE_NOMEM:
56 (void)PyErr_NoMemory();
57 break;
58 case SQLITE_ERROR:
59 case SQLITE_PERM:
60 case SQLITE_ABORT:
61 case SQLITE_BUSY:
62 case SQLITE_LOCKED:
63 case SQLITE_READONLY:
64 case SQLITE_INTERRUPT:
65 case SQLITE_IOERR:
66 case SQLITE_FULL:
67 case SQLITE_CANTOPEN:
68 case SQLITE_PROTOCOL:
69 case SQLITE_EMPTY:
70 case SQLITE_SCHEMA:
71 PyErr_SetString(pysqlite_OperationalError, sqlite3_errmsg(db));
72 break;
73 case SQLITE_CORRUPT:
74 PyErr_SetString(pysqlite_DatabaseError, sqlite3_errmsg(db));
75 break;
76 case SQLITE_TOOBIG:
77 PyErr_SetString(pysqlite_DataError, sqlite3_errmsg(db));
78 break;
79 case SQLITE_CONSTRAINT:
80 case SQLITE_MISMATCH:
81 PyErr_SetString(pysqlite_IntegrityError, sqlite3_errmsg(db));
82 break;
83 case SQLITE_MISUSE:
84 PyErr_SetString(pysqlite_ProgrammingError, sqlite3_errmsg(db));
85 break;
86 default:
87 PyErr_SetString(pysqlite_DatabaseError, sqlite3_errmsg(db));
88 break;
89 }
90
91 return errorcode;
92}
93
94#ifdef WORDS_BIGENDIAN
95# define IS_LITTLE_ENDIAN 0
96#else
97# define IS_LITTLE_ENDIAN 1
98#endif
99
100sqlite_int64
101_pysqlite_long_as_int64(PyObject * py_val)
102{
103 int overflow;
104 long long value = PyLong_AsLongLongAndOverflow(py_val, &overflow);
105 if (value == -1 && PyErr_Occurred())
106 return -1;
107 if (!overflow) {
108# if SIZEOF_LONG_LONG > 8
109 if (-0x8000000000000000LL <= value && value <= 0x7FFFFFFFFFFFFFFFLL)
110# endif
111 return value;
112 }
113 else if (sizeof(value) < sizeof(sqlite_int64)) {
114 sqlite_int64 int64val;
115 if (_PyLong_AsByteArray((PyLongObject *)py_val,
116 (unsigned char *)&int64val, sizeof(int64val),
117 IS_LITTLE_ENDIAN, 1 /* signed */) >= 0) {
118 return int64val;
119 }
120 }
121 PyErr_SetString(PyExc_OverflowError,
122 "Python int too large to convert to SQLite INTEGER");
123 return -1;
124}
125