add support for declared cursors

Declare a cursor with the Finder::declare_cursor() method and next get the cursor with get_cursor():
morm::Cursor<MyObject> cursor = finder.
  declare_cursor("mycursorname").
  select().
  raw("ORDER BY column_name ASC").
  get_cursor();

The cursor now can be used with fetch.*() methods and then get() or get_list():
MyObject myobject = cursor.fetch_next().get();
std::list<MyObject> myobjects = cursor.fetch_forward_count(10).get_list();
This commit is contained in:
2023-07-16 04:03:03 +02:00
parent 6619f3ecb5
commit 21f12a8a98
8 changed files with 448 additions and 80 deletions

View File

@@ -63,9 +63,11 @@ public:
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;
last_query_status = c.last_query_status;
select_flags = c.select_flags;
rows_counter = c.rows_counter;
cursor_name = c.cursor_name;
scroll_cursor = c.scroll_cursor;
if( query_result )
{
@@ -107,9 +109,11 @@ public:
cursor_helper.clear();
finder_helper.clear();
query_result = nullptr;
select_status = false;
last_query_status = false;
select_flags = Select::default_type;
rows_counter = 0;
cursor_name.clear();
scroll_cursor = false;
}
@@ -131,9 +135,9 @@ public:
}
virtual void set_select_status(bool select_status)
virtual void set_last_query_status(bool status)
{
this->select_status = select_status;
this->last_query_status = status;
}
@@ -171,7 +175,7 @@ public:
{
bool has = false;
if( model_connector && query_result && query_result->has_db_result() && select_status )
if( model_connector && query_result && query_result->has_db_result() && last_query_status )
{
has = query_result->cur_row < query_result->result_rows;
}
@@ -234,7 +238,7 @@ public:
}
result.before_select();
res = select_status;
res = last_query_status;
if( res )
{
@@ -326,6 +330,136 @@ public:
}
virtual void set_cursor_name(pt::TextStream & cursor_name)
{
this->cursor_name = cursor_name;
}
virtual void set_scroll_cursor(bool scroll_cursor)
{
this->scroll_cursor = scroll_cursor;
}
virtual Cursor & fetch(const char * fetch_str)
{
last_query_status = false;
rows_counter = 0;
if( query_result )
{
query_result->clear();
if( model_connector )
{
DbConnector * db_connector = model_connector->get_db_connector();
if( db_connector )
{
bool status = db_connector->query_select(fetch_str, *query_result);
last_query_status = status;
}
}
}
return *this;
}
virtual Cursor & fetch(pt::TextStream & str)
{
if( str.size() < 256 )
{
char str_buf[256];
if( str.to_str(str_buf, sizeof(str_buf) / sizeof(char)) )
{
fetch(str_buf);
}
}
else
{
put_cursor_fetch_query_too_long();
}
return *this;
}
virtual Cursor & fetch(pt::WTextStream & str)
{
if( str.size() < 256 )
{
char str_buf[256];
if( str.to_str(str_buf, sizeof(str_buf) / sizeof(char)) )
{
fetch(str_buf);
}
}
else
{
put_cursor_fetch_query_too_long();
}
return *this;
}
virtual Cursor & fetch_next()
{
return fetch(&DbExpression::prepare_fetch_next_query);
}
virtual Cursor & fetch_prior()
{
return fetch(&DbExpression::prepare_fetch_prior_query);
}
virtual Cursor & fetch_first()
{
return fetch(&DbExpression::prepare_fetch_first_query);
}
virtual Cursor & fetch_last()
{
return fetch(&DbExpression::prepare_fetch_last_query);
}
virtual Cursor & fetch_absolute(long position)
{
return fetch(&DbExpression::prepare_fetch_absotule_query, position);
}
virtual Cursor & fetch_relative(long position)
{
return fetch(&DbExpression::prepare_fetch_relative_query, position);
}
virtual Cursor & fetch_forward_count(size_t len)
{
return fetch(&DbExpression::prepare_fetch_forward_count_query, len);
}
virtual Cursor & fetch_backward_count(size_t len)
{
return fetch(&DbExpression::prepare_fetch_backward_count_query, len);
}
virtual Cursor & fetch_all()
{
return fetch(&DbExpression::prepare_fetch_all_query);
}
protected:
@@ -336,11 +470,98 @@ protected:
CursorHelper cursor_helper;
FinderHelper finder_helper; // may CursorHelper and FinderHelper should be one class?
QueryResult * query_result;
bool select_status;
bool last_query_status;
Select select_flags;
size_t rows_counter;
std::wstring rows_counter_column_name;
pt::TextStream cursor_name;
bool scroll_cursor;
typedef void (DbExpression::*prepare_fetch_method_type)(const pt::TextStream & cursor_name, pt::TextStream & out_stream);
typedef void (DbExpression::*prepare_fetch_long_count_method_type)(const pt::TextStream & cursor_name, long count, pt::TextStream & out_stream);
typedef void (DbExpression::*prepare_fetch_size_count_method_type)(const pt::TextStream & cursor_name, size_t count, pt::TextStream & out_stream);
DbExpression * get_db_expression()
{
if( model_connector )
{
DbConnector * db_connector = model_connector->get_db_connector();
if( db_connector )
{
return db_connector->get_expression();
}
}
return nullptr;
}
virtual Cursor & fetch(prepare_fetch_method_type prepare_fetch_method)
{
if( !cursor_name.empty() )
{
DbExpression * db_expression = get_db_expression();
if( db_expression )
{
pt::TextStream str;
(db_expression->*prepare_fetch_method)(cursor_name, str);
fetch(str);
}
}
else
{
put_cursor_not_declared_log();
}
return *this;
}
virtual Cursor & fetch(prepare_fetch_long_count_method_type prepare_fetch_method, long count)
{
if( !cursor_name.empty() )
{
DbExpression * db_expression = get_db_expression();
if( db_expression )
{
pt::TextStream str;
(db_expression->*prepare_fetch_method)(cursor_name, count, str);
fetch(str);
}
}
else
{
put_cursor_not_declared_log();
}
return *this;
}
virtual Cursor & fetch(prepare_fetch_size_count_method_type prepare_fetch_method, size_t count)
{
if( !cursor_name.empty() )
{
DbExpression * db_expression = get_db_expression();
if( db_expression )
{
pt::TextStream str;
(db_expression->*prepare_fetch_method)(cursor_name, count, str);
fetch(str);
}
}
else
{
put_cursor_not_declared_log();
}
return *this;
}
template<typename ContainerType>
@@ -359,7 +580,7 @@ protected:
if( db_connector )
{
res = select_status;
res = last_query_status;
try
{
@@ -448,6 +669,38 @@ protected:
}
pt::Log * get_logger()
{
if( model_connector )
{
return model_connector->get_logger();
}
return nullptr;
}
void put_log(pt::Log::Manipulators level, const char * msg)
{
pt::Log * plog = get_logger();
if( plog )
{
(*plog) << level << msg << pt::Log::logend;
}
}
void put_cursor_fetch_query_too_long()
{
put_log(pt::Log::log2, "Morm: error: a fetch query should be less than 256 characters long");
}
void put_cursor_not_declared_log()
{
put_log(pt::Log::log2, "Morm: you cannot use fetch* methods, a cursor has not been declared, use Finder::declare_cursor(...) to declare a cursor");
}
};