diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index d921b0f3..ab7b8f18 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,35 @@ 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.isNull()) + 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 no 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/SqlExecutionArea.cpp b/src/SqlExecutionArea.cpp index fc3db0af..810091f2 100644 --- a/src/SqlExecutionArea.cpp +++ b/src/SqlExecutionArea.cpp @@ -103,28 +103,6 @@ void SqlExecutionArea::saveAsCsv() dialog.exec(); } -void SqlExecutionArea::saveAsView() -{ - // 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(model->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 SqlExecutionArea::reloadSettings() { // Reload editor and table settings diff --git a/src/SqlExecutionArea.h b/src/SqlExecutionArea.h index ef68fc7a..b8c53326 100644 --- a/src/SqlExecutionArea.h +++ b/src/SqlExecutionArea.h @@ -33,7 +33,6 @@ public: public slots: virtual void finishExecution(const QString& result, const bool ok); virtual void saveAsCsv(); - virtual void saveAsView(); virtual void reloadSettings(); void fetchedData(); void setFindFrameVisibility(bool show); diff --git a/src/sqlitetablemodel.cpp b/src/sqlitetablemodel.cpp index 7cdc680e..124c22d8 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,10 @@ 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 columnId = sqlb::escapeIdentifier(m_headers.at(i.key())); + if(m_vDisplayFormat.size() && m_vDisplayFormat.at(i.key()-1) != columnId) + columnId = sqlb::escapeIdentifier(m_headers.at(i.key()) + "_"); + where.append(QString("%1 %2 AND ").arg(columnId).arg(i.value())); } // Remove last 'AND ' @@ -613,12 +616,21 @@ 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);