fixed: in 'left join' statements there were not table indices used

added: now we set flag save_mode = DO_NOTHING_ON_SAVE for objects for which
       all fields from a database result set are null



git-svn-id: svn://ttmath.org/publicrep/morm/trunk@1210 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
2019-09-18 12:29:20 +00:00
parent afce2234c3
commit ba5f702257
11 changed files with 126 additions and 62 deletions

View File

@@ -822,13 +822,11 @@ protected:
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->table_index = model_env->finder_helper->add_join_table(field_model.model_env->table_name_short);
model_env->finder_helper->join_tables_str << "LEFT JOIN " << field_model.model_env->table_name
<< " AS " << field_model.model_env->table_name_short;
if( field_model.model_env->table_index > 1 )
model_env->finder_helper->join_tables_str << field_model.model_env->table_index;
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();
@@ -842,12 +840,16 @@ protected:
{
field_model.map_fields();
model_env->finder_helper->join_tables_str << " ON " << model_env->table_name_short << '.'
<< db_field_name << " = " << field_model.model_env->table_name_short << '.';
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 )
{
model_env->finder_helper->join_tables_str << model_env->finder_helper->foreign_keys.front();
join_tables_str << model_env->finder_helper->foreign_keys.front();
}
}
else
@@ -856,17 +858,22 @@ protected:
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 << '.';
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 )
{
model_env->finder_helper->join_tables_str << model_env->finder_helper->foreign_keys.front();
join_tables_str << model_env->finder_helper->foreign_keys.front();
}
model_env->finder_helper->join_tables_str << " = " << field_model.model_env->table_name_short << '.' << db_field_name;
join_tables_str << " = ";
field_model.put_table_name_with_index(join_tables_str);
join_tables_str << '.' << db_field_name;
}
model_env->finder_helper->join_tables_str << ' ';
join_tables_str << ' ';
db_expression->set_work_mode(expr_work_mode);
db_expression->set_output_type(expr_output_type);
@@ -1012,14 +1019,13 @@ protected:
field_model.prepare_table_names();
}
// IMPROVE ME
// we need to test if the object is actually defined, test nulls on primary key?
// or check every field whether is it null?
field_model.before_select();
field_model.set_save_mode(Model::DO_UPDATE_ON_SAVE); // IMPROVE ME check if there is a primary key
field_model.map_values_from_query();
field_model.after_select();
if( field_model.found() )
{
field_model.after_select();
}
}
}
}
@@ -1264,15 +1270,16 @@ protected:
template<typename FieldValue>
void get_value_by_field_index(int field_index, FieldValue & field_value)
{
if( model_env->cursor_helper && model_env->cursor_helper->query_result )
DbConnector * db_connector = model_connector->get_db_connector();
if( db_connector && model_env->cursor_helper && model_env->cursor_helper->query_result )
{
const char * val_str = model_env->cursor_helper->query_result->get_field_string_value(field_index);
if( val_str )
if( !model_env->cursor_helper->query_result->is_null(field_index) )
{
DbConnector * db_connector = model_connector->get_db_connector();
model_env->all_fields_are_null = false;
const char * val_str = model_env->cursor_helper->query_result->get_field_string_value(field_index);
if( db_connector )
if( val_str )
{
db_connector->get_value(val_str, field_value);
}
@@ -1288,7 +1295,7 @@ protected:
if( db_connector && model_env->cursor_helper && model_env->cursor_helper->query_result )
{
const char * val_str = nullptr;
int column_index = -1;
if( model_env->cursor_helper->use_table_prefix_for_fetching_values && model_env->finder_helper )
{
@@ -1297,26 +1304,26 @@ protected:
std::wstring table_field_name;
PT::TextStream table_field_name_str;
table_field_name_str = model_env->table_name_short;
if( model_env->table_index > 1 )
{
table_field_name_str << model_env->table_index;
}
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);
val_str = model_env->cursor_helper->query_result->get_field_string_value(table_field_name.c_str());
column_index = model_env->cursor_helper->query_result->get_column_index(table_field_name.c_str());
}
else
{
val_str = model_env->cursor_helper->query_result->get_field_string_value(field_name);
column_index = model_env->cursor_helper->query_result->get_column_index(field_name);
}
if( val_str )
if( column_index != -1 && !model_env->cursor_helper->query_result->is_null(column_index) )
{
db_connector->get_value(val_str, field_value);
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 )
{
db_connector->get_value(val_str, field_value);
}
}
}
}
@@ -1475,6 +1482,7 @@ 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);
template<typename ModelClass> friend class Finder;
template<typename ModelClass> friend class Cursor;