diff options
Diffstat (limited to 'SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.cpp')
| -rw-r--r-- | SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.cpp | 94 |
1 files changed, 57 insertions, 37 deletions
diff --git a/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.cpp b/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.cpp index ba8ccc1..85a7047 100644 --- a/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.cpp +++ b/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.cpp @@ -26,10 +26,10 @@ #include "windows/editorwindow.h" #include "uiconfig.h" #include "themetuner.h" -#include "dialogs/dbconverterdialog.h" #include "querygenerator.h" #include "dialogs/execfromfiledialog.h" #include "dialogs/fileexecerrorsdialog.h" +#include "common/compatibility.h" #include <QApplication> #include <QClipboard> #include <QAction> @@ -44,6 +44,7 @@ #include <QDesktopServices> #include <QDir> #include <QFileDialog> +#include <QElapsedTimer> #include <QtConcurrent/QtConcurrentRun> CFG_KEYS_DEFINE(DbTree) @@ -104,6 +105,7 @@ void DbTree::init() connect(this, &DbTree::updateFileExecProgress, this, &DbTree::setFileExecProgress, Qt::QueuedConnection); connect(this, &DbTree::fileExecCoverToBeClosed, this, &DbTree::hideFileExecCover, Qt::QueuedConnection); connect(this, &DbTree::fileExecErrors, this, &DbTree::showFileExecErrors, Qt::QueuedConnection); + connect(this, SIGNAL(schemaNeedsRefreshing(Db*)), this, SLOT(refreshSchema(Db*)), Qt::QueuedConnection); treeModel = new DbTreeModel(); treeModel->setTreeView(ui->treeView); @@ -144,7 +146,6 @@ void DbTree::createActions() createAction(DISCONNECT_FROM_DB, ICONS.DATABASE_DISCONNECT, tr("&Disconnect from the database"), this, SLOT(disconnectFromDb()), this); createAction(IMPORT_INTO_DB, ICONS.IMPORT, tr("Import"), this, SLOT(import()), this); createAction(EXPORT_DB, ICONS.DATABASE_EXPORT, tr("&Export the database"), this, SLOT(exportDb()), this); - createAction(CONVERT_DB, ICONS.CONVERT_DB, tr("Con&vert database type"), this, SLOT(convertDb()), this); createAction(VACUUM_DB, ICONS.VACUUM_DB, tr("Vac&uum"), this, SLOT(vacuumDb()), this); createAction(INTEGRITY_CHECK, ICONS.INTEGRITY_CHECK, tr("&Integrity check"), this, SLOT(integrityCheck()), this); createAction(ADD_TABLE, ICONS.TABLE_ADD, tr("Create a &table"), this, SLOT(addTable()), this); @@ -207,7 +208,7 @@ void DbTree::updateActionStates(const QStandardItem *item) enabled << DELETE_DB << EDIT_DB; if (dbTreeItem->getDb()->isOpen()) { - enabled << DISCONNECT_FROM_DB << ADD_TABLE << ADD_VIEW << IMPORT_INTO_DB << EXPORT_DB << REFRESH_SCHEMA << CONVERT_DB + enabled << DISCONNECT_FROM_DB << IMPORT_INTO_DB << EXPORT_DB << REFRESH_SCHEMA << VACUUM_DB << INTEGRITY_CHECK; isDbOpen = true; } @@ -340,8 +341,14 @@ void DbTree::updateActionStates(const QStandardItem *item) } if (treeModel->rowCount() > 0) + { enabled << SELECT_ALL; // if there's at least 1 item, enable this + // Table/view always enabled, as long as there is at least 1 db on the list. #4017 + if (treeModel->findFirstItemOfType(DbTreeItem::Type::DB)) + enabled << ADD_TABLE << ADD_VIEW; + } + enabled << REFRESH_SCHEMAS; for (int action : actionMap.keys()) @@ -413,7 +420,6 @@ void DbTree::setupActionsForMenu(DbTreeItem* currItem, QMenu* contextMenu) actions += ActionEntry(REFRESH_SCHEMA); actions += ActionEntry(IMPORT_INTO_DB); actions += ActionEntry(EXPORT_DB); - actions += ActionEntry(CONVERT_DB); actions += ActionEntry(VACUUM_DB); actions += ActionEntry(INTEGRITY_CHECK); actions += ActionEntry(EXEC_SQL_FROM_FILE); @@ -715,7 +721,7 @@ bool DbTree::areDbTreeItemsValidForItem(QList<DbTreeItem*> srcItems, const DbTre {DbTreeItem::Type::INDEX, DbTreeItem::Type::INDEXES} }; - if (!forPasting && srcTypes.toSet().size() == 1 && srcDbs.size() == 1 && dstItem && + if (!forPasting && toSet(srcTypes).size() == 1 && srcDbs.size() == 1 && dstItem && *(srcDbs.begin()) == dstItem->getDb() && reorderingTypeToParent[srcTypes.first()] == dstType) return true; @@ -794,7 +800,7 @@ Db* DbTree::getSelectedOpenDb() TableWindow* DbTree::openTable(DbTreeItem* item) { - QString database = QString::null; // TODO implement this when named databases (attached) are handled by dbtree. + QString database = QString(); // TODO implement this when named databases (attached) are handled by dbtree. Db* db = item->getDb(); return openTable(db, database, item->text()); } @@ -807,7 +813,7 @@ TableWindow* DbTree::openTable(Db* db, const QString& database, const QString& t void DbTree::editIndex(DbTreeItem* item) { - //QString database = QString::null; // TODO implement this when named databases (attached) are handled by dbtree. + //QString database = QString(); // TODO implement this when named databases (attached) are handled by dbtree. Db* db = item->getDb(); DbObjectDialogs dialogs(db); @@ -816,7 +822,7 @@ void DbTree::editIndex(DbTreeItem* item) ViewWindow* DbTree::openView(DbTreeItem* item) { - QString database = QString::null; // TODO implement this when named databases (attached) are handled by dbtree. + QString database = QString(); // TODO implement this when named databases (attached) are handled by dbtree. Db* db = item->getDb(); return openView(db, database, item->text()); } @@ -827,25 +833,21 @@ ViewWindow* DbTree::openView(Db* db, const QString& database, const QString& vie return dialogs.editView(database, view); } -TableWindow* DbTree::newTable(DbTreeItem* item) +TableWindow* DbTree::newTable(Db* db) { - Db* db = item->getDb(); - DbObjectDialogs dialogs(db); return dialogs.addTable(); } -ViewWindow* DbTree::newView(DbTreeItem* item) +ViewWindow* DbTree::newView(Db* db) { - Db* db = item->getDb(); - DbObjectDialogs dialogs(db); return dialogs.addView(); } void DbTree::editTrigger(DbTreeItem* item) { - //QString database = QString::null; // TODO implement this when named databases (attached) are handled by dbtree. + //QString database = QString(); // TODO implement this when named databases (attached) are handled by dbtree. Db* db = item->getDb(); DbObjectDialogs dialogs(db); @@ -1161,7 +1163,7 @@ void DbTree::editDb() void DbTree::removeDb() { - QList<Db*> dbList = getSelectedDatabases().toList(); + QList<Db*> dbList = getSelectedDatabases().values(); if (dbList.isEmpty()) return; @@ -1248,10 +1250,16 @@ void DbTree::addTable() { Db* db = getSelectedOpenDb(); if (!db || !db->isValid()) + { + DbTreeItem* item = treeModel->findFirstItemOfType(DbTreeItem::Type::DB); + if (item) + db = item->getDb(); + } + + if (!db || !db->isValid()) return; - DbTreeItem* item = ui->treeView->currentItem(); - newTable(item); + newTable(db); } void DbTree::editTable() @@ -1267,7 +1275,7 @@ void DbTree::editTable() return; } - openTable(db, QString::null, table); // TODO put database name when supported + openTable(db, QString(), table); // TODO put database name when supported } void DbTree::delTable() @@ -1340,10 +1348,16 @@ void DbTree::addView() { Db* db = getSelectedOpenDb(); if (!db || !db->isValid()) + { + DbTreeItem* item = treeModel->findFirstItemOfType(DbTreeItem::Type::DB); + if (item) + db = item->getDb(); + } + + if (!db || !db->isValid()) return; - DbTreeItem* item = ui->treeView->currentItem(); - newView(item); + newView(db); } void DbTree::editView() @@ -1463,17 +1477,6 @@ void DbTree::delColumn() delColumn(item); } -void DbTree::convertDb() -{ - Db* db = getSelectedDb(); - if (!db || !db->isValid()) - return; - - DbConverterDialog dialog(this); - dialog.setDb(db); - dialog.exec(); -} - void DbTree::vacuumDb() { Db* db = getSelectedDb(); @@ -1569,11 +1572,10 @@ void DbTree::eraseTableData() return; static_qstring(DELETE_SQL, "DELETE FROM %1;"); - Dialect dialect = db->getDialect(); SqlQueryPtr res; for (const QString& table : tables) { - res = db->exec(DELETE_SQL.arg(wrapObjIfNeeded(table, dialect))); + res = db->exec(DELETE_SQL.arg(wrapObjIfNeeded(table))); if (res->isError()) { notifyError(tr("An error occurred while trying to delete data from table '%1': %2").arg(table, res->getErrorText())); @@ -1898,7 +1900,7 @@ void DbTree::execFromFileAsync(const QString& path, Db* db, bool ignoreErrors, c int executed = 0; bool ok = true; - QTime timer; + QElapsedTimer timer; timer.start(); QList<QPair<QString, QString>> errors = executeFileQueries(db, stream, executed, attemptedExecutions, ok, ignoreErrors, fileSize); int millis = timer.elapsed(); @@ -1935,8 +1937,11 @@ QList<QPair<QString, QString>> DbTree::executeFileQueries(Db* db, QTextStream& s } } - if (sql.trimmed().isEmpty()) + if (shouldSkipQueryFromFileExecution(sql)) + { + sql.clear();; continue; + } results = db->exec(sql); attemptedExecutions++; @@ -1961,6 +1966,18 @@ QList<QPair<QString, QString>> DbTree::executeFileQueries(Db* db, QTextStream& s return errors; } +bool DbTree::shouldSkipQueryFromFileExecution(const QString& sql) +{ + if (sql.trimmed().isEmpty()) + return true; + + QString upper = sql.toUpper().trimmed().split("\n").last().trimmed(); + return (upper.startsWith("BEGIN") || + upper.startsWith("COMMIT") || + upper.startsWith("ROLLBACK") || + upper.startsWith("END")); +} + void DbTree::handleFileQueryExecution(Db* db, int executed, int attemptedExecutions, bool ok, bool ignoreErrors, int millis) { bool doCommit = ok ? true : ignoreErrors; @@ -1968,17 +1985,20 @@ void DbTree::handleFileQueryExecution(Db* db, int executed, int attemptedExecuti { if (!db->commit()) { - db->rollback(); notifyError(tr("Could not execute SQL, because application has failed to commit the transaction: %1").arg(db->getErrorText())); + db->rollback(); } else if (!ok) // committed with errors { notifyInfo(tr("Finished executing %1 queries in %2 seconds. %3 were not executed due to errors.") .arg(executed).arg(millis / 1000.0).arg(attemptedExecutions - executed)); + + emit schemaNeedsRefreshing(db); } else { notifyInfo(tr("Finished executing %1 queries in %2 seconds.").arg(executed).arg(millis / 1000.0)); + emit schemaNeedsRefreshing(db); } } else |
