Skip to content

Commit

Permalink
Fix handling of custom rowids/pseudo PKs
Browse files Browse the repository at this point in the history
This should avoid double queries in the Browse Data tab for views and
for tables.
  • Loading branch information
MKleusberg committed Dec 26, 2018
1 parent 4f9c21f commit 7f597d0
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/sql/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Query::Query()
void Query::clear()
{
m_table.clear();
m_rowid_column = "rowid";
m_rowid_column = "_rowid_";
m_selected_columns.clear();
m_where.clear();
m_sort.clear();
Expand Down
1 change: 1 addition & 0 deletions src/sql/Query.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Query

void setRowIdColumn(const std::string& rowid) { m_rowid_column = rowid; }
std::string rowIdColumn() const { return m_rowid_column; }
bool hasCustomRowIdColumn() const { return m_rowid_column != "rowid" && m_rowid_column != "_rowid_"; }

const std::vector<SelectedColumn>& selectedColumns() const { return m_selected_columns; }
std::vector<SelectedColumn>& selectedColumns() { return m_selected_columns; }
Expand Down
26 changes: 11 additions & 15 deletions src/sqlitetablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ void SqliteTableModel::setQuery(const sqlb::Query& query)
{
QString sColumnQuery = QString::fromUtf8("SELECT * FROM %1;").arg(query.table().toString());
if(m_query.rowIdColumn().empty())
m_query.setRowIdColumn("rowid");
m_headers.push_back("rowid");
m_query.setRowIdColumn("_rowid_");
m_headers.push_back("_rowid_");
m_headers.append(getColumns(nullptr, sColumnQuery, m_vDataTypes));
}

Expand Down Expand Up @@ -782,36 +782,32 @@ bool SqliteTableModel::dropMimeData(const QMimeData* data, Qt::DropAction, int r
return false;
}

void SqliteTableModel::setPseudoPk(const QString& pseudoPk)
void SqliteTableModel::setPseudoPk(QString pseudoPk)
{
if(pseudoPk.isNull())
pseudoPk = QString("_rowid_");

// Do nothing if the value didn't change
if(m_query.rowIdColumn() == pseudoPk.toStdString())
return;

if(pseudoPk.isEmpty())
{
m_query.rowIdColumn().clear();
if(m_headers.size())
m_headers[0] = QString("rowid");
} else {
m_query.setRowIdColumn(pseudoPk.toStdString());
if(m_headers.size())
m_headers[0] = pseudoPk;
}
m_query.setRowIdColumn(pseudoPk.toStdString());
if(m_headers.size())
m_headers[0] = pseudoPk;

buildQuery();
}

bool SqliteTableModel::hasPseudoPk() const
{
return !(m_query.rowIdColumn() == "rowid" || m_query.rowIdColumn() == "_rowid_");
return m_query.hasCustomRowIdColumn();
}

bool SqliteTableModel::isEditable() const
{
return !m_query.table().isEmpty() &&
m_db.isOpen() &&
((m_db.getObjectByName(m_query.table()) && m_db.getObjectByName(m_query.table())->type() == sqlb::Object::Types::Table) || !m_query.rowIdColumn().empty());
((m_db.getObjectByName(m_query.table()) && m_db.getObjectByName(m_query.table())->type() == sqlb::Object::Types::Table) || m_query.hasCustomRowIdColumn());
}

void SqliteTableModel::triggerCacheLoad (int row) const
Expand Down
2 changes: 1 addition & 1 deletion src/sqlitetablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class SqliteTableModel : public QAbstractTableModel
QString encoding() const { return m_encoding; }

// The pseudo-primary key is exclusively for editing views
void setPseudoPk(const QString& pseudoPk);
void setPseudoPk(QString pseudoPk);
bool hasPseudoPk() const;
QString pseudoPk() const { return QString::fromStdString(m_query.rowIdColumn()); }

Expand Down

0 comments on commit 7f597d0

Please sign in to comment.