37 #include <relpipe/writer/Factory.h> |
37 #include <relpipe/writer/Factory.h> |
38 |
38 |
39 #include "Configuration.h" |
39 #include "Configuration.h" |
40 #include "SqlException.h" |
40 #include "SqlException.h" |
41 #include "PreparedStatement.h" |
41 #include "PreparedStatement.h" |
|
42 #include "Connection.h" |
42 |
43 |
43 namespace relpipe { |
44 namespace relpipe { |
44 namespace tr { |
45 namespace tr { |
45 namespace sql { |
46 namespace sql { |
46 |
47 |
47 using namespace std; |
48 using namespace std; |
48 using namespace relpipe; |
49 using namespace relpipe; |
49 using namespace relpipe::reader; |
50 using namespace relpipe::reader; |
50 using namespace relpipe::reader::handlers; |
51 using namespace relpipe::reader::handlers; |
51 |
|
52 class Connection { |
|
53 private: |
|
54 sqlite3* db; |
|
55 |
|
56 void begin() { |
|
57 sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr); |
|
58 } |
|
59 |
|
60 public: |
|
61 |
|
62 Connection(const char* filename) { |
|
63 int result = sqlite3_open(filename, &db); |
|
64 if (result != SQLITE_OK) { |
|
65 sqlite3_close(db); |
|
66 throw SqlException(L"Unable to open SQLite database."); |
|
67 } |
|
68 begin(); |
|
69 } |
|
70 |
|
71 virtual ~Connection() { |
|
72 sqlite3_close(db); |
|
73 } |
|
74 |
|
75 PreparedStatement* prepareStatement(const char* sql) { |
|
76 const char* remaining; |
|
77 sqlite3_stmt *stmt; |
|
78 int result = sqlite3_prepare(db, sql, -1, &stmt, &remaining); |
|
79 if (result == SQLITE_OK) return new PreparedStatement(stmt); |
|
80 else throw SqlException(L"Unable to prepare SQLite statement."); |
|
81 } |
|
82 |
|
83 bool getAutoCommit() { |
|
84 return sqlite3_get_autocommit(db); |
|
85 } |
|
86 |
|
87 void setAutoCommit(bool autoCommit) { |
|
88 bool autoCommitOld = getAutoCommit(); |
|
89 if (autoCommit && !autoCommitOld) commit(); |
|
90 else if (!autoCommit && autoCommitOld) begin(); |
|
91 } |
|
92 |
|
93 void commit() { |
|
94 sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr); |
|
95 } |
|
96 |
|
97 void rollback() { |
|
98 sqlite3_exec(db, "ROLLBACK", nullptr, nullptr, nullptr); |
|
99 } |
|
100 |
|
101 }; |
|
102 |
52 |
103 class SqlHandler : public RelationalReaderValueHandler { |
53 class SqlHandler : public RelationalReaderValueHandler { |
104 private: |
54 private: |
105 Configuration configuration; |
55 Configuration configuration; |
106 boolean_t fileAlreadyExisted = false; |
56 boolean_t fileAlreadyExisted = false; |