/
sqlite-pst.cc
144 lines (122 loc) · 3.9 KB
/
sqlite-pst.cc
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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#include "config.h"
#include <string.h>
#include <sstream>
#include <cstdlib>
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <assert.h>
#include "sqlite-pst.hh"
#define MAX_STEPS 10000
PreparedStatement::PreparedStatement(sqlite3 *d, const char *query) {
assert(d);
assert(query);
db = d;
if(sqlite3_prepare_v2(db, query, (int)strlen(query), &st, NULL)
!= SQLITE_OK) {
throw std::runtime_error(sqlite3_errmsg(db));
}
}
PreparedStatement::~PreparedStatement() {
sqlite3_finalize(st);
}
void PreparedStatement::bind(int pos, const char *s) {
bind(pos, s, strlen(s));
}
void PreparedStatement::bind(int pos, const char *s, size_t nbytes) {
sqlite3_bind_blob(st, pos, s, (int)nbytes, SQLITE_STATIC);
}
void PreparedStatement::bind(int pos, int v) {
sqlite3_bind_int(st, pos, v);
}
void PreparedStatement::bind64(int pos, uint64_t v) {
sqlite3_bind_int64(st, pos, v);
}
int PreparedStatement::execute() {
int steps_run = 0, rc = 0;
while ((rc = sqlite3_step(st)) != SQLITE_DONE) {
if (++steps_run > MAX_STEPS) {
return -1;
}
if (rc == SQLITE_ROW) {
// This is rather normal
} else if (rc == SQLITE_BUSY) {
std::cerr << "SQLITE_BUSY (retrying)" << std::endl;
} else {
const char *msg = sqlite3_errmsg(db);
std::cerr << "sqlite error: " << msg << std::endl;
return -1;
}
}
return sqlite3_changes(db);
}
bool PreparedStatement::fetch() {
bool rv = true;
assert(st);
int rc = sqlite3_step(st);
switch(rc) {
case SQLITE_ROW:
break;
case SQLITE_DONE:
rv = false;
break;
default:
std::stringstream ss;
ss << "Unhandled case in sqlite-pst: " << rc;
const char *msg = sqlite3_errmsg(db);
if (msg) {
ss << " (" << msg << ")";
}
throw std::runtime_error(ss.str());
}
return rv;
}
const char *PreparedStatement::column(int x) {
return (char*)sqlite3_column_text(st, x);
}
const void *PreparedStatement::column_blob(int x) {
return (char*)sqlite3_column_text(st, x);
}
int PreparedStatement::column_bytes(int x) {
return sqlite3_column_bytes(st, x);
}
int PreparedStatement::column_int(int x) {
return sqlite3_column_int(st, x);
}
uint64_t PreparedStatement::column_int64(int x) {
return sqlite3_column_int64(st, x);
}
void PreparedStatement::reset() {
// The result of this is ignored as it indicates the last error
// returned from step calls, not whether reset works.
// http://www.sqlite.org/c3ref/reset.html
sqlite3_reset(st);
}
void Statements::initStatements() {
char buf[1024];
snprintf(buf, sizeof(buf),
"insert into %s (k, v, flags, exptime, cas, vbucket) "
"values(?, ?, ?, ?, ?, ?)", tableName.c_str());
ins_stmt = new PreparedStatement(db, buf);
// Note that vbucket IDs don't change here.
snprintf(buf, sizeof(buf),
"update %s set k=?, v=?, flags=?, exptime=?, cas=? "
" where rowid = ?", tableName.c_str());
upd_stmt = new PreparedStatement(db, buf);
snprintf(buf, sizeof(buf),
"select v, flags, exptime, cas, rowid "
"from %s where rowid = ?", tableName.c_str());
sel_stmt = new PreparedStatement(db, buf);
snprintf(buf, sizeof(buf),
"select k, v, flags, exptime, cas, vbucket, rowid "
"from %s", tableName.c_str());
all_stmt = new PreparedStatement(db, buf);
snprintf(buf, sizeof(buf),
"delete from %s where k = ? and vbucket = ?",
tableName.c_str());
del_stmt = new PreparedStatement(db, buf);
snprintf(buf, sizeof(buf),
"delete from %s where vbucket = ?", tableName.c_str());
del_vb_stmt = new PreparedStatement(db, buf);
}