diff --git a/src/baseexpression.cpp b/src/baseexpression.cpp index 9bdf9c9..475cd20 100644 --- a/src/baseexpression.cpp +++ b/src/baseexpression.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018-2019, Tomasz Sowa + * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -204,37 +204,75 @@ bool BaseExpression::is_long_table_name(const PT::TextStream & table_name) } -bool BaseExpression::need_to_add_field_prefix(const wchar_t * field_name) -{ - return !is_long_field_name(field_name); -} - - void BaseExpression::put_field_name(const wchar_t * field_name, ModelEnv * model_env) { - before_field_name(); - - if( use_prefix && model_env ) - { - if( need_to_add_field_prefix(field_name) ) - { - esc(model_env->table_name_short, *out_stream); - - if( model_env->table_index > 1 ) - { - (*out_stream) << model_env->table_index; - } - - (*out_stream) << '.'; - } - } - - esc(field_name, *out_stream); - after_field_name(); + if( is_long_field_name(field_name) ) + put_long_field_name(field_name); + else + put_short_field_name(field_name, model_env); } +const wchar_t * BaseExpression::put_long_part_field_name(const wchar_t * field_name) +{ + while( *field_name != 0 && *field_name != '.' ) + { + esc(*field_name, *out_stream); + field_name += 1; + } + + return field_name; +} + + +void BaseExpression::put_long_field_name(const wchar_t * field_name) +{ + before_first_part_long_field_name(); + field_name = put_long_part_field_name(field_name); + after_first_part_long_field_name(); + + if( *field_name == '.' ) + { + field_name += 1; + (*out_stream) << '.'; + + before_second_part_long_field_name(); + field_name = put_long_part_field_name(field_name); + after_second_part_long_field_name(); + } +} + + +void BaseExpression::put_short_field_name(const wchar_t * field_name, ModelEnv * model_env) +{ + if( use_prefix && model_env ) + { + before_first_part_long_field_name(); + esc(model_env->table_name_short, *out_stream); + + if( model_env->table_index > 1 ) + { + (*out_stream) << model_env->table_index; + } + + after_first_part_long_field_name(); + + (*out_stream) << '.'; + + before_second_part_long_field_name(); + esc(field_name, *out_stream); + after_second_part_long_field_name(); + } + else + { + before_short_field_name(); + esc(field_name, *out_stream); + after_short_field_name(); + } + +} + void BaseExpression::save_foreign_key(const wchar_t * field_name, ModelEnv * model_env) { PT::TextStream str; @@ -253,16 +291,37 @@ void BaseExpression::save_foreign_key(const wchar_t * field_name, ModelEnv * mod } -void BaseExpression::before_field_name() +void BaseExpression::before_short_field_name() { } -void BaseExpression::after_field_name() +void BaseExpression::after_short_field_name() { } +void BaseExpression::before_first_part_long_field_name() +{ +} + + +void BaseExpression::after_first_part_long_field_name() +{ +} + + +void BaseExpression::before_second_part_long_field_name() +{ +} + + +void BaseExpression::after_second_part_long_field_name() +{ +} + + + void BaseExpression::before_field_value(const std::wstring &) { } @@ -330,6 +389,19 @@ void BaseExpression::esc(unsigned char val, PT::TextStream & stream) } +void BaseExpression::esc(wchar_t val, PT::TextStream & stream) +{ + char utf8_buf[10]; + + size_t utf8_len = PT::IntToUTF8((int)val, utf8_buf, sizeof(utf8_buf)); + + for(size_t a = 0 ; a < utf8_len ; ++a) + { + esc(utf8_buf[a], stream); + } +} + + void BaseExpression::esc(const std::wstring & val, PT::TextStream & stream) { char utf8_buf[10]; diff --git a/src/baseexpression.h b/src/baseexpression.h index c6e9534..66372cd 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018-2019, Tomasz Sowa + * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -212,6 +212,8 @@ public: virtual void esc(char val, PT::TextStream & stream); virtual void esc(unsigned char val, PT::TextStream & stream); + virtual void esc(wchar_t val, PT::TextStream & stream); + virtual void esc(const std::wstring & val, PT::TextStream & stream); virtual void esc(const wchar_t * val, PT::TextStream & stream); @@ -262,8 +264,6 @@ protected: //void field(const wchar_t * field_name, Model & field, bool insertable = true, bool updatable = true); - virtual bool need_to_add_field_prefix(const wchar_t * field_name); - virtual void put_field_name(const wchar_t * field_name, ModelEnv * model_env); virtual void save_foreign_key(const wchar_t * field_name, ModelEnv * model_env); @@ -580,8 +580,26 @@ protected: } - virtual void before_field_name(); - virtual void after_field_name(); + /* + * escaping column names in a case when using short form - just only column_name + * [before_short_field_name]column_name[after_short_field_name] + */ + virtual void before_short_field_name(); + virtual void after_short_field_name(); + + /* + * escaping column names in a case when using long form: table_name.column_name + * [before_first_part_long_field_name]table_name[after_first_part_long_field_name].[before_second_part_long_field_name]column_name[after_second_part_long_field_name] + * + */ + virtual void before_first_part_long_field_name(); + virtual void after_first_part_long_field_name(); + virtual void before_second_part_long_field_name(); + virtual void after_second_part_long_field_name(); + + virtual const wchar_t * put_long_part_field_name(const wchar_t * field_name); + virtual void put_long_field_name(const wchar_t * field_name); + virtual void put_short_field_name(const wchar_t * field_name, ModelEnv * model_env); virtual void before_field_value(const std::wstring &); virtual void after_field_value(const std::wstring &); diff --git a/src/jsonexpression.cpp b/src/jsonexpression.cpp index 1212c3f..e9ef3e9 100644 --- a/src/jsonexpression.cpp +++ b/src/jsonexpression.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018, Tomasz Sowa + * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -76,16 +76,35 @@ void JSONExpression::field_before() -void JSONExpression::before_field_name() +void JSONExpression::before_short_field_name() +{ + (*out_stream) << "\""; +} + +void JSONExpression::after_short_field_name() +{ + (*out_stream) << "\""; +} + +void JSONExpression::before_first_part_long_field_name() +{ + (*out_stream) << "\""; +} + +void JSONExpression::after_first_part_long_field_name() +{ +} + +void JSONExpression::before_second_part_long_field_name() +{ +} + +void JSONExpression::after_second_part_long_field_name() { (*out_stream) << "\""; } -void JSONExpression::after_field_name() -{ - (*out_stream) << "\""; -} void JSONExpression::before_field_value_string() diff --git a/src/jsonexpression.h b/src/jsonexpression.h index 6397407..79a91da 100644 --- a/src/jsonexpression.h +++ b/src/jsonexpression.h @@ -53,8 +53,14 @@ protected: void field_before(); - void before_field_name(); - void after_field_name(); + + void before_short_field_name(); + void after_short_field_name(); + void before_first_part_long_field_name(); + void after_first_part_long_field_name(); + void before_second_part_long_field_name(); + void after_second_part_long_field_name(); + void before_field_value(const std::wstring &); void before_field_value(const std::string &); void after_field_value(const std::wstring &); diff --git a/src/model.h b/src/model.h index 380975d..5da8e56 100644 --- a/src/model.h +++ b/src/model.h @@ -488,10 +488,13 @@ protected: { table_name(model_env->table_name); - (*log) << PT::Log::log1 << "Morm: incorrect type of a field in " << model_env->table_name << ", "; - put_fields_to_log(*log, db_field_name, flat_field_name); - (*log) << ", type expected " << typeid(field_value).name() - << " got " << helper.value_type_info->name() << PT::Log::logend; + if( log ) + { + (*log) << PT::Log::log1 << "Morm: incorrect type of a field in " << model_env->table_name << ", "; + put_fields_to_log(*log, db_field_name, flat_field_name); + (*log) << ", type expected " << typeid(field_value).name() + << " got " << helper.value_type_info->name() << PT::Log::logend; + } } } diff --git a/src/postgresqlexpression.cpp b/src/postgresqlexpression.cpp index 34a2fde..4235383 100644 --- a/src/postgresqlexpression.cpp +++ b/src/postgresqlexpression.cpp @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018, Tomasz Sowa + * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -39,6 +39,42 @@ namespace morm { +void PostgreSQLExpression::before_short_field_name() +{ + (*out_stream) << '"'; +} + +void PostgreSQLExpression::after_short_field_name() +{ + (*out_stream) << '"'; +} + + + +void PostgreSQLExpression::before_first_part_long_field_name() +{ +} + + +void PostgreSQLExpression::after_first_part_long_field_name() +{ +} + + +void PostgreSQLExpression::before_second_part_long_field_name() +{ + before_short_field_name(); +} + + +void PostgreSQLExpression::after_second_part_long_field_name() +{ + after_short_field_name(); +} + + + + void PostgreSQLExpression::before_field_value_string() { // if( output_type == MORM_OUTPUT_TYPE_DB_INSERT || diff --git a/src/postgresqlexpression.h b/src/postgresqlexpression.h index 562eca6..b26605b 100644 --- a/src/postgresqlexpression.h +++ b/src/postgresqlexpression.h @@ -5,7 +5,7 @@ */ /* - * Copyright (c) 2018, Tomasz Sowa + * Copyright (c) 2018-2021, Tomasz Sowa * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -51,6 +51,14 @@ public: protected: + virtual void before_short_field_name(); + virtual void after_short_field_name(); + + virtual void before_first_part_long_field_name(); + virtual void after_first_part_long_field_name(); + virtual void before_second_part_long_field_name(); + virtual void after_second_part_long_field_name(); + void before_field_value(const std::wstring &); void after_field_value(const std::wstring &);