blob: 5e78d588460bfb14e4e1792d1a8956c26fe82f31 [file] [log] [blame]
Anthony Baxterc51ee692006-04-01 00:57:31 +00001/* util.c - various utility functions
2 *
Anthony Baxter72289a62006-04-04 06:29:05 +00003 * Copyright (C) 2005-2006 Gerhard Häring <gh@ghaering.de>
Anthony Baxterc51ee692006-04-01 00:57:31 +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
Gerhard Häring0741a602007-01-14 01:43:50 +000027int _sqlite_step_with_busyhandler(sqlite3_stmt* statement, pysqlite_Connection* connection)
Anthony Baxterc51ee692006-04-01 00:57:31 +000028{
29 int rc;
30
Gerhard Häring99b9df82007-12-11 21:07:40 +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 }
Anthony Baxterc51ee692006-04-01 00:57:31 +000040
41 return rc;
42}
43
44/**
45 * Checks the SQLite error code and sets the appropriate DB-API exception.
Georg Brandlcddabbf2006-07-28 18:36:01 +000046 * Returns the error code (0 means no error occurred).
Anthony Baxterc51ee692006-04-01 00:57:31 +000047 */
Gerhard Häring0741a602007-01-14 01:43:50 +000048int _pysqlite_seterror(sqlite3* db)
Anthony Baxterc51ee692006-04-01 00:57:31 +000049{
50 int errorcode;
51
52 errorcode = sqlite3_errcode(db);
53
54 switch (errorcode)
55 {
56 case SQLITE_OK:
57 PyErr_Clear();
58 break;
Anthony Baxterc51ee692006-04-01 00:57:31 +000059 case SQLITE_INTERNAL:
Anthony Baxter72289a62006-04-04 06:29:05 +000060 case SQLITE_NOTFOUND:
Gerhard Häring0741a602007-01-14 01:43:50 +000061 PyErr_SetString(pysqlite_InternalError, sqlite3_errmsg(db));
Anthony Baxterc51ee692006-04-01 00:57:31 +000062 break;
Anthony Baxterc51ee692006-04-01 00:57:31 +000063 case SQLITE_NOMEM:
64 (void)PyErr_NoMemory();
65 break;
Anthony Baxter72289a62006-04-04 06:29:05 +000066 case SQLITE_ERROR:
67 case SQLITE_PERM:
68 case SQLITE_ABORT:
69 case SQLITE_BUSY:
70 case SQLITE_LOCKED:
Anthony Baxterc51ee692006-04-01 00:57:31 +000071 case SQLITE_READONLY:
Anthony Baxterc51ee692006-04-01 00:57:31 +000072 case SQLITE_INTERRUPT:
Anthony Baxterc51ee692006-04-01 00:57:31 +000073 case SQLITE_IOERR:
Anthony Baxter72289a62006-04-04 06:29:05 +000074 case SQLITE_FULL:
75 case SQLITE_CANTOPEN:
76 case SQLITE_PROTOCOL:
77 case SQLITE_EMPTY:
78 case SQLITE_SCHEMA:
Gerhard Häring0741a602007-01-14 01:43:50 +000079 PyErr_SetString(pysqlite_OperationalError, sqlite3_errmsg(db));
Anthony Baxterc51ee692006-04-01 00:57:31 +000080 break;
81 case SQLITE_CORRUPT:
Gerhard Häring0741a602007-01-14 01:43:50 +000082 PyErr_SetString(pysqlite_DatabaseError, sqlite3_errmsg(db));
Anthony Baxterc51ee692006-04-01 00:57:31 +000083 break;
Anthony Baxterc51ee692006-04-01 00:57:31 +000084 case SQLITE_TOOBIG:
Gerhard Häring0741a602007-01-14 01:43:50 +000085 PyErr_SetString(pysqlite_DataError, sqlite3_errmsg(db));
Anthony Baxterc51ee692006-04-01 00:57:31 +000086 break;
87 case SQLITE_CONSTRAINT:
Anthony Baxterc51ee692006-04-01 00:57:31 +000088 case SQLITE_MISMATCH:
Gerhard Häring0741a602007-01-14 01:43:50 +000089 PyErr_SetString(pysqlite_IntegrityError, sqlite3_errmsg(db));
Anthony Baxterc51ee692006-04-01 00:57:31 +000090 break;
91 case SQLITE_MISUSE:
Gerhard Häring0741a602007-01-14 01:43:50 +000092 PyErr_SetString(pysqlite_ProgrammingError, sqlite3_errmsg(db));
Anthony Baxterc51ee692006-04-01 00:57:31 +000093 break;
94 default:
Gerhard Häring0741a602007-01-14 01:43:50 +000095 PyErr_SetString(pysqlite_DatabaseError, sqlite3_errmsg(db));
Anthony Baxter72289a62006-04-04 06:29:05 +000096 break;
Anthony Baxterc51ee692006-04-01 00:57:31 +000097 }
98
99 return errorcode;
100}
101