bzr branch
http://bzr.ed.am/sqlite3cc
1
by edam
- initial commit |
1 |
/* |
2 |
* exception.cpp |
|
3 |
* |
|
4 |
* Copyright (C) 2009 Tim Marston <edam@waxworlds.org> |
|
5 |
* |
|
6 |
* This file is part of sqlitepp (hereafter referred to as "this program"). |
|
7 |
* See http://www.waxworlds.org/edam/software/sqlitepp for more information. |
|
8 |
* |
|
9 |
* This program is free software: you can redistribute it and/or modify |
|
10 |
* it under the terms of the GNU Lesser General Public License as published |
|
11 |
* by the Free Software Foundation, either version 3 of the License, or |
|
12 |
* (at your option) any later version. |
|
13 |
* |
|
14 |
* This program is distributed in the hope that it will be useful, |
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
* GNU Lesser General Public License for more details. |
|
18 |
* |
|
19 |
* You should have received a copy of the GNU Lesser General Public License |
|
20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21 |
*/ |
|
22 |
||
23 |
#include <sqlitepp/exception.hpp> |
|
24 |
#include <boost/assign/list_of.hpp> |
|
25 |
#include <map> |
|
26 |
#include <string> |
|
27 |
||
28 |
||
29 |
sqlite::sqlite_error::sqlite_error( |
|
30 |
int error_code ) |
|
31 |
: |
|
32 |
_error_code( error_code ), |
|
33 |
_message( get_error_message( error_code ) ) |
|
34 |
{ |
|
35 |
} |
|
36 |
||
37 |
||
38 |
sqlite::sqlite_error::sqlite_error( |
|
39 |
const std::string &message, |
|
40 |
int error_code ) |
|
41 |
: |
|
42 |
_error_code( error_code ), |
|
43 |
_message( message ) |
|
44 |
{ |
|
45 |
} |
|
46 |
||
47 |
||
48 |
sqlite::sqlite_error::~sqlite_error() throw( ) |
|
49 |
{ |
|
50 |
} |
|
51 |
||
52 |
||
53 |
int sqlite::sqlite_error::get_error_code() const |
|
54 |
{ |
|
55 |
return _error_code; |
|
56 |
} |
|
57 |
||
58 |
||
59 |
const char* sqlite::sqlite_error::what() const throw( ) |
|
60 |
{ |
|
61 |
return _message.c_str(); |
|
62 |
} |
|
63 |
||
64 |
||
65 |
const std::string &sqlite::sqlite_error::get_error_message( |
|
66 |
int error_code ) |
|
67 |
{ |
|
68 |
static const std::map< int, std::string > messages = |
|
69 |
boost::assign::map_list_of |
|
70 |
( SQLITE_OK, "Successful result" ) |
|
71 |
( SQLITE_ERROR, "SQL error or missing database" ) |
|
72 |
( SQLITE_INTERNAL, "Internal logic error in SQLite" ) |
|
73 |
( SQLITE_PERM, "Access permission denied" ) |
|
74 |
( SQLITE_ABORT, "Callback routine requested an abort" ) |
|
75 |
( SQLITE_BUSY, "The database file is locked" ) |
|
76 |
( SQLITE_LOCKED, "A table in the database is locked" ) |
|
77 |
( SQLITE_NOMEM, "A malloc() failed" ) |
|
78 |
( SQLITE_READONLY, "Attempt to write a readonly database" ) |
|
79 |
( SQLITE_INTERRUPT, "Operation terminated by sqlite3_interrupt()" ) |
|
80 |
( SQLITE_IOERR, "Some kind of disk I/O error occurred" ) |
|
81 |
( SQLITE_CORRUPT, "The database disk image is malformed" ) |
|
82 |
( SQLITE_NOTFOUND, "NOT USED. Table or record not found" ) |
|
83 |
( SQLITE_FULL, "Insertion failed because database is full" ) |
|
84 |
( SQLITE_CANTOPEN, "Unable to open the database file" ) |
|
85 |
( SQLITE_PROTOCOL, "NOT USED. Database lock protocol error" ) |
|
86 |
( SQLITE_EMPTY, "Database is empty" ) |
|
87 |
( SQLITE_SCHEMA, "The database schema changed" ) |
|
88 |
( SQLITE_TOOBIG, "String or BLOB exceeds size limit" ) |
|
89 |
( SQLITE_CONSTRAINT, "Abort due to constraint violation" ) |
|
90 |
( SQLITE_MISMATCH, "Data type mismatch" ) |
|
91 |
( SQLITE_MISUSE, "Library used incorrectly" ) |
|
92 |
( SQLITE_NOLFS, "Uses OS features not supported on host" ) |
|
93 |
( SQLITE_AUTH, "Authorization denied" ) |
|
94 |
( SQLITE_FORMAT, "Auxiliary database format error" ) |
|
95 |
( SQLITE_RANGE, "2nd parameter to sqlite3_bind out of range" ) |
|
96 |
( SQLITE_NOTADB, "File opened that is not a database file" ) |
|
97 |
( SQLITE_ROW, "sqlite3_step() has another row ready" ) |
|
98 |
( SQLITE_DONE, "sqlite3_step() has finished executing" ); |
|
99 |
||
100 |
std::map< int, std::string >::const_iterator i = |
|
101 |
messages.find( error_code ); |
|
102 |
if( i == messages.end() ) |
|
103 |
throw std::range_error( "bad sqlite error code" ); |
|
104 |
return i->second; |
|
105 |
} |