2018-03-23 20:26:57 +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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
#include <cstdlib>
|
2018-11-14 18:14:26 +01:00
|
|
|
#include <memory>
|
2018-03-23 20:26:57 +01:00
|
|
|
#include "dbconnector.h"
|
2018-07-05 11:51:26 +02:00
|
|
|
#include "dbexpression.h"
|
2018-03-23 20:26:57 +01:00
|
|
|
#include "model.h"
|
2018-04-17 00:46:25 +02:00
|
|
|
#include "utf8/utf8.h"
|
2018-03-23 20:26:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
namespace morm
|
|
|
|
{
|
|
|
|
|
|
|
|
DbConnector::DbConnector()
|
|
|
|
{
|
|
|
|
db_expression = nullptr;
|
|
|
|
expression_allocated = false;
|
2018-04-23 17:46:21 +02:00
|
|
|
logger = nullptr;
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
DbConnector::DbConnector(const DbConnector &)
|
|
|
|
{
|
|
|
|
db_expression = nullptr;
|
|
|
|
expression_allocated = false;
|
2018-04-23 17:46:21 +02:00
|
|
|
logger = nullptr;
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
|
|
|
|
DbConnector::~DbConnector()
|
|
|
|
{
|
|
|
|
deallocate_expression();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-23 17:46:21 +02:00
|
|
|
void DbConnector::set_logger(PT::Logger * logger)
|
|
|
|
{
|
|
|
|
this->logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbConnector::set_logger(PT::Logger & logger)
|
|
|
|
{
|
|
|
|
this->logger = &logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query(const PT::TextStream & stream, QueryResult & query_result)
|
2018-04-18 12:22:01 +02:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
std::string query_str;
|
|
|
|
stream.to_string(query_str);
|
2018-03-23 20:26:57 +01:00
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(query_str, query_result);
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query(const std::string & query_str, QueryResult & query_result)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(query_str.c_str(), query_result);
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query(const char * query_str, QueryResult & query_result)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
2018-04-16 01:00:17 +02:00
|
|
|
// do query
|
2018-03-23 20:26:57 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-16 01:00:17 +02:00
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query_select(const char * query_str, QueryResult & query_result)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(query_str, query_result);
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query_update(const char * query_str, QueryResult & query_result)
|
2018-04-16 01:00:17 +02:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(query_str, query_result);
|
2018-04-16 01:00:17 +02:00
|
|
|
}
|
2018-03-23 20:26:57 +01:00
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query_insert(const char * query_str, QueryResult & query_result)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(query_str, query_result);
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query_remove(const char * query_str, QueryResult & query_result)
|
2018-04-22 03:26:46 +02:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(query_str, query_result);
|
2018-04-22 03:26:46 +02:00
|
|
|
}
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
|
|
|
|
bool DbConnector::query_select(const PT::TextStream & stream, QueryResult & query_result)
|
2018-04-22 03:26:46 +02:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(stream, query_result);
|
2018-04-22 03:26:46 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query_update(const PT::TextStream & stream, QueryResult & query_result)
|
2018-04-22 03:26:46 +02:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(stream, query_result);
|
2018-04-22 03:26:46 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query_insert(const PT::TextStream & stream, QueryResult & query_result)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(stream, query_result);
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
2018-03-23 20:26:57 +01:00
|
|
|
|
2018-11-14 18:14:26 +01:00
|
|
|
bool DbConnector::query_remove(const PT::TextStream & stream, QueryResult & query_result)
|
2018-04-16 01:00:17 +02:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
return query(stream, query_result);
|
2018-04-16 01:00:17 +02:00
|
|
|
}
|
2018-03-23 20:26:57 +01:00
|
|
|
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
DbExpression * DbConnector::get_expression()
|
|
|
|
{
|
|
|
|
allocate_default_expression_if_needed();
|
|
|
|
return db_expression;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-02 01:22:32 +02:00
|
|
|
void DbConnector::generate_select_columns(PT::TextStream & stream, Model & model, const std::wstring & column_prefix)
|
2018-03-30 21:34:45 +02:00
|
|
|
{
|
|
|
|
allocate_default_expression_if_needed();
|
|
|
|
|
|
|
|
if( db_expression )
|
|
|
|
{
|
2018-05-02 01:22:32 +02:00
|
|
|
db_expression->prepare_to_new_expression();
|
2018-03-30 21:34:45 +02:00
|
|
|
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS);
|
|
|
|
db_expression->set_output_type(MORM_OUTPUT_TYPE_SELECT_COLUMNS);
|
2018-05-02 01:22:32 +02:00
|
|
|
db_expression->set_column_prefix(column_prefix);
|
2018-03-30 21:34:45 +02:00
|
|
|
db_expression->generate_from_model(stream, model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
|
|
|
|
void DbConnector::generate_insert_query(PT::TextStream & stream, Model & model)
|
|
|
|
{
|
|
|
|
allocate_default_expression_if_needed();
|
|
|
|
|
|
|
|
if( db_expression )
|
|
|
|
{
|
2018-05-02 01:22:32 +02:00
|
|
|
db_expression->prepare_to_new_expression();
|
2018-03-23 20:26:57 +01:00
|
|
|
stream << "insert into ";
|
|
|
|
model.table_name(stream);
|
2018-04-22 03:26:46 +02:00
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
stream << " (";
|
|
|
|
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS);
|
2018-04-22 03:26:46 +02:00
|
|
|
db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE);
|
2018-03-23 20:26:57 +01:00
|
|
|
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 )
|
|
|
|
{
|
2018-05-02 01:22:32 +02:00
|
|
|
db_expression->prepare_to_new_expression();
|
2018-03-23 20:26:57 +01:00
|
|
|
stream << "update ";
|
|
|
|
model.table_name(stream);
|
|
|
|
|
2018-04-22 03:26:46 +02:00
|
|
|
stream << " set ";
|
2018-04-17 00:46:25 +02:00
|
|
|
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS_VALUES);
|
2018-04-22 03:26:46 +02:00
|
|
|
db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE);
|
2018-03-23 20:26:57 +01:00
|
|
|
db_expression->generate_from_model(stream, model);
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
stream << " where ";
|
2018-03-23 20:26:57 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-22 03:26:46 +02:00
|
|
|
void DbConnector::generate_remove_query(PT::TextStream & stream, Model & model)
|
|
|
|
{
|
|
|
|
allocate_default_expression_if_needed();
|
|
|
|
|
|
|
|
if( db_expression )
|
|
|
|
{
|
2018-05-02 01:22:32 +02:00
|
|
|
db_expression->prepare_to_new_expression();
|
2018-04-22 03:26:46 +02:00
|
|
|
stream << "delete from ";
|
|
|
|
model.table_name(stream);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2018-03-23 20:26:57 +01:00
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
bool DbConnector::insert(PT::TextStream & stream, Model & model)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
std::unique_ptr<QueryResult> query_result_ptr(create_query_result());
|
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
generate_insert_query(stream, model);
|
2018-11-14 18:14:26 +01:00
|
|
|
return query_insert(stream, *query_result_ptr);
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
bool DbConnector::update(PT::TextStream & stream, Model & model)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
std::unique_ptr<QueryResult> query_result_ptr(create_query_result());
|
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
generate_update_query(stream, model);
|
2018-11-14 18:14:26 +01:00
|
|
|
return query_update(stream, *query_result_ptr);
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-22 03:26:46 +02:00
|
|
|
bool DbConnector::remove(PT::TextStream & stream, Model & model)
|
|
|
|
{
|
2018-11-14 18:14:26 +01:00
|
|
|
std::unique_ptr<QueryResult> query_result_ptr(create_query_result());
|
|
|
|
|
2018-04-22 03:26:46 +02:00
|
|
|
generate_remove_query(stream, model);
|
2018-11-14 18:14:26 +01:00
|
|
|
return query_remove(stream, *query_result_ptr);
|
2018-04-22 03:26:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, char & field_value)
|
|
|
|
{
|
|
|
|
field_value = *value_str;
|
|
|
|
value_str += 1;
|
|
|
|
|
|
|
|
if( *value_str != 0 )
|
|
|
|
{
|
|
|
|
// value has more than one charater, put some error?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-03 18:55:06 +02:00
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
void DbConnector::get_value(const char * value_str, unsigned char & field_value)
|
|
|
|
{
|
|
|
|
field_value = *(const unsigned char*)value_str;
|
|
|
|
value_str += 1;
|
|
|
|
|
|
|
|
if( *value_str != 0 )
|
|
|
|
{
|
|
|
|
// value has more than one charater, put some error?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::get_value(const char * value_str, std::wstring & field_value)
|
|
|
|
{
|
|
|
|
// CHECKME
|
|
|
|
// what about \0 in val_str?
|
|
|
|
// it is escaped somehow?
|
|
|
|
PT::UTF8ToWide(value_str, field_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::get_value(const char * value_str, std::string & field_value)
|
|
|
|
{
|
|
|
|
field_value = value_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::get_value(const char * value_str, bool & field_value)
|
|
|
|
{
|
|
|
|
// IMPROVE ME
|
|
|
|
// this 't' is locale dependent
|
|
|
|
field_value = (value_str[0]=='t' || value_str[0]=='y' || value_str[0]=='1');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::get_value(const char * value_str, short & field_value)
|
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
|
|
|
field_value = (short)PT::Toi(value_str, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::get_value(const char * value_str, unsigned short & field_value)
|
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
|
|
|
field_value = (unsigned short)PT::Toui(value_str, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::get_value(const char * value_str, int & field_value)
|
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
|
|
|
field_value = PT::Toi(value_str, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::get_value(const char * value_str, unsigned int & field_value)
|
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
|
|
|
field_value = PT::Toui(value_str, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::get_value(const char * value_str, long & field_value)
|
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
|
|
|
field_value = PT::Tol(value_str, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::get_value(const char * value_str, unsigned long & field_value)
|
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
|
|
|
field_value = PT::Toul(value_str, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::get_value(const char * value_str, long long & field_value)
|
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
|
|
|
field_value = PT::Toll(value_str, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::get_value(const char * value_str, unsigned long long & field_value)
|
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
|
|
|
field_value = PT::Toull(value_str, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::get_value(const char * value_str, float & field_value)
|
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
|
|
|
field_value = strtof(value_str, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::get_value(const char * value_str, double & field_value)
|
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
|
|
|
field_value = strtod(value_str, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::get_value(const char * value_str, long double & field_value)
|
|
|
|
{
|
|
|
|
// IMPROVE ME give some overflow checking
|
|
|
|
field_value = strtold(value_str, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbConnector::get_value(const char * value_str, PT::Date & field_value)
|
|
|
|
{
|
|
|
|
// IMPROVE ME give some log if parsing failed
|
2018-04-18 19:52:09 +02:00
|
|
|
field_value.Parse(value_str);
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char * DbConnector::query_last_sequence(const wchar_t * sequence_table_name)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-03-30 21:34:45 +02:00
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|