Add new menu item to show the row counts of all tables

This adds a new action to the Tools menu which creates a new SQL tab
with a statement to count the number of rows of all tables and views.

See issue #2464.
This commit is contained in:
Martin Kleusberg
2020-11-25 23:12:43 +01:00
parent 4a4757575d
commit efb9f9c9e0
3 changed files with 57 additions and 4 deletions

View File

@@ -1834,6 +1834,7 @@ void MainWindow::activateFields(bool enable)
ui->actionQuickCheck->setEnabled(enable);
ui->actionForeignKeyCheck->setEnabled(enable);
ui->actionOptimize->setEnabled(enable);
ui->actionRowCounts->setEnabled(enable);
ui->dockEdit->setEnabled(enable);
ui->dockPlot->setEnabled(enable);
@@ -3384,13 +3385,20 @@ void MainWindow::runSqlNewTab(const QString& query, const QString& title, const
else
message = tr("This action will open a new SQL tab with the following statements for you to edit and run:");
message += QString("<blockquote><tt>%1</tt></blockquote>").arg(query) +
tr("Press Help for opening the corresponding SQLite reference page.");
message += QString("<blockquote><tt>%1</tt></blockquote>").arg(query.size() > 1000 ? query.left(1000) + "\n[...]" : query);
if(!helpUrl.isEmpty())
message += tr("Press Help for opening the corresponding SQLite reference page.");
QString windowTitle = title;
windowTitle.remove('&');
auto result = QMessageBox::information(this,
windowTitle,
message,
QMessageBox::Ok | QMessageBox::Default,
QMessageBox::Cancel | QMessageBox::Escape,
helpUrl.isEmpty() ? 0 : QMessageBox::Help);
switch (QMessageBox::information(this, windowTitle, message, QMessageBox::Ok | QMessageBox::Default, QMessageBox::Cancel | QMessageBox::Escape, QMessageBox::Help))
switch (result)
{
case QMessageBox::Ok: {
if (ui->mainTab->indexOf(ui->query) == -1)
@@ -3763,3 +3771,21 @@ QList<TableBrowserDock*> MainWindow::allTableBrowserDocks() const
{
return ui->tabBrowsers->findChildren<TableBrowserDock*>();
}
void MainWindow::newRowCountsTab()
{
QString sql;
for(const auto& it : db.schemata["main"])
{
if(it.second->type() == sqlb::Object::Table || it.second->type() == sqlb::Object::View)
{
sql += QString("SELECT %1 AS \"name\", %2 AS \"type\", COUNT(*) AS \"rows\" FROM %3\nUNION ").arg(
QString::fromStdString(sqlb::escapeString(it.second->name())),
QString::fromStdString(sqlb::escapeString(it.second->typeToString(it.second->type()))),
QString::fromStdString(sqlb::escapeIdentifier(it.second->name())));
}
}
sql.chop(7); // Remove the last "\nUNION " at the end
runSqlNewTab(sql, ui->actionRowCounts->text());
}

View File

@@ -217,7 +217,7 @@ private slots:
void openFindReplaceDialog();
void toggleSqlBlockComment();
void openSqlPrintDialog();
void runSqlNewTab(const QString& query, const QString& title, const QString& helpUrl, const bool autoRun = true);
void runSqlNewTab(const QString& query, const QString& title, const QString& helpUrl = QString(), const bool autoRun = true);
void printDbStructure();
void updateDatabaseBusyStatus(bool busy, const QString& user);
void openPreferences();
@@ -226,6 +226,7 @@ private slots:
void saveSqlFile(int tabIndex);
void saveAll();
void openUrlOrFile(const QString& urlString);
void newRowCountsTab();
int openSqlTab(bool resetCounter = false);
void closeSqlTab(int index, bool force = false, bool askSaving = true);

View File

@@ -852,6 +852,7 @@ You can drag SQL statements from an object row and drop them into other applicat
<addaction name="actionQuickCheck"/>
<addaction name="actionForeignKeyCheck"/>
<addaction name="actionOptimize"/>
<addaction name="actionRowCounts"/>
</widget>
<addaction name="fileMenu"/>
<addaction name="editMenu"/>
@@ -2228,6 +2229,14 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
<string>This treats the current clipboard contents as a CSV file and opens the same import wizard that is used for importing CSV data from a file.</string>
</property>
</action>
<action name="actionRowCounts">
<property name="text">
<string>Show Row Counts</string>
</property>
<property name="toolTip">
<string>This shows the number of rows for each table and view in the database.</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
@@ -3666,6 +3675,22 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
</hint>
</hints>
</connection>
<connection>
<sender>actionRowCounts</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>newRowCountsTab()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>518</x>
<y>314</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>fileOpen()</slot>
@@ -3727,5 +3752,6 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
<slot>changeTableBrowserTab(int)</slot>
<slot>renameTableBrowserTab(int)</slot>
<slot>showContextMenuTableBrowserTabBar(QPoint)</slot>
<slot>newRowCountsTab()</slot>
</slots>
</ui>