add a CSVConnector/CSVExpression classes to serialize to csv format

Current limitation: list/vectors are not serialized.

while here:
- move the output_type flag from the DbExpression to BaseExpression class
- add Model::to_text(...) methods with an Export parameter
This commit is contained in:
Tomasz Sowa 2023-04-12 17:20:29 +02:00
parent d61fc31b5c
commit 86177889af
Signed by: tomasz.sowa
GPG Key ID: 662CC1438638588B
20 changed files with 604 additions and 68 deletions

View File

@ -16,9 +16,9 @@
./main.o: ../../pikotools/src/textstream/types.h ../src/modelconnector.h
./main.o: ../src/clearer.h ../src/ft.h ../src/dbconnector.h
./main.o: ../../pikotools/src/log/log.h ../../pikotools/src/log/filelog.h
./main.o: ../src/queryresult.h ../src/flatconnector.h ../src/dbexpression.h
./main.o: ../src/baseexpression.h ../src/modelenv.h ../src/modeldata.h
./main.o: ../src/cursorhelper.h ../src/finderhelper.h
./main.o: ../src/queryresult.h ../src/flatconnector.h ../src/export.h
./main.o: ../src/dbexpression.h ../src/baseexpression.h ../src/modelenv.h
./main.o: ../src/modeldata.h ../src/cursorhelper.h ../src/finderhelper.h
./main.o: ../src/fieldvaluehelper.h ../src/wrapper.h ../src/spacewrapper.h
./main.o: ../src/baseobjectwrapper.h ../src/modelcontainerwrapper.h
./main.o: ../src/select.h ../../pikotools/src/convert/text.h

View File

