ThinSQLite++
A thin, safe and convenient modern C++ wrapper for the SQLite API.
Loading...
Searching...
No Matches
exception_iface.hpp
1/*
2 Copyright 2019 Eugene Gershnik
3
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://github.com/gershnik/thinsqlitepp/blob/master/LICENSE
7*/
8
9#ifndef HEADER_SQLITEPP_EXCEPTION_IFACE_INCLUDED
10#define HEADER_SQLITEPP_EXCEPTION_IFACE_INCLUDED
11
12#include "config.hpp"
13
14#include <exception>
15#include <memory>
16#include <limits>
17
18namespace thinsqlitepp
19{
20 SQLITEPP_EXPORTED class database;
21 SQLITEPP_EXPORTED class exception;
22
27
37 SQLITEPP_EXPORTED
38 class error
39 {
40 friend class exception;
41 private:
42 class free_message
43 {
44 public:
45 free_message() noexcept: _free(nullptr) {}
46 free_message(void (*free)(void *)) noexcept: _free(free) {}
47
48 void operator()(const char * message) const noexcept
49 { if (_free) _free(const_cast<char *>(message)); }
50
51 explicit operator bool() const noexcept
52 { return _free != nullptr; }
53 private:
54 void (*_free)(void *);
55 };
56
57 public:
60
61 public:
67 error(int error_code) noexcept;
68
78 error(int error_code, const database * db) noexcept;
79
81 error(int error_code, const std::unique_ptr<database> & db) noexcept:
82 error(error_code, db.get())
83 {}
84
85 error(int error_code, const database & db) noexcept:
86 error(error_code, &db)
87 {}
88
94 error(int error_code, message_ptr && error_message) noexcept:
95 _error_code(error_code),
96 _message(std::move(error_message))
97 {}
98
99 error(const error & src) noexcept:
100 _error_code(src._error_code),
101 _message(src._message.get_deleter() ? copy_message(src._message.get()) : message_ptr(src._message.get()))
102 {}
103 error(error && src) noexcept:
104 _error_code(src._error_code),
105 _message(std::move(src._message))
106 {}
107 ~error() noexcept = default;
108
109 error & operator=(error src) noexcept
110 {
111 swap(*this, src);
112 return *this;
113 }
114
115 friend void swap(error & lhs, error & rhs) noexcept
116 {
117 using std::swap;
118 swap(lhs._error_code, rhs._error_code);
119 swap(lhs._message, rhs._message);
120 }
121
123 int extended() const noexcept
124 { return _error_code; }
125
126 int primary() const noexcept
127 { return _error_code & 0x0FF; }
128
129 int system() const noexcept
130 { return _system_error_code; }
131
132 const char * message() const noexcept
133 { return _message.get(); }
134 #if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 38, 0)
140 int offset() const noexcept
141 { return _offset;}
142 #endif
143
151 { return std::move(_message); }
152
159 { return std::move(_message); }
160 private:
161 static message_ptr copy_message(const char * src) noexcept;
162 private:
163 int _error_code = 0;
164 int _system_error_code = 0;
165 #if SQLITE_VERSION_NUMBER >= SQLITEPP_SQLITE_VERSION(3, 38, 0)
166 int _offset = -1;
167 #endif
168 message_ptr _message = nullptr;
169 };
170
178 SQLITEPP_EXPORTED
180 {
181 public:
183 exception(class error && err) noexcept:
184 _error(std::move(err))
185 {}
186
187 exception(const class error & err) noexcept:
188 _error(err)
189 {}
190
192 exception(int error_code) noexcept:
193 _error(error_code)
194 {}
195
197 exception(int error_code, const database * db) noexcept:
198 _error(error_code, db)
199 {}
200
201 exception(int error_code, const std::unique_ptr<database> & db) noexcept:
202 _error(error_code, db)
203 {}
204
205 exception(int error_code, const database & db) noexcept:
206 _error(error_code, db)
207 {}
208
210 exception(int error_code, error::message_ptr && message) noexcept:
211 _error(error_code, std::move(message))
212 {}
213
215 int extended_error_code() const noexcept
216 { return _error.extended(); }
217
218 int primary_error_code() const noexcept
219 { return _error.primary(); }
220
221 int system_error_code() const noexcept
222 { return _error.system(); }
223
225 const class error & error() const & noexcept
226 { return _error; }
227
229 class error & error() & noexcept
230 { return _error; }
231
233 class error && error() && noexcept
234 { return std::move(_error); }
235
242 const char * what() const noexcept override;
243
244 private:
245 class error _error;
246 };
247
249
250 inline int int_size(size_t size)
251 {
253 throw exception(SQLITE_TOOBIG);
254 return int(size);
255 }
256
257 inline sqlite3_int64 int64_size(size_t size) noexcept(sizeof(size_t) < sizeof(sqlite3_int64))
258 {
259 if constexpr (sizeof(size_t) >= sizeof(sqlite3_int64)) {
260 if (size > size_t(std::numeric_limits<sqlite3_int64>::max()))
261 throw exception(SQLITE_TOOBIG);
262 }
263 return sqlite3_int64(size);
264 }
265
267
269}
270
271#endif
Database Connection.
Definition database_iface.hpp:109
Carries information about SQLite error.
Definition exception_iface.hpp:39
int primary() const noexcept
Returns primary error code part.
Definition exception_iface.hpp:126
error(int error_code, const database &db) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition exception_iface.hpp:85
int extended() const noexcept
Returns full extended error code.
Definition exception_iface.hpp:123
message_ptr extract_message() &&noexcept
Move the message out of this object.
Definition exception_iface.hpp:158
const char * message() const noexcept
Returns error message or nullptr, if not available.
Definition exception_iface.hpp:132
int offset() const noexcept
Returns the offset within SQL statement that produced the error.
Definition exception_iface.hpp:140
std::unique_ptr< const char, free_message > message_ptr
An owning pointer to SQLite error message.
Definition exception_iface.hpp:59
error(int error_code, const std::unique_ptr< database > &db) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition exception_iface.hpp:81
error(int error_code) noexcept
Constructs an instance from database independent error code.
message_ptr extract_message() &noexcept
Move the message out of this object.
Definition exception_iface.hpp:150
int system() const noexcept
Returns system errno error code, if available.
Definition exception_iface.hpp:129
error(int error_code, const database *db) noexcept
Constructs an instance from the last error reported from a database.
error(int error_code, message_ptr &&error_message) noexcept
Constructs an instance with a given error code and message.
Definition exception_iface.hpp:94
Exception used to report any SQLite errors.
Definition exception_iface.hpp:180
int system_error_code() const noexcept
Returns system errno error code of the stored error, if available.
Definition exception_iface.hpp:221
exception(int error_code, error::message_ptr &&message) noexcept
Constructs an instance with a given error code and message.
Definition exception_iface.hpp:210
int extended_error_code() const noexcept
Returns full extended error code of the stored error.
Definition exception_iface.hpp:215
exception(class error &&err) noexcept
Constructs an instance by moving an error in.
Definition exception_iface.hpp:183
exception(int error_code, const database *db) noexcept
Constructs an instance from the last error reported from a database.
Definition exception_iface.hpp:197
exception(int error_code) noexcept
Constructs an instance from database independent error code.
Definition exception_iface.hpp:192
class error & error() &noexcept
Returns the stored error.
Definition exception_iface.hpp:229
exception(const class error &err) noexcept
Constructs an instance by copying an error.
Definition exception_iface.hpp:187
int primary_error_code() const noexcept
Returns primary error code part of the stored error.
Definition exception_iface.hpp:218
class error && error() &&noexcept
Returns the stored error.
Definition exception_iface.hpp:233
const char * what() const noexcept override
Returns error message.
const class error & error() const &noexcept
Returns the stored error.
Definition exception_iface.hpp:225
exception(int error_code, const database &db) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition exception_iface.hpp:205
exception(int error_code, const std::unique_ptr< database > &db) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition exception_iface.hpp:201
T max(T... args)
ThinSQLite++ namespace.
Definition backup_iface.hpp:17
T swap(T... args)