/* * This file is a part of morm * and is distributed under the 2-Clause BSD licence. * Author: Tomasz Sowa */ /* * Copyright (c) 2018, 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 "dbexpression.h" #include "textstream/textstream.h" #include "logger/logger.h" namespace morm { class Model; class DbConnector { public: DbConnector(); DbConnector(const DbConnector &); virtual ~DbConnector(); virtual void set_logger(PT::Logger * logger); virtual void set_logger(PT::Logger & logger); DbExpression * get_expression(); virtual void clear_last_query_result(); virtual void generate_select_columns(PT::TextStream & stream, Model & model, const std::wstring & column_prefix); 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 bool query(const PT::TextStream & stream); //virtual bool query(const std::wstring & query); virtual bool query(const std::string & query_str); //virtual bool query(const wchar_t * query); virtual bool query(const char * query_str); virtual bool query_select(const PT::TextStream & stream); virtual bool query_update(const PT::TextStream & stream); virtual bool query_insert(const PT::TextStream & stream); virtual bool query_remove(const PT::TextStream & stream); virtual size_t last_select_size(); virtual std::wstring get_last_query_error_msg(); virtual bool was_error_in_last_query(); // give me a better name virtual void set_current_row_at_beginning(); virtual void advance_current_row(); //virtual void map_values_from_query(Model & model); // these methods should be somewhere else // flat_parsers (json parser) can use them as well virtual void clear_value(char & field_value); virtual void clear_value(unsigned char & field_value); virtual void clear_value(std::wstring & field_value); virtual void clear_value(std::string & field_value); virtual void clear_value(bool & field_value); virtual void clear_value(short & field_value); virtual void clear_value(unsigned short & field_value); virtual void clear_value(int & field_value); virtual void clear_value(unsigned int & field_value); virtual void clear_value(long & field_value); virtual void clear_value(unsigned long & field_value); virtual void clear_value(long long & field_value); virtual void clear_value(unsigned long long & field_value); virtual void clear_value(float & field_value); virtual void clear_value(double & field_value); virtual void clear_value(long double & field_value); virtual void clear_value(PT::Date & field_value); virtual void clear_value(Model & field_value); template void clear_value(std::list & list) { list.clear(); } virtual void get_value(const char * value_str, char & field_value); virtual void get_value(const char * value_str, unsigned char & field_value); virtual void get_value(const char * value_str, std::wstring & field_value); virtual void get_value(const char * value_str, std::string & field_value); virtual void get_value(const char * value_str, bool & field_value); virtual void get_value(const char * value_str, short & field_value); virtual void get_value(const char * value_str, unsigned short & field_value); virtual void get_value(const char * value_str, int & field_value); virtual void get_value(const char * value_str, unsigned int & field_value); virtual void get_value(const char * value_str, long & field_value); virtual void get_value(const char * value_str, unsigned long & field_value); virtual void get_value(const char * value_str, long long & field_value); virtual void get_value(const char * value_str, unsigned long long & field_value); virtual void get_value(const char * value_str, float & field_value); virtual void get_value(const char * value_str, double & field_value); virtual void get_value(const char * value_str, long double & field_value); virtual void get_value(const char * value_str, PT::Date & field_value); template void get_value_by_field_name(const wchar_t * field_name, FieldValue & field_value) { const char * val_str = get_field_string_value(field_name); if( val_str ) { get_value(val_str, field_value); } } template void 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); } } protected: DbExpression * db_expression; bool expression_allocated; std::wstring last_query_error_msg; PT::Logger * logger; virtual void allocate_default_expression() = 0; virtual void allocate_default_expression_if_needed(); virtual void deallocate_expression(); virtual const char * get_field_string_value(const char * field_name); virtual const char * get_field_string_value(const wchar_t * field_name); virtual const char * query_last_sequence(const wchar_t * sequence_table_name); }; } #endif