@ -18,7 +18,7 @@
./baseexpression.o: ../../pikotools/src/log/filelog.h finderhelper.h
./baseexpression.o: fieldvaluehelper.h wrapper.h spacewrapper.h
./baseexpression.o: baseobjectwrapper.h modelcontainerwrapper.h select.h ft.h
./baseexpression.o: ../../pikotools/src/convert/text.h model.h
./baseexpression.o: export.h ../../pikotools/src/convert/text.h model.h
./baseexpression.o: modelconnector.h clearer.h dbconnector.h flatconnector.h
./baseexpression.o: dbexpression.h flatexpression.h
./clearer.o: clearer.h ../../pikotools/src/date/date.h
@ -35,7 +35,7 @@
./clearer.o: ../../pikotools/src/textstream/types.h modelconnector.h
./clearer.o: dbconnector.h ../../pikotools/src/log/log.h
./clearer.o: ../../pikotools/src/log/filelog.h queryresult.h flatconnector.h
./clearer.o: dbexpression.h baseexpression.h morm_types.h modelenv.h
./clearer.o: export.h dbexpression.h baseexpression.h morm_types.h modelenv.h
./clearer.o: modeldata.h cursorhelper.h finderhelper.h fieldvaluehelper.h
./clearer.o: wrapper.h spacewrapper.h baseobjectwrapper.h
./clearer.o: modelcontainerwrapper.h select.h
@ -60,7 +60,7 @@
./dbconnector.o: dbexpression.h baseexpression.h morm_types.h modelenv.h
./dbconnector.o: modeldata.h cursorhelper.h finderhelper.h fieldvaluehelper.h
./dbconnector.o: wrapper.h spacewrapper.h baseobjectwrapper.h
./dbconnector.o: modelcontainerwrapper.h select.h
./dbconnector.o: modelcontainerwrapper.h select.h export.h
./dbconnector.o: ../../pikotools/src/convert/text.h model.h modelconnector.h
./dbconnector.o: clearer.h flatconnector.h flatexpression.h
./dbconnector.o: ../../pikotools/src/convert/convert.h
@ -88,8 +88,9 @@
./dbexpression.o: ../../pikotools/src/log/filelog.h finderhelper.h
./dbexpression.o: fieldvaluehelper.h wrapper.h spacewrapper.h
./dbexpression.o: baseobjectwrapper.h modelcontainerwrapper.h select.h ft.h
./dbexpression.o: ../../pikotools/src/convert/text.h model.h modelconnector.h
./dbexpression.o: clearer.h dbconnector.h flatconnector.h flatexpression.h
./dbexpression.o: export.h ../../pikotools/src/convert/text.h model.h
./dbexpression.o: modelconnector.h clearer.h dbconnector.h flatconnector.h
./dbexpression.o: flatexpression.h
./flatconnector.o: flatconnector.h
./flatconnector.o: ../../pikotools/src/textstream/textstream.h
./flatconnector.o: ../../pikotools/src/textstream/stream.h
@ -102,9 +103,10 @@
./flatconnector.o: ../../pikotools/src/utf8/utf8_private.h
./flatconnector.o: ../../pikotools/src/date/date.h
./flatconnector.o: ../../pikotools/src/membuffer/membuffer.h
./flatconnector.o: ../../pikotools/src/textstream/types.h flatexpression.h
./flatconnector.o: baseexpression.h morm_types.h modelenv.h modeldata.h
./flatconnector.o: cursorhelper.h queryresult.h ../../pikotools/src/log/log.h
./flatconnector.o: ../../pikotools/src/textstream/types.h export.h
./flatconnector.o: flatexpression.h baseexpression.h morm_types.h modelenv.h
./flatconnector.o: modeldata.h cursorhelper.h queryresult.h
./flatconnector.o: ../../pikotools/src/log/log.h
./flatconnector.o: ../../pikotools/src/log/filelog.h finderhelper.h
./flatconnector.o: fieldvaluehelper.h wrapper.h spacewrapper.h
./flatconnector.o: baseobjectwrapper.h modelcontainerwrapper.h select.h ft.h
@ -128,7 +130,7 @@
./flatexpression.o: ../../pikotools/src/log/filelog.h finderhelper.h
./flatexpression.o: fieldvaluehelper.h wrapper.h spacewrapper.h
./flatexpression.o: baseobjectwrapper.h modelcontainerwrapper.h select.h ft.h
./flatexpression.o: ../../pikotools/src/convert/text.h
./flatexpression.o: export.h ../../pikotools/src/convert/text.h
./jsonconnector.o: jsonconnector.h flatconnector.h
./jsonconnector.o: ../../pikotools/src/textstream/textstream.h
./jsonconnector.o: ../../pikotools/src/textstream/stream.h
@ -141,10 +143,10 @@
./jsonconnector.o: ../../pikotools/src/utf8/utf8_private.h
./jsonconnector.o: ../../pikotools/src/date/date.h
./jsonconnector.o: ../../pikotools/src/membuffer/membuffer.h
./jsonconnector.o: ../../pikotools/src/textstream/types.h jsonexpression.h
./jsonconnector.o: flatexpression.h baseexpression.h morm_types.h modelenv.h
./jsonconnector.o: modeldata.h cursorhelper.h queryresult.h
./jsonconnector.o: ../../pikotools/src/log/log.h
./jsonconnector.o: ../../pikotools/src/textstream/types.h export.h
./jsonconnector.o: jsonexpression.h flatexpression.h baseexpression.h
./jsonconnector.o: morm_types.h modelenv.h modeldata.h cursorhelper.h
./jsonconnector.o: queryresult.h ../../pikotools/src/log/log.h
./jsonconnector.o: ../../pikotools/src/log/filelog.h finderhelper.h
./jsonconnector.o: fieldvaluehelper.h wrapper.h spacewrapper.h
./jsonconnector.o: baseobjectwrapper.h modelcontainerwrapper.h select.h ft.h
@ -167,7 +169,7 @@
./jsonexpression.o: ../../pikotools/src/log/filelog.h finderhelper.h
./jsonexpression.o: fieldvaluehelper.h wrapper.h spacewrapper.h
./jsonexpression.o: baseobjectwrapper.h modelcontainerwrapper.h select.h ft.h
./jsonexpression.o: ../../pikotools/src/convert/text.h
./jsonexpression.o: export.h ../../pikotools/src/convert/text.h
./jsonexpression.o: ../../pikotools/src/convert/misc.h
./jsonexpression.o: ../../pikotools/src/convert/text.h
./model.o: model.h ../../pikotools/src/textstream/textstream.h
@ -184,7 +186,7 @@
./model.o: ../../pikotools/src/textstream/types.h modelconnector.h clearer.h
./model.o: ft.h dbconnector.h ../../pikotools/src/log/log.h
./model.o: ../../pikotools/src/log/filelog.h queryresult.h flatconnector.h
./model.o: dbexpression.h baseexpression.h morm_types.h modelenv.h
./model.o: export.h dbexpression.h baseexpression.h morm_types.h modelenv.h
./model.o: modeldata.h cursorhelper.h finderhelper.h fieldvaluehelper.h
./model.o: wrapper.h spacewrapper.h baseobjectwrapper.h
./model.o: modelcontainerwrapper.h select.h
@ -204,7 +206,7 @@
./modelconnector.o: ../../pikotools/src/textstream/types.h
./modelconnector.o: ../../pikotools/src/log/log.h
./modelconnector.o: ../../pikotools/src/log/filelog.h queryresult.h
./modelconnector.o: flatconnector.h
./modelconnector.o: flatconnector.h export.h
./postgresqlconnector.o: postgresqlconnector.h dbconnector.h
./postgresqlconnector.o: ../../pikotools/src/textstream/textstream.h
./postgresqlconnector.o: ../../pikotools/src/textstream/stream.h
@ -225,7 +227,7 @@
./postgresqlconnector.o: modelenv.h modeldata.h cursorhelper.h finderhelper.h
./postgresqlconnector.o: fieldvaluehelper.h wrapper.h spacewrapper.h
./postgresqlconnector.o: baseobjectwrapper.h modelcontainerwrapper.h select.h
./postgresqlconnector.o: ../../pikotools/src/convert/text.h
./postgresqlconnector.o: export.h ../../pikotools/src/convert/text.h
./postgresqlconnector.o: ../../pikotools/src/convert/strtoint.h
./postgresqlconnector.o: ../../pikotools/src/convert/text.h
./postgresqlconnector.o: ../../pikotools/src/convert/misc.h
@ -248,7 +250,8 @@
./postgresqlexpression.o: ../../pikotools/src/log/filelog.h finderhelper.h
./postgresqlexpression.o: fieldvaluehelper.h wrapper.h spacewrapper.h
./postgresqlexpression.o: baseobjectwrapper.h modelcontainerwrapper.h
./postgresqlexpression.o: select.h ft.h ../../pikotools/src/convert/text.h
./postgresqlexpression.o: select.h ft.h export.h
./postgresqlexpression.o: ../../pikotools/src/convert/text.h
./postgresqlqueryresult.o: postgresqlqueryresult.h queryresult.h
./postgresqlqueryresult.o: ../../pikotools/src/log/log.h
./postgresqlqueryresult.o: ../../pikotools/src/textstream/textstream.h
@ -292,7 +295,7 @@
./transaction.o: ../../pikotools/src/membuffer/membuffer.h
./transaction.o: ../../pikotools/src/textstream/types.h
./transaction.o: ../../pikotools/src/log/filelog.h modelconnector.h clearer.h
./transaction.o: ft.h dbconnector.h queryresult.h flatconnector.h
./transaction.o: ft.h dbconnector.h queryresult.h flatconnector.h export.h
./xmlconnector.o: xmlconnector.h flatconnector.h
./xmlconnector.o: ../../pikotools/src/textstream/textstream.h
./xmlconnector.o: ../../pikotools/src/textstream/stream.h
@ -305,10 +308,10 @@
./xmlconnector.o: ../../pikotools/src/utf8/utf8_private.h
./xmlconnector.o: ../../pikotools/src/date/date.h
./xmlconnector.o: ../../pikotools/src/membuffer/membuffer.h
./xmlconnector.o: ../../pikotools/src/textstream/types.h xmlexpression.h
./xmlconnector.o: flatexpression.h baseexpression.h morm_types.h modelenv.h
./xmlconnector.o: modeldata.h cursorhelper.h queryresult.h
./xmlconnector.o: ../../pikotools/src/log/log.h
./xmlconnector.o: ../../pikotools/src/textstream/types.h export.h
./xmlconnector.o: xmlexpression.h flatexpression.h baseexpression.h
./xmlconnector.o: morm_types.h modelenv.h modeldata.h cursorhelper.h
./xmlconnector.o: queryresult.h ../../pikotools/src/log/log.h
./xmlconnector.o: ../../pikotools/src/log/filelog.h finderhelper.h
./xmlconnector.o: fieldvaluehelper.h wrapper.h spacewrapper.h
./xmlconnector.o: baseobjectwrapper.h modelcontainerwrapper.h select.h ft.h
@ -331,6 +334,47 @@
./xmlexpression.o: ../../pikotools/src/log/filelog.h finderhelper.h
./xmlexpression.o: fieldvaluehelper.h wrapper.h spacewrapper.h
./xmlexpression.o: baseobjectwrapper.h modelcontainerwrapper.h select.h ft.h
./xmlexpression.o: ../../pikotools/src/convert/text.h
./xmlexpression.o: export.h ../../pikotools/src/convert/text.h
./xmlexpression.o: ../../pikotools/src/convert/misc.h
./xmlexpression.o: ../../pikotools/src/convert/text.h
./csvexpression.o: csvexpression.h flatexpression.h baseexpression.h
./csvexpression.o: ../../pikotools/src/textstream/textstream.h
./csvexpression.o: ../../pikotools/src/textstream/stream.h
./csvexpression.o: ../../pikotools/src/space/space.h
./csvexpression.o: ../../pikotools/src/textstream/types.h
./csvexpression.o: ../../pikotools/src/convert/inttostr.h
./csvexpression.o: ../../pikotools/src/utf8/utf8.h
./csvexpression.o: ../../pikotools/src/textstream/stream.h
./csvexpression.o: ../../pikotools/src/utf8/utf8_templates.h
./csvexpression.o: ../../pikotools/src/utf8/utf8_private.h
./csvexpression.o: ../../pikotools/src/date/date.h
./csvexpression.o: ../../pikotools/src/membuffer/membuffer.h
./csvexpression.o: ../../pikotools/src/textstream/types.h morm_types.h
./csvexpression.o: modelenv.h modeldata.h cursorhelper.h queryresult.h
./csvexpression.o: ../../pikotools/src/log/log.h
./csvexpression.o: ../../pikotools/src/log/filelog.h finderhelper.h
./csvexpression.o: fieldvaluehelper.h wrapper.h spacewrapper.h
./csvexpression.o: baseobjectwrapper.h modelcontainerwrapper.h select.h ft.h
./csvexpression.o: export.h ../../pikotools/src/convert/text.h
./csvexpression.o: ../../pikotools/src/convert/misc.h
./csvexpression.o: ../../pikotools/src/convert/text.h
./csvconnector.o: csvconnector.h flatconnector.h
./csvconnector.o: ../../pikotools/src/textstream/textstream.h
./csvconnector.o: ../../pikotools/src/textstream/stream.h
./csvconnector.o: ../../pikotools/src/space/space.h
./csvconnector.o: ../../pikotools/src/textstream/types.h
./csvconnector.o: ../../pikotools/src/convert/inttostr.h
./csvconnector.o: ../../pikotools/src/utf8/utf8.h
./csvconnector.o: ../../pikotools/src/textstream/stream.h
./csvconnector.o: ../../pikotools/src/utf8/utf8_templates.h
./csvconnector.o: ../../pikotools/src/utf8/utf8_private.h
./csvconnector.o: ../../pikotools/src/date/date.h
./csvconnector.o: ../../pikotools/src/membuffer/membuffer.h
./csvconnector.o: ../../pikotools/src/textstream/types.h export.h
./csvconnector.o: csvexpression.h flatexpression.h baseexpression.h
./csvconnector.o: morm_types.h modelenv.h modeldata.h cursorhelper.h
./csvconnector.o: queryresult.h ../../pikotools/src/log/log.h
./csvconnector.o: ../../pikotools/src/log/filelog.h finderhelper.h
./csvconnector.o: fieldvaluehelper.h wrapper.h spacewrapper.h
./csvconnector.o: baseobjectwrapper.h modelcontainerwrapper.h select.h ft.h
./csvconnector.o: ../../pikotools/src/convert/text.h

