From 346fe193f3a4f0f9851b4e602f81fc77cdc973dd Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Sun, 15 Jul 2018 22:36:04 +0000 Subject: [PATCH] start working on generating documentation from a model git-svn-id: svn://ttmath.org/publicrep/morm/trunk@1124 e52654a7-88a9-db11-a3e9-0013d4bc506e --- src/Makefile.dep | 25 +++ src/Makefile.o.dep | 2 +- src/baseexpression.cpp | 138 +++++++++++++++ src/baseexpression.h | 63 ++++++- src/dbexpression.h | 9 +- src/dochtmlconnector.cpp | 66 +++++++ src/dochtmlconnector.h | 60 +++++++ src/dochtmlexpression.cpp | 71 ++++++++ src/dochtmlexpression.h | 68 ++++++++ src/model.cpp | 48 +++++ src/model.h | 357 ++++++++++++++++++++++++++++++++++++++ src/modelconnector.cpp | 14 ++ src/modelconnector.h | 5 + src/morm.h | 12 +- src/morm_types.h | 12 +- 15 files changed, 938 insertions(+), 12 deletions(-) create mode 100644 src/dochtmlconnector.cpp create mode 100644 src/dochtmlconnector.h create mode 100644 src/dochtmlexpression.cpp create mode 100644 src/dochtmlexpression.h diff --git a/src/Makefile.dep b/src/Makefile.dep index 5218aca..cf4df03 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep @@ -48,6 +48,31 @@ dbexpression.o: ../../pikotools/convert/strtoint.h dbexpression.o: ../../pikotools/convert/text.h ../../pikotools/convert/misc.h dbexpression.o: ../../pikotools/membuffer/membuffer.h dbexpression.o: ../../pikotools/textstream/types.h morm_types.h +dochtmlconnector.o: dochtmlconnector.h flatconnector.h +dochtmlconnector.o: ../../pikotools/textstream/textstream.h +dochtmlconnector.o: ../../pikotools/space/space.h +dochtmlconnector.o: ../../pikotools/textstream/types.h +dochtmlconnector.o: ../../pikotools/date/date.h +dochtmlconnector.o: ../../pikotools/convert/convert.h +dochtmlconnector.o: ../../pikotools/convert/inttostr.h +dochtmlconnector.o: ../../pikotools/convert/strtoint.h +dochtmlconnector.o: ../../pikotools/convert/text.h +dochtmlconnector.o: ../../pikotools/convert/misc.h +dochtmlconnector.o: ../../pikotools/membuffer/membuffer.h +dochtmlconnector.o: ../../pikotools/textstream/types.h dochtmlexpression.h +dochtmlconnector.o: flatexpression.h baseexpression.h morm_types.h +dochtmlexpression.o: dochtmlexpression.h flatexpression.h baseexpression.h +dochtmlexpression.o: ../../pikotools/textstream/textstream.h +dochtmlexpression.o: ../../pikotools/space/space.h +dochtmlexpression.o: ../../pikotools/textstream/types.h +dochtmlexpression.o: ../../pikotools/date/date.h +dochtmlexpression.o: ../../pikotools/convert/convert.h +dochtmlexpression.o: ../../pikotools/convert/inttostr.h +dochtmlexpression.o: ../../pikotools/convert/strtoint.h +dochtmlexpression.o: ../../pikotools/convert/text.h +dochtmlexpression.o: ../../pikotools/convert/misc.h +dochtmlexpression.o: ../../pikotools/membuffer/membuffer.h +dochtmlexpression.o: ../../pikotools/textstream/types.h morm_types.h flatconnector.o: flatconnector.h ../../pikotools/textstream/textstream.h flatconnector.o: ../../pikotools/space/space.h flatconnector.o: ../../pikotools/textstream/types.h diff --git a/src/Makefile.o.dep b/src/Makefile.o.dep index 21838c4..11c43c6 100644 --- a/src/Makefile.o.dep +++ b/src/Makefile.o.dep @@ -1 +1 @@ -o = baseexpression.o clearer.o dbconnector.o dbexpression.o flatconnector.o flatexpression.o jsonconnector.o jsonexpression.o model.o modelconnector.o postgresqlconnector.o postgresqlexpression.o \ No newline at end of file +o = baseexpression.o clearer.o dbconnector.o dbexpression.o dochtmlconnector.o dochtmlexpression.o flatconnector.o flatexpression.o jsonconnector.o jsonexpression.o model.o modelconnector.o postgresqlconnector.o postgresqlexpression.o \ No newline at end of file diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index 828e458..d24c994 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -68,6 +68,13 @@ void BaseExpression::prepare_to_new_expression() work_mode = 0; } + +PT::TextStream * BaseExpression::get_current_stream() +{ + return out_stream; +} + + void BaseExpression::set_column_prefix(const std::wstring & prefix) { column_prefix = prefix; @@ -91,6 +98,20 @@ void BaseExpression::generate_from_model(Model & model) } } + +/* +void BaseExpression::put_field_doc(void * pointer) +{ + if( out_stream ) + { + before_generate_from_model(); + model.doc_field_pointer = pointer; + model.map_doc_fields(); + after_generate_from_model(); + } +} +*/ + void BaseExpression::before_generate_from_model() { is_first_field = true; @@ -138,6 +159,15 @@ void BaseExpression::put_field_name(const wchar_t * field_name, bool add_column_ } + + +void BaseExpression::put_field_doc(Model & model, const void * field_pointer, bool insertable, bool updatable, bool is_primary_key) +{ + model.doc_field_pointer = field_pointer; + model.map_doc_fields(); + model.doc_field_pointer = nullptr; +} + void BaseExpression::before_field_name() { } @@ -348,6 +378,114 @@ void BaseExpression::esc(const PT::Date & date, PT::TextStream & stream) } +void BaseExpression::put_type(char val, PT::TextStream & stream) +{ + stream << "char"; +} + +void BaseExpression::put_type(unsigned char val, PT::TextStream & stream) +{ + stream << "unsigned char"; +} + + +void BaseExpression::put_type(const std::wstring & val, PT::TextStream & stream) +{ + stream << "text"; +} + +void BaseExpression::put_type(const wchar_t * val, PT::TextStream & stream) +{ + stream << "text"; +} + + +void BaseExpression::put_type(const std::string & val, PT::TextStream & stream) +{ + stream << "text"; +} + +void BaseExpression::put_type(const char * val, PT::TextStream & stream) +{ + stream << "text"; +} + + +void BaseExpression::put_type(bool val, PT::TextStream & stream) +{ + stream << "boolean"; +} + +void BaseExpression::put_type(short val, PT::TextStream & stream) +{ + stream << "short integer"; +} + +void BaseExpression::put_type(unsigned short val, PT::TextStream & stream) +{ + stream << "unsigned short integer"; +} + +void BaseExpression::put_type(int val, PT::TextStream & stream) +{ + stream << "integer"; +} + +void BaseExpression::put_type(unsigned int val, PT::TextStream & stream) +{ + stream << "unsigned integer"; +} + +void BaseExpression::put_type(long val, PT::TextStream & stream) +{ + stream << "long integer"; +} + +void BaseExpression::put_type(unsigned long val, PT::TextStream & stream) +{ + stream << "unsigned long integer"; +} + +void BaseExpression::put_type(long long val, PT::TextStream & stream) +{ + stream << "very long integer"; +} + +void BaseExpression::put_type(unsigned long long val, PT::TextStream & stream) +{ + stream << "unsigned very long integer"; +} + +void BaseExpression::put_type(float val, PT::TextStream & stream) +{ + stream << "float"; +} + +void BaseExpression::put_type(double val, PT::TextStream & stream) +{ + stream << "double"; +} + +void BaseExpression::put_type(long double val, PT::TextStream & stream) +{ + stream << "long double"; +} + +//void BaseExpression::put_type(void* val, PT::TextStream & stream) +//{ +//} + + +void BaseExpression::put_type(const PT::Date & date, PT::TextStream & stream) +{ + stream << "date"; +} + +void BaseExpression::put_type(const Model & model, PT::TextStream & stream) +{ + stream << "object"; +} + } diff --git a/src/baseexpression.h b/src/baseexpression.h index b58e692..15f0677 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -61,6 +61,14 @@ public: virtual void generate_from_model(PT::TextStream & stream, Model & model); + virtual PT::TextStream * get_current_stream(); + + + // give me a better name + virtual void put_field_doc(Model & model, const void * field_pointer, bool insertable, bool updatable, bool is_primary_key); + + + template void field(const wchar_t * field_name, const FieldValue & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false, bool add_column_prefix = true) { @@ -89,6 +97,27 @@ public: } } + template + void field_doc(Model & model, const wchar_t * field_name, const FieldValue & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false, bool add_column_prefix = true) + { + if( out_stream ) + { + field_before(); + + put_field_name(field_name, add_column_prefix); + put_name_value_separator(); + + put_field_doc(model, reinterpret_cast(&field_value), insertable, updatable, is_primary_key); + put_name_value_separator(); + + put_type(field_value, *out_stream); + + field_after(); + } + } + + + /* template void field(const PT::TextStream & field_name, const FieldValue & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false, bool add_column_prefix = true) { @@ -97,6 +126,7 @@ public: return field(field_name_str.c_str(), field_value, insertable, updatable, is_primary_key, add_column_prefix); } + */ template void field_in(PT::TextStream & stream, const wchar_t * field_name, const std::set & container) @@ -239,7 +269,7 @@ protected: } } - + // what about lists with a pod types? e.g. list template void put_field_value_list(ModelContainer & field_value, ModelConnector * model_connector, int model_connector_mode) { @@ -342,6 +372,37 @@ protected: virtual void put_name_value_separator(); + virtual void put_type(char val, PT::TextStream & stream); + virtual void put_type(unsigned char val, PT::TextStream & stream); + + virtual void put_type(const std::wstring & val, PT::TextStream & stream); + virtual void put_type(const wchar_t * val, PT::TextStream & stream); + + virtual void put_type(const std::string & val, PT::TextStream & stream); + virtual void put_type(const char * val, PT::TextStream & stream); + + virtual void put_type(bool val, PT::TextStream & stream); + virtual void put_type(short val, PT::TextStream & stream); + virtual void put_type(unsigned short val, PT::TextStream & stream); + virtual void put_type(int val, PT::TextStream & stream); + virtual void put_type(unsigned int val, PT::TextStream & stream); + virtual void put_type(long val, PT::TextStream & stream); + virtual void put_type(unsigned long val, PT::TextStream & stream); + virtual void put_type(long long val, PT::TextStream & stream); + virtual void put_type(unsigned long long val, PT::TextStream & stream); + virtual void put_type(float val, PT::TextStream & stream); + virtual void put_type(double val, PT::TextStream & stream); + virtual void put_type(long double val, PT::TextStream & stream); + //virtual void put_type(void* val, PT::TextStream & stream); + + virtual void put_type(const PT::Date & date, PT::TextStream & stream); + virtual void put_type(const Model & model, PT::TextStream & stream); + + template + void put_type(const std::list & model, PT::TextStream & stream) + { + stream << "table"; + } }; diff --git a/src/dbexpression.h b/src/dbexpression.h index 3de17b7..b48b989 100644 --- a/src/dbexpression.h +++ b/src/dbexpression.h @@ -65,10 +65,13 @@ public: template void add_field_for_select(const wchar_t * new_column_expression, const wchar_t * new_column_name, FieldValue & field_value) { - PT::TextStream column_expression; - column_expression << new_column_expression << " as " << new_column_name; + std::wstring column_expression; // field() methods can be called recursively, so don't make it as class object - field(column_expression, field_value, false, false, false, false); + column_expression = new_column_expression; + column_expression += L" as "; + column_expression += new_column_name; + + field(column_expression.c_str(), field_value, false, false, false, false); } diff --git a/src/dochtmlconnector.cpp b/src/dochtmlconnector.cpp new file mode 100644 index 0000000..99124ce --- /dev/null +++ b/src/dochtmlconnector.cpp @@ -0,0 +1,66 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * 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 "dochtmlconnector.h" +#include "dochtmlexpression.h" + + +namespace morm +{ + +void DocHtmlConnector::allocate_default_expression() +{ + deallocate_expression(); + flat_expression = new DocHtmlExpression(); + expression_allocated = true; +} + + +void DocHtmlConnector::to_text(PT::TextStream & stream, Model & model) +{ + allocate_default_expression_if_needed(); + + if( flat_expression ) + { +// flat_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS); + + stream << "NameDescriptionType\n"; + + flat_expression->generate_from_model(stream, model); + } +} + + +} diff --git a/src/dochtmlconnector.h b/src/dochtmlconnector.h new file mode 100644 index 0000000..38f3bb4 --- /dev/null +++ b/src/dochtmlconnector.h @@ -0,0 +1,60 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * 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. + * + */ + +#ifndef headerfile_morm_dochtmlconnector +#define headerfile_morm_dochtmlconnector + +#include "flatconnector.h" + + + +namespace morm +{ + + +class DocHtmlConnector : public FlatConnector +{ +public: + + void to_text(PT::TextStream & stream, Model & model); + +protected: + + void allocate_default_expression(); + +}; + +} + +#endif diff --git a/src/dochtmlexpression.cpp b/src/dochtmlexpression.cpp new file mode 100644 index 0000000..ad7a0d8 --- /dev/null +++ b/src/dochtmlexpression.cpp @@ -0,0 +1,71 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * 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 "dochtmlexpression.h" + + +namespace morm +{ + + + +void DocHtmlExpression::field_before() +{ + (*out_stream) << ""; +} + + + +void DocHtmlExpression::field_after() +{ + (*out_stream) << "\n"; +} + + +void DocHtmlExpression::put_name_value_separator() +{ + (*out_stream) << ""; +} + + + + + + + + + +} + + diff --git a/src/dochtmlexpression.h b/src/dochtmlexpression.h new file mode 100644 index 0000000..1fb97fc --- /dev/null +++ b/src/dochtmlexpression.h @@ -0,0 +1,68 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * 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. + * + */ + +#ifndef headerfile_morm_dochtmlexpression +#define headerfile_morm_dochtmlexpression + +#include "flatexpression.h" + + +namespace morm +{ + + +class DocHtmlExpression : public FlatExpression +{ +public: + + +protected: + + virtual void field_before(); + virtual void field_after(); + + + void put_name_value_separator(); + + + +}; + +} + + + +#endif + + diff --git a/src/model.cpp b/src/model.cpp index ef5891f..92aba1a 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -42,6 +42,7 @@ Model::Model() { model_connector = nullptr; model_data = nullptr; + doc_field_pointer = nullptr; save_mode = DO_INSERT_ON_SAVE; model_connector_mode = MORM_MODEL_CONNECTOR_MODE_NONE; } @@ -424,6 +425,53 @@ void Model::generate_select_columns(PT::TextStream & stream, const std::wstring } + +void Model::generate_doc_for_flat(PT::TextStream & stream, bool clear_stream) +{ + if( clear_stream ) + { + stream.clear(); + } + + model_connector_mode = MORM_MODEL_CONNECTOR_MODE_GENERATING_DOC_FOR_FLAT; + + if( model_connector ) + { + FlatConnector * flat_connector = model_connector->get_doc_connector(); // different connector will be here + + if( flat_connector ) + { + flat_connector->to_text(stream, *this); + } + } + + model_connector_mode = MORM_MODEL_CONNECTOR_MODE_NONE; +} + + +void Model::generate_doc_for_db(PT::TextStream & stream, bool clear_stream) +{ + if( clear_stream ) + { + stream.clear(); + } + + model_connector_mode = MORM_MODEL_CONNECTOR_MODE_GENERATING_DOC_FOR_DB; + + if( model_connector ) + { + FlatConnector * flat_connector = model_connector->get_doc_connector(); // different connector will be here + + if( flat_connector ) + { + flat_connector->to_text(stream, *this); + } + } + + model_connector_mode = MORM_MODEL_CONNECTOR_MODE_NONE; +} + + void Model::map_values_from_query() { model_connector_mode = MORM_MODEL_CONNECTOR_MODE_READING_VALUE_FROM_DB_RESULTSET; diff --git a/src/model.h b/src/model.h index 2acb169..3119802 100644 --- a/src/model.h +++ b/src/model.h @@ -106,6 +106,8 @@ public: * */ virtual void map_fields() = 0; + virtual void map_doc_fields() {} ; + /* @@ -139,6 +141,9 @@ public: virtual void generate_select_columns(PT::TextStream & stream, const std::wstring & column_prefix); + virtual void generate_doc_for_flat(PT::TextStream & stream, bool clear_stream = true); + virtual void generate_doc_for_db(PT::TextStream & stream, bool clear_stream = true); + // set object to default values virtual void clear(); @@ -148,6 +153,7 @@ protected: ModelConnector * model_connector; SaveMode save_mode; ModelData * model_data; + const void * doc_field_pointer; int model_connector_mode; Model(); @@ -402,6 +408,233 @@ protected: field_list(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); } +////////////////////// + void doc(const wchar_t * field_name, char & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + + void doc(const wchar_t * field_name, unsigned char & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + + void doc(const wchar_t * field_name, std::wstring & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + +// void doc(const wchar_t * field_name, wchar_t * field_value) +// { +// doc_field_generic(field_name, field_name, field_value); +// } + + void doc(const wchar_t * field_name, std::string & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + +// void doc(const wchar_t * field_name, char * field_value) +// { +// doc_field_generic(field_name, field_name, field_value); +// } + + void doc(const wchar_t * field_name, bool & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + + void doc(const wchar_t * field_name, short & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + + void doc(const wchar_t * field_name, unsigned short & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + + void doc(const wchar_t * field_name, int & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + + void doc(const wchar_t * field_name, unsigned int & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + + void doc(const wchar_t * field_name, long & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + + void doc(const wchar_t * field_name, unsigned long & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + + void doc(const wchar_t * field_name, long long & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + + void doc(const wchar_t * field_name, unsigned long long & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + + void doc(const wchar_t * field_name, float & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + + void doc(const wchar_t * field_name, double & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + + void doc(const wchar_t * field_name, long double & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + +// void doc(const wchar_t * field_name, void * field_value) +// { +// doc_field_generic(field_name, field_name, field_value); +// } + + void doc(const wchar_t * field_name, PT::Date & field_value) + { + doc_field_generic(field_name, field_name, field_value); + } + + void doc(const wchar_t * field_name, Model & field_value) + { + doc_field_model(field_name, field_name, field_value); + } + + template + void doc(const wchar_t * field_name, std::list & field_value) + { + doc_field_list(field_name, field_name, field_value); + } + + +////////////////////// + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, char & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned char & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::wstring & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + +// void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, wchar_t * field_value) +// { +// doc_field_generic(db_field_name, flat_field_name, field_value); +// } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::string & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + +// void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, char * field_value) +// { +// doc_field_generic(db_field_name, flat_field_name, field_value); +// } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, bool & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, short & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned short & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, int & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned int & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, long & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned long & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, long long & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned long long & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, float & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, double & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, long double & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + +// void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, void * field_value) +// { +// doc_field_generic(db_field_name, flat_field_name, field_value); +// } + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, PT::Date & field_value) + { + doc_field_generic(db_field_name, flat_field_name, field_value); + } + + + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_value) + { + doc_field_model(db_field_name, flat_field_name, field_value); + } + + template + void doc(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::list & field_value) + { + doc_field_list(db_field_name, flat_field_name, field_value); + } + ////////////////////// @@ -464,6 +697,23 @@ protected: clearer->clear_value(field_value); } } + + if( model_connector_mode == MORM_MODEL_CONNECTOR_MODE_GENERATING_DOC_FOR_FLAT ) + { + FlatConnector * doc_connector = model_connector->get_doc_connector(); // different connector will be here (doc, flat) + + if( doc_connector ) + { + FlatExpression * doc_expression = doc_connector->get_expression(); + + // potrzebujemy dwa rozne doce, jeden dla flat drugi dla bazy danych + if( doc_expression && !is_empty_field(flat_field_name) ) + { + // insertable, updatable and is_primary_key are ignored here + doc_expression->field_doc(*this, flat_field_name, field_value, insertable, updatable, is_primary_key); + } + } + } } } @@ -501,6 +751,23 @@ protected: clearer->clear_model(field_model); } } + + if( model_connector_mode == MORM_MODEL_CONNECTOR_MODE_GENERATING_DOC_FOR_FLAT ) + { + FlatConnector * doc_connector = model_connector->get_doc_connector(); // different connector will be here (doc, flat) + + if( doc_connector ) + { + FlatExpression * doc_expression = doc_connector->get_expression(); + + // potrzebujemy dwa rozne doce, jeden dla flat drugi dla bazy danych + if( doc_expression && !is_empty_field(flat_field_name) ) + { + // insertable, updatable and is_primary_key are ignored here + doc_expression->field_doc(*this, flat_field_name, field_model, insertable, updatable, is_primary_key); + } + } + } } } @@ -538,9 +805,99 @@ protected: clearer->clear_container(field_container); } } + + if( model_connector_mode == MORM_MODEL_CONNECTOR_MODE_GENERATING_DOC_FOR_FLAT ) + { + FlatConnector * doc_connector = model_connector->get_doc_connector(); // different connector will be here (doc, flat) + + if( doc_connector ) + { + FlatExpression * doc_expression = doc_connector->get_expression(); + + // potrzebujemy dwa rozne doce, jeden dla flat drugi dla bazy danych + if( doc_expression && !is_empty_field(flat_field_name) ) + { + // insertable, updatable and is_primary_key are ignored here + doc_expression->field_doc(*this, flat_field_name, field_container, insertable, updatable, is_primary_key); + } + } + } } } + //// + template + void doc_field_generic(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value) + { + if( reinterpret_cast(&field_value) == doc_field_pointer && model_connector ) + { + FlatConnector * doc_connector = model_connector->get_doc_connector(); + + if( doc_connector ) + { + FlatExpression * doc_expression = doc_connector->get_expression(); + + if( doc_expression ) + { + PT::TextStream * out_stream = doc_expression->get_current_stream(); + + if( out_stream ) + { + (*out_stream) << flat_field_name; + } + } + } + } + } + + + void doc_field_model(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model) + { + if( reinterpret_cast(&field_model) == doc_field_pointer && model_connector ) + { + FlatConnector * doc_connector = model_connector->get_doc_connector(); + + if( doc_connector ) + { + FlatExpression * doc_expression = doc_connector->get_expression(); + + if( doc_expression ) + { + PT::TextStream * out_stream = doc_expression->get_current_stream(); + + if( out_stream ) + { + (*out_stream) << flat_field_name; + } + } + } + } + } + + + template + void doc_field_list(const wchar_t * db_field_name, const wchar_t * flat_field_name, ModelContainer & field_container) + { + if( reinterpret_cast(&field_container) == doc_field_pointer && model_connector ) + { + FlatConnector * doc_connector = model_connector->get_doc_connector(); + + if( doc_connector ) + { + FlatExpression * doc_expression = doc_connector->get_expression(); + + if( doc_expression ) + { + PT::TextStream * out_stream = doc_expression->get_current_stream(); + + if( out_stream ) + { + (*out_stream) << flat_field_name; + } + } + } + } + } public: diff --git a/src/modelconnector.cpp b/src/modelconnector.cpp index 2e1311a..0de05ee 100644 --- a/src/modelconnector.cpp +++ b/src/modelconnector.cpp @@ -43,6 +43,7 @@ ModelConnector::ModelConnector() { flat_connector = nullptr; db_connector = nullptr; + doc_connector = nullptr; logger = nullptr; out_stream = nullptr; @@ -168,6 +169,18 @@ DbConnector * ModelConnector::get_db_connector() } +void ModelConnector::set_doc_connector(FlatConnector & doc_connector) +{ + this->doc_connector = &doc_connector; +} + + +FlatConnector * ModelConnector::get_doc_connector() +{ + return this->doc_connector; +} + + void ModelConnector::set_clearer(Clearer & clearer) { deallocate_clearer(); @@ -183,6 +196,7 @@ Clearer * ModelConnector::get_clearer() + bool ModelConnector::was_db_error() { if( db_connector ) diff --git a/src/modelconnector.h b/src/modelconnector.h index bcb005d..f6d2860 100644 --- a/src/modelconnector.h +++ b/src/modelconnector.h @@ -77,16 +77,21 @@ public: virtual void set_db_connector(DbConnector & db_connector); virtual DbConnector * get_db_connector(); + virtual void set_doc_connector(FlatConnector & doc_connector); + virtual FlatConnector * get_doc_connector(); + virtual void set_clearer(Clearer & clearer); virtual Clearer * get_clearer(); + protected: PT::Logger * logger; FlatConnector * flat_connector; DbConnector * db_connector; + FlatConnector * doc_connector; PT::TextStream * out_stream; // IMPROVE ME give here an interface to the base stream (implement him) bool out_stream_allocated; diff --git a/src/morm.h b/src/morm.h index 8c6ee39..b58f474 100644 --- a/src/morm.h +++ b/src/morm.h @@ -39,12 +39,18 @@ #include "morm_types.h" #include "model.h" #include "finder.h" -#include "jsonconnector.h" -#include "postgresqlconnector.h" + #include "jsonexpression.h" #include "postgresqlexpression.h" +#include "dochtmlexpression.h" + +#include "jsonconnector.h" +#include "postgresqlconnector.h" +#include "dochtmlconnector.h" + #include "modelconnector.h" -#include "finder.h" +#include "clearer.h" + namespace morm diff --git a/src/morm_types.h b/src/morm_types.h index 89eedd8..379412e 100644 --- a/src/morm_types.h +++ b/src/morm_types.h @@ -37,13 +37,15 @@ #define MORM_MODEL_CONNECTOR_MODE_NONE 0 #define MORM_MODEL_CONNECTOR_MODE_GENERATING_FLAT_STRING 1 -#define MORM_MODEL_CONNECTOR_MODE_GENERATING_DB_SQL 2 +#define MORM_MODEL_CONNECTOR_MODE_GENERATING_DOC_FOR_FLAT 2 +#define MORM_MODEL_CONNECTOR_MODE_GENERATING_DOC_FOR_DB 3 +#define MORM_MODEL_CONNECTOR_MODE_GENERATING_DB_SQL 4 // not used at the moment, there are not any flat parsers (json parser) -#define MORM_MODEL_CONNECTOR_MODE_READING_VALUE_FROM_FLAT_STRING 3 +#define MORM_MODEL_CONNECTOR_MODE_READING_VALUE_FROM_FLAT_STRING 5 -#define MORM_MODEL_CONNECTOR_MODE_READING_VALUE_FROM_DB_RESULTSET 4 -#define MORM_MODEL_CONNECTOR_MODE_CLEARING_VALUE 5 +#define MORM_MODEL_CONNECTOR_MODE_READING_VALUE_FROM_DB_RESULTSET 6 +#define MORM_MODEL_CONNECTOR_MODE_CLEARING_VALUE 7 @@ -51,6 +53,8 @@ #define MORM_WORK_MODE_MODEL_VALUES 2 #define MORM_WORK_MODE_MODEL_FIELDS_VALUES 3 + + // IMPROVE ME give me a better name //#define MORM_WORK_MODE_MODEL_SELECT_FROM_DB 4