Skip to content

Diagnose attempts to execute multistatements #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ int main() {
}
```

You can not execute multiple statements separated by semicolons in one go.

Additional flags
----
You can pass additional open flags to SQLite by using a config object:
Expand Down
14 changes: 10 additions & 4 deletions hdr/sqlite_modern_cpp.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <algorithm>
#include <cctype>
#include <string>
#include <functional>
#include <stdexcept>
Expand Down Expand Up @@ -85,10 +87,11 @@ namespace sqlite {
class more_rows: public sqlite_exception { using sqlite_exception::sqlite_exception; };
class no_rows: public sqlite_exception { using sqlite_exception::sqlite_exception; };
class reexecution: public sqlite_exception { using sqlite_exception::sqlite_exception; }; // Prepared statements need to be reset before calling them again
class more_statements: public sqlite_exception { using sqlite_exception::sqlite_exception; }; // Prepared statements can only contain one statement

static void throw_sqlite_error(const int& error_code, const std::string &sql = "") {
if(error_code == SQLITE_ERROR) throw exceptions::error(error_code, sql);
else if(error_code == SQLITE_INTERNAL) throw exceptions::internal (error_code, sql);
else if(error_code == SQLITE_INTERNAL) throw exceptions::internal(error_code, sql);
else if(error_code == SQLITE_PERM) throw exceptions::perm(error_code, sql);
else if(error_code == SQLITE_ABORT) throw exceptions::abort(error_code, sql);
else if(error_code == SQLITE_BUSY) throw exceptions::busy(error_code, sql);
Expand Down Expand Up @@ -249,8 +252,11 @@ namespace sqlite {
sqlite3_stmt* _prepare(const std::string& sql) {
int hresult;
sqlite3_stmt* tmp = nullptr;
hresult = sqlite3_prepare_v2(_db.get(), sql.data(), -1, &tmp, nullptr);
if((hresult) != SQLITE_OK) exceptions::throw_sqlite_error(hresult, sql);
const char *remaining;
hresult = sqlite3_prepare_v2(_db.get(), sql.data(), -1, &tmp, &remaining);
if(hresult != SQLITE_OK) exceptions::throw_sqlite_error(hresult, sql);
if(!std::all_of(remaining, sql.data() + sql.size(), [](char ch) {return std::isblank(ch);}))
throw exceptions::more_statements("Multiple semicolon separated statements are unsupported", sql);
return tmp;
}

Expand Down Expand Up @@ -296,7 +302,7 @@ namespace sqlite {
// Overload instead of specializing function templates (http://www.gotw.ca/publications/mill17.htm)
friend database_binder& operator<<(database_binder& db, const int& val);
friend void get_col_from_db(database_binder& db, int inx, int& val);
friend database_binder& operator <<(database_binder& db, const sqlite_int64& val);
friend database_binder& operator <<(database_binder& db, const sqlite_int64& val);
friend void get_col_from_db(database_binder& db, int inx, sqlite3_int64& i);
friend database_binder& operator <<(database_binder& db, const float& val);
friend void get_col_from_db(database_binder& db, int inx, float& f);
Expand Down