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>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2022-05-12 00:21:42 +02:00
|
|
|
* Copyright (c) 2018-2022, Tomasz Sowa
|
2018-03-23 20:26:57 +01:00
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-26 19:34:07 +01:00
|
|
|
int DbExpression::get_output_type()
|
|
|
|
{
|
|
|
|
return output_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
bool DbExpression::can_field_be_generated(const FT & field_type)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_DB_INSERT )
|
|
|
|
{
|
2021-03-10 16:20:11 +01:00
|
|
|
return field_type.is_insertable();
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_DB_UPDATE )
|
|
|
|
{
|
2021-03-10 16:20:11 +01:00
|
|
|
return field_type.is_updatable();
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
else
|
2019-08-21 19:42:50 +02:00
|
|
|
if( output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY ||
|
|
|
|
output_type == MORM_OUTPUT_TYPE_JOIN_TABLES ||
|
2022-05-11 23:18:25 +02:00
|
|
|
output_type == MORM_OUTPUT_TYPE_DB_INSERT_PRIMARY_KEY ||
|
|
|
|
output_type == MORM_OUTPUT_TYPE_DB_UPDATE_PRIMARY_KEY )
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
2021-03-10 16:20:11 +01:00
|
|
|
return field_type.is_primary_key();
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2019-06-17 12:59:39 +02:00
|
|
|
(*out_stream) << ", ";
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY )
|
|
|
|
{
|
|
|
|
(*out_stream) << " AND ";
|
|
|
|
}
|
2018-03-30 21:34:45 +02:00
|
|
|
else
|
2022-05-11 23:18:25 +02:00
|
|
|
if( output_type == MORM_OUTPUT_TYPE_DB_INSERT_PRIMARY_KEY ||
|
|
|
|
output_type == MORM_OUTPUT_TYPE_DB_UPDATE_PRIMARY_KEY )
|
2019-08-21 19:42:50 +02:00
|
|
|
{
|
|
|
|
(*out_stream) << ", ";
|
|
|
|
}
|
|
|
|
else
|
2018-03-30 21:34:45 +02:00
|
|
|
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 ||
|
2021-02-23 16:51:43 +01:00
|
|
|
output_type == MORM_OUTPUT_TYPE_WHERE_NOT_EQ ||
|
2018-04-18 19:52:09 +02:00
|
|
|
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 ||
|
2019-09-04 18:02:18 +02:00
|
|
|
output_type == MORM_OUTPUT_TYPE_DB_UPDATE ||
|
|
|
|
output_type == MORM_OUTPUT_TYPE_DB_INSERT_PRIMARY_KEY ||
|
|
|
|
output_type == MORM_OUTPUT_TYPE_DB_UPDATE_PRIMARY_KEY )
|
2018-03-30 21:34:45 +02:00
|
|
|
{
|
2019-06-17 12:59:39 +02:00
|
|
|
(*out_stream) << "=";
|
2018-03-30 21:34:45 +02:00
|
|
|
}
|
|
|
|
else
|
2019-05-31 20:28:09 +02:00
|
|
|
if( output_type == MORM_OUTPUT_TYPE_WHERE_NOT_EQ )
|
|
|
|
{
|
2019-06-17 12:59:39 +02:00
|
|
|
(*out_stream) << "<>";
|
2019-05-31 20:28:09 +02:00
|
|
|
}
|
|
|
|
else
|
2018-03-30 21:34:45 +02:00
|
|
|
if( output_type == MORM_OUTPUT_TYPE_WHERE_LT )
|
|
|
|
{
|
2019-06-17 12:59:39 +02:00
|
|
|
(*out_stream) << "<";
|
2018-03-30 21:34:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_WHERE_GT )
|
|
|
|
{
|
2019-06-17 12:59:39 +02:00
|
|
|
(*out_stream) << ">";
|
2018-03-30 21:34:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_WHERE_LE )
|
|
|
|
{
|
2019-06-17 12:59:39 +02:00
|
|
|
(*out_stream) << "<=";
|
2018-03-30 21:34:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
if( output_type == MORM_OUTPUT_TYPE_WHERE_GE )
|
|
|
|
{
|
2019-06-17 12:59:39 +02:00
|
|
|
(*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
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-13 02:32:03 +02:00
|
|
|
void DbExpression::schema_table_separator()
|
|
|
|
{
|
|
|
|
(*out_stream) << '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbExpression::table_field_separator()
|
|
|
|
{
|
|
|
|
(*out_stream) << '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbExpression::before_schema_name()
|
|
|
|
{
|
|
|
|
(*out_stream) << '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbExpression::after_schema_name()
|
|
|
|
{
|
|
|
|
(*out_stream) << '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbExpression::before_table_name()
|
|
|
|
{
|
|
|
|
(*out_stream) << '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbExpression::after_table_name()
|
|
|
|
{
|
|
|
|
(*out_stream) << '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbExpression::before_field_name()
|
|
|
|
{
|
|
|
|
(*out_stream) << '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void DbExpression::after_field_name()
|
|
|
|
{
|
|
|
|
(*out_stream) << '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbExpression::before_field_value_string(const FT & field_type)
|
2018-03-23 20:26:57 +01:00
|
|
|
{
|
2021-03-11 18:40:32 +01:00
|
|
|
(*out_stream) << "'";
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|
|
|
|
|
2018-03-30 21:34:45 +02:00
|
|
|
|
2021-05-12 04:53:23 +02:00
|
|
|
void DbExpression::after_field_value_string(const FT & field_type)
|
2018-04-17 00:46:25 +02:00
|
|
|
{
|
2021-03-11 18:40:32 +01:00
|
|
|
(*out_stream) << "'";
|
2018-04-17 00:46:25 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-05-20 16:25:01 +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;
|
|
|
|
}
|
|
|
|
|
2021-05-20 16:25:01 +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;
|
|
|
|
}
|
|
|
|
|
2021-05-20 16:25:01 +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
|
|
|
|
|
|
|
|
2021-05-20 16:25:01 +02:00
|
|
|
DbExpression & DbExpression::page(pt::TextStream & stream, size_t page_number, size_t page_size)
|
2018-04-22 23:04:50 +02:00
|
|
|
{
|
|
|
|
stream << " limit " << page_number << "," << page_size << " ";
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-05-31 20:28:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-23 20:26:57 +01:00
|
|
|
}
|