some work: we need a different way of naming tables for joins
git-svn-id: svn://ttmath.org/publicrep/morm/branches/join_models@1187 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
73
src/model.h
73
src/model.h
@@ -96,6 +96,7 @@ public:
|
||||
*
|
||||
*/
|
||||
virtual void table_name(PT::TextStream & stream);
|
||||
virtual void table_name_for_join_as(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);
|
||||
@@ -123,7 +124,7 @@ public:
|
||||
virtual bool save(ModelData * model_data = nullptr);
|
||||
virtual bool save(ModelData & model_data);
|
||||
|
||||
virtual void generate_select_columns(PT::TextStream & stream, const std::wstring & column_prefix, ModelData * model_data);
|
||||
virtual void generate_select_columns(PT::TextStream & stream, const std::string & column_prefix, ModelData * model_data);
|
||||
|
||||
virtual void generate_doc_for_flat(PT::TextStream & stream, bool clear_stream = true);
|
||||
virtual void generate_doc_for_db(PT::TextStream & stream, bool clear_stream = true);
|
||||
@@ -720,24 +721,61 @@ protected:
|
||||
{
|
||||
model_data->morm_current_max_column += 1;
|
||||
model_data->morm_foreign_keys.clear();
|
||||
|
||||
field_model.table_index = model_data->morm_current_max_column;
|
||||
|
||||
model_data->morm_finder_join_tables << "LEFT JOIN ";
|
||||
field_model.table_name(model_data->morm_finder_join_tables);
|
||||
model_data->morm_finder_join_tables << " AS t" << (model_data->morm_current_max_column);
|
||||
|
||||
model_data->morm_finder_join_tables << " AS ";
|
||||
|
||||
|
||||
//////////////
|
||||
PT::TextStream join_table_name_this;
|
||||
table_name_for_join_as(join_table_name_this);
|
||||
|
||||
std::string sss1; // improve me
|
||||
join_table_name_this.to_string(sss1);
|
||||
|
||||
int this_index = model_data->find_join_table_index(sss1);
|
||||
|
||||
if( this_index > 1 )
|
||||
join_table_name_this << this_index;
|
||||
///////////
|
||||
PT::TextStream join_table_name;
|
||||
field_model.table_name_for_join_as(join_table_name);
|
||||
|
||||
std::string sss; // improve me
|
||||
join_table_name.to_string(sss);
|
||||
int table_index = model_data->add_join_table(sss);
|
||||
|
||||
if( table_index > 1 )
|
||||
join_table_name << table_index;
|
||||
//////////
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
model_data->morm_finder_join_tables << join_table_name;
|
||||
|
||||
int expr_work_mode = db_expression->get_work_mode();
|
||||
int expr_output_type = db_expression->get_output_type();
|
||||
std::string old_prefix = db_expression->get_column_prefix();
|
||||
|
||||
db_expression->set_work_mode(MORM_WORK_MODE_MODEL_SAVE_FIELDS);
|
||||
db_expression->set_output_type(MORM_OUTPUT_TYPE_JOIN_TABLES);
|
||||
|
||||
std::string empty;
|
||||
db_expression->set_column_prefix(empty);
|
||||
|
||||
if( has_foreign_key )
|
||||
{
|
||||
field_model.map_fields();
|
||||
|
||||
model_data->morm_finder_join_tables << " ON t" << table_index << '.' << db_field_name;
|
||||
model_data->morm_finder_join_tables << " = ";
|
||||
model_data->morm_finder_join_tables << " ON " << join_table_name_this << '.' << db_field_name; // escape?
|
||||
model_data->morm_finder_join_tables << " = " << join_table_name << '.';
|
||||
|
||||
if( !model_data->morm_foreign_keys.empty() )
|
||||
{
|
||||
@@ -748,19 +786,20 @@ protected:
|
||||
{
|
||||
map_fields();
|
||||
|
||||
model_data->morm_finder_join_tables << " ON ";
|
||||
model_data->morm_finder_join_tables << " ON " << join_table_name_this << '.';
|
||||
|
||||
if( !model_data->morm_foreign_keys.empty() )
|
||||
{
|
||||
model_data->morm_finder_join_tables << model_data->morm_foreign_keys.front();
|
||||
}
|
||||
|
||||
model_data->morm_finder_join_tables << " = t" << (model_data->morm_current_max_column) << '.' << db_field_name;
|
||||
model_data->morm_finder_join_tables << " = " << (join_table_name) << '.' << db_field_name;
|
||||
}
|
||||
|
||||
model_data->morm_finder_join_tables << ' ';
|
||||
db_expression->set_work_mode(expr_work_mode);
|
||||
db_expression->set_output_type(expr_output_type);
|
||||
db_expression->set_column_prefix(old_prefix);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -809,7 +848,29 @@ protected:
|
||||
if( db_expression->get_output_type() != MORM_OUTPUT_TYPE_JOIN_TABLES )
|
||||
{
|
||||
field_model.model_connector_mode = MORM_MODEL_CONNECTOR_MODE_GENERATING_DB_SQL;
|
||||
std::string current_prefix = db_expression->get_column_prefix();
|
||||
|
||||
///////////
|
||||
PT::TextStream join_table_name;
|
||||
field_model.table_name_for_join_as(join_table_name);
|
||||
|
||||
std::string sss; // improve me
|
||||
join_table_name.to_string(sss);
|
||||
int table_index = model_data->add_join_table(sss);
|
||||
|
||||
if( table_index > 1 )
|
||||
{
|
||||
join_table_name << table_index;
|
||||
join_table_name.to_string(sss);
|
||||
}
|
||||
|
||||
//////////
|
||||
db_expression->set_column_prefix(sss);
|
||||
|
||||
|
||||
|
||||
field_model.map_fields();
|
||||
db_expression->set_column_prefix(current_prefix);
|
||||
field_model.model_connector_mode = MORM_MODEL_CONNECTOR_MODE_NONE;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user