mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 02:50:46 -06:00
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`.
This commit is contained in:
@@ -1861,7 +1861,7 @@ void MainWindow::saveSqlResultsAsCsv()
|
||||
|
||||
void MainWindow::saveSqlResultsAsView()
|
||||
{
|
||||
qobject_cast<SqlExecutionArea*>(ui->tabSqlAreas->currentWidget())->saveAsView();
|
||||
saveAsView(qobject_cast<SqlExecutionArea*>(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."));
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user