move Connection class to separate .h and .cpp files v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Wed, 25 Dec 2019 01:07:41 +0100
branchv_0
changeset 27 9441a517fa1f
parent 26 49919a60c747
child 28 498d5d3406c3
move Connection class to separate .h and .cpp files
nbproject/configurations.xml
src/CMakeLists.txt
src/Connection.cpp
src/Connection.h
src/PreparedStatement.h
src/SqlHandler.h
--- 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 @@
   <logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">
     <df root="." name="0">
       <df name="src">
+        <in>Connection.cpp</in>
         <in>PreparedStatement.cpp</in>
         <in>SqlException.h</in>
         <in>relpipe-tr-sql.cpp</in>
@@ -94,6 +95,10 @@
           <preBuildFirst>true</preBuildFirst>
         </preBuild>
       </makefileType>
+      <item path="src/Connection.cpp" ex="false" tool="1" flavor2="0">
+        <ccTool flags="0">
+        </ccTool>
+      </item>
       <item path="src/PreparedStatement.cpp" ex="false" tool="1" flavor2="0">
         <ccTool flags="0">
         </ccTool>
--- 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
 )
 
--- /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 <http://www.gnu.org/licenses/>.
+ */
+
+#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);
+}
+
+}
+}
+}
--- /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 <http://www.gnu.org/licenses/>.
+ */
+#pragma once
+
+#include <memory>
+#include <string>
+#include <sstream>
+#include <regex>
+#include <vector>
+#include <locale>
+#include <codecvt>
+#include <unistd.h>
+#include <cassert>
+#include <sys/stat.h>
+
+#include <sqlite3.h>
+
+#include <relpipe/reader/typedefs.h>
+#include <relpipe/reader/TypeId.h>
+#include <relpipe/reader/handlers/RelationalReaderValueHandler.h>
+#include <relpipe/reader/handlers/AttributeMetadata.h>
+
+#include <relpipe/writer/Factory.h>
+
+#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();
+};
+
+}
+}
+}
--- 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);
-
 };
 
 }
--- 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;