Unify handling of views and tables in SqliteTableModel a bit

This also avoids querying the column names another time when browsing a
view.
This commit is contained in:
Martin Kleusberg
2021-01-28 22:42:39 +01:00
parent 18c7e9c477
commit 13ab455d5c
3 changed files with 29 additions and 33 deletions
+3 -3
View File
@@ -444,7 +444,7 @@ StringVector Table::rowidColumns() const
FieldInfoList Table::fieldInformation() const
{
FieldInfoList result;
std::transform(fields.begin(), fields.end(), std::back_inserter(result), [](const auto& f) { return FieldInfo(f.name(), f.type(), f.toString(" ", " ")); });
std::transform(fields.begin(), fields.end(), std::back_inserter(result), [](const auto& f) { return FieldInfo(f.name(), f.type(), f.toString(" ", " "), f.affinity()); });
return result;
}
@@ -675,7 +675,7 @@ std::string Index::sql(const std::string& schema, bool ifNotExists) const
FieldInfoList Index::fieldInformation() const
{
FieldInfoList result;
std::transform(fields.begin(), fields.end(), std::back_inserter(result), [](const auto& c) { return FieldInfo(c.name(), c.order(), c.toString(" ", " ")); });
std::transform(fields.begin(), fields.end(), std::back_inserter(result), [](const auto& c) { return FieldInfo(c.name(), c.order(), c.toString(" ", " "), Field::IntegerAffinity); });
return result;
}
@@ -714,7 +714,7 @@ StringVector View::fieldNames() const
FieldInfoList View::fieldInformation() const
{
FieldInfoList result;
std::transform(fields.begin(), fields.end(), std::back_inserter(result), [](const auto& f) { return FieldInfo(f.name(), f.type(), f.toString(" ", " ")); });
std::transform(fields.begin(), fields.end(), std::back_inserter(result), [](const auto& f) { return FieldInfo(f.name(), f.type(), f.toString(" ", " "), f.affinity()); });
return result;
}
+12 -11
View File
@@ -73,17 +73,6 @@ using IndexedColumnVector = std::vector<IndexedColumn>;
using ConstraintSet = std::set<ConstraintPtr>;
using FieldInfoList = std::vector<FieldInfo>;
struct FieldInfo
{
FieldInfo(const std::string& name_, const std::string& type_, const std::string& sql_)
: name(name_), type(type_), sql(sql_)
{}
std::string name;
std::string type;
std::string sql;
};
class Object
{
public:
@@ -431,6 +420,18 @@ private:
std::shared_ptr<GeneratedColumnConstraint> m_generated;
};
struct FieldInfo
{
FieldInfo(const std::string& name_, const std::string& type_, const std::string& sql_, Field::Affinity affinity_)
: name(name_), type(type_), sql(sql_), affinity(affinity_)
{}
std::string name;
std::string type;
std::string sql;
Field::Affinity affinity;
};
class Table : public Object
{
public:
+14 -19
View File
@@ -126,32 +126,27 @@ void SqliteTableModel::setQuery(const sqlb::Query& query)
// Save the query
m_query = query;
m_table_of_query = m_db.getObjectByName<sqlb::Table>(query.table());
// Get the data types of all other columns as well as the column names
// Retrieve field names and types
sqlb::ObjectPtr object = m_db.getObjectByName(query.table());
// Set the row id columns
if(m_table_of_query && m_table_of_query->fields.size()) // It is a table and parsing was OK
m_table_of_query = std::dynamic_pointer_cast<sqlb::Table>(object);
if(m_table_of_query) // It is a table
{
sqlb::StringVector rowids = m_table_of_query->rowidColumns();
m_query.setRowIdColumns(rowids);
m_vDataTypes.emplace_back(SQLITE_INTEGER); // TODO This is not necessarily true for tables without ROWID or with multiple PKs
m_headers.push_back(sqlb::joinStringVector(rowids, ","));
// Store field names and affinity data types
for(const sqlb::Field& fld : m_table_of_query->fields)
{
m_headers.push_back(fld.name());
m_vDataTypes.push_back(fld.affinity());
}
m_query.setRowIdColumns(m_table_of_query->rowidColumns());
} else {
// 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
// information we retrieve it from SQLite using an extra query.
if(m_query.rowIdColumns().empty())
m_query.setRowIdColumn("_rowid_");
}
m_vDataTypes.emplace_back(SQLITE_INTEGER); // TODO This is not necessarily true for tables without ROWID or with multiple PKs
m_headers.push_back(sqlb::joinStringVector(m_query.rowIdColumns(), ","));
getColumnNames("SELECT _rowid_,* FROM " + query.table().toString());
// Store field names and affinity data types
for(const auto& fld : object->fieldInformation())
{
m_headers.push_back(fld.name);
m_vDataTypes.push_back(fld.affinity);
}
// Tell the query object about the column names