1 | //============================================================================ |
2 | // |
3 | // SSSS tt lll lll |
4 | // SS SS tt ll ll |
5 | // SS tttttt eeee ll ll aaaa |
6 | // SSSS tt ee ee ll ll aa |
7 | // SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator" |
8 | // SS SS tt ee ll ll aa aa |
9 | // SSSS ttt eeeee llll llll aaaaa |
10 | // |
11 | // Copyright (c) 1995-2019 by Bradford W. Mott, Stephen Anthony |
12 | // and the Stella Team |
13 | // |
14 | // See the file "License.txt" for information on usage and redistribution of |
15 | // this file, and for a DISCLAIMER OF ALL WARRANTIES. |
16 | |
17 | #include "SqliteStatement.hxx" |
18 | #include "SqliteError.hxx" |
19 | |
20 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
21 | SqliteStatement::SqliteStatement(sqlite3* handle, string sql) : myStmt(nullptr), myHandle(handle) |
22 | { |
23 | if (sqlite3_prepare_v2(handle, sql.c_str(), -1, &myStmt, nullptr) != SQLITE_OK) |
24 | throw SqliteError(handle); |
25 | } |
26 | |
27 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
28 | SqliteStatement::~SqliteStatement() |
29 | { |
30 | if (myStmt) sqlite3_finalize(myStmt); |
31 | } |
32 | |
33 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
34 | SqliteStatement& SqliteStatement::bind(int index, const string& value) |
35 | { |
36 | if (sqlite3_bind_text(myStmt, index, value.c_str(), -1, SQLITE_TRANSIENT) != SQLITE_OK) |
37 | throw SqliteError(myHandle); |
38 | |
39 | return *this; |
40 | } |
41 | |
42 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
43 | bool SqliteStatement::step() const |
44 | { |
45 | int result = sqlite3_step(myStmt); |
46 | |
47 | if (result == SQLITE_ERROR) throw SqliteError(myHandle); |
48 | |
49 | return result == SQLITE_ROW; |
50 | } |
51 | |
52 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
53 | void SqliteStatement::reset() const |
54 | { |
55 | if (sqlite3_reset(myStmt) != SQLITE_OK) throw SqliteError(myHandle); |
56 | } |
57 | |
58 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
59 | string SqliteStatement::columnText(int index) const |
60 | { |
61 | return reinterpret_cast<const char*>(sqlite3_column_text(myStmt, index)); |
62 | } |
63 | |