From 133a45c84b0828e68ddfde192968a6aab1d7958a Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Tue, 9 Mar 2021 18:10:34 +0100 Subject: [PATCH 01/27] Added flag has_primary_key_set to Model Now we know whether the primary key is defined or not and we do not allow to make update/remove if the key is not defined. And when doing insert/update we can put NULL if child models don't have the primary key set (fields with has_foreign_key set to true). Now in after_select() we should also set has_primary_key_set flag or just call get_last_sequence_for_primary_key instead of get_last_sequence. fixed: added prefix +00 when serializing PT::Date to PostgreSQL (time zone) (for a column with a time zone there was a wrong value saved) --- .gitignore | 3 + samples/Makefile | 4 +- samples/Makefile.dep | 2 +- samples/attachment.h | 18 +++- samples/attachment2.h | 120 +++++++++++++++++++++++ samples/language.h | 4 +- samples/person.h | 25 +++-- samples/sample01.h | 72 +++++++++----- samples/type.h | 4 +- src/baseexpression.cpp | 1 + src/baseexpression.h | 28 +++++- src/dbconnector.h | 6 +- src/jsonexpression.cpp | 2 - src/model.cpp | 178 +++++++++++++++++++++++++++++------ src/model.h | 97 ++++++++++++++----- src/modelenv.h | 12 ++- src/postgresqlexpression.cpp | 6 ++ src/postgresqlexpression.h | 2 + 18 files changed, 474 insertions(+), 110 deletions(-) create mode 100644 samples/attachment2.h diff --git a/.gitignore b/.gitignore index 2da86a3..6bbc54f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ .settings/ *.o *.a +log.txt +samples/log.txt +samples/mormsample diff --git a/samples/Makefile b/samples/Makefile index 0a3dd00..f4336cd 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -7,13 +7,13 @@ GLOBAL_WORKING_DIR := $(shell pwd)/../.. endif -CXX = g++7 +CXX = g++10 #CXX = clang++ # -fsanitize=address # -Wl,-rpath=/usr/local/lib/gcc5 or just compile with -static-libstdc++ -CXXFLAGS = -Wl,-rpath=/usr/local/lib/gcc7 -Wfatal-errors -fPIC -Wall -pedantic -O0 -g3 -gdwarf-2 -pthread -std=c++17 -I/usr/local/include -I$(GLOBAL_WORKING_DIR)/pikotools -I$(GLOBAL_WORKING_DIR)/morm/src +CXXFLAGS = -Wl,-rpath=/usr/local/lib/gcc10 -Wfatal-errors -fPIC -Wall -pedantic -O0 -g3 -gdwarf-2 -pthread -std=c++20 -I/usr/local/include -I$(GLOBAL_WORKING_DIR)/pikotools -I$(GLOBAL_WORKING_DIR)/morm/src LDFLAGS = -L/usr/local/lib diff --git a/samples/Makefile.dep b/samples/Makefile.dep index 2150a94..3bdaf52 100644 --- a/samples/Makefile.dep +++ b/samples/Makefile.dep @@ -20,4 +20,4 @@ main.o: ../../morm/src/cursor.h ../../morm/src/jsonexpression.h main.o: ../../morm/src/postgresqlexpression.h ../../morm/src/jsonconnector.h main.o: ../../morm/src/postgresqlconnector.h main.o: ../../morm/src/postgresqlqueryresult.h person.h language.h -main.o: attachment.h type.h +main.o: attachment.h type.h attachment2.h diff --git a/samples/attachment.h b/samples/attachment.h index 176cf4a..50bc389 100644 --- a/samples/attachment.h +++ b/samples/attachment.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2019, Tomasz Sowa + * Copyright (c) 2019-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -38,7 +38,7 @@ #include #include "morm.h" #include "type.h" - +#include "language.h" namespace morm @@ -54,7 +54,7 @@ CREATE TABLE public.attachment ( content text, some_flags bool, created_date timestamp with time zone, - bigint language_id, + language_id bigint, primary key(id) ); @@ -82,7 +82,6 @@ public: field(L"name", name); field(L"content", content); field(L"attachment_id", L"types", types); - //field(L"types", types); field(L"some_flags", some_flags); field(L"created_date", created_date); field(L"language_id", L"language", language); @@ -94,9 +93,18 @@ public: stream << "public.attachment"; } + void after_select() + { + if( has_primary_key_set ) + { + morm::Finder finder(model_connector); + types = finder.select().where().eq(L"attachment_id", id).get_vector(); + } + } + void after_insert() { - get_last_sequence(L"public.attachment_id_seq", id); + get_last_sequence_for_primary_key(L"public.attachment_id_seq", id); } diff --git a/samples/attachment2.h b/samples/attachment2.h new file mode 100644 index 0000000..3b654e8 --- /dev/null +++ b/samples/attachment2.h @@ -0,0 +1,120 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2021, 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_samples_attachment2 +#define headerfile_morm_samples_attachment2 + +#include +#include "morm.h" +#include "type.h" +#include "language.h" + + +namespace morm +{ +namespace samples +{ + +/* +CREATE TABLE public.attachment2 ( + id bigserial, + person_id bigint, + name varchar(64), + content text, + some_flags bool, + created_date timestamp with time zone, + language_id bigint, + + primary key(id) +); +*/ + +// copied from Attachment and changed the table name +class Attachment2 : public morm::Model +{ +public: + + long id; + long person_id; + std::wstring name; + std::string content; + std::vector types; + bool some_flags; + PT::Date created_date; + Language language; + + + void map_fields() + { + field(L"id", id, false, false, true); + field(L"person_id", person_id); + field(L"name", name); + field(L"content", content); + field(L"attachment_id", L"types", types); + field(L"some_flags", some_flags); + field(L"created_date", created_date); + field(L"language_id", L"language", language); + } + + void table_name(PT::TextStream & stream) + { + // schema.table_name or just table_name + stream << "public.attachment2"; + } + + void after_select() + { + if( has_primary_key_set ) + { + morm::Finder finder(model_connector); + types = finder.select().where().eq(L"attachment_id", id).get_vector(); + } + } + + void after_insert() + { + get_last_sequence_for_primary_key(L"public.attachment2_id_seq", id); + } + + +}; + + + +} +} + + +#endif + diff --git a/samples/language.h b/samples/language.h index 4fa2088..fa60363 100644 --- a/samples/language.h +++ b/samples/language.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2019, Tomasz Sowa + * Copyright (c) 2019-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -86,7 +86,7 @@ public: void after_insert() { - get_last_sequence(L"public.language_id_seq", id); + get_last_sequence_for_primary_key(L"public.language_id_seq", id); } diff --git a/samples/person.h b/samples/person.h index f906d59..d70fb03 100644 --- a/samples/person.h +++ b/samples/person.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2019, Tomasz Sowa + * Copyright (c) 2019-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -39,6 +39,7 @@ #include "morm.h" #include "language.h" #include "attachment.h" +#include "attachment2.h" @@ -64,29 +65,31 @@ class Person : public morm::Model public: long id; + std::wstring first_name; std::wstring last_name; std::wstring email; Language language; std::list attachments; - Attachment attachment; + Attachment2 attachment2; void map_fields() { field(L"id", id, false, false, true); - //field(L"id", id, f::no_insertable | f::no_updatable | f::primary_key); + field(L"language_id", L"language", language); field(L"first_name", first_name); field(L"last_name", last_name); field(L"email", email); - field(L"language_id", L"language", language); field(L"person_id", L"attachments", attachments); - field(L"person_id", L"attachment", attachment, true, true, false); + field(L"person_id", L"attachment2", attachment2, true, true, false); + + //field(L"id", id, f::no_insertable | f::no_updatable | f::primary_key); //field(L"person_id", attachment, f::insertable | f::updatable | f::foreign_key); //field(L"person_id", attachment, f::insertable, f::updatable, f::foreign_key); } @@ -99,13 +102,19 @@ public: void after_select() { - morm::Finder finder(model_connector); - attachments = finder.select().where().eq(L"person_id", id).get_list(); + if( has_primary_key_set ) + { + morm::Finder finder(model_connector); + attachments = finder.select().where().eq(L"person_id", id).get_list(); + + morm::Finder finder2(model_connector); + attachment2 = finder2.select().where().eq(L"person_id", id).get(); + } } void after_insert() { - get_last_sequence(L"public.person_id_seq", id); + get_last_sequence_for_primary_key(L"public.person_id_seq", id); } diff --git a/samples/sample01.h b/samples/sample01.h index 0558729..87f6db3 100644 --- a/samples/sample01.h +++ b/samples/sample01.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2019, Tomasz Sowa + * Copyright (c) 2019-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -51,23 +51,37 @@ public: void make() { + // delete all datas + // delete from attachment; delete from attachment2; delete from language; delete from person; delete from types; + + // select * from person; select * from attachment; select * from attachment2 ; select * from types; select * from language; + + std::string str; + + person.set_connector(model_connector); load_defaults(person); - std::wstring sss = L"cosik wstawiony dynamicznie"; - person.set_field_value_generic(L"email", L"email", sss); + //std::wstring sss = L"cosik wstawiony dynamicznie"; + //person.set_field_value_generic(L"email", L"email", sss); + + //person.insert(); //person.update(); - person.save(); + //person.save(); //person.remove(); + //person.to_text(str, true, true); morm::Finder finder(model_connector); + Person p = finder.select().where().eq(L"id", 191).get(); + p.to_text(str, true, true); + + //std::list plist = finder.use_table_prefix(false).select().where().eq(L"id", 120).get_list(); - //Person p = finder.use_table_prefix(false).select().where().eq(L"id", 120).get(); // Person p = finder.prepare_to_select().use_table_prefix(true).raw("select person.id as \"person.id\", person.first_name as \"person.first_name\", person.last_name as \"person.last_name\", person.email as \"person.email\", " // "language.id as \"language.id\", language.english_name as \"language.english_name\", language.local_name as \"language.local_name\", language.code_str as \"language.code_str\", language.code_int as \"language.code_int\", " @@ -79,14 +93,13 @@ void make() // "LEFT JOIN public.language AS language2 ON attachment.language_id = language2.id " // "where person.id=120").get(); - std::cout << "--------------------------------" << std::endl; + //std::cout << "--------------------------------" << std::endl; //p.remove(); //std::cout << "--------------------------------" << std::endl; - std::string str; //str += "--------\n"; - person.to_text(str, true, true); + // for(Person & person : plist) // { @@ -112,6 +125,7 @@ private: person.first_name = L"MyFirstName"; person.last_name = L"MyLastName"; person.email = L"myemail@mydomain.ltd"; + //person.set_save_mode(Model::DO_NOTHING_ON_SAVE); person.set_save_mode(Model::DO_INSERT_ON_SAVE); //person.set_save_mode(Model::DO_UPDATE_ON_SAVE); @@ -120,29 +134,38 @@ private: person.language.local_name = L"polish"; person.language.code_str = L"en"; person.language.code_int = 200; + //person.language.set_save_mode(Model::DO_NOTHING_ON_SAVE); person.language.set_save_mode(Model::DO_INSERT_ON_SAVE); //person.language.set_save_mode(Model::DO_UPDATE_ON_SAVE); std::time_t t = std::time(0); -// person.attachment.id = 40; - person.attachment.person_id = person.id; - person.attachment.created_date.FromTime(t); - person.attachment.name = L"attachment name"; - person.attachment.content = "long binary content"; - person.attachment.some_flags = true; - person.attachment.set_save_mode(Model::DO_INSERT_ON_SAVE); -// //person.attachment.set_save_mode(Model::DO_UPDATE_ON_SAVE); -// - person.attachment.language.id = 86; - person.attachment.language.english_name = L"attachment language"; - person.attachment.language.local_name = L"attachment local name"; - person.attachment.language.code_str = L"loen"; - person.attachment.language.code_int = 300; -// person.attachment.language.set_save_mode(Model::DO_UPDATE_ON_SAVE); - Type type; type.id = 0; + type.set_save_mode(Model::DO_INSERT_ON_SAVE); + + person.attachment2.id = 40; + person.attachment2.person_id = person.id; + person.attachment2.created_date.FromTime(t); + person.attachment2.name = L"attachment name"; + person.attachment2.content = "long binary content"; + person.attachment2.some_flags = true; + person.attachment2.set_save_mode(Model::DO_INSERT_ON_SAVE); +// //person.attachment.set_save_mode(Model::DO_UPDATE_ON_SAVE); + + person.attachment2.language.id = 86; + person.attachment2.language.english_name = L"attachment language"; + person.attachment2.language.local_name = L"attachment local name"; + person.attachment2.language.code_str = L"loen"; + person.attachment2.language.code_int = 300; + person.attachment2.language.set_save_mode(Model::DO_INSERT_ON_SAVE); + person.attachment2.language.set_has_primary_key_set(true); + + type.name = L"abcde - fghi"; + person.attachment2.types.push_back(type); + + type.name = L"second type"; + person.attachment2.types.push_back(type); Attachment attachment; attachment.id = 0; @@ -186,7 +209,6 @@ private: type.name = L"Typ dla attachment 3 - 3"; person.attachments.back().types.push_back(type); - //type.name = L"Typik"; //person.attachment.types.push_back(type); diff --git a/samples/type.h b/samples/type.h index 2fb43f7..05ec7fd 100644 --- a/samples/type.h +++ b/samples/type.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2019, Tomasz Sowa + * Copyright (c) 2019-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -79,7 +79,7 @@ public: void after_insert() { - get_last_sequence(L"public.types_id_seq", id); + get_last_sequence_for_primary_key(L"public.types_id_seq", id); } diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index 475cd20..59e4931 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -118,6 +118,7 @@ void BaseExpression::dump_additional_info(Model & model) if( model.model_env && model.model_env->dump_mode ) { field(L"model_save_mode", model.save_mode, false, false, false, model.model_env); + field(L"has_primary_key_set", model.has_primary_key_set, false, false, false, model.model_env); } } diff --git a/src/baseexpression.h b/src/baseexpression.h index 66372cd..05c06e4 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -91,7 +91,7 @@ public: else if( work_mode == MORM_WORK_MODE_MODEL_VALUES ) { - put_field_value(field_value); + put_field_value_or_null(field_value, is_primary_key, model_env); } else if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES ) @@ -102,7 +102,7 @@ public: { put_field_name((*model_env->set_field_name_helper)[model_env->field_index], model_env); put_name_value_separator(); - put_field_value(field_value); + put_field_value_or_null(field_value, is_primary_key, model_env); } model_env->field_index += 1; @@ -111,7 +111,7 @@ public: { put_field_name(field_name, model_env); put_name_value_separator(); - put_field_value(field_value); + put_field_value_or_null(field_value, is_primary_key, model_env); } } @@ -121,6 +121,22 @@ public: } + template + void put_field_value_or_null(const FieldValue & field_value, bool is_primary_key, ModelEnv * model_env) + { + if( is_primary_key ) + { + if( model_env && model_env->has_primary_key_set ) + put_field_value(field_value); + else + put_null_value(); + } + else + { + put_field_value(field_value); + } + } + template void field_in(PT::TextStream & stream, const wchar_t * field_name, const std::set & container, ModelEnv * model_env) { @@ -282,6 +298,12 @@ protected: } + virtual void put_null_value() + { + (*out_stream) << "null"; + } + + virtual void before_field_value_list() { } diff --git a/src/dbconnector.h b/src/dbconnector.h index 7e2753d..4c1b8f0 100644 --- a/src/dbconnector.h +++ b/src/dbconnector.h @@ -110,7 +110,7 @@ public: template - void get_last_sequence(const wchar_t * sequence_table_name, FieldValue & field_value) + bool get_last_sequence(const wchar_t * sequence_table_name, FieldValue & field_value) { const char * val_str = query_last_sequence(sequence_table_name); @@ -122,7 +122,11 @@ public: { (*log) << PT::Log::log3 << "Morm: sequence value: " << field_value << PT::Log::logend; } + + return true; } + + return false; } diff --git a/src/jsonexpression.cpp b/src/jsonexpression.cpp index e9ef3e9..692693e 100644 --- a/src/jsonexpression.cpp +++ b/src/jsonexpression.cpp @@ -105,8 +105,6 @@ void JSONExpression::after_second_part_long_field_name() } - - void JSONExpression::before_field_value_string() { (*out_stream) << "\""; diff --git a/src/model.cpp b/src/model.cpp index 8c4b63f..ce588fc 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -43,6 +43,7 @@ Model::Model() model_connector = nullptr; model_env = nullptr; save_mode = DO_INSERT_ON_SAVE; + has_primary_key_set = false; } @@ -50,7 +51,8 @@ Model::Model(const Model & m) { model_connector = m.model_connector; save_mode = m.save_mode; - model_env = m.model_env; // or just set to null? + model_env = nullptr; + has_primary_key_set = m.has_primary_key_set; } @@ -71,6 +73,19 @@ Model::SaveMode Model::get_save_mode() } +void Model::set_has_primary_key_set(bool has_primary_key) +{ + this->has_primary_key_set = has_primary_key; +} + + +bool Model::get_has_primary_key_set() +{ + return this->has_primary_key_set; +} + + + void Model::mark_to_delete() { save_mode = DO_DELETE_ON_SAVE; @@ -140,8 +155,10 @@ void Model::to_text(PT::TextStream & stream, ModelData * model_data, bool clear_ ModelEnv model_env_local; 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->model_data = model_data; if( model_connector ) { @@ -149,13 +166,18 @@ void Model::to_text(PT::TextStream & stream, ModelData * model_data, bool clear_ if( flat_connector ) { - model_env->model_data = model_data; - flat_connector->to_text(stream, *this); - model_env->model_data = nullptr; + try + { + flat_connector->to_text(stream, *this); + } + catch(...) + { + model_env = nullptr; + throw; + } } } - // what about if an exception was thrown? this pointer will not be null model_env = nullptr; } @@ -221,6 +243,7 @@ void Model::generate_insert_query(PT::TextStream & stream, ModelData * model_dat { ModelEnv model_env_local; model_env = &model_env_local; + model_env->has_primary_key_set = has_primary_key_set; model_env->model_data = model_data; model_env->model_work_mode = MORM_MODEL_WORK_MODE_GENERATING_DB_SQL; @@ -230,11 +253,18 @@ void Model::generate_insert_query(PT::TextStream & stream, ModelData * model_dat if( db_connector ) { - db_connector->generate_insert_query(stream, *this); + try + { + db_connector->generate_insert_query(stream, *this); + } + catch(...) + { + model_env = nullptr; + throw; + } } } - // what about if an exception was thrown? this pointer will not be null model_env = nullptr; } @@ -257,11 +287,19 @@ bool Model::insert(ModelData * model_data, bool insert_whole_tree) model_env = &model_env_local; model_env->model_data = model_data; - bool status = insert_tree(insert_whole_tree); + bool status = false; + + try + { + status = insert_tree(insert_whole_tree); + } + catch(...) + { + model_env = nullptr; + throw; + } - // what about if an exception was thrown? this pointer will not be null model_env = nullptr; - return status; } @@ -271,6 +309,8 @@ bool Model::insert(ModelData * model_data, bool insert_whole_tree) bool Model::insert_tree(bool insert_whole_tree) { bool result = false; + has_primary_key_set = false; // the key will be overwritten (the database will create a new key) + model_env->has_primary_key_set = false; if( insert_whole_tree ) { @@ -296,9 +336,21 @@ bool Model::insert_tree(bool insert_whole_tree) if( result ) { - save_mode = DO_UPDATE_ON_SAVE; // IMPROVE ME check if there is a primary key + /* + * after_insert() should read the new primary key and set has_primary_key_set flag if the key was read correctly + */ after_insert(); - set_parent_key_in_childs(); + model_env->has_primary_key_set = has_primary_key_set; + + if( has_primary_key_set ) + { + save_mode = DO_UPDATE_ON_SAVE; + set_parent_key_in_childs(); // may it would be better to set it even if we do not have a primary key? set it to zero or something? + } + else + { + save_mode = DO_NOTHING_ON_SAVE; + } } else { @@ -323,6 +375,7 @@ void Model::generate_update_query(PT::TextStream & stream, ModelData * model_dat { ModelEnv model_env_local; model_env = &model_env_local; + model_env->has_primary_key_set = has_primary_key_set; model_env->model_data = model_data; model_env->model_work_mode = MORM_MODEL_WORK_MODE_GENERATING_DB_SQL; @@ -359,11 +412,19 @@ bool Model::update(ModelData * model_data, bool update_whole_tree) model_env = &model_env_local; model_env->model_data = model_data; - bool status = update_tree(update_whole_tree); + bool status = false; + + try + { + status = update_tree(update_whole_tree); + } + catch(...) + { + model_env = nullptr; + throw; + } - // what about if an exception was thrown? this pointer will not be null model_env = nullptr; - return status; } @@ -372,6 +433,13 @@ bool Model::update(ModelData * model_data, bool update_whole_tree) bool Model::update_tree(bool update_whole_tree) { bool result = false; + model_env->has_primary_key_set = has_primary_key_set; + + if( !has_primary_key_set ) + { + put_to_log(L"Morm: call update but model doesn't have a primary key set"); + return result; + } if( update_whole_tree ) { @@ -416,6 +484,7 @@ void Model::generate_remove_query(PT::TextStream & stream, ModelData * model_dat { ModelEnv model_env_local; model_env = &model_env_local; + model_env->has_primary_key_set = has_primary_key_set; model_env->model_data = model_data; model_env->model_work_mode = MORM_MODEL_WORK_MODE_GENERATING_DB_SQL; @@ -453,11 +522,19 @@ bool Model::remove(ModelData * model_data, bool remove_whole_tree) model_env = &model_env_local; model_env->model_data = model_data; - bool status = remove_tree(remove_whole_tree); + bool status = false; + + try + { + status = remove_tree(remove_whole_tree); + } + catch(...) + { + model_env = nullptr; + throw; + } - // what about if an exception was thrown? this pointer will not be null model_env = nullptr; - return status; } @@ -466,6 +543,13 @@ bool Model::remove(ModelData * model_data, bool remove_whole_tree) bool Model::remove_tree(bool remove_whole_tree) { bool result = false; + model_env->has_primary_key_set = has_primary_key_set; + + if( !has_primary_key_set ) + { + put_to_log(L"Morm: call remove but model doesn't have a primary key set"); + return result; + } if( remove_whole_tree ) { @@ -490,7 +574,9 @@ bool Model::remove_tree(bool remove_whole_tree) if( result ) { - save_mode = DO_INSERT_ON_SAVE; // CHECKME may it would be better to set DO_NOTHING_ON_SAVE? + save_mode = DO_NOTHING_ON_SAVE; + has_primary_key_set = false; + model_env->has_primary_key_set = false; after_remove(); } else @@ -531,11 +617,19 @@ bool Model::save(ModelData * model_data, bool save_whole_tree) model_env = &model_env_local; model_env->model_data = model_data; - bool status = save_tree(save_whole_tree); + bool status = false; + + try + { + status = save_tree(save_whole_tree); + } + catch(...) + { + model_env = nullptr; + throw; + } - // what about if an exception was thrown? this pointer will not be null model_env = nullptr; - return status; } @@ -544,6 +638,7 @@ bool Model::save(ModelData * model_data, bool save_whole_tree) bool Model::save_tree(bool save_whole_tree) { bool result = false; + model_env->has_primary_key_set = has_primary_key_set; if( save_whole_tree ) { @@ -617,17 +712,20 @@ void Model::map_values_from_query() if( model_env ) { model_env->model_work_mode = MORM_MODEL_WORK_MODE_READING_VALUE_FROM_DB_RESULTSET; - model_env->all_fields_are_null = true; + model_env->was_primary_key_read = false; // whether or not there was at least one column with primary_key flag + model_env->has_primary_key_set = true; // whether all primary_columns were different than null map_fields(); model_env->model_work_mode = MORM_MODEL_WORK_MODE_NONE; - if( model_env->all_fields_are_null ) + if( model_env->was_primary_key_read && model_env->has_primary_key_set ) { - save_mode = DO_NOTHING_ON_SAVE; + has_primary_key_set = true; + save_mode = DO_UPDATE_ON_SAVE; } else { - save_mode = DO_UPDATE_ON_SAVE; + has_primary_key_set = false; + save_mode = DO_NOTHING_ON_SAVE; } } } @@ -640,10 +738,20 @@ void Model::clear() model_env = &model_env_local; model_env->model_work_mode = MORM_MODEL_WORK_MODE_CLEARING_VALUE; - map_fields(); - model_env = nullptr; + try + { + map_fields(); + } + catch(...) + { + model_env = nullptr; + throw; + } + + model_env = nullptr; save_mode = DO_INSERT_ON_SAVE; + has_primary_key_set = false; } @@ -794,6 +902,20 @@ void Model::put_table_name_with_index(PT::TextStream & str) } +void Model::put_to_log(const wchar_t * str) +{ + if( model_connector ) + { + PT::Log * log = model_connector->get_logger(); + + if( log ) + { + (*log) << str << PT::Log::logend; + } + } +} + + void Model::put_fields_to_log(PT::Log & log, const wchar_t * db_field_name, const wchar_t * flat_field_name) { bool was_db_field_put = false; diff --git a/src/model.h b/src/model.h index 5da8e56..b8f3535 100644 --- a/src/model.h +++ b/src/model.h @@ -67,6 +67,9 @@ public: virtual void set_save_mode(SaveMode save_mode); virtual SaveMode get_save_mode(); + virtual void set_has_primary_key_set(bool has_primary_key); + virtual bool get_has_primary_key_set(); + virtual void mark_to_delete(); virtual void mark_to_remove(); @@ -176,8 +179,9 @@ public: protected: ModelConnector * model_connector; - SaveMode save_mode; ModelEnv * model_env; + SaveMode save_mode; + bool has_primary_key_set; @@ -568,12 +572,12 @@ protected: { if( model_env->cursor_helper && model_env->cursor_helper->has_autogenerated_select ) { - get_value_by_field_index(model_env->cursor_helper->current_column, field_value); + get_value_by_field_index(model_env->cursor_helper->current_column, field_value, is_primary_key); model_env->cursor_helper->current_column += 1; } else { - get_value_by_field_name(db_field_name, field_value); + get_value_by_field_name(db_field_name, field_value, is_primary_key); } } } @@ -596,6 +600,11 @@ protected: { if( model_connector && model_env ) { + if( is_primary_key ) + { + model_env->was_primary_key_read = true; + } + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_SET_FIELD_VALUE ) { field_generic_set_field_value(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); @@ -748,6 +757,7 @@ protected: { ModelEnv model_env_local; model_env_local.copy_global_objects(*model_env); + model_env_local.has_primary_key_set = field_model.has_primary_key_set; model_env_local.model_work_mode = MORM_MODEL_WORK_MODE_SET_FIELD_VALUE; model_env_local.field_value_helper_tab = &helper_tab; model_env_local.field_index = 0; @@ -875,32 +885,42 @@ protected: { if( db_expression->get_work_mode() == MORM_WORK_MODE_MODEL_FIELDS && db_expression->get_output_type() == MORM_OUTPUT_TYPE_DB_INSERT ) { - int not_used_object = 0; - db_expression->field(db_field_name, not_used_object, insertable, updatable, false, model_env); + if( insertable ) + { + int not_used_object = 0; + db_expression->field(db_field_name, not_used_object, insertable, updatable, false, model_env); + } } if( db_expression->get_work_mode() == MORM_WORK_MODE_MODEL_VALUES && db_expression->get_output_type() == MORM_OUTPUT_TYPE_DB_INSERT ) { - db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_INSERT_PRIMARY_KEY); - field_model.map_fields(); - db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_INSERT); + if( insertable ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_INSERT_PRIMARY_KEY); + field_model.map_fields(); + db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_INSERT); + } } if( db_expression->get_work_mode() == MORM_WORK_MODE_MODEL_FIELDS_VALUES && db_expression->get_output_type() == MORM_OUTPUT_TYPE_DB_UPDATE ) { - std::vector key_fields; - key_fields.push_back(db_field_name); // at the moment only one key - - db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE_PRIMARY_KEY); - field_model.model_env->field_index = 0; - field_model.model_env->set_field_name_helper = &key_fields; - field_model.map_fields(); - db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE); - - if( (size_t)field_model.model_env->field_index != key_fields.size() ) + if( updatable ) { - // number of keys are different - // put error log here + std::vector key_fields; + key_fields.push_back(db_field_name); // at the moment only one key + + db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE_PRIMARY_KEY); + field_model.model_env->field_index = 0; + field_model.model_env->set_field_name_helper = &key_fields; + field_model.map_fields(); + db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE); + + if( (size_t)field_model.model_env->field_index != key_fields.size() ) + { + // IMPROVEME + // number of keys are different + // put error log here + } } } } @@ -969,6 +989,7 @@ protected: model_env_local.copy_global_objects(*model_env); field_model.model_env = &model_env_local; + field_model.model_env->has_primary_key_set = field_model.has_primary_key_set; field_model.set_connector(model_connector); if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_SET_PARENT_ID ) @@ -1056,6 +1077,7 @@ protected: model_env_local.copy_global_objects(*model_env); child_model.model_env = &model_env_local; + child_model.model_env->has_primary_key_set = child_model.has_primary_key_set; child_model.set_connector(model_connector); if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_INSERT ) @@ -1159,7 +1181,7 @@ protected: template - void get_value_by_field_index(int field_index, FieldValue & field_value) + void get_value_by_field_index(int field_index, FieldValue & field_value, bool is_primary_key) { DbConnector * db_connector = model_connector->get_db_connector(); @@ -1167,7 +1189,6 @@ protected: { if( !model_env->cursor_helper->query_result->is_null(field_index) ) { - model_env->all_fields_are_null = false; const char * val_str = model_env->cursor_helper->query_result->get_field_string_value(field_index); if( val_str ) @@ -1175,12 +1196,19 @@ protected: db_connector->get_value(val_str, field_value); } } + else + { + if( is_primary_key ) + { + model_env->has_primary_key_set = false; + } + } } } template - void get_value_by_field_name(const wchar_t * field_name, FieldValue & field_value) + void get_value_by_field_name(const wchar_t * field_name, FieldValue & field_value, bool is_primary_key) { DbConnector * db_connector = model_connector->get_db_connector(); @@ -1208,7 +1236,6 @@ protected: if( column_index != -1 && !model_env->cursor_helper->query_result->is_null(column_index) ) { - model_env->all_fields_are_null = false; const char * val_str = model_env->cursor_helper->query_result->get_field_string_value(column_index); if( val_str ) @@ -1216,6 +1243,13 @@ protected: db_connector->get_value(val_str, field_value); } } + else + { + if( is_primary_key ) + { + model_env->has_primary_key_set = false; + } + } } } @@ -1234,7 +1268,7 @@ public: template - void get_last_sequence(const wchar_t * sequence_table_name, FieldValue & field_value) + bool get_last_sequence(const wchar_t * sequence_table_name, FieldValue & field_value) { if( model_connector ) { @@ -1242,9 +1276,19 @@ public: if( db_connector && !is_empty_field(sequence_table_name) ) { - db_connector->get_last_sequence(sequence_table_name, field_value); + return db_connector->get_last_sequence(sequence_table_name, field_value); } } + + return false; + } + + + template + bool get_last_sequence_for_primary_key(const wchar_t * sequence_table_name, FieldValue & field_value) + { + has_primary_key_set = get_last_sequence(sequence_table_name, field_value); + return has_primary_key_set; } @@ -1302,6 +1346,7 @@ protected: virtual void put_table_name_with_index(PT::TextStream & str); + virtual void put_to_log(const wchar_t * str); virtual void put_fields_to_log(PT::Log & log, const wchar_t * db_field_name, const wchar_t * flat_field_name); template friend class Finder; diff --git a/src/modelenv.h b/src/modelenv.h index be29b45..9cdca98 100644 --- a/src/modelenv.h +++ b/src/modelenv.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2019, Tomasz Sowa + * Copyright (c) 2019-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -63,7 +63,8 @@ public: PT::TextStream table_name_short; int table_index; int field_index; - bool all_fields_are_null; + bool was_primary_key_read; + bool has_primary_key_set; std::vector * set_field_name_helper; std::vector * field_value_helper_tab; @@ -74,7 +75,6 @@ public: clear(); } - ~ModelEnv() { } @@ -92,7 +92,8 @@ public: set_field_name_helper = e.set_field_name_helper; field_value_helper_tab = e.field_value_helper_tab; field_index = e.field_index; - all_fields_are_null = e.all_fields_are_null; + was_primary_key_read = e.was_primary_key_read; + has_primary_key_set = e.has_primary_key_set; // table_name and table_name_short don't have to bo copied } @@ -124,7 +125,8 @@ public: set_field_name_helper = nullptr; field_value_helper_tab = nullptr; field_index = 0; - all_fields_are_null = false; + was_primary_key_read = false; + has_primary_key_set = false; } diff --git a/src/postgresqlexpression.cpp b/src/postgresqlexpression.cpp index 4235383..ee70474 100644 --- a/src/postgresqlexpression.cpp +++ b/src/postgresqlexpression.cpp @@ -152,6 +152,12 @@ void PostgreSQLExpression::esc(char val, PT::TextStream & stream) } +void PostgreSQLExpression::esc(const PT::Date & date, PT::TextStream & stream) +{ + stream << date << "+00"; +} + + DbExpression & PostgreSQLExpression::page(PT::TextStream & stream, size_t page_number, size_t page_size) { stream << " offset " << page_number << " limit " << page_size << " "; diff --git a/src/postgresqlexpression.h b/src/postgresqlexpression.h index b26605b..6443e10 100644 --- a/src/postgresqlexpression.h +++ b/src/postgresqlexpression.h @@ -46,6 +46,8 @@ class PostgreSQLExpression : public DbExpression public: void esc(char val, PT::TextStream & stream); + void esc(const PT::Date & date, PT::TextStream & stream); + DbExpression & page(PT::TextStream & stream, size_t page_number, size_t page_size); From fcf1d28b185f61ff460c6f2c1aba7376abc5efbb Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Wed, 10 Mar 2021 16:20:11 +0100 Subject: [PATCH 02/27] added FT class which is used in Model::field() methods FT class has following types: enum FieldType { default_type = 0, primary_key = 1, foreign_key = 2, foreign_key_in_child = 4, no_insertable = 8, no_updatable = 16, no_fetchable = 32, /* not supported yet */ }; an object of FT class are now used in Model::field() methods instead of insertable/updatable/is_primary_key/... boolean flags changed the semantic of has_foreign_key (which was a bool) flag in child Models: now on Models and list/vector of Models you should use either FT::foreign_key or FT::foreign_key_in_child 1. FT::foreign_key means that field with this flag is a foreign key and is pointing to the child object (it was the case when has_foreign_key was equal to true beforehand) 2. FT::foreign_key_in child means that the foreign key is in the child object and is pointing to the parent object --- samples/Makefile.dep | 10 +- samples/attachment.h | 6 +- samples/attachment2.h | 8 +- samples/language.h | 2 +- samples/person.h | 18 +- samples/sample01.h | 2 +- samples/type.h | 2 +- src/Makefile.dep | 27 +- src/baseexpression.cpp | 6 +- src/baseexpression.h | 34 +-- src/dbexpression.cpp | 10 +- src/dbexpression.h | 4 +- src/finder.h | 12 +- src/ft.h | 138 +++++++++ src/model.cpp | 20 ++ src/model.h | 661 +++++++++++++++++++++-------------------- src/modelenv.h | 2 + 17 files changed, 571 insertions(+), 391 deletions(-) create mode 100644 src/ft.h diff --git a/samples/Makefile.dep b/samples/Makefile.dep index 3bdaf52..1155a2c 100644 --- a/samples/Makefile.dep +++ b/samples/Makefile.dep @@ -14,10 +14,10 @@ main.o: ../../morm/src/queryresult.h ../../morm/src/flatconnector.h main.o: ../../morm/src/dbexpression.h ../../morm/src/baseexpression.h main.o: ../../morm/src/modelenv.h ../../morm/src/modeldata.h main.o: ../../morm/src/cursorhelper.h ../../morm/src/finderhelper.h -main.o: ../../morm/src/fieldvaluehelper.h ../../morm/src/flatexpression.h -main.o: ../../morm/src/finder.h ../../pikotools/utf8/utf8.h -main.o: ../../morm/src/cursor.h ../../morm/src/jsonexpression.h -main.o: ../../morm/src/postgresqlexpression.h ../../morm/src/jsonconnector.h -main.o: ../../morm/src/postgresqlconnector.h +main.o: ../../morm/src/fieldvaluehelper.h ../../morm/src/ft.h +main.o: ../../morm/src/flatexpression.h ../../morm/src/finder.h +main.o: ../../pikotools/utf8/utf8.h ../../morm/src/cursor.h +main.o: ../../morm/src/jsonexpression.h ../../morm/src/postgresqlexpression.h +main.o: ../../morm/src/jsonconnector.h ../../morm/src/postgresqlconnector.h main.o: ../../morm/src/postgresqlqueryresult.h person.h language.h main.o: attachment.h type.h attachment2.h diff --git a/samples/attachment.h b/samples/attachment.h index 50bc389..4b2146d 100644 --- a/samples/attachment.h +++ b/samples/attachment.h @@ -77,14 +77,14 @@ public: void map_fields() { - field(L"id", id, false, false, true); + field(L"id", id, FT::no_insertable | FT::no_updatable | FT::primary_key); field(L"person_id", person_id); field(L"name", name); field(L"content", content); - field(L"attachment_id", L"types", types); + field(L"attachment_id", L"types", types, FT::foreign_key_in_child); field(L"some_flags", some_flags); field(L"created_date", created_date); - field(L"language_id", L"language", language); + field(L"language_id", L"language", language, FT::foreign_key); } void table_name(PT::TextStream & stream) diff --git a/samples/attachment2.h b/samples/attachment2.h index 3b654e8..e384d2f 100644 --- a/samples/attachment2.h +++ b/samples/attachment2.h @@ -77,14 +77,14 @@ public: void map_fields() { - field(L"id", id, false, false, true); - field(L"person_id", person_id); + field(L"id", id, FT::no_insertable | FT::no_updatable | FT::primary_key); + //field(L"person_id", person_id); field(L"name", name); field(L"content", content); - field(L"attachment_id", L"types", types); + field(L"attachment_id", L"types", types, FT::foreign_key_in_child); field(L"some_flags", some_flags); field(L"created_date", created_date); - field(L"language_id", L"language", language); + field(L"language_id", L"language", language, FT::foreign_key); } void table_name(PT::TextStream & stream) diff --git a/samples/language.h b/samples/language.h index fa60363..d1e0e3e 100644 --- a/samples/language.h +++ b/samples/language.h @@ -70,7 +70,7 @@ public: void map_fields() { - field(L"id", id, false, false, true); + field(L"id", id, FT::no_insertable | FT::no_updatable | FT::primary_key); field(L"english_name", english_name); field(L"local_name", local_name); field(L"code_str", code_str); diff --git a/samples/person.h b/samples/person.h index d70fb03..a0a5a88 100644 --- a/samples/person.h +++ b/samples/person.h @@ -77,29 +77,23 @@ public: void map_fields() { - field(L"id", id, false, false, true); - field(L"language_id", L"language", language); - + field(L"id", id, FT::no_insertable | FT::no_updatable | FT::primary_key); + field(L"language_id", L"language", language, FT::foreign_key); field(L"first_name", first_name); field(L"last_name", last_name); field(L"email", email); - - field(L"person_id", L"attachments", attachments); - - field(L"person_id", L"attachment2", attachment2, true, true, false); - - - //field(L"id", id, f::no_insertable | f::no_updatable | f::primary_key); - //field(L"person_id", attachment, f::insertable | f::updatable | f::foreign_key); - //field(L"person_id", attachment, f::insertable, f::updatable, f::foreign_key); + field(L"person_id", L"attachments", attachments, FT::foreign_key_in_child); + field(L"person_id", L"attachment2", attachment2, FT::foreign_key_in_child | FT::no_insertable | FT::no_updatable); } + void table_name(PT::TextStream & stream) { // schema.table_name or just table_name stream << "public.person"; } + void after_select() { if( has_primary_key_set ) diff --git a/samples/sample01.h b/samples/sample01.h index 87f6db3..a9d46a4 100644 --- a/samples/sample01.h +++ b/samples/sample01.h @@ -75,7 +75,7 @@ void make() morm::Finder finder(model_connector); - Person p = finder.select().where().eq(L"id", 191).get(); + Person p = finder.select().where().eq(L"id", 207).get(); p.to_text(str, true, true); diff --git a/samples/type.h b/samples/type.h index 05ec7fd..b34e50d 100644 --- a/samples/type.h +++ b/samples/type.h @@ -65,7 +65,7 @@ public: void map_fields() { - field(L"id", id, false, false, true); + field(L"id", id, FT::no_insertable | FT::no_updatable | FT::primary_key); field(L"attachment_id", attachment_id); field(L"name", name); } diff --git a/src/Makefile.dep b/src/Makefile.dep index c7b9f44..e0eb565 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep @@ -9,9 +9,9 @@ baseexpression.o: ../../pikotools/membuffer/membuffer.h baseexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h baseexpression.o: modeldata.h cursorhelper.h queryresult.h baseexpression.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h -baseexpression.o: finderhelper.h fieldvaluehelper.h model.h modelconnector.h -baseexpression.o: clearer.h dbconnector.h flatconnector.h dbexpression.h -baseexpression.o: flatexpression.h ../../pikotools/utf8/utf8.h +baseexpression.o: finderhelper.h fieldvaluehelper.h ft.h model.h +baseexpression.o: modelconnector.h clearer.h dbconnector.h flatconnector.h +baseexpression.o: dbexpression.h flatexpression.h ../../pikotools/utf8/utf8.h clearer.o: clearer.h ../../pikotools/date/date.h clearer.o: ../../pikotools/convert/inttostr.h model.h clearer.o: ../../pikotools/textstream/textstream.h @@ -21,7 +21,7 @@ clearer.o: ../../pikotools/textstream/types.h modelconnector.h dbconnector.h clearer.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h clearer.o: queryresult.h flatconnector.h dbexpression.h baseexpression.h clearer.o: morm_types.h modelenv.h modeldata.h cursorhelper.h finderhelper.h -clearer.o: fieldvaluehelper.h flatexpression.h +clearer.o: fieldvaluehelper.h ft.h flatexpression.h dbconnector.o: dbconnector.h ../../pikotools/textstream/textstream.h dbconnector.o: ../../pikotools/space/space.h dbconnector.o: ../../pikotools/textstream/types.h ../../pikotools/date/date.h @@ -30,7 +30,7 @@ dbconnector.o: ../../pikotools/membuffer/membuffer.h dbconnector.o: ../../pikotools/textstream/types.h ../../pikotools/log/log.h dbconnector.o: ../../pikotools/log/filelog.h queryresult.h dbexpression.h dbconnector.o: baseexpression.h morm_types.h modelenv.h modeldata.h -dbconnector.o: cursorhelper.h finderhelper.h fieldvaluehelper.h model.h +dbconnector.o: cursorhelper.h finderhelper.h fieldvaluehelper.h ft.h model.h dbconnector.o: modelconnector.h clearer.h flatconnector.h flatexpression.h dbconnector.o: ../../pikotools/utf8/utf8.h ../../pikotools/convert/convert.h dbconnector.o: ../../pikotools/convert/inttostr.h @@ -47,7 +47,7 @@ dbexpression.o: ../../pikotools/membuffer/membuffer.h dbexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h dbexpression.o: modeldata.h cursorhelper.h queryresult.h dbexpression.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h -dbexpression.o: finderhelper.h fieldvaluehelper.h +dbexpression.o: finderhelper.h fieldvaluehelper.h ft.h flatconnector.o: flatconnector.h ../../pikotools/textstream/textstream.h flatconnector.o: ../../pikotools/space/space.h flatconnector.o: ../../pikotools/textstream/types.h @@ -58,7 +58,7 @@ flatconnector.o: ../../pikotools/textstream/types.h flatexpression.h flatconnector.o: baseexpression.h morm_types.h modelenv.h modeldata.h flatconnector.o: cursorhelper.h queryresult.h ../../pikotools/log/log.h flatconnector.o: ../../pikotools/log/filelog.h finderhelper.h -flatconnector.o: fieldvaluehelper.h model.h modelconnector.h clearer.h +flatconnector.o: fieldvaluehelper.h ft.h model.h modelconnector.h clearer.h flatconnector.o: dbconnector.h dbexpression.h flatexpression.o: flatexpression.h baseexpression.h flatexpression.o: ../../pikotools/textstream/textstream.h @@ -70,7 +70,7 @@ flatexpression.o: ../../pikotools/membuffer/membuffer.h flatexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h flatexpression.o: modeldata.h cursorhelper.h queryresult.h flatexpression.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h -flatexpression.o: finderhelper.h fieldvaluehelper.h +flatexpression.o: finderhelper.h fieldvaluehelper.h ft.h jsonconnector.o: jsonconnector.h flatconnector.h jsonconnector.o: ../../pikotools/textstream/textstream.h jsonconnector.o: ../../pikotools/space/space.h @@ -82,7 +82,7 @@ jsonconnector.o: ../../pikotools/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/log/log.h ../../pikotools/log/filelog.h -jsonconnector.o: finderhelper.h fieldvaluehelper.h +jsonconnector.o: finderhelper.h fieldvaluehelper.h ft.h jsonexpression.o: jsonexpression.h flatexpression.h baseexpression.h jsonexpression.o: ../../pikotools/textstream/textstream.h jsonexpression.o: ../../pikotools/space/space.h @@ -93,7 +93,7 @@ jsonexpression.o: ../../pikotools/membuffer/membuffer.h jsonexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h jsonexpression.o: modeldata.h cursorhelper.h queryresult.h jsonexpression.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h -jsonexpression.o: finderhelper.h fieldvaluehelper.h +jsonexpression.o: finderhelper.h fieldvaluehelper.h ft.h model.o: model.h ../../pikotools/textstream/textstream.h model.o: ../../pikotools/space/space.h ../../pikotools/textstream/types.h model.o: ../../pikotools/date/date.h ../../pikotools/convert/inttostr.h @@ -102,7 +102,8 @@ model.o: ../../pikotools/textstream/types.h modelconnector.h clearer.h model.o: dbconnector.h ../../pikotools/log/log.h model.o: ../../pikotools/log/filelog.h queryresult.h flatconnector.h model.o: dbexpression.h baseexpression.h morm_types.h modelenv.h modeldata.h -model.o: cursorhelper.h finderhelper.h fieldvaluehelper.h flatexpression.h +model.o: cursorhelper.h finderhelper.h fieldvaluehelper.h ft.h +model.o: flatexpression.h modelconnector.o: modelconnector.h clearer.h ../../pikotools/date/date.h modelconnector.o: ../../pikotools/convert/inttostr.h dbconnector.h modelconnector.o: ../../pikotools/textstream/textstream.h @@ -125,7 +126,7 @@ postgresqlconnector.o: ../../pikotools/log/filelog.h queryresult.h postgresqlconnector.o: postgresqlqueryresult.h ../../pikotools/utf8/utf8.h postgresqlconnector.o: postgresqlexpression.h dbexpression.h baseexpression.h postgresqlconnector.o: morm_types.h modelenv.h modeldata.h cursorhelper.h -postgresqlconnector.o: finderhelper.h fieldvaluehelper.h +postgresqlconnector.o: finderhelper.h fieldvaluehelper.h ft.h postgresqlconnector.o: ../../pikotools/convert/strtoint.h postgresqlconnector.o: ../../pikotools/convert/text.h postgresqlconnector.o: ../../pikotools/convert/misc.h @@ -141,7 +142,7 @@ postgresqlexpression.o: ../../pikotools/textstream/types.h morm_types.h postgresqlexpression.o: modelenv.h modeldata.h cursorhelper.h queryresult.h postgresqlexpression.o: ../../pikotools/log/log.h postgresqlexpression.o: ../../pikotools/log/filelog.h finderhelper.h -postgresqlexpression.o: fieldvaluehelper.h +postgresqlexpression.o: fieldvaluehelper.h ft.h postgresqlqueryresult.o: postgresqlqueryresult.h queryresult.h postgresqlqueryresult.o: ../../pikotools/log/log.h postgresqlqueryresult.o: ../../pikotools/textstream/textstream.h diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index 59e4931..f26df0b 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -117,8 +117,8 @@ void BaseExpression::dump_additional_info(Model & model) { if( model.model_env && model.model_env->dump_mode ) { - field(L"model_save_mode", model.save_mode, false, false, false, model.model_env); - field(L"has_primary_key_set", model.has_primary_key_set, false, false, false, model.model_env); + field(L"model_save_mode", model.save_mode, FT::no_insertable | FT::no_updatable | FT::no_fetchable, model.model_env); + field(L"has_primary_key_set", model.has_primary_key_set, FT::no_insertable | FT::no_updatable | FT::no_fetchable, model.model_env); } } @@ -134,7 +134,7 @@ void BaseExpression::after_generate_from_model() } -bool BaseExpression::can_field_be_generated(bool insertable, bool updatable, bool is_primary_key) +bool BaseExpression::can_field_be_generated(FT) { return true; } diff --git a/src/baseexpression.h b/src/baseexpression.h index 05c06e4..2f6b23a 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -41,7 +41,7 @@ #include "date/date.h" #include "morm_types.h" #include "modelenv.h" - +#include "ft.h" namespace morm @@ -73,9 +73,9 @@ public: template - void field(const wchar_t * field_name, const FieldValue & field_value, bool insertable, bool updatable, bool is_primary_key, ModelEnv * model_env) + void field(const wchar_t * field_name, const FieldValue & field_value, FT field_type, ModelEnv * model_env) { - if( out_stream && can_field_be_generated(insertable, updatable, is_primary_key) ) + if( out_stream && can_field_be_generated(field_type) ) { field_before(); @@ -91,7 +91,7 @@ public: else if( work_mode == MORM_WORK_MODE_MODEL_VALUES ) { - put_field_value_or_null(field_value, is_primary_key, model_env); + put_field_value_or_null(field_value, field_type, model_env); } else if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES ) @@ -102,7 +102,7 @@ public: { put_field_name((*model_env->set_field_name_helper)[model_env->field_index], model_env); put_name_value_separator(); - put_field_value_or_null(field_value, is_primary_key, model_env); + put_field_value_or_null(field_value, field_type, model_env); } model_env->field_index += 1; @@ -111,7 +111,7 @@ public: { put_field_name(field_name, model_env); put_name_value_separator(); - put_field_value_or_null(field_value, is_primary_key, model_env); + put_field_value_or_null(field_value, field_type, model_env); } } @@ -122,9 +122,9 @@ public: template - void put_field_value_or_null(const FieldValue & field_value, bool is_primary_key, ModelEnv * model_env) + void put_field_value_or_null(const FieldValue & field_value, FT field_type, ModelEnv * model_env) { - if( is_primary_key ) + if( field_type.is_primary_key() ) { if( model_env && model_env->has_primary_key_set ) put_field_value(field_value); @@ -159,10 +159,9 @@ public: template - void field_list(const wchar_t * field_name, ModelContainer & field_value, bool insertable, bool updatable, bool is_primary_key, - ModelConnector * model_connector, ModelEnv * model_env) + void field_list(const wchar_t * field_name, ModelContainer & field_value, FT field_type, ModelConnector * model_connector, ModelEnv * model_env) { - if( out_stream && can_field_be_generated(insertable, updatable, is_primary_key) ) + if( out_stream && can_field_be_generated(field_type) ) { field_before(); @@ -183,9 +182,9 @@ public: } template - void field_model(const wchar_t * field_name, ModelClass & field_model, bool insertable, bool updatable, bool is_primary_key, ModelEnv * model_env) + void field_model(const wchar_t * field_name, ModelClass & field_model, FT field_type, ModelEnv * model_env) { - if( out_stream && can_field_be_generated(insertable, updatable, is_primary_key) ) + if( out_stream && can_field_be_generated(field_type) ) { field_before(); @@ -211,11 +210,10 @@ public: } template - void field_to_stream(PT::TextStream & stream, const wchar_t * field_name, const FieldValue & field_value, bool insertable, bool updatable, bool is_primary_key, - ModelEnv * model_env) + void field_to_stream(PT::TextStream & stream, const wchar_t * field_name, const FieldValue & field_value, FT field_type, ModelEnv * model_env) { this->out_stream = &stream; - field(field_name, field_value, insertable, updatable, is_primary_key, model_env); + field(field_name, field_value, field_type, model_env); this->out_stream = nullptr; } @@ -274,12 +272,10 @@ protected: virtual void before_generate_from_model(); virtual void after_generate_from_model(); - virtual bool can_field_be_generated(bool insertable, bool updatable, bool is_primary_key); + virtual bool can_field_be_generated(FT); virtual void field_before(); virtual void field_after(); - //void field(const wchar_t * field_name, Model & field, bool insertable = true, bool updatable = true); - virtual void put_field_name(const wchar_t * field_name, ModelEnv * model_env); virtual void save_foreign_key(const wchar_t * field_name, ModelEnv * model_env); diff --git a/src/dbexpression.cpp b/src/dbexpression.cpp index d496f2d..0036359 100644 --- a/src/dbexpression.cpp +++ b/src/dbexpression.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018-2019, Tomasz Sowa + * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -62,23 +62,23 @@ int DbExpression::get_output_type() } -bool DbExpression::can_field_be_generated(bool insertable, bool updatable, bool is_primary_key) +bool DbExpression::can_field_be_generated(FT field_type) { if( output_type == MORM_OUTPUT_TYPE_DB_INSERT ) { - return insertable; + return field_type.is_insertable(); } else if( output_type == MORM_OUTPUT_TYPE_DB_UPDATE ) { - return updatable; + return field_type.is_updatable(); } else if( output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY || output_type == MORM_OUTPUT_TYPE_JOIN_TABLES || output_type == MORM_OUTPUT_TYPE_DB_INSERT_PRIMARY_KEY ) { - return is_primary_key; + return field_type.is_primary_key(); } else { diff --git a/src/dbexpression.h b/src/dbexpression.h index 53d2dba..ab6d549 100644 --- a/src/dbexpression.h +++ b/src/dbexpression.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018, Tomasz Sowa + * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -86,7 +86,7 @@ protected: std::vector conjunctions; - bool can_field_be_generated(bool insertable, bool updatable, bool is_primary_key); + bool can_field_be_generated(FT field_type); void field_before(); diff --git a/src/finder.h b/src/finder.h index 1bc7466..3407b9d 100644 --- a/src/finder.h +++ b/src/finder.h @@ -333,7 +333,7 @@ public: if( db_expression ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_EQ); - db_expression->field_to_stream(*out_stream, field_name, field_value, false, false, false, &model_env); + db_expression->field_to_stream(*out_stream, field_name, field_value, FT::default_type, &model_env); } return *this; @@ -346,7 +346,7 @@ public: if( db_expression ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_NOT_EQ); - db_expression->field_to_stream(*out_stream, field_name, field_value, false, false, false, &model_env); + db_expression->field_to_stream(*out_stream, field_name, field_value, FT::default_type, &model_env); } return *this; @@ -358,7 +358,7 @@ public: if( db_expression ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_LT); - db_expression->field_to_stream(*out_stream, field_name, field_value, false, false, false, &model_env); + db_expression->field_to_stream(*out_stream, field_name, field_value, FT::default_type, &model_env); } return *this; @@ -371,7 +371,7 @@ public: if( db_expression ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_GT); - db_expression->field_to_stream(*out_stream, field_name, field_value, false, false, false, &model_env); + db_expression->field_to_stream(*out_stream, field_name, field_value, FT::default_type, &model_env); } return *this; @@ -384,7 +384,7 @@ public: if( db_expression ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_LE); - db_expression->field_to_stream(*out_stream, field_name, field_value, false, false, false, &model_env); + db_expression->field_to_stream(*out_stream, field_name, field_value, FT::default_type, &model_env); } return *this; @@ -397,7 +397,7 @@ public: if( db_expression ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_GE); - db_expression->field_to_stream(*out_stream, field_name, field_value, false, false, false, &model_env); + db_expression->field_to_stream(*out_stream, field_name, field_value, FT::default_type, &model_env); } return *this; diff --git a/src/ft.h b/src/ft.h new file mode 100644 index 0000000..581f9d3 --- /dev/null +++ b/src/ft.h @@ -0,0 +1,138 @@ +/* + * This file is a part of morm + * and is distributed under the 2-Clause BSD licence. + * Author: Tomasz Sowa + */ + +/* + * Copyright (c) 2021, 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_ft +#define headerfile_morm_ft + +namespace morm +{ + + + +/* + * field types + */ +class FT +{ +public: + + enum FieldType + { + default_type = 0, + primary_key = 1, + foreign_key = 2, + foreign_key_in_child = 4, + no_insertable = 8, + no_updatable = 16, + no_fetchable = 32, /* not supported yet */ + }; + + /* + * type can be a superposition from FieldType values + */ + int type; + + + + FT() + { + type = 0; + } + + FT(const FT & field_type) + { + type = field_type.type; + } + + FT(FieldType type) + { + this->type = static_cast(type); + } + + FT(int type) + { + this->type = type; + } + + FT & operator=(const FT & field_type) + { + type = field_type.type; + return *this; + } + + bool is_flag_set(int flag_mask) const + { + return (type & flag_mask) != 0; + } + + + bool is_primary_key() const + { + return is_flag_set(primary_key); + } + + + bool is_foreign_key() const + { + return is_flag_set(foreign_key); + } + + bool is_foreign_key_in_child() const + { + return is_flag_set(foreign_key_in_child); + } + + bool is_insertable() const + { + return !is_flag_set(no_insertable); + } + + + bool is_updatable() const + { + return !is_flag_set(no_updatable); + } + + + bool is_fetchable() const + { + return !is_flag_set(no_fetchable); + } + +}; + +} + +#endif + diff --git a/src/model.cpp b/src/model.cpp index ce588fc..416f778 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -943,5 +943,25 @@ void Model::put_fields_to_log(PT::Log & log, const wchar_t * db_field_name, cons } +PT::TextStream Model::log_table_name() +{ + PT::TextStream buf; + table_name(buf); + + return buf; +} + + +PT::TextStream Model::log_table_name(const wchar_t * db_field_name) +{ + PT::TextStream buf; + table_name(buf); + buf << '.' << db_field_name; + + return buf; +} + + + } // namespace diff --git a/src/model.h b/src/model.h index b8f3535..8588c20 100644 --- a/src/model.h +++ b/src/model.h @@ -46,6 +46,7 @@ #include "dbexpression.h" #include "flatexpression.h" #include "modelenv.h" +#include "ft.h" namespace morm @@ -222,246 +223,220 @@ protected: * field methods for such field_values: signed char, wchar_t, char16_t, char32_t, std::u16string, std::u32string * */ - - void field(const wchar_t * field_name, char & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, char & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, unsigned char & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, unsigned char & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, std::wstring & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, std::wstring & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } -// void field(const wchar_t * field_name, wchar_t * field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) -// { -// field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); -// } - - void field(const wchar_t * field_name, std::string & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, std::string & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } -// void field(const wchar_t * field_name, char * field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) -// { -// field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); -// } - - void field(const wchar_t * field_name, bool & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, bool & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, short & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, short & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, unsigned short & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, unsigned short & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, int & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, int & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, unsigned int & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, unsigned int & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, long & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, long & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, unsigned long & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, unsigned long & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, long long & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, long long & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, unsigned long long & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, unsigned long long & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, float & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, float & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, double & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, double & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, long double & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, long double & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } -// void field(const wchar_t * field_name, void * field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) -// { -// field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); -// } - - void field(const wchar_t * field_name, PT::Date & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) + void field(const wchar_t * field_name, PT::Date & field_value, FT field_type = FT::default_type) { - field_generic(field_name, field_name, field_value, insertable, updatable, is_primary_key); + field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, Model & field_value, bool insertable = true, bool updatable = true, bool has_foreign_key = true) + void field(const wchar_t * field_name, Model & field_value, FT field_type = FT::default_type) { - field_model(field_name, field_name, field_value, insertable, updatable, has_foreign_key); + // has_foreign_key was here + field_model(field_name, field_name, field_value, field_type); } template - void field(const wchar_t * field_name, std::list & field_value, bool insertable = true, bool updatable = true) + void field(const wchar_t * field_name, std::list & field_value, FT field_type = FT::default_type) { ModelClass * list_model_null_pointer = nullptr; - field_list(field_name, field_name, field_value, list_model_null_pointer, insertable, updatable); + field_list(field_name, field_name, field_value, list_model_null_pointer, field_type); } template - void field(const wchar_t * field_name, std::vector & field_value, bool insertable = true, bool updatable = true) + void field(const wchar_t * field_name, std::vector & field_value, FT field_type = FT::default_type) { ModelClass * list_model_null_pointer = nullptr; - field_list(field_name, field_name, field_value, list_model_null_pointer, insertable, updatable); - } - -////////////////////// - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, char & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned char & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::wstring & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - -// void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, wchar_t * field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) -// { -// field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); -// } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::string & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - -// void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, char * field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) -// { -// field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); -// } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, bool & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, short & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned short & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, int & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned int & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, long & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned long & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, long long & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned long long & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, float & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, double & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, long double & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); - } - -// void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, void * field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) -// { -// field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); -// } - - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, PT::Date & field_value, bool insertable = true, bool updatable = true, bool is_primary_key = false) - { - field_generic(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); + field_list(field_name, field_name, field_value, list_model_null_pointer, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_value, bool insertable = true, bool updatable = true, bool has_foreign_key = true) + /* + * field methods which take two names: db_field_name and flat_field_name + */ + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, char & field_value, FT field_type = FT::default_type) { - field_model(db_field_name, flat_field_name, field_value, insertable, updatable, has_foreign_key); + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned char & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::wstring & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::string & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, bool & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, short & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned short & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, int & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned int & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, long & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned long & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, long long & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned long long & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, float & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, double & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, long double & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, PT::Date & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + + + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_value, FT field_type = FT::default_type) + { + // has_foreign_key was here + field_model(db_field_name, flat_field_name, field_value, field_type); } template - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::list & field_value, bool insertable = true, bool updatable = true) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::list & field_value, FT field_type = FT::default_type) { ModelClass * list_model_null_pointer = nullptr; - field_list(db_field_name, flat_field_name, field_value, list_model_null_pointer, insertable, updatable); + field_list(db_field_name, flat_field_name, field_value, list_model_null_pointer, field_type); } template - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::vector & field_value, bool insertable = true, bool updatable = true) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::vector & field_value, FT field_type = FT::default_type) { ModelClass * list_model_null_pointer = nullptr; - field_list(db_field_name, flat_field_name, field_value, list_model_null_pointer, insertable, updatable); + field_list(db_field_name, flat_field_name, field_value, list_model_null_pointer, field_type); } @@ -470,7 +445,7 @@ protected: template - void field_generic_set_field_value(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, bool insertable, bool updatable, bool is_primary_key) + void field_generic_set_field_value(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value) { if( model_env->field_value_helper_tab ) { @@ -510,9 +485,9 @@ protected: } template - void field_generic_iterate_primary_key_values(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, bool insertable, bool updatable, bool is_primary_key) + void field_generic_iterate_primary_key_values(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) { - if( is_primary_key ) + if( field_type.is_primary_key() ) { if( model_env->field_value_helper_tab ) { @@ -529,7 +504,7 @@ protected: } template - void field_generic_generate_flat_string(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, bool insertable, bool updatable, bool is_primary_key) + void field_generic_generate_flat_string(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) { FlatConnector * flat_connector = model_connector->get_flat_connector(); @@ -539,14 +514,13 @@ protected: if( flat_expression && !is_empty_field(flat_field_name) ) { - // insertable, updatable and is_primary_key are ignored here - flat_expression->field(flat_field_name, field_value, insertable, updatable, is_primary_key, model_env); + flat_expression->field(flat_field_name, field_value, field_type, model_env); } } } template - void field_generic_generate_db_sql(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, bool insertable, bool updatable, bool is_primary_key) + void field_generic_generate_db_sql(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) { DbConnector * db_connector = model_connector->get_db_connector(); @@ -556,13 +530,13 @@ protected: if( db_expression && !is_empty_field(db_field_name) ) { - db_expression->field(db_field_name, field_value, insertable, updatable, is_primary_key, model_env); + db_expression->field(db_field_name, field_value, field_type, model_env); } } } template - void field_generic_read_value_from_db_resultset(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, bool insertable, bool updatable, bool is_primary_key) + void field_generic_read_value_from_db_resultset(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) { DbConnector * db_connector = model_connector->get_db_connector(); @@ -572,19 +546,19 @@ protected: { if( model_env->cursor_helper && model_env->cursor_helper->has_autogenerated_select ) { - get_value_by_field_index(model_env->cursor_helper->current_column, field_value, is_primary_key); + get_value_by_field_index(model_env->cursor_helper->current_column, field_value, field_type); model_env->cursor_helper->current_column += 1; } else { - get_value_by_field_name(db_field_name, field_value, is_primary_key); + get_value_by_field_name(db_field_name, field_value, field_type); } } } } template - void field_generic_clear_value(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, bool insertable, bool updatable, bool is_primary_key) + void field_generic_clear_value(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) { Clearer * clearer = model_connector->get_clearer(); @@ -596,43 +570,43 @@ protected: template - void field_generic(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, bool insertable, bool updatable, bool is_primary_key) + void field_generic(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) { if( model_connector && model_env ) { - if( is_primary_key ) + if( field_type.is_primary_key() ) { model_env->was_primary_key_read = true; } if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_SET_FIELD_VALUE ) { - field_generic_set_field_value(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); + field_generic_set_field_value(db_field_name, flat_field_name, field_value); } if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_ITERATE_PRIMARY_KEY_VALUES ) { - field_generic_iterate_primary_key_values(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); + field_generic_iterate_primary_key_values(db_field_name, flat_field_name, field_value, field_type); } if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_FLAT_STRING ) { - field_generic_generate_flat_string(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); + field_generic_generate_flat_string(db_field_name, flat_field_name, field_value, field_type); } if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_DB_SQL ) { - field_generic_generate_db_sql(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); + field_generic_generate_db_sql(db_field_name, flat_field_name, field_value, field_type); } if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_READING_VALUE_FROM_DB_RESULTSET ) { - field_generic_read_value_from_db_resultset(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); + field_generic_read_value_from_db_resultset(db_field_name, flat_field_name, field_value, field_type); } if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_CLEARING_VALUE ) { - field_generic_clear_value(db_field_name, flat_field_name, field_value, insertable, updatable, is_primary_key); + field_generic_clear_value(db_field_name, flat_field_name, field_value, field_type); } } } @@ -643,7 +617,7 @@ protected: * this is only in a case when has_foreign_key is false, may it can be ignored? we can use unique index in such a case * */ - void field_model_left_join(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model, bool insertable, bool updatable, bool has_foreign_key, DbExpression * db_expression) + void field_model_left_join(const wchar_t * db_field_name, Model & field_model, FT field_type, DbExpression * db_expression) { if( model_env && field_model.model_env && model_env->finder_helper ) { @@ -662,7 +636,7 @@ protected: db_expression->set_output_type(MORM_OUTPUT_TYPE_JOIN_TABLES); db_expression->allow_to_use_prefix(false); - if( has_foreign_key ) + if( field_type.is_foreign_key() ) { field_model.map_fields(); @@ -773,9 +747,17 @@ protected: if( field_model.model_env->table_name.empty() ) field_model.table_name(field_model.model_env->table_name); - (*log) << PT::Log::log1 << "Morm: primary key in " << model_env->table_name << " consists of " << model_env->field_index << " column(s)" - << " but in " << field_model.model_env->table_name << " there are only " - << field_model.model_env->field_index << " matching column(s)" << PT::Log::logend; + if( field_model.model_env->field_index == 0 ) + { + (*log) << PT::Log::log1 << "Morm: there is no a foreign key in " << field_model.model_env->table_name + << " called " << db_field_name << " pointing to " << model_env->table_name << PT::Log::logend; + } + else + { + (*log) << PT::Log::log1 << "Morm: primary key in " << model_env->table_name << " consists of " << model_env->field_index << " column(s)" + << " but foreign key in " << field_model.model_env->table_name << " consists of " + << field_model.model_env->field_index << " column(s)" << PT::Log::logend; + } } field_model.model_env = nullptr; @@ -815,33 +797,30 @@ protected: void field_model_iterate_through_childs(const wchar_t * db_field_name, Model & field_model) { - if( !is_empty_field(db_field_name) ) + if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_INSERT ) { - if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_INSERT ) - { - field_model.insert_tree(true); - } + field_model.insert_tree(true); + } - if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_UPDATE ) - { - field_model.update_tree(true); - } + if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_UPDATE ) + { + field_model.update_tree(true); + } - if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_REMOVE ) - { - field_model.remove_tree(true); - } + if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_REMOVE ) + { + field_model.remove_tree(true); + } - if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_SAVE ) - { - field_model.save_tree(true); - } + if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_SAVE ) + { + field_model.save_tree(true); } } - void field_model_generate_flat_string(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model, bool insertable, bool updatable) + void field_model_generate_flat_string(const wchar_t * flat_field_name, Model & field_model, FT field_type) { FlatConnector * flat_connector = model_connector->get_flat_connector(); @@ -849,19 +828,19 @@ protected: { FlatExpression * flat_expression = flat_connector->get_expression(); - if( flat_expression && !is_empty_field(flat_field_name) ) + if( flat_expression ) { if( model_env->dump_mode || field_model.save_mode == DO_INSERT_ON_SAVE || field_model.save_mode == DO_UPDATE_ON_SAVE ) { field_model.model_env->model_work_mode = MORM_MODEL_WORK_MODE_GENERATING_FLAT_STRING; - flat_expression->field_model(flat_field_name, field_model, insertable, updatable, false, model_env); + flat_expression->field_model(flat_field_name, field_model, field_type, model_env); } } } } - void field_model_generate_db_sql(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model, bool insertable, bool updatable, bool has_foreign_key) + void field_model_generate_db_sql(const wchar_t * db_field_name, Model & field_model, FT field_type) { DbConnector * db_connector = model_connector->get_db_connector(); @@ -878,23 +857,23 @@ protected: if( db_expression->get_output_type() == MORM_OUTPUT_TYPE_SELECT_COLUMNS ) { - field_model_left_join(db_field_name, flat_field_name, field_model, insertable, updatable, has_foreign_key, db_expression); + field_model_left_join(db_field_name, field_model, field_type, db_expression); } - if( has_foreign_key ) + if( field_type.is_foreign_key() ) { if( db_expression->get_work_mode() == MORM_WORK_MODE_MODEL_FIELDS && db_expression->get_output_type() == MORM_OUTPUT_TYPE_DB_INSERT ) { - if( insertable ) + if( field_type.is_insertable() ) { int not_used_object = 0; - db_expression->field(db_field_name, not_used_object, insertable, updatable, false, model_env); + db_expression->field(db_field_name, not_used_object, field_type, model_env); } } if( db_expression->get_work_mode() == MORM_WORK_MODE_MODEL_VALUES && db_expression->get_output_type() == MORM_OUTPUT_TYPE_DB_INSERT ) { - if( insertable ) + if( field_type.is_insertable() ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_INSERT_PRIMARY_KEY); field_model.map_fields(); @@ -904,7 +883,7 @@ protected: if( db_expression->get_work_mode() == MORM_WORK_MODE_MODEL_FIELDS_VALUES && db_expression->get_output_type() == MORM_OUTPUT_TYPE_DB_UPDATE ) { - if( updatable ) + if( field_type.is_updatable() ) { std::vector key_fields; key_fields.push_back(db_field_name); // at the moment only one key @@ -939,7 +918,7 @@ protected: } - void field_model_clear_values(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model, bool insertable, bool updatable, bool has_foreign_key) + void field_model_clear_values(Model & field_model) { Clearer * clearer = model_connector->get_clearer(); @@ -950,38 +929,36 @@ protected: } - void field_model_read_values_from_queryresult(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model, bool insertable, bool updatable, bool has_foreign_key) + void field_model_read_values_from_queryresult(const wchar_t * db_field_name, Model & field_model, FT field_type) { DbConnector * db_connector = model_connector->get_db_connector(); if( db_connector ) { - if( !is_empty_field(db_field_name) ) + DbExpression * db_expression = db_connector->get_expression(); + + if( db_expression ) { - DbExpression * db_expression = db_connector->get_expression(); - - if( db_expression ) + if( model_env->cursor_helper && + !model_env->cursor_helper->has_autogenerated_select && + model_env->cursor_helper->use_table_prefix_for_fetching_values ) { - if( model_env->cursor_helper && - !model_env->cursor_helper->has_autogenerated_select && - model_env->cursor_helper->use_table_prefix_for_fetching_values ) - { - field_model.prepare_table_names(); - } + field_model.prepare_table_names(); + } - field_model.before_select(); - field_model.map_values_from_query(); + field_model.before_select(); + field_model.map_values_from_query(); - if( field_model.found() ) - { - field_model.after_select(); - } + if( field_model.found() ) + { + field_model.after_select(); } } } } - void field_model(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model, bool insertable, bool updatable, bool has_foreign_key) + + void field_model(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model, FT field_type) { if( model_connector && model_env ) { @@ -992,48 +969,35 @@ protected: field_model.model_env->has_primary_key_set = field_model.has_primary_key_set; field_model.set_connector(model_connector); - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_SET_PARENT_ID ) + if( !is_empty_field(db_field_name) ) { - if( !has_foreign_key ) + if( field_type.is_foreign_key() || field_type.is_foreign_key_in_child() ) { - field_model_set_parent_key(db_field_name, field_model); + field_model_for_db(db_field_name, field_model, field_type); + } + else + { + PT::Log * plog = model_connector->get_logger(); + + if( plog ) + { + (*plog) << "Morm: error in " << log_table_name(db_field_name) + << " field, you should set FT::is_foreign_key or FT::is_foreign_key_in_child flag for a model child object" << PT::Log::logend; + } } } - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITH_FOREIGN_KEY ) + if( !is_empty_field(flat_field_name) ) { - if( has_foreign_key ) + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_FLAT_STRING ) { - field_model_iterate_through_childs(db_field_name, field_model); + field_model_generate_flat_string(flat_field_name, field_model, field_type); } } - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITHOUT_FOREIGN_KEY ) - { - if( !has_foreign_key ) - { - field_model_iterate_through_childs(db_field_name, field_model); - } - } - - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_FLAT_STRING ) - { - field_model_generate_flat_string(db_field_name, flat_field_name, field_model, insertable, updatable); - } - - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_DB_SQL ) - { - field_model_generate_db_sql(db_field_name, flat_field_name, field_model, insertable, updatable, has_foreign_key); - } - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_CLEARING_VALUE ) { - field_model_clear_values(db_field_name, flat_field_name, field_model, insertable, updatable, has_foreign_key); - } - - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_READING_VALUE_FROM_DB_RESULTSET ) - { - field_model_read_values_from_queryresult(db_field_name, flat_field_name, field_model, insertable, updatable, has_foreign_key); + field_model_clear_values(field_model); } field_model.model_env = nullptr; @@ -1041,6 +1005,45 @@ protected: } + void field_model_for_db(const wchar_t * db_field_name, Model & field_model, FT field_type) + { + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_SET_PARENT_ID ) + { + if( field_type.is_foreign_key_in_child() ) + { + field_model_set_parent_key(db_field_name, field_model); + } + } + + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITH_FOREIGN_KEY ) + { + if( field_type.is_foreign_key() ) + { + field_model_iterate_through_childs(db_field_name, field_model); + } + } + + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITHOUT_FOREIGN_KEY ) + { + if( field_type.is_foreign_key_in_child() ) + { + field_model_iterate_through_childs(db_field_name, field_model); + } + } + + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_DB_SQL ) + { + field_model_generate_db_sql(db_field_name, field_model, field_type); + } + + + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_READING_VALUE_FROM_DB_RESULTSET ) + { + field_model_read_values_from_queryresult(db_field_name, field_model, field_type); + } + } + + template void field_list_set_parent_key(const wchar_t * db_field_name, ModelContainer & field_container, ModelContainerType * model_container_type) { @@ -1069,46 +1072,42 @@ protected: template void field_list_iterate_through_childs(const wchar_t * db_field_name, ModelContainer & field_container, ModelContainerType * model_container_type) { - if( !is_empty_field(db_field_name) ) + for(ModelContainerType & child_model : field_container) { - for(ModelContainerType & child_model : field_container) + ModelEnv model_env_local; + model_env_local.copy_global_objects(*model_env); + + child_model.model_env = &model_env_local; + child_model.model_env->has_primary_key_set = child_model.has_primary_key_set; + child_model.set_connector(model_connector); + + if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_INSERT ) { - ModelEnv model_env_local; - model_env_local.copy_global_objects(*model_env); - - child_model.model_env = &model_env_local; - child_model.model_env->has_primary_key_set = child_model.has_primary_key_set; - child_model.set_connector(model_connector); - - if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_INSERT ) - { - child_model.insert_tree(true); - } - - if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_UPDATE ) - { - child_model.update_tree(true); - } - - if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_REMOVE ) - { - child_model.remove_tree(true); - } - - if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_SAVE ) - { - child_model.save_tree(true); - } - - child_model.model_env = nullptr; + child_model.insert_tree(true); } - } + if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_UPDATE ) + { + child_model.update_tree(true); + } + + if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_REMOVE ) + { + child_model.remove_tree(true); + } + + if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_SAVE ) + { + child_model.save_tree(true); + } + + child_model.model_env = nullptr; + } } template - void field_list_generate_flat_string(const wchar_t * flat_field_name, ModelContainer & field_container, bool insertable, bool updatable) + void field_list_generate_flat_string(const wchar_t * flat_field_name, ModelContainer & field_container, FT field_type) { FlatConnector * flat_connector = model_connector->get_flat_connector(); @@ -1116,10 +1115,9 @@ protected: { FlatExpression * flat_expression = flat_connector->get_expression(); - if( flat_expression && !is_empty_field(flat_field_name) ) + if( flat_expression ) { - // insertable and updatable will be ignored there - flat_expression->field_list(flat_field_name, field_container, insertable, updatable, false, model_connector, model_env); + flat_expression->field_list(flat_field_name, field_container, field_type, model_connector, model_env); } } } @@ -1138,38 +1136,66 @@ protected: template - void field_list(const wchar_t * db_field_name, const wchar_t * flat_field_name, ModelContainer & field_container, ModelContainerType * model_container_type, bool insertable, bool updatable) + void field_list(const wchar_t * db_field_name, const wchar_t * flat_field_name, ModelContainer & field_container, ModelContainerType * model_container_type, FT field_type) { if( model_connector && model_env ) { + PT::Log * plog = model_connector->get_logger(); + if( !is_empty_field(db_field_name) ) { - if constexpr (std::is_base_of()) + /* + * IMPROVEME + * field_type.is_foreign_key() is not implemented for lists yet + * (in such a case parent will point only to one object (or none) so the list will consists of only one object (or none)) + */ + if( field_type.is_foreign_key() ) { - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_SET_PARENT_ID ) + if( plog ) { - field_list_set_parent_key(db_field_name, field_container, model_container_type); + (*plog) << "Morm: error: FT::is_foreign_key is not implemented for a list/vector yet" << PT::Log::logend; + return; } + } - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITHOUT_FOREIGN_KEY ) + if( field_type.is_foreign_key() || field_type.is_foreign_key_in_child() ) + { + if constexpr (std::is_base_of()) { - field_list_iterate_through_childs(db_field_name, field_container, model_container_type); + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_SET_PARENT_ID ) + { + field_list_set_parent_key(db_field_name, field_container, model_container_type); + } + + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITHOUT_FOREIGN_KEY ) + { + field_list_iterate_through_childs(db_field_name, field_container, model_container_type); + } + } + else + { + if( plog ) + { + (*plog) << "Morm: ignoring " << log_table_name(db_field_name) << " as this is not a container with Model objects" << PT::Log::logend; + } } } else { - PT::Log * plog = model_connector->get_logger(); - if( plog ) { - (*plog) << "Morm: ignoring " << db_field_name << " as this is not a container with Model objects" << PT::Log::logend; + (*plog) << "Morm: error in " << log_table_name(db_field_name) + << " field, you should set FT::is_foreign_key or FT::is_foreign_key_in_child flag for a list of child objects" << PT::Log::logend; } } } - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_FLAT_STRING ) + if( !is_empty_field(flat_field_name) ) { - field_list_generate_flat_string(flat_field_name, field_container, insertable, updatable); + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_FLAT_STRING ) + { + field_list_generate_flat_string(flat_field_name, field_container, field_type); + } } if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_CLEARING_VALUE ) @@ -1181,7 +1207,7 @@ protected: template - void get_value_by_field_index(int field_index, FieldValue & field_value, bool is_primary_key) + void get_value_by_field_index(int field_index, FieldValue & field_value, FT field_type) { DbConnector * db_connector = model_connector->get_db_connector(); @@ -1198,7 +1224,7 @@ protected: } else { - if( is_primary_key ) + if( field_type.is_primary_key() ) { model_env->has_primary_key_set = false; } @@ -1208,7 +1234,7 @@ protected: template - void get_value_by_field_name(const wchar_t * field_name, FieldValue & field_value, bool is_primary_key) + void get_value_by_field_name(const wchar_t * field_name, FieldValue & field_value, FT field_type) { DbConnector * db_connector = model_connector->get_db_connector(); @@ -1245,7 +1271,7 @@ protected: } else { - if( is_primary_key ) + if( field_type.is_primary_key() ) { model_env->has_primary_key_set = false; } @@ -1345,10 +1371,13 @@ protected: virtual void prepare_table_names(bool prepare_table_index = true); virtual void put_table_name_with_index(PT::TextStream & str); - virtual void put_to_log(const wchar_t * str); virtual void put_fields_to_log(PT::Log & log, const wchar_t * db_field_name, const wchar_t * flat_field_name); + PT::TextStream log_table_name(); + PT::TextStream log_table_name(const wchar_t * db_field_name); + + template friend class Finder; template friend class Cursor; friend class BaseExpression; diff --git a/src/modelenv.h b/src/modelenv.h index 9cdca98..8b5945d 100644 --- a/src/modelenv.h +++ b/src/modelenv.h @@ -46,6 +46,8 @@ namespace morm { + + class ModelEnv { public: From f7490594ad4f9ef5934c0626ebac34928399ad90 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 11 Mar 2021 12:22:37 +0100 Subject: [PATCH 03/27] changed the way how the table name is set in a Model - added prepare_table() method removed from Model: virtual void table_name(PT::TextStream & stream); added to Model: virtual void prepare_table(); virtual void table(const wchar_t * table_name); virtual void table(const wchar_t * schema_name, const wchar_t * table_name); --- samples/Makefile | 5 +- samples/attachment.h | 5 +- samples/attachment2.h | 7 +- samples/language.h | 6 +- samples/person.h | 5 +- samples/sample01.h | 9 ++- samples/type.h | 6 +- src/baseexpression.cpp | 23 +++---- src/baseexpression.h | 5 +- src/cursor.h | 4 +- src/dbconnector.cpp | 15 +++-- src/dbexpression.cpp | 33 --------- src/dbexpression.h | 2 - src/finder.h | 6 +- src/finderhelper.h | 8 +-- src/model.cpp | 149 ++++++++++++++++++++++++++++------------- src/model.h | 67 ++++++++---------- src/modelenv.h | 20 ++++-- 18 files changed, 193 insertions(+), 182 deletions(-) diff --git a/samples/Makefile b/samples/Makefile index f4336cd..fdbcbd1 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -27,11 +27,12 @@ current_path := $(shell pwd) global_relative_working_dir := $(shell relative_path $(current_path) $(GLOBAL_WORKING_DIR)) - +# IMPROVE ME +# add dependency to pikotools all: morm $(name) -$(name): $(o) +$(name): morm $(o) $(CXX) -o $(name) $(CXXFLAGS) $(LDFLAGS) $(o) $(GLOBAL_WORKING_DIR)/morm/src/morm.a $(GLOBAL_WORKING_DIR)/pikotools/log/log.a $(GLOBAL_WORKING_DIR)/pikotools/space/space.a $(GLOBAL_WORKING_DIR)/pikotools/mainspaceparser/mainspaceparser.a $(GLOBAL_WORKING_DIR)/pikotools/date/date.a $(GLOBAL_WORKING_DIR)/pikotools/convert/convert.a $(GLOBAL_WORKING_DIR)/pikotools/utf8/utf8.a $(LDFLAGS) -lpq -lpthread diff --git a/samples/attachment.h b/samples/attachment.h index 4b2146d..a247ab1 100644 --- a/samples/attachment.h +++ b/samples/attachment.h @@ -87,10 +87,9 @@ public: field(L"language_id", L"language", language, FT::foreign_key); } - void table_name(PT::TextStream & stream) + void prepare_table() { - // schema.table_name or just table_name - stream << "public.attachment"; + table(L"public", L"attachment"); } void after_select() diff --git a/samples/attachment2.h b/samples/attachment2.h index e384d2f..2548060 100644 --- a/samples/attachment2.h +++ b/samples/attachment2.h @@ -78,7 +78,7 @@ public: void map_fields() { field(L"id", id, FT::no_insertable | FT::no_updatable | FT::primary_key); - //field(L"person_id", person_id); + field(L"person_id", person_id); field(L"name", name); field(L"content", content); field(L"attachment_id", L"types", types, FT::foreign_key_in_child); @@ -87,10 +87,9 @@ public: field(L"language_id", L"language", language, FT::foreign_key); } - void table_name(PT::TextStream & stream) + void prepare_table() { - // schema.table_name or just table_name - stream << "public.attachment2"; + table(L"public", L"attachment2"); } void after_select() diff --git a/samples/language.h b/samples/language.h index d1e0e3e..4c79951 100644 --- a/samples/language.h +++ b/samples/language.h @@ -77,11 +77,9 @@ public: field(L"code_int", code_int); } - - void table_name(PT::TextStream & stream) + void prepare_table() { - // schema.table_name or just table_name - stream << "public.language"; + table(L"public", L"language"); } void after_insert() diff --git a/samples/person.h b/samples/person.h index a0a5a88..54a72c3 100644 --- a/samples/person.h +++ b/samples/person.h @@ -87,10 +87,9 @@ public: } - void table_name(PT::TextStream & stream) + void prepare_table() { - // schema.table_name or just table_name - stream << "public.person"; + table(L"public", L"person"); } diff --git a/samples/sample01.h b/samples/sample01.h index a9d46a4..95dc80a 100644 --- a/samples/sample01.h +++ b/samples/sample01.h @@ -32,6 +32,9 @@ * */ +#ifndef headerfile_morm_samples_sample01 +#define headerfile_morm_samples_sample01 + #include #include "basesample.h" #include "person.h" @@ -62,7 +65,7 @@ void make() person.set_connector(model_connector); load_defaults(person); - //std::wstring sss = L"cosik wstawiony dynamicznie"; + //std::wstring sss = L"some text put dynamically"; //person.set_field_value_generic(L"email", L"email", sss); @@ -75,7 +78,7 @@ void make() morm::Finder finder(model_connector); - Person p = finder.select().where().eq(L"id", 207).get(); + Person p = finder.select().where().eq(L"id", 210).get(); p.to_text(str, true, true); @@ -223,3 +226,5 @@ private: } } +#endif + diff --git a/samples/type.h b/samples/type.h index b34e50d..cfa1399 100644 --- a/samples/type.h +++ b/samples/type.h @@ -70,11 +70,9 @@ public: field(L"name", name); } - - void table_name(PT::TextStream & stream) + void prepare_table() { - // schema.table_name or just table_name - stream << "public.types"; + table(L"public", L"types"); } void after_insert() diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index f26df0b..96f6248 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -194,18 +194,6 @@ bool BaseExpression::is_long_field_name(const PT::TextStream & field_name) } -bool BaseExpression::is_long_table_name(const wchar_t * table_name) -{ - return is_long_field_name(table_name); -} - -bool BaseExpression::is_long_table_name(const PT::TextStream & table_name) -{ - return is_long_field_name(table_name); -} - - - void BaseExpression::put_field_name(const wchar_t * field_name, ModelEnv * model_env) { if( is_long_field_name(field_name) ) @@ -250,7 +238,7 @@ void BaseExpression::put_short_field_name(const wchar_t * field_name, ModelEnv * if( use_prefix && model_env ) { before_first_part_long_field_name(); - esc(model_env->table_name_short, *out_stream); + esc(model_env->table_name, *out_stream); if( model_env->table_index > 1 ) { @@ -545,6 +533,15 @@ void BaseExpression::esc(const PT::TextStream & val, PT::TextStream & stream) } } +void BaseExpression::esc(const PT::WTextStream & val, PT::TextStream & stream) +{ + PT::WTextStream::const_iterator i = val.begin(); + + for(; i != val.end() ; ++i) + { + esc(*i, stream); + } +} void BaseExpression::put_type(char val, PT::TextStream & stream) { diff --git a/src/baseexpression.h b/src/baseexpression.h index 2f6b23a..f344a06 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -250,14 +250,11 @@ public: virtual void esc(const PT::Date & date, PT::TextStream & stream); virtual void esc(const PT::TextStream & val,PT::TextStream & stream); + virtual void esc(const PT::WTextStream & val,PT::TextStream & stream); virtual bool is_long_field_name(const wchar_t * field_name); virtual bool is_long_field_name(const PT::TextStream & table_name); - virtual bool is_long_table_name(const wchar_t * field_name); - virtual bool is_long_table_name(const PT::TextStream & table_name); - - protected: diff --git a/src/cursor.h b/src/cursor.h index 47d639c..b3ec707 100644 --- a/src/cursor.h +++ b/src/cursor.h @@ -193,7 +193,7 @@ public: if( !cursor_helper.has_autogenerated_select && cursor_helper.use_table_prefix_for_fetching_values ) { - result.prepare_table_names(); + result.model_env->add_table_name_to_finder_helper(); } result.before_select(); @@ -369,7 +369,7 @@ protected: if( !cursor_helper.has_autogenerated_select && cursor_helper.use_table_prefix_for_fetching_values ) { - added_model.prepare_table_names(); + added_model.model_env->add_table_name_to_finder_helper(); } added_model.before_select(); diff --git a/src/dbconnector.cpp b/src/dbconnector.cpp index c1ac2cb..8c9a41d 100644 --- a/src/dbconnector.cpp +++ b/src/dbconnector.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018-2019, Tomasz Sowa + * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -182,8 +182,8 @@ void DbConnector::generate_insert_query(PT::TextStream & stream, Model & model) db_expression->clear(); db_expression->allow_to_use_prefix(false); - stream << "insert into "; - model.table_name(stream); + // IMPROVEME escape table_name + stream << "insert into " << model.get_table_name(); stream << " ("; db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS); @@ -207,8 +207,8 @@ void DbConnector::generate_update_query(PT::TextStream & stream, Model & model) db_expression->clear(); db_expression->allow_to_use_prefix(false); - stream << "update "; - model.table_name(stream); + // IMPROVEME escape table_name + stream << "update " << model.get_table_name(); stream << " set "; db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS_VALUES); @@ -232,8 +232,9 @@ void DbConnector::generate_remove_query(PT::TextStream & stream, Model & model) db_expression->clear(); db_expression->allow_to_use_prefix(false); - stream << "delete from "; - model.table_name(stream); + // IMPROVEME escape table_name + stream << "delete from " << model.get_table_name(); + stream << " where "; db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS_VALUES); db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_PRIMARY_KEY); diff --git a/src/dbexpression.cpp b/src/dbexpression.cpp index 0036359..2d0306c 100644 --- a/src/dbexpression.cpp +++ b/src/dbexpression.cpp @@ -314,37 +314,4 @@ DbExpression & DbExpression::page(PT::TextStream & stream, size_t page_number, s - -void DbExpression::prepare_short_table_name(const PT::TextStream & table_name, PT::TextStream & short_table_name) -{ - short_table_name.clear(); - - if( is_long_table_name(table_name) ) - { - PT::TextStream::const_iterator i = table_name.begin(); - bool was_dot = false; - - while( i != table_name.end() ) - { - if( was_dot ) - { - short_table_name << *i; - } - else - if( *i == '.' ) - { - was_dot = true; - } - - ++i; - } - } - - if( short_table_name.empty() ) - { - short_table_name = table_name; - } -} - - } diff --git a/src/dbexpression.h b/src/dbexpression.h index ab6d549..95510df 100644 --- a/src/dbexpression.h +++ b/src/dbexpression.h @@ -77,8 +77,6 @@ public: } - virtual void prepare_short_table_name(const PT::TextStream & table_name, PT::TextStream & short_table_name); - protected: diff --git a/src/finder.h b/src/finder.h index 3407b9d..b67012e 100644 --- a/src/finder.h +++ b/src/finder.h @@ -227,17 +227,17 @@ public: model.model_env = &model_env; model.model_env->model_data = model_data; model.model_env->finder_helper = &finder_helper; + model.prepare_table(); + model.model_env->add_table_name_to_finder_helper(); has_autogenerated_select = true; if( model_connector && out_stream && db_expression ) { - model.prepare_table_names(); - (*out_stream) << "SELECT "; model.generate_select_columns(*out_stream); (*out_stream) << " FROM " << model.model_env->table_name << " AS "; - (*out_stream) << model.model_env->table_name_short; + (*out_stream) << model.model_env->table_name; (*out_stream) << " "; (*out_stream) << finder_helper.join_tables_str; } diff --git a/src/finderhelper.h b/src/finderhelper.h index 4898fe8..7a771ff 100644 --- a/src/finderhelper.h +++ b/src/finderhelper.h @@ -48,7 +48,7 @@ public: PT::TextStream join_tables_str; - std::map join_tables_map; + std::map join_tables_map; std::list foreign_keys; @@ -72,16 +72,16 @@ public: } - virtual int add_join_table(const PT::TextStream & table_name) + virtual int add_join_table(const PT::WTextStream & table_name) { - std::string table_name_str; + std::wstring table_name_str; table_name.to_string(table_name_str); return add_join_table(table_name_str); } - virtual int add_join_table(const std::string & table_name) + virtual int add_join_table(const std::wstring & table_name) { auto res = join_tables_map.insert(std::make_pair(table_name, 1)); diff --git a/src/model.cpp b/src/model.cpp index 416f778..2317b96 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -109,12 +109,6 @@ void Model::mark_to_update() -void Model::table_name(PT::TextStream & stream) -{ -} - - - void Model::set_connector(ModelConnector & connector) { set_connector(&connector); @@ -133,6 +127,44 @@ ModelConnector * Model::get_connector() } +void Model::prepare_table() +{ + if( model_connector ) + { + PT::Log * plog = model_connector->get_logger(); + + if( plog ) + { + (*plog) << PT::Log::log1 << "Morm: you should provide the table name e.g. provide prepare_table() method and call table(...) there" << PT::Log::logend; + } + } +} + +void Model::table(const wchar_t * table_name) +{ + if( model_env ) + { + model_env->schema_name.clear(); + model_env->table_name.clear(); + + model_env->table_name << table_name; + } +} + +void Model::table(const wchar_t * schema_name, const wchar_t * table_name) +{ + if( model_env ) + { + model_env->schema_name.clear(); + model_env->table_name.clear(); + + model_env->schema_name << schema_name; + model_env->table_name << table_name; + } +} + + + bool Model::object_exists() { return save_mode == DO_UPDATE_ON_SAVE; @@ -168,6 +200,7 @@ void Model::to_text(PT::TextStream & stream, ModelData * model_data, bool clear_ { try { + prepare_table(); flat_connector->to_text(stream, *this); } catch(...) @@ -255,6 +288,7 @@ void Model::generate_insert_query(PT::TextStream & stream, ModelData * model_dat { try { + prepare_table(); db_connector->generate_insert_query(stream, *this); } catch(...) @@ -291,6 +325,7 @@ bool Model::insert(ModelData * model_data, bool insert_whole_tree) try { + prepare_table(); status = insert_tree(insert_whole_tree); } catch(...) @@ -385,6 +420,7 @@ void Model::generate_update_query(PT::TextStream & stream, ModelData * model_dat if( db_connector ) { + prepare_table(); db_connector->generate_update_query(stream, *this); } } @@ -416,6 +452,7 @@ bool Model::update(ModelData * model_data, bool update_whole_tree) try { + prepare_table(); status = update_tree(update_whole_tree); } catch(...) @@ -494,6 +531,7 @@ void Model::generate_remove_query(PT::TextStream & stream, ModelData * model_dat if( db_connector ) { + prepare_table(); db_connector->generate_remove_query(stream, *this); } } @@ -526,6 +564,7 @@ bool Model::remove(ModelData * model_data, bool remove_whole_tree) try { + prepare_table(); status = remove_tree(remove_whole_tree); } catch(...) @@ -621,6 +660,7 @@ bool Model::save(ModelData * model_data, bool save_whole_tree) try { + prepare_table(); status = save_tree(save_whole_tree); } catch(...) @@ -741,6 +781,7 @@ void Model::clear() try { + // prepare_table() doesn't have to be called map_fields(); } catch(...) @@ -865,34 +906,11 @@ bool Model::is_the_same_field(const wchar_t * field1, const wchar_t * field2) } - -void Model::prepare_table_names(bool prepare_table_index) -{ - DbConnector * db_connector = model_connector->get_db_connector(); - - if( db_connector && model_env ) - { - DbExpression * db_expression = db_connector->get_expression(); - - if( db_expression ) - { - table_name(model_env->table_name); - db_expression->prepare_short_table_name(model_env->table_name, model_env->table_name_short); - - if( prepare_table_index && model_env->finder_helper ) - { - model_env->table_index = model_env->finder_helper->add_join_table(model_env->table_name_short); - } - } - } -} - - void Model::put_table_name_with_index(PT::TextStream & str) { if( model_env ) { - str << model_env->table_name_short; + str << model_env->table_name; if( model_env->table_index > 1 ) { @@ -902,6 +920,59 @@ void Model::put_table_name_with_index(PT::TextStream & str) } +PT::WTextStream Model::get_table_name(bool put_schema_name) +{ + PT::WTextStream str; + + if( model_env ) + { + if( put_schema_name && !model_env->schema_name.empty() ) + { + str << model_env->schema_name; + + // IMPROVEME make a virtual method in dbexpression to put such a dot + str << '.'; + } + + str << model_env->table_name; + } + + return str; +} + + +PT::WTextStream Model::get_table_name_with_field(const wchar_t * db_field_name, bool put_schema_name) +{ + PT::WTextStream str; + bool is_empty_field_name = is_empty_field(db_field_name); + + if( model_env ) + { + if( put_schema_name && !model_env->schema_name.empty() ) + { + str << model_env->schema_name; + + // IMPROVEME make a virtual method in dbexpression to put such a dot + str << '.'; + } + + str << model_env->table_name; + + if( !is_empty_field_name ) + { + str << '.'; // IMPROVEME get a virtual method from dbexpression + } + } + + if( !is_empty_field_name ) + { + str << db_field_name; + } + + return str; +} + + void Model::put_to_log(const wchar_t * str) { if( model_connector ) @@ -943,24 +1014,6 @@ void Model::put_fields_to_log(PT::Log & log, const wchar_t * db_field_name, cons } -PT::TextStream Model::log_table_name() -{ - PT::TextStream buf; - table_name(buf); - - return buf; -} - - -PT::TextStream Model::log_table_name(const wchar_t * db_field_name) -{ - PT::TextStream buf; - table_name(buf); - buf << '.' << db_field_name; - - return buf; -} - } // namespace diff --git a/src/model.h b/src/model.h index 8588c20..0e9de17 100644 --- a/src/model.h +++ b/src/model.h @@ -88,19 +88,16 @@ public: /* * map fields to names * - * + * IMPROVEME rename me to fields() and make protected */ virtual void map_fields() = 0; - - /* - * - * we can use the object for a different purpose than database (e.g. json) - * so let the table_name be non pure-virtual - * + * IMPROVEME make me protected */ - virtual void table_name(PT::TextStream & stream); + virtual void prepare_table(); + virtual void table(const wchar_t * table_name); + virtual void table(const wchar_t * schema_name, const wchar_t * table_name); 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); @@ -147,6 +144,7 @@ public: model_env = &model_env_local; model_env->model_work_mode = MORM_MODEL_WORK_MODE_SET_FIELD_VALUE; model_env->field_index = 0; + prepare_table(); // CHECK ME it is needed to set table name? FieldValueHelper field_value_helper; field_value_helper.db_field_name = db_field_name; @@ -166,7 +164,7 @@ public: if( plog ) { - (*plog) << "Morm: I cannot find such a property: "; + (*plog) << PT::Log::log1 << "Morm: I cannot find such a property: "; put_fields_to_log(*plog, db_field_name, flat_field_name); (*plog) << PT::Log::logend; } @@ -465,11 +463,9 @@ protected: } else { - table_name(model_env->table_name); - if( log ) { - (*log) << PT::Log::log1 << "Morm: incorrect type of a field in " << model_env->table_name << ", "; + (*log) << PT::Log::log1 << "Morm: incorrect type of a field in " << get_table_name() << ", "; put_fields_to_log(*log, db_field_name, flat_field_name); (*log) << ", type expected " << typeid(field_value).name() << " got " << helper.value_type_info->name() << PT::Log::logend; @@ -623,7 +619,7 @@ protected: { model_env->finder_helper->foreign_keys.clear(); PT::TextStream & join_tables_str = model_env->finder_helper->join_tables_str; - field_model.model_env->table_index = model_env->finder_helper->add_join_table(field_model.model_env->table_name_short); + field_model.model_env->add_table_name_to_finder_helper(); join_tables_str << "LEFT JOIN " << field_model.model_env->table_name << " AS "; field_model.put_table_name_with_index(join_tables_str); @@ -704,7 +700,7 @@ protected: if( model_env->field_value_helper_tab->empty() && plog ) { - (*plog) << "Morm: I cannot find a primary key in " << model_env->table_name << PT::Log::logend; + (*plog) << PT::Log::log1 << "Morm: I cannot find a primary key in " << get_table_name() << PT::Log::logend; } } } @@ -736,26 +732,21 @@ protected: model_env_local.field_value_helper_tab = &helper_tab; model_env_local.field_index = 0; field_model.model_env = &model_env_local; + field_model.prepare_table(); field_model.map_fields(); if( (size_t)field_model.model_env->field_index != helper_tab.size() && log ) { - if( model_env->table_name.empty() ) - table_name(model_env->table_name); - - if( field_model.model_env->table_name.empty() ) - field_model.table_name(field_model.model_env->table_name); - if( field_model.model_env->field_index == 0 ) { - (*log) << PT::Log::log1 << "Morm: there is no a foreign key in " << field_model.model_env->table_name - << " called " << db_field_name << " pointing to " << model_env->table_name << PT::Log::logend; + (*log) << PT::Log::log1 << "Morm: there is no a foreign key in " << field_model.get_table_name() + << " called " << db_field_name << " pointing to " << get_table_name() << PT::Log::logend; } else { - (*log) << PT::Log::log1 << "Morm: primary key in " << model_env->table_name << " consists of " << model_env->field_index << " column(s)" - << " but foreign key in " << field_model.model_env->table_name << " consists of " + (*log) << PT::Log::log1 << "Morm: primary key in " << get_table_name() << " consists of " << model_env->field_index << " column(s)" + << " but foreign key in " << field_model.get_table_name() << " consists of " << field_model.model_env->field_index << " column(s)" << PT::Log::logend; } } @@ -765,9 +756,7 @@ protected: else if( log ) { - table_name(model_env->table_name); - - (*log) << PT::Log::log1 << "Morm: primary key in " << model_env->table_name << " consists of incorrect number of columns" + (*log) << PT::Log::log1 << "Morm: primary key in " << get_table_name() << " consists of incorrect number of columns" << ", expected " << helper_tab.size() << " column(s) but got " << model_env->field_index << PT::Log::logend; } } @@ -852,9 +841,6 @@ protected: { field_model.model_env->model_work_mode = MORM_MODEL_WORK_MODE_GENERATING_DB_SQL; - field_model.table_name(field_model.model_env->table_name); - db_expression->prepare_short_table_name(field_model.model_env->table_name, field_model.model_env->table_name_short); - if( db_expression->get_output_type() == MORM_OUTPUT_TYPE_SELECT_COLUMNS ) { field_model_left_join(db_field_name, field_model, field_type, db_expression); @@ -943,7 +929,7 @@ protected: !model_env->cursor_helper->has_autogenerated_select && model_env->cursor_helper->use_table_prefix_for_fetching_values ) { - field_model.prepare_table_names(); + field_model.model_env->add_table_name_to_finder_helper(); } field_model.before_select(); @@ -968,6 +954,7 @@ protected: field_model.model_env = &model_env_local; field_model.model_env->has_primary_key_set = field_model.has_primary_key_set; field_model.set_connector(model_connector); + field_model.prepare_table(); if( !is_empty_field(db_field_name) ) { @@ -981,7 +968,7 @@ protected: if( plog ) { - (*plog) << "Morm: error in " << log_table_name(db_field_name) + (*plog) << PT::Log::log1 << "Morm: error in " << get_table_name_with_field(db_field_name) << " field, you should set FT::is_foreign_key or FT::is_foreign_key_in_child flag for a model child object" << PT::Log::logend; } } @@ -1080,6 +1067,7 @@ protected: child_model.model_env = &model_env_local; child_model.model_env->has_primary_key_set = child_model.has_primary_key_set; child_model.set_connector(model_connector); + child_model.prepare_table(); if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_INSERT ) { @@ -1153,7 +1141,7 @@ protected: { if( plog ) { - (*plog) << "Morm: error: FT::is_foreign_key is not implemented for a list/vector yet" << PT::Log::logend; + (*plog) << PT::Log::log1 << "Morm: error: FT::is_foreign_key is not implemented for a list/vector yet" << PT::Log::logend; return; } } @@ -1176,7 +1164,7 @@ protected: { if( plog ) { - (*plog) << "Morm: ignoring " << log_table_name(db_field_name) << " as this is not a container with Model objects" << PT::Log::logend; + (*plog) << PT::Log::log1 << "Morm: ignoring " << get_table_name_with_field(db_field_name) << " as this is not a container with Model objects" << PT::Log::logend; } } } @@ -1184,7 +1172,7 @@ protected: { if( plog ) { - (*plog) << "Morm: error in " << log_table_name(db_field_name) + (*plog) << PT::Log::log1 << "Morm: error in " << get_table_name_with_field(db_field_name) << " field, you should set FT::is_foreign_key or FT::is_foreign_key_in_child flag for a list of child objects" << PT::Log::logend; } } @@ -1363,24 +1351,23 @@ public: } - protected: virtual bool is_empty_field(const wchar_t * value); virtual bool is_the_same_field(const wchar_t * field1, const wchar_t * field2); - virtual void prepare_table_names(bool prepare_table_index = true); + virtual void put_table_name_with_index(PT::TextStream & str); virtual void put_to_log(const wchar_t * str); virtual void put_fields_to_log(PT::Log & log, const wchar_t * db_field_name, const wchar_t * flat_field_name); - PT::TextStream log_table_name(); - PT::TextStream log_table_name(const wchar_t * db_field_name); - + virtual PT::WTextStream get_table_name(bool put_schema_name = true); + virtual PT::WTextStream get_table_name_with_field(const wchar_t * db_field_name = nullptr, bool put_schema_name = true); template friend class Finder; template friend class Cursor; friend class BaseExpression; + friend class DbConnector; }; diff --git a/src/modelenv.h b/src/modelenv.h index 8b5945d..9cb6047 100644 --- a/src/modelenv.h +++ b/src/modelenv.h @@ -61,8 +61,8 @@ public: int model_work_submode; bool dump_mode; - PT::TextStream table_name; // CHECK ME may it should be PT::WTextStream? - PT::TextStream table_name_short; + PT::WTextStream schema_name; + PT::WTextStream table_name; int table_index; int field_index; bool was_primary_key_read; @@ -97,7 +97,7 @@ public: was_primary_key_read = e.was_primary_key_read; has_primary_key_set = e.has_primary_key_set; - // table_name and table_name_short don't have to bo copied + // schema_name and table_name don't have to be copied } @@ -121,8 +121,8 @@ public: model_work_mode = MORM_MODEL_WORK_MODE_NONE; model_work_submode = MORM_MODEL_WORK_SUBMODE_NONE; dump_mode = false; + schema_name.clear(); table_name.clear(); - table_name_short.clear(); table_index = 0; set_field_name_helper = nullptr; field_value_helper_tab = nullptr; @@ -132,6 +132,18 @@ public: } + void add_table_name_to_finder_helper() + { + if( finder_helper ) + { + table_index = finder_helper->add_join_table(table_name); + } + else + { + table_index = 0; + } + } + }; } From 7bef1d5ead5a8d15baf5b8af52ee02a5814f59ac Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 11 Mar 2021 18:40:32 +0100 Subject: [PATCH 04/27] added support for PT::Space as a field in a Model methods before_field_value_string() and after_field_value_string() moved from DbExpression and JsonExpression to BaseExpression and made virtual and now methods before_field_value(const std::wstring &) and after_field_value(const std::wstring &) (and the rest of them with string arguments) can be removed from DbExpression, PostgreSqlExpression and JsonExpression --- src/Makefile.dep | 16 +-- src/baseexpression.cpp | 229 +++++++++++++++++++++-------------- src/baseexpression.h | 77 +++++++----- src/clearer.cpp | 7 +- src/clearer.h | 4 +- src/dbconnector.cpp | 24 ++++ src/dbconnector.h | 3 +- src/dbexpression.cpp | 68 +---------- src/dbexpression.h | 15 --- src/jsonexpression.cpp | 32 ----- src/jsonexpression.h | 7 -- src/model.h | 17 +++ src/postgresqlexpression.cpp | 54 +-------- src/postgresqlexpression.h | 11 -- 14 files changed, 246 insertions(+), 318 deletions(-) diff --git a/src/Makefile.dep b/src/Makefile.dep index e0eb565..2a1bf2a 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep @@ -13,18 +13,20 @@ baseexpression.o: finderhelper.h fieldvaluehelper.h ft.h model.h baseexpression.o: modelconnector.h clearer.h dbconnector.h flatconnector.h baseexpression.o: dbexpression.h flatexpression.h ../../pikotools/utf8/utf8.h clearer.o: clearer.h ../../pikotools/date/date.h -clearer.o: ../../pikotools/convert/inttostr.h model.h +clearer.o: ../../pikotools/convert/inttostr.h ../../pikotools/space/space.h +clearer.o: ../../pikotools/textstream/types.h model.h clearer.o: ../../pikotools/textstream/textstream.h -clearer.o: ../../pikotools/space/space.h ../../pikotools/textstream/types.h clearer.o: ../../pikotools/membuffer/membuffer.h clearer.o: ../../pikotools/textstream/types.h modelconnector.h dbconnector.h clearer.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h clearer.o: queryresult.h flatconnector.h dbexpression.h baseexpression.h clearer.o: morm_types.h modelenv.h modeldata.h cursorhelper.h finderhelper.h clearer.o: fieldvaluehelper.h ft.h flatexpression.h -dbconnector.o: dbconnector.h ../../pikotools/textstream/textstream.h +dbconnector.o: ../../pikotools/space/spaceparser.h dbconnector.o: ../../pikotools/space/space.h -dbconnector.o: ../../pikotools/textstream/types.h ../../pikotools/date/date.h +dbconnector.o: ../../pikotools/textstream/types.h dbconnector.h +dbconnector.o: ../../pikotools/textstream/textstream.h +dbconnector.o: ../../pikotools/space/space.h ../../pikotools/date/date.h dbconnector.o: ../../pikotools/convert/inttostr.h dbconnector.o: ../../pikotools/membuffer/membuffer.h dbconnector.o: ../../pikotools/textstream/types.h ../../pikotools/log/log.h @@ -105,10 +107,10 @@ model.o: dbexpression.h baseexpression.h morm_types.h modelenv.h modeldata.h model.o: cursorhelper.h finderhelper.h fieldvaluehelper.h ft.h model.o: flatexpression.h modelconnector.o: modelconnector.h clearer.h ../../pikotools/date/date.h -modelconnector.o: ../../pikotools/convert/inttostr.h dbconnector.h -modelconnector.o: ../../pikotools/textstream/textstream.h +modelconnector.o: ../../pikotools/convert/inttostr.h modelconnector.o: ../../pikotools/space/space.h -modelconnector.o: ../../pikotools/textstream/types.h +modelconnector.o: ../../pikotools/textstream/types.h dbconnector.h +modelconnector.o: ../../pikotools/textstream/textstream.h modelconnector.o: ../../pikotools/membuffer/membuffer.h modelconnector.o: ../../pikotools/textstream/types.h modelconnector.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index 96f6248..be1f0a3 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -313,51 +313,70 @@ void BaseExpression::after_second_part_long_field_name() void BaseExpression::before_field_value(const std::wstring &) { + before_field_value_string(); } void BaseExpression::before_field_value(const std::string &) { + before_field_value_string(); } void BaseExpression::after_field_value(const std::wstring &) { + after_field_value_string(); } void BaseExpression::after_field_value(const std::string &) { + after_field_value_string(); } void BaseExpression::before_field_value(const wchar_t *) { + before_field_value_string(); } void BaseExpression::after_field_value(const wchar_t *) { + after_field_value_string(); } void BaseExpression::before_field_value(const char *) { + before_field_value_string(); } void BaseExpression::after_field_value(const char *) { + after_field_value_string(); } void BaseExpression::before_field_value(const PT::Date &) { + before_field_value_string(); } void BaseExpression::after_field_value(const PT::Date &) { + after_field_value_string(); } +void BaseExpression::before_field_value(const PT::Space &) +{ + before_field_value_string(); +} + +void BaseExpression::after_field_value(const PT::Space &) +{ + after_field_value_string(); +} void BaseExpression::put_name_value_separator() @@ -543,114 +562,138 @@ void BaseExpression::esc(const PT::WTextStream & val, PT::TextStream & stream) } } -void BaseExpression::put_type(char val, PT::TextStream & stream) +void BaseExpression::esc(const PT::Space & space, PT::TextStream & stream) { - stream << "char"; -} + PT::WTextStream tmp_stream; -void BaseExpression::put_type(unsigned char val, PT::TextStream & stream) -{ - stream << "unsigned char"; + if( space.table.size() > 0 ) + { + tmp_stream.clear(); + } + + space.Serialize(tmp_stream, true); + esc(tmp_stream, stream); } -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(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"; //} -void BaseExpression::put_type(const PT::Date & date, PT::TextStream & stream) +void BaseExpression::before_field_value_string() { - stream << "date"; } -void BaseExpression::put_type(const Model & model, PT::TextStream & stream) +void BaseExpression::after_field_value_string() { - stream << "object"; } + } diff --git a/src/baseexpression.h b/src/baseexpression.h index f344a06..57c2144 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -251,6 +251,7 @@ public: virtual void esc(const PT::Date & date, PT::TextStream & stream); virtual void esc(const PT::TextStream & val,PT::TextStream & stream); virtual void esc(const PT::WTextStream & val,PT::TextStream & stream); + virtual void esc(const PT::Space & space, PT::TextStream & stream); virtual bool is_long_field_name(const wchar_t * field_name); virtual bool is_long_field_name(const PT::TextStream & table_name); @@ -631,6 +632,8 @@ protected: virtual void before_field_value(const PT::Date &); virtual void after_field_value(const PT::Date &); + virtual void before_field_value(const PT::Space &); + virtual void after_field_value(const PT::Space &); template void before_field_value(const FieldValue &) @@ -650,43 +653,51 @@ protected: * put_type for: signed char, wchar_t, char16_t, char32_t * */ - 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(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); +// 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"; // may just use std::list? +// } +// +// template +// void put_type(const std::vector & model, PT::TextStream & stream) +// { +// stream << "table"; // may just just std::vector? +// } + + +private: + + virtual void before_field_value_string(); + virtual void after_field_value_string(); - template - void put_type(const std::list & model, PT::TextStream & stream) - { - stream << "table"; // may just use std::list? - } - template - void put_type(const std::vector & model, PT::TextStream & stream) - { - stream << "table"; // may just just std::vector? - } }; diff --git a/src/clearer.cpp b/src/clearer.cpp index 8fe9f1c..3075547 100644 --- a/src/clearer.cpp +++ b/src/clearer.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018, Tomasz Sowa + * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -139,6 +139,11 @@ void Clearer::clear_value(PT::Date & field_value) field_value.Clear(); } +void Clearer::clear_value(PT::Space & field_value) +{ + field_value.Clear(); +} + void Clearer::clear_model(Model & field_value) { field_value.clear(); diff --git a/src/clearer.h b/src/clearer.h index 9f1efbb..94f9de3 100644 --- a/src/clearer.h +++ b/src/clearer.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018, Tomasz Sowa + * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -37,6 +37,7 @@ #include #include "date/date.h" +#include "space/space.h" namespace morm @@ -68,6 +69,7 @@ public: virtual void clear_value(double & field_value); virtual void clear_value(long double & field_value); virtual void clear_value(PT::Date & field_value); + virtual void clear_value(PT::Space & field_value); virtual void clear_model(Model & field_value); diff --git a/src/dbconnector.cpp b/src/dbconnector.cpp index 8c9a41d..b19c040 100644 --- a/src/dbconnector.cpp +++ b/src/dbconnector.cpp @@ -34,6 +34,7 @@ #include #include +#include "space/spaceparser.h" #include "dbconnector.h" #include "dbexpression.h" #include "model.h" @@ -427,6 +428,29 @@ void DbConnector::get_value(const char * value_str, PT::Date & field_value) } +void DbConnector::get_value(const char * value_str, PT::Space & field_value) +{ + field_value.Clear(); + + if( *value_str != '\0' ) + { + PT::SpaceParser space_parser; + space_parser.SetSpace(field_value); + + if( space_parser.ParseString(value_str) != PT::SpaceParser::ok ) + { + field_value.Clear(); + + if( log ) + { + (*log) << PT::Log::log1 << "Morm: I cannot correctly parse the Space struct from the datebase" + << ", the raw string is: " << value_str << PT::Log::logend; + } + } + } +} + + const char * DbConnector::query_last_sequence(const wchar_t * sequence_table_name) { return nullptr; diff --git a/src/dbconnector.h b/src/dbconnector.h index 4c1b8f0..f86e90c 100644 --- a/src/dbconnector.h +++ b/src/dbconnector.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018-2019, Tomasz Sowa + * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -107,6 +107,7 @@ public: virtual void get_value(const char * value_str, double & field_value); virtual void get_value(const char * value_str, long double & field_value); virtual void get_value(const char * value_str, PT::Date & field_value); + virtual void get_value(const char * value_str, PT::Space & field_value); template diff --git a/src/dbexpression.cpp b/src/dbexpression.cpp index 2d0306c..a0af7ef 100644 --- a/src/dbexpression.cpp +++ b/src/dbexpression.cpp @@ -185,75 +185,13 @@ void DbExpression::put_name_value_separator() void DbExpression::before_field_value_string() { -// 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 ) - { - (*out_stream) << "'"; - } + (*out_stream) << "'"; } + void DbExpression::after_field_value_string() { -// 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 ) - { - (*out_stream) << "'"; - } -} - - -void DbExpression::before_field_value(const std::wstring &) -{ - before_field_value_string(); -} - -void DbExpression::after_field_value(const std::wstring &) -{ - after_field_value_string(); -} - -void DbExpression::before_field_value(const std::string &) -{ - before_field_value_string(); -} - -void DbExpression::after_field_value(const std::string &) -{ - after_field_value_string(); -} - -void DbExpression::before_field_value(const wchar_t *) -{ - before_field_value_string(); -} - -void DbExpression::after_field_value(const wchar_t *) -{ - after_field_value_string(); -} - -void DbExpression::before_field_value(const char *) -{ - before_field_value_string(); -} - -void DbExpression::after_field_value(const char *) -{ - after_field_value_string(); -} - -void DbExpression::before_field_value(const PT::Date &) -{ - before_field_value_string(); -} - -void DbExpression::after_field_value(const PT::Date &) -{ - after_field_value_string(); + (*out_stream) << "'"; } diff --git a/src/dbexpression.h b/src/dbexpression.h index 95510df..3fbb890 100644 --- a/src/dbexpression.h +++ b/src/dbexpression.h @@ -88,21 +88,6 @@ protected: void field_before(); - void before_field_value(const std::wstring &); - void after_field_value(const std::wstring &); - - void before_field_value(const std::string &); - void after_field_value(const std::string &); - - void before_field_value(const wchar_t *); - void after_field_value(const wchar_t *); - - void before_field_value(const char *); - void after_field_value(const char *); - - void before_field_value(const PT::Date &); - void after_field_value(const PT::Date &); - void put_name_value_separator(); diff --git a/src/jsonexpression.cpp b/src/jsonexpression.cpp index 692693e..f9cd103 100644 --- a/src/jsonexpression.cpp +++ b/src/jsonexpression.cpp @@ -116,38 +116,6 @@ void JSONExpression::after_field_value_string() } -void JSONExpression::before_field_value(const std::wstring &) -{ - before_field_value_string(); -} - -void JSONExpression::before_field_value(const std::string &) -{ - before_field_value_string(); -} - -void JSONExpression::after_field_value(const std::wstring &) -{ - after_field_value_string(); -} - -void JSONExpression::after_field_value(const std::string &) -{ - after_field_value_string(); -} - -void JSONExpression::before_field_value(const PT::Date &) -{ - before_field_value_string(); -} - -void JSONExpression::after_field_value(const PT::Date &) -{ - after_field_value_string(); -} - - - void JSONExpression::put_name_value_separator() { (*out_stream) << ':'; diff --git a/src/jsonexpression.h b/src/jsonexpression.h index 79a91da..f2091c5 100644 --- a/src/jsonexpression.h +++ b/src/jsonexpression.h @@ -61,15 +61,8 @@ protected: void before_second_part_long_field_name(); void after_second_part_long_field_name(); - void before_field_value(const std::wstring &); - void before_field_value(const std::string &); - void after_field_value(const std::wstring &); - void after_field_value(const std::string &); void put_name_value_separator(); - void before_field_value(const PT::Date &); - void after_field_value(const PT::Date &); - void before_field_value_list(); void after_field_value_list(); diff --git a/src/model.h b/src/model.h index 0e9de17..beaad31 100644 --- a/src/model.h +++ b/src/model.h @@ -42,6 +42,7 @@ #include #include "textstream/textstream.h" +#include "space/space.h" #include "modelconnector.h" #include "dbexpression.h" #include "flatexpression.h" @@ -306,6 +307,11 @@ protected: field_generic(field_name, field_name, field_value, field_type); } + void field(const wchar_t * field_name, PT::Space & field_value, FT field_type = FT::default_type) + { + field_generic(field_name, field_name, field_value, field_type); + } + void field(const wchar_t * field_name, Model & field_value, FT field_type = FT::default_type) { // has_foreign_key was here @@ -416,6 +422,10 @@ protected: field_generic(db_field_name, flat_field_name, field_value, field_type); } + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, PT::Space & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_value, FT field_type = FT::default_type) { @@ -480,6 +490,7 @@ protected: } } + template void field_generic_iterate_primary_key_values(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) { @@ -499,6 +510,7 @@ protected: } } + template void field_generic_generate_flat_string(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) { @@ -515,6 +527,7 @@ protected: } } + template void field_generic_generate_db_sql(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) { @@ -531,6 +544,7 @@ protected: } } + template void field_generic_read_value_from_db_resultset(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) { @@ -553,6 +567,7 @@ protected: } } + template void field_generic_clear_value(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) { @@ -1371,6 +1386,8 @@ protected: }; + + } // namespace #endif diff --git a/src/postgresqlexpression.cpp b/src/postgresqlexpression.cpp index ee70474..5a5b2d7 100644 --- a/src/postgresqlexpression.cpp +++ b/src/postgresqlexpression.cpp @@ -77,65 +77,15 @@ void PostgreSQLExpression::after_second_part_long_field_name() void PostgreSQLExpression::before_field_value_string() { -// if( output_type == MORM_OUTPUT_TYPE_DB_INSERT || -// output_type == MORM_OUTPUT_TYPE_DB_UPDATE || -// output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY ) - { - (*out_stream) << "E'"; - } + (*out_stream) << "E'"; } void PostgreSQLExpression::after_field_value_string() { -// if( output_type == MORM_OUTPUT_TYPE_DB_INSERT || -// output_type == MORM_OUTPUT_TYPE_DB_UPDATE || -// output_type == MORM_OUTPUT_TYPE_DB_PRIMARY_KEY ) - { - (*out_stream) << "'"; - } + (*out_stream) << "'"; } -void PostgreSQLExpression::before_field_value(const std::wstring &) -{ - before_field_value_string(); -} - -void PostgreSQLExpression::after_field_value(const std::wstring &) -{ - after_field_value_string(); -} - -void PostgreSQLExpression::before_field_value(const std::string &) -{ - before_field_value_string(); -} - -void PostgreSQLExpression::after_field_value(const std::string &) -{ - after_field_value_string(); -} - -void PostgreSQLExpression::before_field_value(const wchar_t *) -{ - before_field_value_string(); -} - -void PostgreSQLExpression::after_field_value(const wchar_t *) -{ - after_field_value_string(); -} - -void PostgreSQLExpression::before_field_value(const char *) -{ - before_field_value_string(); -} - -void PostgreSQLExpression::after_field_value(const char *) -{ - after_field_value_string(); -} - void PostgreSQLExpression::esc(char val, PT::TextStream & stream) { diff --git a/src/postgresqlexpression.h b/src/postgresqlexpression.h index 6443e10..873247d 100644 --- a/src/postgresqlexpression.h +++ b/src/postgresqlexpression.h @@ -61,17 +61,6 @@ protected: virtual void before_second_part_long_field_name(); virtual void after_second_part_long_field_name(); - void before_field_value(const std::wstring &); - void after_field_value(const std::wstring &); - - void before_field_value(const std::string &); - void after_field_value(const std::string &); - - void before_field_value(const wchar_t *); - void after_field_value(const wchar_t *); - - void before_field_value(const char *); - void after_field_value(const char *); private: From 1e2cbad6a74f9c535f096e5204f2f824cbb9b429 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 8 Apr 2021 17:18:48 +0200 Subject: [PATCH 05/27] make depend --- src/Makefile.dep | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/Makefile.dep b/src/Makefile.dep index 2a1bf2a..9a673e1 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep @@ -3,8 +3,8 @@ baseexpression.o: baseexpression.h ../../pikotools/textstream/textstream.h baseexpression.o: ../../pikotools/space/space.h baseexpression.o: ../../pikotools/textstream/types.h -baseexpression.o: ../../pikotools/date/date.h baseexpression.o: ../../pikotools/convert/inttostr.h +baseexpression.o: ../../pikotools/date/date.h baseexpression.o: ../../pikotools/membuffer/membuffer.h baseexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h baseexpression.o: modeldata.h cursorhelper.h queryresult.h @@ -12,6 +12,8 @@ baseexpression.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h baseexpression.o: finderhelper.h fieldvaluehelper.h ft.h model.h baseexpression.o: modelconnector.h clearer.h dbconnector.h flatconnector.h baseexpression.o: dbexpression.h flatexpression.h ../../pikotools/utf8/utf8.h +baseexpression.o: ../../pikotools/utf8/utf8_templates.h +baseexpression.o: ../../pikotools/utf8/utf8_private.h clearer.o: clearer.h ../../pikotools/date/date.h clearer.o: ../../pikotools/convert/inttostr.h ../../pikotools/space/space.h clearer.o: ../../pikotools/textstream/types.h model.h @@ -24,17 +26,20 @@ clearer.o: morm_types.h modelenv.h modeldata.h cursorhelper.h finderhelper.h clearer.o: fieldvaluehelper.h ft.h flatexpression.h dbconnector.o: ../../pikotools/space/spaceparser.h dbconnector.o: ../../pikotools/space/space.h -dbconnector.o: ../../pikotools/textstream/types.h dbconnector.h +dbconnector.o: ../../pikotools/textstream/types.h +dbconnector.o: ../../pikotools/convert/inttostr.h dbconnector.h dbconnector.o: ../../pikotools/textstream/textstream.h dbconnector.o: ../../pikotools/space/space.h ../../pikotools/date/date.h -dbconnector.o: ../../pikotools/convert/inttostr.h dbconnector.o: ../../pikotools/membuffer/membuffer.h dbconnector.o: ../../pikotools/textstream/types.h ../../pikotools/log/log.h dbconnector.o: ../../pikotools/log/filelog.h queryresult.h dbexpression.h dbconnector.o: baseexpression.h morm_types.h modelenv.h modeldata.h dbconnector.o: cursorhelper.h finderhelper.h fieldvaluehelper.h ft.h model.h dbconnector.o: modelconnector.h clearer.h flatconnector.h flatexpression.h -dbconnector.o: ../../pikotools/utf8/utf8.h ../../pikotools/convert/convert.h +dbconnector.o: ../../pikotools/utf8/utf8.h +dbconnector.o: ../../pikotools/utf8/utf8_templates.h +dbconnector.o: ../../pikotools/utf8/utf8_private.h +dbconnector.o: ../../pikotools/convert/convert.h dbconnector.o: ../../pikotools/convert/inttostr.h dbconnector.o: ../../pikotools/convert/patternreplacer.h dbconnector.o: ../../pikotools/convert/strtoint.h @@ -43,8 +48,8 @@ dbexpression.o: dbexpression.h baseexpression.h dbexpression.o: ../../pikotools/textstream/textstream.h dbexpression.o: ../../pikotools/space/space.h dbexpression.o: ../../pikotools/textstream/types.h -dbexpression.o: ../../pikotools/date/date.h dbexpression.o: ../../pikotools/convert/inttostr.h +dbexpression.o: ../../pikotools/date/date.h dbexpression.o: ../../pikotools/membuffer/membuffer.h dbexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h dbexpression.o: modeldata.h cursorhelper.h queryresult.h @@ -53,8 +58,8 @@ dbexpression.o: finderhelper.h fieldvaluehelper.h ft.h flatconnector.o: flatconnector.h ../../pikotools/textstream/textstream.h flatconnector.o: ../../pikotools/space/space.h flatconnector.o: ../../pikotools/textstream/types.h -flatconnector.o: ../../pikotools/date/date.h flatconnector.o: ../../pikotools/convert/inttostr.h +flatconnector.o: ../../pikotools/date/date.h flatconnector.o: ../../pikotools/membuffer/membuffer.h flatconnector.o: ../../pikotools/textstream/types.h flatexpression.h flatconnector.o: baseexpression.h morm_types.h modelenv.h modeldata.h @@ -66,8 +71,8 @@ flatexpression.o: flatexpression.h baseexpression.h flatexpression.o: ../../pikotools/textstream/textstream.h flatexpression.o: ../../pikotools/space/space.h flatexpression.o: ../../pikotools/textstream/types.h -flatexpression.o: ../../pikotools/date/date.h flatexpression.o: ../../pikotools/convert/inttostr.h +flatexpression.o: ../../pikotools/date/date.h flatexpression.o: ../../pikotools/membuffer/membuffer.h flatexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h flatexpression.o: modeldata.h cursorhelper.h queryresult.h @@ -77,8 +82,8 @@ jsonconnector.o: jsonconnector.h flatconnector.h jsonconnector.o: ../../pikotools/textstream/textstream.h jsonconnector.o: ../../pikotools/space/space.h jsonconnector.o: ../../pikotools/textstream/types.h -jsonconnector.o: ../../pikotools/date/date.h jsonconnector.o: ../../pikotools/convert/inttostr.h +jsonconnector.o: ../../pikotools/date/date.h jsonconnector.o: ../../pikotools/membuffer/membuffer.h jsonconnector.o: ../../pikotools/textstream/types.h jsonexpression.h jsonconnector.o: flatexpression.h baseexpression.h morm_types.h modelenv.h @@ -89,8 +94,8 @@ jsonexpression.o: jsonexpression.h flatexpression.h baseexpression.h jsonexpression.o: ../../pikotools/textstream/textstream.h jsonexpression.o: ../../pikotools/space/space.h jsonexpression.o: ../../pikotools/textstream/types.h -jsonexpression.o: ../../pikotools/date/date.h jsonexpression.o: ../../pikotools/convert/inttostr.h +jsonexpression.o: ../../pikotools/date/date.h jsonexpression.o: ../../pikotools/membuffer/membuffer.h jsonexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h jsonexpression.o: modeldata.h cursorhelper.h queryresult.h @@ -98,7 +103,7 @@ jsonexpression.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h jsonexpression.o: finderhelper.h fieldvaluehelper.h ft.h model.o: model.h ../../pikotools/textstream/textstream.h model.o: ../../pikotools/space/space.h ../../pikotools/textstream/types.h -model.o: ../../pikotools/date/date.h ../../pikotools/convert/inttostr.h +model.o: ../../pikotools/convert/inttostr.h ../../pikotools/date/date.h model.o: ../../pikotools/membuffer/membuffer.h model.o: ../../pikotools/textstream/types.h modelconnector.h clearer.h model.o: dbconnector.h ../../pikotools/log/log.h @@ -119,13 +124,15 @@ postgresqlconnector.o: postgresqlconnector.h dbconnector.h postgresqlconnector.o: ../../pikotools/textstream/textstream.h postgresqlconnector.o: ../../pikotools/space/space.h postgresqlconnector.o: ../../pikotools/textstream/types.h -postgresqlconnector.o: ../../pikotools/date/date.h postgresqlconnector.o: ../../pikotools/convert/inttostr.h +postgresqlconnector.o: ../../pikotools/date/date.h postgresqlconnector.o: ../../pikotools/membuffer/membuffer.h postgresqlconnector.o: ../../pikotools/textstream/types.h postgresqlconnector.o: ../../pikotools/log/log.h postgresqlconnector.o: ../../pikotools/log/filelog.h queryresult.h postgresqlconnector.o: postgresqlqueryresult.h ../../pikotools/utf8/utf8.h +postgresqlconnector.o: ../../pikotools/utf8/utf8_templates.h +postgresqlconnector.o: ../../pikotools/utf8/utf8_private.h postgresqlconnector.o: postgresqlexpression.h dbexpression.h baseexpression.h postgresqlconnector.o: morm_types.h modelenv.h modeldata.h cursorhelper.h postgresqlconnector.o: finderhelper.h fieldvaluehelper.h ft.h @@ -137,8 +144,8 @@ postgresqlexpression.o: baseexpression.h postgresqlexpression.o: ../../pikotools/textstream/textstream.h postgresqlexpression.o: ../../pikotools/space/space.h postgresqlexpression.o: ../../pikotools/textstream/types.h -postgresqlexpression.o: ../../pikotools/date/date.h postgresqlexpression.o: ../../pikotools/convert/inttostr.h +postgresqlexpression.o: ../../pikotools/date/date.h postgresqlexpression.o: ../../pikotools/membuffer/membuffer.h postgresqlexpression.o: ../../pikotools/textstream/types.h morm_types.h postgresqlexpression.o: modelenv.h modeldata.h cursorhelper.h queryresult.h @@ -150,16 +157,18 @@ postgresqlqueryresult.o: ../../pikotools/log/log.h postgresqlqueryresult.o: ../../pikotools/textstream/textstream.h postgresqlqueryresult.o: ../../pikotools/space/space.h postgresqlqueryresult.o: ../../pikotools/textstream/types.h -postgresqlqueryresult.o: ../../pikotools/date/date.h postgresqlqueryresult.o: ../../pikotools/convert/inttostr.h +postgresqlqueryresult.o: ../../pikotools/date/date.h postgresqlqueryresult.o: ../../pikotools/membuffer/membuffer.h postgresqlqueryresult.o: ../../pikotools/textstream/types.h postgresqlqueryresult.o: ../../pikotools/log/filelog.h queryresult.o: queryresult.h ../../pikotools/log/log.h queryresult.o: ../../pikotools/textstream/textstream.h queryresult.o: ../../pikotools/space/space.h -queryresult.o: ../../pikotools/textstream/types.h ../../pikotools/date/date.h -queryresult.o: ../../pikotools/convert/inttostr.h +queryresult.o: ../../pikotools/textstream/types.h +queryresult.o: ../../pikotools/convert/inttostr.h ../../pikotools/date/date.h queryresult.o: ../../pikotools/membuffer/membuffer.h queryresult.o: ../../pikotools/textstream/types.h queryresult.o: ../../pikotools/log/filelog.h ../../pikotools/utf8/utf8.h +queryresult.o: ../../pikotools/utf8/utf8_templates.h +queryresult.o: ../../pikotools/utf8/utf8_private.h From d78aa325d3df86b8d2631884c3ff6b84585a2fbf Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 8 Apr 2021 17:19:52 +0200 Subject: [PATCH 06/27] fixed: prepare_to_select() should initialize 'model' but it was initialized only in select() --- src/finder.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/finder.h b/src/finder.h index b67012e..8c8c5dc 100644 --- a/src/finder.h +++ b/src/finder.h @@ -173,6 +173,17 @@ public: last_query_error = L"model connector object is required"; } + model.set_connector(model_connector); + + finder_helper.clear(); + model_env.clear(); + + model.model_env = &model_env; + model.model_env->model_data = model_data; + model.model_env->finder_helper = &finder_helper; + model.prepare_table(); + model.model_env->add_table_name_to_finder_helper(); + return *this; } @@ -219,17 +230,6 @@ public: prepare_to_select(); } - model.set_connector(model_connector); - - finder_helper.clear(); - model_env.clear(); - - model.model_env = &model_env; - model.model_env->model_data = model_data; - model.model_env->finder_helper = &finder_helper; - model.prepare_table(); - model.model_env->add_table_name_to_finder_helper(); - has_autogenerated_select = true; if( model_connector && out_stream && db_expression ) From c85165b44222ff7e15e8771cdac5e8755131e4b5 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 8 Apr 2021 17:21:12 +0200 Subject: [PATCH 07/27] updated to the new api of Space from pikotools --- src/baseexpression.cpp | 7 +------ src/clearer.cpp | 2 +- src/dbconnector.cpp | 6 +++--- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index be1f0a3..d658d9e 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -566,12 +566,7 @@ void BaseExpression::esc(const PT::Space & space, PT::TextStream & stream) { PT::WTextStream tmp_stream; - if( space.table.size() > 0 ) - { - tmp_stream.clear(); - } - - space.Serialize(tmp_stream, true); + space.serialize_to_space_stream(tmp_stream, true); esc(tmp_stream, stream); } diff --git a/src/clearer.cpp b/src/clearer.cpp index 3075547..a50c02e 100644 --- a/src/clearer.cpp +++ b/src/clearer.cpp @@ -141,7 +141,7 @@ void Clearer::clear_value(PT::Date & field_value) void Clearer::clear_value(PT::Space & field_value) { - field_value.Clear(); + field_value.clear(); } void Clearer::clear_model(Model & field_value) diff --git a/src/dbconnector.cpp b/src/dbconnector.cpp index b19c040..5ac3d06 100644 --- a/src/dbconnector.cpp +++ b/src/dbconnector.cpp @@ -430,16 +430,16 @@ void DbConnector::get_value(const char * value_str, PT::Date & field_value) void DbConnector::get_value(const char * value_str, PT::Space & field_value) { - field_value.Clear(); + field_value.clear(); if( *value_str != '\0' ) { PT::SpaceParser space_parser; space_parser.SetSpace(field_value); - if( space_parser.ParseString(value_str) != PT::SpaceParser::ok ) + if( space_parser.ParseSpace(value_str) != PT::SpaceParser::ok ) { - field_value.Clear(); + field_value.clear(); if( log ) { From 79fd642ef7d7edf7cdb7ea6295b2ac0e0179dce3 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Fri, 9 Apr 2021 17:50:23 +0200 Subject: [PATCH 08/27] make depend --- samples/Makefile.dep | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/samples/Makefile.dep b/samples/Makefile.dep index 1155a2c..dfcdfb0 100644 --- a/samples/Makefile.dep +++ b/samples/Makefile.dep @@ -2,11 +2,10 @@ main.o: ../../pikotools/mainspaceparser/mainspaceparser.h main.o: ../../pikotools/space/space.h ../../pikotools/textstream/types.h -main.o: sample01.h basesample.h ../../morm/src/morm.h -main.o: ../../morm/src/morm_types.h ../../morm/src/model.h -main.o: ../../pikotools/textstream/textstream.h ../../pikotools/date/date.h -main.o: ../../pikotools/convert/inttostr.h -main.o: ../../pikotools/membuffer/membuffer.h +main.o: ../../pikotools/convert/inttostr.h sample01.h basesample.h +main.o: ../../morm/src/morm.h ../../morm/src/morm_types.h +main.o: ../../morm/src/model.h ../../pikotools/textstream/textstream.h +main.o: ../../pikotools/date/date.h ../../pikotools/membuffer/membuffer.h main.o: ../../pikotools/textstream/types.h ../../morm/src/modelconnector.h main.o: ../../morm/src/clearer.h ../../morm/src/dbconnector.h main.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h @@ -16,7 +15,8 @@ main.o: ../../morm/src/modelenv.h ../../morm/src/modeldata.h main.o: ../../morm/src/cursorhelper.h ../../morm/src/finderhelper.h main.o: ../../morm/src/fieldvaluehelper.h ../../morm/src/ft.h main.o: ../../morm/src/flatexpression.h ../../morm/src/finder.h -main.o: ../../pikotools/utf8/utf8.h ../../morm/src/cursor.h +main.o: ../../pikotools/utf8/utf8.h ../../pikotools/utf8/utf8_templates.h +main.o: ../../pikotools/utf8/utf8_private.h ../../morm/src/cursor.h main.o: ../../morm/src/jsonexpression.h ../../morm/src/postgresqlexpression.h main.o: ../../morm/src/jsonconnector.h ../../morm/src/postgresqlconnector.h main.o: ../../morm/src/postgresqlqueryresult.h person.h language.h From 9a4fd9b050763e88aa365277b999d3ffe19efc13 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Mon, 12 Apr 2021 18:53:55 +0200 Subject: [PATCH 09/27] fixed: add_field_for_select from Model incorrectly escaped a field string (column name) fixed: Finder didn't use full table name (schema.table) in "from" clause --- src/baseexpression.cpp | 19 +++++++++++++------ src/baseexpression.h | 22 ++++++++++------------ src/dbexpression.h | 6 ++---- src/finder.h | 10 +++++++++- src/ft.h | 7 +++++++ src/model.h | 10 ++++++---- 6 files changed, 47 insertions(+), 27 deletions(-) diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index d658d9e..d741c87 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -194,12 +194,19 @@ bool BaseExpression::is_long_field_name(const PT::TextStream & field_name) } -void BaseExpression::put_field_name(const wchar_t * field_name, ModelEnv * model_env) +void BaseExpression::put_field_name(const wchar_t * field_name, FT field_type, ModelEnv * model_env) { - if( is_long_field_name(field_name) ) - put_long_field_name(field_name); + if( field_type.is_raw_field_name() ) + { + (*out_stream) << field_name; + } else - put_short_field_name(field_name, model_env); + { + if( is_long_field_name(field_name) ) + put_long_field_name(field_name); + else + put_short_field_name(field_name, model_env); + } } @@ -262,13 +269,13 @@ void BaseExpression::put_short_field_name(const wchar_t * field_name, ModelEnv * } -void BaseExpression::save_foreign_key(const wchar_t * field_name, ModelEnv * model_env) +void BaseExpression::save_foreign_key(const wchar_t * field_name, FT field_type, ModelEnv * model_env) { PT::TextStream str; PT::TextStream * old_out_stream = out_stream; out_stream = &str; - put_field_name(field_name, model_env); + put_field_name(field_name, field_type, model_env); out_stream = old_out_stream; if( model_env && model_env->finder_helper ) diff --git a/src/baseexpression.h b/src/baseexpression.h index 57c2144..872b6b2 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -81,12 +81,12 @@ public: if( work_mode == MORM_WORK_MODE_MODEL_FIELDS ) { - put_field_name(field_name, model_env); + put_field_name(field_name, field_type, model_env); } else if( work_mode == MORM_WORK_MODE_MODEL_SAVE_FIELDS ) { - save_foreign_key(field_name, model_env); + save_foreign_key(field_name, field_type, model_env); } else if( work_mode == MORM_WORK_MODE_MODEL_VALUES ) @@ -100,7 +100,7 @@ public: { if( (size_t)model_env->field_index < model_env->set_field_name_helper->size() ) { - put_field_name((*model_env->set_field_name_helper)[model_env->field_index], model_env); + put_field_name((*model_env->set_field_name_helper)[model_env->field_index], field_type, model_env); put_name_value_separator(); put_field_value_or_null(field_value, field_type, model_env); } @@ -109,7 +109,7 @@ public: } else { - put_field_name(field_name, model_env); + put_field_name(field_name, field_type, model_env); put_name_value_separator(); put_field_value_or_null(field_value, field_type, model_env); } @@ -172,7 +172,7 @@ public: // else if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES ) { - put_field_name(field_name, model_env); + put_field_name(field_name, field_type, model_env); put_name_value_separator(); put_field_value_list(field_value, model_connector, model_env); } @@ -190,7 +190,7 @@ public: if( work_mode == MORM_WORK_MODE_MODEL_FIELDS ) { - put_field_name(field_name, model_env); + put_field_name(field_name, field_type, model_env); } else if( work_mode == MORM_WORK_MODE_MODEL_VALUES ) @@ -200,7 +200,7 @@ public: else if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES ) { - put_field_name(field_name, model_env); + put_field_name(field_name, field_type, model_env); put_name_value_separator(); generate_from_model(field_model); } @@ -274,10 +274,8 @@ protected: virtual void field_before(); virtual void field_after(); - virtual void put_field_name(const wchar_t * field_name, ModelEnv * model_env); - - virtual void save_foreign_key(const wchar_t * field_name, ModelEnv * model_env); - + virtual void put_field_name(const wchar_t * field_name, FT field_type, ModelEnv * model_env); + virtual void save_foreign_key(const wchar_t * field_name, FT field_type, ModelEnv * model_env); virtual void dump_additional_info(Model & model); template @@ -573,7 +571,7 @@ protected: this->out_stream = &stream; field_before(); - put_field_name(field_name, model_env); + put_field_name(field_name, FT::default_type, model_env); put_name_value_separator(); bool is_first = true; diff --git a/src/dbexpression.h b/src/dbexpression.h index 3fbb890..1e6385e 100644 --- a/src/dbexpression.h +++ b/src/dbexpression.h @@ -63,7 +63,7 @@ public: virtual DbExpression & page(PT::TextStream & stream, size_t page_number, size_t page_size); template - void add_field_for_select(const wchar_t * new_column_expression, const wchar_t * new_column_name, FieldValue & field_value) + void add_field_for_select(const wchar_t * new_column_expression, const wchar_t * new_column_name, FieldValue & field_value, FT field_type, ModelEnv * model_env) { std::wstring column_expression; // field() methods can be called recursively, so don't make it as class object @@ -71,9 +71,7 @@ public: column_expression += L" as "; column_expression += new_column_name; - // put nullptr to ModelEnv* to not allow to use prefix - // or may better remember current value of use_prefix and set it to false for a while? - field(column_expression.c_str(), field_value, false, false, false, nullptr); + field(column_expression.c_str(), field_value, field_type, model_env); } diff --git a/src/finder.h b/src/finder.h index 8c8c5dc..89af0cf 100644 --- a/src/finder.h +++ b/src/finder.h @@ -236,7 +236,15 @@ public: { (*out_stream) << "SELECT "; model.generate_select_columns(*out_stream); - (*out_stream) << " FROM " << model.model_env->table_name << " AS "; + (*out_stream) << " FROM "; + + if( !model.model_env->schema_name.empty() ) + { + (*out_stream) << model.model_env->schema_name << "."; + } + + // IMPROVEME escape table name + (*out_stream) << model.model_env->table_name << " AS "; (*out_stream) << model.model_env->table_name; (*out_stream) << " "; (*out_stream) << finder_helper.join_tables_str; diff --git a/src/ft.h b/src/ft.h index 581f9d3..1d884e9 100644 --- a/src/ft.h +++ b/src/ft.h @@ -56,6 +56,7 @@ public: no_insertable = 8, no_updatable = 16, no_fetchable = 32, /* not supported yet */ + raw_field_name = 64, }; /* @@ -130,6 +131,12 @@ public: return !is_flag_set(no_fetchable); } + + bool is_raw_field_name() const + { + return is_flag_set(raw_field_name); + } + }; } diff --git a/src/model.h b/src/model.h index beaad31..f7b84aa 100644 --- a/src/model.h +++ b/src/model.h @@ -1324,6 +1324,8 @@ public: template void add_field_for_select(const wchar_t * new_column_expression, const wchar_t * new_column_name, const wchar_t * flat_field_name, FieldValue & field_value) { + FT field_type = FT::no_insertable | FT::no_updatable | FT::raw_field_name; + if( model_connector && model_env ) { if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_DB_SQL ) @@ -1336,24 +1338,24 @@ public: if( db_expression && !is_empty_field(new_column_expression) ) { - db_expression->add_field_for_select(new_column_expression, new_column_name, field_value); + db_expression->add_field_for_select(new_column_expression, new_column_name, field_value, field_type, model_env); } } } else if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_FLAT_STRING ) { - field_generic(L"", flat_field_name, field_value, false, false, false); + field_generic(L"", flat_field_name, field_value, field_type); } else if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_READING_VALUE_FROM_DB_RESULTSET ) { - field_generic(new_column_name, L"", field_value, false, false, false); + field_generic(new_column_name, L"", field_value, field_type); } else if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_CLEARING_VALUE ) { - field_generic(L"", L"", field_value, false, false, false); // the names don't matter here + field_generic(L"", L"", field_value, field_type); // the names don't matter here } } } From 1c5d32551a8971b137959a40a432536709172a1a Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Wed, 14 Apr 2021 16:21:10 +0200 Subject: [PATCH 10/27] fixed: a null was printed for id when serializing lists/vectors in BaseExpression::put_field_value_list(): has_primary_key_set flag was not copied to model_env --- src/baseexpression.h | 14 +++----------- src/modelenv.h | 8 ++------ 2 files changed, 5 insertions(+), 17 deletions(-) diff --git a/src/baseexpression.h b/src/baseexpression.h index 872b6b2..5a829d4 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -337,16 +337,7 @@ protected: field_value_list_separator(); } - //ModelEnv model_env_local(*model_env); - //m.model_env = &model_env_local; - //before_field_value(field_value); - //m.set_connector(model_connector); - put_field_value(m); - - //generate_from_model(m); - //m.model_env = nullptr; - //after_field_value(field_value); is_first = false; } @@ -372,11 +363,12 @@ protected: ModelEnv model_env_local(*model_env); m.model_env = &model_env_local; - //before_field_value(field_value); + m.model_env->has_primary_key_set = m.get_has_primary_key_set(); m.set_connector(model_connector); + generate_from_model(m); + m.model_env = nullptr; - //after_field_value(field_value); is_first = false; } diff --git a/src/modelenv.h b/src/modelenv.h index 9cb6047..dae9625 100644 --- a/src/modelenv.h +++ b/src/modelenv.h @@ -84,12 +84,8 @@ public: ModelEnv(const ModelEnv & e) { - model_data = e.model_data; - finder_helper = e.finder_helper; - cursor_helper = e.cursor_helper; - model_work_mode = e.model_work_mode; - model_work_submode = e.model_work_submode; - dump_mode = e.dump_mode; + copy_global_objects(e); + table_index = e.table_index; set_field_name_helper = e.set_field_name_helper; field_value_helper_tab = e.field_value_helper_tab; From c87afb40d269f46798ee8df74e5aff5182a651f6 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Wed, 14 Apr 2021 17:46:19 +0200 Subject: [PATCH 11/27] use prepare_table() only with a database and not for flat strings Model::prepare_table() will create a log: "you should provide the table name...." and it is not needed if a Model object is used only for flat strings --- src/model.cpp | 2 +- src/model.h | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/model.cpp b/src/model.cpp index 2317b96..096bf1a 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -200,7 +200,7 @@ void Model::to_text(PT::TextStream & stream, ModelData * model_data, bool clear_ { try { - prepare_table(); + // prepare_table(); at the moment flat strings (json/space) do not need a table name flat_connector->to_text(stream, *this); } catch(...) diff --git a/src/model.h b/src/model.h index f7b84aa..25d95b1 100644 --- a/src/model.h +++ b/src/model.h @@ -969,10 +969,11 @@ protected: field_model.model_env = &model_env_local; field_model.model_env->has_primary_key_set = field_model.has_primary_key_set; field_model.set_connector(model_connector); - field_model.prepare_table(); if( !is_empty_field(db_field_name) ) { + field_model.prepare_table(); + if( field_type.is_foreign_key() || field_type.is_foreign_key_in_child() ) { field_model_for_db(db_field_name, field_model, field_type); @@ -993,6 +994,7 @@ protected: { if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_FLAT_STRING ) { + // calling field_model.prepare_table(); is not needed in generating strings (at least for json/space formats) field_model_generate_flat_string(flat_field_name, field_model, field_type); } } From 2afe111c57d85e4c161386d1b28d79c6ea5be065 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Fri, 30 Apr 2021 01:23:22 +0200 Subject: [PATCH 12/27] escape table names in Finder (select sql statement) WIP: #2 --- src/baseexpression.h | 65 ++++++++++++++++++++++++++++++++++++ src/finder.h | 19 +++++++---- src/postgresqlexpression.cpp | 6 ++-- 3 files changed, 82 insertions(+), 8 deletions(-) diff --git a/src/baseexpression.h b/src/baseexpression.h index 5a829d4..c5e00c0 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -121,6 +121,8 @@ public: } + + template void put_field_value_or_null(const FieldValue & field_value, FT field_type, ModelEnv * model_env) { @@ -218,6 +220,69 @@ public: } + void table_to_stream(PT::TextStream & stream, const wchar_t * schema_name, const wchar_t * table_name, ModelEnv * model_env) + { + this->out_stream = &stream; + //table(table_name, model_env); + + before_first_part_long_field_name(); + esc(schema_name, *out_stream); + after_first_part_long_field_name(); + + (*out_stream) << '.'; + + before_second_part_long_field_name(); + esc(table_name, *out_stream); + after_second_part_long_field_name(); + + this->out_stream = nullptr; + } + + void table_to_stream(PT::TextStream & stream, const PT::WTextStream & schema_name, const PT::WTextStream & table_name, ModelEnv * model_env) + { + this->out_stream = &stream; + //table(table_name, model_env); + + before_first_part_long_field_name(); + esc(schema_name, *out_stream); + after_first_part_long_field_name(); + + (*out_stream) << '.'; + + before_second_part_long_field_name(); + esc(table_name, *out_stream); + after_second_part_long_field_name(); + + this->out_stream = nullptr; + } + + void table_to_stream(PT::TextStream & stream, const wchar_t * table_name, ModelEnv * model_env) + { + this->out_stream = &stream; + //table(table_name, model_env); + + if( is_long_field_name(table_name) ) + put_long_field_name(table_name); + else + put_short_field_name(table_name, model_env); + + this->out_stream = nullptr; + } + + + void table_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, ModelEnv * model_env) + { + this->out_stream = &stream; + //table(table_name, model_env); + + before_short_field_name(); + esc(table_name, *out_stream); + after_short_field_name(); + + this->out_stream = nullptr; + } + + /* * IMPLEMENT ME * esc for: signed char, wchar_t, char16_t, char32_t diff --git a/src/finder.h b/src/finder.h index 89af0cf..8d936b7 100644 --- a/src/finder.h +++ b/src/finder.h @@ -240,14 +240,21 @@ public: if( !model.model_env->schema_name.empty() ) { - (*out_stream) << model.model_env->schema_name << "."; + db_expression->table_to_stream(*out_stream, model.model_env->schema_name, model.model_env->table_name, &model_env); + } + else + { + db_expression->table_to_stream(*out_stream, model.model_env->table_name, &model_env); } - // IMPROVEME escape table name - (*out_stream) << model.model_env->table_name << " AS "; - (*out_stream) << model.model_env->table_name; - (*out_stream) << " "; - (*out_stream) << finder_helper.join_tables_str; + (*out_stream) << " AS "; + db_expression->table_to_stream(*out_stream, model.model_env->table_name, &model_env); + + if( !finder_helper.join_tables_str.empty() ) + { + (*out_stream) << " "; + (*out_stream) << finder_helper.join_tables_str; + } } return *this; diff --git a/src/postgresqlexpression.cpp b/src/postgresqlexpression.cpp index 5a5b2d7..4da9ef6 100644 --- a/src/postgresqlexpression.cpp +++ b/src/postgresqlexpression.cpp @@ -53,23 +53,25 @@ void PostgreSQLExpression::after_short_field_name() void PostgreSQLExpression::before_first_part_long_field_name() { + (*out_stream) << '"'; } void PostgreSQLExpression::after_first_part_long_field_name() { + (*out_stream) << '"'; } void PostgreSQLExpression::before_second_part_long_field_name() { - before_short_field_name(); + (*out_stream) << '"'; } void PostgreSQLExpression::after_second_part_long_field_name() { - after_short_field_name(); + (*out_stream) << '"'; } From 009955a0fd83d1c9ddcf1b88660047686224afaf Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Tue, 11 May 2021 22:11:31 +0200 Subject: [PATCH 13/27] added support for hex strings and binary strings added FT::hexadecimal, FT::binary and FT::dont_use_utf8 --- src/Makefile.dep | 205 +++++---------------- src/baseexpression.cpp | 216 ++++++++++++++-------- src/baseexpression.h | 104 ++++++----- src/clearer.cpp | 4 + src/clearer.h | 1 + src/dbconnector.cpp | 334 +++++++++++++++++++++++++++++++---- src/dbconnector.h | 56 ++++-- src/dbexpression.cpp | 4 +- src/dbexpression.h | 7 +- src/flatexpression.cpp | 2 +- src/flatexpression.h | 2 +- src/ft.h | 19 ++ src/jsonexpression.cpp | 56 ++++-- src/jsonexpression.h | 6 +- src/model.h | 14 +- src/postgresqlconnector.cpp | 171 +++++------------- src/postgresqlconnector.h | 7 +- src/postgresqlexpression.cpp | 41 +++-- src/postgresqlexpression.h | 10 +- 19 files changed, 744 insertions(+), 515 deletions(-) diff --git a/src/Makefile.dep b/src/Makefile.dep index 9a673e1..6940b12 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep @@ -1,174 +1,49 @@ # DO NOT DELETE -baseexpression.o: baseexpression.h ../../pikotools/textstream/textstream.h -baseexpression.o: ../../pikotools/space/space.h -baseexpression.o: ../../pikotools/textstream/types.h -baseexpression.o: ../../pikotools/convert/inttostr.h -baseexpression.o: ../../pikotools/date/date.h -baseexpression.o: ../../pikotools/membuffer/membuffer.h -baseexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h -baseexpression.o: modeldata.h cursorhelper.h queryresult.h -baseexpression.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h -baseexpression.o: finderhelper.h fieldvaluehelper.h ft.h model.h -baseexpression.o: modelconnector.h clearer.h dbconnector.h flatconnector.h -baseexpression.o: dbexpression.h flatexpression.h ../../pikotools/utf8/utf8.h -baseexpression.o: ../../pikotools/utf8/utf8_templates.h -baseexpression.o: ../../pikotools/utf8/utf8_private.h -clearer.o: clearer.h ../../pikotools/date/date.h -clearer.o: ../../pikotools/convert/inttostr.h ../../pikotools/space/space.h -clearer.o: ../../pikotools/textstream/types.h model.h -clearer.o: ../../pikotools/textstream/textstream.h -clearer.o: ../../pikotools/membuffer/membuffer.h -clearer.o: ../../pikotools/textstream/types.h modelconnector.h dbconnector.h -clearer.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h -clearer.o: queryresult.h flatconnector.h dbexpression.h baseexpression.h -clearer.o: morm_types.h modelenv.h modeldata.h cursorhelper.h finderhelper.h -clearer.o: fieldvaluehelper.h ft.h flatexpression.h -dbconnector.o: ../../pikotools/space/spaceparser.h -dbconnector.o: ../../pikotools/space/space.h -dbconnector.o: ../../pikotools/textstream/types.h -dbconnector.o: ../../pikotools/convert/inttostr.h dbconnector.h -dbconnector.o: ../../pikotools/textstream/textstream.h -dbconnector.o: ../../pikotools/space/space.h ../../pikotools/date/date.h -dbconnector.o: ../../pikotools/membuffer/membuffer.h -dbconnector.o: ../../pikotools/textstream/types.h ../../pikotools/log/log.h -dbconnector.o: ../../pikotools/log/filelog.h queryresult.h dbexpression.h +baseexpression.o: baseexpression.h morm_types.h modelenv.h modeldata.h +baseexpression.o: cursorhelper.h queryresult.h finderhelper.h +baseexpression.o: fieldvaluehelper.h ft.h model.h modelconnector.h clearer.h +baseexpression.o: dbconnector.h flatconnector.h dbexpression.h +baseexpression.o: flatexpression.h +clearer.o: clearer.h model.h modelconnector.h dbconnector.h queryresult.h +clearer.o: ft.h flatconnector.h dbexpression.h baseexpression.h morm_types.h +clearer.o: modelenv.h modeldata.h cursorhelper.h finderhelper.h +clearer.o: fieldvaluehelper.h flatexpression.h +dbconnector.o: dbconnector.h queryresult.h ft.h dbexpression.h dbconnector.o: baseexpression.h morm_types.h modelenv.h modeldata.h -dbconnector.o: cursorhelper.h finderhelper.h fieldvaluehelper.h ft.h model.h +dbconnector.o: cursorhelper.h finderhelper.h fieldvaluehelper.h model.h dbconnector.o: modelconnector.h clearer.h flatconnector.h flatexpression.h -dbconnector.o: ../../pikotools/utf8/utf8.h -dbconnector.o: ../../pikotools/utf8/utf8_templates.h -dbconnector.o: ../../pikotools/utf8/utf8_private.h -dbconnector.o: ../../pikotools/convert/convert.h -dbconnector.o: ../../pikotools/convert/inttostr.h -dbconnector.o: ../../pikotools/convert/patternreplacer.h -dbconnector.o: ../../pikotools/convert/strtoint.h -dbconnector.o: ../../pikotools/convert/text.h ../../pikotools/convert/misc.h -dbexpression.o: dbexpression.h baseexpression.h -dbexpression.o: ../../pikotools/textstream/textstream.h -dbexpression.o: ../../pikotools/space/space.h -dbexpression.o: ../../pikotools/textstream/types.h -dbexpression.o: ../../pikotools/convert/inttostr.h -dbexpression.o: ../../pikotools/date/date.h -dbexpression.o: ../../pikotools/membuffer/membuffer.h -dbexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h -dbexpression.o: modeldata.h cursorhelper.h queryresult.h -dbexpression.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h -dbexpression.o: finderhelper.h fieldvaluehelper.h ft.h -flatconnector.o: flatconnector.h ../../pikotools/textstream/textstream.h -flatconnector.o: ../../pikotools/space/space.h -flatconnector.o: ../../pikotools/textstream/types.h -flatconnector.o: ../../pikotools/convert/inttostr.h -flatconnector.o: ../../pikotools/date/date.h -flatconnector.o: ../../pikotools/membuffer/membuffer.h -flatconnector.o: ../../pikotools/textstream/types.h flatexpression.h -flatconnector.o: baseexpression.h morm_types.h modelenv.h modeldata.h -flatconnector.o: cursorhelper.h queryresult.h ../../pikotools/log/log.h -flatconnector.o: ../../pikotools/log/filelog.h finderhelper.h -flatconnector.o: fieldvaluehelper.h ft.h model.h modelconnector.h clearer.h -flatconnector.o: dbconnector.h dbexpression.h -flatexpression.o: flatexpression.h baseexpression.h -flatexpression.o: ../../pikotools/textstream/textstream.h -flatexpression.o: ../../pikotools/space/space.h -flatexpression.o: ../../pikotools/textstream/types.h -flatexpression.o: ../../pikotools/convert/inttostr.h -flatexpression.o: ../../pikotools/date/date.h -flatexpression.o: ../../pikotools/membuffer/membuffer.h -flatexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h -flatexpression.o: modeldata.h cursorhelper.h queryresult.h -flatexpression.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h -flatexpression.o: finderhelper.h fieldvaluehelper.h ft.h -jsonconnector.o: jsonconnector.h flatconnector.h -jsonconnector.o: ../../pikotools/textstream/textstream.h -jsonconnector.o: ../../pikotools/space/space.h -jsonconnector.o: ../../pikotools/textstream/types.h -jsonconnector.o: ../../pikotools/convert/inttostr.h -jsonconnector.o: ../../pikotools/date/date.h -jsonconnector.o: ../../pikotools/membuffer/membuffer.h -jsonconnector.o: ../../pikotools/textstream/types.h jsonexpression.h +dbexpression.o: dbexpression.h baseexpression.h morm_types.h modelenv.h +dbexpression.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h +dbexpression.o: fieldvaluehelper.h ft.h +flatconnector.o: flatconnector.h flatexpression.h baseexpression.h +flatconnector.o: morm_types.h modelenv.h modeldata.h cursorhelper.h +flatconnector.o: queryresult.h finderhelper.h fieldvaluehelper.h ft.h model.h +flatconnector.o: modelconnector.h clearer.h dbconnector.h dbexpression.h +flatexpression.o: flatexpression.h baseexpression.h morm_types.h modelenv.h +flatexpression.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h +flatexpression.o: fieldvaluehelper.h ft.h +jsonconnector.o: jsonconnector.h flatconnector.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/log/log.h ../../pikotools/log/filelog.h -jsonconnector.o: finderhelper.h fieldvaluehelper.h ft.h +jsonconnector.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h +jsonconnector.o: fieldvaluehelper.h ft.h jsonexpression.o: jsonexpression.h flatexpression.h baseexpression.h -jsonexpression.o: ../../pikotools/textstream/textstream.h -jsonexpression.o: ../../pikotools/space/space.h -jsonexpression.o: ../../pikotools/textstream/types.h -jsonexpression.o: ../../pikotools/convert/inttostr.h -jsonexpression.o: ../../pikotools/date/date.h -jsonexpression.o: ../../pikotools/membuffer/membuffer.h -jsonexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h -jsonexpression.o: modeldata.h cursorhelper.h queryresult.h -jsonexpression.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h -jsonexpression.o: finderhelper.h fieldvaluehelper.h ft.h -model.o: model.h ../../pikotools/textstream/textstream.h -model.o: ../../pikotools/space/space.h ../../pikotools/textstream/types.h -model.o: ../../pikotools/convert/inttostr.h ../../pikotools/date/date.h -model.o: ../../pikotools/membuffer/membuffer.h -model.o: ../../pikotools/textstream/types.h modelconnector.h clearer.h -model.o: dbconnector.h ../../pikotools/log/log.h -model.o: ../../pikotools/log/filelog.h queryresult.h flatconnector.h -model.o: dbexpression.h baseexpression.h morm_types.h modelenv.h modeldata.h -model.o: cursorhelper.h finderhelper.h fieldvaluehelper.h ft.h -model.o: flatexpression.h -modelconnector.o: modelconnector.h clearer.h ../../pikotools/date/date.h -modelconnector.o: ../../pikotools/convert/inttostr.h -modelconnector.o: ../../pikotools/space/space.h -modelconnector.o: ../../pikotools/textstream/types.h dbconnector.h -modelconnector.o: ../../pikotools/textstream/textstream.h -modelconnector.o: ../../pikotools/membuffer/membuffer.h -modelconnector.o: ../../pikotools/textstream/types.h -modelconnector.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h -modelconnector.o: queryresult.h flatconnector.h -postgresqlconnector.o: postgresqlconnector.h dbconnector.h -postgresqlconnector.o: ../../pikotools/textstream/textstream.h -postgresqlconnector.o: ../../pikotools/space/space.h -postgresqlconnector.o: ../../pikotools/textstream/types.h -postgresqlconnector.o: ../../pikotools/convert/inttostr.h -postgresqlconnector.o: ../../pikotools/date/date.h -postgresqlconnector.o: ../../pikotools/membuffer/membuffer.h -postgresqlconnector.o: ../../pikotools/textstream/types.h -postgresqlconnector.o: ../../pikotools/log/log.h -postgresqlconnector.o: ../../pikotools/log/filelog.h queryresult.h -postgresqlconnector.o: postgresqlqueryresult.h ../../pikotools/utf8/utf8.h -postgresqlconnector.o: ../../pikotools/utf8/utf8_templates.h -postgresqlconnector.o: ../../pikotools/utf8/utf8_private.h -postgresqlconnector.o: postgresqlexpression.h dbexpression.h baseexpression.h -postgresqlconnector.o: morm_types.h modelenv.h modeldata.h cursorhelper.h -postgresqlconnector.o: finderhelper.h fieldvaluehelper.h ft.h -postgresqlconnector.o: ../../pikotools/convert/strtoint.h -postgresqlconnector.o: ../../pikotools/convert/text.h -postgresqlconnector.o: ../../pikotools/convert/misc.h +jsonexpression.o: morm_types.h modelenv.h modeldata.h cursorhelper.h +jsonexpression.o: queryresult.h finderhelper.h fieldvaluehelper.h ft.h +model.o: model.h modelconnector.h clearer.h dbconnector.h queryresult.h ft.h +model.o: flatconnector.h dbexpression.h baseexpression.h morm_types.h +model.o: modelenv.h modeldata.h cursorhelper.h finderhelper.h +model.o: fieldvaluehelper.h flatexpression.h +modelconnector.o: modelconnector.h clearer.h dbconnector.h queryresult.h ft.h +modelconnector.o: flatconnector.h +postgresqlconnector.o: postgresqlconnector.h dbconnector.h queryresult.h ft.h +postgresqlconnector.o: postgresqlqueryresult.h postgresqlexpression.h +postgresqlconnector.o: dbexpression.h baseexpression.h morm_types.h +postgresqlconnector.o: modelenv.h modeldata.h cursorhelper.h finderhelper.h +postgresqlconnector.o: fieldvaluehelper.h postgresqlexpression.o: postgresqlexpression.h dbexpression.h -postgresqlexpression.o: baseexpression.h -postgresqlexpression.o: ../../pikotools/textstream/textstream.h -postgresqlexpression.o: ../../pikotools/space/space.h -postgresqlexpression.o: ../../pikotools/textstream/types.h -postgresqlexpression.o: ../../pikotools/convert/inttostr.h -postgresqlexpression.o: ../../pikotools/date/date.h -postgresqlexpression.o: ../../pikotools/membuffer/membuffer.h -postgresqlexpression.o: ../../pikotools/textstream/types.h morm_types.h -postgresqlexpression.o: modelenv.h modeldata.h cursorhelper.h queryresult.h -postgresqlexpression.o: ../../pikotools/log/log.h -postgresqlexpression.o: ../../pikotools/log/filelog.h finderhelper.h +postgresqlexpression.o: baseexpression.h morm_types.h modelenv.h modeldata.h +postgresqlexpression.o: cursorhelper.h queryresult.h finderhelper.h postgresqlexpression.o: fieldvaluehelper.h ft.h postgresqlqueryresult.o: postgresqlqueryresult.h queryresult.h -postgresqlqueryresult.o: ../../pikotools/log/log.h -postgresqlqueryresult.o: ../../pikotools/textstream/textstream.h -postgresqlqueryresult.o: ../../pikotools/space/space.h -postgresqlqueryresult.o: ../../pikotools/textstream/types.h -postgresqlqueryresult.o: ../../pikotools/convert/inttostr.h -postgresqlqueryresult.o: ../../pikotools/date/date.h -postgresqlqueryresult.o: ../../pikotools/membuffer/membuffer.h -postgresqlqueryresult.o: ../../pikotools/textstream/types.h -postgresqlqueryresult.o: ../../pikotools/log/filelog.h -queryresult.o: queryresult.h ../../pikotools/log/log.h -queryresult.o: ../../pikotools/textstream/textstream.h -queryresult.o: ../../pikotools/space/space.h -queryresult.o: ../../pikotools/textstream/types.h -queryresult.o: ../../pikotools/convert/inttostr.h ../../pikotools/date/date.h -queryresult.o: ../../pikotools/membuffer/membuffer.h -queryresult.o: ../../pikotools/textstream/types.h -queryresult.o: ../../pikotools/log/filelog.h ../../pikotools/utf8/utf8.h -queryresult.o: ../../pikotools/utf8/utf8_templates.h -queryresult.o: ../../pikotools/utf8/utf8_private.h +queryresult.o: queryresult.h diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index d741c87..0c56c0f 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -318,71 +318,95 @@ void BaseExpression::after_second_part_long_field_name() -void BaseExpression::before_field_value(const std::wstring &) +void BaseExpression::before_field_value(const std::wstring &, FT field_type) { - before_field_value_string(); + before_field_value_string(field_type); } -void BaseExpression::before_field_value(const std::string &) +void BaseExpression::before_field_value(const std::string &, FT field_type) { - before_field_value_string(); + before_field_value_string(field_type); } -void BaseExpression::after_field_value(const std::wstring &) +void BaseExpression::after_field_value(const std::wstring &, FT field_type) { - after_field_value_string(); + after_field_value_string(field_type); } -void BaseExpression::after_field_value(const std::string &) +void BaseExpression::after_field_value(const std::string &, FT field_type) { - after_field_value_string(); + after_field_value_string(field_type); } -void BaseExpression::before_field_value(const wchar_t *) +void BaseExpression::before_field_value(const wchar_t *, FT field_type) { - before_field_value_string(); + before_field_value_string(field_type); } -void BaseExpression::after_field_value(const wchar_t *) +void BaseExpression::after_field_value(const wchar_t *, FT field_type) { - after_field_value_string(); + after_field_value_string(field_type); } -void BaseExpression::before_field_value(const char *) +void BaseExpression::before_field_value(const char *, FT field_type) { - before_field_value_string(); + before_field_value_string(field_type); } -void BaseExpression::after_field_value(const char *) +void BaseExpression::after_field_value(const char *, FT field_type) { - after_field_value_string(); + after_field_value_string(field_type); } -void BaseExpression::before_field_value(const PT::Date &) + +void BaseExpression::before_field_value(wchar_t, FT field_type) { - before_field_value_string(); + before_field_value_string(field_type); } -void BaseExpression::after_field_value(const PT::Date &) + +void BaseExpression::after_field_value(wchar_t, FT field_type) { - after_field_value_string(); + after_field_value_string(field_type); } -void BaseExpression::before_field_value(const PT::Space &) + +void BaseExpression::before_field_value(char, FT field_type) { - before_field_value_string(); + before_field_value_string(field_type); } -void BaseExpression::after_field_value(const PT::Space &) + +void BaseExpression::after_field_value(char, FT field_type) { - after_field_value_string(); + after_field_value_string(field_type); +} + +void BaseExpression::before_field_value(const PT::Date &, FT field_type) +{ + before_field_value_string(field_type); +} + +void BaseExpression::after_field_value(const PT::Date &, FT field_type) +{ + after_field_value_string(field_type); +} + +void BaseExpression::before_field_value(const PT::Space &, FT field_type) +{ + before_field_value_string(field_type); +} + +void BaseExpression::after_field_value(const PT::Space &, FT field_type) +{ + after_field_value_string(field_type); } @@ -392,82 +416,120 @@ void BaseExpression::put_name_value_separator() } -void BaseExpression::esc(char val, PT::TextStream & stream) +char BaseExpression::char_to_hex_part(char c) { - stream << val; + if( c < 10 ) + return c + '0'; + + return c - 10 + 'A'; } -void BaseExpression::esc(unsigned char val, PT::TextStream & stream) +void BaseExpression::char_to_hex(char c, PT::TextStream & stream) { - esc(static_cast(val), stream); + stream << char_to_hex_part(((unsigned char)c) >> 4); + stream << char_to_hex_part(((unsigned char)c) & 0x0f); } -void BaseExpression::esc(wchar_t val, PT::TextStream & stream) + + +void BaseExpression::esc(char val, PT::TextStream & stream, FT field_type) { - char utf8_buf[10]; - - size_t utf8_len = PT::IntToUTF8((int)val, utf8_buf, sizeof(utf8_buf)); - - for(size_t a = 0 ; a < utf8_len ; ++a) + if( field_type.is_binary() || field_type.is_hexadecimal() ) { - esc(utf8_buf[a], stream); + char_to_hex(val, stream); + } + else + { + stream << val; } } -void BaseExpression::esc(const std::wstring & val, PT::TextStream & stream) +void BaseExpression::esc(unsigned char val, PT::TextStream & stream, FT field_type) { - char utf8_buf[10]; + esc(static_cast(val), stream, field_type); +} - for(size_t i = 0 ; i < val.size() ; ++i) + +void BaseExpression::esc(wchar_t val, PT::TextStream & stream, FT field_type) +{ + if( field_type.use_utf8() ) { - size_t utf8_len = PT::IntToUTF8((int)val[i], utf8_buf, sizeof(utf8_buf)); + char utf8_buf[10]; + + size_t utf8_len = PT::IntToUTF8((int)val, utf8_buf, sizeof(utf8_buf)); for(size_t a = 0 ; a < utf8_len ; ++a) { - esc(utf8_buf[a], stream); + esc(utf8_buf[a], stream, field_type); + } + } + else + { + esc(static_cast(val), stream, field_type); + } +} + + +void BaseExpression::esc(const wchar_t * val, bool has_known_length, size_t len, PT::TextStream & stream, FT field_type) +{ + if( field_type.use_utf8() ) + { + char utf8_buf[10]; + + for(size_t i = 0 ; has_known_length ? (i < len) : val[i] != 0 ; ++i) + { + size_t utf8_len = PT::IntToUTF8((int)val[i], utf8_buf, sizeof(utf8_buf)); + + for(size_t a = 0 ; a < utf8_len ; ++a) + { + esc(utf8_buf[a], stream, field_type); + } + } + } + else + { + for(size_t i = 0 ; has_known_length ? (i < len) : val[i] != 0 ; ++i) + { + esc(static_cast(val[i]), stream, field_type); } } } -void BaseExpression::esc(const wchar_t * val, PT::TextStream & stream) +void BaseExpression::esc(const std::wstring & val, PT::TextStream & stream, FT field_type) { - char utf8_buf[10]; - - for(size_t i = 0 ; val[i] != 0 ; ++i) - { - size_t utf8_len = PT::IntToUTF8((int)val[i], utf8_buf, sizeof(utf8_buf)); - - for(size_t a = 0 ; a < utf8_len ; ++a) - { - esc(utf8_buf[a], stream); - } - } + esc(val.c_str(), true, val.size(), stream, field_type); } -void BaseExpression::esc(const std::string & val, PT::TextStream & stream) +void BaseExpression::esc(const wchar_t * val, PT::TextStream & stream, FT field_type) +{ + esc(val, false, 0, stream, field_type); +} + + +void BaseExpression::esc(const std::string & val, PT::TextStream & stream, FT field_type) { for(size_t i = 0 ; i < val.size() ; ++i) { - esc(val[i], stream); + esc(val[i], stream, field_type); } } -void BaseExpression::esc(const char * val, PT::TextStream & stream) +void BaseExpression::esc(const char * val, PT::TextStream & stream, FT field_type) { for(size_t i = 0 ; val[i] != 0 ; ++i) { - esc(val[i], stream); + esc(val[i], stream, field_type); } } -void BaseExpression::esc(bool val, PT::TextStream & stream) +void BaseExpression::esc(bool val, PT::TextStream & stream, FT field_type) { if( val ) stream << "true"; @@ -476,105 +538,105 @@ void BaseExpression::esc(bool val, PT::TextStream & stream) } -void BaseExpression::esc(short val, PT::TextStream & stream) +void BaseExpression::esc(short val, PT::TextStream & stream, FT field_type) { stream << val; } -void BaseExpression::esc(unsigned short val, PT::TextStream & stream) +void BaseExpression::esc(unsigned short val, PT::TextStream & stream, FT field_type) { stream << val; } -void BaseExpression::esc(int val, PT::TextStream & stream) +void BaseExpression::esc(int val, PT::TextStream & stream, FT field_type) { stream << val; } -void BaseExpression::esc(unsigned int val, PT::TextStream & stream) +void BaseExpression::esc(unsigned int val, PT::TextStream & stream, FT field_type) { stream << val; } -void BaseExpression::esc(long val, PT::TextStream & stream) +void BaseExpression::esc(long val, PT::TextStream & stream, FT field_type) { stream << val; } -void BaseExpression::esc(unsigned long val, PT::TextStream & stream) +void BaseExpression::esc(unsigned long val, PT::TextStream & stream, FT field_type) { stream << val; } -void BaseExpression::esc(long long val, PT::TextStream & stream) +void BaseExpression::esc(long long val, PT::TextStream & stream, FT field_type) { // not implemented in PT::TextStream yet //stream << val; } -void BaseExpression::esc(unsigned long long val, PT::TextStream & stream) +void BaseExpression::esc(unsigned long long val, PT::TextStream & stream, FT field_type) { //stream << val; } -void BaseExpression::esc(float val, PT::TextStream & stream) +void BaseExpression::esc(float val, PT::TextStream & stream, FT field_type) { stream << val; } -void BaseExpression::esc(double val, PT::TextStream & stream) +void BaseExpression::esc(double val, PT::TextStream & stream, FT field_type) { stream << val; } -void BaseExpression::esc(long double val, PT::TextStream & stream) +void BaseExpression::esc(long double val, PT::TextStream & stream, FT field_type) { // IMPLEMENT ME in PT::TextStream //stream << val; } -void BaseExpression::esc(const PT::Date & date, PT::TextStream & stream) +void BaseExpression::esc(const PT::Date & date, PT::TextStream & stream, FT field_type) { stream << date; } -void BaseExpression::esc(const PT::TextStream & val, PT::TextStream & stream) +void BaseExpression::esc(const PT::TextStream & val, PT::TextStream & stream, FT field_type) { PT::TextStream::const_iterator i = val.begin(); for(; i != val.end() ; ++i) { - esc(*i, stream); + esc(*i, stream, field_type); } } -void BaseExpression::esc(const PT::WTextStream & val, PT::TextStream & stream) +void BaseExpression::esc(const PT::WTextStream & val, PT::TextStream & stream, FT field_type) { PT::WTextStream::const_iterator i = val.begin(); for(; i != val.end() ; ++i) { - esc(*i, stream); + esc(*i, stream, field_type); } } -void BaseExpression::esc(const PT::Space & space, PT::TextStream & stream) +void BaseExpression::esc(const PT::Space & space, PT::TextStream & stream, FT field_type) { PT::WTextStream tmp_stream; space.serialize_to_space_stream(tmp_stream, true); - esc(tmp_stream, stream); + esc(tmp_stream, stream, field_type); } @@ -687,11 +749,11 @@ void BaseExpression::esc(const PT::Space & space, PT::TextStream & stream) //} -void BaseExpression::before_field_value_string() +void BaseExpression::before_field_value_string(FT field_type) { } -void BaseExpression::after_field_value_string() +void BaseExpression::after_field_value_string(FT field_type) { } diff --git a/src/baseexpression.h b/src/baseexpression.h index c5e00c0..e06758c 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -129,13 +129,13 @@ public: if( field_type.is_primary_key() ) { if( model_env && model_env->has_primary_key_set ) - put_field_value(field_value); + put_field_value(field_value, field_type); else put_null_value(); } else { - put_field_value(field_value); + put_field_value(field_value, field_type); } } @@ -288,35 +288,35 @@ public: * esc for: signed char, wchar_t, char16_t, char32_t * */ - virtual void esc(char val, PT::TextStream & stream); - virtual void esc(unsigned char val, PT::TextStream & stream); + virtual void esc(char val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(unsigned char val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(wchar_t val, PT::TextStream & stream); + virtual void esc(wchar_t val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(const std::wstring & val, PT::TextStream & stream); - virtual void esc(const wchar_t * val, PT::TextStream & stream); + virtual void esc(const std::wstring & val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(const wchar_t * val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(const std::string & val, PT::TextStream & stream); - virtual void esc(const char * val, PT::TextStream & stream); + virtual void esc(const std::string & val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(const char * val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(bool val, PT::TextStream & stream); - virtual void esc(short val, PT::TextStream & stream); - virtual void esc(unsigned short val, PT::TextStream & stream); - virtual void esc(int val, PT::TextStream & stream); - virtual void esc(unsigned int val, PT::TextStream & stream); - virtual void esc(long val, PT::TextStream & stream); - virtual void esc(unsigned long val, PT::TextStream & stream); - virtual void esc(long long val, PT::TextStream & stream); - virtual void esc(unsigned long long val, PT::TextStream & stream); - virtual void esc(float val, PT::TextStream & stream); - virtual void esc(double val, PT::TextStream & stream); - virtual void esc(long double val, PT::TextStream & stream); + virtual void esc(bool val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(short val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(unsigned short val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(int val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(unsigned int val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(long val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(unsigned long val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(long long val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(unsigned long long val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(float val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(double val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(long double val, PT::TextStream & stream, FT field_type = FT::default_type); //virtual void esc(void* val, PT::TextStream & stream); - virtual void esc(const PT::Date & date, PT::TextStream & stream); - virtual void esc(const PT::TextStream & val,PT::TextStream & stream); - virtual void esc(const PT::WTextStream & val,PT::TextStream & stream); - virtual void esc(const PT::Space & space, PT::TextStream & stream); + virtual void esc(const PT::Date & date, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(const PT::TextStream & val,PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(const PT::WTextStream & val,PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(const PT::Space & space, PT::TextStream & stream, FT field_type = FT::default_type); virtual bool is_long_field_name(const wchar_t * field_name); virtual bool is_long_field_name(const PT::TextStream & table_name); @@ -344,13 +344,13 @@ protected: virtual void dump_additional_info(Model & model); template - void put_field_value(const FieldValue & field_value) + void put_field_value(const FieldValue & field_value, FT field_type) { if( out_stream ) { - before_field_value(field_value); - esc(field_value, *out_stream); - after_field_value(field_value); + before_field_value(field_value, field_type); + esc(field_value, *out_stream, field_type); + after_field_value(field_value, field_type); } } @@ -402,7 +402,7 @@ protected: field_value_list_separator(); } - put_field_value(m); + put_field_value(m, FT::default_type); is_first = false; } @@ -641,7 +641,7 @@ protected: field_value_list_separator(); } - put_field_value(v); + put_field_value(v, FT::default_type); is_first = false; } @@ -672,31 +672,37 @@ protected: virtual void put_long_field_name(const wchar_t * field_name); virtual void put_short_field_name(const wchar_t * field_name, ModelEnv * model_env); - virtual void before_field_value(const std::wstring &); - virtual void after_field_value(const std::wstring &); + virtual void before_field_value(const std::wstring &, FT field_type); + virtual void after_field_value(const std::wstring &, FT field_type); - virtual void before_field_value(const std::string &); - virtual void after_field_value(const std::string &); + virtual void before_field_value(const std::string &, FT field_type); + virtual void after_field_value(const std::string &, FT field_type); - virtual void before_field_value(const wchar_t *); - virtual void after_field_value(const wchar_t *); + virtual void before_field_value(const wchar_t *, FT field_type); + virtual void after_field_value(const wchar_t *, FT field_type); - virtual void before_field_value(const char *); - virtual void after_field_value(const char *); + virtual void before_field_value(const char *, FT field_type); + virtual void after_field_value(const char *, FT field_type); - virtual void before_field_value(const PT::Date &); - virtual void after_field_value(const PT::Date &); + virtual void before_field_value(wchar_t, FT field_type); + virtual void after_field_value(wchar_t, FT field_type); - virtual void before_field_value(const PT::Space &); - virtual void after_field_value(const PT::Space &); + virtual void before_field_value(char, FT field_type); + virtual void after_field_value(char, FT field_type); + + virtual void before_field_value(const PT::Date &, FT field_type); + virtual void after_field_value(const PT::Date &, FT field_type); + + virtual void before_field_value(const PT::Space &, FT field_type); + virtual void after_field_value(const PT::Space &, FT field_type); template - void before_field_value(const FieldValue &) + void before_field_value(const FieldValue &, FT field_type) { } template - void after_field_value(const FieldValue &) + void after_field_value(const FieldValue &, FT field_type) { } @@ -747,12 +753,14 @@ protected: // } -private: - virtual void before_field_value_string(); - virtual void after_field_value_string(); + virtual void before_field_value_string(FT field_type); + virtual void after_field_value_string(FT field_type); + char char_to_hex_part(char c); + void char_to_hex(char c, PT::TextStream & stream); + void esc(const wchar_t * val, bool has_known_length, size_t len, PT::TextStream & stream, FT field_type = FT::default_type); }; diff --git a/src/clearer.cpp b/src/clearer.cpp index a50c02e..53b2381 100644 --- a/src/clearer.cpp +++ b/src/clearer.cpp @@ -61,6 +61,10 @@ void Clearer::clear_value(unsigned char & field_value) field_value = 0; } +void Clearer::clear_value(wchar_t & field_value) +{ + field_value = 0; +} void Clearer::clear_value(std::wstring & field_value) { diff --git a/src/clearer.h b/src/clearer.h index 94f9de3..d6ab824 100644 --- a/src/clearer.h +++ b/src/clearer.h @@ -54,6 +54,7 @@ public: virtual void clear_value(char & field_value); virtual void clear_value(unsigned char & field_value); + virtual void clear_value(wchar_t & field_value); virtual void clear_value(std::wstring & field_value); virtual void clear_value(std::string & field_value); virtual void clear_value(bool & field_value); diff --git a/src/dbconnector.cpp b/src/dbconnector.cpp index 5ac3d06..4b3a4c3 100644 --- a/src/dbconnector.cpp +++ b/src/dbconnector.cpp @@ -291,52 +291,310 @@ void DbConnector::allocate_default_expression_if_needed() } - - - - - -void DbConnector::get_value(const char * value_str, char & field_value) +char DbConnector::unescape_hex_char_part(char hex) { - field_value = *value_str; - value_str += 1; - - if( *value_str != 0 ) + if( hex>='0' && hex<='9' ) { - // value has more than one charater, put some error? + return hex - '0'; + } + else + if( hex>='a' && hex<='f' ) + { + return hex - 'a' + 10; + } + else + if( hex>='A' && hex<='F' ) + { + return hex - 'A' + 10; + } + else + { + if( log ) + { + (*log) << PT::Log::log2 << "Morm: incorrect character when reading a hex string, char code: " << (int)(unsigned char)hex; + + if( hex >= 32 ) + { + (*log) << " '" << hex << "'"; + } + + (*log) << PT::Log::logend; + } + } + + return 0; +} + + +char DbConnector::unescape_hex_char(char char1, char char2) +{ + int c1 = unescape_hex_char_part(char1); + int c2 = unescape_hex_char_part(char2); + + return static_cast(((c1 << 4) | c2)); +} + + +void DbConnector::unescape_hex_string(const char * str, std::string & out) +{ + for(size_t i=0 ; str[i] != 0 ; i+=2 ) + { + out += unescape_hex_char(str[i], str[i+1]); + } +} + + +void DbConnector::unescape_hex_string(const char * str, std::wstring & out, FT field_type) +{ + if( field_type.use_utf8() ) + { + size_t len; + wchar_t c; + + while( *str != 0 && (len = unescape_hex_char(str, c, field_type)) > 0 ) + { + out += c; + str += len; + } + } + else + { + for(size_t i=0 ; str[i] != 0 ; i+=2 ) + { + out += static_cast(static_cast(unescape_hex_char(str[i], str[i+1]))); + } + } +} + + +void DbConnector::unescape_bin_string(const char * str, std::string & out) +{ + unescape_hex_string(str, out); +} + + +void DbConnector::unescape_bin_string(const char * str, std::wstring & out, FT field_type) +{ + unescape_hex_string(str, out, field_type); +} + + +// returns how many characters have been provided to utf8_str buffer +// min size of utf8_str should be 5 bytes (max 4 bytes for utf8 sequence + terminating null) +size_t DbConnector::unescape_hex_char(const char * value_str, char * utf8_str, size_t utf8_str_max_len) +{ + size_t value_str_index = 0; + size_t utf8_str_index = 0; + + utf8_str[0] = 0; + + while( utf8_str_index + 1 < utf8_str_max_len ) + { + if( value_str[value_str_index] != 0 && value_str[value_str_index+1] != 0 ) + { + utf8_str[utf8_str_index] = unescape_hex_char(value_str[value_str_index], value_str[value_str_index+1]); + utf8_str[utf8_str_index+1] = 0; + } + else + { + break; + } + + value_str_index += 2; + utf8_str_index += 1; + } + + return utf8_str_index; +} + + +// CHECKME need to be tested +// returns how many characters were used from value_str +size_t DbConnector::unescape_hex_char(const char * value_str, wchar_t & field_value, FT field_type) +{ + size_t len = 0; + + if( field_type.use_utf8() ) + { + char utf8_str[4 + 1]; // max utf8 sequence length + terminating zero + size_t utf8_str_len = unescape_hex_char(value_str, utf8_str, sizeof(utf8_str) / sizeof(char)); + + int value_int; + bool is_correct; + len = PT::UTF8ToInt(utf8_str, utf8_str_len, value_int, is_correct); + len = len * 2; + + if( is_correct ) + { + field_value = static_cast(value_int); + } + else + { + if( log ) + { + (*log) << PT::Log::log2 << "Morm: incorrect utf-8 sequence (ignoring)" << PT::Log::logend; + } + } + } + else + { + if( value_str[0] != 0 && value_str[1] != 0 ) + { + field_value = static_cast(static_cast(unescape_hex_char(value_str[0], value_str[1]))); + len = 2; + } + else + { + if( log ) + { + (*log) << PT::Log::log2 << "Morm: unexpected end of string (ignoring)" << PT::Log::logend; + } + } + } + + return len; +} + + +size_t DbConnector::unescape_bin_char(const char * value_str, wchar_t & field_value, FT field_type) +{ + return unescape_hex_char(value_str, field_value, field_type); +} + + + +// CHECKME need to be tested +void DbConnector::get_value(const char * value_str, char & field_value, FT field_type) +{ + wchar_t c; + + field_value = 0; + get_value(value_str, c, field_type); + + if( field_type.use_utf8() ) + { + if( c <= 127 ) + { + field_value = static_cast(c); + } + else + { + if( log ) + { + (*log) << PT::Log::log2 << "Morm: a character greater than 127 cannot be stored in char type, code point: " + << (int)c << " '" << c << "'" << PT::Log::logend; + } + } + } + else + { + field_value = static_cast(c); } } -void DbConnector::get_value(const char * value_str, unsigned char & field_value) +// CHECKME need to be tested +void DbConnector::get_value(const char * value_str, unsigned char & field_value, FT field_type) { - field_value = *(const unsigned char*)value_str; - value_str += 1; + char tmp_char; + get_value(value_str, tmp_char, field_type); - if( *value_str != 0 ) + field_value = static_cast(tmp_char); +} + + + +// CHECKME need to be tested +void DbConnector::get_value(const char * value_str, wchar_t & field_value, FT field_type) +{ + field_value = 0; + + if( field_type.is_binary() ) { - // value has more than one charater, put some error? + unescape_bin_char(value_str, field_value, field_type); + } + else + if( field_type.is_hexadecimal() ) + { + unescape_hex_char(value_str, field_value, field_type); + } + else + { + if( field_type.use_utf8() ) + { + int value_int; + bool is_correct; + + PT::UTF8ToInt(value_str, value_int, is_correct); + + if( is_correct ) + { + field_value = static_cast(value_int); + } + else + { + // report an error? + } + } + else + { + field_value = static_cast((unsigned char)*value_str); + } } } -void DbConnector::get_value(const char * value_str, std::wstring & field_value) + +// CHECKME need to be tested +void DbConnector::get_value(const char * value_str, std::wstring & field_value, FT field_type) { - // CHECKME - // what about \0 in val_str? - // it is escaped somehow? - PT::UTF8ToWide(value_str, field_value); + if( field_type.is_binary() ) + { + unescape_bin_string(value_str, field_value, field_type); + } + else + if( field_type.is_hexadecimal() ) + { + unescape_hex_string(value_str, field_value, field_type); + } + else + { + if( field_type.use_utf8() ) + { + PT::UTF8ToWide(value_str, field_value); + } + else + { + for(size_t i=0 ; value_str[i] != 0 ; ++i) + { + field_value += static_cast(value_str[i]); + } + } + } } -void DbConnector::get_value(const char * value_str, std::string & field_value) +// CHECKME need to be tested +void DbConnector::get_value(const char * value_str, std::string & field_value, FT field_type) { - field_value = value_str; + if( field_type.is_binary() ) + { + unescape_bin_string(value_str, field_value); + } + else + if( field_type.is_hexadecimal() ) + { + unescape_hex_string(value_str, field_value); + } + else + { + field_value = value_str; + } } -void DbConnector::get_value(const char * value_str, bool & field_value) +void DbConnector::get_value(const char * value_str, bool & field_value, FT field_type) { // IMPROVE ME // this 't' is locale dependent @@ -344,91 +602,91 @@ void DbConnector::get_value(const char * value_str, bool & field_value) } -void DbConnector::get_value(const char * value_str, short & field_value) +void DbConnector::get_value(const char * value_str, short & field_value, FT field_type) { // 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) +void DbConnector::get_value(const char * value_str, unsigned short & field_value, FT field_type) { // 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) +void DbConnector::get_value(const char * value_str, int & field_value, FT field_type) { // 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) +void DbConnector::get_value(const char * value_str, unsigned int & field_value, FT field_type) { // IMPROVE ME give some overflow checking field_value = PT::Toui(value_str, 10); } -void DbConnector::get_value(const char * value_str, long & field_value) +void DbConnector::get_value(const char * value_str, long & field_value, FT field_type) { // 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) +void DbConnector::get_value(const char * value_str, unsigned long & field_value, FT field_type) { // 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) +void DbConnector::get_value(const char * value_str, long long & field_value, FT field_type) { // 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) +void DbConnector::get_value(const char * value_str, unsigned long long & field_value, FT field_type) { // IMPROVE ME give some overflow checking field_value = PT::Toull(value_str, 10); } -void DbConnector::get_value(const char * value_str, float & field_value) +void DbConnector::get_value(const char * value_str, float & field_value, FT field_type) { // IMPROVE ME give some overflow checking field_value = strtof(value_str, 0); } -void DbConnector::get_value(const char * value_str, double & field_value) +void DbConnector::get_value(const char * value_str, double & field_value, FT field_type) { // IMPROVE ME give some overflow checking field_value = strtod(value_str, 0); } -void DbConnector::get_value(const char * value_str, long double & field_value) +void DbConnector::get_value(const char * value_str, long double & field_value, FT field_type) { // IMPROVE ME give some overflow checking field_value = strtold(value_str, 0); } -void DbConnector::get_value(const char * value_str, PT::Date & field_value) +void DbConnector::get_value(const char * value_str, PT::Date & field_value, FT field_type) { // IMPROVE ME give some log if parsing failed field_value.Parse(value_str); } -void DbConnector::get_value(const char * value_str, PT::Space & field_value) +void DbConnector::get_value(const char * value_str, PT::Space & field_value, FT field_type) { field_value.clear(); @@ -443,7 +701,7 @@ void DbConnector::get_value(const char * value_str, PT::Space & field_value) if( log ) { - (*log) << PT::Log::log1 << "Morm: I cannot correctly parse the Space struct from the datebase" + (*log) << PT::Log::log2 << "Morm: I cannot correctly parse the Space struct from the datebase" << ", the raw string is: " << value_str << PT::Log::logend; } } diff --git a/src/dbconnector.h b/src/dbconnector.h index f86e90c..d7e3b2e 100644 --- a/src/dbconnector.h +++ b/src/dbconnector.h @@ -38,6 +38,7 @@ #include "textstream/textstream.h" #include "log/log.h" #include "queryresult.h" +#include "ft.h" namespace morm @@ -90,25 +91,27 @@ public: virtual bool query_insert(const PT::TextStream & stream, QueryResult & query_result); virtual bool query_remove(const PT::TextStream & stream, QueryResult & query_result); - virtual void get_value(const char * value_str, char & field_value); - virtual void get_value(const char * value_str, unsigned char & field_value); - virtual void get_value(const char * value_str, std::wstring & field_value); - virtual void get_value(const char * value_str, std::string & field_value); - virtual void get_value(const char * value_str, bool & field_value); - virtual void get_value(const char * value_str, short & field_value); - virtual void get_value(const char * value_str, unsigned short & field_value); - virtual void get_value(const char * value_str, int & field_value); - virtual void get_value(const char * value_str, unsigned int & field_value); - virtual void get_value(const char * value_str, long & field_value); - virtual void get_value(const char * value_str, unsigned long & field_value); - virtual void get_value(const char * value_str, long long & field_value); - virtual void get_value(const char * value_str, unsigned long long & field_value); - virtual void get_value(const char * value_str, float & field_value); - virtual void get_value(const char * value_str, double & field_value); - virtual void get_value(const char * value_str, long double & field_value); - virtual void get_value(const char * value_str, PT::Date & field_value); - virtual void get_value(const char * value_str, PT::Space & field_value); + virtual void get_value(const char * value_str, char & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, unsigned char & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, wchar_t & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, std::wstring & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, std::string & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, bool & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, short & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, unsigned short & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, int & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, unsigned int & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, long & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, unsigned long & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, long long & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, unsigned long long & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, float & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, double & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, long double & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, PT::Date & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, PT::Space & field_value, FT field_type = FT::default_type); + // add get_value for PT::TextStream and PT::WTextStream template bool get_last_sequence(const wchar_t * sequence_table_name, FieldValue & field_value) @@ -145,6 +148,23 @@ protected: virtual const char * query_last_sequence(const wchar_t * sequence_table_name); + virtual void unescape_hex_string(const char * str, std::string & out); + virtual void unescape_hex_string(const char * str, std::wstring & out, FT field_type); + + virtual void unescape_bin_string(const char * str, std::string & out); + virtual void unescape_bin_string(const char * str, std::wstring & out, FT field_type); + + virtual size_t unescape_hex_char(const char * value_str, char * utf8_str, size_t utf8_str_max_len); + virtual size_t unescape_hex_char(const char * value_str, wchar_t & field_value, FT field_type); + virtual size_t unescape_bin_char(const char * value_str, wchar_t & field_value, FT field_type); + +private: + + char unescape_hex_char_part(char hex); + char unescape_hex_char(char char1, char char2); + + + }; diff --git a/src/dbexpression.cpp b/src/dbexpression.cpp index a0af7ef..5bf82d4 100644 --- a/src/dbexpression.cpp +++ b/src/dbexpression.cpp @@ -183,13 +183,13 @@ void DbExpression::put_name_value_separator() -void DbExpression::before_field_value_string() +void DbExpression::before_field_value_string(FT field_type) { (*out_stream) << "'"; } -void DbExpression::after_field_value_string() +void DbExpression::after_field_value_string(FT field_type) { (*out_stream) << "'"; } diff --git a/src/dbexpression.h b/src/dbexpression.h index 1e6385e..5c4cbdb 100644 --- a/src/dbexpression.h +++ b/src/dbexpression.h @@ -81,18 +81,15 @@ protected: int output_type; std::vector conjunctions; - bool can_field_be_generated(FT field_type); - void field_before(); - void put_name_value_separator(); private: - void before_field_value_string(); - void after_field_value_string(); + void before_field_value_string(FT field_type); + void after_field_value_string(FT field_type); }; diff --git a/src/flatexpression.cpp b/src/flatexpression.cpp index 4252d47..0a31f52 100644 --- a/src/flatexpression.cpp +++ b/src/flatexpression.cpp @@ -39,7 +39,7 @@ namespace morm { -void FlatExpression::esc(const PT::Date & date, PT::TextStream & stream) +void FlatExpression::esc(const PT::Date & date, PT::TextStream & stream, FT field_type) { date.SerializeISO(stream); } diff --git a/src/flatexpression.h b/src/flatexpression.h index b928607..fc45823 100644 --- a/src/flatexpression.h +++ b/src/flatexpression.h @@ -45,7 +45,7 @@ class FlatExpression : public BaseExpression { public: - void esc(const PT::Date & date, PT::TextStream & stream); + void esc(const PT::Date & date, PT::TextStream & stream, FT field_type); diff --git a/src/ft.h b/src/ft.h index 1d884e9..0344af1 100644 --- a/src/ft.h +++ b/src/ft.h @@ -57,6 +57,9 @@ public: no_updatable = 16, no_fetchable = 32, /* not supported yet */ raw_field_name = 64, + dont_use_utf8 = 128, + hexadecimal = 256, + binary = 512, }; /* @@ -137,6 +140,22 @@ public: return is_flag_set(raw_field_name); } + bool use_utf8() const + { + return !is_flag_set(dont_use_utf8); + } + + bool is_hexadecimal() const + { + return is_flag_set(hexadecimal); + } + + bool is_binary() const + { + return is_flag_set(binary); + } + + }; } diff --git a/src/jsonexpression.cpp b/src/jsonexpression.cpp index f9cd103..a2d18dd 100644 --- a/src/jsonexpression.cpp +++ b/src/jsonexpression.cpp @@ -105,12 +105,12 @@ void JSONExpression::after_second_part_long_field_name() } -void JSONExpression::before_field_value_string() +void JSONExpression::before_field_value_string(FT field_type) { (*out_stream) << "\""; } -void JSONExpression::after_field_value_string() +void JSONExpression::after_field_value_string(FT field_type) { (*out_stream) << "\""; } @@ -134,20 +134,48 @@ void JSONExpression::after_field_value_list() } -void JSONExpression::esc(char val, PT::TextStream & stream) +void JSONExpression::esc(char val, PT::TextStream & stream, FT field_type) { - switch( val ) + if( field_type.is_hexadecimal() || field_type.is_binary() ) { - case 0: stream << '\\'; stream << '0'; break; // may to skip this character is better? - case '\r': stream << '\\'; stream << 'r'; break; - case '\n': stream << '\\'; stream << 'n'; break; - case '\t': stream << '\\'; stream << 't'; break; - case 0x08: stream << '\\'; stream << 'b'; break; - case 0x0c: stream << '\\'; stream << 'f'; break; - case '\\': stream << '\\'; stream << '\\'; break; - case '"': stream << '\\'; stream << '\"'; break; - default: - stream << val; + char_to_hex(val, stream); + } + else + { + if( (unsigned char)val < 32 ) + { + char buf[10]; + size_t len; + PT::Toa((unsigned char)val, buf, sizeof(buf)/sizeof(char), 16, &len); + + stream << "\\u"; + + if( len < 4 ) + { + for(size_t i=0 ; i < (4-len) ; ++i) + { + stream << '0'; + } + } + + stream << buf; + } + else + { + switch( val ) + { + case 0: stream << '\\'; stream << '0'; break; // may to skip this character is better? + case '\r': stream << '\\'; stream << 'r'; break; + case '\n': stream << '\\'; stream << 'n'; break; + case '\t': stream << '\\'; stream << 't'; break; + case 0x08: stream << '\\'; stream << 'b'; break; + case 0x0c: stream << '\\'; stream << 'f'; break; + case '\\': stream << '\\'; stream << '\\'; break; + case '"': stream << '\\'; stream << '\"'; break; + default: + stream << val; + } + } } } diff --git a/src/jsonexpression.h b/src/jsonexpression.h index f2091c5..a9f39bf 100644 --- a/src/jsonexpression.h +++ b/src/jsonexpression.h @@ -70,13 +70,13 @@ protected: // 'morm::JSONExpression::esc' hides overloaded virtual function [-Woverloaded-virtual] using FlatExpression::esc; - void esc(char val, PT::TextStream & stream); + void esc(char val, PT::TextStream & stream, FT field_type); private: - void before_field_value_string(); - void after_field_value_string(); + void before_field_value_string(FT field_type); + void after_field_value_string(FT field_type); }; diff --git a/src/model.h b/src/model.h index 25d95b1..ebb4c7a 100644 --- a/src/model.h +++ b/src/model.h @@ -232,6 +232,11 @@ protected: field_generic(field_name, field_name, field_value, field_type); } + void field(const wchar_t * field_name, wchar_t & field_value, FT field_type = FT::default_type) + { + field_generic(field_name, field_name, field_value, field_type); + } + void field(const wchar_t * field_name, std::wstring & field_value, FT field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); @@ -347,6 +352,11 @@ protected: field_generic(db_field_name, flat_field_name, field_value, field_type); } + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, wchar_t & field_value, FT field_type = FT::default_type) + { + field_generic(db_field_name, flat_field_name, field_value, field_type); + } + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::wstring & field_value, FT field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); @@ -1224,7 +1234,7 @@ protected: if( val_str ) { - db_connector->get_value(val_str, field_value); + db_connector->get_value(val_str, field_value, field_type); } } else @@ -1271,7 +1281,7 @@ protected: if( val_str ) { - db_connector->get_value(val_str, field_value); + db_connector->get_value(val_str, field_value, field_type); } } else diff --git a/src/postgresqlconnector.cpp b/src/postgresqlconnector.cpp index bad375d..37acfac 100644 --- a/src/postgresqlconnector.cpp +++ b/src/postgresqlconnector.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018-2019, Tomasz Sowa + * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -392,128 +392,6 @@ bool PostgreSQLConnector::query_remove(const PT::TextStream & stream, QueryResul - - - -/* - converting from a bytea - the old way (escape format) -*/ -/* -int PostgreSQLConnector::CharToInt(char c) -{ - return (int)(unsigned char)(c-'0'); -} - -bool PostgreSQLConnector::IsCorrectOctalDigit(char c) -{ - return c>='0' && c<='7'; -} - -// moves 'i' at least once -// return -1 if there is en error -int PostgreSQLConnector::UnescapeBin(const char * str, size_t & i, size_t len) -{ - if( str[i] != '\\' ) - return str[i++]; - - i += 1; - - if( i >= len ) - return -1; - - if( str[i] == '\\' ) - return str[i++]; - - if( i+2 >= len ) - { - i = len; - return -1; - } - - if( !IsCorrectOctalDigit(str[i]) || - !IsCorrectOctalDigit(str[i+1]) || - !IsCorrectOctalDigit(str[i+2]) ) - { - i += 3; - return -1; - } - - int c = 8*8*CharToInt(str[i]) + 8*CharToInt(str[i+1]) + CharToInt(str[i+2]); - - i += 3; - - if( c<0 || c>255 ) - return -1; - -return c; -} - -void PostgreSQLConnector::UnescapeBin(const char * str, size_t len, std::string & out, bool clear_out) -{ -int c; -size_t i = 0; - - if( clear_out ) - out.clear(); - - while( i < len ) - { - c = UnescapeBin(str, i, len); - - if( c != -1 ) - out += c; - } -} -*/ - - - - - -/* - converting from a bytea - the new way (hex format) -*/ - - -//char PostgreSQLConnector::UnescapeBinHexToDigit(char hex) -//{ -// if( hex>='0' && hex<='9' ) -// return hex - '0'; -// -// if( hex>='a' && hex<='z' ) -// return hex - 'a' + 10; -// -// if( hex>='A' && hex<='Z' ) -// return hex - 'A' + 10; -// -//return 0; -//} -// -// -//void PostgreSQLConnector::UnescapeBin(const char * str, size_t len, std::string & out, bool clear_out) -//{ -// if( clear_out ) -// out.clear(); -// -// if( len < 2 || str[0]!='\\' || str[1]!='x' ) -// { -// log << log1 << "Morm: unsupported binary format (skipping)" << logend; -// return; -// } -// -// for(size_t i=2 ; i + 1 < len ; i+=2 ) -// { -// int c1 = UnescapeBinHexToDigit(str[i]); -// int c2 = UnescapeBinHexToDigit(str[i+1]); -// -// out += ((c1 << 4) | c2); -// } -//} - - - void PostgreSQLConnector::set_conn_param(const std::wstring & database_name, const std::wstring & user, const std::wstring & pass) { db_database = database_name; @@ -672,8 +550,55 @@ void PostgreSQLConnector::set_db_parameters() } +void PostgreSQLConnector::log_unsupported_bin_format() +{ + if( log ) + { + (*log) << PT::Log::log1 << "Morm: unsupported binary format (skipping)" << PT::Log::logend; + } +} +size_t PostgreSQLConnector::unescape_bin_char(const char * str, wchar_t & field_value, FT field_type) +{ + if( str[0]!='\\' || str[1]!='x' ) + { + log_unsupported_bin_format(); + return 0; + } + else + { + return unescape_hex_char(str + 2, field_value, field_type); + } +} + + + +void PostgreSQLConnector::unescape_bin_string(const char * str, std::string & out) +{ + if( str[0]!='\\' || str[1]!='x' ) + { + log_unsupported_bin_format(); + } + else + { + unescape_hex_string(str + 2, out); + } +} + + +void PostgreSQLConnector::unescape_bin_string(const char * str, std::wstring & out, FT field_type) +{ + if( str[0]!='\\' || str[1]!='x' ) + { + log_unsupported_bin_format(); + } + else + { + unescape_hex_string(str + 2, out, field_type); + } +} + } diff --git a/src/postgresqlconnector.h b/src/postgresqlconnector.h index 27e4f1a..9403d9f 100644 --- a/src/postgresqlconnector.h +++ b/src/postgresqlconnector.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018-2019, Tomasz Sowa + * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -98,6 +98,11 @@ protected: virtual const char * query_last_sequence(const wchar_t * sequence_table_name); virtual QueryResult * create_query_result(); + void log_unsupported_bin_format(); + + size_t unescape_bin_char(const char * str, wchar_t & field_value, FT field_type); + void unescape_bin_string(const char * str, std::string & out); + void unescape_bin_string(const char * str, std::wstring & out, FT field_type); }; diff --git a/src/postgresqlexpression.cpp b/src/postgresqlexpression.cpp index 4da9ef6..392c0b7 100644 --- a/src/postgresqlexpression.cpp +++ b/src/postgresqlexpression.cpp @@ -77,34 +77,53 @@ void PostgreSQLExpression::after_second_part_long_field_name() -void PostgreSQLExpression::before_field_value_string() +void PostgreSQLExpression::before_field_value_string(FT field_type) { - (*out_stream) << "E'"; + if( field_type.is_binary() ) + { + (*out_stream) << "'\\x"; + } + else + if( field_type.is_hexadecimal() ) + { + (*out_stream) << "'"; + } + else + { + (*out_stream) << "E'"; + } } -void PostgreSQLExpression::after_field_value_string() +void PostgreSQLExpression::after_field_value_string(FT field_type) { (*out_stream) << "'"; } -void PostgreSQLExpression::esc(char val, PT::TextStream & stream) +void PostgreSQLExpression::esc(char val, PT::TextStream & stream, FT field_type) { - switch( val ) + if( field_type.is_hexadecimal() || field_type.is_binary() ) { - case '\\': stream << "\\\\"; break; - case '\'': stream << "\\\'"; break; // don't use "''" because we use the method for PQconnectdb too - default: - if( val != 0 ) + char_to_hex(val, stream); + } + else + { + switch( val ) { - stream << val; + case '\\': stream << "\\\\"; break; + case '\'': stream << "\\\'"; break; // don't use "''" because we use the method for PQconnectdb too + default: + if( val != 0 ) + { + stream << val; + } } } } -void PostgreSQLExpression::esc(const PT::Date & date, PT::TextStream & stream) +void PostgreSQLExpression::esc(const PT::Date & date, PT::TextStream & stream, FT field_type) { stream << date << "+00"; } diff --git a/src/postgresqlexpression.h b/src/postgresqlexpression.h index 873247d..3be9a23 100644 --- a/src/postgresqlexpression.h +++ b/src/postgresqlexpression.h @@ -45,8 +45,8 @@ class PostgreSQLExpression : public DbExpression { public: - void esc(char val, PT::TextStream & stream); - void esc(const PT::Date & date, PT::TextStream & stream); + void esc(char val, PT::TextStream & stream, FT field_type); + void esc(const PT::Date & date, PT::TextStream & stream, FT field_type); DbExpression & page(PT::TextStream & stream, size_t page_number, size_t page_size); @@ -64,10 +64,8 @@ protected: private: - void before_field_value_string(); - void after_field_value_string(); - - + void before_field_value_string(FT field_type); + void after_field_value_string(FT field_type); }; From c7797ff2f17119df0b8f8d279777bfda2969c6fc Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Wed, 12 May 2021 00:27:35 +0200 Subject: [PATCH 14/27] fixed #2: escape tables/columns names in Finder left join queries some methods moved from model.h to model.cpp and from baseexpression.h to baseexpression.cpp --- src/baseexpression.cpp | 194 +++++++++++++++++ src/baseexpression.h | 80 ++----- src/finder.h | 6 +- src/model.cpp | 451 +++++++++++++++++++++++++++++++++++++-- src/model.h | 467 ++--------------------------------------- 5 files changed, 678 insertions(+), 520 deletions(-) diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index 0c56c0f..11ef30d 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -74,6 +74,20 @@ int BaseExpression::get_work_mode() +PT::TextStream * BaseExpression::get_text_stream() +{ + return out_stream; +} + +void BaseExpression::set_text_stream(PT::TextStream * out_stream) +{ + this->out_stream = out_stream; +} + + + + + PT::TextStream * BaseExpression::get_current_stream() { return out_stream; @@ -758,6 +772,186 @@ void BaseExpression::after_field_value_string(FT field_type) } +void BaseExpression::put_schema_table(const wchar_t * schema_name, const wchar_t * table_name) +{ + if( out_stream ) + { + before_first_part_long_field_name(); + esc(schema_name, *out_stream); + after_first_part_long_field_name(); + + (*out_stream) << '.'; + + before_second_part_long_field_name(); + esc(table_name, *out_stream); + after_second_part_long_field_name(); + } +} + + +void BaseExpression::put_schema_table(const PT::WTextStream & schema_name, const PT::WTextStream & table_name) +{ + if( out_stream ) + { + before_first_part_long_field_name(); + esc(schema_name, *out_stream); + after_first_part_long_field_name(); + + (*out_stream) << '.'; + + before_second_part_long_field_name(); + esc(table_name, *out_stream); + after_second_part_long_field_name(); + } +} + + +void BaseExpression::put_table(const wchar_t * table_name) +{ + if( out_stream ) + { + before_short_field_name(); + esc(table_name, *out_stream); + after_short_field_name(); + } +} + + +void BaseExpression::put_table(const PT::WTextStream & table_name) +{ + if( out_stream ) + { + before_short_field_name(); + esc(table_name, *out_stream); + after_short_field_name(); + } +} + + +void BaseExpression::put_table_with_index(const wchar_t * table_name, int index) +{ + if( out_stream ) + { + before_short_field_name(); + esc(table_name, *out_stream); + + if( index > 1 ) + { + (*out_stream) << index; + } + + after_short_field_name(); + } +} + + +void BaseExpression::put_table_with_index(const PT::WTextStream & table_name, int index) +{ + if( out_stream ) + { + before_short_field_name(); + esc(table_name, *out_stream); + + if( index > 1 ) + { + (*out_stream) << index; + } + + after_short_field_name(); + } +} + + +void BaseExpression::put_table_with_index_and_field(const wchar_t * table_name, int index, const wchar_t * field_name, const FT & field_type) +{ + if( out_stream ) + { + put_table_with_index(table_name, index); + (*out_stream) << '.'; + + // IMPROVE ME + // put_field_name seems to be too complicated, it is needed to check there whether field_name is long or short? + put_field_name(field_name, field_type, nullptr); + } +} + +void BaseExpression::put_table_with_index_and_field(const PT::WTextStream & table_name, int index, const wchar_t * field_name, const FT & field_type) +{ + if( out_stream ) + { + put_table_with_index(table_name, index); + (*out_stream) << '.'; + put_field_name(field_name, field_type, nullptr); + } +} + + + + +void BaseExpression::schema_table_to_stream(PT::TextStream & stream, const wchar_t * schema_name, const wchar_t * table_name) +{ + this->out_stream = &stream; + put_schema_table(schema_name, table_name); + this->out_stream = nullptr; +} + + +void BaseExpression::schema_table_to_stream(PT::TextStream & stream, const PT::WTextStream & schema_name, const PT::WTextStream & table_name) +{ + this->out_stream = &stream; + put_schema_table(schema_name, table_name); + this->out_stream = nullptr; +} + + +void BaseExpression::table_to_stream(PT::TextStream & stream, const wchar_t * table_name) +{ + this->out_stream = &stream; + put_table(table_name); + this->out_stream = nullptr; +} + + +void BaseExpression::table_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name) +{ + this->out_stream = &stream; + put_table(table_name); + this->out_stream = nullptr; +} + + +void BaseExpression::table_with_index_to_stream(PT::TextStream & stream, const wchar_t * table_name, int index) +{ + this->out_stream = &stream; + put_table_with_index(table_name, index); + this->out_stream = nullptr; +} + + +void BaseExpression::table_with_index_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, int index) +{ + this->out_stream = &stream; + put_table_with_index(table_name, index); + this->out_stream = nullptr; +} + + +void BaseExpression::table_with_index_and_field_to_stream(PT::TextStream & stream, const wchar_t * table_name, int index, const wchar_t * field_name, const FT & field_type) +{ + this->out_stream = &stream; + put_table_with_index_and_field(table_name, index, field_name, field_type); + this->out_stream = nullptr; +} + + +void BaseExpression::table_with_index_and_field_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, int index, const wchar_t * field_name, const FT & field_type) +{ + this->out_stream = &stream; + put_table_with_index_and_field(table_name, index, field_name, field_type); + this->out_stream = nullptr; +} + + } diff --git a/src/baseexpression.h b/src/baseexpression.h index e06758c..23a7a76 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -60,6 +60,9 @@ public: virtual void set_work_mode(int work_mode); virtual int get_work_mode(); + virtual PT::TextStream * get_text_stream(); + virtual void set_text_stream(PT::TextStream * out_stream); + virtual void clear(); virtual void generate_from_model(PT::TextStream & stream, Model & model); @@ -220,67 +223,23 @@ public: } - void table_to_stream(PT::TextStream & stream, const wchar_t * schema_name, const wchar_t * table_name, ModelEnv * model_env) - { - this->out_stream = &stream; - //table(table_name, model_env); + virtual void put_schema_table(const wchar_t * schema_name, const wchar_t * table_name); + virtual void put_schema_table(const PT::WTextStream & schema_name, const PT::WTextStream & table_name); + virtual void put_table(const wchar_t * table_name); + virtual void put_table(const PT::WTextStream & table_name); + virtual void put_table_with_index(const wchar_t * table_name, int index); + virtual void put_table_with_index(const PT::WTextStream & table_name, int index); + virtual void put_table_with_index_and_field(const wchar_t * table_name, int index, const wchar_t * field_name, const FT & field_type); + virtual void put_table_with_index_and_field(const PT::WTextStream & table_name, int index, const wchar_t * field_name, const FT & field_type); - before_first_part_long_field_name(); - esc(schema_name, *out_stream); - after_first_part_long_field_name(); - - (*out_stream) << '.'; - - before_second_part_long_field_name(); - esc(table_name, *out_stream); - after_second_part_long_field_name(); - - this->out_stream = nullptr; - } - - void table_to_stream(PT::TextStream & stream, const PT::WTextStream & schema_name, const PT::WTextStream & table_name, ModelEnv * model_env) - { - this->out_stream = &stream; - //table(table_name, model_env); - - before_first_part_long_field_name(); - esc(schema_name, *out_stream); - after_first_part_long_field_name(); - - (*out_stream) << '.'; - - before_second_part_long_field_name(); - esc(table_name, *out_stream); - after_second_part_long_field_name(); - - this->out_stream = nullptr; - } - - void table_to_stream(PT::TextStream & stream, const wchar_t * table_name, ModelEnv * model_env) - { - this->out_stream = &stream; - //table(table_name, model_env); - - if( is_long_field_name(table_name) ) - put_long_field_name(table_name); - else - put_short_field_name(table_name, model_env); - - this->out_stream = nullptr; - } - - - void table_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, ModelEnv * model_env) - { - this->out_stream = &stream; - //table(table_name, model_env); - - before_short_field_name(); - esc(table_name, *out_stream); - after_short_field_name(); - - this->out_stream = nullptr; - } + virtual void schema_table_to_stream(PT::TextStream & stream, const wchar_t * schema_name, const wchar_t * table_name); + virtual void schema_table_to_stream(PT::TextStream & stream, const PT::WTextStream & schema_name, const PT::WTextStream & table_name); + virtual void table_to_stream(PT::TextStream & stream, const wchar_t * table_name); + virtual void table_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name); + virtual void table_with_index_to_stream(PT::TextStream & stream, const wchar_t * table_name, int index); + virtual void table_with_index_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, int index); + virtual void table_with_index_and_field_to_stream(PT::TextStream & stream, const wchar_t * table_name, int index, const wchar_t * field_name, const FT & field_type); + virtual void table_with_index_and_field_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, int index, const wchar_t * field_name, const FT & field_type); /* @@ -340,6 +299,7 @@ protected: virtual void field_after(); virtual void put_field_name(const wchar_t * field_name, FT field_type, ModelEnv * model_env); + virtual void save_foreign_key(const wchar_t * field_name, FT field_type, ModelEnv * model_env); virtual void dump_additional_info(Model & model); diff --git a/src/finder.h b/src/finder.h index 8d936b7..2ccfc9e 100644 --- a/src/finder.h +++ b/src/finder.h @@ -240,15 +240,15 @@ public: if( !model.model_env->schema_name.empty() ) { - db_expression->table_to_stream(*out_stream, model.model_env->schema_name, model.model_env->table_name, &model_env); + db_expression->schema_table_to_stream(*out_stream, model.model_env->schema_name, model.model_env->table_name); } else { - db_expression->table_to_stream(*out_stream, model.model_env->table_name, &model_env); + db_expression->table_to_stream(*out_stream, model.model_env->table_name); } (*out_stream) << " AS "; - db_expression->table_to_stream(*out_stream, model.model_env->table_name, &model_env); + db_expression->table_to_stream(*out_stream, model.model_env->table_name); if( !finder_helper.join_tables_str.empty() ) { diff --git a/src/model.cpp b/src/model.cpp index 096bf1a..f9e9845 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -906,19 +906,6 @@ bool Model::is_the_same_field(const wchar_t * field1, const wchar_t * field2) } -void Model::put_table_name_with_index(PT::TextStream & str) -{ - if( model_env ) - { - str << model_env->table_name; - - if( model_env->table_index > 1 ) - { - str << model_env->table_index; - } - } -} - PT::WTextStream Model::get_table_name(bool put_schema_name) { @@ -1014,7 +1001,445 @@ void Model::put_fields_to_log(PT::Log & log, const wchar_t * db_field_name, cons } +/* + * IMPROVE ME there can be more rows in the result set when there are more items on the left hand side of the join + * this is only in a case when has_foreign_key is false, may it can be ignored? we can use unique index in such a case + * + */ +void Model::field_model_left_join(const wchar_t * db_field_name, Model & field_model, FT field_type, DbExpression * db_expression) +{ + if( model_env && field_model.model_env && model_env->finder_helper ) + { + model_env->finder_helper->foreign_keys.clear(); + PT::TextStream & join_tables_str = model_env->finder_helper->join_tables_str; + field_model.model_env->add_table_name_to_finder_helper(); + join_tables_str << "LEFT JOIN "; + + PT::TextStream * db_expression_stream = db_expression->get_text_stream(); + int expr_work_mode = db_expression->get_work_mode(); + int expr_output_type = db_expression->get_output_type(); + bool expr_allow_prefix = db_expression->get_allow_to_use_prefix(); + + db_expression->schema_table_to_stream(join_tables_str, field_model.model_env->schema_name, field_model.model_env->table_name); + join_tables_str << " AS "; + db_expression->table_with_index_to_stream(join_tables_str, field_model.model_env->table_name, field_model.model_env->table_index); + + db_expression->set_work_mode(MORM_WORK_MODE_MODEL_SAVE_FIELDS); + db_expression->set_output_type(MORM_OUTPUT_TYPE_JOIN_TABLES); + db_expression->allow_to_use_prefix(false); + db_expression->set_text_stream(db_expression_stream); // restore original value because schema_table_to_stream() will set the stream to null + + if( field_type.is_foreign_key() ) + { + field_model.map_fields(); + + join_tables_str << " ON "; + db_expression->table_with_index_and_field_to_stream(join_tables_str, model_env->table_name, model_env->table_index, db_field_name, field_type); + join_tables_str << " = "; + + db_expression->table_with_index_to_stream(join_tables_str, field_model.model_env->table_name, field_model.model_env->table_index); + join_tables_str << '.'; + + // IMPROVE ME at the moment support only for foreign keys consisting of only one column + if( model_env->finder_helper->foreign_keys.size() == 1 ) + { + join_tables_str << model_env->finder_helper->foreign_keys.front(); + } + } + else + { + ModelEnv * old_model_env = field_model.model_env; + map_fields(); // map_fields() will set field_model.model_env to null + field_model.model_env = old_model_env; + + join_tables_str << " ON "; + db_expression->table_with_index_to_stream(join_tables_str, model_env->table_name, model_env->table_index); + join_tables_str << '.'; + + // IMPROVE ME at the moment support only for foreign keys consisting of only one column + if( model_env->finder_helper->foreign_keys.size() == 1 ) + { + join_tables_str << model_env->finder_helper->foreign_keys.front(); + } + + join_tables_str << " = "; + db_expression->table_with_index_and_field_to_stream(join_tables_str, field_model.model_env->table_name, field_model.model_env->table_index, db_field_name, field_type); + } + + join_tables_str << ' '; + + db_expression->set_work_mode(expr_work_mode); + db_expression->set_output_type(expr_output_type); + db_expression->allow_to_use_prefix(expr_allow_prefix); + db_expression->set_text_stream(db_expression_stream); + } +} + + +/* + * first we iterate through fields and save primary key values to helper_tab + */ +void Model::field_model_save_key(const wchar_t * db_field_name) +{ + DbConnector * db_connector = model_connector->get_db_connector(); + PT::Log * plog = model_connector->get_logger(); + + if( db_connector ) + { + DbExpression * db_expression = db_connector->get_expression(); + + if( db_expression && !is_empty_field(db_field_name) && model_env->field_value_helper_tab ) + { + int old_work_mode = model_env->model_work_mode; + model_env->model_work_mode = MORM_MODEL_WORK_MODE_ITERATE_PRIMARY_KEY_VALUES; + model_env->field_index = 0; + map_fields(); + model_env->model_work_mode = old_work_mode; + + if( model_env->field_value_helper_tab->empty() && plog ) + { + (*plog) << PT::Log::log1 << "Morm: I cannot find a primary key in " << get_table_name() << PT::Log::logend; + } + } + } +} + + +/* + * now we iterate through fields in field_model and save primary key values from *this object to the specified fields in field_model + */ +void Model::field_model_set_parent_key_in_child(const wchar_t * db_field_name, Model & field_model) +{ + DbConnector * db_connector = model_connector->get_db_connector(); + PT::Log * log = model_connector->get_logger(); + + if( db_connector ) + { + DbExpression * db_expression = db_connector->get_expression(); + + if( db_expression && !is_empty_field(db_field_name) && model_env->field_value_helper_tab ) + { + std::vector & helper_tab = *model_env->field_value_helper_tab; + + if( (size_t)model_env->field_index == helper_tab.size() ) + { + ModelEnv model_env_local; + model_env_local.copy_global_objects(*model_env); + model_env_local.has_primary_key_set = field_model.has_primary_key_set; + model_env_local.model_work_mode = MORM_MODEL_WORK_MODE_SET_FIELD_VALUE; + model_env_local.field_value_helper_tab = &helper_tab; + model_env_local.field_index = 0; + field_model.model_env = &model_env_local; + field_model.prepare_table(); + + field_model.map_fields(); + + if( (size_t)field_model.model_env->field_index != helper_tab.size() && log ) + { + if( field_model.model_env->field_index == 0 ) + { + (*log) << PT::Log::log1 << "Morm: there is no a foreign key in " << field_model.get_table_name() + << " called " << db_field_name << " pointing to " << get_table_name() << PT::Log::logend; + } + else + { + (*log) << PT::Log::log1 << "Morm: primary key in " << get_table_name() << " consists of " << model_env->field_index << " column(s)" + << " but foreign key in " << field_model.get_table_name() << " consists of " + << field_model.model_env->field_index << " column(s)" << PT::Log::logend; + } + } + + field_model.model_env = nullptr; + } + else + if( log ) + { + (*log) << PT::Log::log1 << "Morm: primary key in " << get_table_name() << " consists of incorrect number of columns" + << ", expected " << helper_tab.size() << " column(s) but got " << model_env->field_index << PT::Log::logend; + } + } + } +} + + +void Model::field_model_set_parent_key(const wchar_t * db_field_name, Model & field_model) +{ + FieldValueHelper helper; + helper.db_field_name = db_field_name; + helper.flat_field_name = nullptr; + helper.compare_flat_field_name = false; + + std::vector helper_tab; + helper_tab.push_back(helper); + // only one column at the moment, in the future we can have a primary key from more than one column + + model_env->field_value_helper_tab = &helper_tab; + + field_model_save_key(db_field_name); + field_model_set_parent_key_in_child(db_field_name, field_model); + + model_env->field_value_helper_tab = nullptr; +} + + +void Model::field_model_iterate_through_childs(const wchar_t * db_field_name, Model & field_model) +{ + if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_INSERT ) + { + field_model.insert_tree(true); + } + + if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_UPDATE ) + { + field_model.update_tree(true); + } + + if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_REMOVE ) + { + field_model.remove_tree(true); + } + + if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_SAVE ) + { + field_model.save_tree(true); + } +} + + + +void Model::field_model_generate_flat_string(const wchar_t * flat_field_name, Model & field_model, FT field_type) +{ + FlatConnector * flat_connector = model_connector->get_flat_connector(); + + if( flat_connector ) + { + FlatExpression * flat_expression = flat_connector->get_expression(); + + if( flat_expression ) + { + if( model_env->dump_mode || field_model.save_mode == DO_INSERT_ON_SAVE || field_model.save_mode == DO_UPDATE_ON_SAVE ) + { + field_model.model_env->model_work_mode = MORM_MODEL_WORK_MODE_GENERATING_FLAT_STRING; + flat_expression->field_model(flat_field_name, field_model, field_type, model_env); + } + } + } +} + + +void Model::field_model_generate_db_sql(const wchar_t * db_field_name, Model & field_model, FT field_type) +{ + DbConnector * db_connector = model_connector->get_db_connector(); + + if( db_connector ) + { + DbExpression * db_expression = db_connector->get_expression(); + + if( db_expression && !is_empty_field(db_field_name) ) + { + field_model.model_env->model_work_mode = MORM_MODEL_WORK_MODE_GENERATING_DB_SQL; + + if( db_expression->get_output_type() == MORM_OUTPUT_TYPE_SELECT_COLUMNS ) + { + field_model_left_join(db_field_name, field_model, field_type, db_expression); + } + + if( field_type.is_foreign_key() ) + { + if( db_expression->get_work_mode() == MORM_WORK_MODE_MODEL_FIELDS && db_expression->get_output_type() == MORM_OUTPUT_TYPE_DB_INSERT ) + { + if( field_type.is_insertable() ) + { + int not_used_object = 0; + db_expression->field(db_field_name, not_used_object, field_type, model_env); + } + } + + if( db_expression->get_work_mode() == MORM_WORK_MODE_MODEL_VALUES && db_expression->get_output_type() == MORM_OUTPUT_TYPE_DB_INSERT ) + { + if( field_type.is_insertable() ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_INSERT_PRIMARY_KEY); + field_model.map_fields(); + db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_INSERT); + } + } + + if( db_expression->get_work_mode() == MORM_WORK_MODE_MODEL_FIELDS_VALUES && db_expression->get_output_type() == MORM_OUTPUT_TYPE_DB_UPDATE ) + { + if( field_type.is_updatable() ) + { + std::vector key_fields; + key_fields.push_back(db_field_name); // at the moment only one key + + db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE_PRIMARY_KEY); + field_model.model_env->field_index = 0; + field_model.model_env->set_field_name_helper = &key_fields; + field_model.map_fields(); + db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE); + + if( (size_t)field_model.model_env->field_index != key_fields.size() ) + { + // IMPROVEME + // number of keys are different + // put error log here + } + } + } + } + + if( db_expression->get_output_type() != MORM_OUTPUT_TYPE_JOIN_TABLES && + db_expression->get_output_type() != MORM_OUTPUT_TYPE_DB_PRIMARY_KEY && + db_expression->get_output_type() != MORM_OUTPUT_TYPE_DB_INSERT && + db_expression->get_output_type() != MORM_OUTPUT_TYPE_DB_UPDATE ) + { + field_model.map_fields(); + } + + field_model.model_env->model_work_mode = MORM_MODEL_WORK_MODE_NONE; + } + } +} + + +void Model::field_model_clear_values(Model & field_model) +{ + Clearer * clearer = model_connector->get_clearer(); + + if( clearer ) + { + clearer->clear_model(field_model); + } +} + + +void Model::field_model_read_values_from_queryresult(const wchar_t * db_field_name, Model & field_model, FT field_type) +{ + DbConnector * db_connector = model_connector->get_db_connector(); + + if( db_connector ) + { + DbExpression * db_expression = db_connector->get_expression(); + + if( db_expression ) + { + if( model_env->cursor_helper && + !model_env->cursor_helper->has_autogenerated_select && + model_env->cursor_helper->use_table_prefix_for_fetching_values ) + { + field_model.model_env->add_table_name_to_finder_helper(); + } + + field_model.before_select(); + field_model.map_values_from_query(); + + if( field_model.found() ) + { + field_model.after_select(); + } + } + } +} + + +void Model::field_model(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model, FT field_type) +{ + if( model_connector && model_env ) + { + ModelEnv model_env_local; + model_env_local.copy_global_objects(*model_env); + + field_model.model_env = &model_env_local; + field_model.model_env->has_primary_key_set = field_model.has_primary_key_set; + field_model.set_connector(model_connector); + + if( !is_empty_field(db_field_name) ) + { + field_model.prepare_table(); + + if( field_type.is_foreign_key() || field_type.is_foreign_key_in_child() ) + { + field_model_for_db(db_field_name, field_model, field_type); + } + else + { + PT::Log * plog = model_connector->get_logger(); + + if( plog ) + { + (*plog) << PT::Log::log1 << "Morm: error in " << get_table_name_with_field(db_field_name) + << " field, you should set FT::is_foreign_key or FT::is_foreign_key_in_child flag for a model child object" << PT::Log::logend; + } + } + } + + if( !is_empty_field(flat_field_name) ) + { + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_FLAT_STRING ) + { + // calling field_model.prepare_table(); is not needed in generating strings (at least for json/space formats) + field_model_generate_flat_string(flat_field_name, field_model, field_type); + } + } + + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_CLEARING_VALUE ) + { + field_model_clear_values(field_model); + } + + field_model.model_env = nullptr; + } +} + + +void Model::field_model_for_db(const wchar_t * db_field_name, Model & field_model, FT field_type) +{ + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_SET_PARENT_ID ) + { + if( field_type.is_foreign_key_in_child() ) + { + field_model_set_parent_key(db_field_name, field_model); + } + } + + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITH_FOREIGN_KEY ) + { + if( field_type.is_foreign_key() ) + { + field_model_iterate_through_childs(db_field_name, field_model); + } + } + + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITHOUT_FOREIGN_KEY ) + { + if( field_type.is_foreign_key_in_child() ) + { + field_model_iterate_through_childs(db_field_name, field_model); + } + } + + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_DB_SQL ) + { + field_model_generate_db_sql(db_field_name, field_model, field_type); + } + + + if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_READING_VALUE_FROM_DB_RESULTSET ) + { + field_model_read_values_from_queryresult(db_field_name, field_model, field_type); + } +} + + +void Model::set_parent_key_in_childs() +{ + if( model_env ) + { + model_env->model_work_mode = MORM_MODEL_WORK_MODE_SET_PARENT_ID; + map_fields(); + } +} } // namespace diff --git a/src/model.h b/src/model.h index ebb4c7a..a9d7e57 100644 --- a/src/model.h +++ b/src/model.h @@ -219,7 +219,7 @@ protected: ///////////////////////////////// /* * IMPLEMENT ME - * field methods for such field_values: signed char, wchar_t, char16_t, char32_t, std::u16string, std::u32string + * field methods for such field_values: signed char, char16_t, char32_t, std::u16string, std::u32string * */ void field(const wchar_t * field_name, char & field_value, FT field_type = FT::default_type) @@ -319,7 +319,6 @@ protected: void field(const wchar_t * field_name, Model & field_value, FT field_type = FT::default_type) { - // has_foreign_key was here field_model(field_name, field_name, field_value, field_type); } @@ -633,429 +632,17 @@ protected: } - /* - * IMPROVE ME there can be more rows in the result set when there are more items on the left hand side of the join - * this is only in a case when has_foreign_key is false, may it can be ignored? we can use unique index in such a case - * - */ - void field_model_left_join(const wchar_t * db_field_name, Model & field_model, FT field_type, DbExpression * db_expression) - { - if( model_env && field_model.model_env && model_env->finder_helper ) - { - model_env->finder_helper->foreign_keys.clear(); - PT::TextStream & join_tables_str = model_env->finder_helper->join_tables_str; - field_model.model_env->add_table_name_to_finder_helper(); - - join_tables_str << "LEFT JOIN " << field_model.model_env->table_name << " AS "; - field_model.put_table_name_with_index(join_tables_str); - - int expr_work_mode = db_expression->get_work_mode(); - int expr_output_type = db_expression->get_output_type(); - bool expr_allow_prefix = db_expression->get_allow_to_use_prefix(); - - db_expression->set_work_mode(MORM_WORK_MODE_MODEL_SAVE_FIELDS); - db_expression->set_output_type(MORM_OUTPUT_TYPE_JOIN_TABLES); - db_expression->allow_to_use_prefix(false); - - if( field_type.is_foreign_key() ) - { - field_model.map_fields(); - - join_tables_str << " ON "; - put_table_name_with_index(join_tables_str); - join_tables_str << '.' << db_field_name << " = "; - field_model.put_table_name_with_index(join_tables_str); - join_tables_str << '.'; - - // IMPROVE ME at the moment support only for foreign keys consisting of only one column - if( model_env->finder_helper->foreign_keys.size() == 1 ) - { - join_tables_str << model_env->finder_helper->foreign_keys.front(); - } - } - else - { - ModelEnv * old_model_env = field_model.model_env; - map_fields(); // map_fields() will set field_model.model_env to null - field_model.model_env = old_model_env; - - join_tables_str << " ON "; - put_table_name_with_index(join_tables_str); - join_tables_str << '.'; - - // IMPROVE ME at the moment support only for foreign keys consisting of only one column - if( model_env->finder_helper->foreign_keys.size() == 1 ) - { - join_tables_str << model_env->finder_helper->foreign_keys.front(); - } - - join_tables_str << " = "; - field_model.put_table_name_with_index(join_tables_str); - join_tables_str << '.' << db_field_name; - } - - join_tables_str << ' '; - - db_expression->set_work_mode(expr_work_mode); - db_expression->set_output_type(expr_output_type); - db_expression->allow_to_use_prefix(expr_allow_prefix); - } - } - - - /* - * first we iterate through fields and save primary key values to helper_tab - */ - void field_model_save_key(const wchar_t * db_field_name) - { - DbConnector * db_connector = model_connector->get_db_connector(); - PT::Log * plog = model_connector->get_logger(); - - if( db_connector ) - { - DbExpression * db_expression = db_connector->get_expression(); - - if( db_expression && !is_empty_field(db_field_name) && model_env->field_value_helper_tab ) - { - int old_work_mode = model_env->model_work_mode; - model_env->model_work_mode = MORM_MODEL_WORK_MODE_ITERATE_PRIMARY_KEY_VALUES; - model_env->field_index = 0; - map_fields(); - model_env->model_work_mode = old_work_mode; - - if( model_env->field_value_helper_tab->empty() && plog ) - { - (*plog) << PT::Log::log1 << "Morm: I cannot find a primary key in " << get_table_name() << PT::Log::logend; - } - } - } - } - - - /* - * now we iterate through fields in field_model and save primary key values from *this object to the specified fields in field_model - */ - void field_model_set_parent_key_in_child(const wchar_t * db_field_name, Model & field_model) - { - DbConnector * db_connector = model_connector->get_db_connector(); - PT::Log * log = model_connector->get_logger(); - - if( db_connector ) - { - DbExpression * db_expression = db_connector->get_expression(); - - if( db_expression && !is_empty_field(db_field_name) && model_env->field_value_helper_tab ) - { - std::vector & helper_tab = *model_env->field_value_helper_tab; - - if( (size_t)model_env->field_index == helper_tab.size() ) - { - ModelEnv model_env_local; - model_env_local.copy_global_objects(*model_env); - model_env_local.has_primary_key_set = field_model.has_primary_key_set; - model_env_local.model_work_mode = MORM_MODEL_WORK_MODE_SET_FIELD_VALUE; - model_env_local.field_value_helper_tab = &helper_tab; - model_env_local.field_index = 0; - field_model.model_env = &model_env_local; - field_model.prepare_table(); - - field_model.map_fields(); - - if( (size_t)field_model.model_env->field_index != helper_tab.size() && log ) - { - if( field_model.model_env->field_index == 0 ) - { - (*log) << PT::Log::log1 << "Morm: there is no a foreign key in " << field_model.get_table_name() - << " called " << db_field_name << " pointing to " << get_table_name() << PT::Log::logend; - } - else - { - (*log) << PT::Log::log1 << "Morm: primary key in " << get_table_name() << " consists of " << model_env->field_index << " column(s)" - << " but foreign key in " << field_model.get_table_name() << " consists of " - << field_model.model_env->field_index << " column(s)" << PT::Log::logend; - } - } - - field_model.model_env = nullptr; - } - else - if( log ) - { - (*log) << PT::Log::log1 << "Morm: primary key in " << get_table_name() << " consists of incorrect number of columns" - << ", expected " << helper_tab.size() << " column(s) but got " << model_env->field_index << PT::Log::logend; - } - } - } - } - - - void field_model_set_parent_key(const wchar_t * db_field_name, Model & field_model) - { - FieldValueHelper helper; - helper.db_field_name = db_field_name; - helper.flat_field_name = nullptr; - helper.compare_flat_field_name = false; - - std::vector helper_tab; - helper_tab.push_back(helper); - // only one column at the moment, in the future we can have a primary key from more than one column - - model_env->field_value_helper_tab = &helper_tab; - - field_model_save_key(db_field_name); - field_model_set_parent_key_in_child(db_field_name, field_model); - - model_env->field_value_helper_tab = nullptr; - } - - - void field_model_iterate_through_childs(const wchar_t * db_field_name, Model & field_model) - { - if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_INSERT ) - { - field_model.insert_tree(true); - } - - if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_UPDATE ) - { - field_model.update_tree(true); - } - - if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_REMOVE ) - { - field_model.remove_tree(true); - } - - if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_SAVE ) - { - field_model.save_tree(true); - } - } - - - - void field_model_generate_flat_string(const wchar_t * flat_field_name, Model & field_model, FT field_type) - { - FlatConnector * flat_connector = model_connector->get_flat_connector(); - - if( flat_connector ) - { - FlatExpression * flat_expression = flat_connector->get_expression(); - - if( flat_expression ) - { - if( model_env->dump_mode || field_model.save_mode == DO_INSERT_ON_SAVE || field_model.save_mode == DO_UPDATE_ON_SAVE ) - { - field_model.model_env->model_work_mode = MORM_MODEL_WORK_MODE_GENERATING_FLAT_STRING; - flat_expression->field_model(flat_field_name, field_model, field_type, model_env); - } - } - } - } - - - void field_model_generate_db_sql(const wchar_t * db_field_name, Model & field_model, FT field_type) - { - DbConnector * db_connector = model_connector->get_db_connector(); - - if( db_connector ) - { - DbExpression * db_expression = db_connector->get_expression(); - - if( db_expression && !is_empty_field(db_field_name) ) - { - field_model.model_env->model_work_mode = MORM_MODEL_WORK_MODE_GENERATING_DB_SQL; - - if( db_expression->get_output_type() == MORM_OUTPUT_TYPE_SELECT_COLUMNS ) - { - field_model_left_join(db_field_name, field_model, field_type, db_expression); - } - - if( field_type.is_foreign_key() ) - { - if( db_expression->get_work_mode() == MORM_WORK_MODE_MODEL_FIELDS && db_expression->get_output_type() == MORM_OUTPUT_TYPE_DB_INSERT ) - { - if( field_type.is_insertable() ) - { - int not_used_object = 0; - db_expression->field(db_field_name, not_used_object, field_type, model_env); - } - } - - if( db_expression->get_work_mode() == MORM_WORK_MODE_MODEL_VALUES && db_expression->get_output_type() == MORM_OUTPUT_TYPE_DB_INSERT ) - { - if( field_type.is_insertable() ) - { - db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_INSERT_PRIMARY_KEY); - field_model.map_fields(); - db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_INSERT); - } - } - - if( db_expression->get_work_mode() == MORM_WORK_MODE_MODEL_FIELDS_VALUES && db_expression->get_output_type() == MORM_OUTPUT_TYPE_DB_UPDATE ) - { - if( field_type.is_updatable() ) - { - std::vector key_fields; - key_fields.push_back(db_field_name); // at the moment only one key - - db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE_PRIMARY_KEY); - field_model.model_env->field_index = 0; - field_model.model_env->set_field_name_helper = &key_fields; - field_model.map_fields(); - db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE); - - if( (size_t)field_model.model_env->field_index != key_fields.size() ) - { - // IMPROVEME - // number of keys are different - // put error log here - } - } - } - } - - if( db_expression->get_output_type() != MORM_OUTPUT_TYPE_JOIN_TABLES && - db_expression->get_output_type() != MORM_OUTPUT_TYPE_DB_PRIMARY_KEY && - db_expression->get_output_type() != MORM_OUTPUT_TYPE_DB_INSERT && - db_expression->get_output_type() != MORM_OUTPUT_TYPE_DB_UPDATE ) - { - field_model.map_fields(); - } - - field_model.model_env->model_work_mode = MORM_MODEL_WORK_MODE_NONE; - } - } - } - - - void field_model_clear_values(Model & field_model) - { - Clearer * clearer = model_connector->get_clearer(); - - if( clearer ) - { - clearer->clear_model(field_model); - } - } - - - void field_model_read_values_from_queryresult(const wchar_t * db_field_name, Model & field_model, FT field_type) - { - DbConnector * db_connector = model_connector->get_db_connector(); - - if( db_connector ) - { - DbExpression * db_expression = db_connector->get_expression(); - - if( db_expression ) - { - if( model_env->cursor_helper && - !model_env->cursor_helper->has_autogenerated_select && - model_env->cursor_helper->use_table_prefix_for_fetching_values ) - { - field_model.model_env->add_table_name_to_finder_helper(); - } - - field_model.before_select(); - field_model.map_values_from_query(); - - if( field_model.found() ) - { - field_model.after_select(); - } - } - } - } - - - void field_model(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model, FT field_type) - { - if( model_connector && model_env ) - { - ModelEnv model_env_local; - model_env_local.copy_global_objects(*model_env); - - field_model.model_env = &model_env_local; - field_model.model_env->has_primary_key_set = field_model.has_primary_key_set; - field_model.set_connector(model_connector); - - if( !is_empty_field(db_field_name) ) - { - field_model.prepare_table(); - - if( field_type.is_foreign_key() || field_type.is_foreign_key_in_child() ) - { - field_model_for_db(db_field_name, field_model, field_type); - } - else - { - PT::Log * plog = model_connector->get_logger(); - - if( plog ) - { - (*plog) << PT::Log::log1 << "Morm: error in " << get_table_name_with_field(db_field_name) - << " field, you should set FT::is_foreign_key or FT::is_foreign_key_in_child flag for a model child object" << PT::Log::logend; - } - } - } - - if( !is_empty_field(flat_field_name) ) - { - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_FLAT_STRING ) - { - // calling field_model.prepare_table(); is not needed in generating strings (at least for json/space formats) - field_model_generate_flat_string(flat_field_name, field_model, field_type); - } - } - - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_CLEARING_VALUE ) - { - field_model_clear_values(field_model); - } - - field_model.model_env = nullptr; - } - } - - - void field_model_for_db(const wchar_t * db_field_name, Model & field_model, FT field_type) - { - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_SET_PARENT_ID ) - { - if( field_type.is_foreign_key_in_child() ) - { - field_model_set_parent_key(db_field_name, field_model); - } - } - - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITH_FOREIGN_KEY ) - { - if( field_type.is_foreign_key() ) - { - field_model_iterate_through_childs(db_field_name, field_model); - } - } - - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITHOUT_FOREIGN_KEY ) - { - if( field_type.is_foreign_key_in_child() ) - { - field_model_iterate_through_childs(db_field_name, field_model); - } - } - - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_DB_SQL ) - { - field_model_generate_db_sql(db_field_name, field_model, field_type); - } - - - if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_READING_VALUE_FROM_DB_RESULTSET ) - { - field_model_read_values_from_queryresult(db_field_name, field_model, field_type); - } - } + void field_model_left_join(const wchar_t * db_field_name, Model & field_model, FT field_type, DbExpression * db_expression); + void field_model_save_key(const wchar_t * db_field_name); + void field_model_set_parent_key_in_child(const wchar_t * db_field_name, Model & field_model); + void field_model_set_parent_key(const wchar_t * db_field_name, Model & field_model); + void field_model_iterate_through_childs(const wchar_t * db_field_name, Model & field_model); + void field_model_generate_flat_string(const wchar_t * flat_field_name, Model & field_model, FT field_type); + void field_model_generate_db_sql(const wchar_t * db_field_name, Model & field_model, FT field_type); + void field_model_clear_values(Model & field_model); + void field_model_read_values_from_queryresult(const wchar_t * db_field_name, Model & field_model, FT field_type); + void field_model(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model, FT field_type); + void field_model_for_db(const wchar_t * db_field_name, Model & field_model, FT field_type); template @@ -1259,16 +846,18 @@ protected: if( model_env->cursor_helper->use_table_prefix_for_fetching_values && model_env->finder_helper ) { - // CHECK what about escaping field names here? + DbExpression * db_expression = db_connector->get_expression(); - std::wstring table_field_name; - PT::TextStream table_field_name_str; + if( db_expression ) + { + std::wstring table_field_name; + PT::TextStream table_field_name_str; - put_table_name_with_index(table_field_name_str); - table_field_name_str << '.'; - table_field_name_str << field_name; - table_field_name_str.to_string(table_field_name); - column_index = model_env->cursor_helper->query_result->get_column_index(table_field_name.c_str()); + // CHECK ME not tested yet after changing to db_expression->table_with_index_and_field_to_stream() + db_expression->table_with_index_and_field_to_stream(table_field_name_str, model_env->table_name, model_env->table_index, field_name, field_type); + table_field_name_str.to_string(table_field_name); + column_index = model_env->cursor_helper->query_result->get_column_index(table_field_name.c_str()); + } } else { @@ -1295,15 +884,7 @@ protected: } - virtual void set_parent_key_in_childs() - { - if( model_env ) - { - model_env->model_work_mode = MORM_MODEL_WORK_MODE_SET_PARENT_ID; - map_fields(); - } - } - + virtual void set_parent_key_in_childs(); public: @@ -1385,8 +966,6 @@ protected: virtual bool is_empty_field(const wchar_t * value); virtual bool is_the_same_field(const wchar_t * field1, const wchar_t * field2); - virtual void put_table_name_with_index(PT::TextStream & str); - virtual void put_to_log(const wchar_t * str); virtual void put_fields_to_log(PT::Log & log, const wchar_t * db_field_name, const wchar_t * flat_field_name); From aadc5be3504f2a3f0246f4dc3ee048701abbb29e Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Wed, 12 May 2021 04:53:23 +0200 Subject: [PATCH 15/27] FT field_type changed to const FT & field_type in functions arguments --- src/baseexpression.cpp | 90 +++++++++++++------------- src/baseexpression.h | 106 +++++++++++++++---------------- src/dbconnector.cpp | 46 +++++++------- src/dbconnector.h | 46 +++++++------- src/dbexpression.cpp | 6 +- src/dbexpression.h | 8 +-- src/flatexpression.cpp | 4 +- src/flatexpression.h | 4 +- src/jsonexpression.cpp | 6 +- src/jsonexpression.h | 6 +- src/model.cpp | 12 ++-- src/model.h | 120 +++++++++++++++++------------------ src/postgresqlexpression.cpp | 8 +-- src/postgresqlexpression.h | 8 +-- 14 files changed, 235 insertions(+), 235 deletions(-) diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index 11ef30d..03d024d 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -148,7 +148,7 @@ void BaseExpression::after_generate_from_model() } -bool BaseExpression::can_field_be_generated(FT) +bool BaseExpression::can_field_be_generated(const FT &) { return true; } @@ -208,7 +208,7 @@ bool BaseExpression::is_long_field_name(const PT::TextStream & field_name) } -void BaseExpression::put_field_name(const wchar_t * field_name, FT field_type, ModelEnv * model_env) +void BaseExpression::put_field_name(const wchar_t * field_name, const FT & field_type, ModelEnv * model_env) { if( field_type.is_raw_field_name() ) { @@ -283,7 +283,7 @@ void BaseExpression::put_short_field_name(const wchar_t * field_name, ModelEnv * } -void BaseExpression::save_foreign_key(const wchar_t * field_name, FT field_type, ModelEnv * model_env) +void BaseExpression::save_foreign_key(const wchar_t * field_name, const FT & field_type, ModelEnv * model_env) { PT::TextStream str; PT::TextStream * old_out_stream = out_stream; @@ -332,93 +332,93 @@ void BaseExpression::after_second_part_long_field_name() -void BaseExpression::before_field_value(const std::wstring &, FT field_type) +void BaseExpression::before_field_value(const std::wstring &, const FT & field_type) { before_field_value_string(field_type); } -void BaseExpression::before_field_value(const std::string &, FT field_type) +void BaseExpression::before_field_value(const std::string &, const FT & field_type) { before_field_value_string(field_type); } -void BaseExpression::after_field_value(const std::wstring &, FT field_type) +void BaseExpression::after_field_value(const std::wstring &, const FT & field_type) { after_field_value_string(field_type); } -void BaseExpression::after_field_value(const std::string &, FT field_type) +void BaseExpression::after_field_value(const std::string &, const FT & field_type) { after_field_value_string(field_type); } -void BaseExpression::before_field_value(const wchar_t *, FT field_type) +void BaseExpression::before_field_value(const wchar_t *, const FT & field_type) { before_field_value_string(field_type); } -void BaseExpression::after_field_value(const wchar_t *, FT field_type) +void BaseExpression::after_field_value(const wchar_t *, const FT & field_type) { after_field_value_string(field_type); } -void BaseExpression::before_field_value(const char *, FT field_type) +void BaseExpression::before_field_value(const char *, const FT & field_type) { before_field_value_string(field_type); } -void BaseExpression::after_field_value(const char *, FT field_type) +void BaseExpression::after_field_value(const char *, const FT & field_type) { after_field_value_string(field_type); } -void BaseExpression::before_field_value(wchar_t, FT field_type) +void BaseExpression::before_field_value(wchar_t, const FT & field_type) { before_field_value_string(field_type); } -void BaseExpression::after_field_value(wchar_t, FT field_type) +void BaseExpression::after_field_value(wchar_t, const FT & field_type) { after_field_value_string(field_type); } -void BaseExpression::before_field_value(char, FT field_type) +void BaseExpression::before_field_value(char, const FT & field_type) { before_field_value_string(field_type); } -void BaseExpression::after_field_value(char, FT field_type) +void BaseExpression::after_field_value(char, const FT & field_type) { after_field_value_string(field_type); } -void BaseExpression::before_field_value(const PT::Date &, FT field_type) +void BaseExpression::before_field_value(const PT::Date &, const FT & field_type) { before_field_value_string(field_type); } -void BaseExpression::after_field_value(const PT::Date &, FT field_type) +void BaseExpression::after_field_value(const PT::Date &, const FT & field_type) { after_field_value_string(field_type); } -void BaseExpression::before_field_value(const PT::Space &, FT field_type) +void BaseExpression::before_field_value(const PT::Space &, const FT & field_type) { before_field_value_string(field_type); } -void BaseExpression::after_field_value(const PT::Space &, FT field_type) +void BaseExpression::after_field_value(const PT::Space &, const FT & field_type) { after_field_value_string(field_type); } @@ -448,7 +448,7 @@ void BaseExpression::char_to_hex(char c, PT::TextStream & stream) -void BaseExpression::esc(char val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(char val, PT::TextStream & stream, const FT & field_type) { if( field_type.is_binary() || field_type.is_hexadecimal() ) { @@ -461,13 +461,13 @@ void BaseExpression::esc(char val, PT::TextStream & stream, FT field_type) } -void BaseExpression::esc(unsigned char val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(unsigned char val, PT::TextStream & stream, const FT & field_type) { esc(static_cast(val), stream, field_type); } -void BaseExpression::esc(wchar_t val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(wchar_t val, PT::TextStream & stream, const FT & field_type) { if( field_type.use_utf8() ) { @@ -487,7 +487,7 @@ void BaseExpression::esc(wchar_t val, PT::TextStream & stream, FT field_type) } -void BaseExpression::esc(const wchar_t * val, bool has_known_length, size_t len, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(const wchar_t * val, bool has_known_length, size_t len, PT::TextStream & stream, const FT & field_type) { if( field_type.use_utf8() ) { @@ -513,19 +513,19 @@ void BaseExpression::esc(const wchar_t * val, bool has_known_length, size_t len, } -void BaseExpression::esc(const std::wstring & val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(const std::wstring & val, PT::TextStream & stream, const FT & field_type) { esc(val.c_str(), true, val.size(), stream, field_type); } -void BaseExpression::esc(const wchar_t * val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(const wchar_t * val, PT::TextStream & stream, const FT & field_type) { esc(val, false, 0, stream, field_type); } -void BaseExpression::esc(const std::string & val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(const std::string & val, PT::TextStream & stream, const FT & field_type) { for(size_t i = 0 ; i < val.size() ; ++i) { @@ -534,7 +534,7 @@ void BaseExpression::esc(const std::string & val, PT::TextStream & stream, FT fi } -void BaseExpression::esc(const char * val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(const char * val, PT::TextStream & stream, const FT & field_type) { for(size_t i = 0 ; val[i] != 0 ; ++i) { @@ -543,7 +543,7 @@ void BaseExpression::esc(const char * val, PT::TextStream & stream, FT field_typ } -void BaseExpression::esc(bool val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(bool val, PT::TextStream & stream, const FT & field_type) { if( val ) stream << "true"; @@ -552,80 +552,80 @@ void BaseExpression::esc(bool val, PT::TextStream & stream, FT field_type) } -void BaseExpression::esc(short val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(short val, PT::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(unsigned short val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(unsigned short val, PT::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(int val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(int val, PT::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(unsigned int val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(unsigned int val, PT::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(long val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(long val, PT::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(unsigned long val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(unsigned long val, PT::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(long long val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(long long val, PT::TextStream & stream, const FT & field_type) { // not implemented in PT::TextStream yet //stream << val; } -void BaseExpression::esc(unsigned long long val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(unsigned long long val, PT::TextStream & stream, const FT & field_type) { //stream << val; } -void BaseExpression::esc(float val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(float val, PT::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(double val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(double val, PT::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(long double val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(long double val, PT::TextStream & stream, const FT & field_type) { // IMPLEMENT ME in PT::TextStream //stream << val; } -void BaseExpression::esc(const PT::Date & date, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(const PT::Date & date, PT::TextStream & stream, const FT & field_type) { stream << date; } -void BaseExpression::esc(const PT::TextStream & val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(const PT::TextStream & val, PT::TextStream & stream, const FT & field_type) { PT::TextStream::const_iterator i = val.begin(); @@ -635,7 +635,7 @@ void BaseExpression::esc(const PT::TextStream & val, PT::TextStream & stream, FT } } -void BaseExpression::esc(const PT::WTextStream & val, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(const PT::WTextStream & val, PT::TextStream & stream, const FT & field_type) { PT::WTextStream::const_iterator i = val.begin(); @@ -645,7 +645,7 @@ void BaseExpression::esc(const PT::WTextStream & val, PT::TextStream & stream, F } } -void BaseExpression::esc(const PT::Space & space, PT::TextStream & stream, FT field_type) +void BaseExpression::esc(const PT::Space & space, PT::TextStream & stream, const FT & field_type) { PT::WTextStream tmp_stream; @@ -763,11 +763,11 @@ void BaseExpression::esc(const PT::Space & space, PT::TextStream & stream, FT fi //} -void BaseExpression::before_field_value_string(FT field_type) +void BaseExpression::before_field_value_string(const FT & field_type) { } -void BaseExpression::after_field_value_string(FT field_type) +void BaseExpression::after_field_value_string(const FT & field_type) { } diff --git a/src/baseexpression.h b/src/baseexpression.h index 23a7a76..d00bad2 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -76,7 +76,7 @@ public: template - void field(const wchar_t * field_name, const FieldValue & field_value, FT field_type, ModelEnv * model_env) + void field(const wchar_t * field_name, const FieldValue & field_value, const FT & field_type, ModelEnv * model_env) { if( out_stream && can_field_be_generated(field_type) ) { @@ -127,7 +127,7 @@ public: template - void put_field_value_or_null(const FieldValue & field_value, FT field_type, ModelEnv * model_env) + void put_field_value_or_null(const FieldValue & field_value, const FT & field_type, ModelEnv * model_env) { if( field_type.is_primary_key() ) { @@ -164,7 +164,7 @@ public: template - void field_list(const wchar_t * field_name, ModelContainer & field_value, FT field_type, ModelConnector * model_connector, ModelEnv * model_env) + void field_list(const wchar_t * field_name, ModelContainer & field_value, const FT & field_type, ModelConnector * model_connector, ModelEnv * model_env) { if( out_stream && can_field_be_generated(field_type) ) { @@ -187,7 +187,7 @@ public: } template - void field_model(const wchar_t * field_name, ModelClass & field_model, FT field_type, ModelEnv * model_env) + void field_model(const wchar_t * field_name, ModelClass & field_model, const FT & field_type, ModelEnv * model_env) { if( out_stream && can_field_be_generated(field_type) ) { @@ -215,7 +215,7 @@ public: } template - void field_to_stream(PT::TextStream & stream, const wchar_t * field_name, const FieldValue & field_value, FT field_type, ModelEnv * model_env) + void field_to_stream(PT::TextStream & stream, const wchar_t * field_name, const FieldValue & field_value, const FT & field_type, ModelEnv * model_env) { this->out_stream = &stream; field(field_name, field_value, field_type, model_env); @@ -247,35 +247,35 @@ public: * esc for: signed char, wchar_t, char16_t, char32_t * */ - virtual void esc(char val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(unsigned char val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(char val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(unsigned char val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(wchar_t val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(wchar_t val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(const std::wstring & val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(const wchar_t * val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(const std::wstring & val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(const wchar_t * val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(const std::string & val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(const char * val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(const std::string & val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(const char * val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(bool val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(short val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(unsigned short val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(int val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(unsigned int val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(long val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(unsigned long val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(long long val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(unsigned long long val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(float val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(double val, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(long double val, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(bool val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(short val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(unsigned short val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(int val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(unsigned int val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(long val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(unsigned long val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(long long val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(unsigned long long val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(float val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(double val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(long double val, PT::TextStream & stream, const FT & field_type = FT::default_type); //virtual void esc(void* val, PT::TextStream & stream); - virtual void esc(const PT::Date & date, PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(const PT::TextStream & val,PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(const PT::WTextStream & val,PT::TextStream & stream, FT field_type = FT::default_type); - virtual void esc(const PT::Space & space, PT::TextStream & stream, FT field_type = FT::default_type); + virtual void esc(const PT::Date & date, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(const PT::TextStream & val,PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(const PT::WTextStream & val,PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(const PT::Space & space, PT::TextStream & stream, const FT & field_type = FT::default_type); virtual bool is_long_field_name(const wchar_t * field_name); virtual bool is_long_field_name(const PT::TextStream & table_name); @@ -294,17 +294,17 @@ protected: virtual void before_generate_from_model(); virtual void after_generate_from_model(); - virtual bool can_field_be_generated(FT); + virtual bool can_field_be_generated(const FT &); virtual void field_before(); virtual void field_after(); - virtual void put_field_name(const wchar_t * field_name, FT field_type, ModelEnv * model_env); + virtual void put_field_name(const wchar_t * field_name, const FT & field_type, ModelEnv * model_env); - virtual void save_foreign_key(const wchar_t * field_name, FT field_type, ModelEnv * model_env); + virtual void save_foreign_key(const wchar_t * field_name, const FT & field_type, ModelEnv * model_env); virtual void dump_additional_info(Model & model); template - void put_field_value(const FieldValue & field_value, FT field_type) + void put_field_value(const FieldValue & field_value, const FT & field_type) { if( out_stream ) { @@ -632,37 +632,37 @@ protected: virtual void put_long_field_name(const wchar_t * field_name); virtual void put_short_field_name(const wchar_t * field_name, ModelEnv * model_env); - virtual void before_field_value(const std::wstring &, FT field_type); - virtual void after_field_value(const std::wstring &, FT field_type); + virtual void before_field_value(const std::wstring &, const FT & field_type); + virtual void after_field_value(const std::wstring &, const FT & field_type); - virtual void before_field_value(const std::string &, FT field_type); - virtual void after_field_value(const std::string &, FT field_type); + virtual void before_field_value(const std::string &, const FT & field_type); + virtual void after_field_value(const std::string &, const FT & field_type); - virtual void before_field_value(const wchar_t *, FT field_type); - virtual void after_field_value(const wchar_t *, FT field_type); + virtual void before_field_value(const wchar_t *, const FT & field_type); + virtual void after_field_value(const wchar_t *, const FT & field_type); - virtual void before_field_value(const char *, FT field_type); - virtual void after_field_value(const char *, FT field_type); + virtual void before_field_value(const char *, const FT & field_type); + virtual void after_field_value(const char *, const FT & field_type); - virtual void before_field_value(wchar_t, FT field_type); - virtual void after_field_value(wchar_t, FT field_type); + virtual void before_field_value(wchar_t, const FT & field_type); + virtual void after_field_value(wchar_t, const FT & field_type); - virtual void before_field_value(char, FT field_type); - virtual void after_field_value(char, FT field_type); + virtual void before_field_value(char, const FT & field_type); + virtual void after_field_value(char, const FT & field_type); - virtual void before_field_value(const PT::Date &, FT field_type); - virtual void after_field_value(const PT::Date &, FT field_type); + virtual void before_field_value(const PT::Date &, const FT & field_type); + virtual void after_field_value(const PT::Date &, const FT & field_type); - virtual void before_field_value(const PT::Space &, FT field_type); - virtual void after_field_value(const PT::Space &, FT field_type); + virtual void before_field_value(const PT::Space &, const FT & field_type); + virtual void after_field_value(const PT::Space &, const FT & field_type); template - void before_field_value(const FieldValue &, FT field_type) + void before_field_value(const FieldValue &, const FT & field_type) { } template - void after_field_value(const FieldValue &, FT field_type) + void after_field_value(const FieldValue &, const FT & field_type) { } @@ -714,13 +714,13 @@ protected: - virtual void before_field_value_string(FT field_type); - virtual void after_field_value_string(FT field_type); + virtual void before_field_value_string(const FT & field_type); + virtual void after_field_value_string(const FT & field_type); char char_to_hex_part(char c); void char_to_hex(char c, PT::TextStream & stream); - void esc(const wchar_t * val, bool has_known_length, size_t len, PT::TextStream & stream, FT field_type = FT::default_type); + void esc(const wchar_t * val, bool has_known_length, size_t len, PT::TextStream & stream, const FT & field_type = FT::default_type); }; diff --git a/src/dbconnector.cpp b/src/dbconnector.cpp index 4b3a4c3..6ec2cab 100644 --- a/src/dbconnector.cpp +++ b/src/dbconnector.cpp @@ -344,7 +344,7 @@ void DbConnector::unescape_hex_string(const char * str, std::string & out) } -void DbConnector::unescape_hex_string(const char * str, std::wstring & out, FT field_type) +void DbConnector::unescape_hex_string(const char * str, std::wstring & out, const FT & field_type) { if( field_type.use_utf8() ) { @@ -373,7 +373,7 @@ void DbConnector::unescape_bin_string(const char * str, std::string & out) } -void DbConnector::unescape_bin_string(const char * str, std::wstring & out, FT field_type) +void DbConnector::unescape_bin_string(const char * str, std::wstring & out, const FT & field_type) { unescape_hex_string(str, out, field_type); } @@ -410,7 +410,7 @@ size_t DbConnector::unescape_hex_char(const char * value_str, char * utf8_str, s // CHECKME need to be tested // returns how many characters were used from value_str -size_t DbConnector::unescape_hex_char(const char * value_str, wchar_t & field_value, FT field_type) +size_t DbConnector::unescape_hex_char(const char * value_str, wchar_t & field_value, const FT & field_type) { size_t len = 0; @@ -456,7 +456,7 @@ size_t DbConnector::unescape_hex_char(const char * value_str, wchar_t & field_va } -size_t DbConnector::unescape_bin_char(const char * value_str, wchar_t & field_value, FT field_type) +size_t DbConnector::unescape_bin_char(const char * value_str, wchar_t & field_value, const FT & field_type) { return unescape_hex_char(value_str, field_value, field_type); } @@ -464,7 +464,7 @@ size_t DbConnector::unescape_bin_char(const char * value_str, wchar_t & field_va // CHECKME need to be tested -void DbConnector::get_value(const char * value_str, char & field_value, FT field_type) +void DbConnector::get_value(const char * value_str, char & field_value, const FT & field_type) { wchar_t c; @@ -495,7 +495,7 @@ void DbConnector::get_value(const char * value_str, char & field_value, FT field // CHECKME need to be tested -void DbConnector::get_value(const char * value_str, unsigned char & field_value, FT field_type) +void DbConnector::get_value(const char * value_str, unsigned char & field_value, const FT & field_type) { char tmp_char; get_value(value_str, tmp_char, field_type); @@ -506,7 +506,7 @@ void DbConnector::get_value(const char * value_str, unsigned char & field_value, // CHECKME need to be tested -void DbConnector::get_value(const char * value_str, wchar_t & field_value, FT field_type) +void DbConnector::get_value(const char * value_str, wchar_t & field_value, const FT & field_type) { field_value = 0; @@ -547,7 +547,7 @@ void DbConnector::get_value(const char * value_str, wchar_t & field_value, FT fi // CHECKME need to be tested -void DbConnector::get_value(const char * value_str, std::wstring & field_value, FT field_type) +void DbConnector::get_value(const char * value_str, std::wstring & field_value, const FT & field_type) { if( field_type.is_binary() ) { @@ -576,7 +576,7 @@ void DbConnector::get_value(const char * value_str, std::wstring & field_value, // CHECKME need to be tested -void DbConnector::get_value(const char * value_str, std::string & field_value, FT field_type) +void DbConnector::get_value(const char * value_str, std::string & field_value, const FT & field_type) { if( field_type.is_binary() ) { @@ -594,7 +594,7 @@ void DbConnector::get_value(const char * value_str, std::string & field_value, F } -void DbConnector::get_value(const char * value_str, bool & field_value, FT field_type) +void DbConnector::get_value(const char * value_str, bool & field_value, const FT & field_type) { // IMPROVE ME // this 't' is locale dependent @@ -602,91 +602,91 @@ void DbConnector::get_value(const char * value_str, bool & field_value, FT field } -void DbConnector::get_value(const char * value_str, short & field_value, FT field_type) +void DbConnector::get_value(const char * value_str, short & field_value, const FT & field_type) { // 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, FT field_type) +void DbConnector::get_value(const char * value_str, unsigned short & field_value, const FT & field_type) { // 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, FT field_type) +void DbConnector::get_value(const char * value_str, int & field_value, const FT & field_type) { // 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, FT field_type) +void DbConnector::get_value(const char * value_str, unsigned int & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = PT::Toui(value_str, 10); } -void DbConnector::get_value(const char * value_str, long & field_value, FT field_type) +void DbConnector::get_value(const char * value_str, long & field_value, const FT & field_type) { // 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, FT field_type) +void DbConnector::get_value(const char * value_str, unsigned long & field_value, const FT & field_type) { // 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, FT field_type) +void DbConnector::get_value(const char * value_str, long long & field_value, const FT & field_type) { // 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, FT field_type) +void DbConnector::get_value(const char * value_str, unsigned long long & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = PT::Toull(value_str, 10); } -void DbConnector::get_value(const char * value_str, float & field_value, FT field_type) +void DbConnector::get_value(const char * value_str, float & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = strtof(value_str, 0); } -void DbConnector::get_value(const char * value_str, double & field_value, FT field_type) +void DbConnector::get_value(const char * value_str, double & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = strtod(value_str, 0); } -void DbConnector::get_value(const char * value_str, long double & field_value, FT field_type) +void DbConnector::get_value(const char * value_str, long double & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking field_value = strtold(value_str, 0); } -void DbConnector::get_value(const char * value_str, PT::Date & field_value, FT field_type) +void DbConnector::get_value(const char * value_str, PT::Date & field_value, const FT & field_type) { // IMPROVE ME give some log if parsing failed field_value.Parse(value_str); } -void DbConnector::get_value(const char * value_str, PT::Space & field_value, FT field_type) +void DbConnector::get_value(const char * value_str, PT::Space & field_value, const FT & field_type) { field_value.clear(); diff --git a/src/dbconnector.h b/src/dbconnector.h index d7e3b2e..2a93cd3 100644 --- a/src/dbconnector.h +++ b/src/dbconnector.h @@ -91,25 +91,25 @@ public: virtual bool query_insert(const PT::TextStream & stream, QueryResult & query_result); virtual bool query_remove(const PT::TextStream & stream, QueryResult & query_result); - virtual void get_value(const char * value_str, char & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, unsigned char & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, wchar_t & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, std::wstring & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, std::string & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, bool & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, short & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, unsigned short & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, int & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, unsigned int & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, long & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, unsigned long & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, long long & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, unsigned long long & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, float & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, double & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, long double & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, PT::Date & field_value, FT field_type = FT::default_type); - virtual void get_value(const char * value_str, PT::Space & field_value, FT field_type = FT::default_type); + virtual void get_value(const char * value_str, char & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, unsigned char & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, wchar_t & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, std::wstring & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, std::string & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, bool & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, short & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, unsigned short & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, int & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, unsigned int & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, long & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, unsigned long & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, long long & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, unsigned long long & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, float & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, double & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, long double & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, PT::Date & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, PT::Space & field_value, const FT & field_type = FT::default_type); // add get_value for PT::TextStream and PT::WTextStream @@ -149,14 +149,14 @@ protected: virtual const char * query_last_sequence(const wchar_t * sequence_table_name); virtual void unescape_hex_string(const char * str, std::string & out); - virtual void unescape_hex_string(const char * str, std::wstring & out, FT field_type); + virtual void unescape_hex_string(const char * str, std::wstring & out, const FT & field_type); virtual void unescape_bin_string(const char * str, std::string & out); - virtual void unescape_bin_string(const char * str, std::wstring & out, FT field_type); + virtual void unescape_bin_string(const char * str, std::wstring & out, const FT & field_type); virtual size_t unescape_hex_char(const char * value_str, char * utf8_str, size_t utf8_str_max_len); - virtual size_t unescape_hex_char(const char * value_str, wchar_t & field_value, FT field_type); - virtual size_t unescape_bin_char(const char * value_str, wchar_t & field_value, FT field_type); + virtual size_t unescape_hex_char(const char * value_str, wchar_t & field_value, const FT & field_type); + virtual size_t unescape_bin_char(const char * value_str, wchar_t & field_value, const FT & field_type); private: diff --git a/src/dbexpression.cpp b/src/dbexpression.cpp index 5bf82d4..913d321 100644 --- a/src/dbexpression.cpp +++ b/src/dbexpression.cpp @@ -62,7 +62,7 @@ int DbExpression::get_output_type() } -bool DbExpression::can_field_be_generated(FT field_type) +bool DbExpression::can_field_be_generated(const FT & field_type) { if( output_type == MORM_OUTPUT_TYPE_DB_INSERT ) { @@ -183,13 +183,13 @@ void DbExpression::put_name_value_separator() -void DbExpression::before_field_value_string(FT field_type) +void DbExpression::before_field_value_string(const FT & field_type) { (*out_stream) << "'"; } -void DbExpression::after_field_value_string(FT field_type) +void DbExpression::after_field_value_string(const FT & field_type) { (*out_stream) << "'"; } diff --git a/src/dbexpression.h b/src/dbexpression.h index 5c4cbdb..c09d579 100644 --- a/src/dbexpression.h +++ b/src/dbexpression.h @@ -63,7 +63,7 @@ public: virtual DbExpression & page(PT::TextStream & stream, size_t page_number, size_t page_size); template - void add_field_for_select(const wchar_t * new_column_expression, const wchar_t * new_column_name, FieldValue & field_value, FT field_type, ModelEnv * model_env) + void add_field_for_select(const wchar_t * new_column_expression, const wchar_t * new_column_name, FieldValue & field_value, const FT & field_type, ModelEnv * model_env) { std::wstring column_expression; // field() methods can be called recursively, so don't make it as class object @@ -81,15 +81,15 @@ protected: int output_type; std::vector conjunctions; - bool can_field_be_generated(FT field_type); + bool can_field_be_generated(const FT & field_type); void field_before(); void put_name_value_separator(); private: - void before_field_value_string(FT field_type); - void after_field_value_string(FT field_type); + void before_field_value_string(const FT & field_type); + void after_field_value_string(const FT & field_type); }; diff --git a/src/flatexpression.cpp b/src/flatexpression.cpp index 0a31f52..884604a 100644 --- a/src/flatexpression.cpp +++ b/src/flatexpression.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018, Tomasz Sowa + * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -39,7 +39,7 @@ namespace morm { -void FlatExpression::esc(const PT::Date & date, PT::TextStream & stream, FT field_type) +void FlatExpression::esc(const PT::Date & date, PT::TextStream & stream, const FT & field_type) { date.SerializeISO(stream); } diff --git a/src/flatexpression.h b/src/flatexpression.h index fc45823..87e362e 100644 --- a/src/flatexpression.h +++ b/src/flatexpression.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018, Tomasz Sowa + * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -45,7 +45,7 @@ class FlatExpression : public BaseExpression { public: - void esc(const PT::Date & date, PT::TextStream & stream, FT field_type); + void esc(const PT::Date & date, PT::TextStream & stream, const FT & field_type); diff --git a/src/jsonexpression.cpp b/src/jsonexpression.cpp index a2d18dd..bb15a4f 100644 --- a/src/jsonexpression.cpp +++ b/src/jsonexpression.cpp @@ -105,12 +105,12 @@ void JSONExpression::after_second_part_long_field_name() } -void JSONExpression::before_field_value_string(FT field_type) +void JSONExpression::before_field_value_string(const FT & field_type) { (*out_stream) << "\""; } -void JSONExpression::after_field_value_string(FT field_type) +void JSONExpression::after_field_value_string(const FT & field_type) { (*out_stream) << "\""; } @@ -134,7 +134,7 @@ void JSONExpression::after_field_value_list() } -void JSONExpression::esc(char val, PT::TextStream & stream, FT field_type) +void JSONExpression::esc(char val, PT::TextStream & stream, const FT & field_type) { if( field_type.is_hexadecimal() || field_type.is_binary() ) { diff --git a/src/jsonexpression.h b/src/jsonexpression.h index a9f39bf..ebe7352 100644 --- a/src/jsonexpression.h +++ b/src/jsonexpression.h @@ -70,13 +70,13 @@ protected: // 'morm::JSONExpression::esc' hides overloaded virtual function [-Woverloaded-virtual] using FlatExpression::esc; - void esc(char val, PT::TextStream & stream, FT field_type); + void esc(char val, PT::TextStream & stream, const FT & field_type); private: - void before_field_value_string(FT field_type); - void after_field_value_string(FT field_type); + void before_field_value_string(const FT & field_type); + void after_field_value_string(const FT & field_type); }; diff --git a/src/model.cpp b/src/model.cpp index f9e9845..efe3f58 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -1006,7 +1006,7 @@ void Model::put_fields_to_log(PT::Log & log, const wchar_t * db_field_name, cons * this is only in a case when has_foreign_key is false, may it can be ignored? we can use unique index in such a case * */ -void Model::field_model_left_join(const wchar_t * db_field_name, Model & field_model, FT field_type, DbExpression * db_expression) +void Model::field_model_left_join(const wchar_t * db_field_name, Model & field_model, const FT & field_type, DbExpression * db_expression) { if( model_env && field_model.model_env && model_env->finder_helper ) { @@ -1208,7 +1208,7 @@ void Model::field_model_iterate_through_childs(const wchar_t * db_field_name, Mo -void Model::field_model_generate_flat_string(const wchar_t * flat_field_name, Model & field_model, FT field_type) +void Model::field_model_generate_flat_string(const wchar_t * flat_field_name, Model & field_model, const FT & field_type) { FlatConnector * flat_connector = model_connector->get_flat_connector(); @@ -1228,7 +1228,7 @@ void Model::field_model_generate_flat_string(const wchar_t * flat_field_name, Mo } -void Model::field_model_generate_db_sql(const wchar_t * db_field_name, Model & field_model, FT field_type) +void Model::field_model_generate_db_sql(const wchar_t * db_field_name, Model & field_model, const FT & field_type) { DbConnector * db_connector = model_connector->get_db_connector(); @@ -1314,7 +1314,7 @@ void Model::field_model_clear_values(Model & field_model) } -void Model::field_model_read_values_from_queryresult(const wchar_t * db_field_name, Model & field_model, FT field_type) +void Model::field_model_read_values_from_queryresult(const wchar_t * db_field_name, Model & field_model, const FT & field_type) { DbConnector * db_connector = model_connector->get_db_connector(); @@ -1343,7 +1343,7 @@ void Model::field_model_read_values_from_queryresult(const wchar_t * db_field_na } -void Model::field_model(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model, FT field_type) +void Model::field_model(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model, const FT & field_type) { if( model_connector && model_env ) { @@ -1393,7 +1393,7 @@ void Model::field_model(const wchar_t * db_field_name, const wchar_t * flat_fiel } -void Model::field_model_for_db(const wchar_t * db_field_name, Model & field_model, FT field_type) +void Model::field_model_for_db(const wchar_t * db_field_name, Model & field_model, const FT & field_type) { if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_SET_PARENT_ID ) { diff --git a/src/model.h b/src/model.h index a9d7e57..bd7472e 100644 --- a/src/model.h +++ b/src/model.h @@ -222,115 +222,115 @@ protected: * field methods for such field_values: signed char, char16_t, char32_t, std::u16string, std::u32string * */ - void field(const wchar_t * field_name, char & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, char & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, unsigned char & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, unsigned char & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, wchar_t & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, wchar_t & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, std::wstring & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, std::wstring & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, std::string & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, std::string & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, bool & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, bool & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, short & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, short & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, unsigned short & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, unsigned short & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, int & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, int & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, unsigned int & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, unsigned int & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, long & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, long & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, unsigned long & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, unsigned long & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, long long & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, long long & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, unsigned long long & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, unsigned long long & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, float & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, float & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, double & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, double & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, long double & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, long double & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, PT::Date & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, PT::Date & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, PT::Space & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, PT::Space & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, Model & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, Model & field_value, const FT & field_type = FT::default_type) { field_model(field_name, field_name, field_value, field_type); } template - void field(const wchar_t * field_name, std::list & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, std::list & field_value, const FT & field_type = FT::default_type) { ModelClass * list_model_null_pointer = nullptr; field_list(field_name, field_name, field_value, list_model_null_pointer, field_type); } template - void field(const wchar_t * field_name, std::vector & field_value, FT field_type = FT::default_type) + void field(const wchar_t * field_name, std::vector & field_value, const FT & field_type = FT::default_type) { ModelClass * list_model_null_pointer = nullptr; field_list(field_name, field_name, field_value, list_model_null_pointer, field_type); @@ -341,116 +341,116 @@ protected: * field methods which take two names: db_field_name and flat_field_name */ - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, char & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, char & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned char & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned char & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, wchar_t & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, wchar_t & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::wstring & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::wstring & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::string & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::string & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, bool & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, bool & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, short & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, short & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned short & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned short & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, int & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, int & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned int & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned int & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, long & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, long & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned long & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned long & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, long long & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, long long & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned long long & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, unsigned long long & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, float & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, float & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, double & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, double & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, long double & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, long double & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, PT::Date & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, PT::Date & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, PT::Space & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, PT::Space & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_value, const FT & field_type = FT::default_type) { // has_foreign_key was here field_model(db_field_name, flat_field_name, field_value, field_type); } template - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::list & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::list & field_value, const FT & field_type = FT::default_type) { ModelClass * list_model_null_pointer = nullptr; field_list(db_field_name, flat_field_name, field_value, list_model_null_pointer, field_type); } template - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::vector & field_value, FT field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, std::vector & field_value, const FT & field_type = FT::default_type) { ModelClass * list_model_null_pointer = nullptr; field_list(db_field_name, flat_field_name, field_value, list_model_null_pointer, field_type); @@ -501,7 +501,7 @@ protected: template - void field_generic_iterate_primary_key_values(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) + void field_generic_iterate_primary_key_values(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, const FT & field_type) { if( field_type.is_primary_key() ) { @@ -521,7 +521,7 @@ protected: template - void field_generic_generate_flat_string(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) + void field_generic_generate_flat_string(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, const FT & field_type) { FlatConnector * flat_connector = model_connector->get_flat_connector(); @@ -538,7 +538,7 @@ protected: template - void field_generic_generate_db_sql(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) + void field_generic_generate_db_sql(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, const FT & field_type) { DbConnector * db_connector = model_connector->get_db_connector(); @@ -555,7 +555,7 @@ protected: template - void field_generic_read_value_from_db_resultset(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) + void field_generic_read_value_from_db_resultset(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, const FT & field_type) { DbConnector * db_connector = model_connector->get_db_connector(); @@ -578,7 +578,7 @@ protected: template - void field_generic_clear_value(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) + void field_generic_clear_value(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, const FT & field_type) { Clearer * clearer = model_connector->get_clearer(); @@ -590,7 +590,7 @@ protected: template - void field_generic(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, FT field_type) + void field_generic(const wchar_t * db_field_name, const wchar_t * flat_field_name, FieldValue & field_value, const FT & field_type) { if( model_connector && model_env ) { @@ -632,17 +632,17 @@ protected: } - void field_model_left_join(const wchar_t * db_field_name, Model & field_model, FT field_type, DbExpression * db_expression); + void field_model_left_join(const wchar_t * db_field_name, Model & field_model, const FT & field_type, DbExpression * db_expression); void field_model_save_key(const wchar_t * db_field_name); void field_model_set_parent_key_in_child(const wchar_t * db_field_name, Model & field_model); void field_model_set_parent_key(const wchar_t * db_field_name, Model & field_model); void field_model_iterate_through_childs(const wchar_t * db_field_name, Model & field_model); - void field_model_generate_flat_string(const wchar_t * flat_field_name, Model & field_model, FT field_type); - void field_model_generate_db_sql(const wchar_t * db_field_name, Model & field_model, FT field_type); + void field_model_generate_flat_string(const wchar_t * flat_field_name, Model & field_model, const FT & field_type); + void field_model_generate_db_sql(const wchar_t * db_field_name, Model & field_model, const FT & field_type); void field_model_clear_values(Model & field_model); - void field_model_read_values_from_queryresult(const wchar_t * db_field_name, Model & field_model, FT field_type); - void field_model(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model, FT field_type); - void field_model_for_db(const wchar_t * db_field_name, Model & field_model, FT field_type); + void field_model_read_values_from_queryresult(const wchar_t * db_field_name, Model & field_model, const FT & field_type); + void field_model(const wchar_t * db_field_name, const wchar_t * flat_field_name, Model & field_model, const FT & field_type); + void field_model_for_db(const wchar_t * db_field_name, Model & field_model, const FT & field_type); template @@ -709,7 +709,7 @@ protected: template - void field_list_generate_flat_string(const wchar_t * flat_field_name, ModelContainer & field_container, FT field_type) + void field_list_generate_flat_string(const wchar_t * flat_field_name, ModelContainer & field_container, const FT & field_type) { FlatConnector * flat_connector = model_connector->get_flat_connector(); @@ -738,7 +738,7 @@ protected: template - void field_list(const wchar_t * db_field_name, const wchar_t * flat_field_name, ModelContainer & field_container, ModelContainerType * model_container_type, FT field_type) + void field_list(const wchar_t * db_field_name, const wchar_t * flat_field_name, ModelContainer & field_container, ModelContainerType * model_container_type, const FT & field_type) { if( model_connector && model_env ) { @@ -809,7 +809,7 @@ protected: template - void get_value_by_field_index(int field_index, FieldValue & field_value, FT field_type) + void get_value_by_field_index(int field_index, FieldValue & field_value, const FT & field_type) { DbConnector * db_connector = model_connector->get_db_connector(); @@ -836,7 +836,7 @@ protected: template - void get_value_by_field_name(const wchar_t * field_name, FieldValue & field_value, FT field_type) + void get_value_by_field_name(const wchar_t * field_name, FieldValue & field_value, const FT & field_type) { DbConnector * db_connector = model_connector->get_db_connector(); diff --git a/src/postgresqlexpression.cpp b/src/postgresqlexpression.cpp index 392c0b7..90b9cf8 100644 --- a/src/postgresqlexpression.cpp +++ b/src/postgresqlexpression.cpp @@ -77,7 +77,7 @@ void PostgreSQLExpression::after_second_part_long_field_name() -void PostgreSQLExpression::before_field_value_string(FT field_type) +void PostgreSQLExpression::before_field_value_string(const FT & field_type) { if( field_type.is_binary() ) { @@ -94,14 +94,14 @@ void PostgreSQLExpression::before_field_value_string(FT field_type) } } -void PostgreSQLExpression::after_field_value_string(FT field_type) +void PostgreSQLExpression::after_field_value_string(const FT & field_type) { (*out_stream) << "'"; } -void PostgreSQLExpression::esc(char val, PT::TextStream & stream, FT field_type) +void PostgreSQLExpression::esc(char val, PT::TextStream & stream, const FT & field_type) { if( field_type.is_hexadecimal() || field_type.is_binary() ) { @@ -123,7 +123,7 @@ void PostgreSQLExpression::esc(char val, PT::TextStream & stream, FT field_type) } -void PostgreSQLExpression::esc(const PT::Date & date, PT::TextStream & stream, FT field_type) +void PostgreSQLExpression::esc(const PT::Date & date, PT::TextStream & stream, const FT & field_type) { stream << date << "+00"; } diff --git a/src/postgresqlexpression.h b/src/postgresqlexpression.h index 3be9a23..fc76a1a 100644 --- a/src/postgresqlexpression.h +++ b/src/postgresqlexpression.h @@ -45,8 +45,8 @@ class PostgreSQLExpression : public DbExpression { public: - void esc(char val, PT::TextStream & stream, FT field_type); - void esc(const PT::Date & date, PT::TextStream & stream, FT field_type); + void esc(char val, PT::TextStream & stream, const FT & field_type); + void esc(const PT::Date & date, PT::TextStream & stream, const FT & field_type); DbExpression & page(PT::TextStream & stream, size_t page_number, size_t page_size); @@ -64,8 +64,8 @@ protected: private: - void before_field_value_string(FT field_type); - void after_field_value_string(FT field_type); + void before_field_value_string(const FT & field_type); + void after_field_value_string(const FT & field_type); }; From 179be2864f681fe6ed0db231867e3403b46748a6 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Wed, 12 May 2021 05:39:31 +0200 Subject: [PATCH 16/27] change FT field_type in PostgreSQLConnector --- src/postgresqlconnector.cpp | 4 ++-- src/postgresqlconnector.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/postgresqlconnector.cpp b/src/postgresqlconnector.cpp index 37acfac..4a31f89 100644 --- a/src/postgresqlconnector.cpp +++ b/src/postgresqlconnector.cpp @@ -560,7 +560,7 @@ void PostgreSQLConnector::log_unsupported_bin_format() -size_t PostgreSQLConnector::unescape_bin_char(const char * str, wchar_t & field_value, FT field_type) +size_t PostgreSQLConnector::unescape_bin_char(const char * str, wchar_t & field_value, const FT & field_type) { if( str[0]!='\\' || str[1]!='x' ) { @@ -588,7 +588,7 @@ void PostgreSQLConnector::unescape_bin_string(const char * str, std::string & ou } -void PostgreSQLConnector::unescape_bin_string(const char * str, std::wstring & out, FT field_type) +void PostgreSQLConnector::unescape_bin_string(const char * str, std::wstring & out, const FT & field_type) { if( str[0]!='\\' || str[1]!='x' ) { diff --git a/src/postgresqlconnector.h b/src/postgresqlconnector.h index 9403d9f..0f771d1 100644 --- a/src/postgresqlconnector.h +++ b/src/postgresqlconnector.h @@ -100,9 +100,9 @@ protected: void log_unsupported_bin_format(); - size_t unescape_bin_char(const char * str, wchar_t & field_value, FT field_type); + size_t unescape_bin_char(const char * str, wchar_t & field_value, const FT & field_type); void unescape_bin_string(const char * str, std::string & out); - void unescape_bin_string(const char * str, std::wstring & out, FT field_type); + void unescape_bin_string(const char * str, std::wstring & out, const FT & field_type); }; From a1537cf8d566a168c2ea93d7d8815cf78f04a6c4 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 13 May 2021 00:19:22 +0200 Subject: [PATCH 17/27] BaseExpression: removed methods: put_long_field_name() and put_short_field_name() now method put_field_name() is not making a test whether field_name is in long format and only puts the field name (without table name), this allows us to have a dot in the column name (field_name) --- src/baseexpression.cpp | 140 ++++++++++++++--------------------------- src/baseexpression.h | 27 ++++---- 2 files changed, 60 insertions(+), 107 deletions(-) diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index 03d024d..d022615 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -169,44 +169,20 @@ void BaseExpression::field_after() } -bool BaseExpression::is_long_field_name(const wchar_t * field_name) + +void BaseExpression::put_field_name_and_table_if_needed(const wchar_t * field_name, const FT & field_type, ModelEnv * model_env) { - bool is_long = false; - - while( *field_name != 0 ) + if( use_prefix && model_env ) { - if( *field_name == '.' ) - { - is_long = true; - break; - } - - field_name += 1; + put_table_with_index_and_field(model_env->table_name, model_env->table_index, field_name, field_type); + } + else + { + put_field_name(field_name, field_type, model_env); } - - return is_long; } -bool BaseExpression::is_long_field_name(const PT::TextStream & field_name) -{ - PT::TextStream::const_iterator i = field_name.begin(); - bool is_long = false; - - while( i != field_name.end() ) - { - if( *i == '.' ) - { - is_long = true; - break; - } - - ++i; - } - - return is_long; -} - void BaseExpression::put_field_name(const wchar_t * field_name, const FT & field_type, ModelEnv * model_env) { @@ -216,73 +192,15 @@ void BaseExpression::put_field_name(const wchar_t * field_name, const FT & field } else { - if( is_long_field_name(field_name) ) - put_long_field_name(field_name); - else - put_short_field_name(field_name, model_env); - } -} - - -const wchar_t * BaseExpression::put_long_part_field_name(const wchar_t * field_name) -{ - while( *field_name != 0 && *field_name != '.' ) - { - esc(*field_name, *out_stream); - field_name += 1; - } - - return field_name; -} - - -void BaseExpression::put_long_field_name(const wchar_t * field_name) -{ - before_first_part_long_field_name(); - field_name = put_long_part_field_name(field_name); - after_first_part_long_field_name(); - - if( *field_name == '.' ) - { - field_name += 1; - (*out_stream) << '.'; - - before_second_part_long_field_name(); - field_name = put_long_part_field_name(field_name); - after_second_part_long_field_name(); - } -} - - -void BaseExpression::put_short_field_name(const wchar_t * field_name, ModelEnv * model_env) -{ - if( use_prefix && model_env ) - { - before_first_part_long_field_name(); - esc(model_env->table_name, *out_stream); - - if( model_env->table_index > 1 ) - { - (*out_stream) << model_env->table_index; - } - - after_first_part_long_field_name(); - - (*out_stream) << '.'; - - before_second_part_long_field_name(); - esc(field_name, *out_stream); - after_second_part_long_field_name(); - } - else - { + // !!!!!!!!!!!!!!!!!!! + // there are too many methods: before... after... before_short_field_name(); esc(field_name, *out_stream); after_short_field_name(); } - } + void BaseExpression::save_foreign_key(const wchar_t * field_name, const FT & field_type, ModelEnv * model_env) { PT::TextStream str; @@ -886,6 +804,26 @@ void BaseExpression::put_table_with_index_and_field(const PT::WTextStream & tabl } +void BaseExpression::put_table_and_field(const wchar_t * table_name, const wchar_t * field_name, const FT & field_type) +{ + if( out_stream ) + { + put_table(table_name); + (*out_stream) << '.'; + put_field_name(field_name, field_type, nullptr); + } +} + +void BaseExpression::put_table_and_field(const PT::WTextStream & table_name, const wchar_t * field_name, const FT & field_type) +{ + if( out_stream ) + { + put_table(table_name); + (*out_stream) << '.'; + put_field_name(field_name, field_type, nullptr); + } +} + void BaseExpression::schema_table_to_stream(PT::TextStream & stream, const wchar_t * schema_name, const wchar_t * table_name) @@ -952,6 +890,22 @@ void BaseExpression::table_with_index_and_field_to_stream(PT::TextStream & strea } +void BaseExpression::table_and_field_to_stream(PT::TextStream & stream, const wchar_t * table_name, const wchar_t * field_name, const FT & field_type) +{ + this->out_stream = &stream; + put_table_and_field(table_name, field_name, field_type); + this->out_stream = nullptr; +} + + +void BaseExpression::table_and_field_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, const wchar_t * field_name, const FT & field_type) +{ + this->out_stream = &stream; + put_table_and_field(table_name, field_name, field_type); + this->out_stream = nullptr; +} + + } diff --git a/src/baseexpression.h b/src/baseexpression.h index d00bad2..1d42d64 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -84,7 +84,7 @@ public: if( work_mode == MORM_WORK_MODE_MODEL_FIELDS ) { - put_field_name(field_name, field_type, model_env); + put_field_name_and_table_if_needed(field_name, field_type, model_env); } else if( work_mode == MORM_WORK_MODE_MODEL_SAVE_FIELDS ) @@ -103,7 +103,7 @@ public: { if( (size_t)model_env->field_index < model_env->set_field_name_helper->size() ) { - put_field_name((*model_env->set_field_name_helper)[model_env->field_index], field_type, model_env); + put_field_name_and_table_if_needed((*model_env->set_field_name_helper)[model_env->field_index], field_type, model_env); put_name_value_separator(); put_field_value_or_null(field_value, field_type, model_env); } @@ -112,7 +112,7 @@ public: } else { - put_field_name(field_name, field_type, model_env); + put_field_name_and_table_if_needed(field_name, field_type, model_env); put_name_value_separator(); put_field_value_or_null(field_value, field_type, model_env); } @@ -172,12 +172,12 @@ public: // if( work_mode == MORM_WORK_MODE_MODEL_FIELDS ) // { -// put_field_name(field_name); +// put_field_name_and_table_if_needed(field_name); // } // else if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES ) { - put_field_name(field_name, field_type, model_env); + put_field_name_and_table_if_needed(field_name, field_type, model_env); put_name_value_separator(); put_field_value_list(field_value, model_connector, model_env); } @@ -195,7 +195,7 @@ public: if( work_mode == MORM_WORK_MODE_MODEL_FIELDS ) { - put_field_name(field_name, field_type, model_env); + put_field_name_and_table_if_needed(field_name, field_type, model_env); } else if( work_mode == MORM_WORK_MODE_MODEL_VALUES ) @@ -205,7 +205,7 @@ public: else if( work_mode == MORM_WORK_MODE_MODEL_FIELDS_VALUES ) { - put_field_name(field_name, field_type, model_env); + put_field_name_and_table_if_needed(field_name, field_type, model_env); put_name_value_separator(); generate_from_model(field_model); } @@ -231,6 +231,8 @@ public: virtual void put_table_with_index(const PT::WTextStream & table_name, int index); virtual void put_table_with_index_and_field(const wchar_t * table_name, int index, const wchar_t * field_name, const FT & field_type); virtual void put_table_with_index_and_field(const PT::WTextStream & table_name, int index, const wchar_t * field_name, const FT & field_type); + virtual void put_table_and_field(const wchar_t * table_name, const wchar_t * field_name, const FT & field_type); + virtual void put_table_and_field(const PT::WTextStream & table_name, const wchar_t * field_name, const FT & field_type); virtual void schema_table_to_stream(PT::TextStream & stream, const wchar_t * schema_name, const wchar_t * table_name); virtual void schema_table_to_stream(PT::TextStream & stream, const PT::WTextStream & schema_name, const PT::WTextStream & table_name); @@ -240,6 +242,8 @@ public: virtual void table_with_index_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, int index); virtual void table_with_index_and_field_to_stream(PT::TextStream & stream, const wchar_t * table_name, int index, const wchar_t * field_name, const FT & field_type); virtual void table_with_index_and_field_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, int index, const wchar_t * field_name, const FT & field_type); + virtual void table_and_field_to_stream(PT::TextStream & stream, const wchar_t * table_name, const wchar_t * field_name, const FT & field_type); + virtual void table_and_field_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, const wchar_t * field_name, const FT & field_type); /* @@ -277,8 +281,6 @@ public: virtual void esc(const PT::WTextStream & val,PT::TextStream & stream, const FT & field_type = FT::default_type); virtual void esc(const PT::Space & space, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual bool is_long_field_name(const wchar_t * field_name); - virtual bool is_long_field_name(const PT::TextStream & table_name); protected: @@ -298,6 +300,7 @@ protected: virtual void field_before(); virtual void field_after(); + virtual void put_field_name_and_table_if_needed(const wchar_t * field_name, const FT & field_type, ModelEnv * model_env); virtual void put_field_name(const wchar_t * field_name, const FT & field_type, ModelEnv * model_env); virtual void save_foreign_key(const wchar_t * field_name, const FT & field_type, ModelEnv * model_env); @@ -588,7 +591,7 @@ protected: this->out_stream = &stream; field_before(); - put_field_name(field_name, FT::default_type, model_env); + put_field_name_and_table_if_needed(field_name, FT::default_type, model_env); put_name_value_separator(); bool is_first = true; @@ -628,10 +631,6 @@ protected: virtual void before_second_part_long_field_name(); virtual void after_second_part_long_field_name(); - virtual const wchar_t * put_long_part_field_name(const wchar_t * field_name); - virtual void put_long_field_name(const wchar_t * field_name); - virtual void put_short_field_name(const wchar_t * field_name, ModelEnv * model_env); - virtual void before_field_value(const std::wstring &, const FT & field_type); virtual void after_field_value(const std::wstring &, const FT & field_type); From 2ad6c8c258ac8e9818d2bfc9d174ffdaae6fdf0c Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 13 May 2021 02:32:03 +0200 Subject: [PATCH 18/27] changed the way how to quote schemas, tables and field names, added escaping table names in insert/update/remove removed methods from BaseExpression: virtual void before_short_field_name(); virtual void after_short_field_name(); virtual void before_first_part_long_field_name(); virtual void after_first_part_long_field_name(); virtual void before_second_part_long_field_name(); virtual void after_second_part_long_field_name(); added methods to BaseExpression: virtual void schema_table_separator(); virtual void table_field_separator(); virtual void before_schema_name(); virtual void after_schema_name(); virtual void before_table_name(); virtual void after_table_name(); virtual void before_field_name(); virtual void after_field_name(); --- src/baseexpression.cpp | 117 +++++++++++++++++++++++------------ src/baseexpression.h | 27 ++++---- src/dbconnector.cpp | 12 ++-- src/dbexpression.cpp | 48 ++++++++++++++ src/dbexpression.h | 12 ++++ src/jsonexpression.cpp | 21 +------ src/jsonexpression.h | 8 +-- src/model.cpp | 94 +++++++++++++++------------- src/model.h | 17 +++-- src/postgresqlexpression.cpp | 37 ----------- src/postgresqlexpression.h | 6 -- 11 files changed, 222 insertions(+), 177 deletions(-) diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index d022615..2e2ff62 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -192,11 +192,9 @@ void BaseExpression::put_field_name(const wchar_t * field_name, const FT & field } else { - // !!!!!!!!!!!!!!!!!!! - // there are too many methods: before... after... - before_short_field_name(); + before_field_name(); esc(field_name, *out_stream); - after_short_field_name(); + after_field_name(); } } @@ -219,36 +217,47 @@ void BaseExpression::save_foreign_key(const wchar_t * field_name, const FT & fie } -void BaseExpression::before_short_field_name() + +void BaseExpression::schema_table_separator() { } -void BaseExpression::after_short_field_name() +void BaseExpression::table_field_separator() { } -void BaseExpression::before_first_part_long_field_name() + +void BaseExpression::before_schema_name() { } -void BaseExpression::after_first_part_long_field_name() +void BaseExpression::after_schema_name() { } -void BaseExpression::before_second_part_long_field_name() +void BaseExpression::before_table_name() { } -void BaseExpression::after_second_part_long_field_name() +void BaseExpression::after_table_name() { } +void BaseExpression::before_field_name() +{ +} + + +void BaseExpression::after_field_name() +{ +} + void BaseExpression::before_field_value(const std::wstring &, const FT & field_type) { @@ -690,36 +699,63 @@ void BaseExpression::after_field_value_string(const FT & field_type) } +/* + * schema_name can be a null pointer or just pointing to an empty string + * in such a case we do not put the schema name + */ void BaseExpression::put_schema_table(const wchar_t * schema_name, const wchar_t * table_name) { if( out_stream ) { - before_first_part_long_field_name(); - esc(schema_name, *out_stream); - after_first_part_long_field_name(); + bool has_schema = false; - (*out_stream) << '.'; + if( !is_empty_field(schema_name) ) + { + has_schema = true; + before_schema_name(); + esc(schema_name, *out_stream); + after_schema_name(); + } - before_second_part_long_field_name(); - esc(table_name, *out_stream); - after_second_part_long_field_name(); + if( !is_empty_field(table_name) ) + { + if( has_schema ) + schema_table_separator(); + + before_table_name(); + esc(table_name, *out_stream); + after_table_name(); + } } } +/* + * schema_name can be empty - in such a case we do not put the schema name + */ void BaseExpression::put_schema_table(const PT::WTextStream & schema_name, const PT::WTextStream & table_name) { if( out_stream ) { - before_first_part_long_field_name(); - esc(schema_name, *out_stream); - after_first_part_long_field_name(); + bool has_schema = false; - (*out_stream) << '.'; + if( !schema_name.empty() ) + { + has_schema = true; + before_schema_name(); + esc(schema_name, *out_stream); + after_schema_name(); + } - before_second_part_long_field_name(); - esc(table_name, *out_stream); - after_second_part_long_field_name(); + if( !table_name.empty() ) + { + if( has_schema ) + schema_table_separator(); + + before_table_name(); + esc(table_name, *out_stream); + after_table_name(); + } } } @@ -728,9 +764,9 @@ void BaseExpression::put_table(const wchar_t * table_name) { if( out_stream ) { - before_short_field_name(); + before_table_name(); esc(table_name, *out_stream); - after_short_field_name(); + after_table_name(); } } @@ -739,9 +775,9 @@ void BaseExpression::put_table(const PT::WTextStream & table_name) { if( out_stream ) { - before_short_field_name(); + before_table_name(); esc(table_name, *out_stream); - after_short_field_name(); + after_table_name(); } } @@ -750,7 +786,7 @@ void BaseExpression::put_table_with_index(const wchar_t * table_name, int index) { if( out_stream ) { - before_short_field_name(); + before_table_name(); esc(table_name, *out_stream); if( index > 1 ) @@ -758,7 +794,7 @@ void BaseExpression::put_table_with_index(const wchar_t * table_name, int index) (*out_stream) << index; } - after_short_field_name(); + after_table_name(); } } @@ -767,7 +803,7 @@ void BaseExpression::put_table_with_index(const PT::WTextStream & table_name, in { if( out_stream ) { - before_short_field_name(); + before_table_name(); esc(table_name, *out_stream); if( index > 1 ) @@ -775,7 +811,7 @@ void BaseExpression::put_table_with_index(const PT::WTextStream & table_name, in (*out_stream) << index; } - after_short_field_name(); + after_table_name(); } } @@ -785,10 +821,7 @@ void BaseExpression::put_table_with_index_and_field(const wchar_t * table_name, if( out_stream ) { put_table_with_index(table_name, index); - (*out_stream) << '.'; - - // IMPROVE ME - // put_field_name seems to be too complicated, it is needed to check there whether field_name is long or short? + table_field_separator(); put_field_name(field_name, field_type, nullptr); } } @@ -798,7 +831,7 @@ void BaseExpression::put_table_with_index_and_field(const PT::WTextStream & tabl if( out_stream ) { put_table_with_index(table_name, index); - (*out_stream) << '.'; + table_field_separator(); put_field_name(field_name, field_type, nullptr); } } @@ -809,7 +842,7 @@ void BaseExpression::put_table_and_field(const wchar_t * table_name, const wchar if( out_stream ) { put_table(table_name); - (*out_stream) << '.'; + table_field_separator(); put_field_name(field_name, field_type, nullptr); } } @@ -819,7 +852,7 @@ void BaseExpression::put_table_and_field(const PT::WTextStream & table_name, con if( out_stream ) { put_table(table_name); - (*out_stream) << '.'; + table_field_separator(); put_field_name(field_name, field_type, nullptr); } } @@ -907,5 +940,11 @@ void BaseExpression::table_and_field_to_stream(PT::TextStream & stream, const PT +bool BaseExpression::is_empty_field(const wchar_t * value) +{ + return (!value || *value == '\0'); +} + + } diff --git a/src/baseexpression.h b/src/baseexpression.h index 1d42d64..d29f2d8 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -614,22 +614,18 @@ protected: } - /* - * escaping column names in a case when using short form - just only column_name - * [before_short_field_name]column_name[after_short_field_name] - */ - virtual void before_short_field_name(); - virtual void after_short_field_name(); + virtual void schema_table_separator(); + virtual void table_field_separator(); + + virtual void before_schema_name(); + virtual void after_schema_name(); + + virtual void before_table_name(); + virtual void after_table_name(); + + virtual void before_field_name(); + virtual void after_field_name(); - /* - * escaping column names in a case when using long form: table_name.column_name - * [before_first_part_long_field_name]table_name[after_first_part_long_field_name].[before_second_part_long_field_name]column_name[after_second_part_long_field_name] - * - */ - virtual void before_first_part_long_field_name(); - virtual void after_first_part_long_field_name(); - virtual void before_second_part_long_field_name(); - virtual void after_second_part_long_field_name(); virtual void before_field_value(const std::wstring &, const FT & field_type); virtual void after_field_value(const std::wstring &, const FT & field_type); @@ -721,6 +717,7 @@ protected: void esc(const wchar_t * val, bool has_known_length, size_t len, PT::TextStream & stream, const FT & field_type = FT::default_type); + bool is_empty_field(const wchar_t * value); }; } diff --git a/src/dbconnector.cpp b/src/dbconnector.cpp index 6ec2cab..8c74396 100644 --- a/src/dbconnector.cpp +++ b/src/dbconnector.cpp @@ -183,8 +183,8 @@ void DbConnector::generate_insert_query(PT::TextStream & stream, Model & model) db_expression->clear(); db_expression->allow_to_use_prefix(false); - // IMPROVEME escape table_name - stream << "insert into " << model.get_table_name(); + stream << "insert into "; + db_expression->schema_table_to_stream(stream, model.model_env->schema_name, model.model_env->table_name); stream << " ("; db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS); @@ -208,8 +208,8 @@ void DbConnector::generate_update_query(PT::TextStream & stream, Model & model) db_expression->clear(); db_expression->allow_to_use_prefix(false); - // IMPROVEME escape table_name - stream << "update " << model.get_table_name(); + stream << "update "; + db_expression->schema_table_to_stream(stream, model.model_env->schema_name, model.model_env->table_name); stream << " set "; db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS_VALUES); @@ -233,8 +233,8 @@ void DbConnector::generate_remove_query(PT::TextStream & stream, Model & model) db_expression->clear(); db_expression->allow_to_use_prefix(false); - // IMPROVEME escape table_name - stream << "delete from " << model.get_table_name(); + stream << "delete from "; + db_expression->schema_table_to_stream(stream, model.model_env->schema_name, model.model_env->table_name); stream << " where "; db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS_VALUES); diff --git a/src/dbexpression.cpp b/src/dbexpression.cpp index 913d321..c7b08db 100644 --- a/src/dbexpression.cpp +++ b/src/dbexpression.cpp @@ -182,6 +182,54 @@ void DbExpression::put_name_value_separator() } +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) << '"'; +} + + void DbExpression::before_field_value_string(const FT & field_type) { diff --git a/src/dbexpression.h b/src/dbexpression.h index c09d579..81b1636 100644 --- a/src/dbexpression.h +++ b/src/dbexpression.h @@ -85,6 +85,18 @@ protected: void field_before(); void put_name_value_separator(); + void schema_table_separator(); + void table_field_separator(); + + void before_schema_name(); + void after_schema_name(); + + void before_table_name(); + void after_table_name(); + + void before_field_name(); + void after_field_name(); + private: diff --git a/src/jsonexpression.cpp b/src/jsonexpression.cpp index bb15a4f..6648df0 100644 --- a/src/jsonexpression.cpp +++ b/src/jsonexpression.cpp @@ -76,33 +76,16 @@ void JSONExpression::field_before() -void JSONExpression::before_short_field_name() +void JSONExpression::before_field_name() { (*out_stream) << "\""; } -void JSONExpression::after_short_field_name() +void JSONExpression::after_field_name() { (*out_stream) << "\""; } -void JSONExpression::before_first_part_long_field_name() -{ - (*out_stream) << "\""; -} - -void JSONExpression::after_first_part_long_field_name() -{ -} - -void JSONExpression::before_second_part_long_field_name() -{ -} - -void JSONExpression::after_second_part_long_field_name() -{ - (*out_stream) << "\""; -} void JSONExpression::before_field_value_string(const FT & field_type) diff --git a/src/jsonexpression.h b/src/jsonexpression.h index ebe7352..3ef6ab8 100644 --- a/src/jsonexpression.h +++ b/src/jsonexpression.h @@ -54,12 +54,8 @@ protected: void field_before(); - void before_short_field_name(); - void after_short_field_name(); - void before_first_part_long_field_name(); - void after_first_part_long_field_name(); - void before_second_part_long_field_name(); - void after_second_part_long_field_name(); + void before_field_name(); + void after_field_name(); void put_name_value_separator(); diff --git a/src/model.cpp b/src/model.cpp index efe3f58..6bca2d6 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -907,56 +907,54 @@ bool Model::is_the_same_field(const wchar_t * field1, const wchar_t * field2) -PT::WTextStream Model::get_table_name(bool put_schema_name) +void Model::log_table_name(bool put_schema_name) { - PT::WTextStream str; - - if( model_env ) + if( model_connector && model_env ) { - if( put_schema_name && !model_env->schema_name.empty() ) + PT::Log * plog = model_connector->get_logger(); + + if( plog ) { - str << model_env->schema_name; + if( put_schema_name && !model_env->schema_name.empty() ) + { + (*plog) << model_env->schema_name; - // IMPROVEME make a virtual method in dbexpression to put such a dot - str << '.'; + // although in BaseExpression there is schema_table_separator() method + // but for logging purposes we can use just a dot here + (*plog) << '.'; + } + + (*plog) << model_env->table_name; } - - str << model_env->table_name; } - - return str; } -PT::WTextStream Model::get_table_name_with_field(const wchar_t * db_field_name, bool put_schema_name) +void Model::log_table_name_with_field(const wchar_t * db_field_name, bool put_schema_name) { - PT::WTextStream str; - bool is_empty_field_name = is_empty_field(db_field_name); - - if( model_env ) + if( model_connector && model_env ) { - if( put_schema_name && !model_env->schema_name.empty() ) + PT::Log * plog = model_connector->get_logger(); + + if( plog ) { - str << model_env->schema_name; + bool is_empty_field_name = is_empty_field(db_field_name); - // IMPROVEME make a virtual method in dbexpression to put such a dot - str << '.'; - } + if( put_schema_name && !model_env->schema_name.empty() ) + { + (*plog) << model_env->schema_name; + (*plog) << '.'; + } - str << model_env->table_name; + (*plog) << model_env->table_name; - if( !is_empty_field_name ) - { - str << '.'; // IMPROVEME get a virtual method from dbexpression + if( !is_empty_field_name ) + { + (*plog) << '.'; + (*plog) << db_field_name; + } } } - - if( !is_empty_field_name ) - { - str << db_field_name; - } - - return str; } @@ -1099,7 +1097,9 @@ void Model::field_model_save_key(const wchar_t * db_field_name) if( model_env->field_value_helper_tab->empty() && plog ) { - (*plog) << PT::Log::log1 << "Morm: I cannot find a primary key in " << get_table_name() << PT::Log::logend; + (*plog) << PT::Log::log1 << "Morm: I cannot find a primary key in "; + log_table_name(); + (*plog) << PT::Log::logend; } } } @@ -1139,14 +1139,19 @@ void Model::field_model_set_parent_key_in_child(const wchar_t * db_field_name, M { if( field_model.model_env->field_index == 0 ) { - (*log) << PT::Log::log1 << "Morm: there is no a foreign key in " << field_model.get_table_name() - << " called " << db_field_name << " pointing to " << get_table_name() << PT::Log::logend; + (*log) << PT::Log::log1 << "Morm: there is no a foreign key in "; + field_model.log_table_name(); + (*log) << " called " << db_field_name << " pointing to "; + log_table_name(); + (*log) << PT::Log::logend; } else { - (*log) << PT::Log::log1 << "Morm: primary key in " << get_table_name() << " consists of " << model_env->field_index << " column(s)" - << " but foreign key in " << field_model.get_table_name() << " consists of " - << field_model.model_env->field_index << " column(s)" << PT::Log::logend; + (*log) << PT::Log::log1 << "Morm: primary key in "; + log_table_name(); + (*log) << " consists of " << model_env->field_index << " column(s) but foreign key in "; + field_model.log_table_name(); + (*log) << " consists of " << field_model.model_env->field_index << " column(s)" << PT::Log::logend; } } @@ -1155,8 +1160,10 @@ void Model::field_model_set_parent_key_in_child(const wchar_t * db_field_name, M else if( log ) { - (*log) << PT::Log::log1 << "Morm: primary key in " << get_table_name() << " consists of incorrect number of columns" - << ", expected " << helper_tab.size() << " column(s) but got " << model_env->field_index << PT::Log::logend; + (*log) << PT::Log::log1 << "Morm: primary key in "; + log_table_name(); + (*log) << " consists of incorrect number of columns, expected " << helper_tab.size() + << " column(s) but got " << model_env->field_index << PT::Log::logend; } } } @@ -1368,8 +1375,9 @@ void Model::field_model(const wchar_t * db_field_name, const wchar_t * flat_fiel if( plog ) { - (*plog) << PT::Log::log1 << "Morm: error in " << get_table_name_with_field(db_field_name) - << " field, you should set FT::is_foreign_key or FT::is_foreign_key_in_child flag for a model child object" << PT::Log::logend; + (*plog) << PT::Log::log1 << "Morm: error in "; + log_table_name_with_field(db_field_name); + (*plog) << " field, you should set FT::is_foreign_key or FT::is_foreign_key_in_child flag for a model child object" << PT::Log::logend; } } } diff --git a/src/model.h b/src/model.h index bd7472e..9a5dadd 100644 --- a/src/model.h +++ b/src/model.h @@ -484,7 +484,9 @@ protected: { if( log ) { - (*log) << PT::Log::log1 << "Morm: incorrect type of a field in " << get_table_name() << ", "; + (*log) << PT::Log::log1 << "Morm: incorrect type of a field in "; + log_table_name(); + (*log) << ", "; put_fields_to_log(*log, db_field_name, flat_field_name); (*log) << ", type expected " << typeid(field_value).name() << " got " << helper.value_type_info->name() << PT::Log::logend; @@ -778,7 +780,9 @@ protected: { if( plog ) { - (*plog) << PT::Log::log1 << "Morm: ignoring " << get_table_name_with_field(db_field_name) << " as this is not a container with Model objects" << PT::Log::logend; + (*plog) << PT::Log::log1 << "Morm: ignoring "; + log_table_name_with_field(db_field_name); + (*plog) << " as this is not a container with Model objects" << PT::Log::logend; } } } @@ -786,8 +790,9 @@ protected: { if( plog ) { - (*plog) << PT::Log::log1 << "Morm: error in " << get_table_name_with_field(db_field_name) - << " field, you should set FT::is_foreign_key or FT::is_foreign_key_in_child flag for a list of child objects" << PT::Log::logend; + (*plog) << PT::Log::log1 << "Morm: error in "; + log_table_name_with_field(db_field_name); + (*plog) << " field, you should set FT::is_foreign_key or FT::is_foreign_key_in_child flag for a list of child objects" << PT::Log::logend; } } } @@ -969,8 +974,8 @@ protected: virtual void put_to_log(const wchar_t * str); virtual void put_fields_to_log(PT::Log & log, const wchar_t * db_field_name, const wchar_t * flat_field_name); - virtual PT::WTextStream get_table_name(bool put_schema_name = true); - virtual PT::WTextStream get_table_name_with_field(const wchar_t * db_field_name = nullptr, bool put_schema_name = true); + virtual void log_table_name(bool put_schema_name = true); + virtual void log_table_name_with_field(const wchar_t * db_field_name = nullptr, bool put_schema_name = true); template friend class Finder; template friend class Cursor; diff --git a/src/postgresqlexpression.cpp b/src/postgresqlexpression.cpp index 90b9cf8..4aaa3f7 100644 --- a/src/postgresqlexpression.cpp +++ b/src/postgresqlexpression.cpp @@ -39,43 +39,6 @@ namespace morm { -void PostgreSQLExpression::before_short_field_name() -{ - (*out_stream) << '"'; -} - -void PostgreSQLExpression::after_short_field_name() -{ - (*out_stream) << '"'; -} - - - -void PostgreSQLExpression::before_first_part_long_field_name() -{ - (*out_stream) << '"'; -} - - -void PostgreSQLExpression::after_first_part_long_field_name() -{ - (*out_stream) << '"'; -} - - -void PostgreSQLExpression::before_second_part_long_field_name() -{ - (*out_stream) << '"'; -} - - -void PostgreSQLExpression::after_second_part_long_field_name() -{ - (*out_stream) << '"'; -} - - - void PostgreSQLExpression::before_field_value_string(const FT & field_type) { diff --git a/src/postgresqlexpression.h b/src/postgresqlexpression.h index fc76a1a..0001171 100644 --- a/src/postgresqlexpression.h +++ b/src/postgresqlexpression.h @@ -53,13 +53,7 @@ public: protected: - virtual void before_short_field_name(); - virtual void after_short_field_name(); - virtual void before_first_part_long_field_name(); - virtual void after_first_part_long_field_name(); - virtual void before_second_part_long_field_name(); - virtual void after_second_part_long_field_name(); private: From 6eaa9088e57df9b991cb9138be600c0ab8ea2858 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 13 May 2021 03:27:21 +0200 Subject: [PATCH 19/27] renamed in Model: map_fields() to fields(), prepare_table() to table() --- src/baseexpression.cpp | 2 +- src/finder.h | 2 +- src/model.cpp | 68 +++++++++++++++++++++--------------------- src/model.h | 29 ++++++++---------- 4 files changed, 48 insertions(+), 53 deletions(-) diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index 2e2ff62..ff0bc39 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -121,7 +121,7 @@ void BaseExpression::generate_from_model(Model & model) { before_generate_from_model(); dump_additional_info(model); - model.map_fields(); + model.fields(); after_generate_from_model(); } } diff --git a/src/finder.h b/src/finder.h index 2ccfc9e..7f69477 100644 --- a/src/finder.h +++ b/src/finder.h @@ -181,7 +181,7 @@ public: model.model_env = &model_env; model.model_env->model_data = model_data; model.model_env->finder_helper = &finder_helper; - model.prepare_table(); + model.table(); model.model_env->add_table_name_to_finder_helper(); return *this; diff --git a/src/model.cpp b/src/model.cpp index 6bca2d6..01fc531 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -127,7 +127,7 @@ ModelConnector * Model::get_connector() } -void Model::prepare_table() +void Model::table() { if( model_connector ) { @@ -135,12 +135,12 @@ void Model::prepare_table() if( plog ) { - (*plog) << PT::Log::log1 << "Morm: you should provide the table name e.g. provide prepare_table() method and call table(...) there" << PT::Log::logend; + (*plog) << PT::Log::log1 << "Morm: you should provide the table name e.g. provide table() method and call table(...) there" << PT::Log::logend; } } } -void Model::table(const wchar_t * table_name) +void Model::table_name(const wchar_t * table_name) { if( model_env ) { @@ -151,7 +151,7 @@ void Model::table(const wchar_t * table_name) } } -void Model::table(const wchar_t * schema_name, const wchar_t * table_name) +void Model::table_name(const wchar_t * schema_name, const wchar_t * table_name) { if( model_env ) { @@ -200,7 +200,7 @@ void Model::to_text(PT::TextStream & stream, ModelData * model_data, bool clear_ { try { - // prepare_table(); at the moment flat strings (json/space) do not need a table name + // table(); at the moment flat strings (json/space) do not need a table name flat_connector->to_text(stream, *this); } catch(...) @@ -288,7 +288,7 @@ void Model::generate_insert_query(PT::TextStream & stream, ModelData * model_dat { try { - prepare_table(); + table(); db_connector->generate_insert_query(stream, *this); } catch(...) @@ -325,7 +325,7 @@ bool Model::insert(ModelData * model_data, bool insert_whole_tree) try { - prepare_table(); + table(); status = insert_tree(insert_whole_tree); } catch(...) @@ -351,7 +351,7 @@ bool Model::insert_tree(bool insert_whole_tree) { model_env->model_work_mode = MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITH_FOREIGN_KEY; model_env->model_work_submode = MORM_MODEL_WORK_SUBMODE_INSERT; - map_fields(); + fields(); } if( model_connector ) @@ -398,7 +398,7 @@ bool Model::insert_tree(bool insert_whole_tree) { model_env->model_work_mode = MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITHOUT_FOREIGN_KEY; model_env->model_work_submode = MORM_MODEL_WORK_SUBMODE_INSERT; - map_fields(); + fields(); } return result; @@ -420,7 +420,7 @@ void Model::generate_update_query(PT::TextStream & stream, ModelData * model_dat if( db_connector ) { - prepare_table(); + table(); db_connector->generate_update_query(stream, *this); } } @@ -452,7 +452,7 @@ bool Model::update(ModelData * model_data, bool update_whole_tree) try { - prepare_table(); + table(); status = update_tree(update_whole_tree); } catch(...) @@ -482,7 +482,7 @@ bool Model::update_tree(bool update_whole_tree) { model_env->model_work_mode = MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITH_FOREIGN_KEY; model_env->model_work_submode = MORM_MODEL_WORK_SUBMODE_UPDATE; - map_fields(); + fields(); } if( model_connector ) @@ -510,7 +510,7 @@ bool Model::update_tree(bool update_whole_tree) { model_env->model_work_mode = MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITHOUT_FOREIGN_KEY; model_env->model_work_submode = MORM_MODEL_WORK_SUBMODE_UPDATE; - map_fields(); + fields(); } return result; @@ -531,7 +531,7 @@ void Model::generate_remove_query(PT::TextStream & stream, ModelData * model_dat if( db_connector ) { - prepare_table(); + table(); db_connector->generate_remove_query(stream, *this); } } @@ -564,7 +564,7 @@ bool Model::remove(ModelData * model_data, bool remove_whole_tree) try { - prepare_table(); + table(); status = remove_tree(remove_whole_tree); } catch(...) @@ -594,7 +594,7 @@ bool Model::remove_tree(bool remove_whole_tree) { model_env->model_work_mode = MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITHOUT_FOREIGN_KEY; model_env->model_work_submode = MORM_MODEL_WORK_SUBMODE_REMOVE; - map_fields(); + fields(); } if( model_connector ) @@ -629,7 +629,7 @@ bool Model::remove_tree(bool remove_whole_tree) { model_env->model_work_mode = MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITH_FOREIGN_KEY; model_env->model_work_submode = MORM_MODEL_WORK_SUBMODE_REMOVE; - map_fields(); + fields(); } return result; @@ -660,7 +660,7 @@ bool Model::save(ModelData * model_data, bool save_whole_tree) try { - prepare_table(); + table(); status = save_tree(save_whole_tree); } catch(...) @@ -688,7 +688,7 @@ bool Model::save_tree(bool save_whole_tree) model_env->model_work_mode = MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITH_FOREIGN_KEY; model_env->model_work_submode = MORM_MODEL_WORK_SUBMODE_SAVE; - map_fields(); + fields(); } ModelEnv * old_model_env = model_env; // remove, insert or update will set model_env to nullptr @@ -722,7 +722,7 @@ bool Model::save_tree(bool save_whole_tree) model_env->model_work_mode = MORM_MODEL_WORK_MODE_ITERATE_THROUGH_CHILDS_WITHOUT_FOREIGN_KEY; model_env->model_work_submode = MORM_MODEL_WORK_SUBMODE_SAVE; - map_fields(); + fields(); } return result; @@ -754,7 +754,7 @@ void Model::map_values_from_query() model_env->model_work_mode = MORM_MODEL_WORK_MODE_READING_VALUE_FROM_DB_RESULTSET; model_env->was_primary_key_read = false; // whether or not there was at least one column with primary_key flag model_env->has_primary_key_set = true; // whether all primary_columns were different than null - map_fields(); + fields(); model_env->model_work_mode = MORM_MODEL_WORK_MODE_NONE; if( model_env->was_primary_key_read && model_env->has_primary_key_set ) @@ -781,8 +781,8 @@ void Model::clear() try { - // prepare_table() doesn't have to be called - map_fields(); + // table() doesn't have to be called + fields(); } catch(...) { @@ -1030,7 +1030,7 @@ void Model::field_model_left_join(const wchar_t * db_field_name, Model & field_m if( field_type.is_foreign_key() ) { - field_model.map_fields(); + field_model.fields(); join_tables_str << " ON "; db_expression->table_with_index_and_field_to_stream(join_tables_str, model_env->table_name, model_env->table_index, db_field_name, field_type); @@ -1048,7 +1048,7 @@ void Model::field_model_left_join(const wchar_t * db_field_name, Model & field_m else { ModelEnv * old_model_env = field_model.model_env; - map_fields(); // map_fields() will set field_model.model_env to null + fields(); // fields() will set field_model.model_env to null field_model.model_env = old_model_env; join_tables_str << " ON "; @@ -1092,7 +1092,7 @@ void Model::field_model_save_key(const wchar_t * db_field_name) int old_work_mode = model_env->model_work_mode; model_env->model_work_mode = MORM_MODEL_WORK_MODE_ITERATE_PRIMARY_KEY_VALUES; model_env->field_index = 0; - map_fields(); + fields(); model_env->model_work_mode = old_work_mode; if( model_env->field_value_helper_tab->empty() && plog ) @@ -1131,9 +1131,9 @@ void Model::field_model_set_parent_key_in_child(const wchar_t * db_field_name, M model_env_local.field_value_helper_tab = &helper_tab; model_env_local.field_index = 0; field_model.model_env = &model_env_local; - field_model.prepare_table(); + field_model.table(); - field_model.map_fields(); + field_model.fields(); if( (size_t)field_model.model_env->field_index != helper_tab.size() && log ) { @@ -1268,7 +1268,7 @@ void Model::field_model_generate_db_sql(const wchar_t * db_field_name, Model & f if( field_type.is_insertable() ) { db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_INSERT_PRIMARY_KEY); - field_model.map_fields(); + field_model.fields(); db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_INSERT); } } @@ -1283,7 +1283,7 @@ void Model::field_model_generate_db_sql(const wchar_t * db_field_name, Model & f db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE_PRIMARY_KEY); field_model.model_env->field_index = 0; field_model.model_env->set_field_name_helper = &key_fields; - field_model.map_fields(); + field_model.fields(); db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_UPDATE); if( (size_t)field_model.model_env->field_index != key_fields.size() ) @@ -1301,7 +1301,7 @@ void Model::field_model_generate_db_sql(const wchar_t * db_field_name, Model & f db_expression->get_output_type() != MORM_OUTPUT_TYPE_DB_INSERT && db_expression->get_output_type() != MORM_OUTPUT_TYPE_DB_UPDATE ) { - field_model.map_fields(); + field_model.fields(); } field_model.model_env->model_work_mode = MORM_MODEL_WORK_MODE_NONE; @@ -1363,7 +1363,7 @@ void Model::field_model(const wchar_t * db_field_name, const wchar_t * flat_fiel if( !is_empty_field(db_field_name) ) { - field_model.prepare_table(); + field_model.table(); if( field_type.is_foreign_key() || field_type.is_foreign_key_in_child() ) { @@ -1386,7 +1386,7 @@ void Model::field_model(const wchar_t * db_field_name, const wchar_t * flat_fiel { if( model_env->model_work_mode == MORM_MODEL_WORK_MODE_GENERATING_FLAT_STRING ) { - // calling field_model.prepare_table(); is not needed in generating strings (at least for json/space formats) + // calling field_model.table() is not needed in generating strings (at least for json/space formats) field_model_generate_flat_string(flat_field_name, field_model, field_type); } } @@ -1445,7 +1445,7 @@ void Model::set_parent_key_in_childs() if( model_env ) { model_env->model_work_mode = MORM_MODEL_WORK_MODE_SET_PARENT_ID; - map_fields(); + fields(); } } diff --git a/src/model.h b/src/model.h index 9a5dadd..7b96376 100644 --- a/src/model.h +++ b/src/model.h @@ -86,20 +86,6 @@ public: void set_connector(ModelConnector * connector); ModelConnector * get_connector(); - /* - * map fields to names - * - * IMPROVEME rename me to fields() and make protected - */ - virtual void map_fields() = 0; - - /* - * IMPROVEME make me protected - */ - virtual void prepare_table(); - virtual void table(const wchar_t * table_name); - virtual void table(const wchar_t * schema_name, const wchar_t * table_name); - 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); @@ -145,7 +131,7 @@ public: model_env = &model_env_local; model_env->model_work_mode = MORM_MODEL_WORK_MODE_SET_FIELD_VALUE; model_env->field_index = 0; - prepare_table(); // CHECK ME it is needed to set table name? + table(); // CHECK ME it is needed to set table name? FieldValueHelper field_value_helper; field_value_helper.db_field_name = db_field_name; @@ -157,7 +143,7 @@ public: helper_tab.push_back(field_value_helper); model_env->field_value_helper_tab = &helper_tab; - map_fields(); + fields(); if( !helper_tab.back().found && model_connector ) { @@ -189,6 +175,15 @@ protected: Model(const Model & m); virtual ~Model(); + /* + * the main method of mapping between fields and database resultsets + */ + virtual void fields() = 0; + + virtual void table(); + virtual void table_name(const wchar_t * table_name); + virtual void table_name(const wchar_t * schema_name, const wchar_t * table_name); + virtual void before_select(); virtual void before_insert(); virtual void before_update(); @@ -683,7 +678,7 @@ protected: child_model.model_env = &model_env_local; child_model.model_env->has_primary_key_set = child_model.has_primary_key_set; child_model.set_connector(model_connector); - child_model.prepare_table(); + child_model.table(); if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_INSERT ) { From de4abeb91ce727bd489bdcd6baecff4b147aaa22 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 13 May 2021 19:32:31 +0200 Subject: [PATCH 20/27] added to Finder: methods eq() and similar with table_name as an argument Finder & eq(const wchar_t * table_name, const wchar_t * field_name, const FieldValue & field_value) Finder & eq(const wchar_t * table_name, int table_index, const wchar_t * field_name, const FieldValue & field_value) and similar for neq(), lt(), gt(), le(), ge() and in() --- src/baseexpression.cpp | 9 +- src/finder.h | 273 ++++++++++++++++++++++++++++++++++++++++- src/modelenv.h | 14 +++ 3 files changed, 294 insertions(+), 2 deletions(-) diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index ff0bc39..c14a81b 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -174,7 +174,14 @@ void BaseExpression::put_field_name_and_table_if_needed(const wchar_t * field_na { if( use_prefix && model_env ) { - put_table_with_index_and_field(model_env->table_name, model_env->table_index, field_name, field_type); + if( !is_empty_field(model_env->table2_name) ) + { + put_table_with_index_and_field(model_env->table2_name, model_env->table2_index, field_name, field_type); + } + else + { + put_table_with_index_and_field(model_env->table_name, model_env->table_index, field_name, field_type); + } } else { diff --git a/src/finder.h b/src/finder.h index 7f69477..c369da9 100644 --- a/src/finder.h +++ b/src/finder.h @@ -341,7 +341,6 @@ public: - template Finder & eq(const wchar_t * field_name, const FieldValue & field_value) { @@ -355,6 +354,32 @@ public: } + template + Finder & eq(const wchar_t * table_name, const wchar_t * field_name, const FieldValue & field_value) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_EQ); + field_to_stream(table_name, 1, field_name, field_value); + } + + return *this; + } + + + template + Finder & eq(const wchar_t * table_name, int table_index, const wchar_t * field_name, const FieldValue & field_value) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_EQ); + field_to_stream(table_name, table_index, field_name, field_value); + } + + return *this; + } + + template Finder & neq(const wchar_t * field_name, const FieldValue & field_value) { @@ -367,6 +392,33 @@ public: return *this; } + + template + Finder & neq(const wchar_t * table_name, const wchar_t * field_name, const FieldValue & field_value) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_NOT_EQ); + field_to_stream(table_name, 1, field_name, field_value); + } + + return *this; + } + + + template + Finder & neq(const wchar_t * table_name, int table_index, const wchar_t * field_name, const FieldValue & field_value) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_NOT_EQ); + field_to_stream(table_name, table_index, field_name, field_value); + } + + return *this; + } + + template Finder & lt(const wchar_t * field_name, const FieldValue & field_value) { @@ -380,6 +432,32 @@ public: } + template + Finder & lt(const wchar_t * table_name, const wchar_t * field_name, const FieldValue & field_value) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_LT); + field_to_stream(table_name, 1, field_name, field_value); + } + + return *this; + } + + + template + Finder & lt(const wchar_t * table_name, int table_index, const wchar_t * field_name, const FieldValue & field_value) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_LT); + field_to_stream(table_name, table_index, field_name, field_value); + } + + return *this; + } + + template Finder & gt(const wchar_t * field_name, const FieldValue & field_value) { @@ -393,6 +471,32 @@ public: } + template + Finder & gt(const wchar_t * table_name, const wchar_t * field_name, const FieldValue & field_value) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_GT); + field_to_stream(table_name, 1, field_name, field_value); + } + + return *this; + } + + + template + Finder & gt(const wchar_t * table_name, int table_index, const wchar_t * field_name, const FieldValue & field_value) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_GT); + field_to_stream(table_name, table_index, field_name, field_value); + } + + return *this; + } + + template Finder & le(const wchar_t * field_name, const FieldValue & field_value) { @@ -406,6 +510,34 @@ public: } + template + Finder & le(const wchar_t * table_name, const wchar_t * field_name, const FieldValue & field_value) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_LE); + field_to_stream(table_name, 1, field_name, field_value); + } + + return *this; + } + + + + template + Finder & le(const wchar_t * table_name, int table_index, const wchar_t * field_name, const FieldValue & field_value) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_LE); + field_to_stream(table_name, table_index, field_name, field_value); + } + + return *this; + } + + + template Finder & ge(const wchar_t * field_name, const FieldValue & field_value) { @@ -419,6 +551,33 @@ public: } + template + Finder & ge(const wchar_t * table_name, const wchar_t * field_name, const FieldValue & field_value) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_GE); + field_to_stream(table_name, 1, field_name, field_value); + } + + return *this; + } + + + + template + Finder & ge(const wchar_t * table_name, int table_index, const wchar_t * field_name, const FieldValue & field_value) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_GE); + field_to_stream(table_name, table_index, field_name, field_value); + } + + return *this; + } + + template Finder & in(const wchar_t * field_name, const std::set & container) @@ -432,6 +591,33 @@ public: return *this; } + + template + Finder & in(const wchar_t * table_name, const wchar_t * field_name, const std::set & container) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_IN); + field_in(table_name, 1, field_name, container); + } + + return *this; + } + + + template + Finder & in(const wchar_t * table_name, int table_index, const wchar_t * field_name, const std::set & container) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_IN); + field_in(table_name, table_index, field_name, container); + } + + return *this; + } + + template Finder & in(const wchar_t * field_name, const std::list & container) { @@ -444,6 +630,33 @@ public: return *this; } + + template + Finder & in(const wchar_t * table_name, const wchar_t * field_name, const std::list & container) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_IN); + field_in(table_name, 1, field_name, container); + } + + return *this; + } + + + template + Finder & in(const wchar_t * table_name, int table_index, const wchar_t * field_name, const std::list & container) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_IN); + field_in(table_name, table_index, field_name, container); + } + + return *this; + } + + template Finder & in(const wchar_t * field_name, const std::vector & container) { @@ -456,6 +669,35 @@ public: return *this; } + + template + Finder & in(const wchar_t * table_name, const wchar_t * field_name, const std::vector & container) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_IN); + field_in(table_name, 1, field_name, container); + } + + return *this; + } + + + template + Finder & in(const wchar_t * table_name, int table_index, const wchar_t * field_name, const std::vector & container) + { + if( db_expression ) + { + db_expression->set_output_type(MORM_OUTPUT_TYPE_WHERE_IN); + field_in(table_name, table_index, field_name, container); + } + + return *this; + } + + + + Finder & page(size_t page_number, size_t page_size) { if( out_stream && db_expression ) @@ -640,6 +882,35 @@ private: } + template + void field_to_stream(const wchar_t * table_name, int table_index, const wchar_t * field_name, const FieldValue & field_value) + { + if( db_expression ) + { + model_env.table2_name = table_name; + model_env.table2_index = table_index; + + db_expression->field_to_stream(*out_stream, field_name, field_value, FT::default_type, &model_env); + + model_env.table2_name = nullptr; + model_env.table2_index = 0; + } + } + + template + void field_in(const wchar_t * table_name, int table_index, const wchar_t * field_name, const Container & container) + { + if( db_expression ) + { + model_env.table2_name = table_name; + model_env.table2_index = table_index; + + db_expression->field_in(*out_stream, field_name, container, &model_env); + + model_env.table2_name = nullptr; + model_env.table2_index = 0; + } + } }; diff --git a/src/modelenv.h b/src/modelenv.h index dae9625..c68adbc 100644 --- a/src/modelenv.h +++ b/src/modelenv.h @@ -64,10 +64,20 @@ public: PT::WTextStream schema_name; PT::WTextStream table_name; int table_index; + + /* + * optional additional table name + * used in eq(), neq(), ... methods in Finder + */ + const wchar_t * table2_name; + int table2_index; + + int field_index; bool was_primary_key_read; bool has_primary_key_set; + std::vector * set_field_name_helper; std::vector * field_value_helper_tab; @@ -87,6 +97,7 @@ public: copy_global_objects(e); table_index = e.table_index; + table2_index = e.table2_index; set_field_name_helper = e.set_field_name_helper; field_value_helper_tab = e.field_value_helper_tab; field_index = e.field_index; @@ -94,6 +105,7 @@ public: has_primary_key_set = e.has_primary_key_set; // schema_name and table_name don't have to be copied + table2_name = nullptr; } @@ -119,7 +131,9 @@ public: dump_mode = false; schema_name.clear(); table_name.clear(); + table2_name = nullptr; table_index = 0; + table2_index = 0; set_field_name_helper = nullptr; field_value_helper_tab = nullptr; field_index = 0; From b12037a7e54ded1c8c7441241dd3002688e62420 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Fri, 14 May 2021 03:24:53 +0200 Subject: [PATCH 21/27] added basic support for making migrations --- src/dbconnector.cpp | 22 +++++++++ src/dbconnector.h | 6 +-- src/model.cpp | 113 ++++++++++++++++++++++++++++++++++++++++++++ src/model.h | 32 +++++++++++++ 4 files changed, 170 insertions(+), 3 deletions(-) diff --git a/src/dbconnector.cpp b/src/dbconnector.cpp index 8c74396..12da18b 100644 --- a/src/dbconnector.cpp +++ b/src/dbconnector.cpp @@ -85,6 +85,28 @@ void DbConnector::set_log_queries(bool log_queries) } +bool DbConnector::query(const PT::TextStream & stream) +{ + std::unique_ptr query_result_ptr(create_query_result()); + return query(stream, *query_result_ptr); +} + + +bool DbConnector::query(const std::string & query_str) +{ + std::unique_ptr query_result_ptr(create_query_result()); + return query(query_str, *query_result_ptr); +} + + +bool DbConnector::query(const char * query_str) +{ + std::unique_ptr query_result_ptr(create_query_result()); + return query(query_str, *query_result_ptr); +} + + + bool DbConnector::query(const PT::TextStream & stream, QueryResult & query_result) { std::string query_str; diff --git a/src/dbconnector.h b/src/dbconnector.h index 2a93cd3..a87a896 100644 --- a/src/dbconnector.h +++ b/src/dbconnector.h @@ -62,8 +62,6 @@ public: DbExpression * get_expression(); - //virtual void clear_last_query_result(); - virtual void generate_select_columns(PT::TextStream & stream, Model & model); virtual void generate_insert_query(PT::TextStream & stream, Model & model); virtual void generate_update_query(PT::TextStream & stream, Model & model); @@ -73,7 +71,9 @@ public: virtual bool update(PT::TextStream & stream, Model & model); virtual bool remove(PT::TextStream & stream, Model & model); - //void ModelConnector::get_values_from_db(Model & model) + virtual bool query(const PT::TextStream & stream); + virtual bool query(const std::string & query_str); + virtual bool query(const char * query_str); virtual QueryResult * create_query_result() = 0; diff --git a/src/model.cpp b/src/model.cpp index 01fc531..d424819 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -33,6 +33,7 @@ */ #include "model.h" +#include "utf8/utf8.h" namespace morm @@ -177,6 +178,68 @@ bool Model::found() } +void Model::get_table_name(PT::WTextStream & stream, bool with_schema_name, ModelData * model_data, bool clear_stream) +{ + if( clear_stream ) + { + stream.clear(); + } + + ModelEnv model_env_local; + model_env = &model_env_local; + model_env->model_data = model_data; + + if( model_connector ) + { + DbConnector * db_connector = model_connector->get_db_connector(); + + if( db_connector ) + { + try + { + table(); + + if( with_schema_name && !model_env->schema_name.empty() ) + { + stream << model_env->schema_name; + stream << '.'; + } + + stream << model_env->table_name; + } + catch(...) + { + model_env = nullptr; + throw; + } + } + } + + model_env = nullptr; +} + + +void Model::get_table_name(std::wstring & str, bool with_schema_name, ModelData * model_data, bool clear_string) +{ + PT::WTextStream stream; + + if( clear_string ) + str.clear(); + + get_table_name(stream, with_schema_name, model_data, false); + stream.to_string(str); +} + + +void Model::get_table_name(std::string & str, bool with_schema_name, ModelData * model_data, bool clear_string) +{ + PT::WTextStream stream; + + get_table_name(stream, with_schema_name, model_data, false); + PT::WideStreamToUTF8(stream, str, clear_string); +} + + void Model::to_text(PT::TextStream & stream, ModelData * model_data, bool clear_stream, bool dump_mode) { if( clear_stream ) @@ -272,6 +335,7 @@ std::string Model::to_string() + void Model::generate_insert_query(PT::TextStream & stream, ModelData * model_data) { ModelEnv model_env_local; @@ -797,6 +861,10 @@ void Model::clear() +bool Model::do_migration(int & current_table_version) +{ + return true; +} @@ -1449,5 +1517,50 @@ void Model::set_parent_key_in_childs() } } + + +bool Model::db_query(const char * raw_sql) +{ + bool status = false; + + if( model_connector ) + { + DbConnector * db_connector = model_connector->get_db_connector(); + + if( db_connector ) + { + status = db_connector->query(raw_sql); + } + } + + return status; +} + + +bool Model::db_query(const std::string & raw_sql) +{ + return db_query(raw_sql.c_str()); +} + + +bool Model::db_query(const PT::TextStream & raw_sql) +{ + bool status = false; + + if( model_connector ) + { + DbConnector * db_connector = model_connector->get_db_connector(); + + if( db_connector ) + { + status = db_connector->query(raw_sql); + } + } + + return status; +} + + + } // namespace diff --git a/src/model.h b/src/model.h index 7b96376..b1c6334 100644 --- a/src/model.h +++ b/src/model.h @@ -86,6 +86,10 @@ public: void set_connector(ModelConnector * connector); ModelConnector * get_connector(); + virtual void get_table_name(PT::WTextStream & stream, bool with_schema_name = true, ModelData * model_data = nullptr, bool clear_stream = true); + 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); @@ -122,6 +126,9 @@ public: // set object to default values virtual void clear(); + virtual bool do_migration(int & current_table_version); + + // IMPROVE ME this will be protected // add set_field_value() functions for each POD type template @@ -210,6 +217,9 @@ protected: virtual void map_values_from_query(); + virtual bool db_query(const char * raw_sql); + virtual bool db_query(const std::string & raw_sql); + virtual bool db_query(const PT::TextStream & raw_sql); ///////////////////////////////// /* @@ -972,6 +982,28 @@ protected: virtual void log_table_name(bool put_schema_name = true); virtual void log_table_name_with_field(const wchar_t * db_field_name = nullptr, bool put_schema_name = true); + template + bool do_migration(int & current_table_version, int destination_table_version, ModelObject * model_object, bool (ModelObject::*migration_function)()) + { + bool status = true; + + if( current_table_version < destination_table_version ) + { + if( (model_object->*migration_function)() ) + { + current_table_version = destination_table_version; + } + else + { + status = false; + } + } + + return status; + } + + + template friend class Finder; template friend class Cursor; friend class BaseExpression; From 34274ca230da44891d8ad5a3f24954920b9f47d7 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Thu, 20 May 2021 16:25:01 +0200 Subject: [PATCH 22/27] namespace PT renamed to pt --- samples/attachment.h | 2 +- samples/attachment2.h | 2 +- samples/main.cpp | 16 ++-- src/baseexpression.cpp | 156 ++++++++++++++++----------------- src/baseexpression.h | 160 +++++++++++++++++----------------- src/clearer.cpp | 4 +- src/clearer.h | 4 +- src/dbconnector.cpp | 76 ++++++++-------- src/dbconnector.h | 40 ++++----- src/dbexpression.cpp | 8 +- src/dbexpression.h | 8 +- src/finder.h | 16 ++-- src/finderhelper.h | 4 +- src/flatconnector.cpp | 2 +- src/flatconnector.h | 2 +- src/flatexpression.cpp | 2 +- src/flatexpression.h | 2 +- src/jsonexpression.cpp | 4 +- src/jsonexpression.h | 2 +- src/model.cpp | 76 ++++++++-------- src/model.h | 54 ++++++------ src/modelconnector.cpp | 12 +-- src/modelconnector.h | 14 +-- src/modelenv.h | 4 +- src/postgresqlconnector.cpp | 46 +++++----- src/postgresqlconnector.h | 14 +-- src/postgresqlexpression.cpp | 6 +- src/postgresqlexpression.h | 6 +- src/postgresqlqueryresult.cpp | 14 +-- src/postgresqlqueryresult.h | 2 +- src/queryresult.cpp | 6 +- src/queryresult.h | 2 +- 32 files changed, 383 insertions(+), 383 deletions(-) diff --git a/samples/attachment.h b/samples/attachment.h index a247ab1..ca8e256 100644 --- a/samples/attachment.h +++ b/samples/attachment.h @@ -71,7 +71,7 @@ public: std::string content; std::vector types; bool some_flags; - PT::Date created_date; + pt::Date created_date; Language language; diff --git a/samples/attachment2.h b/samples/attachment2.h index 2548060..6961707 100644 --- a/samples/attachment2.h +++ b/samples/attachment2.h @@ -71,7 +71,7 @@ public: std::string content; std::vector types; bool some_flags; - PT::Date created_date; + pt::Date created_date; Language language; diff --git a/samples/main.cpp b/samples/main.cpp index 413aa3b..063199e 100644 --- a/samples/main.cpp +++ b/samples/main.cpp @@ -61,17 +61,17 @@ void print_syntax() int main(int argc, const char ** argv) { -PT::Space args; -PT::Space & args_options = args.FindAddSpace(L"options"); +pt::Space args; +pt::Space & args_options = args.FindAddSpace(L"options"); args_options.Add(L"c", 1); // one argument - config file args_options.Add(L"config", 1); - PT::MainSpaceParser main_parser; + pt::MainSpaceParser main_parser; main_parser.UTF8(true); main_parser.SetSpace(args); - PT::MainSpaceParser::Status args_status = main_parser.Parse(argc, argv); + pt::MainSpaceParser::Status args_status = main_parser.Parse(argc, argv); - if( args_status != PT::MainSpaceParser::status_ok ) + if( args_status != pt::MainSpaceParser::status_ok ) { std::cout << "Syntax error in arguments" << std::endl; return 1; @@ -101,9 +101,9 @@ args_options.Add(L"config", 1); * create database morm_test owner morm_test; * */ - PT::Log log; - PT::FileLog file_log; - PT::WTextStream log_buffer; + pt::Log log; + pt::FileLog file_log; + pt::WTextStream log_buffer; file_log.init(L"log.txt", true, 4, true); log.SetLogBuffer(&log_buffer); diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index c14a81b..d93a4f9 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -74,12 +74,12 @@ int BaseExpression::get_work_mode() -PT::TextStream * BaseExpression::get_text_stream() +pt::TextStream * BaseExpression::get_text_stream() { return out_stream; } -void BaseExpression::set_text_stream(PT::TextStream * out_stream) +void BaseExpression::set_text_stream(pt::TextStream * out_stream) { this->out_stream = out_stream; } @@ -88,7 +88,7 @@ void BaseExpression::set_text_stream(PT::TextStream * out_stream) -PT::TextStream * BaseExpression::get_current_stream() +pt::TextStream * BaseExpression::get_current_stream() { return out_stream; } @@ -107,7 +107,7 @@ bool BaseExpression::get_allow_to_use_prefix() -void BaseExpression::generate_from_model(PT::TextStream & stream, Model & model) +void BaseExpression::generate_from_model(pt::TextStream & stream, Model & model) { this->out_stream = &stream; generate_from_model(model); @@ -208,8 +208,8 @@ void BaseExpression::put_field_name(const wchar_t * field_name, const FT & field void BaseExpression::save_foreign_key(const wchar_t * field_name, const FT & field_type, ModelEnv * model_env) { - PT::TextStream str; - PT::TextStream * old_out_stream = out_stream; + pt::TextStream str; + pt::TextStream * old_out_stream = out_stream; out_stream = &str; put_field_name(field_name, field_type, model_env); @@ -337,22 +337,22 @@ void BaseExpression::after_field_value(char, const FT & field_type) after_field_value_string(field_type); } -void BaseExpression::before_field_value(const PT::Date &, const FT & field_type) +void BaseExpression::before_field_value(const pt::Date &, const FT & field_type) { before_field_value_string(field_type); } -void BaseExpression::after_field_value(const PT::Date &, const FT & field_type) +void BaseExpression::after_field_value(const pt::Date &, const FT & field_type) { after_field_value_string(field_type); } -void BaseExpression::before_field_value(const PT::Space &, const FT & field_type) +void BaseExpression::before_field_value(const pt::Space &, const FT & field_type) { before_field_value_string(field_type); } -void BaseExpression::after_field_value(const PT::Space &, const FT & field_type) +void BaseExpression::after_field_value(const pt::Space &, const FT & field_type) { after_field_value_string(field_type); } @@ -373,7 +373,7 @@ char BaseExpression::char_to_hex_part(char c) } -void BaseExpression::char_to_hex(char c, PT::TextStream & stream) +void BaseExpression::char_to_hex(char c, pt::TextStream & stream) { stream << char_to_hex_part(((unsigned char)c) >> 4); stream << char_to_hex_part(((unsigned char)c) & 0x0f); @@ -382,7 +382,7 @@ void BaseExpression::char_to_hex(char c, PT::TextStream & stream) -void BaseExpression::esc(char val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(char val, pt::TextStream & stream, const FT & field_type) { if( field_type.is_binary() || field_type.is_hexadecimal() ) { @@ -395,19 +395,19 @@ void BaseExpression::esc(char val, PT::TextStream & stream, const FT & field_typ } -void BaseExpression::esc(unsigned char val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(unsigned char val, pt::TextStream & stream, const FT & field_type) { esc(static_cast(val), stream, field_type); } -void BaseExpression::esc(wchar_t val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(wchar_t val, pt::TextStream & stream, const FT & field_type) { if( field_type.use_utf8() ) { char utf8_buf[10]; - size_t utf8_len = PT::IntToUTF8((int)val, utf8_buf, sizeof(utf8_buf)); + size_t utf8_len = pt::IntToUTF8((int)val, utf8_buf, sizeof(utf8_buf)); for(size_t a = 0 ; a < utf8_len ; ++a) { @@ -421,7 +421,7 @@ void BaseExpression::esc(wchar_t val, PT::TextStream & stream, const FT & field_ } -void BaseExpression::esc(const wchar_t * val, bool has_known_length, size_t len, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(const wchar_t * val, bool has_known_length, size_t len, pt::TextStream & stream, const FT & field_type) { if( field_type.use_utf8() ) { @@ -429,7 +429,7 @@ void BaseExpression::esc(const wchar_t * val, bool has_known_length, size_t len, for(size_t i = 0 ; has_known_length ? (i < len) : val[i] != 0 ; ++i) { - size_t utf8_len = PT::IntToUTF8((int)val[i], utf8_buf, sizeof(utf8_buf)); + size_t utf8_len = pt::IntToUTF8((int)val[i], utf8_buf, sizeof(utf8_buf)); for(size_t a = 0 ; a < utf8_len ; ++a) { @@ -447,19 +447,19 @@ void BaseExpression::esc(const wchar_t * val, bool has_known_length, size_t len, } -void BaseExpression::esc(const std::wstring & val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(const std::wstring & val, pt::TextStream & stream, const FT & field_type) { esc(val.c_str(), true, val.size(), stream, field_type); } -void BaseExpression::esc(const wchar_t * val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(const wchar_t * val, pt::TextStream & stream, const FT & field_type) { esc(val, false, 0, stream, field_type); } -void BaseExpression::esc(const std::string & val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(const std::string & val, pt::TextStream & stream, const FT & field_type) { for(size_t i = 0 ; i < val.size() ; ++i) { @@ -468,7 +468,7 @@ void BaseExpression::esc(const std::string & val, PT::TextStream & stream, const } -void BaseExpression::esc(const char * val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(const char * val, pt::TextStream & stream, const FT & field_type) { for(size_t i = 0 ; val[i] != 0 ; ++i) { @@ -477,7 +477,7 @@ void BaseExpression::esc(const char * val, PT::TextStream & stream, const FT & f } -void BaseExpression::esc(bool val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(bool val, pt::TextStream & stream, const FT & field_type) { if( val ) stream << "true"; @@ -486,82 +486,82 @@ void BaseExpression::esc(bool val, PT::TextStream & stream, const FT & field_typ } -void BaseExpression::esc(short val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(short val, pt::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(unsigned short val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(unsigned short val, pt::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(int val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(int val, pt::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(unsigned int val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(unsigned int val, pt::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(long val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(long val, pt::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(unsigned long val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(unsigned long val, pt::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(long long val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(long long val, pt::TextStream & stream, const FT & field_type) { - // not implemented in PT::TextStream yet + // not implemented in pt::TextStream yet //stream << val; } -void BaseExpression::esc(unsigned long long val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(unsigned long long val, pt::TextStream & stream, const FT & field_type) { //stream << val; } -void BaseExpression::esc(float val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(float val, pt::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(double val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(double val, pt::TextStream & stream, const FT & field_type) { stream << val; } -void BaseExpression::esc(long double val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(long double val, pt::TextStream & stream, const FT & field_type) { - // IMPLEMENT ME in PT::TextStream + // IMPLEMENT ME in pt::TextStream //stream << val; } -void BaseExpression::esc(const PT::Date & date, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(const pt::Date & date, pt::TextStream & stream, const FT & field_type) { stream << date; } -void BaseExpression::esc(const PT::TextStream & val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(const pt::TextStream & val, pt::TextStream & stream, const FT & field_type) { - PT::TextStream::const_iterator i = val.begin(); + pt::TextStream::const_iterator i = val.begin(); for(; i != val.end() ; ++i) { @@ -569,9 +569,9 @@ void BaseExpression::esc(const PT::TextStream & val, PT::TextStream & stream, co } } -void BaseExpression::esc(const PT::WTextStream & val, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(const pt::WTextStream & val, pt::TextStream & stream, const FT & field_type) { - PT::WTextStream::const_iterator i = val.begin(); + pt::WTextStream::const_iterator i = val.begin(); for(; i != val.end() ; ++i) { @@ -579,119 +579,119 @@ void BaseExpression::esc(const PT::WTextStream & val, PT::TextStream & stream, c } } -void BaseExpression::esc(const PT::Space & space, PT::TextStream & stream, const FT & field_type) +void BaseExpression::esc(const pt::Space & space, pt::TextStream & stream, const FT & field_type) { - PT::WTextStream tmp_stream; + pt::WTextStream tmp_stream; space.serialize_to_space_stream(tmp_stream, true); esc(tmp_stream, stream, field_type); } -//void BaseExpression::put_type(char val, PT::TextStream & stream) +//void BaseExpression::put_type(char val, pt::TextStream & stream) //{ // stream << "char"; //} // -//void BaseExpression::put_type(unsigned char val, PT::TextStream & stream) +//void BaseExpression::put_type(unsigned char val, pt::TextStream & stream) //{ // stream << "unsigned char"; //} // // -//void BaseExpression::put_type(const std::wstring & val, PT::TextStream & stream) +//void BaseExpression::put_type(const std::wstring & val, pt::TextStream & stream) //{ // stream << "text"; //} // -//void BaseExpression::put_type(const wchar_t * val, PT::TextStream & stream) +//void BaseExpression::put_type(const wchar_t * val, pt::TextStream & stream) //{ // stream << "text"; //} // // -//void BaseExpression::put_type(const std::string & val, PT::TextStream & stream) +//void BaseExpression::put_type(const std::string & val, pt::TextStream & stream) //{ // stream << "text"; //} // -//void BaseExpression::put_type(const char * val, PT::TextStream & stream) +//void BaseExpression::put_type(const char * val, pt::TextStream & stream) //{ // stream << "text"; //} // // -//void BaseExpression::put_type(bool val, PT::TextStream & stream) +//void BaseExpression::put_type(bool val, pt::TextStream & stream) //{ // stream << "boolean"; //} // -//void BaseExpression::put_type(short val, PT::TextStream & stream) +//void BaseExpression::put_type(short val, pt::TextStream & stream) //{ // stream << "short integer"; //} // -//void BaseExpression::put_type(unsigned short val, PT::TextStream & stream) +//void BaseExpression::put_type(unsigned short val, pt::TextStream & stream) //{ // stream << "unsigned short integer"; //} // -//void BaseExpression::put_type(int val, PT::TextStream & stream) +//void BaseExpression::put_type(int val, pt::TextStream & stream) //{ // stream << "integer"; //} // -//void BaseExpression::put_type(unsigned int val, PT::TextStream & stream) +//void BaseExpression::put_type(unsigned int val, pt::TextStream & stream) //{ // stream << "unsigned integer"; //} // -//void BaseExpression::put_type(long val, PT::TextStream & stream) +//void BaseExpression::put_type(long val, pt::TextStream & stream) //{ // stream << "long integer"; //} // -//void BaseExpression::put_type(unsigned long val, PT::TextStream & stream) +//void BaseExpression::put_type(unsigned long val, pt::TextStream & stream) //{ // stream << "unsigned long integer"; //} // -//void BaseExpression::put_type(long long val, PT::TextStream & stream) +//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) +//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) +//void BaseExpression::put_type(float val, pt::TextStream & stream) //{ // stream << "float"; //} // -//void BaseExpression::put_type(double val, PT::TextStream & stream) +//void BaseExpression::put_type(double val, pt::TextStream & stream) //{ // stream << "double"; //} // -//void BaseExpression::put_type(long double val, PT::TextStream & stream) +//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(void* val, pt::TextStream & stream) ////{ ////} // // -//void BaseExpression::put_type(const PT::Date & date, 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) +//void BaseExpression::put_type(const Model & model, pt::TextStream & stream) //{ // stream << "object"; //} @@ -740,7 +740,7 @@ void BaseExpression::put_schema_table(const wchar_t * schema_name, const wchar_t /* * schema_name can be empty - in such a case we do not put the schema name */ -void BaseExpression::put_schema_table(const PT::WTextStream & schema_name, const PT::WTextStream & table_name) +void BaseExpression::put_schema_table(const pt::WTextStream & schema_name, const pt::WTextStream & table_name) { if( out_stream ) { @@ -778,7 +778,7 @@ void BaseExpression::put_table(const wchar_t * table_name) } -void BaseExpression::put_table(const PT::WTextStream & table_name) +void BaseExpression::put_table(const pt::WTextStream & table_name) { if( out_stream ) { @@ -806,7 +806,7 @@ void BaseExpression::put_table_with_index(const wchar_t * table_name, int index) } -void BaseExpression::put_table_with_index(const PT::WTextStream & table_name, int index) +void BaseExpression::put_table_with_index(const pt::WTextStream & table_name, int index) { if( out_stream ) { @@ -833,7 +833,7 @@ void BaseExpression::put_table_with_index_and_field(const wchar_t * table_name, } } -void BaseExpression::put_table_with_index_and_field(const PT::WTextStream & table_name, int index, const wchar_t * field_name, const FT & field_type) +void BaseExpression::put_table_with_index_and_field(const pt::WTextStream & table_name, int index, const wchar_t * field_name, const FT & field_type) { if( out_stream ) { @@ -854,7 +854,7 @@ void BaseExpression::put_table_and_field(const wchar_t * table_name, const wchar } } -void BaseExpression::put_table_and_field(const PT::WTextStream & table_name, const wchar_t * field_name, const FT & field_type) +void BaseExpression::put_table_and_field(const pt::WTextStream & table_name, const wchar_t * field_name, const FT & field_type) { if( out_stream ) { @@ -866,7 +866,7 @@ void BaseExpression::put_table_and_field(const PT::WTextStream & table_name, con -void BaseExpression::schema_table_to_stream(PT::TextStream & stream, const wchar_t * schema_name, const wchar_t * table_name) +void BaseExpression::schema_table_to_stream(pt::TextStream & stream, const wchar_t * schema_name, const wchar_t * table_name) { this->out_stream = &stream; put_schema_table(schema_name, table_name); @@ -874,7 +874,7 @@ void BaseExpression::schema_table_to_stream(PT::TextStream & stream, const wchar } -void BaseExpression::schema_table_to_stream(PT::TextStream & stream, const PT::WTextStream & schema_name, const PT::WTextStream & table_name) +void BaseExpression::schema_table_to_stream(pt::TextStream & stream, const pt::WTextStream & schema_name, const pt::WTextStream & table_name) { this->out_stream = &stream; put_schema_table(schema_name, table_name); @@ -882,7 +882,7 @@ void BaseExpression::schema_table_to_stream(PT::TextStream & stream, const PT::W } -void BaseExpression::table_to_stream(PT::TextStream & stream, const wchar_t * table_name) +void BaseExpression::table_to_stream(pt::TextStream & stream, const wchar_t * table_name) { this->out_stream = &stream; put_table(table_name); @@ -890,7 +890,7 @@ void BaseExpression::table_to_stream(PT::TextStream & stream, const wchar_t * ta } -void BaseExpression::table_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name) +void BaseExpression::table_to_stream(pt::TextStream & stream, const pt::WTextStream & table_name) { this->out_stream = &stream; put_table(table_name); @@ -898,7 +898,7 @@ void BaseExpression::table_to_stream(PT::TextStream & stream, const PT::WTextStr } -void BaseExpression::table_with_index_to_stream(PT::TextStream & stream, const wchar_t * table_name, int index) +void BaseExpression::table_with_index_to_stream(pt::TextStream & stream, const wchar_t * table_name, int index) { this->out_stream = &stream; put_table_with_index(table_name, index); @@ -906,7 +906,7 @@ void BaseExpression::table_with_index_to_stream(PT::TextStream & stream, const w } -void BaseExpression::table_with_index_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, int index) +void BaseExpression::table_with_index_to_stream(pt::TextStream & stream, const pt::WTextStream & table_name, int index) { this->out_stream = &stream; put_table_with_index(table_name, index); @@ -914,7 +914,7 @@ void BaseExpression::table_with_index_to_stream(PT::TextStream & stream, const P } -void BaseExpression::table_with_index_and_field_to_stream(PT::TextStream & stream, const wchar_t * table_name, int index, const wchar_t * field_name, const FT & field_type) +void BaseExpression::table_with_index_and_field_to_stream(pt::TextStream & stream, const wchar_t * table_name, int index, const wchar_t * field_name, const FT & field_type) { this->out_stream = &stream; put_table_with_index_and_field(table_name, index, field_name, field_type); @@ -922,7 +922,7 @@ void BaseExpression::table_with_index_and_field_to_stream(PT::TextStream & strea } -void BaseExpression::table_with_index_and_field_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, int index, const wchar_t * field_name, const FT & field_type) +void BaseExpression::table_with_index_and_field_to_stream(pt::TextStream & stream, const pt::WTextStream & table_name, int index, const wchar_t * field_name, const FT & field_type) { this->out_stream = &stream; put_table_with_index_and_field(table_name, index, field_name, field_type); @@ -930,7 +930,7 @@ void BaseExpression::table_with_index_and_field_to_stream(PT::TextStream & strea } -void BaseExpression::table_and_field_to_stream(PT::TextStream & stream, const wchar_t * table_name, const wchar_t * field_name, const FT & field_type) +void BaseExpression::table_and_field_to_stream(pt::TextStream & stream, const wchar_t * table_name, const wchar_t * field_name, const FT & field_type) { this->out_stream = &stream; put_table_and_field(table_name, field_name, field_type); @@ -938,7 +938,7 @@ void BaseExpression::table_and_field_to_stream(PT::TextStream & stream, const wc } -void BaseExpression::table_and_field_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, const wchar_t * field_name, const FT & field_type) +void BaseExpression::table_and_field_to_stream(pt::TextStream & stream, const pt::WTextStream & table_name, const wchar_t * field_name, const FT & field_type) { this->out_stream = &stream; put_table_and_field(table_name, field_name, field_type); diff --git a/src/baseexpression.h b/src/baseexpression.h index d29f2d8..c702422 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -60,14 +60,14 @@ public: virtual void set_work_mode(int work_mode); virtual int get_work_mode(); - virtual PT::TextStream * get_text_stream(); - virtual void set_text_stream(PT::TextStream * out_stream); + virtual pt::TextStream * get_text_stream(); + virtual void set_text_stream(pt::TextStream * out_stream); virtual void clear(); - virtual void generate_from_model(PT::TextStream & stream, Model & model); + virtual void generate_from_model(pt::TextStream & stream, Model & model); - virtual PT::TextStream * get_current_stream(); + virtual pt::TextStream * get_current_stream(); // rename me virtual void allow_to_use_prefix(bool use_prefix); @@ -143,21 +143,21 @@ public: } template - void field_in(PT::TextStream & stream, const wchar_t * field_name, const std::set & container, ModelEnv * model_env) + void field_in(pt::TextStream & stream, const wchar_t * field_name, const std::set & container, ModelEnv * model_env) { field_in_generic>(stream, field_name, container, model_env); } template - void field_in(PT::TextStream & stream, const wchar_t * field_name, const std::list & container, ModelEnv * model_env) + void field_in(pt::TextStream & stream, const wchar_t * field_name, const std::list & container, ModelEnv * model_env) { field_in_generic>(stream, field_name, container, model_env); } template - void field_in(PT::TextStream & stream, const wchar_t * field_name, const std::vector & container, ModelEnv * model_env) + void field_in(pt::TextStream & stream, const wchar_t * field_name, const std::vector & container, ModelEnv * model_env) { field_in_generic>(stream, field_name, container, model_env); } @@ -215,7 +215,7 @@ public: } template - void field_to_stream(PT::TextStream & stream, const wchar_t * field_name, const FieldValue & field_value, const FT & field_type, ModelEnv * model_env) + void field_to_stream(pt::TextStream & stream, const wchar_t * field_name, const FieldValue & field_value, const FT & field_type, ModelEnv * model_env) { this->out_stream = &stream; field(field_name, field_value, field_type, model_env); @@ -224,26 +224,26 @@ public: virtual void put_schema_table(const wchar_t * schema_name, const wchar_t * table_name); - virtual void put_schema_table(const PT::WTextStream & schema_name, const PT::WTextStream & table_name); + virtual void put_schema_table(const pt::WTextStream & schema_name, const pt::WTextStream & table_name); virtual void put_table(const wchar_t * table_name); - virtual void put_table(const PT::WTextStream & table_name); + virtual void put_table(const pt::WTextStream & table_name); virtual void put_table_with_index(const wchar_t * table_name, int index); - virtual void put_table_with_index(const PT::WTextStream & table_name, int index); + virtual void put_table_with_index(const pt::WTextStream & table_name, int index); virtual void put_table_with_index_and_field(const wchar_t * table_name, int index, const wchar_t * field_name, const FT & field_type); - virtual void put_table_with_index_and_field(const PT::WTextStream & table_name, int index, const wchar_t * field_name, const FT & field_type); + virtual void put_table_with_index_and_field(const pt::WTextStream & table_name, int index, const wchar_t * field_name, const FT & field_type); virtual void put_table_and_field(const wchar_t * table_name, const wchar_t * field_name, const FT & field_type); - virtual void put_table_and_field(const PT::WTextStream & table_name, const wchar_t * field_name, const FT & field_type); + virtual void put_table_and_field(const pt::WTextStream & table_name, const wchar_t * field_name, const FT & field_type); - virtual void schema_table_to_stream(PT::TextStream & stream, const wchar_t * schema_name, const wchar_t * table_name); - virtual void schema_table_to_stream(PT::TextStream & stream, const PT::WTextStream & schema_name, const PT::WTextStream & table_name); - virtual void table_to_stream(PT::TextStream & stream, const wchar_t * table_name); - virtual void table_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name); - virtual void table_with_index_to_stream(PT::TextStream & stream, const wchar_t * table_name, int index); - virtual void table_with_index_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, int index); - virtual void table_with_index_and_field_to_stream(PT::TextStream & stream, const wchar_t * table_name, int index, const wchar_t * field_name, const FT & field_type); - virtual void table_with_index_and_field_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, int index, const wchar_t * field_name, const FT & field_type); - virtual void table_and_field_to_stream(PT::TextStream & stream, const wchar_t * table_name, const wchar_t * field_name, const FT & field_type); - virtual void table_and_field_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, const wchar_t * field_name, const FT & field_type); + virtual void schema_table_to_stream(pt::TextStream & stream, const wchar_t * schema_name, const wchar_t * table_name); + virtual void schema_table_to_stream(pt::TextStream & stream, const pt::WTextStream & schema_name, const pt::WTextStream & table_name); + virtual void table_to_stream(pt::TextStream & stream, const wchar_t * table_name); + virtual void table_to_stream(pt::TextStream & stream, const pt::WTextStream & table_name); + virtual void table_with_index_to_stream(pt::TextStream & stream, const wchar_t * table_name, int index); + virtual void table_with_index_to_stream(pt::TextStream & stream, const pt::WTextStream & table_name, int index); + virtual void table_with_index_and_field_to_stream(pt::TextStream & stream, const wchar_t * table_name, int index, const wchar_t * field_name, const FT & field_type); + virtual void table_with_index_and_field_to_stream(pt::TextStream & stream, const pt::WTextStream & table_name, int index, const wchar_t * field_name, const FT & field_type); + virtual void table_and_field_to_stream(pt::TextStream & stream, const wchar_t * table_name, const wchar_t * field_name, const FT & field_type); + virtual void table_and_field_to_stream(pt::TextStream & stream, const pt::WTextStream & table_name, const wchar_t * field_name, const FT & field_type); /* @@ -251,35 +251,35 @@ public: * esc for: signed char, wchar_t, char16_t, char32_t * */ - virtual void esc(char val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(unsigned char val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(char val, pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(unsigned char val, pt::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(wchar_t val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(wchar_t val, pt::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(const std::wstring & val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(const wchar_t * val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(const std::wstring & val, pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(const wchar_t * val, pt::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(const std::string & val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(const char * val, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(const std::string & val, pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(const char * val, pt::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(bool val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(short val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(unsigned short val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(int val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(unsigned int val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(long val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(unsigned long val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(long long val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(unsigned long long val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(float val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(double val, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(long double val, PT::TextStream & stream, const FT & field_type = FT::default_type); - //virtual void esc(void* val, PT::TextStream & stream); + virtual void esc(bool val, pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(short val, pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(unsigned short val, pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(int val, pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(unsigned int val, pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(long val, pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(unsigned long val, pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(long long val, pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(unsigned long long val, pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(float val, pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(double val, pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(long double val, pt::TextStream & stream, const FT & field_type = FT::default_type); + //virtual void esc(void* val, pt::TextStream & stream); - virtual void esc(const PT::Date & date, PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(const PT::TextStream & val,PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(const PT::WTextStream & val,PT::TextStream & stream, const FT & field_type = FT::default_type); - virtual void esc(const PT::Space & space, PT::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(const pt::Date & date, pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(const pt::TextStream & val,pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(const pt::WTextStream & val,pt::TextStream & stream, const FT & field_type = FT::default_type); + virtual void esc(const pt::Space & space, pt::TextStream & stream, const FT & field_type = FT::default_type); @@ -288,7 +288,7 @@ protected: int work_mode; /* what to do: generating fields list, values list or fields-values list */ bool is_first_field; - PT::TextStream * out_stream; + pt::TextStream * out_stream; bool use_prefix; @@ -485,7 +485,7 @@ protected: put_field_value_list_non_model(field_value, model_connector); } - void put_field_value_list(std::vector & field_value, ModelConnector * model_connector, ModelEnv *) + void put_field_value_list(std::vector & field_value, ModelConnector * model_connector, ModelEnv *) { put_field_value_list_non_model(field_value, model_connector); } @@ -574,7 +574,7 @@ protected: put_field_value_list_non_model(field_value, model_connector); } - void put_field_value_list(std::list & field_value, ModelConnector * model_connector, ModelEnv *) + void put_field_value_list(std::list & field_value, ModelConnector * model_connector, ModelEnv *) { put_field_value_list_non_model(field_value, model_connector); } @@ -583,7 +583,7 @@ protected: // used in 'in()' statements, may should be renamed? template - void field_in_generic(PT::TextStream & stream, const wchar_t * field_name, const Container & container, ModelEnv * model_env) + void field_in_generic(pt::TextStream & stream, const wchar_t * field_name, const Container & container, ModelEnv * model_env) { // IMPROVE ME // what about if container is empty? @@ -645,11 +645,11 @@ protected: virtual void before_field_value(char, const FT & field_type); virtual void after_field_value(char, const FT & field_type); - virtual void before_field_value(const PT::Date &, const FT & field_type); - virtual void after_field_value(const PT::Date &, const FT & field_type); + virtual void before_field_value(const pt::Date &, const FT & field_type); + virtual void after_field_value(const pt::Date &, const FT & field_type); - virtual void before_field_value(const PT::Space &, const FT & field_type); - virtual void after_field_value(const PT::Space &, const FT & field_type); + virtual void before_field_value(const pt::Space &, const FT & field_type); + virtual void after_field_value(const pt::Space &, const FT & field_type); template void before_field_value(const FieldValue &, const FT & field_type) @@ -669,40 +669,40 @@ protected: * put_type for: signed char, wchar_t, char16_t, char32_t * */ -// virtual void put_type(char val, PT::TextStream & stream); -// virtual void put_type(unsigned char val, PT::TextStream & stream); +// 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::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(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(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); +// 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) +// void put_type(const std::list & model, pt::TextStream & stream) // { // stream << "table"; // may just use std::list? // } // // template -// void put_type(const std::vector & model, PT::TextStream & stream) +// void put_type(const std::vector & model, pt::TextStream & stream) // { // stream << "table"; // may just just std::vector? // } @@ -713,9 +713,9 @@ protected: virtual void after_field_value_string(const FT & field_type); char char_to_hex_part(char c); - void char_to_hex(char c, PT::TextStream & stream); + void char_to_hex(char c, pt::TextStream & stream); - void esc(const wchar_t * val, bool has_known_length, size_t len, PT::TextStream & stream, const FT & field_type = FT::default_type); + void esc(const wchar_t * val, bool has_known_length, size_t len, pt::TextStream & stream, const FT & field_type = FT::default_type); bool is_empty_field(const wchar_t * value); }; diff --git a/src/clearer.cpp b/src/clearer.cpp index 53b2381..97a2270 100644 --- a/src/clearer.cpp +++ b/src/clearer.cpp @@ -138,12 +138,12 @@ void Clearer::clear_value(long double & field_value) field_value = 0.0; } -void Clearer::clear_value(PT::Date & field_value) +void Clearer::clear_value(pt::Date & field_value) { field_value.Clear(); } -void Clearer::clear_value(PT::Space & field_value) +void Clearer::clear_value(pt::Space & field_value) { field_value.clear(); } diff --git a/src/clearer.h b/src/clearer.h index d6ab824..5427de2 100644 --- a/src/clearer.h +++ b/src/clearer.h @@ -69,8 +69,8 @@ public: virtual void clear_value(float & field_value); virtual void clear_value(double & field_value); virtual void clear_value(long double & field_value); - virtual void clear_value(PT::Date & field_value); - virtual void clear_value(PT::Space & field_value); + virtual void clear_value(pt::Date & field_value); + virtual void clear_value(pt::Space & field_value); virtual void clear_model(Model & field_value); diff --git a/src/dbconnector.cpp b/src/dbconnector.cpp index 12da18b..b39f8be 100644 --- a/src/dbconnector.cpp +++ b/src/dbconnector.cpp @@ -67,13 +67,13 @@ DbConnector::~DbConnector() } -void DbConnector::set_logger(PT::Log * log) +void DbConnector::set_logger(pt::Log * log) { this->log = log; } -void DbConnector::set_logger(PT::Log & log) +void DbConnector::set_logger(pt::Log & log) { this->log = &log; } @@ -85,7 +85,7 @@ void DbConnector::set_log_queries(bool log_queries) } -bool DbConnector::query(const PT::TextStream & stream) +bool DbConnector::query(const pt::TextStream & stream) { std::unique_ptr query_result_ptr(create_query_result()); return query(stream, *query_result_ptr); @@ -107,7 +107,7 @@ bool DbConnector::query(const char * query_str) -bool DbConnector::query(const PT::TextStream & stream, QueryResult & query_result) +bool DbConnector::query(const pt::TextStream & stream, QueryResult & query_result) { std::string query_str; stream.to_string(query_str); @@ -152,22 +152,22 @@ bool DbConnector::query_remove(const char * query_str, QueryResult & query_resul -bool DbConnector::query_select(const PT::TextStream & stream, QueryResult & query_result) +bool DbConnector::query_select(const pt::TextStream & stream, QueryResult & query_result) { return query(stream, query_result); } -bool DbConnector::query_update(const PT::TextStream & stream, QueryResult & query_result) +bool DbConnector::query_update(const pt::TextStream & stream, QueryResult & query_result) { return query(stream, query_result); } -bool DbConnector::query_insert(const PT::TextStream & stream, QueryResult & query_result) +bool DbConnector::query_insert(const pt::TextStream & stream, QueryResult & query_result) { return query(stream, query_result); } -bool DbConnector::query_remove(const PT::TextStream & stream, QueryResult & query_result) +bool DbConnector::query_remove(const pt::TextStream & stream, QueryResult & query_result) { return query(stream, query_result); } @@ -181,7 +181,7 @@ DbExpression * DbConnector::get_expression() } -void DbConnector::generate_select_columns(PT::TextStream & stream, Model & model) +void DbConnector::generate_select_columns(pt::TextStream & stream, Model & model) { allocate_default_expression_if_needed(); @@ -196,7 +196,7 @@ void DbConnector::generate_select_columns(PT::TextStream & stream, Model & model } -void DbConnector::generate_insert_query(PT::TextStream & stream, Model & model) +void DbConnector::generate_insert_query(pt::TextStream & stream, Model & model) { allocate_default_expression_if_needed(); @@ -221,7 +221,7 @@ void DbConnector::generate_insert_query(PT::TextStream & stream, Model & model) } -void DbConnector::generate_update_query(PT::TextStream & stream, Model & model) +void DbConnector::generate_update_query(pt::TextStream & stream, Model & model) { allocate_default_expression_if_needed(); @@ -246,7 +246,7 @@ void DbConnector::generate_update_query(PT::TextStream & stream, Model & model) } -void DbConnector::generate_remove_query(PT::TextStream & stream, Model & model) +void DbConnector::generate_remove_query(pt::TextStream & stream, Model & model) { allocate_default_expression_if_needed(); @@ -265,7 +265,7 @@ void DbConnector::generate_remove_query(PT::TextStream & stream, Model & model) } } -bool DbConnector::insert(PT::TextStream & stream, Model & model) +bool DbConnector::insert(pt::TextStream & stream, Model & model) { std::unique_ptr query_result_ptr(create_query_result()); @@ -274,7 +274,7 @@ bool DbConnector::insert(PT::TextStream & stream, Model & model) } -bool DbConnector::update(PT::TextStream & stream, Model & model) +bool DbConnector::update(pt::TextStream & stream, Model & model) { std::unique_ptr query_result_ptr(create_query_result()); @@ -283,7 +283,7 @@ bool DbConnector::update(PT::TextStream & stream, Model & model) } -bool DbConnector::remove(PT::TextStream & stream, Model & model) +bool DbConnector::remove(pt::TextStream & stream, Model & model) { std::unique_ptr query_result_ptr(create_query_result()); @@ -333,14 +333,14 @@ char DbConnector::unescape_hex_char_part(char hex) { if( log ) { - (*log) << PT::Log::log2 << "Morm: incorrect character when reading a hex string, char code: " << (int)(unsigned char)hex; + (*log) << pt::Log::log2 << "Morm: incorrect character when reading a hex string, char code: " << (int)(unsigned char)hex; if( hex >= 32 ) { (*log) << " '" << hex << "'"; } - (*log) << PT::Log::logend; + (*log) << pt::Log::logend; } } @@ -443,7 +443,7 @@ size_t DbConnector::unescape_hex_char(const char * value_str, wchar_t & field_va int value_int; bool is_correct; - len = PT::UTF8ToInt(utf8_str, utf8_str_len, value_int, is_correct); + len = pt::UTF8ToInt(utf8_str, utf8_str_len, value_int, is_correct); len = len * 2; if( is_correct ) @@ -454,7 +454,7 @@ size_t DbConnector::unescape_hex_char(const char * value_str, wchar_t & field_va { if( log ) { - (*log) << PT::Log::log2 << "Morm: incorrect utf-8 sequence (ignoring)" << PT::Log::logend; + (*log) << pt::Log::log2 << "Morm: incorrect utf-8 sequence (ignoring)" << pt::Log::logend; } } } @@ -469,7 +469,7 @@ size_t DbConnector::unescape_hex_char(const char * value_str, wchar_t & field_va { if( log ) { - (*log) << PT::Log::log2 << "Morm: unexpected end of string (ignoring)" << PT::Log::logend; + (*log) << pt::Log::log2 << "Morm: unexpected end of string (ignoring)" << pt::Log::logend; } } } @@ -503,8 +503,8 @@ void DbConnector::get_value(const char * value_str, char & field_value, const FT { if( log ) { - (*log) << PT::Log::log2 << "Morm: a character greater than 127 cannot be stored in char type, code point: " - << (int)c << " '" << c << "'" << PT::Log::logend; + (*log) << pt::Log::log2 << "Morm: a character greater than 127 cannot be stored in char type, code point: " + << (int)c << " '" << c << "'" << pt::Log::logend; } } } @@ -548,7 +548,7 @@ void DbConnector::get_value(const char * value_str, wchar_t & field_value, const int value_int; bool is_correct; - PT::UTF8ToInt(value_str, value_int, is_correct); + pt::UTF8ToInt(value_str, value_int, is_correct); if( is_correct ) { @@ -584,7 +584,7 @@ void DbConnector::get_value(const char * value_str, std::wstring & field_value, { if( field_type.use_utf8() ) { - PT::UTF8ToWide(value_str, field_value); + pt::UTF8ToWide(value_str, field_value); } else { @@ -627,56 +627,56 @@ void DbConnector::get_value(const char * value_str, bool & field_value, const FT void DbConnector::get_value(const char * value_str, short & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking - field_value = (short)PT::Toi(value_str, 10); + field_value = (short)pt::Toi(value_str, 10); } void DbConnector::get_value(const char * value_str, unsigned short & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking - field_value = (unsigned short)PT::Toui(value_str, 10); + field_value = (unsigned short)pt::Toui(value_str, 10); } void DbConnector::get_value(const char * value_str, int & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking - field_value = PT::Toi(value_str, 10); + field_value = pt::Toi(value_str, 10); } void DbConnector::get_value(const char * value_str, unsigned int & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking - field_value = PT::Toui(value_str, 10); + field_value = pt::Toui(value_str, 10); } void DbConnector::get_value(const char * value_str, long & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking - field_value = PT::Tol(value_str, 10); + field_value = pt::Tol(value_str, 10); } void DbConnector::get_value(const char * value_str, unsigned long & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking - field_value = PT::Toul(value_str, 10); + field_value = pt::Toul(value_str, 10); } void DbConnector::get_value(const char * value_str, long long & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking - field_value = PT::Toll(value_str, 10); + field_value = pt::Toll(value_str, 10); } void DbConnector::get_value(const char * value_str, unsigned long long & field_value, const FT & field_type) { // IMPROVE ME give some overflow checking - field_value = PT::Toull(value_str, 10); + field_value = pt::Toull(value_str, 10); } @@ -701,30 +701,30 @@ void DbConnector::get_value(const char * value_str, long double & field_value, c } -void DbConnector::get_value(const char * value_str, PT::Date & field_value, const FT & field_type) +void DbConnector::get_value(const char * value_str, pt::Date & field_value, const FT & field_type) { // IMPROVE ME give some log if parsing failed field_value.Parse(value_str); } -void DbConnector::get_value(const char * value_str, PT::Space & field_value, const FT & field_type) +void DbConnector::get_value(const char * value_str, pt::Space & field_value, const FT & field_type) { field_value.clear(); if( *value_str != '\0' ) { - PT::SpaceParser space_parser; + pt::SpaceParser space_parser; space_parser.SetSpace(field_value); - if( space_parser.ParseSpace(value_str) != PT::SpaceParser::ok ) + if( space_parser.ParseSpace(value_str) != pt::SpaceParser::ok ) { field_value.clear(); if( log ) { - (*log) << PT::Log::log2 << "Morm: I cannot correctly parse the Space struct from the datebase" - << ", the raw string is: " << value_str << PT::Log::logend; + (*log) << pt::Log::log2 << "Morm: I cannot correctly parse the Space struct from the datebase" + << ", the raw string is: " << value_str << pt::Log::logend; } } } diff --git a/src/dbconnector.h b/src/dbconnector.h index a87a896..e4f76e5 100644 --- a/src/dbconnector.h +++ b/src/dbconnector.h @@ -55,29 +55,29 @@ public: DbConnector(const DbConnector &); virtual ~DbConnector(); - virtual void set_logger(PT::Log * log); - virtual void set_logger(PT::Log & log); + virtual void set_logger(pt::Log * log); + virtual void set_logger(pt::Log & log); virtual void set_log_queries(bool log_queries); DbExpression * get_expression(); - virtual void generate_select_columns(PT::TextStream & stream, Model & model); - virtual void generate_insert_query(PT::TextStream & stream, Model & model); - virtual void generate_update_query(PT::TextStream & stream, Model & model); - virtual void generate_remove_query(PT::TextStream & stream, Model & model); + virtual void generate_select_columns(pt::TextStream & stream, Model & model); + virtual void generate_insert_query(pt::TextStream & stream, Model & model); + virtual void generate_update_query(pt::TextStream & stream, Model & model); + virtual void generate_remove_query(pt::TextStream & stream, Model & model); - virtual bool insert(PT::TextStream & stream, Model & model); - virtual bool update(PT::TextStream & stream, Model & model); - virtual bool remove(PT::TextStream & stream, Model & model); + virtual bool insert(pt::TextStream & stream, Model & model); + virtual bool update(pt::TextStream & stream, Model & model); + virtual bool remove(pt::TextStream & stream, Model & model); - virtual bool query(const PT::TextStream & stream); + virtual bool query(const pt::TextStream & stream); virtual bool query(const std::string & query_str); virtual bool query(const char * query_str); virtual QueryResult * create_query_result() = 0; - virtual bool query(const PT::TextStream & stream, QueryResult & query_result); + virtual bool query(const pt::TextStream & stream, QueryResult & query_result); virtual bool query(const std::string & query_str, QueryResult & query_result); virtual bool query(const char * query_str, QueryResult & query_result); @@ -86,10 +86,10 @@ public: virtual bool query_insert(const char * query_str, QueryResult & query_result); virtual bool query_remove(const char * query_str, QueryResult & query_result); - virtual bool query_select(const PT::TextStream & stream, QueryResult & query_result); - virtual bool query_update(const PT::TextStream & stream, QueryResult & query_result); - virtual bool query_insert(const PT::TextStream & stream, QueryResult & query_result); - virtual bool query_remove(const PT::TextStream & stream, QueryResult & query_result); + virtual bool query_select(const pt::TextStream & stream, QueryResult & query_result); + virtual bool query_update(const pt::TextStream & stream, QueryResult & query_result); + virtual bool query_insert(const pt::TextStream & stream, QueryResult & query_result); + virtual bool query_remove(const pt::TextStream & stream, QueryResult & query_result); virtual void get_value(const char * value_str, char & field_value, const FT & field_type = FT::default_type); virtual void get_value(const char * value_str, unsigned char & field_value, const FT & field_type = FT::default_type); @@ -108,10 +108,10 @@ public: virtual void get_value(const char * value_str, float & field_value, const FT & field_type = FT::default_type); virtual void get_value(const char * value_str, double & field_value, const FT & field_type = FT::default_type); virtual void get_value(const char * value_str, long double & field_value, const FT & field_type = FT::default_type); - virtual void get_value(const char * value_str, PT::Date & field_value, const FT & field_type = FT::default_type); - virtual void get_value(const char * value_str, PT::Space & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, pt::Date & field_value, const FT & field_type = FT::default_type); + virtual void get_value(const char * value_str, pt::Space & field_value, const FT & field_type = FT::default_type); - // add get_value for PT::TextStream and PT::WTextStream + // add get_value for pt::TextStream and pt::WTextStream template bool get_last_sequence(const wchar_t * sequence_table_name, FieldValue & field_value) @@ -124,7 +124,7 @@ public: if( log && log_queries ) { - (*log) << PT::Log::log3 << "Morm: sequence value: " << field_value << PT::Log::logend; + (*log) << pt::Log::log3 << "Morm: sequence value: " << field_value << pt::Log::logend; } return true; @@ -138,7 +138,7 @@ protected: DbExpression * db_expression; bool expression_allocated; - PT::Log * log; + pt::Log * log; bool log_queries; diff --git a/src/dbexpression.cpp b/src/dbexpression.cpp index c7b08db..ac92a2e 100644 --- a/src/dbexpression.cpp +++ b/src/dbexpression.cpp @@ -251,7 +251,7 @@ void DbExpression::prepare_to_where_clause() } -DbExpression & DbExpression::group_or(PT::TextStream & stream) +DbExpression & DbExpression::group_or(pt::TextStream & stream) { out_stream = &stream; field_before(); @@ -263,7 +263,7 @@ DbExpression & DbExpression::group_or(PT::TextStream & stream) return *this; } -DbExpression & DbExpression::group_and(PT::TextStream & stream) +DbExpression & DbExpression::group_and(pt::TextStream & stream) { out_stream = &stream; field_before(); @@ -275,7 +275,7 @@ DbExpression & DbExpression::group_and(PT::TextStream & stream) return *this; } -DbExpression & DbExpression::group_end(PT::TextStream & stream) +DbExpression & DbExpression::group_end(pt::TextStream & stream) { out_stream = &stream; @@ -291,7 +291,7 @@ DbExpression & DbExpression::group_end(PT::TextStream & stream) } -DbExpression & DbExpression::page(PT::TextStream & stream, size_t page_number, size_t page_size) +DbExpression & DbExpression::page(pt::TextStream & stream, size_t page_number, size_t page_size) { stream << " limit " << page_number << "," << page_size << " "; return *this; diff --git a/src/dbexpression.h b/src/dbexpression.h index 81b1636..0de03ab 100644 --- a/src/dbexpression.h +++ b/src/dbexpression.h @@ -56,11 +56,11 @@ public: virtual void prepare_to_where_clause(); - virtual DbExpression & group_or(PT::TextStream & stream); - virtual DbExpression & group_and(PT::TextStream & stream); - virtual DbExpression & group_end(PT::TextStream & stream); + virtual DbExpression & group_or(pt::TextStream & stream); + virtual DbExpression & group_and(pt::TextStream & stream); + virtual DbExpression & group_end(pt::TextStream & stream); - virtual DbExpression & page(PT::TextStream & stream, size_t page_number, size_t page_size); + virtual DbExpression & page(pt::TextStream & stream, size_t page_number, size_t page_size); template void add_field_for_select(const wchar_t * new_column_expression, const wchar_t * new_column_name, FieldValue & field_value, const FT & field_type, ModelEnv * model_env) diff --git a/src/finder.h b/src/finder.h index c369da9..e37ebb9 100644 --- a/src/finder.h +++ b/src/finder.h @@ -66,7 +66,7 @@ public: } - Finder(PT::TextStream & out_stream) + Finder(pt::TextStream & out_stream) { this->out_stream = &out_stream; this->model_connector = nullptr; @@ -95,7 +95,7 @@ public: set_out_stream(); } - Finder(PT::TextStream & out_stream, ModelConnector & model_connector) + Finder(pt::TextStream & out_stream, ModelConnector & model_connector) { this->out_stream = &out_stream; this->model_connector = &model_connector; @@ -104,7 +104,7 @@ public: model_data = nullptr; } - Finder(PT::TextStream & out_stream, ModelConnector * model_connector) + Finder(pt::TextStream & out_stream, ModelConnector * model_connector) { this->out_stream = &out_stream; this->model_connector = model_connector; @@ -116,7 +116,7 @@ public: - Finder & set_out_stream(PT::TextStream * out_stream) + Finder & set_out_stream(pt::TextStream * out_stream) { this->out_stream = out_stream; return *this; @@ -204,7 +204,7 @@ public: return select(); } - Finder & select(PT::TextStream & out_stream, ModelConnector & model_connector) + Finder & select(pt::TextStream & out_stream, ModelConnector & model_connector) { this->out_stream = &out_stream; this->model_connector = &model_connector; @@ -213,7 +213,7 @@ public: return select(); } - Finder & select(PT::TextStream & out_stream, ModelConnector * model_connector) + Finder & select(pt::TextStream & out_stream, ModelConnector * model_connector) { this->out_stream = &out_stream; this->model_connector = model_connector; @@ -726,7 +726,7 @@ public: } - Finder & raw(PT::TextStream & sql, bool add_spaces = true) + Finder & raw(pt::TextStream & sql, bool add_spaces = true) { if( out_stream ) { @@ -855,7 +855,7 @@ protected: private: - PT::TextStream * out_stream; + pt::TextStream * out_stream; ModelConnector * model_connector; DbExpression * db_expression; ModelClass model; diff --git a/src/finderhelper.h b/src/finderhelper.h index 7a771ff..b0adf7b 100644 --- a/src/finderhelper.h +++ b/src/finderhelper.h @@ -46,7 +46,7 @@ class FinderHelper { public: - PT::TextStream join_tables_str; + pt::TextStream join_tables_str; std::map join_tables_map; @@ -72,7 +72,7 @@ public: } - virtual int add_join_table(const PT::WTextStream & table_name) + virtual int add_join_table(const pt::WTextStream & table_name) { std::wstring table_name_str; table_name.to_string(table_name_str); diff --git a/src/flatconnector.cpp b/src/flatconnector.cpp index 99bdb7d..2a0d2b8 100644 --- a/src/flatconnector.cpp +++ b/src/flatconnector.cpp @@ -68,7 +68,7 @@ FlatExpression * FlatConnector::get_expression() -void FlatConnector::to_text(PT::TextStream & stream, Model & model) +void FlatConnector::to_text(pt::TextStream & stream, Model & model) { allocate_default_expression_if_needed(); diff --git a/src/flatconnector.h b/src/flatconnector.h index d1e403a..a762b13 100644 --- a/src/flatconnector.h +++ b/src/flatconnector.h @@ -51,7 +51,7 @@ public: FlatConnector(); virtual ~FlatConnector(); - virtual void to_text(PT::TextStream & stream, Model & model); + virtual void to_text(pt::TextStream & stream, Model & model); virtual void set_expression(FlatExpression & expression); virtual FlatExpression * get_expression(); diff --git a/src/flatexpression.cpp b/src/flatexpression.cpp index 884604a..7a14d87 100644 --- a/src/flatexpression.cpp +++ b/src/flatexpression.cpp @@ -39,7 +39,7 @@ namespace morm { -void FlatExpression::esc(const PT::Date & date, PT::TextStream & stream, const FT & field_type) +void FlatExpression::esc(const pt::Date & date, pt::TextStream & stream, const FT & field_type) { date.SerializeISO(stream); } diff --git a/src/flatexpression.h b/src/flatexpression.h index 87e362e..075a14e 100644 --- a/src/flatexpression.h +++ b/src/flatexpression.h @@ -45,7 +45,7 @@ class FlatExpression : public BaseExpression { public: - void esc(const PT::Date & date, PT::TextStream & stream, const FT & field_type); + void esc(const pt::Date & date, pt::TextStream & stream, const FT & field_type); diff --git a/src/jsonexpression.cpp b/src/jsonexpression.cpp index 6648df0..9b1db1b 100644 --- a/src/jsonexpression.cpp +++ b/src/jsonexpression.cpp @@ -117,7 +117,7 @@ void JSONExpression::after_field_value_list() } -void JSONExpression::esc(char val, PT::TextStream & stream, const FT & field_type) +void JSONExpression::esc(char val, pt::TextStream & stream, const FT & field_type) { if( field_type.is_hexadecimal() || field_type.is_binary() ) { @@ -129,7 +129,7 @@ void JSONExpression::esc(char val, PT::TextStream & stream, const FT & field_typ { char buf[10]; size_t len; - PT::Toa((unsigned char)val, buf, sizeof(buf)/sizeof(char), 16, &len); + pt::Toa((unsigned char)val, buf, sizeof(buf)/sizeof(char), 16, &len); stream << "\\u"; diff --git a/src/jsonexpression.h b/src/jsonexpression.h index 3ef6ab8..ebc78ca 100644 --- a/src/jsonexpression.h +++ b/src/jsonexpression.h @@ -66,7 +66,7 @@ protected: // 'morm::JSONExpression::esc' hides overloaded virtual function [-Woverloaded-virtual] using FlatExpression::esc; - void esc(char val, PT::TextStream & stream, const FT & field_type); + void esc(char val, pt::TextStream & stream, const FT & field_type); private: diff --git a/src/model.cpp b/src/model.cpp index d424819..c44bbac 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -132,11 +132,11 @@ void Model::table() { if( model_connector ) { - PT::Log * plog = model_connector->get_logger(); + pt::Log * plog = model_connector->get_logger(); if( plog ) { - (*plog) << PT::Log::log1 << "Morm: you should provide the table name e.g. provide table() method and call table(...) there" << PT::Log::logend; + (*plog) << pt::Log::log1 << "Morm: you should provide the table name e.g. provide table() method and call table(...) there" << pt::Log::logend; } } } @@ -178,7 +178,7 @@ bool Model::found() } -void Model::get_table_name(PT::WTextStream & stream, bool with_schema_name, ModelData * model_data, bool clear_stream) +void Model::get_table_name(pt::WTextStream & stream, bool with_schema_name, ModelData * model_data, bool clear_stream) { if( clear_stream ) { @@ -221,7 +221,7 @@ void Model::get_table_name(PT::WTextStream & stream, bool with_schema_name, Mode void Model::get_table_name(std::wstring & str, bool with_schema_name, ModelData * model_data, bool clear_string) { - PT::WTextStream stream; + pt::WTextStream stream; if( clear_string ) str.clear(); @@ -233,14 +233,14 @@ void Model::get_table_name(std::wstring & str, bool with_schema_name, ModelData void Model::get_table_name(std::string & str, bool with_schema_name, ModelData * model_data, bool clear_string) { - PT::WTextStream stream; + pt::WTextStream stream; get_table_name(stream, with_schema_name, model_data, false); - PT::WideStreamToUTF8(stream, str, clear_string); + pt::WideStreamToUTF8(stream, str, clear_string); } -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, bool clear_stream, bool dump_mode) { if( clear_stream ) { @@ -278,13 +278,13 @@ void Model::to_text(PT::TextStream & stream, ModelData * model_data, bool clear_ } -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, bool clear_stream, bool dump_mode) { to_text(stream, &model_data, clear_stream, dump_mode); } -void Model::to_text(PT::TextStream & stream, bool clear_stream, bool dump_mode) +void Model::to_text(pt::TextStream & stream, bool clear_stream, bool dump_mode) { to_text(stream, nullptr, clear_stream, dump_mode); } @@ -296,7 +296,7 @@ void Model::to_text(std::string & str, ModelData * model_data, bool clear_string if( model_connector ) { // CHECK ME what if the stream is being used by something other? - PT::TextStream * out_stream = model_connector->get_stream(); + pt::TextStream * out_stream = model_connector->get_stream(); if( out_stream ) { @@ -336,7 +336,7 @@ std::string Model::to_string() -void Model::generate_insert_query(PT::TextStream & stream, ModelData * model_data) +void Model::generate_insert_query(pt::TextStream & stream, ModelData * model_data) { ModelEnv model_env_local; model_env = &model_env_local; @@ -424,7 +424,7 @@ bool Model::insert_tree(bool insert_whole_tree) DbConnector * db_connector = model_connector->get_db_connector(); // CHECK ME what if the stream is being used by something other? - PT::TextStream * out_stream = model_connector->get_stream(); + pt::TextStream * out_stream = model_connector->get_stream(); if( db_connector && out_stream ) { @@ -470,7 +470,7 @@ bool Model::insert_tree(bool insert_whole_tree) -void Model::generate_update_query(PT::TextStream & stream, ModelData * model_data) +void Model::generate_update_query(pt::TextStream & stream, ModelData * model_data) { ModelEnv model_env_local; model_env = &model_env_local; @@ -555,7 +555,7 @@ bool Model::update_tree(bool update_whole_tree) DbConnector * db_connector = model_connector->get_db_connector(); // CHECK ME what if the stream is being used by something other? - PT::TextStream * out_stream = model_connector->get_stream(); + pt::TextStream * out_stream = model_connector->get_stream(); if( db_connector && out_stream ) { @@ -581,7 +581,7 @@ bool Model::update_tree(bool update_whole_tree) } -void Model::generate_remove_query(PT::TextStream & stream, ModelData * model_data) +void Model::generate_remove_query(pt::TextStream & stream, ModelData * model_data) { ModelEnv model_env_local; model_env = &model_env_local; @@ -667,7 +667,7 @@ bool Model::remove_tree(bool remove_whole_tree) DbConnector * db_connector = model_connector->get_db_connector(); // CHECK ME what if the stream is being used by something other? - PT::TextStream * out_stream = model_connector->get_stream(); + pt::TextStream * out_stream = model_connector->get_stream(); if( db_connector && out_stream ) { @@ -793,7 +793,7 @@ bool Model::save_tree(bool save_whole_tree) } -void Model::generate_select_columns(PT::TextStream & stream) +void Model::generate_select_columns(pt::TextStream & stream) { if( model_connector && model_env ) { @@ -979,7 +979,7 @@ void Model::log_table_name(bool put_schema_name) { if( model_connector && model_env ) { - PT::Log * plog = model_connector->get_logger(); + pt::Log * plog = model_connector->get_logger(); if( plog ) { @@ -1002,7 +1002,7 @@ void Model::log_table_name_with_field(const wchar_t * db_field_name, bool put_sc { if( model_connector && model_env ) { - PT::Log * plog = model_connector->get_logger(); + pt::Log * plog = model_connector->get_logger(); if( plog ) { @@ -1030,17 +1030,17 @@ void Model::put_to_log(const wchar_t * str) { if( model_connector ) { - PT::Log * log = model_connector->get_logger(); + pt::Log * log = model_connector->get_logger(); if( log ) { - (*log) << str << PT::Log::logend; + (*log) << str << pt::Log::logend; } } } -void Model::put_fields_to_log(PT::Log & log, const wchar_t * db_field_name, const wchar_t * flat_field_name) +void Model::put_fields_to_log(pt::Log & log, const wchar_t * db_field_name, const wchar_t * flat_field_name) { bool was_db_field_put = false; bool was_flat_field_put = false; @@ -1077,12 +1077,12 @@ void Model::field_model_left_join(const wchar_t * db_field_name, Model & field_m if( model_env && field_model.model_env && model_env->finder_helper ) { model_env->finder_helper->foreign_keys.clear(); - PT::TextStream & join_tables_str = model_env->finder_helper->join_tables_str; + pt::TextStream & join_tables_str = model_env->finder_helper->join_tables_str; field_model.model_env->add_table_name_to_finder_helper(); join_tables_str << "LEFT JOIN "; - PT::TextStream * db_expression_stream = db_expression->get_text_stream(); + pt::TextStream * db_expression_stream = db_expression->get_text_stream(); int expr_work_mode = db_expression->get_work_mode(); int expr_output_type = db_expression->get_output_type(); bool expr_allow_prefix = db_expression->get_allow_to_use_prefix(); @@ -1149,7 +1149,7 @@ void Model::field_model_left_join(const wchar_t * db_field_name, Model & field_m void Model::field_model_save_key(const wchar_t * db_field_name) { DbConnector * db_connector = model_connector->get_db_connector(); - PT::Log * plog = model_connector->get_logger(); + pt::Log * plog = model_connector->get_logger(); if( db_connector ) { @@ -1165,9 +1165,9 @@ void Model::field_model_save_key(const wchar_t * db_field_name) if( model_env->field_value_helper_tab->empty() && plog ) { - (*plog) << PT::Log::log1 << "Morm: I cannot find a primary key in "; + (*plog) << pt::Log::log1 << "Morm: I cannot find a primary key in "; log_table_name(); - (*plog) << PT::Log::logend; + (*plog) << pt::Log::logend; } } } @@ -1180,7 +1180,7 @@ void Model::field_model_save_key(const wchar_t * db_field_name) void Model::field_model_set_parent_key_in_child(const wchar_t * db_field_name, Model & field_model) { DbConnector * db_connector = model_connector->get_db_connector(); - PT::Log * log = model_connector->get_logger(); + pt::Log * log = model_connector->get_logger(); if( db_connector ) { @@ -1207,19 +1207,19 @@ void Model::field_model_set_parent_key_in_child(const wchar_t * db_field_name, M { if( field_model.model_env->field_index == 0 ) { - (*log) << PT::Log::log1 << "Morm: there is no a foreign key in "; + (*log) << pt::Log::log1 << "Morm: there is no a foreign key in "; field_model.log_table_name(); (*log) << " called " << db_field_name << " pointing to "; log_table_name(); - (*log) << PT::Log::logend; + (*log) << pt::Log::logend; } else { - (*log) << PT::Log::log1 << "Morm: primary key in "; + (*log) << pt::Log::log1 << "Morm: primary key in "; log_table_name(); (*log) << " consists of " << model_env->field_index << " column(s) but foreign key in "; field_model.log_table_name(); - (*log) << " consists of " << field_model.model_env->field_index << " column(s)" << PT::Log::logend; + (*log) << " consists of " << field_model.model_env->field_index << " column(s)" << pt::Log::logend; } } @@ -1228,10 +1228,10 @@ void Model::field_model_set_parent_key_in_child(const wchar_t * db_field_name, M else if( log ) { - (*log) << PT::Log::log1 << "Morm: primary key in "; + (*log) << pt::Log::log1 << "Morm: primary key in "; log_table_name(); (*log) << " consists of incorrect number of columns, expected " << helper_tab.size() - << " column(s) but got " << model_env->field_index << PT::Log::logend; + << " column(s) but got " << model_env->field_index << pt::Log::logend; } } } @@ -1439,13 +1439,13 @@ void Model::field_model(const wchar_t * db_field_name, const wchar_t * flat_fiel } else { - PT::Log * plog = model_connector->get_logger(); + pt::Log * plog = model_connector->get_logger(); if( plog ) { - (*plog) << PT::Log::log1 << "Morm: error in "; + (*plog) << pt::Log::log1 << "Morm: error in "; log_table_name_with_field(db_field_name); - (*plog) << " field, you should set FT::is_foreign_key or FT::is_foreign_key_in_child flag for a model child object" << PT::Log::logend; + (*plog) << " field, you should set FT::is_foreign_key or FT::is_foreign_key_in_child flag for a model child object" << pt::Log::logend; } } } @@ -1543,7 +1543,7 @@ bool Model::db_query(const std::string & raw_sql) } -bool Model::db_query(const PT::TextStream & raw_sql) +bool Model::db_query(const pt::TextStream & raw_sql) { bool status = false; diff --git a/src/model.h b/src/model.h index b1c6334..f3f93e3 100644 --- a/src/model.h +++ b/src/model.h @@ -86,13 +86,13 @@ public: void set_connector(ModelConnector * connector); ModelConnector * get_connector(); - virtual void get_table_name(PT::WTextStream & stream, bool with_schema_name = true, ModelData * model_data = nullptr, bool clear_stream = true); + virtual void get_table_name(pt::WTextStream & stream, bool with_schema_name = true, ModelData * model_data = nullptr, bool clear_stream = true); 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, 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(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); @@ -101,17 +101,17 @@ public: virtual std::string to_text(); virtual std::string to_string(); - virtual void generate_insert_query(PT::TextStream & stream, ModelData * model_data = nullptr); + virtual void generate_insert_query(pt::TextStream & stream, ModelData * model_data = nullptr); virtual bool insert(ModelData * model_data, bool insert_whole_tree = true); virtual bool insert(ModelData & model_data, bool insert_whole_tree = true); virtual bool insert(bool insert_whole_tree = true); - virtual void generate_update_query(PT::TextStream & stream, ModelData * model_data = nullptr); + virtual void generate_update_query(pt::TextStream & stream, ModelData * model_data = nullptr); virtual bool update(ModelData * model_data, bool update_whole_tree = true); virtual bool update(ModelData & model_data, bool update_whole_tree = true); virtual bool update(bool update_whole_tree = true); - virtual void generate_remove_query(PT::TextStream & stream, ModelData * model_data = nullptr); + virtual void generate_remove_query(pt::TextStream & stream, ModelData * model_data = nullptr); virtual bool remove(ModelData * model_data, bool remove_whole_tree = true); virtual bool remove(ModelData & model_data, bool remove_whole_tree = true); virtual bool remove(bool remove_whole_tree = true); @@ -121,7 +121,7 @@ public: virtual bool save(bool save_whole_tree = true); - virtual void generate_select_columns(PT::TextStream & stream); + virtual void generate_select_columns(pt::TextStream & stream); // set object to default values virtual void clear(); @@ -154,13 +154,13 @@ public: if( !helper_tab.back().found && model_connector ) { - PT::Log * plog = model_connector->get_logger(); + pt::Log * plog = model_connector->get_logger(); if( plog ) { - (*plog) << PT::Log::log1 << "Morm: I cannot find such a property: "; + (*plog) << pt::Log::log1 << "Morm: I cannot find such a property: "; put_fields_to_log(*plog, db_field_name, flat_field_name); - (*plog) << PT::Log::logend; + (*plog) << pt::Log::logend; } } @@ -219,7 +219,7 @@ protected: virtual bool db_query(const char * raw_sql); virtual bool db_query(const std::string & raw_sql); - virtual bool db_query(const PT::TextStream & raw_sql); + virtual bool db_query(const pt::TextStream & raw_sql); ///////////////////////////////// /* @@ -312,12 +312,12 @@ protected: field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, PT::Date & field_value, const FT & field_type = FT::default_type) + void field(const wchar_t * field_name, pt::Date & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } - void field(const wchar_t * field_name, PT::Space & field_value, const FT & field_type = FT::default_type) + void field(const wchar_t * field_name, pt::Space & field_value, const FT & field_type = FT::default_type) { field_generic(field_name, field_name, field_value, field_type); } @@ -431,12 +431,12 @@ protected: field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, PT::Date & field_value, const FT & field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, pt::Date & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } - void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, PT::Space & field_value, const FT & field_type = FT::default_type) + void field(const wchar_t * db_field_name, const wchar_t * flat_field_name, pt::Space & field_value, const FT & field_type = FT::default_type) { field_generic(db_field_name, flat_field_name, field_value, field_type); } @@ -474,7 +474,7 @@ protected: if( model_env->field_index >= 0 && (size_t)model_env->field_index < model_env->field_value_helper_tab->size() ) { FieldValueHelper & helper = (*model_env->field_value_helper_tab)[model_env->field_index]; - PT::Log * log = model_connector->get_logger(); + pt::Log * log = model_connector->get_logger(); if( (!helper.compare_db_field_name || is_the_same_field(db_field_name, helper.db_field_name)) && (!helper.compare_flat_field_name || is_the_same_field(flat_field_name, helper.flat_field_name)) ) @@ -489,12 +489,12 @@ protected: { if( log ) { - (*log) << PT::Log::log1 << "Morm: incorrect type of a field in "; + (*log) << pt::Log::log1 << "Morm: incorrect type of a field in "; log_table_name(); (*log) << ", "; put_fields_to_log(*log, db_field_name, flat_field_name); (*log) << ", type expected " << typeid(field_value).name() - << " got " << helper.value_type_info->name() << PT::Log::logend; + << " got " << helper.value_type_info->name() << pt::Log::logend; } } } @@ -749,7 +749,7 @@ protected: { if( model_connector && model_env ) { - PT::Log * plog = model_connector->get_logger(); + pt::Log * plog = model_connector->get_logger(); if( !is_empty_field(db_field_name) ) { @@ -762,7 +762,7 @@ protected: { if( plog ) { - (*plog) << PT::Log::log1 << "Morm: error: FT::is_foreign_key is not implemented for a list/vector yet" << PT::Log::logend; + (*plog) << pt::Log::log1 << "Morm: error: FT::is_foreign_key is not implemented for a list/vector yet" << pt::Log::logend; return; } } @@ -785,9 +785,9 @@ protected: { if( plog ) { - (*plog) << PT::Log::log1 << "Morm: ignoring "; + (*plog) << pt::Log::log1 << "Morm: ignoring "; log_table_name_with_field(db_field_name); - (*plog) << " as this is not a container with Model objects" << PT::Log::logend; + (*plog) << " as this is not a container with Model objects" << pt::Log::logend; } } } @@ -795,9 +795,9 @@ protected: { if( plog ) { - (*plog) << PT::Log::log1 << "Morm: error in "; + (*plog) << pt::Log::log1 << "Morm: error in "; log_table_name_with_field(db_field_name); - (*plog) << " field, you should set FT::is_foreign_key or FT::is_foreign_key_in_child flag for a list of child objects" << PT::Log::logend; + (*plog) << " field, you should set FT::is_foreign_key or FT::is_foreign_key_in_child flag for a list of child objects" << pt::Log::logend; } } } @@ -861,7 +861,7 @@ protected: if( db_expression ) { std::wstring table_field_name; - PT::TextStream table_field_name_str; + pt::TextStream table_field_name_str; // CHECK ME not tested yet after changing to db_expression->table_with_index_and_field_to_stream() db_expression->table_with_index_and_field_to_stream(table_field_name_str, model_env->table_name, model_env->table_index, field_name, field_type); @@ -977,7 +977,7 @@ protected: virtual bool is_the_same_field(const wchar_t * field1, const wchar_t * field2); virtual void put_to_log(const wchar_t * str); - virtual void put_fields_to_log(PT::Log & log, const wchar_t * db_field_name, const wchar_t * flat_field_name); + virtual void put_fields_to_log(pt::Log & log, const wchar_t * db_field_name, const wchar_t * flat_field_name); virtual void log_table_name(bool put_schema_name = true); virtual void log_table_name_with_field(const wchar_t * db_field_name = nullptr, bool put_schema_name = true); diff --git a/src/modelconnector.cpp b/src/modelconnector.cpp index 51a6c06..9c7a2fa 100644 --- a/src/modelconnector.cpp +++ b/src/modelconnector.cpp @@ -60,17 +60,17 @@ ModelConnector::~ModelConnector() } -void ModelConnector::set_logger(PT::Log * log) +void ModelConnector::set_logger(pt::Log * log) { this->log = log; } -void ModelConnector::set_logger(PT::Log & log) +void ModelConnector::set_logger(pt::Log & log) { this->log = &log; } -PT::Log * ModelConnector::get_logger() +pt::Log * ModelConnector::get_logger() { return log; } @@ -91,7 +91,7 @@ void ModelConnector::deallocate_stream() void ModelConnector::allocate_default_stream() { deallocate_stream(); - out_stream = new PT::TextStream(); + out_stream = new pt::TextStream(); out_stream_allocated = true; } @@ -133,14 +133,14 @@ void ModelConnector::allocate_default_clearer_if_needed() } -void ModelConnector::set_stream(PT::TextStream & stream) +void ModelConnector::set_stream(pt::TextStream & stream) { deallocate_stream(); this->out_stream = &stream; } -PT::TextStream * ModelConnector::get_stream() +pt::TextStream * ModelConnector::get_stream() { allocate_default_stream_if_needed(); return out_stream; diff --git a/src/modelconnector.h b/src/modelconnector.h index f28dfc3..9e20547 100644 --- a/src/modelconnector.h +++ b/src/modelconnector.h @@ -59,13 +59,13 @@ public: ModelConnector(const ModelConnector &) = delete; virtual ~ModelConnector(); - virtual void set_logger(PT::Log * log); - virtual void set_logger(PT::Log & log); + virtual void set_logger(pt::Log * log); + virtual void set_logger(pt::Log & log); - virtual PT::Log * get_logger(); + virtual pt::Log * get_logger(); - virtual void set_stream(PT::TextStream & stream); - virtual PT::TextStream * get_stream(); + virtual void set_stream(pt::TextStream & stream); + virtual pt::TextStream * get_stream(); virtual void set_flat_connector(FlatConnector & flat_connector); virtual FlatConnector * get_flat_connector(); @@ -80,12 +80,12 @@ public: protected: - PT::Log * log; + pt::Log * log; FlatConnector * flat_connector; DbConnector * db_connector; - PT::TextStream * out_stream; // IMPROVE ME give here an interface to the base stream (implement him) + pt::TextStream * out_stream; // IMPROVE ME give here an interface to the base stream (implement him) bool out_stream_allocated; Clearer * clearer; diff --git a/src/modelenv.h b/src/modelenv.h index c68adbc..7b72405 100644 --- a/src/modelenv.h +++ b/src/modelenv.h @@ -61,8 +61,8 @@ public: int model_work_submode; bool dump_mode; - PT::WTextStream schema_name; - PT::WTextStream table_name; + pt::WTextStream schema_name; + pt::WTextStream table_name; int table_index; /* diff --git a/src/postgresqlconnector.cpp b/src/postgresqlconnector.cpp index 4a31f89..78b78b6 100644 --- a/src/postgresqlconnector.cpp +++ b/src/postgresqlconnector.cpp @@ -92,7 +92,7 @@ bool PostgreSQLConnector::do_query(const char * query_str, PostgreSQLQueryResult if( log_queries && log ) { - (*log) << PT::Log::log3 << "Morm: query: " << query_str << PT::Log::logend; + (*log) << pt::Log::log3 << "Morm: query: " << query_str << pt::Log::logend; } psql_result->psql_result = PQexec(pg_conn, query_str); @@ -119,15 +119,15 @@ bool PostgreSQLConnector::do_query(const char * query_str, PostgreSQLQueryResult if( err_msg ) { - PT::UTF8ToWide(err_msg, psql_result->error_msg); + pt::UTF8ToWide(err_msg, psql_result->error_msg); } if( log ) { - (*log) << PT::Log::log1 << "Morm: Problem with this query: \"" << query_str << '\"' << PT::Log::logend; + (*log) << pt::Log::log1 << "Morm: Problem with this query: \"" << query_str << '\"' << pt::Log::logend; if( err_msg ) - (*log) << PT::Log::log1 << "Morm: " << err_msg << PT::Log::logend; + (*log) << pt::Log::log1 << "Morm: " << err_msg << pt::Log::logend; } } else @@ -171,8 +171,8 @@ const char * PostgreSQLConnector::query_last_sequence(const wchar_t * sequence_t { if( log ) { - (*log) << PT::Log::log1 << "Morm: expected only one row in sequence result, has: " << psql_result.result_rows - << PT::Log::logend; + (*log) << pt::Log::log1 << "Morm: expected only one row in sequence result, has: " << psql_result.result_rows + << pt::Log::logend; } } } @@ -180,8 +180,8 @@ const char * PostgreSQLConnector::query_last_sequence(const wchar_t * sequence_t { if( pg_conn && log ) { - (*log) << PT::Log::log1 << "Morm: error (currval) for table: " << sequence_table_name << ", " - << PQerrorMessage(pg_conn) << PT::Log::logend; + (*log) << pt::Log::log1 << "Morm: error (currval) for table: " << sequence_table_name << ", " + << PQerrorMessage(pg_conn) << pt::Log::logend; } } } @@ -191,7 +191,7 @@ const char * PostgreSQLConnector::query_last_sequence(const wchar_t * sequence_t -bool PostgreSQLConnector::query(const PT::TextStream & stream, QueryResult & query_result) +bool PostgreSQLConnector::query(const pt::TextStream & stream, QueryResult & query_result) { stream.to_string(query_str); return query(query_str.c_str(), query_result); @@ -264,25 +264,25 @@ bool PostgreSQLConnector::query_remove(const char * query_str, QueryResult & que } -bool PostgreSQLConnector::query_select(const PT::TextStream & stream, QueryResult & query_result) +bool PostgreSQLConnector::query_select(const pt::TextStream & stream, QueryResult & query_result) { stream.to_string(query_str); return query_select(query_str.c_str(), query_result); } -bool PostgreSQLConnector::query_update(const PT::TextStream & stream, QueryResult & query_result) +bool PostgreSQLConnector::query_update(const pt::TextStream & stream, QueryResult & query_result) { stream.to_string(query_str); return query_update(query_str.c_str(), query_result); } -bool PostgreSQLConnector::query_insert(const PT::TextStream & stream, QueryResult & query_result) +bool PostgreSQLConnector::query_insert(const pt::TextStream & stream, QueryResult & query_result) { stream.to_string(query_str); return query_insert(query_str.c_str(), query_result); } -bool PostgreSQLConnector::query_remove(const PT::TextStream & stream, QueryResult & query_result) +bool PostgreSQLConnector::query_remove(const pt::TextStream & stream, QueryResult & query_result) { stream.to_string(query_str); return query_remove(query_str.c_str(), query_result); @@ -400,9 +400,9 @@ void PostgreSQLConnector::set_conn_param(const std::wstring & database_name, con } -void PostgreSQLConnector::overwrite(PT::TextStream & stream) +void PostgreSQLConnector::overwrite(pt::TextStream & stream) { - PT::TextStream::iterator i = stream.begin(); + pt::TextStream::iterator i = stream.begin(); for( ; i != stream.end() ; ++i) { @@ -455,9 +455,9 @@ void PostgreSQLConnector::log_connection_socket() { if( pg_conn && log ) { - (*log) << PT::Log::log2 << "Morm: connection to the database works fine" << PT::Log::logend; - (*log) << PT::Log::log3 << "Morm: connection socket: " << PQsocket(pg_conn) << PT::Log::logend; - (*log) << PT::Log::logsave; + (*log) << pt::Log::log2 << "Morm: connection to the database works fine" << pt::Log::logend; + (*log) << pt::Log::log3 << "Morm: connection socket: " << PQsocket(pg_conn) << pt::Log::logend; + (*log) << pt::Log::logsave; } } @@ -467,7 +467,7 @@ void PostgreSQLConnector::wait_for_connection() { if( log ) { - (*log) << PT::Log::log3 << "Morm: waiting for the db to be ready...." << PT::Log::logend << PT::Log::logsave; + (*log) << pt::Log::log3 << "Morm: waiting for the db to be ready...." << pt::Log::logend << pt::Log::logsave; } while( !assert_connection(false) ) @@ -498,7 +498,7 @@ bool was_connection = true; { if( put_log && log ) { - (*log) << PT::Log::log2 << "Morm: connection to the database is lost, trying to recover" << PT::Log::logend << PT::Log::logsave; + (*log) << pt::Log::log2 << "Morm: connection to the database is lost, trying to recover" << pt::Log::logend << pt::Log::logsave; } was_connection = false; @@ -522,7 +522,7 @@ bool was_connection = true; { if( put_log && log ) { - (*log) << PT::Log::log1 << "Morm: connection to db server cannot be established" << PT::Log::logend << PT::Log::logsave; + (*log) << pt::Log::log1 << "Morm: connection to db server cannot be established" << pt::Log::logend << pt::Log::logsave; } // if( throw_if_no_connection ) @@ -543,7 +543,7 @@ void PostgreSQLConnector::set_db_parameters() { if( log ) { - (*log) << PT::Log::log1 << "Morm: Can't set the proper client encoding" << PT::Log::logend << PT::Log::logsave; + (*log) << pt::Log::log1 << "Morm: Can't set the proper client encoding" << pt::Log::logend << pt::Log::logsave; } } } @@ -554,7 +554,7 @@ void PostgreSQLConnector::log_unsupported_bin_format() { if( log ) { - (*log) << PT::Log::log1 << "Morm: unsupported binary format (skipping)" << PT::Log::logend; + (*log) << pt::Log::log1 << "Morm: unsupported binary format (skipping)" << pt::Log::logend; } } diff --git a/src/postgresqlconnector.h b/src/postgresqlconnector.h index 0f771d1..9f21165 100644 --- a/src/postgresqlconnector.h +++ b/src/postgresqlconnector.h @@ -55,7 +55,7 @@ public: virtual ~PostgreSQLConnector(); - bool query(const PT::TextStream & stream, QueryResult & query_result); + bool query(const pt::TextStream & stream, QueryResult & query_result); bool query(const char * query_str, QueryResult & query_result); bool query(const std::string & query_str, QueryResult & query_result); @@ -64,10 +64,10 @@ public: bool query_insert(const char * query_str, QueryResult & query_result); bool query_remove(const char * query_str, QueryResult & query_result); - bool query_select(const PT::TextStream & stream, QueryResult & query_result); - bool query_update(const PT::TextStream & stream, QueryResult & query_result); - bool query_insert(const PT::TextStream & stream, QueryResult & query_result); - bool query_remove(const PT::TextStream & stream, QueryResult & query_result); + bool query_select(const pt::TextStream & stream, QueryResult & query_result); + bool query_update(const pt::TextStream & stream, QueryResult & query_result); + bool query_insert(const pt::TextStream & stream, QueryResult & query_result); + bool query_remove(const pt::TextStream & stream, QueryResult & query_result); virtual void set_conn_param(const std::wstring & database, const std::wstring & user, const std::wstring & pass); @@ -85,7 +85,7 @@ public: protected: PGconn * pg_conn; - PT::TextStream stream; + pt::TextStream stream; std::string query_str; std::wstring db_database; @@ -94,7 +94,7 @@ protected: virtual bool do_query(const char * query_str, PostgreSQLQueryResult * psql_result); virtual void allocate_default_expression(); - virtual void overwrite(PT::TextStream & stream); + virtual void overwrite(pt::TextStream & stream); virtual const char * query_last_sequence(const wchar_t * sequence_table_name); virtual QueryResult * create_query_result(); diff --git a/src/postgresqlexpression.cpp b/src/postgresqlexpression.cpp index 4aaa3f7..c9b4ccd 100644 --- a/src/postgresqlexpression.cpp +++ b/src/postgresqlexpression.cpp @@ -64,7 +64,7 @@ void PostgreSQLExpression::after_field_value_string(const FT & field_type) -void PostgreSQLExpression::esc(char val, PT::TextStream & stream, const FT & field_type) +void PostgreSQLExpression::esc(char val, pt::TextStream & stream, const FT & field_type) { if( field_type.is_hexadecimal() || field_type.is_binary() ) { @@ -86,13 +86,13 @@ void PostgreSQLExpression::esc(char val, PT::TextStream & stream, const FT & fie } -void PostgreSQLExpression::esc(const PT::Date & date, PT::TextStream & stream, const FT & field_type) +void PostgreSQLExpression::esc(const pt::Date & date, pt::TextStream & stream, const FT & field_type) { stream << date << "+00"; } -DbExpression & PostgreSQLExpression::page(PT::TextStream & stream, size_t page_number, size_t page_size) +DbExpression & PostgreSQLExpression::page(pt::TextStream & stream, size_t page_number, size_t page_size) { stream << " offset " << page_number << " limit " << page_size << " "; return *this; diff --git a/src/postgresqlexpression.h b/src/postgresqlexpression.h index 0001171..d3c8346 100644 --- a/src/postgresqlexpression.h +++ b/src/postgresqlexpression.h @@ -45,10 +45,10 @@ class PostgreSQLExpression : public DbExpression { public: - void esc(char val, PT::TextStream & stream, const FT & field_type); - void esc(const PT::Date & date, PT::TextStream & stream, const FT & field_type); + void esc(char val, pt::TextStream & stream, const FT & field_type); + void esc(const pt::Date & date, pt::TextStream & stream, const FT & field_type); - DbExpression & page(PT::TextStream & stream, size_t page_number, size_t page_size); + DbExpression & page(pt::TextStream & stream, size_t page_number, size_t page_size); protected: diff --git a/src/postgresqlqueryresult.cpp b/src/postgresqlqueryresult.cpp index 61c3209..22eeb5f 100644 --- a/src/postgresqlqueryresult.cpp +++ b/src/postgresqlqueryresult.cpp @@ -184,7 +184,7 @@ bool PostgreSQLQueryResult::is_null(int row, int col) } -void PostgreSQLQueryResult::dump_column_names(PT::Log & log) +void PostgreSQLQueryResult::dump_column_names(pt::Log & log) { if( psql_result ) { @@ -192,7 +192,7 @@ void PostgreSQLQueryResult::dump_column_names(PT::Log & log) for(int i = 0 ; i < cols ; ++i) { - log << i << ' ' << PQfname(psql_result, i) << PT::Log::logend; + log << i << ' ' << PQfname(psql_result, i) << pt::Log::logend; } } } @@ -255,20 +255,20 @@ void PostgreSQLQueryResult::dump_column_names(PT::Log & log) //} -//bool PostgreSQLConnector::AssertValueSpace(PGresult * r, int row, int col, PT::Space & space) +//bool PostgreSQLConnector::AssertValueSpace(PGresult * r, int row, int col, pt::Space & space) //{ // const char * res = AssertValue(r, row, col); // // conf_parser.SetSpace(space); // space.Clear(); // -// PT::SpaceParser::Status status = conf_parser.ParseString(res); +// pt::SpaceParser::Status status = conf_parser.ParseString(res); // -// if( status != PT::SpaceParser::ok ) +// if( status != pt::SpaceParser::ok ) // { -// log << log1 << "Morm: a problem with parsing a PT::Space"; +// log << log1 << "Morm: a problem with parsing a pt::Space"; // -// if( status == PT::SpaceParser::syntax_error ) +// if( status == pt::SpaceParser::syntax_error ) // log << ", syntax error at line: " << conf_parser.line; // // log << logend; diff --git a/src/postgresqlqueryresult.h b/src/postgresqlqueryresult.h index ca80482..fcc5758 100644 --- a/src/postgresqlqueryresult.h +++ b/src/postgresqlqueryresult.h @@ -68,7 +68,7 @@ struct PostgreSQLQueryResult : public QueryResult bool is_null(int row, int col); - void dump_column_names(PT::Log & log); + void dump_column_names(pt::Log & log); }; diff --git a/src/queryresult.cpp b/src/queryresult.cpp index cdac798..a5e19de 100644 --- a/src/queryresult.cpp +++ b/src/queryresult.cpp @@ -85,7 +85,7 @@ const char * QueryResult::get_field_string_value(const char * column_name) const char * QueryResult::get_field_string_value(const wchar_t * column_name) { - PT::WideToUTF8(column_name, temp_column_name); + pt::WideToUTF8(column_name, temp_column_name); return get_field_string_value(temp_column_name.c_str()); } @@ -98,7 +98,7 @@ int QueryResult::get_column_index(const char * column_name) int QueryResult::get_column_index(const wchar_t * column_name) { - PT::WideToUTF8(column_name, temp_column_name); + pt::WideToUTF8(column_name, temp_column_name); return get_column_index(temp_column_name.c_str()); } @@ -125,7 +125,7 @@ bool QueryResult::is_null(int row, int col) } -void QueryResult::dump_column_names(PT::Log & log) +void QueryResult::dump_column_names(pt::Log & log) { } diff --git a/src/queryresult.h b/src/queryresult.h index f18e032..fdabfb5 100644 --- a/src/queryresult.h +++ b/src/queryresult.h @@ -76,7 +76,7 @@ struct QueryResult virtual int get_value_length(int row, int col); virtual bool is_null(int row, int col); - virtual void dump_column_names(PT::Log & log); + virtual void dump_column_names(pt::Log & log); }; From 0ff900f62691cc56a325cb98ebb9da25c8a7f196 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Fri, 21 May 2021 00:32:29 +0200 Subject: [PATCH 23/27] updated to the new pikotools api: changed utf8 functions PascalCase to snake_case --- src/baseexpression.cpp | 4 ++-- src/dbconnector.cpp | 6 +++--- src/model.cpp | 2 +- src/postgresqlconnector.cpp | 2 +- src/queryresult.cpp | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index d93a4f9..ede7e5c 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -407,7 +407,7 @@ void BaseExpression::esc(wchar_t val, pt::TextStream & stream, const FT & field_ { char utf8_buf[10]; - size_t utf8_len = pt::IntToUTF8((int)val, utf8_buf, sizeof(utf8_buf)); + size_t utf8_len = pt::int_to_utf8((int)val, utf8_buf, sizeof(utf8_buf)); for(size_t a = 0 ; a < utf8_len ; ++a) { @@ -429,7 +429,7 @@ void BaseExpression::esc(const wchar_t * val, bool has_known_length, size_t len, for(size_t i = 0 ; has_known_length ? (i < len) : val[i] != 0 ; ++i) { - size_t utf8_len = pt::IntToUTF8((int)val[i], utf8_buf, sizeof(utf8_buf)); + size_t utf8_len = pt::int_to_utf8((int)val[i], utf8_buf, sizeof(utf8_buf)); for(size_t a = 0 ; a < utf8_len ; ++a) { diff --git a/src/dbconnector.cpp b/src/dbconnector.cpp index b39f8be..f43669c 100644 --- a/src/dbconnector.cpp +++ b/src/dbconnector.cpp @@ -443,7 +443,7 @@ size_t DbConnector::unescape_hex_char(const char * value_str, wchar_t & field_va int value_int; bool is_correct; - len = pt::UTF8ToInt(utf8_str, utf8_str_len, value_int, is_correct); + len = pt::utf8_to_int(utf8_str, utf8_str_len, value_int, is_correct); len = len * 2; if( is_correct ) @@ -548,7 +548,7 @@ void DbConnector::get_value(const char * value_str, wchar_t & field_value, const int value_int; bool is_correct; - pt::UTF8ToInt(value_str, value_int, is_correct); + pt::utf8_to_int(value_str, value_int, is_correct); if( is_correct ) { @@ -584,7 +584,7 @@ void DbConnector::get_value(const char * value_str, std::wstring & field_value, { if( field_type.use_utf8() ) { - pt::UTF8ToWide(value_str, field_value); + pt::utf8_to_wide(value_str, field_value); } else { diff --git a/src/model.cpp b/src/model.cpp index c44bbac..1739b89 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -236,7 +236,7 @@ void Model::get_table_name(std::string & str, bool with_schema_name, ModelData * pt::WTextStream stream; get_table_name(stream, with_schema_name, model_data, false); - pt::WideStreamToUTF8(stream, str, clear_string); + pt::wide_stream_to_utf8(stream, str, clear_string); } diff --git a/src/postgresqlconnector.cpp b/src/postgresqlconnector.cpp index 78b78b6..a9fec7d 100644 --- a/src/postgresqlconnector.cpp +++ b/src/postgresqlconnector.cpp @@ -119,7 +119,7 @@ bool PostgreSQLConnector::do_query(const char * query_str, PostgreSQLQueryResult if( err_msg ) { - pt::UTF8ToWide(err_msg, psql_result->error_msg); + pt::utf8_to_wide(err_msg, psql_result->error_msg); } if( log ) diff --git a/src/queryresult.cpp b/src/queryresult.cpp index a5e19de..ff276ef 100644 --- a/src/queryresult.cpp +++ b/src/queryresult.cpp @@ -85,7 +85,7 @@ const char * QueryResult::get_field_string_value(const char * column_name) const char * QueryResult::get_field_string_value(const wchar_t * column_name) { - pt::WideToUTF8(column_name, temp_column_name); + pt::wide_to_utf8(column_name, temp_column_name); return get_field_string_value(temp_column_name.c_str()); } @@ -98,7 +98,7 @@ int QueryResult::get_column_index(const char * column_name) int QueryResult::get_column_index(const wchar_t * column_name) { - pt::WideToUTF8(column_name, temp_column_name); + pt::wide_to_utf8(column_name, temp_column_name); return get_column_index(temp_column_name.c_str()); } From 9598cc4defe839915f2bd5e59c9a285309a6729d Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Fri, 21 May 2021 01:35:16 +0200 Subject: [PATCH 24/27] updated to the new pikotools api: SpaceParser::SetSpace(...) methods have been removed --- src/dbconnector.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/dbconnector.cpp b/src/dbconnector.cpp index f43669c..3b00001 100644 --- a/src/dbconnector.cpp +++ b/src/dbconnector.cpp @@ -715,9 +715,8 @@ void DbConnector::get_value(const char * value_str, pt::Space & field_value, con if( *value_str != '\0' ) { pt::SpaceParser space_parser; - space_parser.SetSpace(field_value); - if( space_parser.ParseSpace(value_str) != pt::SpaceParser::ok ) + if( space_parser.ParseSpace(value_str, field_value) != pt::SpaceParser::ok ) { field_value.clear(); From fcd2c4775b484f7fba01b88fedf515660242cd0b Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Fri, 21 May 2021 04:51:12 +0200 Subject: [PATCH 25/27] updated to the new pikotools api: snake_case names of methods from SpaceParser --- src/dbconnector.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dbconnector.cpp b/src/dbconnector.cpp index 3b00001..86a292c 100644 --- a/src/dbconnector.cpp +++ b/src/dbconnector.cpp @@ -716,7 +716,7 @@ void DbConnector::get_value(const char * value_str, pt::Space & field_value, con { pt::SpaceParser space_parser; - if( space_parser.ParseSpace(value_str, field_value) != pt::SpaceParser::ok ) + if( space_parser.parse_space(value_str, field_value) != pt::SpaceParser::ok ) { field_value.clear(); From e0e3465673d0048cbcc169292603a0d84a94c1f3 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Fri, 21 May 2021 22:12:10 +0200 Subject: [PATCH 26/27] fixed: such field types: no_insertable, no_updatable were not taken into account on Model child objects added: field type: no_removable - it is used only with child Models objects --- src/ft.h | 15 +++++++++------ src/model.cpp | 22 +++++++++++++++------- src/model.h | 2 +- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/ft.h b/src/ft.h index 0344af1..deacf1f 100644 --- a/src/ft.h +++ b/src/ft.h @@ -56,10 +56,11 @@ public: no_insertable = 8, no_updatable = 16, no_fetchable = 32, /* not supported yet */ - raw_field_name = 64, - dont_use_utf8 = 128, - hexadecimal = 256, - binary = 512, + no_removable = 64, + raw_field_name = 128, + dont_use_utf8 = 256, + hexadecimal = 512, + binary = 1024, }; /* @@ -122,18 +123,20 @@ public: return !is_flag_set(no_insertable); } - bool is_updatable() const { return !is_flag_set(no_updatable); } - bool is_fetchable() const { return !is_flag_set(no_fetchable); } + bool is_removable() const + { + return !is_flag_set(no_removable); + } bool is_raw_field_name() const { diff --git a/src/model.cpp b/src/model.cpp index 1739b89..8ba5ab5 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -1258,26 +1258,34 @@ void Model::field_model_set_parent_key(const wchar_t * db_field_name, Model & fi } -void Model::field_model_iterate_through_childs(const wchar_t * db_field_name, Model & field_model) +void Model::field_model_iterate_through_childs(const wchar_t * db_field_name, Model & field_model, const FT & field_type) { if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_INSERT ) { - field_model.insert_tree(true); + if( field_type.is_insertable() ) + field_model.insert_tree(true); } if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_UPDATE ) { - field_model.update_tree(true); + if( field_type.is_updatable() ) + field_model.update_tree(true); } if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_REMOVE ) { - field_model.remove_tree(true); + if( field_type.is_removable() ) + field_model.remove_tree(true); } if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_SAVE ) { - field_model.save_tree(true); + if( (field_model.save_mode == Model::DO_INSERT_ON_SAVE && field_type.is_insertable()) || + (field_model.save_mode == Model::DO_UPDATE_ON_SAVE && field_type.is_updatable()) || + (field_model.save_mode == Model::DO_DELETE_ON_SAVE && field_type.is_removable()) ) + { + field_model.save_tree(true); + } } } @@ -1483,7 +1491,7 @@ void Model::field_model_for_db(const wchar_t * db_field_name, Model & field_mode { if( field_type.is_foreign_key() ) { - field_model_iterate_through_childs(db_field_name, field_model); + field_model_iterate_through_childs(db_field_name, field_model, field_type); } } @@ -1491,7 +1499,7 @@ void Model::field_model_for_db(const wchar_t * db_field_name, Model & field_mode { if( field_type.is_foreign_key_in_child() ) { - field_model_iterate_through_childs(db_field_name, field_model); + field_model_iterate_through_childs(db_field_name, field_model, field_type); } } diff --git a/src/model.h b/src/model.h index f3f93e3..c7596b9 100644 --- a/src/model.h +++ b/src/model.h @@ -643,7 +643,7 @@ protected: void field_model_save_key(const wchar_t * db_field_name); void field_model_set_parent_key_in_child(const wchar_t * db_field_name, Model & field_model); void field_model_set_parent_key(const wchar_t * db_field_name, Model & field_model); - void field_model_iterate_through_childs(const wchar_t * db_field_name, Model & field_model); + void field_model_iterate_through_childs(const wchar_t * db_field_name, Model & field_model, const FT & field_type); void field_model_generate_flat_string(const wchar_t * flat_field_name, Model & field_model, const FT & field_type); void field_model_generate_db_sql(const wchar_t * db_field_name, Model & field_model, const FT & field_type); void field_model_clear_values(Model & field_model); From 2f72bd29ec4733ff5ec92cd6a012bdff0c861ae2 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Fri, 21 May 2021 22:36:04 +0200 Subject: [PATCH 27/27] fixed: has_primary_key_set was always set to false in Model::insert() and this prevented to insert a model which had a primary key set by hand --- src/model.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/model.cpp b/src/model.cpp index 8ba5ab5..56bb39b 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -408,8 +408,7 @@ bool Model::insert(ModelData * model_data, bool insert_whole_tree) bool Model::insert_tree(bool insert_whole_tree) { bool result = false; - has_primary_key_set = false; // the key will be overwritten (the database will create a new key) - model_env->has_primary_key_set = false; + model_env->has_primary_key_set = has_primary_key_set; if( insert_whole_tree ) {