• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* util.c - various utility functions
2  *
3  * Copyright (C) 2005-2010 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 
pysqlite_step(sqlite3_stmt * statement,pysqlite_Connection * connection)27 int pysqlite_step(sqlite3_stmt* statement, pysqlite_Connection* connection)
28 {
29     int rc;
30 
31     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     }
40 
41     return rc;
42 }
43 
44 /**
45  * Checks the SQLite error code and sets the appropriate DB-API exception.
46  * Returns the error code (0 means no error occurred).
47  */
_pysqlite_seterror(sqlite3 * db,sqlite3_stmt * st)48 int _pysqlite_seterror(sqlite3* db, sqlite3_stmt* st)
49 {
50     int errorcode;
51 
52     /* SQLite often doesn't report anything useful, unless you reset the statement first */
53     if (st != NULL) {
54         (void)sqlite3_reset(st);
55     }
56 
57     errorcode = sqlite3_errcode(db);
58 
59     switch (errorcode)
60     {
61         case SQLITE_OK:
62             PyErr_Clear();
63             break;
64         case SQLITE_INTERNAL:
65         case SQLITE_NOTFOUND:
66             PyErr_SetString(pysqlite_InternalError, sqlite3_errmsg(db));
67             break;
68         case SQLITE_NOMEM:
69             (void)PyErr_NoMemory();
70             break;
71         case SQLITE_ERROR:
72         case SQLITE_PERM:
73         case SQLITE_ABORT:
74         case SQLITE_BUSY:
75         case SQLITE_LOCKED:
76         case SQLITE_READONLY:
77         case SQLITE_INTERRUPT:
78         case SQLITE_IOERR:
79         case SQLITE_FULL:
80         case SQLITE_CANTOPEN:
81         case SQLITE_PROTOCOL:
82         case SQLITE_EMPTY:
83         case SQLITE_SCHEMA:
84             PyErr_SetString(pysqlite_OperationalError, sqlite3_errmsg(db));
85             break;
86         case SQLITE_CORRUPT:
87             PyErr_SetString(pysqlite_DatabaseError, sqlite3_errmsg(db));
88             break;
89         case SQLITE_TOOBIG:
90             PyErr_SetString(pysqlite_DataError, sqlite3_errmsg(db));
91             break;
92         case SQLITE_CONSTRAINT:
93         case SQLITE_MISMATCH:
94             PyErr_SetString(pysqlite_IntegrityError, sqlite3_errmsg(db));
95             break;
96         case SQLITE_MISUSE:
97             PyErr_SetString(pysqlite_ProgrammingError, sqlite3_errmsg(db));
98             break;
99         default:
100             PyErr_SetString(pysqlite_DatabaseError, sqlite3_errmsg(db));
101             break;
102     }
103 
104     return errorcode;
105 }
106 
107