Skip to content

Commit 8bb16d1

Browse files
committed
DB Schema: drag & drop SELECT queries
New check option in the contextual menu of the DB schema dock to allow drag & drop of SELECT queries, provided that only a table or fields of the same table are dragged from the DB Schema tree.
1 parent f5e0745 commit 8bb16d1

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

src/DbStructureModel.cpp

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ DbStructureModel::DbStructureModel(DBBrowserDB& db, QObject* parent)
1414
m_db(db),
1515
browsablesRootItem(nullptr),
1616
m_dropQualifiedNames(false),
17-
m_dropEnquotedNames(false)
17+
m_dropEnquotedNames(false),
18+
m_dropSelectQuery(true)
1819
{
1920
// Create root item and use its columns to store the header strings
2021
QStringList header;
@@ -211,6 +212,11 @@ QMimeData* DbStructureModel::mimeData(const QModelIndexList& indices) const
211212
// We store the SQL data and the names data separately
212213
QByteArray sqlData, namesData;
213214

215+
// For dropping SELECT queries, these variables take account of
216+
// whether all objects are of the same type and belong to the same table.
217+
QString objectTypeSet;
218+
QString tableSet;
219+
214220
// Loop through selected indices
215221
for(const QModelIndex& index : indices)
216222
{
@@ -220,14 +226,36 @@ QMimeData* DbStructureModel::mimeData(const QModelIndexList& indices) const
220226
// Only export data for valid indices and only once per row (SQL column or Name column).
221227
if(index.isValid()) {
222228
QString objectType = data(index.sibling(index.row(), ColumnObjectType), Qt::DisplayRole).toString();
229+
if (objectTypeSet.isEmpty() || objectTypeSet == objectType) {
230+
objectTypeSet = objectType;
231+
} else {
232+
objectTypeSet = "*";
233+
}
223234

224235
// For names, export a (qualified) (escaped) identifier of the item for statement composition in SQL editor.
225-
if(objectType == "field")
236+
if(objectType == "field") {
226237
namesData.append(getNameForDropping(item->text(ColumnSchema), item->parent()->text(ColumnName), item->text(ColumnName)).toUtf8());
227-
else if(objectType == "database")
238+
QString table = getNameForDropping(item->text(ColumnSchema), item->parent()->text(ColumnName), "");
239+
if (tableSet.isEmpty() || tableSet == table) {
240+
tableSet = table;
241+
} else {
242+
tableSet = "*";
243+
}
244+
} else if(objectType == "table") {
245+
QString table = getNameForDropping(item->text(ColumnSchema), item->text(ColumnName), "");
246+
namesData.append(table.toUtf8());
247+
if (tableSet.isEmpty() || tableSet == table) {
248+
tableSet = table;
249+
} else {
250+
tableSet = "*";
251+
}
252+
} else if(objectType == "database") {
253+
tableSet = "";
228254
namesData.append(getNameForDropping(item->text(ColumnName), "", "").toUtf8());
229-
else if(!objectType.isEmpty())
255+
} else if(!objectType.isEmpty()) {
256+
tableSet = "";
230257
namesData.append(getNameForDropping(item->text(ColumnSchema), item->text(ColumnName), "").toUtf8());
258+
}
231259

232260
if(objectType != "field" && index.column() == ColumnSQL)
233261
{
@@ -271,6 +299,16 @@ QMimeData* DbStructureModel::mimeData(const QModelIndexList& indices) const
271299
else if (namesData.endsWith("."))
272300
namesData.chop(1);
273301

302+
if (tableSet.endsWith("."))
303+
tableSet.chop(1);
304+
305+
if (m_dropSelectQuery && !tableSet.isEmpty() && tableSet != "*" && !objectTypeSet.isEmpty()) {
306+
if (objectTypeSet == "field") {
307+
namesData = ("SELECT " + QString::fromUtf8(namesData) + " FROM " + tableSet + ";").toUtf8();
308+
} else if (objectTypeSet == "table") {
309+
namesData = ("SELECT * FROM " + tableSet + ";").toUtf8();
310+
}
311+
}
274312
mime->setData("text/plain", namesData);
275313
} else
276314
mime->setData("text/plain", sqlData);

src/DbStructureModel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ public slots:
4040
void reloadData();
4141
void setDropQualifiedNames(bool value) { m_dropQualifiedNames = value; }
4242
void setDropEnquotedNames(bool value) { m_dropEnquotedNames = value; }
43+
void setDropSelectQuery(bool value) { m_dropSelectQuery = value; }
4344

4445
private:
4546
DBBrowserDB& m_db;
4647
QTreeWidgetItem* rootItem;
4748
QTreeWidgetItem* browsablesRootItem;
4849
bool m_dropQualifiedNames;
4950
bool m_dropEnquotedNames;
51+
bool m_dropSelectQuery;
5052

5153
void buildTree(QTreeWidgetItem* parent, const std::string& schema);
5254
QTreeWidgetItem* addNode(const std::string& schema, const std::string& name, const std::string& object_type, const std::string& sql, QTreeWidgetItem* parent_item, const std::string& data_type = {}, const std::string& icon_suffix = {});

src/MainWindow.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ void MainWindow::init()
250250
popupSchemaDockMenu->addAction(ui->actionPopupSchemaDockBrowseTable);
251251
popupSchemaDockMenu->addAction(ui->actionPopupSchemaDockDetachDatabase);
252252
popupSchemaDockMenu->addSeparator();
253+
popupSchemaDockMenu->addAction(ui->actionDropSelectQueryCheck);
253254
popupSchemaDockMenu->addAction(ui->actionDropQualifiedCheck);
254255
popupSchemaDockMenu->addAction(ui->actionEnquoteNamesCheck);
255256

@@ -433,10 +434,12 @@ void MainWindow::init()
433434
connect(ui->dbTreeWidget->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MainWindow::changeTreeSelection);
434435
connect(ui->dockEdit, &QDockWidget::visibilityChanged, this, &MainWindow::toggleEditDock);
435436
connect(remoteDock, SIGNAL(openFile(QString)), this, SLOT(fileOpen(QString)));
437+
connect(ui->actionDropSelectQueryCheck, &QAction::toggled, dbStructureModel, &DbStructureModel::setDropSelectQuery);
436438
connect(ui->actionDropQualifiedCheck, &QAction::toggled, dbStructureModel, &DbStructureModel::setDropQualifiedNames);
437439
connect(ui->actionEnquoteNamesCheck, &QAction::toggled, dbStructureModel, &DbStructureModel::setDropEnquotedNames);
438440
connect(&db, &DBBrowserDB::databaseInUseChanged, this, &MainWindow::updateDatabaseBusyStatus);
439441

442+
ui->actionDropSelectQueryCheck->setChecked(Settings::getValue("SchemaDock", "dropSelectQuery").toBool());
440443
ui->actionDropQualifiedCheck->setChecked(Settings::getValue("SchemaDock", "dropQualifiedNames").toBool());
441444
ui->actionEnquoteNamesCheck->setChecked(Settings::getValue("SchemaDock", "dropEnquotedNames").toBool());
442445

@@ -774,6 +777,7 @@ void MainWindow::closeEvent( QCloseEvent* event )
774777
Settings::setValue("MainWindow", "openTabs", saveOpenTabs());
775778

776779
Settings::setValue("SQLLogDock", "Log", ui->comboLogSubmittedBy->currentText());
780+
Settings::setValue("SchemaDock", "dropSelectQuery", ui->actionDropSelectQueryCheck->isChecked());
777781
Settings::setValue("SchemaDock", "dropQualifiedNames", ui->actionDropQualifiedCheck->isChecked());
778782
Settings::setValue("SchemaDock", "dropEnquotedNames", ui->actionEnquoteNamesCheck->isChecked());
779783

src/MainWindow.ui

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,6 +1991,20 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
19911991
<string>New In-&amp;Memory Database</string>
19921992
</property>
19931993
</action>
1994+
<action name="actionDropSelectQueryCheck">
1995+
<property name="checkable">
1996+
<bool>true</bool>
1997+
</property>
1998+
<property name="text">
1999+
<string>Drag &amp;&amp; Drop SELECT Query</string>
2000+
</property>
2001+
<property name="statusTip">
2002+
<string>When dragging fields from the same table or a single table, drop a SELECT query into the editor</string>
2003+
</property>
2004+
<property name="whatsThis">
2005+
<string>When dragging fields from the same table or a single table, drop a SELECT query into the editor</string>
2006+
</property>
2007+
</action>
19942008
<action name="actionDropQualifiedCheck">
19952009
<property name="checkable">
19962010
<bool>true</bool>

src/Settings.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ QVariant Settings::getDefaultValue(const std::string& group, const std::string&
425425
// SchemaDock Drag & drop settings
426426
if(group == "SchemaDock")
427427
{
428+
if(name == "dropSelectQuery")
429+
return true;
430+
428431
if(name == "dropQualifiedNames")
429432
return false;
430433

0 commit comments

Comments
 (0)