From afce2234c31fda8350227ac24f32a3fc0edd4f0f Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Tue, 17 Sep 2019 17:55:39 +0000 Subject: [PATCH] fixed: get_value_by_field_name() is able to correctly take values when we do not use auto generated 'select' and when we are using prefixes for columns git-svn-id: svn://ttmath.org/publicrep/morm/trunk@1209 e52654a7-88a9-db11-a3e9-0013d4bc506e --- samples/Makefile.dep | 7 ++-- samples/sample01.h | 12 +++++- src/Makefile.dep | 65 ++++++++++++++++++++----------- src/cursor.h | 33 +++++++++++++++- src/cursorhelper.h | 5 +++ src/finder.h | 18 +++++++-- src/model.cpp | 22 +++++++++++ src/model.h | 72 ++++++++++++++++++++++------------- src/postgresqlqueryresult.cpp | 16 +++++++- src/postgresqlqueryresult.h | 3 +- src/queryresult.cpp | 6 ++- src/queryresult.h | 4 +- 12 files changed, 200 insertions(+), 63 deletions(-) diff --git a/samples/Makefile.dep b/samples/Makefile.dep index d56c775..0c04d27 100644 --- a/samples/Makefile.dep +++ b/samples/Makefile.dep @@ -15,10 +15,9 @@ main.o: ../../morm/src/dbexpression.h ../../morm/src/baseexpression.h main.o: ../../morm/src/modelenv.h ../../morm/src/modeldata.h main.o: ../../morm/src/cursorhelper.h ../../morm/src/finderhelper.h main.o: ../../morm/src/fieldvaluehelper.h ../../morm/src/flatexpression.h -main.o: ../../morm/src/finder.h ../../pikotools/utf8/utf8.h -main.o: ../../morm/src/cursor.h ../../morm/src/jsonexpression.h -main.o: ../../morm/src/postgresqlexpression.h +main.o: ../../morm/src/postgresqlqueryresult.h ../../morm/src/finder.h +main.o: ../../pikotools/utf8/utf8.h ../../morm/src/cursor.h +main.o: ../../morm/src/jsonexpression.h ../../morm/src/postgresqlexpression.h main.o: ../../morm/src/dochtmlexpression.h ../../morm/src/jsonconnector.h main.o: ../../morm/src/postgresqlconnector.h -main.o: ../../morm/src/postgresqlqueryresult.h main.o: ../../morm/src/dochtmlconnector.h person.h language.h attachment.h diff --git a/samples/sample01.h b/samples/sample01.h index 9f044f0..ad9ee30 100644 --- a/samples/sample01.h +++ b/samples/sample01.h @@ -65,7 +65,17 @@ void make() morm::Finder finder(model_connector); - Person p = finder.select().where().eq(L"id", 110).get(); + //Person p = finder.use_table_prefix(false).select().where().eq(L"id", 110).get(); + + Person p = finder.prepare_to_select().use_table_prefix(true).raw("select person.id as \"person.id\", person.first_name as \"person.first_name\", person.last_name as \"person.last_name\", person.email as \"person.email\", " + "language.id as \"language.id\", language.english_name as \"language.english_name\", language.local_name as \"language.local_name\", language.code_str as \"language.code_str\", language.code_int as \"language.code_int\", " + "attachment.id as \"attachment.id\", attachment.person_id as \"attachment.person_id\", attachment.name as \"attachment.name\", attachment.content as \"attachment.content\", attachment.some_flags as \"attachment.some_flags\", attachment.created_date as \"attachment.created_date\"," + "language2.id as \"language2.id\", language2.english_name as \"language2.english_name\", language2.local_name as \"language2.local_name\", language2.code_str as \"language2.code_str\", language2.code_int as \"language2.code_int\"" + "FROM public.person AS person " + "LEFT JOIN public.language AS language ON person.language_id = language.id " + "LEFT JOIN public.attachment AS attachment ON person.id = attachment.person_id " + "LEFT JOIN public.language AS language2 ON attachment.language_id = language.id " + "where person.id=110").get(); std::string str; p.to_text(str, true, true); diff --git a/src/Makefile.dep b/src/Makefile.dep index 36a6f42..ec7cca1 100644 --- a/src/Makefile.dep +++ b/src/Makefile.dep @@ -7,11 +7,12 @@ baseexpression.o: ../../pikotools/date/date.h baseexpression.o: ../../pikotools/convert/inttostr.h baseexpression.o: ../../pikotools/membuffer/membuffer.h baseexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h -baseexpression.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h -baseexpression.o: fieldvaluehelper.h model.h modelconnector.h clearer.h -baseexpression.o: dbconnector.h ../../pikotools/log/log.h -baseexpression.o: ../../pikotools/log/filelog.h flatconnector.h -baseexpression.o: dbexpression.h flatexpression.h ../../pikotools/utf8/utf8.h +baseexpression.o: modeldata.h cursorhelper.h queryresult.h +baseexpression.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h +baseexpression.o: finderhelper.h fieldvaluehelper.h model.h modelconnector.h +baseexpression.o: clearer.h dbconnector.h flatconnector.h dbexpression.h +baseexpression.o: flatexpression.h postgresqlqueryresult.h +baseexpression.o: ../../pikotools/utf8/utf8.h clearer.o: clearer.h ../../pikotools/date/date.h clearer.o: ../../pikotools/convert/inttostr.h model.h clearer.o: ../../pikotools/textstream/textstream.h @@ -21,7 +22,7 @@ clearer.o: ../../pikotools/textstream/types.h modelconnector.h dbconnector.h clearer.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h clearer.o: queryresult.h flatconnector.h dbexpression.h baseexpression.h clearer.o: morm_types.h modelenv.h modeldata.h cursorhelper.h finderhelper.h -clearer.o: fieldvaluehelper.h flatexpression.h +clearer.o: fieldvaluehelper.h flatexpression.h postgresqlqueryresult.h dbconnector.o: dbconnector.h ../../pikotools/textstream/textstream.h dbconnector.o: ../../pikotools/space/space.h dbconnector.o: ../../pikotools/textstream/types.h ../../pikotools/date/date.h @@ -32,7 +33,8 @@ dbconnector.o: ../../pikotools/log/filelog.h queryresult.h dbexpression.h dbconnector.o: baseexpression.h morm_types.h modelenv.h modeldata.h dbconnector.o: cursorhelper.h finderhelper.h fieldvaluehelper.h model.h dbconnector.o: modelconnector.h clearer.h flatconnector.h flatexpression.h -dbconnector.o: ../../pikotools/utf8/utf8.h ../../pikotools/convert/convert.h +dbconnector.o: postgresqlqueryresult.h ../../pikotools/utf8/utf8.h +dbconnector.o: ../../pikotools/convert/convert.h dbconnector.o: ../../pikotools/convert/inttostr.h dbconnector.o: ../../pikotools/convert/patternreplacer.h dbconnector.o: ../../pikotools/convert/strtoint.h @@ -45,8 +47,9 @@ dbexpression.o: ../../pikotools/date/date.h dbexpression.o: ../../pikotools/convert/inttostr.h dbexpression.o: ../../pikotools/membuffer/membuffer.h dbexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h -dbexpression.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h -dbexpression.o: fieldvaluehelper.h +dbexpression.o: modeldata.h cursorhelper.h queryresult.h +dbexpression.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h +dbexpression.o: finderhelper.h fieldvaluehelper.h dochtmlconnector.o: dochtmlconnector.h flatconnector.h dochtmlconnector.o: ../../pikotools/textstream/textstream.h dochtmlconnector.o: ../../pikotools/space/space.h @@ -56,8 +59,9 @@ dochtmlconnector.o: ../../pikotools/convert/inttostr.h dochtmlconnector.o: ../../pikotools/membuffer/membuffer.h dochtmlconnector.o: ../../pikotools/textstream/types.h dochtmlexpression.h dochtmlconnector.o: flatexpression.h baseexpression.h morm_types.h modelenv.h -dochtmlconnector.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h -dochtmlconnector.o: fieldvaluehelper.h +dochtmlconnector.o: modeldata.h cursorhelper.h queryresult.h +dochtmlconnector.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h +dochtmlconnector.o: finderhelper.h fieldvaluehelper.h dochtmlexpression.o: dochtmlexpression.h flatexpression.h baseexpression.h dochtmlexpression.o: ../../pikotools/textstream/textstream.h dochtmlexpression.o: ../../pikotools/space/space.h @@ -67,6 +71,7 @@ dochtmlexpression.o: ../../pikotools/convert/inttostr.h dochtmlexpression.o: ../../pikotools/membuffer/membuffer.h dochtmlexpression.o: ../../pikotools/textstream/types.h morm_types.h dochtmlexpression.o: modelenv.h modeldata.h cursorhelper.h queryresult.h +dochtmlexpression.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h dochtmlexpression.o: finderhelper.h fieldvaluehelper.h flatconnector.o: flatconnector.h ../../pikotools/textstream/textstream.h flatconnector.o: ../../pikotools/space/space.h @@ -76,10 +81,10 @@ flatconnector.o: ../../pikotools/convert/inttostr.h flatconnector.o: ../../pikotools/membuffer/membuffer.h flatconnector.o: ../../pikotools/textstream/types.h flatexpression.h flatconnector.o: baseexpression.h morm_types.h modelenv.h modeldata.h -flatconnector.o: cursorhelper.h queryresult.h finderhelper.h +flatconnector.o: cursorhelper.h queryresult.h ../../pikotools/log/log.h +flatconnector.o: ../../pikotools/log/filelog.h finderhelper.h flatconnector.o: fieldvaluehelper.h model.h modelconnector.h clearer.h -flatconnector.o: dbconnector.h ../../pikotools/log/log.h -flatconnector.o: ../../pikotools/log/filelog.h dbexpression.h +flatconnector.o: dbconnector.h dbexpression.h postgresqlqueryresult.h flatexpression.o: flatexpression.h baseexpression.h flatexpression.o: ../../pikotools/textstream/textstream.h flatexpression.o: ../../pikotools/space/space.h @@ -88,8 +93,9 @@ flatexpression.o: ../../pikotools/date/date.h flatexpression.o: ../../pikotools/convert/inttostr.h flatexpression.o: ../../pikotools/membuffer/membuffer.h flatexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h -flatexpression.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h -flatexpression.o: fieldvaluehelper.h +flatexpression.o: modeldata.h cursorhelper.h queryresult.h +flatexpression.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h +flatexpression.o: finderhelper.h fieldvaluehelper.h jsonconnector.o: jsonconnector.h flatconnector.h jsonconnector.o: ../../pikotools/textstream/textstream.h jsonconnector.o: ../../pikotools/space/space.h @@ -99,8 +105,9 @@ jsonconnector.o: ../../pikotools/convert/inttostr.h jsonconnector.o: ../../pikotools/membuffer/membuffer.h jsonconnector.o: ../../pikotools/textstream/types.h jsonexpression.h jsonconnector.o: flatexpression.h baseexpression.h morm_types.h modelenv.h -jsonconnector.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h -jsonconnector.o: fieldvaluehelper.h +jsonconnector.o: modeldata.h cursorhelper.h queryresult.h +jsonconnector.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h +jsonconnector.o: finderhelper.h fieldvaluehelper.h jsonexpression.o: jsonexpression.h flatexpression.h baseexpression.h jsonexpression.o: ../../pikotools/textstream/textstream.h jsonexpression.o: ../../pikotools/space/space.h @@ -109,8 +116,9 @@ jsonexpression.o: ../../pikotools/date/date.h jsonexpression.o: ../../pikotools/convert/inttostr.h jsonexpression.o: ../../pikotools/membuffer/membuffer.h jsonexpression.o: ../../pikotools/textstream/types.h morm_types.h modelenv.h -jsonexpression.o: modeldata.h cursorhelper.h queryresult.h finderhelper.h -jsonexpression.o: fieldvaluehelper.h +jsonexpression.o: modeldata.h cursorhelper.h queryresult.h +jsonexpression.o: ../../pikotools/log/log.h ../../pikotools/log/filelog.h +jsonexpression.o: finderhelper.h fieldvaluehelper.h model.o: model.h ../../pikotools/textstream/textstream.h model.o: ../../pikotools/space/space.h ../../pikotools/textstream/types.h model.o: ../../pikotools/date/date.h ../../pikotools/convert/inttostr.h @@ -120,6 +128,7 @@ model.o: dbconnector.h ../../pikotools/log/log.h model.o: ../../pikotools/log/filelog.h queryresult.h flatconnector.h model.o: dbexpression.h baseexpression.h morm_types.h modelenv.h modeldata.h model.o: cursorhelper.h finderhelper.h fieldvaluehelper.h flatexpression.h +model.o: postgresqlqueryresult.h modelconnector.o: modelconnector.h clearer.h ../../pikotools/date/date.h modelconnector.o: ../../pikotools/convert/inttostr.h dbconnector.h modelconnector.o: ../../pikotools/textstream/textstream.h @@ -156,12 +165,24 @@ postgresqlexpression.o: ../../pikotools/convert/inttostr.h postgresqlexpression.o: ../../pikotools/membuffer/membuffer.h postgresqlexpression.o: ../../pikotools/textstream/types.h morm_types.h postgresqlexpression.o: modelenv.h modeldata.h cursorhelper.h queryresult.h -postgresqlexpression.o: finderhelper.h fieldvaluehelper.h +postgresqlexpression.o: ../../pikotools/log/log.h +postgresqlexpression.o: ../../pikotools/log/filelog.h finderhelper.h +postgresqlexpression.o: fieldvaluehelper.h postgresqlqueryresult.o: postgresqlqueryresult.h queryresult.h -queryresult.o: queryresult.h ../../pikotools/utf8/utf8.h +postgresqlqueryresult.o: ../../pikotools/log/log.h +postgresqlqueryresult.o: ../../pikotools/textstream/textstream.h +postgresqlqueryresult.o: ../../pikotools/space/space.h +postgresqlqueryresult.o: ../../pikotools/textstream/types.h +postgresqlqueryresult.o: ../../pikotools/date/date.h +postgresqlqueryresult.o: ../../pikotools/convert/inttostr.h +postgresqlqueryresult.o: ../../pikotools/membuffer/membuffer.h +postgresqlqueryresult.o: ../../pikotools/textstream/types.h +postgresqlqueryresult.o: ../../pikotools/log/filelog.h +queryresult.o: queryresult.h ../../pikotools/log/log.h queryresult.o: ../../pikotools/textstream/textstream.h queryresult.o: ../../pikotools/space/space.h queryresult.o: ../../pikotools/textstream/types.h ../../pikotools/date/date.h queryresult.o: ../../pikotools/convert/inttostr.h queryresult.o: ../../pikotools/membuffer/membuffer.h queryresult.o: ../../pikotools/textstream/types.h +queryresult.o: ../../pikotools/log/filelog.h ../../pikotools/utf8/utf8.h diff --git a/src/cursor.h b/src/cursor.h index a737f51..151b191 100644 --- a/src/cursor.h +++ b/src/cursor.h @@ -61,6 +61,7 @@ public: model_connector = c.model_connector; model_data = c.model_data; has_autogenerated_select = c.has_autogenerated_select; + use_table_prefix_for_fetching = c.use_table_prefix_for_fetching; query_result = c.query_result; select_status = c.select_status; @@ -71,6 +72,7 @@ public: // helper doesn't have to be copied cursor_helper.clear(); + finder_helper.clear(); } @@ -99,10 +101,11 @@ public: model_connector = nullptr; model_data = nullptr; has_autogenerated_select = false; + use_table_prefix_for_fetching = false; cursor_helper.clear(); + finder_helper.clear(); query_result = nullptr; select_status = false; - } @@ -136,6 +139,12 @@ public: } + virtual void use_table_prefix(bool use_table_prefix_for_fetching) + { + this->use_table_prefix_for_fetching = use_table_prefix_for_fetching; + } + + virtual QueryResult * get_query_result() { return query_result; @@ -171,14 +180,22 @@ public: ModelEnv model_env_local; result.model_env = &model_env_local; result.model_env->cursor_helper = &cursor_helper; + result.model_env->finder_helper = &finder_helper; try { + finder_helper.clear(); // at the moment used only for calculating table prefixes (indices) cursor_helper.clear(); cursor_helper.query_result = query_result; cursor_helper.has_autogenerated_select = has_autogenerated_select; - + cursor_helper.use_table_prefix_for_fetching_values = use_table_prefix_for_fetching; result.model_env->model_data = model_data; + + if( !cursor_helper.has_autogenerated_select && cursor_helper.use_table_prefix_for_fetching_values ) + { + result.prepare_table_names(); + } + result.before_select(); res = select_status; @@ -285,7 +302,9 @@ protected: ModelConnector * model_connector; ModelData * model_data; bool has_autogenerated_select; + bool use_table_prefix_for_fetching; CursorHelper cursor_helper; + FinderHelper finder_helper; // may CursorHelper and FinderHelper should be one class? QueryResult * query_result; bool select_status; @@ -306,17 +325,27 @@ protected: try { + finder_helper.clear(); + cursor_helper.clear(); cursor_helper.query_result = query_result; cursor_helper.has_autogenerated_select = has_autogenerated_select; + cursor_helper.use_table_prefix_for_fetching_values = use_table_prefix_for_fetching; added_model.set_connector(model_connector); added_model.clear(); added_model.model_env = &model_env_local; added_model.model_env->cursor_helper = &cursor_helper; + added_model.model_env->finder_helper = &finder_helper; added_model.set_save_mode(Model::DO_UPDATE_ON_SAVE); // IMPROVE ME check if there is a primary key added_model.model_env->model_data = model_data; + + if( !cursor_helper.has_autogenerated_select && cursor_helper.use_table_prefix_for_fetching_values ) + { + added_model.prepare_table_names(); + } + added_model.before_select(); added_model.map_values_from_query(); added_model.after_select(); diff --git a/src/cursorhelper.h b/src/cursorhelper.h index 2b469f2..5a7165e 100644 --- a/src/cursorhelper.h +++ b/src/cursorhelper.h @@ -49,6 +49,10 @@ public: QueryResult * query_result; int current_column; + // used if has_autogenerated_select is equal false + // if use_table_prefix_for_fetching_values is true we find a column in such a form: table.column_name instead of just column_name + bool use_table_prefix_for_fetching_values; + CursorHelper() @@ -67,6 +71,7 @@ public: has_autogenerated_select = false; query_result = nullptr; current_column = 0; + use_table_prefix_for_fetching_values = false; } }; diff --git a/src/finder.h b/src/finder.h index 86faf13..90018db 100644 --- a/src/finder.h +++ b/src/finder.h @@ -152,6 +152,7 @@ public: was_query_error = false; last_query_error.clear(); has_autogenerated_select = false; + use_table_prefix_for_fetching = false; if( model_connector ) { @@ -231,10 +232,7 @@ public: if( model_connector && out_stream && db_expression ) { - model.table_name(model.model_env->table_name); - db_expression->prepare_short_table_name(model.model_env->table_name, model.model_env->table_name_short); - - model.model_env->table_index = finder_helper.add_join_table(model.model_env->table_name_short); + model.prepare_table_names(); (*out_stream) << "SELECT "; model.generate_select_columns(*out_stream); @@ -272,6 +270,16 @@ public: } + /* + * used if we are not using auto generated selects + */ + Finder & use_table_prefix(bool use_table_prefix_for_fetching) + { + this->use_table_prefix_for_fetching = use_table_prefix_for_fetching; + return *this; + } + + Finder & where() { if( out_stream && db_expression ) @@ -500,6 +508,7 @@ public: Cursor cursor; cursor.set_model_data(model_data); cursor.set_has_autogenerated_select(has_autogenerated_select); + cursor.use_table_prefix(use_table_prefix_for_fetching); if( model_connector && out_stream ) { @@ -584,6 +593,7 @@ private: FinderHelper finder_helper; ModelData * model_data; bool has_autogenerated_select; + bool use_table_prefix_for_fetching; void set_db_expression() diff --git a/src/model.cpp b/src/model.cpp index a548746..deae7f2 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -752,5 +752,27 @@ 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); + } + } + } +} + } // namespace diff --git a/src/model.h b/src/model.h index 9d41602..272ede2 100644 --- a/src/model.h +++ b/src/model.h @@ -47,7 +47,6 @@ #include "modelenv.h" - namespace morm { @@ -1002,12 +1001,26 @@ protected: { if( !is_empty_field(db_field_name) ) { - // we need to test if the object is actually defined, test nulls on primary key? + DbExpression * db_expression = db_connector->get_expression(); - 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( db_expression ) + { + if( model_env->cursor_helper && + !model_env->cursor_helper->has_autogenerated_select && + model_env->cursor_helper->use_table_prefix_for_fetching_values ) + { + 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(); + } } } } @@ -1271,33 +1284,39 @@ protected: template void get_value_by_field_name(const wchar_t * field_name, 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 ) { -// std::wstring table_field_name; + const char * val_str = nullptr; - // CHECK what about escaping field names here? + if( model_env->cursor_helper->use_table_prefix_for_fetching_values && model_env->finder_helper ) + { + // 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; + std::wstring table_field_name; + PT::TextStream table_field_name_str; - const char * val_str = model_env->cursor_helper->query_result->get_field_string_value(field_name); + table_field_name_str = model_env->table_name_short; + + if( model_env->table_index > 1 ) + { + table_field_name_str << model_env->table_index; + } + + 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()); + } + else + { + val_str = model_env->cursor_helper->query_result->get_field_string_value(field_name); + } if( val_str ) { - DbConnector * db_connector = model_connector->get_db_connector(); - - if( db_connector ) - { - db_connector->get_value(val_str, field_value); - } + db_connector->get_value(val_str, field_value); } } } @@ -1455,6 +1474,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); template friend class Finder; template friend class Cursor; diff --git a/src/postgresqlqueryresult.cpp b/src/postgresqlqueryresult.cpp index a28f240..fffff4c 100644 --- a/src/postgresqlqueryresult.cpp +++ b/src/postgresqlqueryresult.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018, Tomasz Sowa + * Copyright (c) 2018-2019, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -161,6 +161,20 @@ bool PostgreSQLQueryResult::is_null(int row, int col) } +void PostgreSQLQueryResult::dump_column_names(PT::Log & log) +{ + if( psql_result ) + { + int cols = PQnfields(psql_result); + + for(int i = 0 ; i < cols ; ++i) + { + log << i << ' ' << PQfname(psql_result, i) << PT::Log::logend; + } + } +} + + //int PostgreSQLQueryResult::Rows(PGresult * r) //{ // // PQntuples - Returns the number of rows (tuples) in the query result. Because it returns diff --git a/src/postgresqlqueryresult.h b/src/postgresqlqueryresult.h index 3efa8f3..638bebf 100644 --- a/src/postgresqlqueryresult.h +++ b/src/postgresqlqueryresult.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018, Tomasz Sowa + * Copyright (c) 2018-2019, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -66,6 +66,7 @@ struct PostgreSQLQueryResult : public QueryResult bool is_null(int row, int col); + void dump_column_names(PT::Log & log); }; diff --git a/src/queryresult.cpp b/src/queryresult.cpp index 2e3c16b..da2c7b7 100644 --- a/src/queryresult.cpp +++ b/src/queryresult.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018, Tomasz Sowa + * Copyright (c) 2018-2019, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -120,6 +120,10 @@ bool QueryResult::is_null(int row, int col) } +void QueryResult::dump_column_names(PT::Log & log) +{ +} + } // namespace diff --git a/src/queryresult.h b/src/queryresult.h index 9ca13bb..5f22d8d 100644 --- a/src/queryresult.h +++ b/src/queryresult.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018, Tomasz Sowa + * Copyright (c) 2018-2019, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -35,6 +35,7 @@ #ifndef headerfile_morm_queryresult #define headerfile_morm_queryresult +#include #include @@ -74,6 +75,7 @@ struct QueryResult virtual int get_value_length(int row, int col); virtual bool is_null(int row, int col); + virtual void dump_column_names(PT::Log & log); };