From 057d879b4c4cea1a1a511671b50627fbda74c4ae Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Fri, 13 Sep 2019 18:17:02 +0000 Subject: [PATCH] added: to Model::to_text() methods added dump_mode (bool) parameter an additional field (model_save_mode) is printed changed: if there is no an object the Cursor returns a Model with DO_NOTHING_ON_SAVE flag now (DO_INSERT_ON_SAVE was beforehand) fixed: when reading a result from db: for auto generated select there should be used get_value_by_field_index() method instead of get_value_by_field_name() changed: flat string (json) is generated only if a model has DO_INSERT_ON_SAVE or DO_UPDATE_ON_SAVE or is dump_mode turn on git-svn-id: svn://ttmath.org/publicrep/morm/trunk@1208 e52654a7-88a9-db11-a3e9-0013d4bc506e --- samples/attachment.h | 2 +- samples/person.h | 4 ++-- samples/sample01.h | 17 +++++++++++++---- src/baseexpression.cpp | 10 ++++++++++ src/baseexpression.h | 1 + src/cursor.h | 1 + src/model.cpp | 23 ++++++++++++----------- src/model.h | 42 +++++++++++++++++++++++++++++++----------- src/modelenv.h | 12 ++++++++++-- 9 files changed, 81 insertions(+), 31 deletions(-) diff --git a/samples/attachment.h b/samples/attachment.h index c16eefa..0b52547 100644 --- a/samples/attachment.h +++ b/samples/attachment.h @@ -76,10 +76,10 @@ public: field(L"id", id, false, false, true); field(L"person_id", person_id); field(L"name", name); - field(L"language_id", language); field(L"content", content); field(L"some_flags", some_flags); field(L"created_date", created_date); + field(L"language_id", L"language", language); } diff --git a/samples/person.h b/samples/person.h index 773e7ca..6261791 100644 --- a/samples/person.h +++ b/samples/person.h @@ -79,9 +79,9 @@ public: field(L"first_name", first_name); field(L"last_name", last_name); field(L"email", email); - field(L"language_id", language); + field(L"language_id", L"language", language); - field(L"person_id", attachment, true, true, false); + field(L"person_id", L"attachment", attachment, true, true, false); //field(L"person_id", attachment, f::insertable | f::updatable | f::foreign_key); //field(L"person_id", attachment, f::insertable, f::updatable, f::foreign_key); } diff --git a/samples/sample01.h b/samples/sample01.h index 2926bb7..9f044f0 100644 --- a/samples/sample01.h +++ b/samples/sample01.h @@ -52,16 +52,25 @@ public: void make() { person.set_connector(model_connector); - load_defaults(person); + //load_defaults(person); - std::wstring sss = L"cosik"; - person.set_field_value_generic(L"email", L"email", sss); + //std::wstring sss = L"cosik"; + //person.set_field_value_generic(L"email", L"email", sss); - person.insert(); + //person.insert(); //person.update(); //person.save(); //person.remove(); + + morm::Finder finder(model_connector); + + Person p = finder.select().where().eq(L"id", 110).get(); + + std::string str; + p.to_text(str, true, true); + std::cout << str << std::endl; + } diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index a108b6b..c1cc228 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -106,12 +106,22 @@ void BaseExpression::generate_from_model(Model & model) if( out_stream ) { before_generate_from_model(); + dump_additional_info(model); model.map_fields(); after_generate_from_model(); } } +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); + } +} + + /* void BaseExpression::put_field_doc(void * pointer) { diff --git a/src/baseexpression.h b/src/baseexpression.h index 5f33058..55cf2dc 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -288,6 +288,7 @@ protected: virtual void save_foreign_key(const wchar_t * field_name, ModelEnv * model_env); + virtual void dump_additional_info(Model & model); template void put_field_value(const FieldValue & field_value) diff --git a/src/cursor.h b/src/cursor.h index e167330..a737f51 100644 --- a/src/cursor.h +++ b/src/cursor.h @@ -160,6 +160,7 @@ public: bool res = false; result.set_connector(model_connector); result.clear(); + result.save_mode = Model::DO_NOTHING_ON_SAVE; if( model_connector && query_result && query_result->has_db_result() ) { diff --git a/src/model.cpp b/src/model.cpp index 259dffb..a548746 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -124,7 +124,7 @@ bool Model::found() } -void Model::to_text(PT::TextStream & stream, ModelData * model_data, bool clear_stream) +void Model::to_text(PT::TextStream & stream, ModelData * model_data, bool clear_stream, bool dump_mode) { if( clear_stream ) { @@ -135,6 +135,7 @@ void Model::to_text(PT::TextStream & stream, ModelData * model_data, bool clear_ model_env = &model_env_local; model_env->model_connector_mode = MORM_MODEL_CONNECTOR_MODE_GENERATING_FLAT_STRING; + model_env->dump_mode = dump_mode; if( model_connector ) { @@ -153,20 +154,20 @@ 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) +void Model::to_text(PT::TextStream & stream, ModelData & model_data, bool clear_stream, bool dump_mode) { - to_text(stream, &model_data, clear_stream); + to_text(stream, &model_data, clear_stream, dump_mode); } -void Model::to_text(PT::TextStream & stream, bool clear_stream) +void Model::to_text(PT::TextStream & stream, bool clear_stream, bool dump_mode) { - to_text(stream, nullptr, clear_stream); + to_text(stream, nullptr, clear_stream, dump_mode); } -void Model::to_text(std::string & str, ModelData * model_data, bool clear_string) +void Model::to_text(std::string & str, ModelData * model_data, bool clear_string, bool dump_mode) { if( model_connector ) { @@ -175,22 +176,22 @@ void Model::to_text(std::string & str, ModelData * model_data, bool clear_string if( out_stream ) { - to_text(*out_stream, model_data, true); + to_text(*out_stream, model_data, true, dump_mode); out_stream->to_string(str, clear_string); } } } -void Model::to_text(std::string & str, ModelData & model_data, bool clear_string) +void Model::to_text(std::string & str, ModelData & model_data, bool clear_string, bool dump_mode) { - to_text(str, &model_data, clear_string); + to_text(str, &model_data, clear_string, dump_mode); } -void Model::to_text(std::string & str, bool clear_string) +void Model::to_text(std::string & str, bool clear_string, bool dump_mode) { - to_text(str, nullptr, clear_string); + to_text(str, nullptr, clear_string, dump_mode); } diff --git a/src/model.h b/src/model.h index 976db3f..9d41602 100644 --- a/src/model.h +++ b/src/model.h @@ -58,7 +58,7 @@ public: enum SaveMode { - DO_INSERT_ON_SAVE, + DO_INSERT_ON_SAVE = 0, DO_UPDATE_ON_SAVE, DO_DELETE_ON_SAVE, DO_NOTHING_ON_SAVE, @@ -99,13 +99,13 @@ public: */ virtual void table_name(PT::TextStream & stream); - virtual void to_text(PT::TextStream & stream, ModelData * model_data, bool clear_stream = true); - virtual void to_text(PT::TextStream & stream, ModelData & model_data, bool clear_stream = true); - virtual void to_text(PT::TextStream & stream, bool clear_stream = 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(std::string & str, ModelData * model_data, bool clear_string = true); - virtual void to_text(std::string & str, ModelData & model_data, bool clear_string = true); - virtual void to_text(std::string & str, bool clear_string = true); + 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); + virtual void to_text(std::string & str, bool clear_string = true, bool dump_mode = false); virtual std::string to_text(); virtual std::string to_string(); @@ -775,7 +775,7 @@ protected: { if( !is_empty_field(db_field_name) ) { - if( model_env->model_data && model_env->cursor_helper && model_env->cursor_helper->has_autogenerated_select ) + 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); model_env->cursor_helper->current_column += 1; @@ -853,7 +853,9 @@ protected: } else { - map_fields(); + 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; model_env->finder_helper->join_tables_str << " ON " << model_env->table_name_short << '.'; @@ -885,8 +887,11 @@ protected: if( flat_expression && !is_empty_field(flat_field_name) ) { - field_model.model_env->model_connector_mode = MORM_MODEL_CONNECTOR_MODE_GENERATING_FLAT_STRING; - flat_expression->field_model(flat_field_name, field_model, insertable, updatable, false, model_env); + 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_connector_mode = MORM_MODEL_CONNECTOR_MODE_GENERATING_FLAT_STRING; + flat_expression->field_model(flat_field_name, field_model, insertable, updatable, false, model_env); + } } } } @@ -1268,6 +1273,21 @@ protected: { if( model_env->cursor_helper && model_env->cursor_helper->query_result ) { +// std::wstring table_field_name; + + // CHECK what about escaping field names here? + + /* + * FIX ME + * we need the table name with correct prefix (index) + * + */ +// model_env->table_name.to_string(table_field_name); +// +// +// table_field_name += '.'; +// table_field_name += field_name; + const char * val_str = model_env->cursor_helper->query_result->get_field_string_value(field_name); if( val_str ) diff --git a/src/modelenv.h b/src/modelenv.h index d246689..160f9d3 100644 --- a/src/modelenv.h +++ b/src/modelenv.h @@ -50,14 +50,19 @@ class ModelEnv { public: + // global objects to the whole models tree + // (values are copied to child models) ModelData * model_data; FinderHelper * finder_helper; CursorHelper * cursor_helper; - int model_connector_mode; + bool dump_mode; + + // local objects + // (for only one model) const void * doc_field_pointer; - PT::TextStream table_name; + PT::TextStream table_name; // CHECK ME may it should be PT::WTextStream? PT::TextStream table_name_short; int table_index; int field_index; @@ -83,6 +88,7 @@ public: finder_helper = e.finder_helper; cursor_helper = e.cursor_helper; model_connector_mode = e.model_connector_mode; + dump_mode = e.dump_mode; table_index = e.table_index; doc_field_pointer = e.doc_field_pointer; set_field_name_helper = e.set_field_name_helper; @@ -100,6 +106,7 @@ public: cursor_helper = e.cursor_helper; model_connector_mode = e.model_connector_mode; + dump_mode = e.dump_mode; // what about doc_field_pointer? } @@ -110,6 +117,7 @@ public: finder_helper = nullptr; cursor_helper = nullptr; model_connector_mode = MORM_MODEL_CONNECTOR_MODE_NONE; + dump_mode = false; doc_field_pointer = nullptr; table_name.clear(); table_name_short.clear();