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.
This commit is contained in:
mgrojo
2022-06-20 17:40:24 +02:00
parent f5e074528d
commit 8bb16d1dd0
5 changed files with 65 additions and 4 deletions

View File

@@ -14,7 +14,8 @@ DbStructureModel::DbStructureModel(DBBrowserDB& db, QObject* parent)
m_db(db),
browsablesRootItem(nullptr),
m_dropQualifiedNames(false),
m_dropEnquotedNames(false)
m_dropEnquotedNames(false),
m_dropSelectQuery(true)
{
// Create root item and use its columns to store the header strings
QStringList header;
@@ -211,6 +212,11 @@ QMimeData* DbStructureModel::mimeData(const QModelIndexList& indices) const
// We store the SQL data and the names data separately
QByteArray sqlData, namesData;
// For dropping SELECT queries, these variables take account of
// whether all objects are of the same type and belong to the same table.
QString objectTypeSet;
QString tableSet;
// Loop through selected indices
for(const QModelIndex& index : indices)
{
@@ -220,14 +226,36 @@ QMimeData* DbStructureModel::mimeData(const QModelIndexList& indices) const
// Only export data for valid indices and only once per row (SQL column or Name column).
if(index.isValid()) {
QString objectType = data(index.sibling(index.row(), ColumnObjectType), Qt::DisplayRole).toString();
if (objectTypeSet.isEmpty() || objectTypeSet == objectType) {
objectTypeSet = objectType;
} else {
objectTypeSet = "*";
}
// For names, export a (qualified) (escaped) identifier of the item for statement composition in SQL editor.
if(objectType == "field")
if(objectType == "field") {
namesData.append(getNameForDropping(item->text(ColumnSchema), item->parent()->text(ColumnName), item->text(ColumnName)).toUtf8());
else if(objectType == "database")
QString table = getNameForDropping(item->text(ColumnSchema), item->parent()->text(ColumnName), "");
if (tableSet.isEmpty() || tableSet == table) {
tableSet = table;
} else {
tableSet = "*";
}
} else if(objectType == "table") {
QString table = getNameForDropping(item->text(ColumnSchema), item->text(ColumnName), "");
namesData.append(table.toUtf8());
if (tableSet.isEmpty() || tableSet == table) {
tableSet = table;
} else {
tableSet = "*";
}
} else if(objectType == "database") {
tableSet = "";
namesData.append(getNameForDropping(item->text(ColumnName), "", "").toUtf8());
else if(!objectType.isEmpty())
} else if(!objectType.isEmpty()) {
tableSet = "";
namesData.append(getNameForDropping(item->text(ColumnSchema), item->text(ColumnName), "").toUtf8());
}
if(objectType != "field" && index.column() == ColumnSQL)
{
@@ -271,6 +299,16 @@ QMimeData* DbStructureModel::mimeData(const QModelIndexList& indices) const
else if (namesData.endsWith("."))
namesData.chop(1);
if (tableSet.endsWith("."))
tableSet.chop(1);
if (m_dropSelectQuery && !tableSet.isEmpty() && tableSet != "*" && !objectTypeSet.isEmpty()) {
if (objectTypeSet == "field") {
namesData = ("SELECT " + QString::fromUtf8(namesData) + " FROM " + tableSet + ";").toUtf8();
} else if (objectTypeSet == "table") {
namesData = ("SELECT * FROM " + tableSet + ";").toUtf8();
}
}
mime->setData("text/plain", namesData);
} else
mime->setData("text/plain", sqlData);

View File

@@ -40,6 +40,7 @@ public slots:
void reloadData();
void setDropQualifiedNames(bool value) { m_dropQualifiedNames = value; }
void setDropEnquotedNames(bool value) { m_dropEnquotedNames = value; }
void setDropSelectQuery(bool value) { m_dropSelectQuery = value; }
private:
DBBrowserDB& m_db;
@@ -47,6 +48,7 @@ private:
QTreeWidgetItem* browsablesRootItem;
bool m_dropQualifiedNames;
bool m_dropEnquotedNames;
bool m_dropSelectQuery;
void buildTree(QTreeWidgetItem* parent, const std::string& schema);
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 = {});

View File

@@ -250,6 +250,7 @@ void MainWindow::init()
popupSchemaDockMenu->addAction(ui->actionPopupSchemaDockBrowseTable);
popupSchemaDockMenu->addAction(ui->actionPopupSchemaDockDetachDatabase);
popupSchemaDockMenu->addSeparator();
popupSchemaDockMenu->addAction(ui->actionDropSelectQueryCheck);
popupSchemaDockMenu->addAction(ui->actionDropQualifiedCheck);
popupSchemaDockMenu->addAction(ui->actionEnquoteNamesCheck);
@@ -433,10 +434,12 @@ void MainWindow::init()
connect(ui->dbTreeWidget->selectionModel(), &QItemSelectionModel::selectionChanged, this, &MainWindow::changeTreeSelection);
connect(ui->dockEdit, &QDockWidget::visibilityChanged, this, &MainWindow::toggleEditDock);
connect(remoteDock, SIGNAL(openFile(QString)), this, SLOT(fileOpen(QString)));
connect(ui->actionDropSelectQueryCheck, &QAction::toggled, dbStructureModel, &DbStructureModel::setDropSelectQuery);
connect(ui->actionDropQualifiedCheck, &QAction::toggled, dbStructureModel, &DbStructureModel::setDropQualifiedNames);
connect(ui->actionEnquoteNamesCheck, &QAction::toggled, dbStructureModel, &DbStructureModel::setDropEnquotedNames);
connect(&db, &DBBrowserDB::databaseInUseChanged, this, &MainWindow::updateDatabaseBusyStatus);
ui->actionDropSelectQueryCheck->setChecked(Settings::getValue("SchemaDock", "dropSelectQuery").toBool());
ui->actionDropQualifiedCheck->setChecked(Settings::getValue("SchemaDock", "dropQualifiedNames").toBool());
ui->actionEnquoteNamesCheck->setChecked(Settings::getValue("SchemaDock", "dropEnquotedNames").toBool());
@@ -774,6 +777,7 @@ void MainWindow::closeEvent( QCloseEvent* event )
Settings::setValue("MainWindow", "openTabs", saveOpenTabs());
Settings::setValue("SQLLogDock", "Log", ui->comboLogSubmittedBy->currentText());
Settings::setValue("SchemaDock", "dropSelectQuery", ui->actionDropSelectQueryCheck->isChecked());
Settings::setValue("SchemaDock", "dropQualifiedNames", ui->actionDropQualifiedCheck->isChecked());
Settings::setValue("SchemaDock", "dropEnquotedNames", ui->actionEnquoteNamesCheck->isChecked());

View File

@@ -1991,6 +1991,20 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
<string>New In-&amp;Memory Database</string>
</property>
</action>
<action name="actionDropSelectQueryCheck">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Drag &amp;&amp; Drop SELECT Query</string>
</property>
<property name="statusTip">
<string>When dragging fields from the same table or a single table, drop a SELECT query into the editor</string>
</property>
<property name="whatsThis">
<string>When dragging fields from the same table or a single table, drop a SELECT query into the editor</string>
</property>
</action>
<action name="actionDropQualifiedCheck">
<property name="checkable">
<bool>true</bool>

View File

@@ -425,6 +425,9 @@ QVariant Settings::getDefaultValue(const std::string& group, const std::string&
// SchemaDock Drag & drop settings
if(group == "SchemaDock")
{
if(name == "dropSelectQuery")
return true;
if(name == "dropQualifiedNames")
return false;