allow to set a custom name for the auto generated rows counter column

This commit is contained in:
Tomasz Sowa 2023-02-28 09:51:05 +01:00
parent c56bae57ca
commit d61fc31b5c
Signed by: tomasz.sowa
GPG Key ID: 662CC1438638588B
2 changed files with 40 additions and 10 deletions

View File

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2018-2022, Tomasz Sowa
* Copyright (c) 2018-2023, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -350,19 +350,29 @@ void DbExpression::add_rows_counter_column(Model & model)
{
field_before();
(*out_stream) << "COUNT(*) OVER() AS ";
before_field_name();
pt::TextStream str;
generate_rows_counter_column_name(*model.model_env, str);
if( !model.model_env->has_autogenerated_select )
if( model.model_env->rows_counter_column_name.empty() )
{
str.to_str(model.model_env->rows_counter_column_name);
pt::TextStream str;
generate_rows_counter_column_name(*model.model_env, str);
esc(str, *out_stream);
/*
* for autogenerated selects we don't have to copy the rows_counter_column_name
* because a field() method will use an index instead of this name
*/
if( !model.model_env->has_autogenerated_select )
{
str.to_str(model.model_env->rows_counter_column_name);
}
}
else
{
esc(model.model_env->rows_counter_column_name, *out_stream);
}
before_field_name();
esc(str, *out_stream);
after_field_name();
field_after();
}
}

View File

@ -63,6 +63,7 @@ public:
db_expression = nullptr;
was_query_error = false;
model_data = nullptr;
rows_counter_name = nullptr;
}
@ -73,6 +74,7 @@ public:
db_expression = nullptr;
was_query_error = false;
model_data = nullptr;
rows_counter_name = nullptr;
}
Finder(ModelConnector & model_connector)
@ -82,6 +84,7 @@ public:
db_expression = nullptr;
was_query_error = false;
model_data = nullptr;
rows_counter_name = nullptr;
set_out_stream();
}
@ -92,6 +95,7 @@ public:
db_expression = nullptr;
was_query_error = false;
model_data = nullptr;
rows_counter_name = nullptr;
set_out_stream();
}
@ -102,6 +106,7 @@ public:
db_expression = nullptr;
was_query_error = false;
model_data = nullptr;
rows_counter_name = nullptr;
}
Finder(pt::TextStream & out_stream, ModelConnector * model_connector)
@ -111,6 +116,7 @@ public:
db_expression = nullptr;
was_query_error = false;
model_data = nullptr;
rows_counter_name = nullptr;
}
Finder<ModelClass> & set_connector(ModelConnector * model_connector)
@ -151,6 +157,17 @@ public:
return *this;
}
Finder<ModelClass> & set_rows_counter_name(const wchar_t * rows_counter_name)
{
this->rows_counter_name = rows_counter_name;
return *this;
}
Finder<ModelClass> & set_rows_counter_name(const std::wstring & rows_counter_name)
{
this->rows_counter_name = &rows_counter_name;
return *this;
}
bool was_error()
{
@ -1228,6 +1245,9 @@ protected:
model_env.finder_helper = &finder_helper;
model_env.model = &model;
if( rows_counter_name )
model_env.rows_counter_column_name = rows_counter_name;
model.model_env = &model_env;
model.table();
model.model_env->add_table_name_to_finder_helper();
@ -1340,7 +1360,7 @@ private:
FinderHelper finder_helper;
ModelData * model_data;
bool use_table_prefix_for_fetching;
const wchar_t * rows_counter_name;
void set_db_expression()