From 34ddf11351868d8ae0e10ce9aaea68c85eec18e8 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Mon, 13 May 2019 03:12:31 +0000 Subject: [PATCH] some work in branches/join_models git-svn-id: svn://ttmath.org/publicrep/morm/branches/join_models@1190 e52654a7-88a9-db11-a3e9-0013d4bc506e --- src/cursor.h | 6 ++++++ src/finder.h | 1 + src/model.h | 29 ++++++++++++++++++++++++++--- src/modeldata.h | 5 +++++ src/postgresqlconnector.cpp | 1 + src/postgresqlqueryresult.cpp | 17 +++++++++++++++++ src/postgresqlqueryresult.h | 1 + src/queryresult.cpp | 7 +++++++ src/queryresult.h | 2 ++ 9 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/cursor.h b/src/cursor.h index 9ff9aa5..1d471f4 100644 --- a/src/cursor.h +++ b/src/cursor.h @@ -152,6 +152,9 @@ public: { try { + if( model_data ) + model_data->current_column = 0; + result.model_data = model_data; result.before_select(); res = select_status; @@ -283,6 +286,9 @@ protected: try { + if( model_data ) + model_data->current_column = 0; + added_model.set_connector(model_connector); added_model.clear(); added_model.set_save_mode(Model::DO_UPDATE_ON_SAVE); // IMPROVE ME check if there is a primary key diff --git a/src/finder.h b/src/finder.h index 375b398..161c43d 100644 --- a/src/finder.h +++ b/src/finder.h @@ -250,6 +250,7 @@ public: } model_data->prepare_to_new_select(); + model_data->has_autogenerated_select = true; if( model_connector && out_stream ) { diff --git a/src/model.h b/src/model.h index 7c2a404..527ef39 100644 --- a/src/model.h +++ b/src/model.h @@ -683,12 +683,15 @@ protected: { if( !is_empty_field(db_field_name) ) { - get_value_by_field_name(db_field_name, field_value); - - if( model_data ) + if( model_data && model_data->has_autogenerated_select ) { + get_value_by_field_index(model_data->current_column, field_value); model_data->current_column += 1; } + else + { + get_value_by_field_name(db_field_name, field_value); + } } } } @@ -978,6 +981,26 @@ protected: } + template + void get_value_by_field_index(int field_index, FieldValue & field_value) + { + if( query_result ) + { + const char * val_str = query_result->get_field_string_value(field_index); + + if( val_str ) + { + DbConnector * db_connector = model_connector->get_db_connector(); + + if( db_connector ) + { + db_connector->get_value(val_str, field_value); + } + } + } + } + + template void get_value_by_field_name(const wchar_t * field_name, FieldValue & field_value) { diff --git a/src/modeldata.h b/src/modeldata.h index 5f435dc..df6ef2d 100644 --- a/src/modeldata.h +++ b/src/modeldata.h @@ -53,6 +53,9 @@ public: { } + bool has_autogenerated_select; + + PT::TextStream morm_finder_join_tables; std::list morm_foreign_keys; int morm_current_max_column; @@ -65,6 +68,8 @@ public: virtual void prepare_to_new_select() { + has_autogenerated_select = false; + morm_current_max_column = 1; morm_finder_join_tables.clear(); morm_table_join_map.clear(); diff --git a/src/postgresqlconnector.cpp b/src/postgresqlconnector.cpp index 39272f4..b3fb529 100644 --- a/src/postgresqlconnector.cpp +++ b/src/postgresqlconnector.cpp @@ -115,6 +115,7 @@ bool PostgreSQLConnector::do_query(const char * query_str, PostgreSQLQueryResult { psql_result->psql_status = PQresultStatus(psql_result->psql_result); psql_result->result_rows = static_cast(PQntuples(psql_result->psql_result)); + psql_result->result_cols = static_cast(PQnfields(psql_result->psql_result)); } if( !psql_result->psql_result || psql_result->psql_status == PGRES_FATAL_ERROR ) diff --git a/src/postgresqlqueryresult.cpp b/src/postgresqlqueryresult.cpp index bcb45d9..a28f240 100644 --- a/src/postgresqlqueryresult.cpp +++ b/src/postgresqlqueryresult.cpp @@ -67,6 +67,23 @@ bool PostgreSQLQueryResult::has_db_result() } +const char * PostgreSQLQueryResult::get_field_string_value(int column_index) +{ + const char * value_str = nullptr; + + if( column_index >= 0 && (size_t)column_index < result_cols ) + { + if( cur_row < result_rows ) + { + value_str = PQgetvalue(psql_result, cur_row, column_index); + } + } + + return value_str; +} + + + const char * PostgreSQLQueryResult::get_field_string_value(const char * column_name) { const char * value_str = nullptr; diff --git a/src/postgresqlqueryresult.h b/src/postgresqlqueryresult.h index 74c4dd5..3efa8f3 100644 --- a/src/postgresqlqueryresult.h +++ b/src/postgresqlqueryresult.h @@ -57,6 +57,7 @@ struct PostgreSQLQueryResult : public QueryResult virtual bool has_db_result(); + const char * get_field_string_value(int column_index); const char * get_field_string_value(const char * column_name); int get_column_index(const char * column_name); diff --git a/src/queryresult.cpp b/src/queryresult.cpp index b5bd092..2e3c16b 100644 --- a/src/queryresult.cpp +++ b/src/queryresult.cpp @@ -56,6 +56,7 @@ QueryResult::~QueryResult() void QueryResult::clear() { result_rows = 0; + result_cols = 0; cur_row = 0; status = false; error_msg.clear(); @@ -70,6 +71,12 @@ bool QueryResult::has_db_result() } +const char * QueryResult::get_field_string_value(int column_index) +{ + return nullptr; +} + + const char * QueryResult::get_field_string_value(const char * column_name) { return nullptr; diff --git a/src/queryresult.h b/src/queryresult.h index 5caef0e..9ca13bb 100644 --- a/src/queryresult.h +++ b/src/queryresult.h @@ -45,6 +45,7 @@ struct QueryResult { bool status; size_t result_rows; // how many rows in the result query + size_t result_cols; // how many columns in the result query size_t cur_row; // used for reading std::wstring error_msg; @@ -57,6 +58,7 @@ struct QueryResult virtual void clear(); virtual bool has_db_result(); + virtual const char * get_field_string_value(int column_index); virtual const char * get_field_string_value(const char * column_name); virtual const char * get_field_string_value(const wchar_t * field_name);