blob: ec400a1faa0d959feb6c8e684a756d3067aec852 [file] [log] [blame]
Anthony Baxterc51ee692006-04-01 00:57:31 +00001/* util.c - various utility functions
2 *
3 * Copyright (C) 2005 Gerhard Häring <gh@ghaering.de>
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
27/*
28 * it's not so trivial to write a portable sleep in C. For now, the simplest
29 * solution is to just use Python's sleep().
30 */
31void pysqlite_sleep(double seconds)
32{
33 PyObject* ret;
34
35 ret = PyObject_CallFunction(time_sleep, "d", seconds);
36 Py_DECREF(ret);
37}
38
39double pysqlite_time(void)
40{
41 PyObject* ret;
42 double time;
43
44 ret = PyObject_CallFunction(time_time, "");
45 time = PyFloat_AsDouble(ret);
46 Py_DECREF(ret);
47
48 return time;
49}
50
51int _sqlite_step_with_busyhandler(sqlite3_stmt* statement, Connection* connection
52)
53{
54 int rc;
55
56 Py_BEGIN_ALLOW_THREADS
57 rc = sqlite3_step(statement);
58 Py_END_ALLOW_THREADS
59
60 return rc;
61}
62
63/**
64 * Checks the SQLite error code and sets the appropriate DB-API exception.
65 * Returns the error code (0 means no error occured).
66 */
67int _seterror(sqlite3* db)
68{
69 int errorcode;
70
71 errorcode = sqlite3_errcode(db);
72
73 switch (errorcode)
74 {
75 case SQLITE_OK:
76 PyErr_Clear();
77 break;
78 case SQLITE_ERROR:
79 PyErr_SetString(OperationalError, sqlite3_errmsg(db));
80 break;
81 case SQLITE_INTERNAL:
82 PyErr_SetString(InternalError, sqlite3_errmsg(db));
83 break;
84 case SQLITE_PERM:
85 PyErr_SetString(OperationalError, sqlite3_errmsg(db));
86 break;
87 case SQLITE_ABORT:
88 PyErr_SetString(OperationalError, sqlite3_errmsg(db));
89 break;
90 case SQLITE_BUSY:
91 PyErr_SetString(OperationalError, sqlite3_errmsg(db));
92 break;
93 case SQLITE_LOCKED:
94 PyErr_SetString(OperationalError, sqlite3_errmsg(db));
95 break;
96 case SQLITE_NOMEM:
97 (void)PyErr_NoMemory();
98 break;
99 case SQLITE_READONLY:
100 PyErr_SetString(OperationalError, sqlite3_errmsg(db));
101 break;
102 case SQLITE_INTERRUPT:
103 PyErr_SetString(OperationalError, sqlite3_errmsg(db));
104 break;
105 case SQLITE_IOERR:
106 PyErr_SetString(OperationalError, sqlite3_errmsg(db));
107 break;
108 case SQLITE_CORRUPT:
109 PyErr_SetString(DatabaseError, sqlite3_errmsg(db));
110 break;
111 case SQLITE_NOTFOUND:
112 PyErr_SetString(InternalError, sqlite3_errmsg(db));
113 break;
114 case SQLITE_FULL:
115 PyErr_SetString(OperationalError, sqlite3_errmsg(db));
116 break;
117 case SQLITE_CANTOPEN:
118 PyErr_SetString(OperationalError, sqlite3_errmsg(db));
119 break;
120 case SQLITE_PROTOCOL:
121 PyErr_SetString(OperationalError, sqlite3_errmsg(db));
122 break;
123 case SQLITE_EMPTY:
124 PyErr_SetString(OperationalError, sqlite3_errmsg(db));
125 break;
126 case SQLITE_SCHEMA:
127 PyErr_SetString(OperationalError, sqlite3_errmsg(db));
128 break;
129 case SQLITE_TOOBIG:
130 PyErr_SetString(DataError, sqlite3_errmsg(db));
131 break;
132 case SQLITE_CONSTRAINT:
133 PyErr_SetString(IntegrityError, sqlite3_errmsg(db));
134 break;
135 case SQLITE_MISMATCH:
136 PyErr_SetString(IntegrityError, sqlite3_errmsg(db));
137 break;
138 case SQLITE_MISUSE:
139 PyErr_SetString(ProgrammingError, sqlite3_errmsg(db));
140 break;
141 default:
142 PyErr_SetString(DatabaseError, sqlite3_errmsg(db));
143 }
144
145 return errorcode;
146}
147