-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsqlite3x.hpp
210 lines (167 loc) · 5.95 KB
/
sqlite3x.hpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
Copyright (C) 2004-2005 Cory Nelson
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
CVS Info :
$Author: phrostbyte $
$Date: 2005/06/16 20:46:40 $
$Revision: 1.1 $
*/
#ifndef __SQLITE3X_HPP__
#define __SQLITE3X_HPP__
#include <string>
#include <stdexcept>
namespace sqlite3x {
class sqlite3_connection {
private:
friend class sqlite3_command;
friend class database_error;
struct sqlite3 *db;
public:
sqlite3_connection();
sqlite3_connection(const char *db);
sqlite3_connection(const wchar_t *db);
~sqlite3_connection();
void open(const char *db);
void open(const wchar_t *db);
void close();
long long insertid();
void setbusytimeout(int ms);
void executenonquery(const char *sql);
void executenonquery(const wchar_t *sql);
void executenonquery(const std::string &sql);
void executenonquery(const std::wstring &sql);
int executeint(const char *sql);
int executeint(const wchar_t *sql);
int executeint(const std::string &sql);
int executeint(const std::wstring &sql);
long long executeint64(const char *sql);
long long executeint64(const wchar_t *sql);
long long executeint64(const std::string &sql);
long long executeint64(const std::wstring &sql);
double executedouble(const char *sql);
double executedouble(const wchar_t *sql);
double executedouble(const std::string &sql);
double executedouble(const std::wstring &sql);
std::string executestring(const char *sql);
std::string executestring(const wchar_t *sql);
std::string executestring(const std::string &sql);
std::string executestring(const std::wstring &sql);
std::wstring executestring16(const char *sql);
std::wstring executestring16(const wchar_t *sql);
std::wstring executestring16(const std::string &sql);
std::wstring executestring16(const std::wstring &sql);
std::string executeblob(const char *sql);
std::string executeblob(const wchar_t *sql);
std::string executeblob(const std::string &sql);
std::string executeblob(const std::wstring &sql);
private: // emphasize the following members are private
sqlite3_connection( const sqlite3_connection& );
const sqlite3_connection& operator=( const sqlite3_connection& );
};
class sqlite3_transaction {
private:
sqlite3_connection &con;
bool intrans;
public:
sqlite3_transaction(sqlite3_connection &con, bool start=true);
~sqlite3_transaction();
void begin();
void commit();
void rollback();
private: // emphasize the following members are private
sqlite3_transaction( const sqlite3_transaction& );
const sqlite3_transaction& operator=( const sqlite3_transaction& );
};
class sqlite3_reader;
class sqlite3_command {
private:
friend class sqlite3_reader;
sqlite3_connection &con;
struct sqlite3_stmt *stmt;
unsigned int refs;
int argc;
public:
sqlite3_command(sqlite3_connection &con, const char *sql);
sqlite3_command(sqlite3_connection &con, const wchar_t *sql);
sqlite3_command(sqlite3_connection &con, const std::string &sql);
sqlite3_command(sqlite3_connection &con, const std::wstring &sql);
~sqlite3_command();
void bind(int index);
void bind(int index, int data);
void bind(int index, long long data);
void bind(int index, double data);
void bind(int index, const char *data, int datalen);
void bind(int index, const wchar_t *data, int datalen);
void bind(int index, const void *data, int datalen);
void bind(int index, const std::string &data);
void bind(int index, const std::wstring &data);
sqlite3_reader executereader();
void executenonquery();
int executeint();
long long executeint64();
double executedouble();
std::string executestring();
std::wstring executestring16();
std::string executeblob();
private: // emphasize the following members are private
sqlite3_command( const sqlite3_command& );
const sqlite3_command& operator=( const sqlite3_command& );
};
#if __cplusplus > 199711L
namespace detail {
template<unsigned I, class T>
void st_multi_bind_index(sqlite3_command& st, T&& arg) {
st.bind(I, arg);
}
template<unsigned I, class T, class... Args>
void st_multi_bind_index(sqlite3_command& st, T&& arg, Args&&... args) {
st.bind(I, arg);
detail::st_multi_bind_index<I+1, Args&&...>(st, std::forward<Args>(args)...);
}
}
template<class... Args>
void multi_bind(sqlite3_command& st, Args&&... args) {
detail::st_multi_bind_index<1, Args&&...>(st, std::forward<Args>(args)...);
}
#endif
class sqlite3_reader {
private:
friend class sqlite3_command;
sqlite3_command *cmd;
sqlite3_reader(sqlite3_command *cmd);
public:
sqlite3_reader();
sqlite3_reader(const sqlite3_reader ©);
~sqlite3_reader();
sqlite3_reader& operator=(const sqlite3_reader ©);
bool read();
void reset();
void close();
int getint(int index);
long long getint64(int index);
double getdouble(int index);
std::string getstring(int index);
std::wstring getstring16(int index);
std::string getblob(int index);
std::string getcolname(int index);
std::wstring getcolname16(int index);
};
class database_error : public std::runtime_error {
public:
database_error(const char *msg);
database_error(sqlite3_connection &con);
};
}
#endif