Skip to content

Commit 2df66f1

Browse files
committed
Fixed reexecution of statements #97
- Used getter setter for all uses of execution_started - removed reset() call from _extract* - added exception for reexecution of already used statment
1 parent 558f5fa commit 2df66f1

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

hdr/sqlite_modern_cpp.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,14 @@ namespace sqlite {
150150

151151
void execute() {
152152
int hresult;
153+
used(true); /* prevent from executing again when goes out of scope */
153154

154155
while((hresult = sqlite3_step(_stmt.get())) == SQLITE_ROW) {}
155156

156157
if(hresult != SQLITE_DONE) {
157158
exceptions::throw_sqlite_error(hresult, sql());
158159
}
159-
used(true); /* prevent from executing again when goes out of scope */
160+
160161
}
161162

162163
std::string sql() {
@@ -173,7 +174,12 @@ namespace sqlite {
173174
return sqlite3_sql(_stmt.get());
174175
}
175176

176-
void used(bool state) { execution_started = state; }
177+
void used(bool state) {
178+
if(execution_started == true && state == true) {
179+
throw sqlite_exception("Already used statement executed again! Please reset() first!",sql());
180+
}
181+
execution_started = state;
182+
}
177183
bool used() const { return execution_started; }
178184

179185
private:
@@ -186,8 +192,8 @@ namespace sqlite {
186192
bool execution_started = false;
187193

188194
void _extract(std::function<void(void)> call_back) {
189-
execution_started = true;
190195
int hresult;
196+
used(true);
191197

192198
while((hresult = sqlite3_step(_stmt.get())) == SQLITE_ROW) {
193199
call_back();
@@ -196,12 +202,11 @@ namespace sqlite {
196202
if(hresult != SQLITE_DONE) {
197203
exceptions::throw_sqlite_error(hresult, sql());
198204
}
199-
reset();
200205
}
201206

202207
void _extract_single_value(std::function<void(void)> call_back) {
203-
execution_started = true;
204208
int hresult;
209+
used(true);
205210

206211
if((hresult = sqlite3_step(_stmt.get())) == SQLITE_ROW) {
207212
call_back();
@@ -216,7 +221,6 @@ namespace sqlite {
216221
if(hresult != SQLITE_DONE) {
217222
exceptions::throw_sqlite_error(hresult, sql());
218223
}
219-
reset();
220224
}
221225

222226
#ifdef _MSC_VER
@@ -307,7 +311,7 @@ namespace sqlite {
307311
~database_binder() noexcept(false) {
308312
/* Will be executed if no >>op is found, but not if an exception
309313
is in mid flight */
310-
if(!execution_started && !_has_uncaught_exception && _stmt) {
314+
if(!used() && !_has_uncaught_exception && _stmt) {
311315
execute();
312316
}
313317
}

0 commit comments

Comments
 (0)