From 2afe111c57d85e4c161386d1b28d79c6ea5be065 Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Fri, 30 Apr 2021 01:23:22 +0200 Subject: [PATCH] escape table names in Finder (select sql statement) WIP: #2 --- src/baseexpression.h | 65 ++++++++++++++++++++++++++++++++++++ src/finder.h | 19 +++++++---- src/postgresqlexpression.cpp | 6 ++-- 3 files changed, 82 insertions(+), 8 deletions(-) diff --git a/src/baseexpression.h b/src/baseexpression.h index 5a829d4..c5e00c0 100644 --- a/src/baseexpression.h +++ b/src/baseexpression.h @@ -121,6 +121,8 @@ public: } + + template void put_field_value_or_null(const FieldValue & field_value, FT field_type, ModelEnv * model_env) { @@ -218,6 +220,69 @@ public: } + void table_to_stream(PT::TextStream & stream, const wchar_t * schema_name, const wchar_t * table_name, ModelEnv * model_env) + { + this->out_stream = &stream; + //table(table_name, model_env); + + before_first_part_long_field_name(); + esc(schema_name, *out_stream); + after_first_part_long_field_name(); + + (*out_stream) << '.'; + + before_second_part_long_field_name(); + esc(table_name, *out_stream); + after_second_part_long_field_name(); + + this->out_stream = nullptr; + } + + void table_to_stream(PT::TextStream & stream, const PT::WTextStream & schema_name, const PT::WTextStream & table_name, ModelEnv * model_env) + { + this->out_stream = &stream; + //table(table_name, model_env); + + before_first_part_long_field_name(); + esc(schema_name, *out_stream); + after_first_part_long_field_name(); + + (*out_stream) << '.'; + + before_second_part_long_field_name(); + esc(table_name, *out_stream); + after_second_part_long_field_name(); + + this->out_stream = nullptr; + } + + void table_to_stream(PT::TextStream & stream, const wchar_t * table_name, ModelEnv * model_env) + { + this->out_stream = &stream; + //table(table_name, model_env); + + if( is_long_field_name(table_name) ) + put_long_field_name(table_name); + else + put_short_field_name(table_name, model_env); + + this->out_stream = nullptr; + } + + + void table_to_stream(PT::TextStream & stream, const PT::WTextStream & table_name, ModelEnv * model_env) + { + this->out_stream = &stream; + //table(table_name, model_env); + + before_short_field_name(); + esc(table_name, *out_stream); + after_short_field_name(); + + this->out_stream = nullptr; + } + + /* * IMPLEMENT ME * esc for: signed char, wchar_t, char16_t, char32_t diff --git a/src/finder.h b/src/finder.h index 89af0cf..8d936b7 100644 --- a/src/finder.h +++ b/src/finder.h @@ -240,14 +240,21 @@ public: if( !model.model_env->schema_name.empty() ) { - (*out_stream) << model.model_env->schema_name << "."; + db_expression->table_to_stream(*out_stream, model.model_env->schema_name, model.model_env->table_name, &model_env); + } + else + { + db_expression->table_to_stream(*out_stream, model.model_env->table_name, &model_env); } - // IMPROVEME escape table name - (*out_stream) << model.model_env->table_name << " AS "; - (*out_stream) << model.model_env->table_name; - (*out_stream) << " "; - (*out_stream) << finder_helper.join_tables_str; + (*out_stream) << " AS "; + db_expression->table_to_stream(*out_stream, model.model_env->table_name, &model_env); + + if( !finder_helper.join_tables_str.empty() ) + { + (*out_stream) << " "; + (*out_stream) << finder_helper.join_tables_str; + } } return *this; diff --git a/src/postgresqlexpression.cpp b/src/postgresqlexpression.cpp index 5a5b2d7..4da9ef6 100644 --- a/src/postgresqlexpression.cpp +++ b/src/postgresqlexpression.cpp @@ -53,23 +53,25 @@ void PostgreSQLExpression::after_short_field_name() void PostgreSQLExpression::before_first_part_long_field_name() { + (*out_stream) << '"'; } void PostgreSQLExpression::after_first_part_long_field_name() { + (*out_stream) << '"'; } void PostgreSQLExpression::before_second_part_long_field_name() { - before_short_field_name(); + (*out_stream) << '"'; } void PostgreSQLExpression::after_second_part_long_field_name() { - after_short_field_name(); + (*out_stream) << '"'; }