Skip to content

Commit f1df40c

Browse files
committed
Prevent an error message when browsing views using SQLite 3.36 or above
Starting with SQLite 3.36 you cannot select the rowid column anymore when selecting from a view. Trying to do so will throw an error. Previously SQLite would silently return NULL when trying to do that. This commit mimicks SQLite's previous behaviour.
1 parent f61e983 commit f1df40c

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/TableBrowser.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,9 @@ void TableBrowser::updateRecordsetLabel()
753753

754754
sqlb::Query TableBrowser::buildQuery(const BrowseDataTableSettings& storedData, const sqlb::ObjectIdentifier& tablename) const
755755
{
756-
sqlb::Query query(tablename);
756+
const auto table = db->getTableByName(tablename);
757+
758+
sqlb::Query query(tablename, table && table->isView());
757759

758760
// Construct a query from the retrieved settings
759761

@@ -771,7 +773,6 @@ sqlb::Query TableBrowser::buildQuery(const BrowseDataTableSettings& storedData,
771773

772774
// Display formats
773775
bool only_defaults = true;
774-
const auto table = db->getTableByName(tablename);
775776
if(table)
776777
{
777778
// When there is at least one custom display format, we have to set all columns for the query explicitly here

src/sql/Query.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,13 @@ std::string Query::buildQuery(bool withRowid) const
8181
// If there is only one rowid column, we leave it as is.
8282
if(m_rowid_columns.size() == 1)
8383
{
84-
selector = sqlb::escapeIdentifier(m_rowid_columns.at(0)) + ",";
84+
// As of SQLite 3.36 when selecting rowid from a view SQLite does not return NULL anymore but instead returns "rowid" and throws
85+
// an error. To avoid that error (we don't actually care for the value of this column, so the value is not the issue here) we
86+
// explicitly select NULL (without any quotes) here.
87+
if(m_is_view && !hasCustomRowIdColumn())
88+
selector = "NULL,";
89+
else
90+
selector = sqlb::escapeIdentifier(m_rowid_columns.at(0)) + ",";
8591
} else {
8692
selector += "sqlb_make_single_value(";
8793
for(size_t i=0;i<m_rowid_columns.size();i++)

src/sql/Query.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ class Query
5757
{
5858
public:
5959
Query() {}
60-
explicit Query(const sqlb::ObjectIdentifier& table) :
61-
m_table(table)
60+
explicit Query(const sqlb::ObjectIdentifier& table, bool is_view = false) :
61+
m_table(table),
62+
m_is_view(is_view)
6263
{}
6364

6465
void clear();
@@ -98,6 +99,7 @@ class Query
9899
std::unordered_map<std::string, std::string> m_where; // TODO The two where variables should be merged into a single variable which ...
99100
std::vector<std::string> m_global_where; // ... holds some sort of general tree structure for all sorts of where conditions.
100101
std::vector<OrderBy> m_sort;
102+
bool m_is_view;
101103

102104
std::vector<SelectedColumn>::iterator findSelectedColumnByName(const std::string& name);
103105
std::vector<SelectedColumn>::const_iterator findSelectedColumnByName(const std::string& name) const;

0 commit comments

Comments
 (0)