1 /* util.c - various utility functions
3 * Copyright (C) 2005 Gerhard Häring <gh@ghaering.de>
5 * This file is part of pysqlite.
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.
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:
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.
25 #include "connection.h"
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().
31 void pysqlite_sleep(double seconds
)
35 ret
= PyObject_CallFunction(time_sleep
, "d", seconds
);
39 double pysqlite_time(void)
44 ret
= PyObject_CallFunction(time_time
, "");
45 time
= PyFloat_AsDouble(ret
);
51 int _sqlite_step_with_busyhandler(sqlite3_stmt
* statement
, Connection
* connection
56 Py_BEGIN_ALLOW_THREADS
57 rc
= sqlite3_step(statement
);
64 * Checks the SQLite error code and sets the appropriate DB-API exception.
65 * Returns the error code (0 means no error occured).
67 int _seterror(sqlite3
* db
)
71 errorcode
= sqlite3_errcode(db
);
79 PyErr_SetString(OperationalError
, sqlite3_errmsg(db
));
82 PyErr_SetString(InternalError
, sqlite3_errmsg(db
));
85 PyErr_SetString(OperationalError
, sqlite3_errmsg(db
));
88 PyErr_SetString(OperationalError
, sqlite3_errmsg(db
));
91 PyErr_SetString(OperationalError
, sqlite3_errmsg(db
));
94 PyErr_SetString(OperationalError
, sqlite3_errmsg(db
));
97 (void)PyErr_NoMemory();
100 PyErr_SetString(OperationalError
, sqlite3_errmsg(db
));
102 case SQLITE_INTERRUPT
:
103 PyErr_SetString(OperationalError
, sqlite3_errmsg(db
));
106 PyErr_SetString(OperationalError
, sqlite3_errmsg(db
));
109 PyErr_SetString(DatabaseError
, sqlite3_errmsg(db
));
111 case SQLITE_NOTFOUND
:
112 PyErr_SetString(InternalError
, sqlite3_errmsg(db
));
115 PyErr_SetString(OperationalError
, sqlite3_errmsg(db
));
117 case SQLITE_CANTOPEN
:
118 PyErr_SetString(OperationalError
, sqlite3_errmsg(db
));
120 case SQLITE_PROTOCOL
:
121 PyErr_SetString(OperationalError
, sqlite3_errmsg(db
));
124 PyErr_SetString(OperationalError
, sqlite3_errmsg(db
));
127 PyErr_SetString(OperationalError
, sqlite3_errmsg(db
));
130 PyErr_SetString(DataError
, sqlite3_errmsg(db
));
132 case SQLITE_CONSTRAINT
:
133 PyErr_SetString(IntegrityError
, sqlite3_errmsg(db
));
135 case SQLITE_MISMATCH
:
136 PyErr_SetString(IntegrityError
, sqlite3_errmsg(db
));
139 PyErr_SetString(ProgrammingError
, sqlite3_errmsg(db
));
142 PyErr_SetString(DatabaseError
, sqlite3_errmsg(db
));