From e5f7456863907e57bf0e578d0e488f5fc8fd555f Mon Sep 17 00:00:00 2001 From: mgrojo Date: Thu, 30 Nov 2017 20:41:21 +0100 Subject: [PATCH] Save current filter, sort column and display formats as a new view A new button is added to the Browse Data tab for saving the current display of the table (current filter, sort column and display formats) as a new view. This allows (specially for non advanced users) the creation of simple views. It can be seen, either as a way of storing the current filtering or as an easy way of creating views. This reuses the query set in sqlitetablemodel, but the column aliases when a display format is used has been changed from col%1 to the original column names, i.e. format(`orig`) AS `orig`. --- src/MainWindow.cpp | 35 ++++++++++++++++++++++++++++++++++- src/MainWindow.h | 2 ++ src/MainWindow.ui | 30 ++++++++++++++++++++++++++++++ src/sqlitetablemodel.cpp | 38 +++++++++++++++++++++++++------------- src/sqlitetablemodel.h | 2 ++ 5 files changed, 93 insertions(+), 14 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 3840fb4d..2200f315 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -1861,7 +1861,7 @@ void MainWindow::saveSqlResultsAsCsv() void MainWindow::saveSqlResultsAsView() { - qobject_cast(ui->tabSqlAreas->currentWidget())->saveAsView(); + saveAsView(qobject_cast(ui->tabSqlAreas->currentWidget())->getModel()->query()); } void MainWindow::loadExtension() @@ -2719,3 +2719,36 @@ void MainWindow::openFindReplaceDialog() findReplaceDialog->show(); } } + +void MainWindow::saveAsView(QString query) +{ + // Let the user select a name for the new view and make sure it doesn't already exist + QString name; + while(true) + { + name = QInputDialog::getText(this, qApp->applicationName(), tr("Please specify the view name")).trimmed(); + if(name.isEmpty()) + return; + if(db.getObjectByName(sqlb::ObjectIdentifier("main", name)) != nullptr) + QMessageBox::warning(this, qApp->applicationName(), tr("There is already an object with that name. Please choose a different name.")); + else + break; + } + + // Create the view + if(db.executeSQL(QString("CREATE VIEW %1 AS %2;").arg(sqlb::escapeIdentifier(name)).arg(query))) + QMessageBox::information(this, qApp->applicationName(), tr("View successfully created.")); + else + QMessageBox::warning(this, qApp->applicationName(), tr("Error creating view: %1").arg(db.lastError())); +} + + +void MainWindow::saveFilterAsView() +{ + if (m_browseTableModel->filterCount() > 0) + // Save as view a custom query without rowid + saveAsView(m_browseTableModel->customQuery(false)); + else + QMessageBox::information(this, qApp->applicationName(), tr("There is not any filter set for this table. View will not be created.")); + +} diff --git a/src/MainWindow.h b/src/MainWindow.h index d417d167..5e028648 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -190,6 +190,7 @@ private: void activateFields(bool enable = true); void enableEditing(bool enable_edit, bool enable_insertdelete); void loadExtensionsFromSettings(); + void saveAsView(QString query); sqlb::ObjectIdentifier currentlyBrowsedTableName() const; @@ -291,6 +292,7 @@ private slots: void renameSqlTab(int index); void setFindFrameVisibility(bool show); void openFindReplaceDialog(); + void saveFilterAsView(); }; #endif diff --git a/src/MainWindow.ui b/src/MainWindow.ui index 2589962a..f54e67b7 100644 --- a/src/MainWindow.ui +++ b/src/MainWindow.ui @@ -158,6 +158,20 @@ + + + + Save the current filter as a view + + + ... + + + + :/icons/save_table:/icons/save_table + + + @@ -3021,6 +3035,22 @@ + + buttonSaveFilterAsView + clicked() + MainWindow + saveFilterAsView() + + + 290 + 127 + + + 518 + 314 + + + fileOpen() diff --git a/src/sqlitetablemodel.cpp b/src/sqlitetablemodel.cpp index 7cdc680e..a48aa5c7 100644 --- a/src/sqlitetablemodel.cpp +++ b/src/sqlitetablemodel.cpp @@ -216,6 +216,11 @@ int SqliteTableModel::columnCount(const QModelIndex&) const return m_headers.size(); } +int SqliteTableModel::filterCount() const +{ + return m_mWhere.size(); +} + QVariant SqliteTableModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role != Qt::DisplayRole) @@ -590,7 +595,7 @@ void SqliteTableModel::fetchData(unsigned int from, unsigned to) }); } -void SqliteTableModel::buildQuery() +QString SqliteTableModel::customQuery(bool withRowid) { QString where; @@ -600,12 +605,8 @@ void SqliteTableModel::buildQuery() for(QMap::const_iterator i=m_mWhere.constBegin();i!=m_mWhere.constEnd();++i) { - QString column; - if(m_vDisplayFormat.size()) - column = QString("col%1").arg(i.key()); - else - column = m_headers.at(i.key()); - where.append(QString("%1 %2 AND ").arg(sqlb::escapeIdentifier(column)).arg(i.value())); + QString column = sqlb::escapeIdentifier(m_headers.at(i.key())); + where.append(QString("%1 %2 AND ").arg(column).arg(i.value())); } // Remove last 'AND ' @@ -613,12 +614,20 @@ void SqliteTableModel::buildQuery() } QString selector; + if (withRowid) + selector = sqlb::escapeIdentifier(m_headers.at(0)) + ","; + if(m_vDisplayFormat.empty()) { - selector = "*"; + selector += "*"; } else { - for(int i=0;i &display_format = QVector()); void setChunkSize(size_t chunksize); void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);