2018-03-23 20:26:57 +01:00
|
|
|
/*
|
|
|
|
* This file is a part of morm
|
|
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2021-03-11 12:22:37 +01:00
|
|
|
* Copyright (c) 2018-2021, Tomasz Sowa
|
2018-03-23 20:26:57 +01:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
#include <cstdlib>
|
2018-11-14 18:14:26 +01:00
|
|
|
#include <memory>
|
2021-03-11 18:40:32 +01:00
|
|
|
#include "space/spaceparser.h"
|
2018-03-23 20:26:57 +01:00
|
|
|
#include "dbconnector.h"
|
2018-07-05 11:51:26 +02:00
|
|
|
#include "dbexpression.h"
|
2018-03-23 20:26:57 +01:00
|
|
|
#include "model.h"
|
2018-04-17 00:46:25 +02:00
|
|
|
#include "utf8/utf8.h"
|
2019-01-07 05:33:31 +01:00
|
|
|
#include "convert/convert.h"
|
2018-03-23 20:26:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
namespace morm
|
|
|
|
{
|
|
|
|
|
|
|
|
DbConnector::DbConnector()
|
|
|
|
{
|
|
|
|
db_expression = nullptr;
|
|
|
|
expression_allocated = false;
|
2018-11-23 18:13:50 +01:00
|
|
|
log = nullptr;
|
2019-06-17 12:59:39 +02:00
|
|
|
log_queries = false;
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
DbConnector::DbConnector(const DbConnector &)
|
|
|
|
{
|
|
|
|
db_expression = nullptr;
|
|
|
|
expression_allocated = false;
|
2018-11-23 18:13:50 +01:00
|
|
|
log = nullptr;
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
|
|
|
|
DbConnector::~DbConnector()
|
|
|
|
{
|
|
|
|
deallocate_expression();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
void DbConnector::set_logger(pt::Log * log)
|
2018-04-23 17:46:21 +02:00
|
|
|
{
|
2018-11-23 18:13:50 +01:00
|
|
|
this->log = log;
|
2018-04-23 17:46:21 +02:00
|
|
|
}
|
|
|
|
|
2019-06-17 12:59:39 +02:00
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
void DbConnector::set_logger(pt::Log & log)
|
2018-04-23 17:46:21 +02:00
|
|
|
{
|
2018-11-23 18:13:50 +01:00
|
|
|
this->log = &log;
|
2018-04-23 17:46:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-17 12:59:39 +02:00
|
|
|
void DbConnector::set_log_queries(bool log_queries)
|
|
|
|
{
|
|
|
|
this->log_queries = log_queries;
|
|
|
|
}
|
|
|
|
|
2018-04-23 17:46:21 +02:00
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
bool DbConnector::query(const pt::TextStream & stream)
|
2021-05-14 03:24:53 +02:00
|
|
|
{
|
|
|
|
std::unique_ptr<QueryResult> query_result_ptr(create_query_result());
|
|
|
|
return query(stream, *query_result_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DbConnector::query(const std::string & query_str)
|
|
|
|
{
|
|
|
|
std::unique_ptr<QueryResult> query_result_ptr(create_query_result());
|
|
|
|
return query(query_str, *query_result_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DbConnector::query(const char * query_str)
|
|
|
|
{
|
|
|
|
std::unique_ptr<QueryResult> query_result_ptr(create_query_result());
|
|
|
|
return query(query_str, *query_result_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
bool DbConnector::query(const pt::TextStream & stream, QueryResult & query_result)
|
2018-04-18 12:22:01 +02:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
std::string query_str;
|
2021-06-20 17:49:54 +02:00
|
|
|
stream.to_str(query_str);
|
2018-03-23 20:26:57 +01:00
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(query_str, query_result);
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query(const std::string & query_str, QueryResult & query_result)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(query_str.c_str(), query_result);
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query(const char * query_str, QueryResult & query_result)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
2018-04-16 01:00:17 +02:00
|
|
|
// do query
|
2018-03-23 20:26:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-16 01:00:17 +02:00
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query_select(const char * query_str, QueryResult & query_result)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(query_str, query_result);
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query_update(const char * query_str, QueryResult & query_result)
|
2018-04-16 01:00:17 +02:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(query_str, query_result);
|
2018-04-16 01:00:17 +02:00
|
|
|
}
|
2018-03-23 20:26:57 +01:00
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query_insert(const char * query_str, QueryResult & query_result)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(query_str, query_result);
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query_remove(const char * query_str, QueryResult & query_result)
|
2018-04-22 03:26:46 +02:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(query_str, query_result);
|
2018-04-22 03:26:46 +02:00
|
|
|
}
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
bool DbConnector::query_select(const pt::TextStream & stream, QueryResult & query_result)
|
2018-04-22 03:26:46 +02:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(stream, query_result);
|
2018-04-22 03:26:46 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
bool DbConnector::query_update(const pt::TextStream & stream, QueryResult & query_result)
|
2018-04-22 03:26:46 +02:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(stream, query_result);
|
2018-04-22 03:26:46 +02:00
|
|
|
}
|
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
bool DbConnector::query_insert(const pt::TextStream & stream, QueryResult & query_result)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(stream, query_result);
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
2018-03-23 20:26:57 +01:00
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
bool DbConnector::query_remove(const pt::TextStream & stream, QueryResult & query_result)
|
2018-04-16 01:00:17 +02:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(stream, query_result);
|
2018-04-16 01:00:17 +02:00
|
|
|
}
|
2018-03-23 20:26:57 +01:00
|
|
|
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
DbExpression * DbConnector::get_expression()
|
|
|
|
{
|
|
|
|
allocate_default_expression_if_needed();
|
|
|
|
return db_expression;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
void DbConnector::generate_select_columns(pt::TextStream & stream, Model & model)
|
2018-03-30 21:34:45 +02:00
|
|
|
{
|
|
|
|
allocate_default_expression_if_needed();
|
|
|
|
|
|
|
|
if( db_expression )
|
|
|
|
{
|
2019-05-31 20:28:09 +02:00
|
|
|
db_expression->clear();
|
2018-03-30 21:34:45 +02:00
|
|
|
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS);
|
|
|
|
db_expression->set_output_type(MORM_OUTPUT_TYPE_SELECT_COLUMNS);
|
2019-05-31 20:28:09 +02:00
|
|
|
db_expression->allow_to_use_prefix(true);
|
2018-03-30 21:34:45 +02:00
|
|
|
db_expression->generate_from_model(stream, model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
void DbConnector::generate_insert_query(pt::TextStream & stream, Model & model)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
|
|
|
allocate_default_expression_if_needed();
|
|
|
|
|
|
|
|
if( db_expression )
|
|
|
|
{
|
2019-05-31 20:28:09 +02:00
|
|
|
db_expression->clear();
|
2019-06-17 12:59:39 +02:00
|
|
|
db_expression->allow_to_use_prefix(false);
|
2019-05-31 20:28:09 +02:00
|
|
|
|
2021-05-13 02:32:03 +02:00
|
|
|
stream << "insert into ";
|
|
|
|
db_expression->schema_table_to_stream(stream, model.model_env->schema_name, model.model_env->table_name);
|
2018-04-22 03:26:46 +02:00
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
stream << " (";
|
|
|
|
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS);
|
2019-08-21 19:42:50 +02:00
|
|
|
db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_INSERT);
|
2018-03-23 20:26:57 +01:00
|
|
|
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 << ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
void DbConnector::generate_update_query(pt::TextStream & stream, Model & model)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
|
|
|
allocate_default_expression_if_needed();
|
|
|
|
|
|
|
|
if( db_expression )
|
|
|
|
{
|
2019-05-31 20:28:09 +02:00
|
|
|
db_expression->clear();
|
2019-06-17 12:59:39 +02:00
|
|
|
db_expression->allow_to_use_prefix(false);
|
2019-05-31 20:28:09 +02:00
|
|
|
|
2021-05-13 02:32:03 +02:00
|
|
|
stream << "update ";
|
|
|
|
db_expression->schema_table_to_stream(stream, model.model_env->schema_name, model.model_env->table_name);
|
2018-03-23 20:26:57 +01:00
|
|
|
|
2018-04-22 03:26:46 +02:00
|
|
|
stream << " set ";
|
2018-04-17 00:46:25 +02:00
|
|
|
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS_VALUES);
|
2018-04-22 03:26:46 +02:00
|
|
|
db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE);
|
2018-03-23 20:26:57 +01:00
|
|
|
db_expression->generate_from_model(stream, model);
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
stream << " where ";
|
2018-03-23 20:26:57 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
void DbConnector::generate_remove_query(pt::TextStream & stream, Model & model)
|
2018-04-22 03:26:46 +02:00
|
|
|
{
|
|
|
|
allocate_default_expression_if_needed();
|
|
|
|
|
|
|
|
if( db_expression )
|
|
|
|
{
|
2019-05-31 20:28:09 +02:00
|
|
|
db_expression->clear();
|
2019-06-17 12:59:39 +02:00
|
|
|
db_expression->allow_to_use_prefix(false);
|
2019-05-31 20:28:09 +02:00
|
|
|
|
2021-05-13 02:32:03 +02:00
|
|
|
stream << "delete from ";
|
|
|
|
db_expression->schema_table_to_stream(stream, model.model_env->schema_name, model.model_env->table_name);
|
2021-03-11 12:22:37 +01:00
|
|
|
|
2018-04-22 03:26:46 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2018-03-23 20:26:57 +01:00
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
bool DbConnector::insert(pt::TextStream & stream, Model & model)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
std::unique_ptr<QueryResult> query_result_ptr(create_query_result());
|
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
generate_insert_query(stream, model);
|
2018-11-14 18:14:26 +01:00
|
|
|
return query_insert(stream, *query_result_ptr);
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
bool DbConnector::update(pt::TextStream & stream, Model & model)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
std::unique_ptr<QueryResult> query_result_ptr(create_query_result());
|
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
generate_update_query(stream, model);
|
2018-11-14 18:14:26 +01:00
|
|
|
return query_update(stream, *query_result_ptr);
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
bool DbConnector::remove(pt::TextStream & stream, Model & model)
|
2018-04-22 03:26:46 +02:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
std::unique_ptr<QueryResult> query_result_ptr(create_query_result());
|
|
|
|
|
2018-04-22 03:26:46 +02:00
|
|
|
generate_remove_query(stream, model);
|
2018-11-14 18:14:26 +01:00
|
|
|
return query_remove(stream, *query_result_ptr);
|
2018-04-22 03:26:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
|
2021-05-11 22:11:31 +02:00
|
|
|
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 )
|
|
|
|
{
|
2021-05-20 16:25:01 +02:00
|
|
|
(*log) << pt::Log::log2 << "Morm: incorrect character when reading a hex string, char code: " << (int)(unsigned char)hex;
|
2021-05-11 22:11:31 +02:00
|
|
|
|
|
|
|
if( hex >= 32 )
|
|
|
|
{
|
|
|
|
(*log) << " '" << hex << "'";
|
|
|
|
}
|
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
(*log) << pt::Log::logend;
|
2021-05-11 22:11:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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<char>(((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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::unescape_hex_string(const char * str, std::wstring & out, const FT & field_type)
|
2021-05-11 22:11:31 +02:00
|
|
|
{
|
|
|
|
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<wchar_t>(static_cast<unsigned char>(unescape_hex_char(str[i], str[i+1])));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::unescape_bin_string(const char * str, std::string & out)
|
|
|
|
{
|
|
|
|
unescape_hex_string(str, out);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::unescape_bin_string(const char * str, std::wstring & out, const FT & field_type)
|
2021-05-11 22:11:31 +02:00
|
|
|
{
|
|
|
|
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
|
2021-05-12 04:53:23 +02:00
|
|
|
size_t DbConnector::unescape_hex_char(const char * value_str, wchar_t & field_value, const FT & field_type)
|
2021-05-11 22:11:31 +02:00
|
|
|
{
|
|
|
|
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;
|
2021-05-21 00:32:29 +02:00
|
|
|
len = pt::utf8_to_int(utf8_str, utf8_str_len, value_int, is_correct);
|
2021-05-11 22:11:31 +02:00
|
|
|
len = len * 2;
|
|
|
|
|
|
|
|
if( is_correct )
|
|
|
|
{
|
|
|
|
field_value = static_cast<wchar_t>(value_int);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( log )
|
|
|
|
{
|
2021-05-20 16:25:01 +02:00
|
|
|
(*log) << pt::Log::log2 << "Morm: incorrect utf-8 sequence (ignoring)" << pt::Log::logend;
|
2021-05-11 22:11:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( value_str[0] != 0 && value_str[1] != 0 )
|
|
|
|
{
|
|
|
|
field_value = static_cast<wchar_t>(static_cast<unsigned char>(unescape_hex_char(value_str[0], value_str[1])));
|
|
|
|
len = 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( log )
|
|
|
|
{
|
2021-05-20 16:25:01 +02:00
|
|
|
(*log) << pt::Log::log2 << "Morm: unexpected end of string (ignoring)" << pt::Log::logend;
|
2021-05-11 22:11:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-17 00:46:25 +02:00
|
|
|
|
2021-05-11 22:11:31 +02:00
|
|
|
return len;
|
|
|
|
}
|
2018-04-17 00:46:25 +02:00
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
size_t DbConnector::unescape_bin_char(const char * value_str, wchar_t & field_value, const FT & field_type)
|
2021-05-11 22:11:31 +02:00
|
|
|
{
|
|
|
|
return unescape_hex_char(value_str, field_value, field_type);
|
|
|
|
}
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
|
|
|
|
|
2021-05-11 22:11:31 +02:00
|
|
|
// CHECKME need to be tested
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, char & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
2021-05-11 22:11:31 +02:00
|
|
|
wchar_t c;
|
2018-04-17 00:46:25 +02:00
|
|
|
|
2021-05-11 22:11:31 +02:00
|
|
|
field_value = 0;
|
|
|
|
get_value(value_str, c, field_type);
|
|
|
|
|
|
|
|
if( field_type.use_utf8() )
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
2021-05-11 22:11:31 +02:00
|
|
|
if( c <= 127 )
|
|
|
|
{
|
|
|
|
field_value = static_cast<char>(c);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( log )
|
|
|
|
{
|
2021-05-20 16:25:01 +02:00
|
|
|
(*log) << pt::Log::log2 << "Morm: a character greater than 127 cannot be stored in char type, code point: "
|
|
|
|
<< (int)c << " '" << c << "'" << pt::Log::logend;
|
2021-05-11 22:11:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
field_value = static_cast<char>(c);
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-03 18:55:06 +02:00
|
|
|
|
2021-05-11 22:11:31 +02:00
|
|
|
// CHECKME need to be tested
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, unsigned char & field_value, const FT & field_type)
|
2021-05-11 22:11:31 +02:00
|
|
|
{
|
|
|
|
char tmp_char;
|
|
|
|
get_value(value_str, tmp_char, field_type);
|
|
|
|
|
|
|
|
field_value = static_cast<unsigned char>(tmp_char);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// CHECKME need to be tested
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, wchar_t & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
2021-05-11 22:11:31 +02:00
|
|
|
field_value = 0;
|
2018-04-17 00:46:25 +02:00
|
|
|
|
2021-05-11 22:11:31 +02:00
|
|
|
if( field_type.is_binary() )
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
2021-05-11 22:11:31 +02:00
|
|
|
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;
|
|
|
|
|
2021-05-21 00:32:29 +02:00
|
|
|
pt::utf8_to_int(value_str, value_int, is_correct);
|
2021-05-11 22:11:31 +02:00
|
|
|
|
|
|
|
if( is_correct )
|
|
|
|
{
|
|
|
|
field_value = static_cast<wchar_t>(value_int);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// report an error?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
field_value = static_cast<wchar_t>((unsigned char)*value_str);
|
|
|
|
}
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-11 22:11:31 +02:00
|
|
|
|
|
|
|
// CHECKME need to be tested
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, std::wstring & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
2021-05-11 22:11:31 +02:00
|
|
|
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() )
|
|
|
|
{
|
2021-05-21 00:32:29 +02:00
|
|
|
pt::utf8_to_wide(value_str, field_value);
|
2021-05-11 22:11:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(size_t i=0 ; value_str[i] != 0 ; ++i)
|
|
|
|
{
|
|
|
|
field_value += static_cast<wchar_t>(value_str[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-11 22:11:31 +02:00
|
|
|
// CHECKME need to be tested
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, std::string & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
2021-05-11 22:11:31 +02:00
|
|
|
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;
|
|
|
|
}
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-01 19:34:34 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, std::string_view & field_value, const FT & field_type)
|
|
|
|
{
|
|
|
|
field_value = value_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, bool & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
|
|
|
// IMPROVE ME
|
|
|
|
// this 't' is locale dependent
|
|
|
|
field_value = (value_str[0]=='t' || value_str[0]=='y' || value_str[0]=='1');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, short & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
2021-05-20 16:25:01 +02:00
|
|
|
field_value = (short)pt::Toi(value_str, 10);
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, unsigned short & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
2021-05-20 16:25:01 +02:00
|
|
|
field_value = (unsigned short)pt::Toui(value_str, 10);
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, int & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
2021-05-20 16:25:01 +02:00
|
|
|
field_value = pt::Toi(value_str, 10);
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, unsigned int & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
2021-05-20 16:25:01 +02:00
|
|
|
field_value = pt::Toui(value_str, 10);
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, long & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
2021-05-20 16:25:01 +02:00
|
|
|
field_value = pt::Tol(value_str, 10);
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, unsigned long & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
2021-05-20 16:25:01 +02:00
|
|
|
field_value = pt::Toul(value_str, 10);
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, long long & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
2021-05-20 16:25:01 +02:00
|
|
|
field_value = pt::Toll(value_str, 10);
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, unsigned long long & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
2021-05-20 16:25:01 +02:00
|
|
|
field_value = pt::Toull(value_str, 10);
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, float & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
|
|
|
field_value = strtof(value_str, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, double & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
|
|
|
field_value = strtod(value_str, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, long double & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
|
|
|
field_value = strtold(value_str, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, pt::Date & field_value, const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
|
|
|
// IMPROVE ME give some log if parsing failed
|
2018-04-18 19:52:09 +02:00
|
|
|
field_value.Parse(value_str);
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, pt::Space & field_value, const FT & field_type)
|
2021-03-11 18:40:32 +01:00
|
|
|
{
|
2021-04-08 17:21:12 +02:00
|
|
|
field_value.clear();
|
2021-03-11 18:40:32 +01:00
|
|
|
|
|
|
|
if( *value_str != '\0' )
|
|
|
|
{
|
2021-05-20 16:25:01 +02:00
|
|
|
pt::SpaceParser space_parser;
|
2021-03-11 18:40:32 +01:00
|
|
|
|
2021-08-12 21:56:32 +02:00
|
|
|
if( field_type.is_space() )
|
2021-03-11 18:40:32 +01:00
|
|
|
{
|
2021-08-12 21:56:32 +02:00
|
|
|
if( space_parser.parse_space(value_str, field_value) != pt::SpaceParser::ok )
|
|
|
|
{
|
|
|
|
field_value.clear();
|
2021-03-11 18:40:32 +01:00
|
|
|
|
2021-08-12 21:56:32 +02:00
|
|
|
if( log )
|
|
|
|
{
|
|
|
|
(*log) << pt::Log::log2 << "Morm: I cannot correctly parse the Space struct (space format) from the datebase"
|
|
|
|
<< ", the raw string was: " << value_str << pt::Log::logend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( space_parser.parse_json(value_str, field_value) != pt::SpaceParser::ok )
|
2021-03-11 18:40:32 +01:00
|
|
|
{
|
2021-08-12 21:56:32 +02:00
|
|
|
field_value.clear();
|
|
|
|
|
|
|
|
if( log )
|
|
|
|
{
|
|
|
|
(*log) << pt::Log::log2 << "Morm: I cannot correctly parse the Space struct (json format) from the datebase"
|
|
|
|
<< ", the raw string was: " << value_str << pt::Log::logend;
|
|
|
|
}
|
2021-03-11 18:40:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
const char * DbConnector::query_last_sequence(const wchar_t * sequence_table_name)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-03-30 21:34:45 +02:00
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|