forked from membase/ep-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlite-pst.hh
208 lines (177 loc) · 4.38 KB
/
sqlite-pst.hh
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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#ifndef SQLITE_PST_H
#define SQLITE_PST_H 1
#include <string>
#include <stdio.h>
#include <inttypes.h>
#ifdef USE_SYSTEM_LIBSQLITE3
#include <sqlite3.h>
#else
#include "embedded/sqlite3.h"
#endif
#include "common.hh"
/**
* A sqlite prepared statement.
*/
class PreparedStatement {
public:
/**
* Construct a prepared statement.
*
* @param d the DB where the prepared statement will execute
* @param query the query to prepare
*/
PreparedStatement(sqlite3 *d, const char *query);
/**
* Clean up.
*/
~PreparedStatement();
/**
* Bind a null-terminated string parameter to a binding in
* this statement.
*
* @param pos the binding position (starting at 1)
* @param s the value to bind
*/
void bind(int pos, const char *s);
/**
* Bind a string parameter to a binding in this statement.
*
* @param pos the binding position (starting at 1)
* @param s the value to bind
* @param nbytes number of bytes in the string.
*/
void bind(int pos, const char *s, size_t nbytes);
/**
* Bind a uint32 value.
*
* @param pos the binding position (starting at 1)
* @param d the value to bind
*/
void bind(int pos, int d);
/**
* Bind a uint64 value.
*
* @param pos the binding position (starting at 1)
* @param d the value to bind
*/
void bind64(int pos, uint64_t d);
/**
* Execute a prepared statement that does not return results.
*
* @return how many rows were affected
*/
int execute();
/**
* Execute a prepared statement that does return results
* and/or return the next row.
*
* @return true if there are more rows after this one
*/
bool fetch();
/**
* Reset the bindings.
*
* Call this before reusing a prepared statement.
*/
void reset();
/**
* Get the value at a given column in the current row.
*
* Use this along with fetch.
*
* @param x the column number
* @return the value
*/
const char *column(int x);
/**
* Get the value as a blob from the given column in the current
* row.
*
* Use this along with fetch and column_bytes().
*
* @param x the column number
* @return the value
*/
const void *column_blob(int x);
/**
* Get the number of bytes stored in the given column for the
* current row.
*
* Use this along with fetch and column() or column_blob().
*
* @param x the column number
* @return the number of bytes found
*/
int column_bytes(int x);
/**
* Get the integer valueof the given column at the current row.
*
* Use this along with fetch.
*
* @param x the column number
* @return the value
*/
int column_int(int x);
/**
* Get the 64-bit integer value of the given column at the current
* row.
*
* Use this along with fetch.
*
* @param x the column number
* @return the value
*/
uint64_t column_int64(int x);
private:
sqlite3 *db;
sqlite3_stmt *st;
};
// Container of multiple statements.
class Statements {
public:
Statements(sqlite3 *dbh, std::string tab) {
db = dbh;
tableName = tab;
initStatements();
}
~Statements() {
delete ins_stmt;
delete upd_stmt;
delete sel_stmt;
delete del_stmt;
delete del_vb_stmt;
delete all_stmt;
ins_stmt = upd_stmt = sel_stmt = del_stmt = del_vb_stmt = all_stmt = NULL;
}
PreparedStatement *ins() {
return ins_stmt;
}
PreparedStatement *upd() {
return upd_stmt;
}
PreparedStatement *sel() {
return sel_stmt;
}
PreparedStatement *del() {
return del_stmt;
}
PreparedStatement *del_vb() {
return del_vb_stmt;
}
PreparedStatement *all() {
return all_stmt;
}
private:
void initStatements();
sqlite3 *db;
std::string tableName;
PreparedStatement *ins_stmt;
PreparedStatement *upd_stmt;
PreparedStatement *sel_stmt;
PreparedStatement *del_stmt;
PreparedStatement *del_vb_stmt;
PreparedStatement *all_stmt;
DISALLOW_COPY_AND_ASSIGN(Statements);
};
#endif /* SQLITE_PST_H */