Skip to content

Commit 13ab455

Browse files
committed
Unify handling of views and tables in SqliteTableModel a bit
This also avoids querying the column names another time when browsing a view.
1 parent 18c7e9c commit 13ab455

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
lines changed

src/sql/sqlitetypes.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ StringVector Table::rowidColumns() const
444444
FieldInfoList Table::fieldInformation() const
445445
{
446446
FieldInfoList result;
447-
std::transform(fields.begin(), fields.end(), std::back_inserter(result), [](const auto& f) { return FieldInfo(f.name(), f.type(), f.toString(" ", " ")); });
447+
std::transform(fields.begin(), fields.end(), std::back_inserter(result), [](const auto& f) { return FieldInfo(f.name(), f.type(), f.toString(" ", " "), f.affinity()); });
448448
return result;
449449
}
450450

@@ -675,7 +675,7 @@ std::string Index::sql(const std::string& schema, bool ifNotExists) const
675675
FieldInfoList Index::fieldInformation() const
676676
{
677677
FieldInfoList result;
678-
std::transform(fields.begin(), fields.end(), std::back_inserter(result), [](const auto& c) { return FieldInfo(c.name(), c.order(), c.toString(" ", " ")); });
678+
std::transform(fields.begin(), fields.end(), std::back_inserter(result), [](const auto& c) { return FieldInfo(c.name(), c.order(), c.toString(" ", " "), Field::IntegerAffinity); });
679679
return result;
680680
}
681681

@@ -714,7 +714,7 @@ StringVector View::fieldNames() const
714714
FieldInfoList View::fieldInformation() const
715715
{
716716
FieldInfoList result;
717-
std::transform(fields.begin(), fields.end(), std::back_inserter(result), [](const auto& f) { return FieldInfo(f.name(), f.type(), f.toString(" ", " ")); });
717+
std::transform(fields.begin(), fields.end(), std::back_inserter(result), [](const auto& f) { return FieldInfo(f.name(), f.type(), f.toString(" ", " "), f.affinity()); });
718718
return result;
719719
}
720720

src/sql/sqlitetypes.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,6 @@ using IndexedColumnVector = std::vector<IndexedColumn>;
7373
using ConstraintSet = std::set<ConstraintPtr>;
7474
using FieldInfoList = std::vector<FieldInfo>;
7575

76-
struct FieldInfo
77-
{
78-
FieldInfo(const std::string& name_, const std::string& type_, const std::string& sql_)
79-
: name(name_), type(type_), sql(sql_)
80-
{}
81-
82-
std::string name;
83-
std::string type;
84-
std::string sql;
85-
};
86-
8776
class Object
8877
{
8978
public:
@@ -431,6 +420,18 @@ class Field
431420
std::shared_ptr<GeneratedColumnConstraint> m_generated;
432421
};
433422

423+
struct FieldInfo
424+
{
425+
FieldInfo(const std::string& name_, const std::string& type_, const std::string& sql_, Field::Affinity affinity_)
426+
: name(name_), type(type_), sql(sql_), affinity(affinity_)
427+
{}
428+
429+
std::string name;
430+
std::string type;
431+
std::string sql;
432+
Field::Affinity affinity;
433+
};
434+
434435
class Table : public Object
435436
{
436437
public:

src/sqlitetablemodel.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,32 +126,27 @@ void SqliteTableModel::setQuery(const sqlb::Query& query)
126126

127127
// Save the query
128128
m_query = query;
129-
m_table_of_query = m_db.getObjectByName<sqlb::Table>(query.table());
130129

131-
// Get the data types of all other columns as well as the column names
130+
// Retrieve field names and types
131+
sqlb::ObjectPtr object = m_db.getObjectByName(query.table());
132+
132133
// Set the row id columns
133-
if(m_table_of_query && m_table_of_query->fields.size()) // It is a table and parsing was OK
134+
m_table_of_query = std::dynamic_pointer_cast<sqlb::Table>(object);
135+
if(m_table_of_query) // It is a table
134136
{
135-
sqlb::StringVector rowids = m_table_of_query->rowidColumns();
136-
m_query.setRowIdColumns(rowids);
137-
138-
m_vDataTypes.emplace_back(SQLITE_INTEGER); // TODO This is not necessarily true for tables without ROWID or with multiple PKs
139-
m_headers.push_back(sqlb::joinStringVector(rowids, ","));
140-
141-
// Store field names and affinity data types
142-
for(const sqlb::Field& fld : m_table_of_query->fields)
143-
{
144-
m_headers.push_back(fld.name());
145-
m_vDataTypes.push_back(fld.affinity());
146-
}
137+
m_query.setRowIdColumns(m_table_of_query->rowidColumns());
147138
} else {
148-
// If for one reason or another (either it's a view or we couldn't parse the table statement) we couldn't get the field
149-
// information we retrieve it from SQLite using an extra query.
150-
151139
if(m_query.rowIdColumns().empty())
152140
m_query.setRowIdColumn("_rowid_");
141+
}
142+
m_vDataTypes.emplace_back(SQLITE_INTEGER); // TODO This is not necessarily true for tables without ROWID or with multiple PKs
143+
m_headers.push_back(sqlb::joinStringVector(m_query.rowIdColumns(), ","));
153144

154-
getColumnNames("SELECT _rowid_,* FROM " + query.table().toString());
145+
// Store field names and affinity data types
146+
for(const auto& fld : object->fieldInformation())
147+
{
148+
m_headers.push_back(fld.name);
149+
m_vDataTypes.push_back(fld.affinity);
155150
}
156151

157152
// Tell the query object about the column names

0 commit comments

Comments
 (0)