src/SqlHandler.h
branchv_0
changeset 26 49919a60c747
parent 25 ec793cb3e686
child 27 9441a517fa1f
--- a/src/SqlHandler.h	Tue Dec 24 17:36:57 2019 +0100
+++ b/src/SqlHandler.h	Wed Dec 25 00:57:26 2019 +0100
@@ -38,6 +38,7 @@
 
 #include "Configuration.h"
 #include "SqlException.h"
+#include "PreparedStatement.h"
 
 namespace relpipe {
 namespace tr {
@@ -48,90 +49,6 @@
 using namespace relpipe::reader;
 using namespace relpipe::reader::handlers;
 
-class PreparedStatement {
-private:
-	sqlite3_stmt* stmt;
-
-public:
-
-	PreparedStatement(sqlite3_stmt* stmt) : stmt(stmt) {
-	}
-
-	virtual ~PreparedStatement() {
-		sqlite3_finalize(stmt);
-	}
-
-	void setBoolean(int parameterIndex, relpipe::reader::boolean_t value) {
-		int result = sqlite3_bind_int(stmt, parameterIndex, value);
-		if (result != SQLITE_OK) throw SqlException(L"Unable to set SQLite parameter.");
-	}
-
-	void setInteger(int parameterIndex, relpipe::reader::integer_t value) {
-		int result = sqlite3_bind_int64(stmt, parameterIndex, value);
-		if (result != SQLITE_OK) throw SqlException(L"Unable to set SQLite parameter.");
-	}
-
-	void setString(int parameterIndex, std::string value) {
-		int result = sqlite3_bind_text(stmt, parameterIndex, value.c_str(), -1, SQLITE_TRANSIENT);
-		if (result != SQLITE_OK) throw SqlException(L"Unable to set SQLite parameter.");
-	}
-
-	void setNull(int parameterIndex) {
-		int result = sqlite3_bind_null(stmt, parameterIndex);
-		if (result != SQLITE_OK) throw SqlException(L"Unable to set SQLite parameter.");
-	}
-
-	bool next() {
-		int result = sqlite3_step(stmt);
-		if (result == SQLITE_ROW) return true;
-		else if (result == SQLITE_DONE) return false;
-		else throw SqlException(L"Error while iterating over SQLite result.");
-	}
-
-	void reset() {
-		int result = sqlite3_reset(stmt);
-		if (result != SQLITE_OK) throw SqlException(L"Unable to reset SQLite prepared statement.");
-	}
-
-	int getColumnCount() {
-		return sqlite3_column_count(stmt);
-	}
-
-	std::string getColumName(int columnIndex) {
-		const char* name = sqlite3_column_name(stmt, columnIndex);
-		if (name) return name;
-		else throw SqlException(L"Unable to get SQLite column name.");
-	}
-
-	relpipe::writer::TypeId getColumType(int columnIndex, relpipe::writer::TypeId defaultType = relpipe::writer::TypeId::STRING) {
-		const char* type = sqlite3_column_decltype(stmt, columnIndex);
-
-		// TODO: sqlite3_column_decltype returns value only for columns of existing tables, not for dynamic expressions – SQLite uses dynamic types
-		// maybe we could write a function/module that returns result set metadata for given query (before executing it)
-		// or use at least explicit casts in SQL and modify sqlite3_column_decltype() function or add some new one to return such casted type
-		// 
-		// fprintf(stderr, "%d → %s\n", columnIndex, type);
-		// SELECT typeof(1+1); == "integer"
-		// https://www.sqlite.org/c3ref/column_decltype.html – sqlite3_column_decltype
-		// https://www.sqlite.org/c3ref/column_blob.html – sqlite3_column_type
-		// https://www.sqlite.org/datatype3.html – Datatypes In SQLite Version 3
-		// https://dba.stackexchange.com/questions/203220/sqlite-what-is-the-use-of-specifying-data-types
-		// https://www.mail-archive.com/sqlite-users@mailinglists.sqlite.org/msg118093.html
-
-		if (type == nullptr) return relpipe::writer::TypeId::STRING;
-		else if (strcmp(type, "integer") == 0) return relpipe::writer::TypeId::INTEGER;
-		else if (strcmp(type, "text") == 0) return relpipe::writer::TypeId::STRING;
-		else return defaultType;
-		// TODO: support also other data types
-	}
-
-	std::string getString(int columnIndex) {
-		const char* value = (const char*) sqlite3_column_text(stmt, columnIndex);
-		return value ? value : ""; // TODO: support NULL values (when supported in relpipe format)
-	}
-
-};
-
 class Connection {
 private:
 	sqlite3* db;