aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/guiSQLiteStudio/dbtree
diff options
context:
space:
mode:
Diffstat (limited to 'SQLiteStudio3/guiSQLiteStudio/dbtree')
-rw-r--r--SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.cpp38
-rw-r--r--SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.h3
-rw-r--r--SQLiteStudio3/guiSQLiteStudio/dbtree/dbtreemodel.cpp29
3 files changed, 64 insertions, 6 deletions
diff --git a/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.cpp b/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.cpp
index 509594d..1aeff0f 100644
--- a/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.cpp
+++ b/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.cpp
@@ -136,6 +136,7 @@ void DbTree::createActions()
createAction(CLEAR_FILTER, tr("Clear filter"), ui->nameFilter, SLOT(clear()), this);
createAction(REFRESH_SCHEMAS, ICONS.DATABASE_RELOAD, tr("Refresh all database schemas"), this, SLOT(refreshSchemas()), this);
createAction(REFRESH_SCHEMA, ICONS.DATABASE_RELOAD, tr("Refresh selected database schema"), this, SLOT(refreshSchema()), this);
+ createAction(ERASE_TABLE_DATA, ICONS.ERASE_TABLE_DATA, tr("Erase table data"), this, SLOT(eraseTableData()), this);
}
void DbTree::updateActionStates(const QStandardItem *item)
@@ -189,7 +190,7 @@ void DbTree::updateActionStates(const QStandardItem *item)
break;
case DbTreeItem::Type::TABLE:
enabled << EDIT_TABLE << DEL_TABLE << EXPORT_TABLE << IMPORT_TABLE << POPULATE_TABLE << ADD_COLUMN << CREATE_SIMILAR_TABLE;
- enabled << RESET_AUTOINCREMENT << ADD_INDEX << ADD_TRIGGER;
+ enabled << RESET_AUTOINCREMENT << ADD_INDEX << ADD_TRIGGER << ERASE_TABLE_DATA;
break;
case DbTreeItem::Type::VIRTUAL_TABLE:
// TODO change below when virtual tables can be edited
@@ -393,6 +394,7 @@ void DbTree::setupActionsForMenu(DbTreeItem* currItem, QMenu* contextMenu)
actions += ActionEntry(POPULATE_TABLE);
actions += ActionEntry(CREATE_SIMILAR_TABLE);
actions += ActionEntry(RESET_AUTOINCREMENT);
+ actions += ActionEntry(ERASE_TABLE_DATA);
actions += ActionEntry(_separator);
actions += dbEntryExt;
break;
@@ -1399,6 +1401,35 @@ void DbTree::resetAutoincrement()
notifyInfo(tr("Autoincrement value for table '%1' has been reset successfly.").arg(table));
}
+void DbTree::eraseTableData()
+{
+ Db* db = getSelectedDb();
+ if (!db || !db->isValid())
+ return;
+
+ DbTreeItem* item = ui->treeView->currentItem();
+ QString table = item->getTable();
+ if (table.isNull())
+ {
+ qWarning() << "Tried to erase table data, while table wasn't selected in DbTree.";
+ return;
+ }
+
+ QMessageBox::StandardButton btn = QMessageBox::question(this, tr("Erase table data"), tr("Are you sure you want to delete all data from table '%1'?")
+ .arg(table));
+ if (btn != QMessageBox::Yes)
+ return;
+
+ SqlQueryPtr res = db->exec(QString("DELETE FROM %1;").arg(wrapObjIfNeeded(table, db->getDialect())));
+ if (res->isError())
+ {
+ notifyError(tr("An error occurred while trying to delete data from table '%1': %2").arg(table, res->getErrorText()));
+ return;
+ }
+
+ notifyInfo(tr("All data has been deleted for table '%1'.").arg(table));
+}
+
void DbTree::addColumn(DbTreeItem* item)
{
Db* db = getSelectedOpenDb();
@@ -1591,6 +1622,11 @@ void DbTree::setupDefShortcuts()
BIND_SHORTCUTS(DbTree, Action);
}
+void DbTree::closeEvent(QCloseEvent *e)
+{
+ e->ignore();
+}
+
int qHash(DbTree::Action action)
{
return static_cast<int>(action);
diff --git a/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.h b/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.h
index 60b8dd5..2f5583e 100644
--- a/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.h
+++ b/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtree.h
@@ -84,6 +84,7 @@ class GUI_API_EXPORT DbTree : public QDockWidget, public ExtActionContainer
REFRESH_SCHEMA,
CREATE_SIMILAR_TABLE,
RESET_AUTOINCREMENT,
+ ERASE_TABLE_DATA,
_separator // Never use it directly, it's just for menu setup
};
@@ -116,6 +117,7 @@ class GUI_API_EXPORT DbTree : public QDockWidget, public ExtActionContainer
protected:
void createActions();
void setupDefShortcuts();
+ void closeEvent(QCloseEvent* e);
private:
void setActionEnabled(int action, bool enabled);
@@ -187,6 +189,7 @@ class GUI_API_EXPORT DbTree : public QDockWidget, public ExtActionContainer
void integrityCheck();
void createSimilarTable();
void resetAutoincrement();
+ void eraseTableData();
void addColumn(DbTreeItem* item);
void editColumn(DbTreeItem* item);
void delColumn(DbTreeItem* item);
diff --git a/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtreemodel.cpp b/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtreemodel.cpp
index a4e736f..78f0db8 100644
--- a/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtreemodel.cpp
+++ b/SQLiteStudio3/guiSQLiteStudio/dbtree/dbtreemodel.cpp
@@ -250,8 +250,19 @@ void DbTreeModel::restoreGroup(const Config::DbGroupPtr& group, QList<Db*>* dbLi
{
if (db)
{
- if (db->open())
+ // If the db was stored in cfg as open, it was already open by DbManager.
+ // Now the DbTreeModel didn't catch that (as it didn't exist yet), so we need to
+ // call handler for 'connected' event, instead of forcing another open call.
+ // Otherwise the database that could not be open would be requested to open twice:
+ // 1. when restoring DbManager
+ // 2. here
+ // Instead of that, we just check if the database is already open (by DbManager)
+ // and call proper handler to refresh database's schema and create tree nodes.
+ if (db->isOpen())
+ {
+ dbConnected(db);
treeView->expand(item->index());
+ }
}
else
{
@@ -401,8 +412,12 @@ QString DbTreeModel::getDbToolTip(DbTreeItem* item) const
QStringList rows;
Db* db = item->getDb();
- QFile dbFile(db->getPath());
QString iconPath = db->isValid() ? ICONS.DATABASE.toImgSrc() : ICONS.DATABASE_INVALID.toImgSrc();
+ int fileSize = -1;
+
+ QUrl url(db->getPath());
+ if (url.scheme().isEmpty() || url.scheme() == "file")
+ fileSize = QFile(db->getPath()).size();
rows << toolTipHdrRowTmp.arg(iconPath).arg(tr("Database: %1", "dbtree tooltip").arg(db->getName()));
rows << toolTipRowTmp.arg("URI:").arg(db->getPath());
@@ -410,13 +425,17 @@ QString DbTreeModel::getDbToolTip(DbTreeItem* item) const
if (db->isValid())
{
rows << toolTipRowTmp.arg(tr("Version:", "dbtree tooltip")).arg(QString("SQLite %1").arg(db->getVersion()));
- rows << toolTipRowTmp.arg(tr("File size:", "dbtree tooltip")).arg(formatFileSize(dbFile.size()));
- rows << toolTipRowTmp.arg(tr("Encoding:", "dbtree tooltip")).arg(db->getEncoding());
+
+ if (fileSize > -1)
+ rows << toolTipRowTmp.arg(tr("File size:", "dbtree tooltip")).arg(formatFileSize(fileSize));
+
+ if (db->isOpen())
+ rows << toolTipRowTmp.arg(tr("Encoding:", "dbtree tooltip")).arg(db->getEncoding());
}
else
{
InvalidDb* idb = dynamic_cast<InvalidDb*>(db);
- rows << toolTipRowTmp.arg(tr("Error details:", "dbtree tooltip")).arg(idb->getError());
+ rows << toolTipRowTmp.arg(tr("Error:", "dbtree tooltip")).arg(idb->getError());
}
return toolTipTableTmp.arg(rows.join(""));