179 lines
7.1 KiB
C++
179 lines
7.1 KiB
C++
/*
|
|
* This file is a part of morm
|
|
* and is distributed under the 2-Clause BSD licence.
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
*/
|
|
|
|
/*
|
|
* 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.
|
|
*
|
|
*/
|
|
|
|
#ifndef headerfile_morm_dbconnector
|
|
#define headerfile_morm_dbconnector
|
|
|
|
#include "textstream/textstream.h"
|
|
#include "log/log.h"
|
|
#include "queryresult.h"
|
|
#include "ft.h"
|
|
|
|
|
|
namespace morm
|
|
{
|
|
class Model;
|
|
class DbExpression;
|
|
|
|
class DbConnector
|
|
{
|
|
|
|
public:
|
|
|
|
DbConnector();
|
|
DbConnector(const DbConnector &);
|
|
virtual ~DbConnector();
|
|
|
|
virtual void set_logger(PT::Log * log);
|
|
virtual void set_logger(PT::Log & log);
|
|
|
|
virtual void set_log_queries(bool log_queries);
|
|
|
|
DbExpression * get_expression();
|
|
|
|
//virtual void clear_last_query_result();
|
|
|
|
virtual void generate_select_columns(PT::TextStream & stream, Model & model);
|
|
virtual void generate_insert_query(PT::TextStream & stream, Model & model);
|
|
virtual void generate_update_query(PT::TextStream & stream, Model & model);
|
|
virtual void generate_remove_query(PT::TextStream & stream, Model & model);
|
|
|
|
virtual bool insert(PT::TextStream & stream, Model & model);
|
|
virtual bool update(PT::TextStream & stream, Model & model);
|
|
virtual bool remove(PT::TextStream & stream, Model & model);
|
|
|
|
//void ModelConnector::get_values_from_db(Model & model)
|
|
|
|
virtual QueryResult * create_query_result() = 0;
|
|
|
|
virtual bool query(const PT::TextStream & stream, QueryResult & query_result);
|
|
virtual bool query(const std::string & query_str, QueryResult & query_result);
|
|
virtual bool query(const char * query_str, QueryResult & query_result);
|
|
|
|
virtual bool query_select(const char * query_str, QueryResult & query_result);
|
|
virtual bool query_update(const char * query_str, QueryResult & query_result);
|
|
virtual bool query_insert(const char * query_str, QueryResult & query_result);
|
|
virtual bool query_remove(const char * query_str, QueryResult & query_result);
|
|
|
|
virtual bool query_select(const PT::TextStream & stream, QueryResult & query_result);
|
|
virtual bool query_update(const PT::TextStream & stream, QueryResult & query_result);
|
|
virtual bool query_insert(const PT::TextStream & stream, QueryResult & query_result);
|
|
virtual bool query_remove(const PT::TextStream & stream, QueryResult & query_result);
|
|
|
|
virtual void get_value(const char * value_str, char & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, unsigned char & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, wchar_t & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, std::wstring & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, std::string & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, bool & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, short & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, unsigned short & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, int & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, unsigned int & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, long & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, unsigned long & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, long long & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, unsigned long long & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, float & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, double & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, long double & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, PT::Date & field_value, FT field_type = FT::default_type);
|
|
virtual void get_value(const char * value_str, PT::Space & field_value, FT field_type = FT::default_type);
|
|
|
|
// add get_value for PT::TextStream and PT::WTextStream
|
|
|
|
template<typename FieldValue>
|
|
bool get_last_sequence(const wchar_t * sequence_table_name, FieldValue & field_value)
|
|
{
|
|
const char * val_str = query_last_sequence(sequence_table_name);
|
|
|
|
if( val_str )
|
|
{
|
|
get_value(val_str, field_value);
|
|
|
|
if( log && log_queries )
|
|
{
|
|
(*log) << PT::Log::log3 << "Morm: sequence value: " << field_value << PT::Log::logend;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
protected:
|
|
|
|
DbExpression * db_expression;
|
|
bool expression_allocated;
|
|
PT::Log * log;
|
|
bool log_queries;
|
|
|
|
|
|
virtual void allocate_default_expression() = 0;
|
|
virtual void allocate_default_expression_if_needed();
|
|
virtual void deallocate_expression();
|
|
|
|
virtual const char * query_last_sequence(const wchar_t * sequence_table_name);
|
|
|
|
virtual void unescape_hex_string(const char * str, std::string & out);
|
|
virtual void unescape_hex_string(const char * str, std::wstring & out, FT field_type);
|
|
|
|
virtual void unescape_bin_string(const char * str, std::string & out);
|
|
virtual void unescape_bin_string(const char * str, std::wstring & out, FT field_type);
|
|
|
|
virtual size_t unescape_hex_char(const char * value_str, char * utf8_str, size_t utf8_str_max_len);
|
|
virtual size_t unescape_hex_char(const char * value_str, wchar_t & field_value, FT field_type);
|
|
virtual size_t unescape_bin_char(const char * value_str, wchar_t & field_value, FT field_type);
|
|
|
|
private:
|
|
|
|
char unescape_hex_char_part(char hex);
|
|
char unescape_hex_char(char char1, char char2);
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|