# HG changeset patch # User František Kučera # Date 1577231846 -3600 # Node ID 49919a60c74778946ec6e118c02155a8d822d9ac # Parent ec793cb3e686a474736a6c8b83c287ca1f696510 move PreparedStatement class to separate .h and .cpp files diff -r ec793cb3e686 -r 49919a60c747 nbproject/configurations.xml --- 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 @@ + PreparedStatement.cpp SqlException.h relpipe-tr-sql.cpp @@ -93,7 +94,11 @@ true - + + + + + diff -r ec793cb3e686 -r 49919a60c747 src/CMakeLists.txt --- 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 ) diff -r ec793cb3e686 -r 49919a60c747 src/PreparedStatement.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 . + */ + +#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) +} + +} +} +} diff -r ec793cb3e686 -r 49919a60c747 src/PreparedStatement.h --- /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 . + */ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#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); + +}; + +} +} +} diff -r ec793cb3e686 -r 49919a60c747 src/SqlHandler.h --- 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;