in BaseExpression: changed the way how field names are escaped:

methods removed:
  virtual void before_field_name();
  virtual void after_field_name();
methods added:
  virtual void before_short_field_name();
  virtual void after_short_field_name();
  they are used for escaping column names in a case when using short form - just only column_name
  e.g.: [before_short_field_name]column_name[after_short_field_name]
methods added:
  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();
  they are used for escaping column names in a case when using long form: table_name.column_name
  e.g.: [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]
methods added:
  virtual void esc(wchar_t val, PT::TextStream & stream);
This commit is contained in:
Tomasz Sowa 2021-02-24 01:15:17 +01:00
parent 0843e384eb
commit ff551a64b8
7 changed files with 209 additions and 47 deletions

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2018-2019, Tomasz Sowa * Copyright (c) 2018-2021, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * 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) void BaseExpression::put_field_name(const wchar_t * field_name, ModelEnv * model_env)
{ {
before_field_name(); if( is_long_field_name(field_name) )
put_long_field_name(field_name);
if( use_prefix && model_env ) else
{ put_short_field_name(field_name, 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();
} }
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) void BaseExpression::save_foreign_key(const wchar_t * field_name, ModelEnv * model_env)
{ {
PT::TextStream str; 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 &) 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) void BaseExpression::esc(const std::wstring & val, PT::TextStream & stream)
{ {
char utf8_buf[10]; char utf8_buf[10];

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2018-2019, Tomasz Sowa * Copyright (c) 2018-2021, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * 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(char val, PT::TextStream & stream);
virtual void esc(unsigned 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 std::wstring & val, PT::TextStream & stream);
virtual void esc(const wchar_t * 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); //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 put_field_name(const wchar_t * field_name, ModelEnv * model_env);
virtual void save_foreign_key(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 before_field_value(const std::wstring &);
virtual void after_field_value(const std::wstring &); virtual void after_field_value(const std::wstring &);

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2018, Tomasz Sowa * Copyright (c) 2018-2021, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * 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) << "\""; (*out_stream) << "\"";
} }
void JSONExpression::after_field_name()
{
(*out_stream) << "\"";
}
void JSONExpression::before_field_value_string() void JSONExpression::before_field_value_string()

View File

@ -53,8 +53,14 @@ protected:
void field_before(); 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::wstring &);
void before_field_value(const std::string &); void before_field_value(const std::string &);
void after_field_value(const std::wstring &); void after_field_value(const std::wstring &);

View File

@ -488,10 +488,13 @@ protected:
{ {
table_name(model_env->table_name); table_name(model_env->table_name);
(*log) << PT::Log::log1 << "Morm: incorrect type of a field in " << model_env->table_name << ", "; if( log )
put_fields_to_log(*log, db_field_name, flat_field_name); {
(*log) << ", type expected " << typeid(field_value).name() (*log) << PT::Log::log1 << "Morm: incorrect type of a field in " << model_env->table_name << ", ";
<< " got " << helper.value_type_info->name() << PT::Log::logend; 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;
}
} }
} }

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2018, Tomasz Sowa * Copyright (c) 2018-2021, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * 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() void PostgreSQLExpression::before_field_value_string()
{ {
// if( output_type == MORM_OUTPUT_TYPE_DB_INSERT || // if( output_type == MORM_OUTPUT_TYPE_DB_INSERT ||

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2018, Tomasz Sowa * Copyright (c) 2018-2021, Tomasz Sowa
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -51,6 +51,14 @@ public:
protected: 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 before_field_value(const std::wstring &);
void after_field_value(const std::wstring &); void after_field_value(const std::wstring &);