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);
This commit is contained in:
parent
fcf1d28b18
commit
f7490594ad
|
@ -27,11 +27,12 @@ current_path := $(shell pwd)
|
||||||
global_relative_working_dir := $(shell relative_path $(current_path) $(GLOBAL_WORKING_DIR))
|
global_relative_working_dir := $(shell relative_path $(current_path) $(GLOBAL_WORKING_DIR))
|
||||||
|
|
||||||
|
|
||||||
|
# IMPROVE ME
|
||||||
|
# add dependency to pikotools
|
||||||
all: morm $(name)
|
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
|
$(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
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -87,10 +87,9 @@ public:
|
||||||
field(L"language_id", L"language", language, FT::foreign_key);
|
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
|
table(L"public", L"attachment");
|
||||||
stream << "public.attachment";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void after_select()
|
void after_select()
|
||||||
|
|
|
@ -78,7 +78,7 @@ public:
|
||||||
void map_fields()
|
void map_fields()
|
||||||
{
|
{
|
||||||
field(L"id", id, FT::no_insertable | FT::no_updatable | FT::primary_key);
|
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"name", name);
|
||||||
field(L"content", content);
|
field(L"content", content);
|
||||||
field(L"attachment_id", L"types", types, FT::foreign_key_in_child);
|
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);
|
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
|
table(L"public", L"attachment2");
|
||||||
stream << "public.attachment2";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void after_select()
|
void after_select()
|
||||||
|
|
|
@ -77,11 +77,9 @@ public:
|
||||||
field(L"code_int", code_int);
|
field(L"code_int", code_int);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void prepare_table()
|
||||||
void table_name(PT::TextStream & stream)
|
|
||||||
{
|
{
|
||||||
// schema.table_name or just table_name
|
table(L"public", L"language");
|
||||||
stream << "public.language";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void after_insert()
|
void after_insert()
|
||||||
|
|
|
@ -87,10 +87,9 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void table_name(PT::TextStream & stream)
|
void prepare_table()
|
||||||
{
|
{
|
||||||
// schema.table_name or just table_name
|
table(L"public", L"person");
|
||||||
stream << "public.person";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -32,6 +32,9 @@
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifndef headerfile_morm_samples_sample01
|
||||||
|
#define headerfile_morm_samples_sample01
|
||||||
|
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include "basesample.h"
|
#include "basesample.h"
|
||||||
#include "person.h"
|
#include "person.h"
|
||||||
|
@ -62,7 +65,7 @@ void make()
|
||||||
person.set_connector(model_connector);
|
person.set_connector(model_connector);
|
||||||
load_defaults(person);
|
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);
|
//person.set_field_value_generic(L"email", L"email", sss);
|
||||||
|
|
||||||
|
|
||||||
|
@ -75,7 +78,7 @@ void make()
|
||||||
|
|
||||||
|
|
||||||
morm::Finder<Person> finder(model_connector);
|
morm::Finder<Person> 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);
|
p.to_text(str, true, true);
|
||||||
|
|
||||||
|
|
||||||
|
@ -223,3 +226,5 @@ private:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -70,11 +70,9 @@ public:
|
||||||
field(L"name", name);
|
field(L"name", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void prepare_table()
|
||||||
void table_name(PT::TextStream & stream)
|
|
||||||
{
|
{
|
||||||
// schema.table_name or just table_name
|
table(L"public", L"types");
|
||||||
stream << "public.types";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void after_insert()
|
void after_insert()
|
||||||
|
|
|
@ -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)
|
void BaseExpression::put_field_name(const wchar_t * field_name, ModelEnv * model_env)
|
||||||
{
|
{
|
||||||
if( is_long_field_name(field_name) )
|
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 )
|
if( use_prefix && model_env )
|
||||||
{
|
{
|
||||||
before_first_part_long_field_name();
|
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 )
|
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)
|
void BaseExpression::put_type(char val, PT::TextStream & stream)
|
||||||
{
|
{
|
||||||
|
|
|
@ -250,14 +250,11 @@ public:
|
||||||
|
|
||||||
virtual void esc(const PT::Date & date, 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::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 wchar_t * field_name);
|
||||||
virtual bool is_long_field_name(const PT::TextStream & table_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:
|
protected:
|
||||||
|
|
||||||
|
|
|
@ -193,7 +193,7 @@ public:
|
||||||
|
|
||||||
if( !cursor_helper.has_autogenerated_select && cursor_helper.use_table_prefix_for_fetching_values )
|
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();
|
result.before_select();
|
||||||
|
@ -369,7 +369,7 @@ protected:
|
||||||
|
|
||||||
if( !cursor_helper.has_autogenerated_select && cursor_helper.use_table_prefix_for_fetching_values )
|
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();
|
added_model.before_select();
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2019, Tomasz Sowa
|
* Copyright (c) 2018-2021, Tomasz Sowa
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* 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->clear();
|
||||||
db_expression->allow_to_use_prefix(false);
|
db_expression->allow_to_use_prefix(false);
|
||||||
|
|
||||||
stream << "insert into ";
|
// IMPROVEME escape table_name
|
||||||
model.table_name(stream);
|
stream << "insert into " << model.get_table_name();
|
||||||
|
|
||||||
stream << " (";
|
stream << " (";
|
||||||
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS);
|
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->clear();
|
||||||
db_expression->allow_to_use_prefix(false);
|
db_expression->allow_to_use_prefix(false);
|
||||||
|
|
||||||
stream << "update ";
|
// IMPROVEME escape table_name
|
||||||
model.table_name(stream);
|
stream << "update " << model.get_table_name();
|
||||||
|
|
||||||
stream << " set ";
|
stream << " set ";
|
||||||
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS_VALUES);
|
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->clear();
|
||||||
db_expression->allow_to_use_prefix(false);
|
db_expression->allow_to_use_prefix(false);
|
||||||
|
|
||||||
stream << "delete from ";
|
// IMPROVEME escape table_name
|
||||||
model.table_name(stream);
|
stream << "delete from " << model.get_table_name();
|
||||||
|
|
||||||
stream << " where ";
|
stream << " where ";
|
||||||
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS_VALUES);
|
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_FIELDS_VALUES);
|
||||||
db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_PRIMARY_KEY);
|
db_expression->set_output_type(MORM_OUTPUT_TYPE_DB_PRIMARY_KEY);
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,8 +77,6 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
virtual void prepare_short_table_name(const PT::TextStream & table_name, PT::TextStream & short_table_name);
|
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
|
|
@ -227,17 +227,17 @@ public:
|
||||||
model.model_env = &model_env;
|
model.model_env = &model_env;
|
||||||
model.model_env->model_data = model_data;
|
model.model_env->model_data = model_data;
|
||||||
model.model_env->finder_helper = &finder_helper;
|
model.model_env->finder_helper = &finder_helper;
|
||||||
|
model.prepare_table();
|
||||||
|
model.model_env->add_table_name_to_finder_helper();
|
||||||
|
|
||||||
has_autogenerated_select = true;
|
has_autogenerated_select = true;
|
||||||
|
|
||||||
if( model_connector && out_stream && db_expression )
|
if( model_connector && out_stream && db_expression )
|
||||||
{
|
{
|
||||||
model.prepare_table_names();
|
|
||||||
|
|
||||||
(*out_stream) << "SELECT ";
|
(*out_stream) << "SELECT ";
|
||||||
model.generate_select_columns(*out_stream);
|
model.generate_select_columns(*out_stream);
|
||||||
(*out_stream) << " FROM " << model.model_env->table_name << " AS ";
|
(*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) << " ";
|
||||||
(*out_stream) << finder_helper.join_tables_str;
|
(*out_stream) << finder_helper.join_tables_str;
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ public:
|
||||||
|
|
||||||
PT::TextStream join_tables_str;
|
PT::TextStream join_tables_str;
|
||||||
|
|
||||||
std::map<std::string, int> join_tables_map;
|
std::map<std::wstring, int> join_tables_map;
|
||||||
|
|
||||||
std::list<std::string> foreign_keys;
|
std::list<std::string> 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);
|
table_name.to_string(table_name_str);
|
||||||
|
|
||||||
return add_join_table(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));
|
auto res = join_tables_map.insert(std::make_pair(table_name, 1));
|
||||||
|
|
||||||
|
|
149
src/model.cpp
149
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)
|
void Model::set_connector(ModelConnector & connector)
|
||||||
{
|
{
|
||||||
set_connector(&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()
|
bool Model::object_exists()
|
||||||
{
|
{
|
||||||
return save_mode == DO_UPDATE_ON_SAVE;
|
return save_mode == DO_UPDATE_ON_SAVE;
|
||||||
|
@ -168,6 +200,7 @@ void Model::to_text(PT::TextStream & stream, ModelData * model_data, bool clear_
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
prepare_table();
|
||||||
flat_connector->to_text(stream, *this);
|
flat_connector->to_text(stream, *this);
|
||||||
}
|
}
|
||||||
catch(...)
|
catch(...)
|
||||||
|
@ -255,6 +288,7 @@ void Model::generate_insert_query(PT::TextStream & stream, ModelData * model_dat
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
prepare_table();
|
||||||
db_connector->generate_insert_query(stream, *this);
|
db_connector->generate_insert_query(stream, *this);
|
||||||
}
|
}
|
||||||
catch(...)
|
catch(...)
|
||||||
|
@ -291,6 +325,7 @@ bool Model::insert(ModelData * model_data, bool insert_whole_tree)
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
prepare_table();
|
||||||
status = insert_tree(insert_whole_tree);
|
status = insert_tree(insert_whole_tree);
|
||||||
}
|
}
|
||||||
catch(...)
|
catch(...)
|
||||||
|
@ -385,6 +420,7 @@ void Model::generate_update_query(PT::TextStream & stream, ModelData * model_dat
|
||||||
|
|
||||||
if( db_connector )
|
if( db_connector )
|
||||||
{
|
{
|
||||||
|
prepare_table();
|
||||||
db_connector->generate_update_query(stream, *this);
|
db_connector->generate_update_query(stream, *this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -416,6 +452,7 @@ bool Model::update(ModelData * model_data, bool update_whole_tree)
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
prepare_table();
|
||||||
status = update_tree(update_whole_tree);
|
status = update_tree(update_whole_tree);
|
||||||
}
|
}
|
||||||
catch(...)
|
catch(...)
|
||||||
|
@ -494,6 +531,7 @@ void Model::generate_remove_query(PT::TextStream & stream, ModelData * model_dat
|
||||||
|
|
||||||
if( db_connector )
|
if( db_connector )
|
||||||
{
|
{
|
||||||
|
prepare_table();
|
||||||
db_connector->generate_remove_query(stream, *this);
|
db_connector->generate_remove_query(stream, *this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -526,6 +564,7 @@ bool Model::remove(ModelData * model_data, bool remove_whole_tree)
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
prepare_table();
|
||||||
status = remove_tree(remove_whole_tree);
|
status = remove_tree(remove_whole_tree);
|
||||||
}
|
}
|
||||||
catch(...)
|
catch(...)
|
||||||
|
@ -621,6 +660,7 @@ bool Model::save(ModelData * model_data, bool save_whole_tree)
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
prepare_table();
|
||||||
status = save_tree(save_whole_tree);
|
status = save_tree(save_whole_tree);
|
||||||
}
|
}
|
||||||
catch(...)
|
catch(...)
|
||||||
|
@ -741,6 +781,7 @@ void Model::clear()
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// prepare_table() doesn't have to be called
|
||||||
map_fields();
|
map_fields();
|
||||||
}
|
}
|
||||||
catch(...)
|
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)
|
void Model::put_table_name_with_index(PT::TextStream & str)
|
||||||
{
|
{
|
||||||
if( model_env )
|
if( model_env )
|
||||||
{
|
{
|
||||||
str << model_env->table_name_short;
|
str << model_env->table_name;
|
||||||
|
|
||||||
if( model_env->table_index > 1 )
|
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)
|
void Model::put_to_log(const wchar_t * str)
|
||||||
{
|
{
|
||||||
if( model_connector )
|
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
|
} // namespace
|
||||||
|
|
67
src/model.h
67
src/model.h
|
@ -88,19 +88,16 @@ public:
|
||||||
/*
|
/*
|
||||||
* map fields to names
|
* map fields to names
|
||||||
*
|
*
|
||||||
*
|
* IMPROVEME rename me to fields() and make protected
|
||||||
*/
|
*/
|
||||||
virtual void map_fields() = 0;
|
virtual void map_fields() = 0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*
|
* IMPROVEME make me protected
|
||||||
* we can use the object for a different purpose than database (e.g. json)
|
|
||||||
* so let the table_name be non pure-virtual
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
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);
|
||||||
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_env_local;
|
||||||
model_env->model_work_mode = MORM_MODEL_WORK_MODE_SET_FIELD_VALUE;
|
model_env->model_work_mode = MORM_MODEL_WORK_MODE_SET_FIELD_VALUE;
|
||||||
model_env->field_index = 0;
|
model_env->field_index = 0;
|
||||||
|
prepare_table(); // CHECK ME it is needed to set table name?
|
||||||
|
|
||||||
FieldValueHelper field_value_helper;
|
FieldValueHelper field_value_helper;
|
||||||
field_value_helper.db_field_name = db_field_name;
|
field_value_helper.db_field_name = db_field_name;
|
||||||
|
@ -166,7 +164,7 @@ public:
|
||||||
|
|
||||||
if( plog )
|
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);
|
put_fields_to_log(*plog, db_field_name, flat_field_name);
|
||||||
(*plog) << PT::Log::logend;
|
(*plog) << PT::Log::logend;
|
||||||
}
|
}
|
||||||
|
@ -465,11 +463,9 @@ protected:
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
table_name(model_env->table_name);
|
|
||||||
|
|
||||||
if( log )
|
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);
|
put_fields_to_log(*log, db_field_name, flat_field_name);
|
||||||
(*log) << ", type expected " << typeid(field_value).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;
|
||||||
|
@ -623,7 +619,7 @@ protected:
|
||||||
{
|
{
|
||||||
model_env->finder_helper->foreign_keys.clear();
|
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->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 ";
|
join_tables_str << "LEFT JOIN " << field_model.model_env->table_name << " AS ";
|
||||||
field_model.put_table_name_with_index(join_tables_str);
|
field_model.put_table_name_with_index(join_tables_str);
|
||||||
|
@ -704,7 +700,7 @@ protected:
|
||||||
|
|
||||||
if( model_env->field_value_helper_tab->empty() && plog )
|
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_value_helper_tab = &helper_tab;
|
||||||
model_env_local.field_index = 0;
|
model_env_local.field_index = 0;
|
||||||
field_model.model_env = &model_env_local;
|
field_model.model_env = &model_env_local;
|
||||||
|
field_model.prepare_table();
|
||||||
|
|
||||||
field_model.map_fields();
|
field_model.map_fields();
|
||||||
|
|
||||||
if( (size_t)field_model.model_env->field_index != helper_tab.size() && log )
|
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 )
|
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
|
(*log) << PT::Log::log1 << "Morm: there is no a foreign key in " << field_model.get_table_name()
|
||||||
<< " called " << db_field_name << " pointing to " << model_env->table_name << PT::Log::logend;
|
<< " called " << db_field_name << " pointing to " << get_table_name() << PT::Log::logend;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
(*log) << PT::Log::log1 << "Morm: primary key in " << model_env->table_name << " consists of " << model_env->field_index << " column(s)"
|
(*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.model_env->table_name << " consists of "
|
<< " 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->field_index << " column(s)" << PT::Log::logend;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -765,9 +756,7 @@ protected:
|
||||||
else
|
else
|
||||||
if( log )
|
if( log )
|
||||||
{
|
{
|
||||||
table_name(model_env->table_name);
|
(*log) << PT::Log::log1 << "Morm: primary key in " << get_table_name() << " consists of incorrect number of columns"
|
||||||
|
|
||||||
(*log) << PT::Log::log1 << "Morm: primary key in " << model_env->table_name << " consists of incorrect number of columns"
|
|
||||||
<< ", expected " << helper_tab.size() << " column(s) but got " << model_env->field_index << PT::Log::logend;
|
<< ", 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.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 )
|
if( db_expression->get_output_type() == MORM_OUTPUT_TYPE_SELECT_COLUMNS )
|
||||||
{
|
{
|
||||||
field_model_left_join(db_field_name, field_model, field_type, db_expression);
|
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->has_autogenerated_select &&
|
||||||
model_env->cursor_helper->use_table_prefix_for_fetching_values )
|
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();
|
field_model.before_select();
|
||||||
|
@ -968,6 +954,7 @@ protected:
|
||||||
field_model.model_env = &model_env_local;
|
field_model.model_env = &model_env_local;
|
||||||
field_model.model_env->has_primary_key_set = field_model.has_primary_key_set;
|
field_model.model_env->has_primary_key_set = field_model.has_primary_key_set;
|
||||||
field_model.set_connector(model_connector);
|
field_model.set_connector(model_connector);
|
||||||
|
field_model.prepare_table();
|
||||||
|
|
||||||
if( !is_empty_field(db_field_name) )
|
if( !is_empty_field(db_field_name) )
|
||||||
{
|
{
|
||||||
|
@ -981,7 +968,7 @@ protected:
|
||||||
|
|
||||||
if( plog )
|
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;
|
<< " 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 = &model_env_local;
|
||||||
child_model.model_env->has_primary_key_set = child_model.has_primary_key_set;
|
child_model.model_env->has_primary_key_set = child_model.has_primary_key_set;
|
||||||
child_model.set_connector(model_connector);
|
child_model.set_connector(model_connector);
|
||||||
|
child_model.prepare_table();
|
||||||
|
|
||||||
if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_INSERT )
|
if( model_env->model_work_submode == MORM_MODEL_WORK_SUBMODE_INSERT )
|
||||||
{
|
{
|
||||||
|
@ -1153,7 +1141,7 @@ protected:
|
||||||
{
|
{
|
||||||
if( plog )
|
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1176,7 +1164,7 @@ protected:
|
||||||
{
|
{
|
||||||
if( plog )
|
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 )
|
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;
|
<< " 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:
|
protected:
|
||||||
|
|
||||||
virtual bool is_empty_field(const wchar_t * value);
|
virtual bool is_empty_field(const wchar_t * value);
|
||||||
virtual bool is_the_same_field(const wchar_t * field1, const wchar_t * field2);
|
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_table_name_with_index(PT::TextStream & str);
|
||||||
|
|
||||||
virtual void put_to_log(const wchar_t * 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);
|
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();
|
virtual PT::WTextStream get_table_name(bool put_schema_name = true);
|
||||||
PT::TextStream log_table_name(const wchar_t * db_field_name);
|
virtual PT::WTextStream get_table_name_with_field(const wchar_t * db_field_name = nullptr, bool put_schema_name = true);
|
||||||
|
|
||||||
|
|
||||||
template<typename ModelClass> friend class Finder;
|
template<typename ModelClass> friend class Finder;
|
||||||
template<typename ModelClass> friend class Cursor;
|
template<typename ModelClass> friend class Cursor;
|
||||||
friend class BaseExpression;
|
friend class BaseExpression;
|
||||||
|
friend class DbConnector;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -61,8 +61,8 @@ public:
|
||||||
int model_work_submode;
|
int model_work_submode;
|
||||||
bool dump_mode;
|
bool dump_mode;
|
||||||
|
|
||||||
PT::TextStream table_name; // CHECK ME may it should be PT::WTextStream?
|
PT::WTextStream schema_name;
|
||||||
PT::TextStream table_name_short;
|
PT::WTextStream table_name;
|
||||||
int table_index;
|
int table_index;
|
||||||
int field_index;
|
int field_index;
|
||||||
bool was_primary_key_read;
|
bool was_primary_key_read;
|
||||||
|
@ -97,7 +97,7 @@ public:
|
||||||
was_primary_key_read = e.was_primary_key_read;
|
was_primary_key_read = e.was_primary_key_read;
|
||||||
has_primary_key_set = e.has_primary_key_set;
|
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_mode = MORM_MODEL_WORK_MODE_NONE;
|
||||||
model_work_submode = MORM_MODEL_WORK_SUBMODE_NONE;
|
model_work_submode = MORM_MODEL_WORK_SUBMODE_NONE;
|
||||||
dump_mode = false;
|
dump_mode = false;
|
||||||
|
schema_name.clear();
|
||||||
table_name.clear();
|
table_name.clear();
|
||||||
table_name_short.clear();
|
|
||||||
table_index = 0;
|
table_index = 0;
|
||||||
set_field_name_helper = nullptr;
|
set_field_name_helper = nullptr;
|
||||||
field_value_helper_tab = 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue