morm/src/finder.h

457 lines
8.8 KiB
C
Raw Normal View History

/*
* 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, 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 <vector>
#include <list>
#include <set>
#include "model.h"
#include "textstream/textstream.h"
#include "dbconnector.h"
#include "modelconnector.h"
namespace morm
{
template<typename ModelClass>
class Finder
{
public:
Finder()
{
model_connector = nullptr;
out_stream = nullptr;
db_expression = nullptr;
}
Finder(ModelConnector & model_connector)
{
this->model_connector = &model_connector;
out_stream = nullptr;
db_expression = nullptr;
}
Finder(PT::TextStream & out_stream, ModelConnector & model_connector)
{
this->out_stream = &out_stream;
this->model_connector = &model_connector;
db_expression = nullptr;
}
Finder<ModelClass> & find(ModelConnector & model_connector)
{
this->model_connector = &model_connector;
return find();
}
Finder<ModelClass> & find(PT::TextStream & out_stream, ModelConnector & model_connector)
{
this->out_stream = &out_stream;
this->model_connector = &model_connector;
return find();
}
Finder<ModelClass> & find()
{
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);
model.clear();
(*out_stream) << "SELECT ";
model_connector->generate_select_columns(*out_stream, model);
(*out_stream) << " FROM ";
model.table_name(*out_stream);
}
}
return *this;
}
ModelClass find_one()
{
//model.set_object_exists(true);
// at the end
db_expression = nullptr;
return model;
}
std::vector<ModelClass> find_many()
{
std::vector<ModelClass> res;
// at the end
db_expression = nullptr;
return res;
}
Finder<ModelClass> & where()
{
if( out_stream && db_expression )
{
(*out_stream) << " WHERE ";
db_expression->prepare_to_where_clause();
}
return *this;
}
// may better names?
Finder<ModelClass> & group_or()
{
if( db_expression && out_stream )
{
db_expression->group_or(*out_stream);
}
return *this;
}
Finder<ModelClass> & group_and()
{
if( db_expression && out_stream )
{
db_expression->group_and(*out_stream);
}
return *this;
}
Finder<ModelClass> & group_end()
{
if( db_expression && out_stream )
{
db_expression->group_end(*out_stream);
}
return *this;
}
template<typename FieldValue>
Finder<ModelClass> & 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<typename FieldValue>
Finder<ModelClass> & 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<typename FieldValue>
Finder<ModelClass> & 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<typename FieldValue>
Finder<ModelClass> & 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<typename FieldValue>
Finder<ModelClass> & 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<typename FieldValue>
Finder<ModelClass> & in(const wchar_t * field_name, const std::set<FieldValue> & 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<typename FieldValue>
Finder<ModelClass> & in(const wchar_t * field_name, const std::list<FieldValue> & 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<typename FieldValue>
Finder<ModelClass> & in(const wchar_t * field_name, const std::vector<FieldValue> & 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<ModelClass> & raw_sql(const char * sql)
{
if( out_stream )
{
(*out_stream) << " " << sql << " ";
}
return *this;
}
bool get(ModelClass & result)
{
bool res = false;
result.clear();
result.set_object_exists(false);
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 )
{
result.set_object_exists(true);
result.set_connector(*model_connector);
result.before_select();
model_connector->map_values_from_query(result);
result.after_select();
}
if( !res )
{
// put some log here?
}
}
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<ModelClass> & 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)
{
model.clear();
model.set_object_exists(true);
model.set_connector(*model_connector);
model.before_select();
model_connector->map_values_from_query(model);
model.after_select();
result.push_back(model);
db_connector->advance_current_row();
}
}
if( !res )
{
// put some log here?
}
}
catch(...)
{
// throw or something?
// make sure to call db_connector->clear_last_query_result()
}
db_connector->clear_last_query_result();
}
}
return res;
}
std::list<ModelClass> get_list()
{
std::list<ModelClass> result;
get_list(result, false);
return result;
}
private:
PT::TextStream * out_stream;
ModelConnector * model_connector;
DbExpression * db_expression;
ModelClass model;
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