View File

@ -51,6 +51,7 @@ BaseExpression::BaseExpression()
clear();
}
BaseExpression::~BaseExpression()
{
}
@ -61,6 +62,7 @@ void BaseExpression::clear()
out_stream = nullptr;
is_first_field = false;
work_mode = 0;
output_type = 0;
use_prefix = false;
}
@ -77,6 +79,18 @@ int BaseExpression::get_work_mode()
}
void BaseExpression::set_output_type(int output_type)
{
this->output_type = output_type;
}
int BaseExpression::get_output_type()
{
return output_type;
}
pt::TextStream * BaseExpression::get_text_stream()
{
@ -184,6 +198,13 @@ bool BaseExpression::can_field_model_be_generated(bool has_model_primary_key, co
}
bool BaseExpression::can_field_list_be_generated(const FT &)
{
return true;
}
bool BaseExpression::should_field_model_be_generated_as_null(bool has_model_primary_key, const FT & field_type)
{
return false;

View File

@ -43,6 +43,7 @@
#include "morm_types.h"
#include "modelenv.h"
#include "ft.h"
#include "export.h"
#include "convert/text.h"
#ifdef MORM_HAS_EZC_LIBRARY
@ -69,6 +70,9 @@ public:
virtual void set_work_mode(int work_mode);
virtual int get_work_mode();
virtual void set_output_type(int output_type);
virtual int get_output_type();
virtual pt::TextStream * get_text_stream();
virtual void set_text_stream(pt::TextStream * out_stream);
@ -160,7 +164,7 @@ public:
void field_list(const wchar_t * field_name, ModelContainer & field_value, ModelContainerType * model_container_type,
const FT & field_type, ModelConnector * model_connector, ModelEnv * model_env, IsContainerByValueRenameMe * foo)
{
if( out_stream && can_field_be_generated(field_type) )
if( out_stream && can_field_be_generated(field_type) && can_field_list_be_generated(field_type) )
{
field_before();
@ -192,12 +196,15 @@ public:
if( work_mode == MORM_WORK_MODE_MODEL_FIELDS )
{
put_field_name_and_table_if_needed(field_name, field_type, model_env);
if( output_type == MORM_OUTPUT_TYPE_FIELDS_RECURSIVE )
generate_from_model(field_model, field_type);
else
put_field_name_and_table_if_needed(field_name, field_type, model_env);
}
else
if( work_mode == MORM_WORK_MODE_MODEL_VALUES )
{
generate_from_model(field_model, field_type); // is it ok as a value?
generate_from_model(field_model, field_type);
}
else
if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES )
@ -341,6 +348,7 @@ public:
protected:
int work_mode; /* what to do: generating fields list, values list or fields-values list */
int output_type;
bool is_first_field;
pt::TextStream * out_stream;
bool use_prefix;
@ -354,6 +362,7 @@ protected:
virtual bool can_field_be_generated(const FT &);
virtual bool can_field_model_be_generated(bool has_model_primary_key, const FT & field_type);
virtual bool can_field_list_be_generated(const FT &);
virtual bool should_field_model_be_generated_as_null(bool has_model_primary_key, const FT & field_type);
virtual void field_before();

81
src/csvconnector.cpp Normal file
View File

@ -0,0 +1,81 @@
/*
* 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) 2023, 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 "csvconnector.h"
#include "csvexpression.h"
namespace morm
{
CSVConnector::CSVConnector()
{
}
void CSVConnector::allocate_default_expression()
{
deallocate_expression();
flat_expression = new CSVExpression();
expression_allocated = true;
}
void CSVConnector::to_text(pt::TextStream & stream, Model & model, Export exp)
{
allocate_default_expression_if_needed();
if( flat_expression )
{
flat_expression->clear();
if( exp.is_export_headers() )
{
flat_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS);
flat_expression->set_output_type(MORM_OUTPUT_TYPE_FIELDS_RECURSIVE);
}
else
{
flat_expression->set_work_mode(MORM_WORK_MODE_MODEL_VALUES);
}
flat_expression->allow_to_use_prefix(false);
flat_expression->generate_from_model(stream, model);
}
}
}

63
src/csvconnector.h Normal file
View File

@ -0,0 +1,63 @@
/*
* 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) 2023, 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.
*
*/
#ifndef headerfile_morm_src_csvconnector
#define headerfile_morm_src_csvconnector
#include "flatconnector.h"
namespace morm
{
class CSVConnector : public FlatConnector
{
public:
CSVConnector();
void to_text(pt::TextStream & stream, Model & model, Export exp);
protected:
void allocate_default_expression();
};
}
#endif

97
src/csvexpression.cpp Normal file
View File

@ -0,0 +1,97 @@
/*
* 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) 2023, 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 "csvexpression.h"
#include "morm_types.h"
#include "convert/misc.h"
namespace morm
{
bool CSVExpression::can_field_list_be_generated(const FT &)
{
return false;
}
void CSVExpression::field_before()
{
BaseExpression::field_before();
if( !is_first_field )
{
(*out_stream) << ",";
}
}
void CSVExpression::after_generate_from_model()
{
(*out_stream) << ",";
}
void CSVExpression::before_field_name()
{
(*out_stream) << '"';
}
void CSVExpression::after_field_name()
{
(*out_stream) << '"';
}
void CSVExpression::before_field_value_string(const FT & field_type, ModelEnv * model_env)
{
(*out_stream) << '"';
}
void CSVExpression::after_field_value_string(const FT & field_type, ModelEnv * model_env)
{
(*out_stream) << '"';
}
bool CSVExpression::esc_char(wchar_t val, pt::TextStream & stream, const FT & field_type, ModelEnv * model_env)
{
return pt::try_esc_to_csv(val, stream);
}
}

68
src/csvexpression.h Normal file
View File

@ -0,0 +1,68 @@
/*
* 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) 2023, 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.
*
*/
#ifndef headerfile_morm_src_csvexpression
#define headerfile_morm_src_csvexpression
#include "flatexpression.h"
namespace morm
{
class CSVExpression : public FlatExpression
{
protected:
bool can_field_list_be_generated(const FT &);
void field_before();
void after_generate_from_model();
void before_field_name();
void after_field_name();
void before_field_value_string(const FT & field_type, ModelEnv * model_env);
void after_field_value_string(const FT & field_type, ModelEnv * model_env);
using BaseExpression::esc;
bool esc_char(wchar_t val, pt::TextStream & stream, const FT & field_type, ModelEnv * model_env);
};
}
#endif

View File

@ -42,7 +42,6 @@ namespace morm
DbExpression::DbExpression()
{
output_type = 0;
}
@ -51,18 +50,6 @@ DbExpression::~DbExpression()
}
void DbExpression::set_output_type(int output_type)
{
this->output_type = output_type;
}
int DbExpression::get_output_type()
{
return output_type;
}
bool DbExpression::can_field_be_generated(const FT & field_type)
{
if( output_type == MORM_OUTPUT_TYPE_DB_INSERT )

View File

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2018-2022, Tomasz Sowa
* Copyright (c) 2018-2023, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -53,11 +53,6 @@ public:
constexpr static const char * COLUMN_ROWS_COUNTER_POSTFIX = "_autoadded_rows_counter";
virtual void set_output_type(int output_type);
virtual int get_output_type();
virtual void prepare_to_where_clause();
virtual DbExpression & group_or(pt::TextStream & stream);
@ -86,7 +81,7 @@ public:
protected:
int output_type;
std::vector<int> conjunctions;
bool can_field_be_generated(const FT & field_type);

128
src/export.h Normal file
View File

@ -0,0 +1,128 @@
/*
* 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) 2023, 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 SOFTWAExportType, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef headerfile_morm_src_export
#define headerfile_morm_src_export
namespace morm
{
/*
* field types
*/
class Export
{
public:
enum ExportType
{
default_type = 0,
no_clear_stream = 1,
dump_mode = 2,
export_headers = 4,
};
/*
* type can be a superposition from ExportType values
*/
int type;
Export()
{
type = 0;
}
Export(const Export & field_type)
{
type = field_type.type;
}
Export(ExportType type)
{
this->type = static_cast<int>(type);
}
Export(int type)
{
this->type = type;
}
Export & operator=(const Export & field_type)
{
type = field_type.type;
return *this;
}
Export & operator=(ExportType type)
{
this->type = static_cast<int>(type);
return *this;
}
Export & operator=(int type)
{
this->type = type;
return *this;
}
bool is_flag_set(int flag_mask) const
{
return (type & flag_mask) != 0;
}
bool is_no_clear_stream() const
{
return is_flag_set(no_clear_stream);
}
bool is_dump_mode() const
{
return is_flag_set(dump_mode);
}
bool is_export_headers() const
{
return is_flag_set(export_headers);
}
};
}
#endif

View File

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2018-2021, Tomasz Sowa
* Copyright (c) 2018-2023, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -67,8 +67,7 @@ FlatExpression * FlatConnector::get_expression()
}
void FlatConnector::to_text(pt::TextStream & stream, Model & model)
void FlatConnector::to_text(pt::TextStream & stream, Model & model, Export exp)
{
allocate_default_expression_if_needed();
@ -82,8 +81,6 @@ void FlatConnector::to_text(pt::TextStream & stream, Model & model)
}
void FlatConnector::deallocate_expression()
{
if( expression_allocated )

View File

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2018-2021, Tomasz Sowa
* Copyright (c) 2018-2023, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -36,6 +36,7 @@
#define headerfile_morm_src_flatconnector
#include "textstream/textstream.h"
#include "export.h"
namespace morm
@ -51,7 +52,7 @@ public:
FlatConnector();
virtual ~FlatConnector();
virtual void to_text(pt::TextStream & stream, Model & model);
virtual void to_text(pt::TextStream & stream, Model & model, Export exp);
virtual void set_expression(FlatExpression & expression);
virtual FlatExpression * get_expression();

View File

@ -39,6 +39,17 @@
namespace morm
{
FlatExpression::FlatExpression()
{
}
FlatExpression::~FlatExpression()
{
}
bool FlatExpression::should_field_model_be_generated_as_null(bool has_model_primary_key, const FT & field_type)
{
return !has_model_primary_key && field_type.is_serialize_to_null_if_null();

View File

@ -45,6 +45,9 @@ class FlatExpression : public BaseExpression
{
public:
FlatExpression();
virtual ~FlatExpression();
protected:

View File

@ -347,10 +347,9 @@ bool Model::get_raw_value(const wchar_t * db_field_name, const wchar_t * flat_fi
void Model::to_text(pt::TextStream & stream, ModelData * model_data, bool clear_stream, bool dump_mode)
void Model::to_text(pt::TextStream & stream, ModelData * model_data, Export exp)
{
if( clear_stream )
if( !exp.is_no_clear_stream() )
{
stream.clear();
}
@ -359,7 +358,7 @@ void Model::to_text(pt::TextStream & stream, ModelData * model_data, bool clear_
model_env = &model_env_local;
model_env->has_primary_key_set = has_primary_key_set;
model_env->model_work_mode = MORM_MODEL_WORK_MODE_GENERATING_FLAT_STRING;
model_env->dump_mode = dump_mode;
model_env->dump_mode = exp.is_dump_mode();
model_env->model_data = model_data;
model_env->model = this;
@ -372,7 +371,7 @@ void Model::to_text(pt::TextStream & stream, ModelData * model_data, bool clear_
try
{
// table(); at the moment flat strings (json/space) do not need a table name
flat_connector->to_text(stream, *this);
flat_connector->to_text(stream, *this, exp);
}
catch(...)
{
@ -386,6 +385,32 @@ void Model::to_text(pt::TextStream & stream, ModelData * model_data, bool clear_
}
void Model::to_text(pt::TextStream & stream, ModelData & model_data, Export exp)
{
to_text(stream, &model_data, exp);
}
void Model::to_text(pt::TextStream & stream, Export exp)
{
to_text(stream, nullptr, exp);
}
void Model::to_text(pt::TextStream & stream, ModelData * model_data, bool clear_stream, bool dump_mode)
{
Export exp = Export::default_type;
if( !clear_stream )
exp = exp.type | Export::no_clear_stream;
if( dump_mode )
exp = exp.type | Export::dump_mode;
to_text(stream, model_data, exp);
}
void Model::to_text(pt::TextStream & stream, ModelData & model_data, bool clear_stream, bool dump_mode)
{
to_text(stream, &model_data, clear_stream, dump_mode);
@ -399,6 +424,7 @@ void Model::to_text(pt::TextStream & stream, bool clear_stream, bool dump_mode)
void Model::to_text(std::string & str, ModelData * model_data, bool clear_string, bool dump_mode)
{
if( model_connector )

View File

@ -177,9 +177,13 @@ public:
virtual void get_table_name(std::wstring & str, bool with_schema_name = true, ModelData * model_data = nullptr, bool clear_string = true);
virtual void get_table_name(std::string & str, bool with_schema_name = true, ModelData * model_data = nullptr, bool clear_string = true);
virtual void to_text(pt::TextStream & stream, ModelData * model_data, bool clear_stream = true, bool dump_mode = false);
virtual void to_text(pt::TextStream & stream, ModelData & model_data, bool clear_stream = true, bool dump_mode = false);
virtual void to_text(pt::TextStream & stream, bool clear_stream = true, bool dump_mode = false);
virtual void to_text(pt::TextStream & stream, ModelData * model_data, Export exp = Export::default_type);
virtual void to_text(pt::TextStream & stream, ModelData & model_data, Export exp = Export::default_type);
virtual void to_text(pt::TextStream & stream, Export exp = Export::default_type);
virtual void to_text(pt::TextStream & stream, ModelData * model_data, bool clear_stream, bool dump_mode);
virtual void to_text(pt::TextStream & stream, ModelData & model_data, bool clear_stream, bool dump_mode);
virtual void to_text(pt::TextStream & stream, bool clear_stream, bool dump_mode);
virtual void to_text(std::string & str, ModelData * model_data, bool clear_string = true, bool dump_mode = false);
virtual void to_text(std::string & str, ModelData & model_data, bool clear_string = true, bool dump_mode = false);

View File

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2018-2022, Tomasz Sowa
* Copyright (c) 2018-2023, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -102,6 +102,7 @@
#define MORM_OUTPUT_TYPE_WHERE_LIKE 27
#define MORM_OUTPUT_TYPE_WHERE_ILIKE 28
#define MORM_OUTPUT_TYPE_FIELDS_RECURSIVE 29
#define MORM_CONJUNCTION_AND 1
#define MORM_CONJUNCTION_OR 2

View File

@ -100,7 +100,7 @@ void XMLConnector::allocate_default_expression()
}
void XMLConnector::to_text(pt::TextStream & stream, Model & model)
void XMLConnector::to_text(pt::TextStream & stream, Model & model, Export exp)
{
allocate_default_expression_if_needed();

View File

@ -57,7 +57,7 @@ public:
virtual void set_root_element(const std::wstring & root_name);
virtual std::wstring & get_root_element();
void to_text(pt::TextStream & stream, Model & model);
void to_text(pt::TextStream & stream, Model & model, Export exp);
protected: