2018-03-13 23:22:17 +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>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
#include <vector>
|
2018-04-16 01:00:17 +02:00
|
|
|
#include <list>
|
2018-03-30 21:34:45 +02:00
|
|
|
#include "model.h"
|
|
|
|
#include "textstream/textstream.h"
|
|
|
|
#include "dbconnector.h"
|
|
|
|
#include "modelconnector.h"
|
|
|
|
|
2018-03-13 23:22:17 +01:00
|
|
|
|
|
|
|
namespace morm
|
|
|
|
{
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
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;
|
2018-04-16 01:00:17 +02:00
|
|
|
this->model_connector = &model_connector;
|
2018-03-30 21:34:45 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-16 01:00:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
Finder<ModelClass> & find()
|
|
|
|
{
|
|
|
|
if( model_connector )
|
|
|
|
{
|
2018-04-16 01:00:17 +02:00
|
|
|
if( !out_stream )
|
2018-03-30 21:34:45 +02:00
|
|
|
{
|
|
|
|
out_stream = model_connector->get_stream();
|
|
|
|
}
|
|
|
|
|
2018-04-16 01:00:17 +02:00
|
|
|
if( out_stream )
|
|
|
|
{
|
|
|
|
set_db_expression();
|
2018-03-30 21:34:45 +02:00
|
|
|
|
2018-04-16 01:00:17 +02:00
|
|
|
out_stream->clear();
|
|
|
|
model.set_connector(*model_connector);
|
|
|
|
model.clear();
|
2018-03-30 21:34:45 +02:00
|
|
|
|
2018-04-16 01:00:17 +02:00
|
|
|
(*out_stream) << "SELECT ";
|
|
|
|
model_connector->generate_select_columns(*out_stream, model);
|
|
|
|
(*out_stream) << " FROM ";
|
|
|
|
model.table_name(*out_stream);
|
|
|
|
}
|
2018-03-30 21:34:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2018-04-16 01:00:17 +02:00
|
|
|
if( db_expression && out_stream )
|
2018-03-30 21:34:45 +02:00
|
|
|
{
|
2018-04-16 01:00:17 +02:00
|
|
|
db_expression->group_or(*out_stream);
|
2018-03-30 21:34:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Finder<ModelClass> & group_and()
|
|
|
|
{
|
2018-04-16 01:00:17 +02:00
|
|
|
if( db_expression && out_stream )
|
2018-03-30 21:34:45 +02:00
|
|
|
{
|
2018-04-16 01:00:17 +02:00
|
|
|
db_expression->group_and(*out_stream);
|
2018-03-30 21:34:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Finder<ModelClass> & group_end()
|
|
|
|
{
|
2018-04-16 01:00:17 +02:00
|
|
|
if( db_expression && out_stream )
|
2018-03-30 21:34:45 +02:00
|
|
|
{
|
2018-04-16 01:00:17 +02:00
|
|
|
db_expression->group_end(*out_stream);
|
2018-03-30 21:34:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-04-16 01:00:17 +02:00
|
|
|
db_expression->field(*out_stream, field_name, field_value, false, false, false);
|
2018-03-30 21:34:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-04-16 01:00:17 +02:00
|
|
|
db_expression->field(*out_stream, field_name, field_value, false, false, false);
|
2018-03-30 21:34:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-04-16 01:00:17 +02:00
|
|
|
db_expression->field(*out_stream, field_name, field_value, false, false, false);
|
2018-03-30 21:34:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2018-04-16 01:00:17 +02:00
|
|
|
db_expression->field(*out_stream, field_name, field_value, false, false, false);
|
2018-03-30 21:34:45 +02:00
|
|
|
}
|
2018-03-13 23:22:17 +01:00
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2018-03-13 23:22:17 +01:00
|
|
|
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
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);
|
2018-04-16 01:00:17 +02:00
|
|
|
db_expression->field(*out_stream, field_name, field_value, false, false, false);
|
2018-03-30 21:34:45 +02:00
|
|
|
}
|
2018-03-13 23:22:17 +01:00
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2018-03-13 23:22:17 +01:00
|
|
|
|
|
|
|
|
2018-04-16 01:00:17 +02:00
|
|
|
void get_list(std::list<ModelClass> & result, bool clear_list = true)
|
|
|
|
{
|
|
|
|
if( clear_list )
|
|
|
|
{
|
|
|
|
result.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( model_connector && out_stream )
|
|
|
|
{
|
|
|
|
DbConnector * db_connector = model_connector->get_db_connector();
|
|
|
|
|
|
|
|
if( db_connector )
|
|
|
|
{
|
|
|
|
bool res = db_connector->query_select(*out_stream);
|
|
|
|
|
|
|
|
if( res )
|
|
|
|
{
|
|
|
|
//ModelClass model;
|
|
|
|
//db_connector->map_values_from_query(model);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !res )
|
|
|
|
{
|
|
|
|
// put some log here?
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<ModelClass> get_list()
|
|
|
|
{
|
|
|
|
std::list<ModelClass> result;
|
|
|
|
get_list(result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
private:
|
2018-03-13 23:22:17 +01:00
|
|
|
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
PT::TextStream * out_stream;
|
|
|
|
ModelConnector * model_connector;
|
|
|
|
DbExpression * db_expression;
|
|
|
|
ModelClass model;
|
2018-03-13 23:22:17 +01:00
|
|
|
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
void set_db_expression()
|
|
|
|
{
|
|
|
|
if( model_connector )
|
|
|
|
{
|
|
|
|
DbConnector * db_connector = model_connector->get_db_connector();
|
2018-03-13 23:22:17 +01:00
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
if( db_connector )
|
|
|
|
{
|
|
|
|
db_expression = db_connector->get_expression();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 23:22:17 +01:00
|
|
|
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
};
|
2018-03-13 23:22:17 +01:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#endif
|