--- a/nbproject/configurations.xml Tue Dec 24 17:36:57 2019 +0100
+++ b/nbproject/configurations.xml Wed Dec 25 00:57:26 2019 +0100
@@ -42,6 +42,7 @@
<logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">
<df root="." name="0">
<df name="src">
+ <in>PreparedStatement.cpp</in>
<in>SqlException.h</in>
<in>relpipe-tr-sql.cpp</in>
</df>
@@ -93,7 +94,11 @@
<preBuildFirst>true</preBuildFirst>
</preBuild>
</makefileType>
- <item path="src/relpipe-tr-sql.cpp" ex="false" tool="1" flavor2="0">
+ <item path="src/PreparedStatement.cpp" ex="false" tool="1" flavor2="0">
+ <ccTool flags="0">
+ </ccTool>
+ </item>
+ <item path="src/relpipe-tr-sql.cpp" ex="false" tool="1" flavor2="11">
<ccTool flags="0">
</ccTool>
</item>
--- a/src/CMakeLists.txt Tue Dec 24 17:36:57 2019 +0100
+++ b/src/CMakeLists.txt Wed Dec 25 00:57:26 2019 +0100
@@ -30,6 +30,7 @@
# Executable output:
add_executable(
${EXECUTABLE_FILE}
+ PreparedStatement.cpp
relpipe-tr-sql.cpp
)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/PreparedStatement.cpp Wed Dec 25 00:57:26 2019 +0100
@@ -0,0 +1,102 @@
+/**
+ * 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 "PreparedStatement.h"
+
+namespace relpipe {
+namespace tr {
+namespace sql {
+
+PreparedStatement::PreparedStatement(sqlite3_stmt* stmt) : stmt(stmt) {
+}
+
+PreparedStatement::~PreparedStatement() {
+ sqlite3_finalize(stmt);
+}
+
+void PreparedStatement::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 PreparedStatement::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 PreparedStatement::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 PreparedStatement::setNull(int parameterIndex) {
+ int result = sqlite3_bind_null(stmt, parameterIndex);
+ if (result != SQLITE_OK) throw SqlException(L"Unable to set SQLite parameter.");
+}
+
+bool PreparedStatement::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 PreparedStatement::reset() {
+ int result = sqlite3_reset(stmt);
+ if (result != SQLITE_OK) throw SqlException(L"Unable to reset SQLite prepared statement.");
+}
+
+int PreparedStatement::getColumnCount() {
+ return sqlite3_column_count(stmt);
+}
+
+std::string PreparedStatement::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 PreparedStatement::getColumType(int columnIndex, relpipe::writer::TypeId defaultType) {
+ 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 PreparedStatement::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)
+}
+
+}
+}
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/PreparedStatement.h Wed Dec 25 00:57:26 2019 +0100
@@ -0,0 +1,85 @@
+/**
+ * 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"
+
+namespace relpipe {
+namespace tr {
+namespace sql {
+
+using namespace std;
+using namespace relpipe;
+using namespace relpipe::reader;
+using namespace relpipe::reader::handlers;
+
+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 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;