342 lines
6.9 KiB
C++
342 lines
6.9 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-2019, 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_cursor
|
|
#define headerfile_morm_cursor
|
|
|
|
#include "queryresult.h"
|
|
#include "modelconnector.h"
|
|
|
|
|
|
namespace morm
|
|
{
|
|
|
|
template<typename ModelClass>
|
|
class Cursor
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
Cursor()
|
|
{
|
|
query_result = nullptr;
|
|
clear();
|
|
}
|
|
|
|
|
|
Cursor(const Cursor & c)
|
|
{
|
|
model_connector = c.model_connector;
|
|
model_data = c.model_data;
|
|
has_autogenerated_select = c.has_autogenerated_select;
|
|
query_result = c.query_result;
|
|
select_status = c.select_status;
|
|
|
|
if( query_result )
|
|
{
|
|
query_result->references_count += 1;
|
|
}
|
|
|
|
// helper doesn't have to be copied
|
|
cursor_helper.clear();
|
|
}
|
|
|
|
|
|
Cursor & operator=(const Cursor & c) = delete;
|
|
|
|
|
|
virtual ~Cursor()
|
|
{
|
|
if( query_result && query_result->references_count > 0 )
|
|
{
|
|
query_result->references_count -= 1;
|
|
}
|
|
|
|
clear();
|
|
}
|
|
|
|
|
|
virtual void clear()
|
|
{
|
|
if( query_result && query_result->references_count == 0 )
|
|
{
|
|
query_result->clear();
|
|
delete query_result;
|
|
}
|
|
|
|
model_connector = nullptr;
|
|
model_data = nullptr;
|
|
has_autogenerated_select = false;
|
|
cursor_helper.clear();
|
|
query_result = nullptr;
|
|
select_status = false;
|
|
|
|
}
|
|
|
|
|
|
virtual void set_model_connector(ModelConnector * model_connector)
|
|
{
|
|
this->model_connector = model_connector;
|
|
}
|
|
|
|
|
|
virtual void set_model_data(ModelData * model_data)
|
|
{
|
|
this->model_data = model_data;
|
|
}
|
|
|
|
|
|
virtual void set_query_result(QueryResult * query_result)
|
|
{
|
|
this->query_result = query_result;
|
|
}
|
|
|
|
|
|
virtual void set_select_status(bool select_status)
|
|
{
|
|
this->select_status = select_status;
|
|
}
|
|
|
|
|
|
virtual void set_has_autogenerated_select(bool has_autogenerated_select)
|
|
{
|
|
this->has_autogenerated_select = has_autogenerated_select;
|
|
}
|
|
|
|
|
|
virtual QueryResult * get_query_result()
|
|
{
|
|
return query_result;
|
|
}
|
|
|
|
|
|
virtual bool has_next()
|
|
{
|
|
bool has = false;
|
|
|
|
if( model_connector && query_result && query_result->has_db_result() && select_status )
|
|
{
|
|
has = query_result->cur_row < query_result->result_rows;
|
|
}
|
|
|
|
return has;
|
|
}
|
|
|
|
|
|
virtual bool get(ModelClass & result)
|
|
{
|
|
bool res = false;
|
|
result.set_connector(model_connector);
|
|
result.clear();
|
|
|
|
if( model_connector && query_result && query_result->has_db_result() )
|
|
{
|
|
DbConnector * db_connector = model_connector->get_db_connector();
|
|
|
|
if( db_connector )
|
|
{
|
|
ModelEnv model_env_local;
|
|
result.model_env = &model_env_local;
|
|
result.model_env->cursor_helper = &cursor_helper;
|
|
|
|
try
|
|
{
|
|
cursor_helper.clear();
|
|
cursor_helper.query_result = query_result;
|
|
cursor_helper.has_autogenerated_select = has_autogenerated_select;
|
|
|
|
result.model_env->model_data = model_data;
|
|
result.before_select();
|
|
res = select_status;
|
|
|
|
if( res )
|
|
{
|
|
if( query_result->cur_row < query_result->result_rows )
|
|
{
|
|
result.set_save_mode(Model::DO_UPDATE_ON_SAVE); // IMPROVE ME check if there is a primary key
|
|
result.map_values_from_query();
|
|
result.after_select();
|
|
query_result->cur_row += 1;
|
|
}
|
|
else
|
|
{
|
|
res = false;
|
|
// log some error or throw an exception?
|
|
}
|
|
}
|
|
else
|
|
{
|
|
result.after_select_failure();
|
|
}
|
|
}
|
|
catch(...)
|
|
{
|
|
res = false;
|
|
// throw something?
|
|
}
|
|
|
|
result.model_env = nullptr;
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
virtual ModelClass get()
|
|
{
|
|
ModelClass model;
|
|
|
|
get(model);
|
|
return model;
|
|
}
|
|
|
|
|
|
virtual bool get_list(std::list<ModelClass> & result, bool clear_list = true)
|
|
{
|
|
bool res = false;
|
|
|
|
if( clear_list )
|
|
{
|
|
result.clear();
|
|
}
|
|
|
|
if( model_connector && query_result && query_result->has_db_result() )
|
|
{
|
|
DbConnector * db_connector = model_connector->get_db_connector();
|
|
|
|
if( db_connector )
|
|
{
|
|
res = select_status;
|
|
|
|
try
|
|
{
|
|
if( res )
|
|
{
|
|
res = add_models_to_list(result);
|
|
}
|
|
else
|
|
{
|
|
// add log
|
|
|
|
// if there was a failure we do not call after_select_failure()
|
|
// because there are no any objects in the list
|
|
}
|
|
}
|
|
catch(...)
|
|
{
|
|
res = false;
|
|
// throw or something?
|
|
}
|
|
}
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
virtual std::list<ModelClass> get_list()
|
|
{
|
|
std::list<ModelClass> result;
|
|
|
|
get_list(result, false);
|
|
return std::move(result);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ModelConnector * model_connector;
|
|
ModelData * model_data;
|
|
bool has_autogenerated_select;
|
|
CursorHelper cursor_helper;
|
|
QueryResult * query_result;
|
|
bool select_status;
|
|
|
|
|
|
|
|
|
|
template<typename ContainerType>
|
|
bool add_models_to_list(ContainerType & result)
|
|
{
|
|
bool res = true;
|
|
|
|
for(query_result->cur_row = 0 ; query_result->cur_row < query_result->result_rows ; query_result->cur_row += 1)
|
|
{
|
|
result.emplace_back(); // it returns a reference from c++17
|
|
ModelClass & added_model = result.back();
|
|
|
|
ModelEnv model_env_local;
|
|
|
|
try
|
|
{
|
|
cursor_helper.clear();
|
|
cursor_helper.query_result = query_result;
|
|
cursor_helper.has_autogenerated_select = has_autogenerated_select;
|
|
|
|
added_model.set_connector(model_connector);
|
|
added_model.clear();
|
|
|
|
added_model.model_env = &model_env_local;
|
|
added_model.model_env->cursor_helper = &cursor_helper;
|
|
added_model.set_save_mode(Model::DO_UPDATE_ON_SAVE); // IMPROVE ME check if there is a primary key
|
|
added_model.model_env->model_data = model_data;
|
|
added_model.before_select();
|
|
added_model.map_values_from_query();
|
|
added_model.after_select();
|
|
}
|
|
catch(...)
|
|
{
|
|
res = false;
|
|
// throw or something?
|
|
}
|
|
|
|
added_model.model_env = nullptr;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
#endif
|