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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "dbexpression.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace morm
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
DbExpression::DbExpression()
|
|
|
|
{
|
|
|
|
output_type = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DbExpression::~DbExpression()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbExpression::set_output_type(int output_type)
|
|
|
|
{
|
|
|
|
this->output_type = output_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool DbExpression::can_field_be_generated(bool insertable, bool updatable, bool is_primary_key)
|
|
|
|
{
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_DB_INSERT )
|
|
|
|
{
|
|
|
|
return insertable;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_DB_UPDATE )
|
|
|
|
{
|
|
|
|
return updatable;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY )
|
|
|
|
{
|
|
|
|
return is_primary_key;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void DbExpression::field_before()
|
|
|
|
{
|
|
|
|
BaseExpression::field_before();
|
|
|
|
|
|
|
|
if( !is_first_field )
|
|
|
|
{
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_DB_INSERT ||
|
2018-03-30 21:34:45 +02:00
|
|
|
output_type == MORM_OUTPUT_TYPE_DB_UPDATE ||
|
|
|
|
output_type == MORM_OUTPUT_TYPE_SELECT_COLUMNS )
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
|
|
|
(*out_stream) << ",";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY )
|
|
|
|
{
|
|
|
|
(*out_stream) << " AND ";
|
|
|
|
}
|
2018-03-30 21:34:45 +02:00
|
|
|
else
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_WHERE_EQ ||
|
|
|
|
output_type == MORM_OUTPUT_TYPE_WHERE_GE ||
|
|
|
|
output_type == MORM_OUTPUT_TYPE_WHERE_GT ||
|
|
|
|
output_type == MORM_OUTPUT_TYPE_WHERE_LE ||
|
2018-04-18 19:52:09 +02:00
|
|
|
output_type == MORM_OUTPUT_TYPE_WHERE_LT ||
|
|
|
|
output_type == MORM_OUTPUT_TYPE_WHERE_IN )
|
2018-03-30 21:34:45 +02:00
|
|
|
{
|
|
|
|
int conjunction = MORM_CONJUNCTION_AND;
|
|
|
|
|
|
|
|
if( !conjunctions.empty() )
|
|
|
|
{
|
|
|
|
conjunction = conjunctions.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( conjunction == MORM_CONJUNCTION_AND )
|
|
|
|
{
|
|
|
|
(*out_stream) << " AND ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( conjunction == MORM_CONJUNCTION_OR )
|
|
|
|
{
|
|
|
|
(*out_stream) << " OR ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbExpression::put_name_value_separator()
|
|
|
|
{
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY ||
|
2018-04-17 00:46:25 +02:00
|
|
|
output_type == MORM_OUTPUT_TYPE_WHERE_EQ ||
|
|
|
|
output_type == MORM_OUTPUT_TYPE_DB_UPDATE)
|
2018-03-30 21:34:45 +02:00
|
|
|
{
|
|
|
|
(*out_stream) << " = ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_WHERE_LT )
|
|
|
|
{
|
|
|
|
(*out_stream) << " < ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_WHERE_GT )
|
|
|
|
{
|
|
|
|
(*out_stream) << " > ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_WHERE_LE )
|
|
|
|
{
|
|
|
|
(*out_stream) << " <= ";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_WHERE_GE )
|
|
|
|
{
|
|
|
|
(*out_stream) << " >= ";
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
2018-04-18 19:52:09 +02:00
|
|
|
else
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_WHERE_IN )
|
|
|
|
{
|
|
|
|
(*out_stream) << " in ";
|
|
|
|
}
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
void DbExpression::before_field_value_string()
|
|
|
|
{
|
2018-03-30 21:34:45 +02:00
|
|
|
// if( output_type == MORM_OUTPUT_TYPE_SELECT_COLUMNS ||
|
|
|
|
// output_type == MORM_OUTPUT_TYPE_DB_INSERT ||
|
|
|
|
// output_type == MORM_OUTPUT_TYPE_DB_UPDATE ||
|
|
|
|
// output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY )
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
|
|
|
(*out_stream) << "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbExpression::after_field_value_string()
|
|
|
|
{
|
2018-03-30 21:34:45 +02:00
|
|
|
// if( output_type == MORM_OUTPUT_TYPE_SELECT_COLUMNS ||
|
|
|
|
// output_type == MORM_OUTPUT_TYPE_DB_INSERT ||
|
|
|
|
// output_type == MORM_OUTPUT_TYPE_DB_UPDATE ||
|
|
|
|
// output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY )
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
|
|
|
(*out_stream) << "'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbExpression::before_field_value(const std::wstring &)
|
|
|
|
{
|
|
|
|
before_field_value_string();
|
|
|
|
}
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
void DbExpression::after_field_value(const std::wstring &)
|
|
|
|
{
|
|
|
|
after_field_value_string();
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
void DbExpression::before_field_value(const std::string &)
|
|
|
|
{
|
|
|
|
before_field_value_string();
|
|
|
|
}
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
void DbExpression::after_field_value(const std::string &)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
|
|
|
after_field_value_string();
|
|
|
|
}
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
void DbExpression::before_field_value(const wchar_t *)
|
|
|
|
{
|
|
|
|
before_field_value_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbExpression::after_field_value(const wchar_t *)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
|
|
|
after_field_value_string();
|
|
|
|
}
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
void DbExpression::before_field_value(const char *)
|
|
|
|
{
|
|
|
|
before_field_value_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbExpression::after_field_value(const char *)
|
|
|
|
{
|
|
|
|
after_field_value_string();
|
|
|
|
}
|
|
|
|
|
2018-04-17 00:46:25 +02:00
|
|
|
void DbExpression::before_field_value(const PT::Date &)
|
|
|
|
{
|
|
|
|
before_field_value_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DbExpression::after_field_value(const PT::Date &)
|
|
|
|
{
|
|
|
|
after_field_value_string();
|
|
|
|
}
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
|
|
|
|
void DbExpression::prepare_to_where_clause()
|
|
|
|
{
|
|
|
|
work_mode = MORM_WORK_MODE_MODEL_FIELDS_VALUES;
|
|
|
|
conjunctions.clear();
|
|
|
|
is_first_field = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-16 01:00:17 +02:00
|
|
|
DbExpression & DbExpression::group_or(PT::TextStream & stream)
|
2018-03-30 21:34:45 +02:00
|
|
|
{
|
2018-04-16 01:00:17 +02:00
|
|
|
out_stream = &stream;
|
2018-03-30 21:34:45 +02:00
|
|
|
field_before();
|
|
|
|
(*out_stream) << "(";
|
|
|
|
is_first_field = true;
|
|
|
|
conjunctions.push_back(MORM_CONJUNCTION_OR);
|
2018-04-16 01:00:17 +02:00
|
|
|
out_stream = nullptr;
|
2018-03-30 21:34:45 +02:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-04-16 01:00:17 +02:00
|
|
|
DbExpression & DbExpression::group_and(PT::TextStream & stream)
|
2018-03-30 21:34:45 +02:00
|
|
|
{
|
2018-04-16 01:00:17 +02:00
|
|
|
out_stream = &stream;
|
2018-03-30 21:34:45 +02:00
|
|
|
field_before();
|
|
|
|
(*out_stream) << "(";
|
|
|
|
is_first_field = true;
|
|
|
|
conjunctions.push_back(MORM_CONJUNCTION_AND);
|
2018-04-16 01:00:17 +02:00
|
|
|
out_stream = nullptr;
|
2018-03-23 20:26:57 +01:00
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-04-16 01:00:17 +02:00
|
|
|
DbExpression & DbExpression::group_end(PT::TextStream & stream)
|
2018-03-30 21:34:45 +02:00
|
|
|
{
|
2018-04-16 01:00:17 +02:00
|
|
|
out_stream = &stream;
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
if( !conjunctions.empty() )
|
|
|
|
{
|
|
|
|
conjunctions.pop_back();
|
|
|
|
(*out_stream) << ")";
|
|
|
|
field_after();
|
|
|
|
}
|
|
|
|
|
2018-04-16 01:00:17 +02:00
|
|
|
out_stream = nullptr;
|
2018-03-30 21:34:45 +02:00
|
|
|
return *this;
|
|
|
|
}
|
2018-03-23 20:26:57 +01:00
|
|
|
|
|
|
|
|
2018-04-22 23:04:50 +02:00
|
|
|
DbExpression & DbExpression::page(PT::TextStream & stream, size_t page_number, size_t page_size)
|
|
|
|
{
|
|
|
|
stream << " limit " << page_number << "," << page_size << " ";
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|