VSQLite++ 0.3
Loading...
Searching...
No Matches
database_exception.hpp
Go to the documentation of this file.
1/*##############################################################################
2 VSQLite++ - virtuosic bytes SQLite3 C++ wrapper
3
4 Copyright (c) 2006-2014 Vinzenz Feenstra vinzenz.feenstra@gmail.com
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15 * Neither the name of virtuosic bytes nor the names of its contributors may
16 be used to endorse or promote products derived from this software without
17 specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
30
31##############################################################################*/
32#ifndef GUARD_SQLITE_DATABASE_EXCEPTION_HPP_INCLUDED
33#define GUARD_SQLITE_DATABASE_EXCEPTION_HPP_INCLUDED
34
35#include <stdexcept>
36#include <string>
37#include <utility>
38
46namespace sqlite {
47inline namespace v2 {
49 struct database_exception : public std::runtime_error {
50 database_exception(std::string const &msg) : std::runtime_error(msg.c_str()) {}
51 };
52
54 inline std::string append_sql_context(std::string message, std::string const &sql) {
55 if (sql.empty()) {
56 return message;
57 }
58 message.append(" [SQL: ");
59 message.append(sql);
60 message.push_back(']');
61 return message;
62 }
63
66 database_exception_code(std::string const &error_message, int sqlite_error_code,
67 std::string sql_context = std::string()) :
68 database_exception(append_sql_context(error_message, sql_context)),
69 sqlite_error_code_(sqlite_error_code), sql_(std::move(sql_context)) {}
70
71 int error_code() const {
72 return sqlite_error_code_;
73 }
74
75 std::string const &sql() const {
76 return sql_;
77 }
78
79 protected:
81 std::string sql_;
82 };
83
85 struct buffer_too_small_exception : public std::runtime_error {
86 buffer_too_small_exception(std::string const &msg) : std::runtime_error(msg.c_str()) {}
87 };
88
90 struct database_misuse_exception : public std::logic_error {
91 database_misuse_exception(std::string const &msg) : std::logic_error(msg) {}
92 };
93
96 database_misuse_exception_code(std::string const &msg, int sqlite_error_code,
97 std::string sql_context = std::string()) :
99 sqlite_error_code_(sqlite_error_code), sql_(std::move(sql_context)) {}
100
101 int error_code() const {
102 return sqlite_error_code_;
103 }
104
105 std::string const &sql() const {
106 return sql_;
107 }
108
109 protected:
111 std::string sql_;
112 };
113
116 database_system_error(std::string const &msg, int error_code) :
118
119 int error_code() const {
120 return error_code_;
121 }
122
123 protected:
125 };
126} // namespace v2
127} // namespace sqlite
128
129#endif // GUARD_SQLITE_DATABASE_EXCEPTION_HPP_INCLUDED
std::string append_sql_context(std::string message, std::string const &sql)
Helper that appends [SQL: ...] context to an existing message.
Raised when a caller-provided buffer is too small to hold a blob/text payload.
buffer_too_small_exception(std::string const &msg)
Exception that carries the original SQLite error code and optional SQL snippet.
database_exception_code(std::string const &error_message, int sqlite_error_code, std::string sql_context=std::string())
Generic runtime failure raised for most SQLite errors.
database_exception(std::string const &msg)
Logic-error flavour that also exposes the SQLite status code and SQL string.
database_misuse_exception_code(std::string const &msg, int sqlite_error_code, std::string sql_context=std::string())
Used for programming errors such as double-closing or using invalidated resources.
database_misuse_exception(std::string const &msg)
Wraps system-level failures (e.g., file I/O) that bubble up from SQLite APIs.
database_system_error(std::string const &msg, int error_code)