src/Connection.cpp
branchv_0
changeset 30 629565ff82d3
parent 29 b0ef1e1dc9c8
child 32 77180ee275df
equal deleted inserted replaced
29:b0ef1e1dc9c8 30:629565ff82d3
    22 namespace relpipe {
    22 namespace relpipe {
    23 namespace tr {
    23 namespace tr {
    24 namespace sql {
    24 namespace sql {
    25 
    25 
    26 void Connection::begin() {
    26 void Connection::begin() {
    27 	sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr);
    27 	sqlite3_exec((sqlite3*) db, "BEGIN", nullptr, nullptr, nullptr);
    28 }
    28 }
    29 
    29 
    30 Connection::Connection(const char* filename) {
    30 Connection::Connection(const char* filename) {
    31 	int result = sqlite3_open(filename, &db);
    31 	int result = sqlite3_open(filename, (sqlite3**) & db);
    32 	if (result != SQLITE_OK) {
    32 	if (result != SQLITE_OK) {
    33 		sqlite3_close(db);
    33 		sqlite3_close((sqlite3*) db);
    34 		throw SqlException(L"Unable to open SQLite database.");
    34 		throw SqlException(L"Unable to open SQLite database.");
    35 	}
    35 	}
    36 	begin();
    36 	begin();
    37 }
    37 }
    38 
    38 
    39 Connection::~Connection() {
    39 Connection::~Connection() {
    40 	sqlite3_close(db);
    40 	sqlite3_close((sqlite3*) db);
    41 }
    41 }
    42 
    42 
    43 PreparedStatement* Connection::prepareStatement(const char* sql) {
    43 PreparedStatement* Connection::prepareStatement(const char* sql) {
    44 	const char* remaining;
    44 	const char* remaining;
    45 	sqlite3_stmt *stmt;
    45 	sqlite3_stmt *stmt;
    46 	int result = sqlite3_prepare(db, sql, -1, &stmt, &remaining);
    46 	int result = sqlite3_prepare((sqlite3*) db, sql, -1, &stmt, &remaining);
    47 	if (result == SQLITE_OK) return new PreparedStatement(stmt);
    47 	if (result == SQLITE_OK) return new PreparedStatement(stmt);
    48 	else throw SqlException(L"Unable to prepare SQLite statement.");
    48 	else throw SqlException(L"Unable to prepare SQLite statement.");
    49 }
    49 }
    50 
    50 
    51 bool Connection::getAutoCommit() {
    51 bool Connection::getAutoCommit() {
    52 	return sqlite3_get_autocommit(db);
    52 	return sqlite3_get_autocommit((sqlite3*) db);
    53 }
    53 }
    54 
    54 
    55 void Connection::setAutoCommit(bool autoCommit) {
    55 void Connection::setAutoCommit(bool autoCommit) {
    56 	bool autoCommitOld = getAutoCommit();
    56 	bool autoCommitOld = getAutoCommit();
    57 	if (autoCommit && !autoCommitOld) commit();
    57 	if (autoCommit && !autoCommitOld) commit();
    58 	else if (!autoCommit && autoCommitOld) begin();
    58 	else if (!autoCommit && autoCommitOld) begin();
    59 }
    59 }
    60 
    60 
    61 void Connection::commit() {
    61 void Connection::commit() {
    62 	sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr);
    62 	sqlite3_exec((sqlite3*) db, "COMMIT", nullptr, nullptr, nullptr);
    63 }
    63 }
    64 
    64 
    65 void Connection::rollback() {
    65 void Connection::rollback() {
    66 	sqlite3_exec(db, "ROLLBACK", nullptr, nullptr, nullptr);
    66 	sqlite3_exec((sqlite3*) db, "ROLLBACK", nullptr, nullptr, nullptr);
    67 }
    67 }
    68 
    68 
    69 }
    69 }
    70 }
    70 }
    71 }
    71 }