/* * This file is a part of morm * and is distributed under the 2-Clause BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * */ #include #include #include "space/spaceparser.h" #include "dbconnector.h" #include "dbexpression.h" #include "model.h" #include "utf8/utf8.h" #include "convert/convert.h" namespace morm { DbConnector::DbConnector() { db_expression = nullptr; expression_allocated = false; log = nullptr; log_queries = false; } DbConnector::DbConnector(const DbConnector &) { db_expression = nullptr; expression_allocated = false; log = nullptr; } DbConnector::~DbConnector() { deallocate_expression(); } void DbConnector::set_logger(pt::Log * log) { this->log = log; } void DbConnector::set_logger(pt::Log & log) { this->log = &log; } void DbConnector::set_log_queries(bool log_queries) { this->log_queries = log_queries; } bool DbConnector::query(const pt::TextStream & stream) { std::unique_ptr query_result_ptr(create_query_result()); return query(stream, *query_result_ptr); } bool DbConnector::query(const std::string & query_str) { std::unique_ptr query_result_ptr(create_query_result()); return query(query_str, *query_result_ptr); } bool DbConnector::query(const char * query_str) { std::unique_ptr query_result_ptr(create_query_result()); return query(query_str, *query_result_ptr); } bool DbConnector::query(const pt::TextStream & stream, QueryResult & query_result) { std::string query_str; stream.to_string(query_str); return query(query_str, query_result); } bool DbConnector::query(const std::string & query_str, QueryResult & query_result) { return query(query_str.c_str(), query_result); } bool DbConnector::query(const char * query_str, QueryResult & query_result) { // do query return false; } bool DbConnector::query_select(const char * query_str, QueryResult & query_result) { return query(query_str, query_result); } bool DbConnector::query_update(const char * query_str, QueryResult & query_result) { return query(query_str, query_result); } bool DbConnector::query_insert(const char * query_str, QueryResult & query_result) { return query(query_str, query_result); } bool DbConnector::query_remove(const char * query_str, QueryResult & query_result) { return query(query_str, query_result); } bool DbConnector::query_select(const pt::TextStream & stream, QueryResult & query_result) { return query(stream, query_result); } bool DbConnector::query_update(const pt::TextStream & stream, QueryResult & query_result) { return query(stream, query_result); } bool DbConnector::query_insert(const pt::TextStream & stream, QueryResult & query_result) { return query(stream, query_result); } bool DbConnector::query_remove(const pt::TextStream & stream, QueryResult & query_result) { return query(stream, query_result); } DbExpression * DbConnector::get_expression() { allocate_default_expression_if_needed(); return db_expression; } void DbConnector::generate_select_columns(pt::TextStream & stream, Model & model) { allocate_default_expression_if_needed(); if( db_expression ) { db_expression->clear(); db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS); db_expression->set_output_type(MORM_OUTPUT_TYPE_SELECT_COLUMNS); db_expression->allow_to_use_prefix(true); db_expression->generate_from_model(stream, model); } } void DbConnector::generate_insert_query(pt::TextStream & stream, Model & model) { allocate_default_expression_if_needed(); if( db_expression ) { db_expression->clear(); db_expression->allow_to_use_prefix(false); stream << "insert into "; db_expression->schema_table_to_stream(stream, model.model_env->schema_name, model.model_env->table_name); stream << " ("; db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS); db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_INSERT); db_expression->generate_from_model(stream, model); stream << ") values ("; db_expression->set_work_mode(MORM_WORK_MODE_MODEL_VALUES); db_expression->generate_from_model(stream, model); stream << ")"; } } void DbConnector::generate_update_query(pt::TextStream & stream, Model & model) { allocate_default_expression_if_needed(); if( db_expression ) { db_expression->clear(); db_expression->allow_to_use_prefix(false); stream << "update "; db_expression->schema_table_to_stream(stream, model.model_env->schema_name, model.model_env->table_name); stream << " set "; db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS_VALUES); db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE); db_expression->generate_from_model(stream, model); stream << " where "; db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS_VALUES); db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_PRIMARY_KEY); db_expression->generate_from_model(stream, model); } } void DbConnector::generate_remove_query(pt::TextStream & stream, Model & model) { allocate_default_expression_if_needed(); if( db_expression ) { db_expression->clear(); db_expression->allow_to_use_prefix(false); stream << "delete from "; db_expression->schema_table_to_stream(stream, model.model_env->schema_name, model.model_env->table_name); stream << " where "; db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS_VALUES); db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_PRIMARY_KEY); db_expression->generate_from_model(stream, model); } } bool DbConnector::insert(pt::TextStream & stream, Model & model) { std::unique_ptr query_result_ptr(create_query_result()); generate_insert_query(stream, model); return query_insert(stream, *query_result_ptr); } bool DbConnector::update(pt::TextStream & stream, Model & model) { std::unique_ptr query_result_ptr(create_query_result()); generate_update_query(stream, model); return query_update(stream, *query_result_ptr); } bool DbConnector::remove(pt::TextStream & stream, Model & model) { std::unique_ptr query_result_ptr(create_query_result()); generate_remove_query(stream, model); return query_remove(stream, *query_result_ptr); } void DbConnector::deallocate_expression() { if( expression_allocated ) { delete db_expression; db_expression = nullptr; expression_allocated = false; } } void DbConnector::allocate_default_expression_if_needed() { if( !db_expression ) { allocate_default_expression(); } } char DbConnector::unescape_hex_char_part(char hex) { if( hex>='0' && hex<='9' ) { return hex - '0'; } else if( hex>='a' && hex<='f' ) { return hex - 'a' + 10; } else if( hex>='A' && hex<='F' ) { return hex - 'A' + 10; } else { if( log ) { (*log) << pt::Log::log2 << "Morm: incorrect character when reading a hex string, char code: " << (int)(unsigned char)hex; if( hex >= 32 ) { (*log) << " '" << hex << "'"; } (*log) << pt::Log::logend; } } return 0; } char DbConnector::unescape_hex_char(char char1, char char2) { int c1 = unescape_hex_char_part(char1); int c2 = unescape_hex_char_part(char2); return static_cast(((c1 << 4) | c2)); } void DbConnector::unescape_hex_string(const char * str, std::string & out) { for(size_t i=0 ; str[i] != 0 ; i+=2 ) { out += unescape_hex_char(str[i], str[i+1]); } } void DbConnector::unescape_hex_string(const char * str, std::wstring & out, const FT & field_type) { if( field_type.use_utf8() ) { size_t len; wchar_t c; while( *str != 0 && (len = unescape_hex_char(str, c, field_type)) > 0 ) { out += c; str += len; } } else { for(size_t i=0 ; str[i] != 0 ; i+=2 ) { out += static_cast(static_cast(unescape_hex_char(str[i], str[i+1]))); } } } void DbConnector::unescape_bin_string(const char * str, std::string & out) { unescape_hex_string(str, out); } void DbConnector::unescape_bin_string(const char * str, std::wstring & out, const FT & field_type) { unescape_hex_string(str, out, field_type); } // returns how many characters have been provided to utf8_str buffer // min size of utf8_str should be 5 bytes (max 4 bytes for utf8 sequence + terminating null) size_t DbConnector::unescape_hex_char(const char * value_str, char * utf8_str, size_t utf8_str_max_len) { size_t value_str_index = 0; size_t utf8_str_index = 0; utf8_str[0] = 0; while( utf8_str_index + 1 < utf8_str_max_len ) { if( value_str[value_str_index] != 0 && value_str[value_str_index+1] != 0 ) { utf8_str[utf8_str_index] = unescape_hex_char(value_str[value_str_index], value_str[value_str_index+1]); utf8_str[utf8_str_index+1] = 0; } else { break; } value_str_index += 2; utf8_str_index += 1; } return utf8_str_index; } // CHECKME need to be tested // returns how many characters were used from value_str size_t DbConnector::unescape_hex_char(const char * value_str, wchar_t & field_value, const FT & field_type) { size_t len = 0; if( field_type.use_utf8() ) { char utf8_str[4 + 1]; // max utf8 sequence length + terminating zero size_t utf8_str_len = unescape_hex_char(value_str, utf8_str, sizeof(utf8_str) / sizeof(char)); int value_int; bool is_correct; len = pt::UTF8ToInt(utf8_str, utf8_str_len, value_int, is_correct); len = len * 2; if( is_correct ) { field_value = static_cast(value_int); } else { if( log ) { (*log) << pt::Log::log2 << "Morm: incorrect utf-8 sequence (ignoring)" << pt::Log::logend; } } } else { if( value_str[0] != 0 && value_str[1] != 0 ) { field_value = static_cast(static_cast(unescape_hex_char(value_str[0], value_str[1]))); len = 2; } else { if( log ) { (*log) << pt::Log::log2 << "Morm: unexpected end of string (ignoring)" << pt::Log::logend; } } } return len; } size_t DbConnector::unescape_bin_char(const char * value_str, wchar_t & field_value, const FT & field_type) { return unescape_hex_char(value_str, field_value, field_type); } // CHECKME need to be tested void DbConnector::get_value(const char * value_str, char & field_value, const FT & field_type) { wchar_t c; field_value = 0; get_value(value_str, c, field_type); if( field_type.use_utf8() ) { if( c <= 127 ) { field_value = static_cast(c); } else { if( log ) { (*log) << pt::Log::log2 << "Morm: a character greater than 127 cannot be stored in char type, code point: " << (int)c << " '" << c << "'" << pt::Log::logend; } } } else { field_value = static_cast(c); } } // CHECKME need to be tested void DbConnector::get_value(const char * value_str, unsigned char & field_value, const FT & field_type) { char tmp_char; get_value(value_str, tmp_char, field_type); field_value = static_cast(tmp_char); } // CHECKME need to be tested void DbConnector::get_value(const char * value_str, wchar_t & field_value, const FT & field_type) { field_value = 0; if( field_type.is_binary() ) { unescape_bin_char(value_str, field_value, field_type); } else if( field_type.is_hexadecimal() ) { unescape_hex_char(value_str, field_value, field_type); } else { if( field_type.use_utf8() ) { int value_int; bool is_correct; pt::UTF8ToInt(value_str, value_int, is_correct); if( is_correct ) { field_value = static_cast(value_int); } else { // report an error? } } else { field_value = static_cast((unsigned char)*value_str); } } } // CHECKME need to be tested void DbConnector::get_value(const char * value_str, std::wstring & field_value, const FT & field_type) { if( field_type.is_binary() ) { unescape_bin_string(value_str, field_value, field_type); } else if( field_type.is_hexadecimal() ) { unescape_hex_string(value_str, field_value, field_type); } else { if( field_type.use_utf8() ) { pt::UTF8ToWide(value_str, field_value); } else { for(size_t i=0 ; value_str[i] != 0 ; ++i) { field_value += static_cast(value_str[i]); } } } } // CHECKME need to be tested void DbConnector::get_value(const char * value_str, std::string & field_value, const FT & field_type) { if( field_type.is_binary() ) { unescape_bin_string(value_str, field_value); } else if( field_type.is_hexadecimal() ) { unescape_hex_string(value_str, field_value); } else { field_value = value_str; } } void DbConnector::get_value(const char * value_str, bool & field_value, const FT & field_type) { // IMPROVE ME // this 't' is locale dependent field_value = (value_str[0]=='t' || value_str[0]=='y' || value_str[0]=='1'); } void DbConnector::get_value(const char * value_str, short & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = (short)pt::Toi(value_str, 10); } void DbConnector::get_value(const char * value_str, unsigned short & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = (unsigned short)pt::Toui(value_str, 10); } void DbConnector::get_value(const char * value_str, int & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = pt::Toi(value_str, 10); } void DbConnector::get_value(const char * value_str, unsigned int & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = pt::Toui(value_str, 10); } void DbConnector::get_value(const char * value_str, long & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = pt::Tol(value_str, 10); } void DbConnector::get_value(const char * value_str, unsigned long & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = pt::Toul(value_str, 10); } void DbConnector::get_value(const char * value_str, long long & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = pt::Toll(value_str, 10); } void DbConnector::get_value(const char * value_str, unsigned long long & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = pt::Toull(value_str, 10); } void DbConnector::get_value(const char * value_str, float & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = strtof(value_str, 0); } void DbConnector::get_value(const char * value_str, double & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = strtod(value_str, 0); } void DbConnector::get_value(const char * value_str, long double & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = strtold(value_str, 0); } void DbConnector::get_value(const char * value_str, pt::Date & field_value, const FT & field_type) { // IMPROVE ME give some log if parsing failed field_value.Parse(value_str); } void DbConnector::get_value(const char * value_str, pt::Space & field_value, const FT & field_type) { field_value.clear(); if( *value_str != '\0' ) { pt::SpaceParser space_parser; space_parser.SetSpace(field_value); if( space_parser.ParseSpace(value_str) != pt::SpaceParser::ok ) { field_value.clear(); if( log ) { (*log) << pt::Log::log2 << "Morm: I cannot correctly parse the Space struct from the datebase" << ", the raw string is: " << value_str << pt::Log::logend; } } } } const char * DbConnector::query_last_sequence(const wchar_t * sequence_table_name) { return nullptr; } }