# HG changeset patch # User František Kučera # Date 1577232461 -3600 # Node ID 9441a517fa1f1c413bf5ff86b909dc166408b949 # Parent 49919a60c74778946ec6e118c02155a8d822d9ac move Connection class to separate .h and .cpp files diff -r 49919a60c747 -r 9441a517fa1f nbproject/configurations.xml --- a/nbproject/configurations.xml Wed Dec 25 00:57:26 2019 +0100 +++ b/nbproject/configurations.xml Wed Dec 25 01:07:41 2019 +0100 @@ -42,6 +42,7 @@ + Connection.cpp PreparedStatement.cpp SqlException.h relpipe-tr-sql.cpp @@ -94,6 +95,10 @@ true + + + + diff -r 49919a60c747 -r 9441a517fa1f src/CMakeLists.txt --- a/src/CMakeLists.txt Wed Dec 25 00:57:26 2019 +0100 +++ b/src/CMakeLists.txt Wed Dec 25 01:07:41 2019 +0100 @@ -31,6 +31,7 @@ add_executable( ${EXECUTABLE_FILE} PreparedStatement.cpp + Connection.cpp relpipe-tr-sql.cpp ) diff -r 49919a60c747 -r 9441a517fa1f src/Connection.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Connection.cpp Wed Dec 25 01:07:41 2019 +0100 @@ -0,0 +1,69 @@ +/** + * Relational pipes + * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "Connection.h" + +namespace relpipe { +namespace tr { +namespace sql { + +void Connection::begin() { + sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr); +} + +Connection::Connection(const char* filename) { + int result = sqlite3_open(filename, &db); + if (result != SQLITE_OK) { + sqlite3_close(db); + throw SqlException(L"Unable to open SQLite database."); + } + begin(); +} + +Connection::~Connection() { + sqlite3_close(db); +} + +PreparedStatement* Connection::prepareStatement(const char* sql) { + const char* remaining; + sqlite3_stmt *stmt; + int result = sqlite3_prepare(db, sql, -1, &stmt, &remaining); + if (result == SQLITE_OK) return new PreparedStatement(stmt); + else throw SqlException(L"Unable to prepare SQLite statement."); +} + +bool Connection::getAutoCommit() { + return sqlite3_get_autocommit(db); +} + +void Connection::setAutoCommit(bool autoCommit) { + bool autoCommitOld = getAutoCommit(); + if (autoCommit && !autoCommitOld) commit(); + else if (!autoCommit && autoCommitOld) begin(); +} + +void Connection::commit() { + sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr); +} + +void Connection::rollback() { + sqlite3_exec(db, "ROLLBACK", nullptr, nullptr, nullptr); +} + +} +} +} diff -r 49919a60c747 -r 9441a517fa1f src/Connection.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/Connection.h Wed Dec 25 01:07:41 2019 +0100 @@ -0,0 +1,68 @@ +/** + * Relational pipes + * Copyright © 2019 František Kučera (Frantovo.cz, GlobalCode.info) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#include "Configuration.h" +#include "SqlException.h" +#include "PreparedStatement.h" + +namespace relpipe { +namespace tr { +namespace sql { + +using namespace std; +using namespace relpipe; +using namespace relpipe::reader; +using namespace relpipe::reader::handlers; + +class Connection { +private: + sqlite3* db; + void begin(); +public: + Connection(const char* filename); + virtual ~Connection(); + PreparedStatement* prepareStatement(const char* sql); + bool getAutoCommit(); + void setAutoCommit(bool autoCommit); + void commit(); + void rollback(); +}; + +} +} +} diff -r 49919a60c747 -r 9441a517fa1f src/PreparedStatement.h --- a/src/PreparedStatement.h Wed Dec 25 00:57:26 2019 +0100 +++ b/src/PreparedStatement.h Wed Dec 25 01:07:41 2019 +0100 @@ -51,33 +51,19 @@ class PreparedStatement { private: sqlite3_stmt* stmt; - public: - PreparedStatement(sqlite3_stmt* stmt); - virtual ~PreparedStatement(); - void setBoolean(int parameterIndex, relpipe::reader::boolean_t value); - void setInteger(int parameterIndex, relpipe::reader::integer_t value); - void setString(int parameterIndex, std::string value); - void setNull(int parameterIndex); - bool next(); - void reset(); - int getColumnCount(); - std::string getColumName(int columnIndex); - relpipe::writer::TypeId getColumType(int columnIndex, relpipe::writer::TypeId defaultType = relpipe::writer::TypeId::STRING); - std::string getString(int columnIndex); - }; } diff -r 49919a60c747 -r 9441a517fa1f src/SqlHandler.h --- a/src/SqlHandler.h Wed Dec 25 00:57:26 2019 +0100 +++ b/src/SqlHandler.h Wed Dec 25 01:07:41 2019 +0100 @@ -39,6 +39,7 @@ #include "Configuration.h" #include "SqlException.h" #include "PreparedStatement.h" +#include "Connection.h" namespace relpipe { namespace tr { @@ -49,57 +50,6 @@ using namespace relpipe::reader; using namespace relpipe::reader::handlers; -class Connection { -private: - sqlite3* db; - - void begin() { - sqlite3_exec(db, "BEGIN", nullptr, nullptr, nullptr); - } - -public: - - Connection(const char* filename) { - int result = sqlite3_open(filename, &db); - if (result != SQLITE_OK) { - sqlite3_close(db); - throw SqlException(L"Unable to open SQLite database."); - } - begin(); - } - - virtual ~Connection() { - sqlite3_close(db); - } - - PreparedStatement* prepareStatement(const char* sql) { - const char* remaining; - sqlite3_stmt *stmt; - int result = sqlite3_prepare(db, sql, -1, &stmt, &remaining); - if (result == SQLITE_OK) return new PreparedStatement(stmt); - else throw SqlException(L"Unable to prepare SQLite statement."); - } - - bool getAutoCommit() { - return sqlite3_get_autocommit(db); - } - - void setAutoCommit(bool autoCommit) { - bool autoCommitOld = getAutoCommit(); - if (autoCommit && !autoCommitOld) commit(); - else if (!autoCommit && autoCommitOld) begin(); - } - - void commit() { - sqlite3_exec(db, "COMMIT", nullptr, nullptr, nullptr); - } - - void rollback() { - sqlite3_exec(db, "ROLLBACK", nullptr, nullptr, nullptr); - } - -}; - class SqlHandler : public RelationalReaderValueHandler { private: Configuration configuration;