230 lines
4.5 KiB
C++
230 lines
4.5 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, 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.
|
|
*
|
|
*/
|
|
|
|
#include "modelconnector.h"
|
|
#include "model.h"
|
|
#include "flatconnector.h"
|
|
#include "dbconnector.h"
|
|
|
|
|
|
namespace morm
|
|
{
|
|
|
|
ModelConnector::ModelConnector()
|
|
{
|
|
flat_connector = nullptr;
|
|
db_connector = nullptr;
|
|
expression_callback = nullptr;
|
|
|
|
out_stream = nullptr;
|
|
out_stream_allocated = false;
|
|
}
|
|
|
|
ModelConnector::~ModelConnector()
|
|
{
|
|
deallocate_stream();
|
|
}
|
|
|
|
|
|
|
|
|
|
void ModelConnector::set_db_expression_output_type(int output_type)
|
|
{
|
|
if( db_connector )
|
|
{
|
|
DbExpression * db_expression = db_connector->get_expression();
|
|
|
|
if( db_expression )
|
|
{
|
|
db_expression->set_output_type(output_type);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void ModelConnector::deallocate_stream()
|
|
{
|
|
if( out_stream_allocated )
|
|
{
|
|
delete out_stream;
|
|
out_stream = nullptr;
|
|
out_stream_allocated = false;
|
|
}
|
|
}
|
|
|
|
|
|
void ModelConnector::allocate_default_stream()
|
|
{
|
|
deallocate_stream();
|
|
out_stream = new PT::TextStream();
|
|
out_stream_allocated = true;
|
|
}
|
|
|
|
|
|
void ModelConnector::allocate_default_stream_if_needed()
|
|
{
|
|
if( !out_stream )
|
|
{
|
|
allocate_default_stream();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void ModelConnector::set_stream(PT::TextStream & stream)
|
|
{
|
|
deallocate_stream();
|
|
this->out_stream = &stream;
|
|
}
|
|
|
|
|
|
PT::TextStream * ModelConnector::get_stream()
|
|
{
|
|
allocate_default_stream_if_needed();
|
|
return out_stream;
|
|
}
|
|
|
|
|
|
|
|
void ModelConnector::set_flat_connector(FlatConnector & flat_connector)
|
|
{
|
|
this->flat_connector = &flat_connector;
|
|
}
|
|
|
|
|
|
void ModelConnector::set_db_connector(DbConnector & db_connector)
|
|
{
|
|
this->db_connector = &db_connector;
|
|
}
|
|
|
|
|
|
DbConnector * ModelConnector::get_db_connector()
|
|
{
|
|
return db_connector;
|
|
}
|
|
|
|
|
|
void ModelConnector::to_text(PT::TextStream & stream, Model & model)
|
|
{
|
|
if( flat_connector )
|
|
{
|
|
expression_callback = flat_connector->get_expression();
|
|
|
|
if( expression_callback )
|
|
{
|
|
flat_connector->to_text(stream, model);
|
|
}
|
|
|
|
expression_callback = nullptr;
|
|
}
|
|
}
|
|
|
|
|
|
void ModelConnector::generate_select_columns(PT::TextStream & stream, Model & model)
|
|
{
|
|
if( db_connector )
|
|
{
|
|
expression_callback = db_connector->get_expression();
|
|
|
|
if( expression_callback )
|
|
{
|
|
db_connector->generate_select_columns(stream, model);
|
|
}
|
|
|
|
expression_callback = nullptr;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void ModelConnector::generate_insert_query(PT::TextStream & stream, Model & model)
|
|
{
|
|
if( db_connector )
|
|
{
|
|
expression_callback = db_connector->get_expression();
|
|
|
|
if( expression_callback )
|
|
{
|
|
db_connector->generate_insert_query(stream, model);
|
|
}
|
|
|
|
expression_callback = nullptr;
|
|
}
|
|
}
|
|
|
|
|
|
void ModelConnector::generate_update_query(PT::TextStream & stream, Model & model)
|
|
{
|
|
if( db_connector )
|
|
{
|
|
expression_callback = db_connector->get_expression();
|
|
|
|
if( expression_callback )
|
|
{
|
|
db_connector->generate_update_query(stream, model);
|
|
}
|
|
|
|
expression_callback = nullptr;
|
|
}
|
|
}
|
|
|
|
|
|
void ModelConnector::insert(Model & model)
|
|
{
|
|
allocate_default_stream_if_needed();
|
|
|
|
if( db_connector && out_stream )
|
|
{
|
|
out_stream->clear();
|
|
db_connector->insert(*out_stream, model);
|
|
}
|
|
}
|
|
|
|
|
|
void ModelConnector::update(Model & model)
|
|
{
|
|
allocate_default_stream_if_needed();
|
|
|
|
if( db_connector && out_stream )
|
|
{
|
|
out_stream->clear();
|
|
db_connector->update(*out_stream, model);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|