Skip to content

Commit ff830e7

Browse files
committed
Drag & Drop or Copy & Paste insert statements from the DB Schema dock
See issue #3817
1 parent 3aaa752 commit ff830e7

5 files changed

Lines changed: 104 additions & 12 deletions

File tree

src/DbStructureModel.cpp

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88
#include <QMimeData>
99
#include <QMessageBox>
1010
#include <QApplication>
11+
#include <QClipboard>
1112

1213
DbStructureModel::DbStructureModel(DBBrowserDB& db, QObject* parent)
1314
: QAbstractItemModel(parent),
1415
m_db(db),
1516
browsablesRootItem(nullptr),
1617
m_dropQualifiedNames(false),
1718
m_dropEnquotedNames(false),
18-
m_dropSelectQuery(true)
19+
m_dropSelectQuery(true),
20+
m_dropInsert(false)
1921
{
2022
// Create root item and use its columns to store the header strings
2123
QStringList header;
@@ -210,7 +212,7 @@ QStringList DbStructureModel::mimeTypes() const
210212
QMimeData* DbStructureModel::mimeData(const QModelIndexList& indices) const
211213
{
212214
// We store the SQL data and the names data separately
213-
QByteArray sqlData, namesData;
215+
QByteArray sqlData, namesData, parametersData;
214216

215217
// For dropping SELECT queries, these variables take account of
216218
// whether all objects are of the same type and belong to the same table.
@@ -235,6 +237,8 @@ QMimeData* DbStructureModel::mimeData(const QModelIndexList& indices) const
235237
// For names, export a (qualified) (escaped) identifier of the item for statement composition in SQL editor.
236238
if(objectType == "field") {
237239
namesData.append(getNameForDropping(item->text(ColumnSchema), item->parent()->text(ColumnName), item->text(ColumnName)).toUtf8());
240+
parametersData.append(QString("?" + item->text(ColumnName) + ", ").toUtf8());
241+
238242
QString table = getNameForDropping(item->text(ColumnSchema), item->parent()->text(ColumnName), "");
239243
if (tableSet.isEmpty() || tableSet == table) {
240244
tableSet = table;
@@ -299,14 +303,27 @@ QMimeData* DbStructureModel::mimeData(const QModelIndexList& indices) const
299303
else if (namesData.endsWith("."))
300304
namesData.chop(1);
301305

306+
if (parametersData.endsWith(", "))
307+
parametersData.chop(2);
308+
else if (parametersData.endsWith("."))
309+
parametersData.chop(1);
310+
302311
if (tableSet.endsWith("."))
303312
tableSet.chop(1);
304313

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();
314+
if (!tableSet.isEmpty() && tableSet != "*" && !objectTypeSet.isEmpty()) {
315+
if (m_dropSelectQuery) {
316+
if (objectTypeSet == "field") {
317+
namesData = ("SELECT " + QString::fromUtf8(namesData) + " FROM " + tableSet + ";\n").toUtf8();
318+
} else if (objectTypeSet == "table") {
319+
namesData = ("SELECT * FROM " + tableSet + ";").toUtf8();
320+
}
321+
} else if (m_dropInsert) {
322+
if (objectTypeSet == "field") {
323+
namesData = ("INSERT INTO " + tableSet + " (" + QString::fromUtf8(namesData) + ") VALUES (" + parametersData + ");\n").toUtf8();
324+
} else if (objectTypeSet == "table") {
325+
namesData = ("INSERT INTO " + tableSet + " DEFAULT VALUES;\n").toUtf8();
326+
}
310327
}
311328
}
312329
mime->setData("text/plain", namesData);
@@ -315,6 +332,12 @@ QMimeData* DbStructureModel::mimeData(const QModelIndexList& indices) const
315332
return mime;
316333
}
317334

335+
void DbStructureModel::copy(const QModelIndexList& indices) const
336+
{
337+
QMimeData *mimeData = DbStructureModel::mimeData(indices);
338+
qApp->clipboard()->setMimeData(mimeData);
339+
}
340+
318341
bool DbStructureModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int, int, const QModelIndex&)
319342
{
320343
if(action == Qt::IgnoreAction)

src/DbStructureModel.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class DbStructureModel : public QAbstractItemModel
2727
QMimeData* mimeData(const QModelIndexList& indices) const override;
2828
bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) override;
2929

30+
// Copy selected items to clipboard
31+
void copy(const QModelIndexList& indices) const;
32+
3033
enum Columns
3134
{
3235
ColumnName,
@@ -41,6 +44,7 @@ public slots:
4144
void setDropQualifiedNames(bool value) { m_dropQualifiedNames = value; }
4245
void setDropEnquotedNames(bool value) { m_dropEnquotedNames = value; }
4346
void setDropSelectQuery(bool value) { m_dropSelectQuery = value; }
47+
void setDropInsert(bool value) { m_dropInsert = value; }
4448

4549
private:
4650
DBBrowserDB& m_db;
@@ -49,6 +53,7 @@ public slots:
4953
bool m_dropQualifiedNames;
5054
bool m_dropEnquotedNames;
5155
bool m_dropSelectQuery;
56+
bool m_dropInsert;
5257

5358
void buildTree(QTreeWidgetItem* parent, const std::string& schema);
5459
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: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,30 @@ void MainWindow::init()
247247
popupSchemaDockMenu->addAction(ui->actionPopupSchemaDockDetachDatabase);
248248
popupSchemaDockMenu->addSeparator();
249249
popupSchemaDockMenu->addAction(ui->actionDropSelectQueryCheck);
250+
popupSchemaDockMenu->addAction(ui->actionDropInsertCheck);
251+
popupSchemaDockMenu->addAction(ui->actionDropNamesCheck);
252+
253+
QActionGroup* dropGroup = new QActionGroup(popupSchemaDockMenu);
254+
dropGroup->addAction(ui->actionDropSelectQueryCheck);
255+
dropGroup->addAction(ui->actionDropInsertCheck);
256+
dropGroup->addAction(ui->actionDropNamesCheck);
257+
258+
popupSchemaDockMenu->addSeparator();
250259
popupSchemaDockMenu->addAction(ui->actionDropQualifiedCheck);
251260
popupSchemaDockMenu->addAction(ui->actionEnquoteNamesCheck);
252261

262+
popupSchemaDockMenu->addSeparator();
263+
QAction* copyAction = new QAction(QIcon(":/icons/copy"), tr("Copy"), popupSchemaDockMenu);
264+
copyAction->setShortcut(QKeySequence::Copy);
265+
popupSchemaDockMenu->addAction(copyAction);
266+
connect(copyAction, &QAction::triggered, this, [=]() {
267+
dbStructureModel->copy(ui->treeSchemaDock->selectionModel()->selectedIndexes());
268+
});
269+
auto copyShortcut = new QShortcut(QKeySequence::Copy, ui->treeSchemaDock);
270+
connect(copyShortcut, &QShortcut::activated, this, [=]() {
271+
dbStructureModel->copy(ui->treeSchemaDock->selectionModel()->selectedIndexes());
272+
});
273+
253274
popupOpenDbMenu = new QMenu(this);
254275
popupOpenDbMenu->addAction(ui->fileOpenAction);
255276
popupOpenDbMenu->addAction(ui->fileOpenReadOnlyAction);
@@ -419,14 +440,16 @@ void MainWindow::init()
419440
connect(ui->dockEdit, &QDockWidget::visibilityChanged, this, &MainWindow::toggleEditDock);
420441
connect(remoteDock, SIGNAL(openFile(QString)), this, SLOT(fileOpen(QString)));
421442
connect(ui->actionDropSelectQueryCheck, &QAction::toggled, dbStructureModel, &DbStructureModel::setDropSelectQuery);
443+
connect(ui->actionDropInsertCheck, &QAction::toggled, dbStructureModel, &DbStructureModel::setDropInsert);
444+
connect(ui->actionDropNamesCheck, &QAction::toggled, dbStructureModel,
445+
[this]() {
446+
dbStructureModel->setDropSelectQuery(false);
447+
dbStructureModel->setDropInsert(false);
448+
});
422449
connect(ui->actionDropQualifiedCheck, &QAction::toggled, dbStructureModel, &DbStructureModel::setDropQualifiedNames);
423450
connect(ui->actionEnquoteNamesCheck, &QAction::toggled, dbStructureModel, &DbStructureModel::setDropEnquotedNames);
424451
connect(&db, &DBBrowserDB::databaseInUseChanged, this, &MainWindow::updateDatabaseBusyStatus);
425452

426-
ui->actionDropSelectQueryCheck->setChecked(Settings::getValue("SchemaDock", "dropSelectQuery").toBool());
427-
ui->actionDropQualifiedCheck->setChecked(Settings::getValue("SchemaDock", "dropQualifiedNames").toBool());
428-
ui->actionEnquoteNamesCheck->setChecked(Settings::getValue("SchemaDock", "dropEnquotedNames").toBool());
429-
430453
connect(ui->actionSqlStop, &QAction::triggered, this, [this]() {
431454
if(execute_sql_worker && execute_sql_worker->isRunning())
432455
execute_sql_worker->stop();
@@ -770,6 +793,7 @@ void MainWindow::closeEvent( QCloseEvent* event )
770793

771794
Settings::setValue("SQLLogDock", "Log", ui->comboLogSubmittedBy->currentText());
772795
Settings::setValue("SchemaDock", "dropSelectQuery", ui->actionDropSelectQueryCheck->isChecked());
796+
Settings::setValue("SchemaDock", "dropInsert", ui->actionDropInsertCheck->isChecked());
773797
Settings::setValue("SchemaDock", "dropQualifiedNames", ui->actionDropQualifiedCheck->isChecked());
774798
Settings::setValue("SchemaDock", "dropEnquotedNames", ui->actionEnquoteNamesCheck->isChecked());
775799

@@ -2423,7 +2447,16 @@ void MainWindow::reloadSettings()
24232447

24242448
sqlb::setIdentifierQuoting(static_cast<sqlb::escapeQuoting>(Settings::getValue("editor", "identifier_quotes").toInt()));
24252449

2426-
ui->tabSqlAreas->setTabsClosable(Settings::getValue("editor", "close_button_on_tabs").toBool());
2450+
ui->tabSqlAreas->setTabsClosable(
2451+
Settings::getValue("editor", "close_button_on_tabs").toBool());
2452+
2453+
ui->actionDropSelectQueryCheck->setChecked(Settings::getValue("SchemaDock", "dropSelectQuery").toBool());
2454+
ui->actionDropInsertCheck->setChecked(Settings::getValue("SchemaDock", "dropInsert").toBool());
2455+
ui->actionDropNamesCheck->setChecked(!ui->actionDropSelectQueryCheck->isChecked() &&
2456+
!ui->actionDropInsertCheck->isChecked());
2457+
2458+
ui->actionDropQualifiedCheck->setChecked(Settings::getValue("SchemaDock", "dropQualifiedNames").toBool());
2459+
ui->actionEnquoteNamesCheck->setChecked(Settings::getValue("SchemaDock", "dropEnquotedNames").toBool());
24272460
}
24282461

24292462
void MainWindow::checkNewVersion(const bool automatic)

src/MainWindow.ui

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,6 +2415,34 @@ You can drag SQL statements from the Schema column and drop them into the SQL ed
24152415
<string>When dragging fields from the same table or a single table, drop a SELECT query into the editor</string>
24162416
</property>
24172417
</action>
2418+
<action name="actionDropNamesCheck">
2419+
<property name="checkable">
2420+
<bool>true</bool>
2421+
</property>
2422+
<property name="text">
2423+
<string>Drag &amp;&amp; Drop Names</string>
2424+
</property>
2425+
<property name="statusTip">
2426+
<string>When dragging fields or tables, drop just the names into the editor</string>
2427+
</property>
2428+
<property name="whatsThis">
2429+
<string>When dragging fields or tables, drop just the names into the editor</string>
2430+
</property>
2431+
</action>
2432+
<action name="actionDropInsertCheck">
2433+
<property name="checkable">
2434+
<bool>true</bool>
2435+
</property>
2436+
<property name="text">
2437+
<string>Drag &amp;&amp; Drop INSERT Statement</string>
2438+
</property>
2439+
<property name="statusTip">
2440+
<string>When dragging fields from the same table or a single table, drop an INSERT statement into the editor</string>
2441+
</property>
2442+
<property name="whatsThis">
2443+
<string>When dragging fields from the same table or a single table, drop an INSERT statement into the editor</string>
2444+
</property>
2445+
</action>
24182446
<action name="actionDropQualifiedCheck">
24192447
<property name="checkable">
24202448
<bool>true</bool>

src/Settings.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ QVariant Settings::getDefaultValue(const std::string& group, const std::string&
433433
if(name == "dropSelectQuery")
434434
return true;
435435

436+
if(name == "dropInsert")
437+
return false;
438+
436439
if(name == "dropQualifiedNames")
437440
return false;
438441

0 commit comments

Comments
 (0)