»Core Development>Code coverage>Modules/_sqlite/util.c

Python code coverage for Modules/_sqlite/util.c

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