Fix possible crash when sorted column does not exist anymore

When sorting a table or a view by a column, then removing enough columns
from that table, that view, or the underlying table of the view so that
the column index get out of bound, and then going back to browse that
table the application crashes. This commit makes sure to ignore such
columns which would cause a crash.

See issue #1774.
This commit is contained in:
Martin Kleusberg
2019-03-06 11:59:34 +01:00
parent db17cd9e4f
commit 61e13aec03

View File

@@ -66,13 +66,16 @@ std::string Query::buildQuery(bool withRowid) const
// Sorting
std::string order_by;
if(m_sort.size())
for(const auto& sorted_column : m_sort)
{
order_by = "ORDER BY ";
for(const auto& sorted_column : m_sort)
if(sorted_column.column < m_column_names.size())
order_by += sqlb::escapeIdentifier(m_column_names.at(sorted_column.column)) + " "
+ (sorted_column.direction == sqlb::Ascending ? "ASC" : "DESC") + ",";
}
if(order_by.size())
{
order_by.pop_back();
order_by = "ORDER BY " + order_by;
}
return "SELECT " + selector + " FROM " + m_table.toString().toStdString() + " " + where + " " + order_by;