/* * 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_finder #define headerfile_morm_finder #include #include #include #include "model.h" #include "textstream/textstream.h" #include "dbconnector.h" #include "modelconnector.h" namespace morm { template class Finder { public: Finder() { model_connector = nullptr; out_stream = nullptr; db_expression = nullptr; was_query_error = false; } Finder(PT::TextStream & out_stream) { this->out_stream = &out_stream; this->model_connector = nullptr; db_expression = nullptr; was_query_error = false; } Finder(ModelConnector & model_connector) { this->model_connector = &model_connector; out_stream = nullptr; db_expression = nullptr; was_query_error = false; } Finder(ModelConnector * model_connector) { this->model_connector = model_connector; out_stream = nullptr; db_expression = nullptr; was_query_error = false; } Finder(PT::TextStream & out_stream, ModelConnector & model_connector) { this->out_stream = &out_stream; this->model_connector = &model_connector; db_expression = nullptr; was_query_error = false; } Finder(PT::TextStream & out_stream, ModelConnector * model_connector) { this->out_stream = &out_stream; this->model_connector = model_connector; db_expression = nullptr; was_query_error = false; } bool was_error() { return was_query_error; } std::wstring get_error_msg() { return last_query_error; } Finder & select(ModelConnector & model_connector) { this->model_connector = &model_connector; return select(); } Finder & select(ModelConnector * model_connector) { this->model_connector = model_connector; return select(); } Finder & select(PT::TextStream & out_stream, ModelConnector & model_connector) { this->out_stream = &out_stream; this->model_connector = &model_connector; return select(); } Finder & select(PT::TextStream & out_stream, ModelConnector * model_connector) { this->out_stream = &out_stream; this->model_connector = model_connector; return select(); } Finder & prepare_to_select() { was_query_error = false; last_query_error.clear(); if( model_connector ) { if( !out_stream ) { out_stream = model_connector->get_stream(); } if( out_stream ) { set_db_expression(); out_stream->clear(); model.set_connector(model_connector); } else { was_query_error = true; last_query_error = L"out stream object is required"; } } else { was_query_error = true; last_query_error = L"model connector object is required"; } return *this; } Finder & select() { prepare_to_select(); if( model_connector && out_stream ) { (*out_stream) << "SELECT "; model_connector->generate_select_columns(*out_stream, model); (*out_stream) << " FROM "; model.table_name(*out_stream); (*out_stream) << " "; } return *this; } Finder & where() { if( out_stream && db_expression ) { (*out_stream) << " WHERE "; db_expression->prepare_to_where_clause(); } return *this; } Finder & group_or() { if( db_expression && out_stream ) { db_expression->group_or(*out_stream); } return *this; } Finder & group_and() { if( db_expression && out_stream ) { db_expression->group_and(*out_stream); } return *this; } Finder & group_end() { if( db_expression && out_stream ) { db_expression->group_end(*out_stream); } return *this; } template Finder & eq(const wchar_t * field_name, const FieldValue & field_value) { if( db_expression ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_EQ); db_expression->field(*out_stream, field_name, field_value, false, false, false); } return *this; } template Finder & lt(const wchar_t * field_name, const FieldValue & field_value) { if( db_expression ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_LT); db_expression->field(*out_stream, field_name, field_value, false, false, false); } return *this; } template Finder & gt(const wchar_t * field_name, const FieldValue & field_value) { if( db_expression ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_GT); db_expression->field(*out_stream, field_name, field_value, false, false, false); } return *this; } template Finder & le(const wchar_t * field_name, const FieldValue & field_value) { if( db_expression ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_LE); db_expression->field(*out_stream, field_name, field_value, false, false, false); } return *this; } template Finder & ge(const wchar_t * field_name, const FieldValue & field_value) { if( db_expression ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_GE); db_expression->field(*out_stream, field_name, field_value, false, false, false); } return *this; } template Finder & in(const wchar_t * field_name, const std::set & container) { if( db_expression ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_IN); db_expression->field_in(*out_stream, field_name, container); } return *this; } template Finder & in(const wchar_t * field_name, const std::list & container) { if( db_expression ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_IN); db_expression->field_in(*out_stream, field_name, container); } return *this; } template Finder & in(const wchar_t * field_name, const std::vector & container) { if( db_expression ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_IN); db_expression->field_in(*out_stream, field_name, container); } return *this; } Finder & raw_sql(const char * sql) { if( out_stream ) { (*out_stream) << " " << sql << " "; } return *this; } bool get(ModelClass & result) { bool res = false; result.clear(); // here has to be a model connector (it clears all fields) // what about setting a model connector for the result? // it can be a different object if( model_connector && out_stream ) { DbConnector * db_connector = model_connector->get_db_connector(); if( db_connector ) { try { result.before_select(); res = db_connector->query_select(*out_stream); if( res ) { result.set_save_mode(Model::DO_UPDATE_ON_SAVE); model_connector->map_values_from_query(result); result.after_select(); } else { result.after_select_failure(); } } catch(...) { // throw something? // if yes then make sure to call db_connector->clear_last_query_result(); } db_connector->clear_last_query_result(); } } return res; } ModelClass get() { get(model); return model; } bool get_list(std::list & result, bool clear_list = true) { bool res = false; if( clear_list ) { result.clear(); } if( model_connector && out_stream ) { DbConnector * db_connector = model_connector->get_db_connector(); if( db_connector ) { res = db_connector->query_select(*out_stream); try { if( res ) { size_t len = db_connector->last_select_size(); db_connector->set_current_row_at_beginning(); for(size_t i = 0 ; i < len ; ++i) { result.emplace_back(); // it returns a reference from c++17 ModelClass & added_model = result.back(); added_model.set_connector(model_connector); added_model.clear(); added_model.set_save_mode(Model::DO_UPDATE_ON_SAVE); added_model.before_select(); model_connector->map_values_from_query(added_model); added_model.after_select(); db_connector->advance_current_row(); } // if there was a failure we do not call after_select_failure() // because there are no any objects in the list } else { // log } } catch(...) { // throw or something? // make sure to call db_connector->clear_last_query_result() } db_connector->clear_last_query_result(); } } return res; } std::list get_list() { std::list result; get_list(result, false); return std::move(result); } private: PT::TextStream * out_stream; ModelConnector * model_connector; DbExpression * db_expression; ModelClass model; bool was_query_error; std::wstring last_query_error; void set_db_expression() { if( model_connector ) { DbConnector * db_connector = model_connector->get_db_connector(); if( db_connector ) { db_expression = db_connector->get_expression(); } } } }; } // namespace #endif