-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSqliteStatement.cpp
71 lines (57 loc) · 2.38 KB
/
SqliteStatement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "SqliteStatement.hpp"
template <typename T>
constexpr int Sqlite::sqliteReader<T>::getStringLength(const int columnNum) const noexcept {
return sqlite3_column_bytes(static_cast<const T *>(this)->getABI(), columnNum);
}
template <typename T>
constexpr int Sqlite::sqliteReader<T>::getWideStringLength(const int columnNum) const noexcept
{
return (sqlite3_column_bytes16(static_cast<const T *>(this)->getABI(), columnNum) / sizeof(wchar_t));
}
void Sqlite::SqliteStatement::throwLastError() const {
throw exception(sqlite3_db_handle(getABI()));
}
bool Sqlite::SqliteStatement::execute() const {
const int result = sqlite3_step(getABI());
if (result == SQLITE_ROW)
return true;
else if (result == SQLITE_DONE)
return false;
else {
throwLastError();
//the control will never reach here, as exception will be thrown
//added return false, just to remove warning: control may reach end of non-void function [-Wreturn-type]
return false;
}
}
void Sqlite::SqliteStatement::bind(const int index, const int value) const {
if (SQLITE_OK != sqlite3_bind_int(getABI(), index, value)) {
throwLastError();
}
}
void Sqlite::SqliteStatement::bind(const int index, const char *const strValue, const int size = -1) const {
if (SQLITE_OK != sqlite3_bind_text(getABI(), index, strValue, size, SQLITE_STATIC)) {
throwLastError();
}
}
void Sqlite::SqliteStatement::bind(const int index, const wchar_t *const strValue, const int size = -1) const {
if (SQLITE_OK != sqlite3_bind_text16(getABI(), index, strValue, size, SQLITE_STATIC)) {
throwLastError();
}
}
void Sqlite::SqliteStatement::bind(const int index, const std::string &strValue) const {
bind(index, strValue.c_str(), static_cast<int>(strValue.size()));
}
void Sqlite::SqliteStatement::bind(const int index, const std::wstring &strValue) const {
bind(index, strValue.c_str(), static_cast<int>((strValue.size() * sizeof(wchar_t))));
}
void Sqlite::SqliteStatement::bind(const int index, const std::string &&strValue) const {
if (SQLITE_OK != sqlite3_bind_text(getABI(), index, strValue.c_str(), static_cast<int>(strValue.size()), SQLITE_TRANSIENT)) {
throwLastError();
}
}
void Sqlite::SqliteStatement::bind(const int index, const std::wstring &&strValue) const {
if (SQLITE_OK != sqlite3_bind_text16(getABI(), index, strValue.c_str(), static_cast<int>((strValue.size() * sizeof(wchar_t))), SQLITE_TRANSIENT)) {
throwLastError();
}
}