-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSQLitePreparedStatement.cpp
More file actions
194 lines (157 loc) · 5.77 KB
/
Copy pathSQLitePreparedStatement.cpp
File metadata and controls
194 lines (157 loc) · 5.77 KB
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
#include "SQLitePreparedStatement.h"
#include "SQLiteCountProxy.h"
#include "SQLiteConnection.h"
#include "SQLiteExceptions.h"
#include <sqlite3.h>
void finalize_sqlite3_stmt(sqlite3_stmt* statement)
{
if (statement)
sqlite3_finalize(statement);
}
namespace
{
/** Helper to init _statement in SQLitePreparedStatement constructor. */
sqlite3_stmt* init_statement(dbc::SQLiteConnection& db, const std::string& sql)
{
const char* tail_unused;
// If the caller knows that the supplied string is nul-terminated,
// then there is a small performance advantage to be gained by passing an
// nByte parameter that is equal to the number of bytes in the input
// string *including* the nul-terminator bytes.
sqlite3_stmt* statement;
int ret = sqlite3_prepare_v2(db.handle(), sql.c_str(), sql.length() + 1,
&statement, &tail_unused);
if (ret != SQLITE_OK)
throw dbc::SQLiteSqlError(db, "sqlite3_prepare_v2() failed", sql);
return statement;
}
}
namespace dbc
{
#include <xstring>
std::string toMBStr(std::wstring const& str)
{
std::string mbstr( str.size(), 0 );
size_t res;
wcstombs_s(&res, &mbstr[0], mbstr.size(), &str[0], str.size() );
return mbstr;
}
#define THROW_IF_SET_STMT_NOT_OK(retval, func_name, val) \
if (retval != SQLITE_OK) \
{ \
std::ostringstream err; \
err << func_name "(" << index << ", " << val << ") failed"; \
throw SQLiteSqlError(_db, err.str(), getSQL()); \
}
#define THROW_IF_SET_STMT_NOT_OK_W(retval, func_name, val) \
if (retval != SQLITE_OK) \
{ \
std::wostringstream err; \
err << func_name L"(" << index << L", " << val << L") failed"; \
throw SQLiteSqlError(_db, toMBStr(err.str()), getSQL()); \
}
// sql has to be UTF-8
SQLitePreparedStatement::SQLitePreparedStatement(const std::string& sql,
SQLiteConnection& db) :
PreparedStatement(),
_db(db),
_statement(init_statement(db, sql)),
_param_tracker(sqlite3_bind_parameter_count(_statement.get()))
{}
ResultSet::ptr SQLitePreparedStatement::doExecuteQuery()
{
checkParams();
return ResultSet::ptr(new SQLiteResultSet(*this));
}
const CountProxy& SQLitePreparedStatement::doExecuteUpdate()
{
checkParams();
static SQLiteCountProxy count(_db.handle());
int ret = sqlite3_step(_statement.get());
if (ret != SQLITE_DONE) {
sqlite3_reset(_statement.get());
throw SQLiteSqlError(_db,
"sqlite3_step() must return SQLITE_DONE in executeUpdate()",
getSQL());
}
// SQLITE_DONE means that the statement has finished executing
// successfully. sqlite3_step() should not be called again on this virtual
// machine without first calling sqlite3_reset() to reset the virtual
// machine back to its initial state.
reset();
return count;
}
void SQLitePreparedStatement::doClear()
{
int ret = sqlite3_clear_bindings(_statement.get());
if (ret != SQLITE_OK)
throw SQLiteSqlError(_db, "sqlite3_clear_bindings() failed", getSQL());
}
void SQLitePreparedStatement::doReset()
{
// We have experienced SQLITE_BUSY errors here indicating that the
// database is locked because of another ongoing operation. Increasing
// sqlite3_busy_timeout() and/or using transactions should fix this.
int ret = sqlite3_reset(_statement.get());
if (ret != SQLITE_OK)
throw SQLiteSqlError(_db, "sqlite3_reset() failed", getSQL());
}
__int64 SQLitePreparedStatement::getLastInsertId()
{
return static_cast<__int64>(sqlite3_last_insert_rowid(_db.handle()));
}
const char* SQLitePreparedStatement::getSQL() const
{
return sqlite3_sql(_statement.get());
}
void SQLitePreparedStatement::setInt(const int index, const int value)
{
_param_tracker.setParameter(index);
int ret = sqlite3_bind_int(_statement.get(), index, value);
THROW_IF_SET_STMT_NOT_OK(ret, "sqlite3_bind_int", value);
}
void SQLitePreparedStatement::setInt64(const int index, const __int64 value)
{
_param_tracker.setParameter(index);
int ret = sqlite3_bind_int64(_statement.get(), index, value);
THROW_IF_SET_STMT_NOT_OK(ret, "sqlite3_bind_int64", value);
}
void SQLitePreparedStatement::setBool(const int index, const bool value)
{
_param_tracker.setParameter(index);
int ret = sqlite3_bind_int(_statement.get(), index, value ? 1 : 0);
THROW_IF_SET_STMT_NOT_OK(ret, "sqlite3_bind_int", value);
}
void SQLitePreparedStatement::setDouble(const int index, const double value)
{
_param_tracker.setParameter(index);
int ret = sqlite3_bind_double(_statement.get(), index, value);
THROW_IF_SET_STMT_NOT_OK(ret, "sqlite3_bind_double", value);
}
void SQLitePreparedStatement::setString(const int index, const std::string& value)
{
_param_tracker.setParameter(index);
// Note that SQLITE_TRANSIENT makes copy of the data.
// This may be expensive if it is large.
int ret = sqlite3_bind_text(_statement.get(), index,
value.c_str(), -1, SQLITE_TRANSIENT);
THROW_IF_SET_STMT_NOT_OK(ret, "sqlite3_bind_text",
(value.length() < 50 ? value : value.substr(0, 47) + "..."));
}
void SQLitePreparedStatement::setWString(const int index, const std::wstring& value)
{
_param_tracker.setParameter(index);
// Note that SQLITE_TRANSIENT makes copy of the data.
// This may be expensive if it is large.
int ret = sqlite3_bind_text16(_statement.get(), index,
value.c_str(), -1, SQLITE_TRANSIENT);
THROW_IF_SET_STMT_NOT_OK_W(ret, L"sqlite3_bind_text16",
(value.length() < 50 ? value : value.substr(0, 47) + L"..."));
}
void SQLitePreparedStatement::setNull(const int index)
{
_param_tracker.setParameter(index);
int ret = sqlite3_bind_null(_statement.get(), index);
THROW_IF_SET_STMT_NOT_OK(ret, "sqlite3_bind_null", "");
}
}