src/PreparedStatement.cpp
author František Kučera <franta-hg@frantovo.cz>
Wed, 25 Dec 2019 00:57:26 +0100
branchv_0
changeset 26 49919a60c747
parent 25 src/SqlHandler.h@ec793cb3e686
child 28 498d5d3406c3
permissions -rw-r--r--
move PreparedStatement class to separate .h and .cpp files

/**
 * 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)
}

}
}
}