morm/src/dbconnector.cpp

185 lines
4.1 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 "dbconnector.h"
#include "model.h"
namespace morm
{
DbConnector::DbConnector()
{
db_expression = nullptr;
expression_allocated = false;
}
DbConnector::~DbConnector()
{
deallocate_expression();
}
bool DbConnector::query(PT::TextStream & stream)
{
return false;
}
bool DbConnector::query(const std::wstring & query)
{
return false;
}
bool DbConnector::query(const std::string & query)
{
return false;
}
bool DbConnector::query(const wchar_t * query)
{
return false;
}
bool DbConnector::query(const char * query)
{
// do query
return false;
}
DbExpression * DbConnector::get_expression()
{
allocate_default_expression_if_needed();
return db_expression;
}
void DbConnector::generate_insert_query(PT::TextStream & stream, Model & model)
{
allocate_default_expression_if_needed();
if( db_expression )
{
db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE);
stream << "insert into ";
model.table_name(stream);
stream << " (";
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS);
db_expression->generate_from_model(stream, model);
stream << ") values (";
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_VALUES);
db_expression->generate_from_model(stream, model);
stream << ")";
}
}
void DbConnector::generate_update_query(PT::TextStream & stream, Model & model)
{
allocate_default_expression_if_needed();
if( db_expression )
{
db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE);
stream << "update ";
model.table_name(stream);
stream << " set (";
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS);
db_expression->generate_from_model(stream, model);
stream << ") values (";
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_VALUES);
db_expression->generate_from_model(stream, model);
stream << ") where ";
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS_VALUES);
db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_PRIMARY_KEY);
db_expression->generate_from_model(stream, model);
}
}
void DbConnector::insert(PT::TextStream & stream, Model & model)
{
generate_insert_query(stream, model);
// do the real inserting
// query(stream);
// and get autogenerated columns
}
void DbConnector::update(PT::TextStream & stream, Model & model)
{
generate_update_query(stream, model);
// do the real updating
// query(stream);
// and get autogenerated columns
}
void DbConnector::deallocate_expression()
{
if( expression_allocated )
{
delete db_expression;
db_expression = nullptr;
expression_allocated = false;
}
}
void DbConnector::allocate_default_expression_if_needed()
{
if( !db_expression )
{
allocate_default_expression();
}
}
}