Commit 48e43ae5 authored by Artyom Kirnev's avatar Artyom Kirnev
Browse files

added "select data" action for table

parent be884de4
......@@ -271,23 +271,27 @@ void SchemaWidget::slotCustomContextMenuRequested(const QPoint &pos)
if (item) {
if (item->type() == SchemaWidget::SystemTableType || item->type() == SchemaWidget::TableType || item->type() == SchemaWidget::ViewType
|| item->type() == SchemaWidget::FieldType) {
menu.addAction(QIcon::fromTheme(QStringLiteral("view-sort-descending")),
i18nc("@action:inmenu Context menu", "Select Data"),
this,
&SchemaWidget::executeSelect);
menu.addSeparator();
QMenu *submenu = menu.addMenu(QIcon::fromTheme(QStringLiteral("tools-wizard")), i18nc("@action:inmenu Submenu title", "Generate"));
submenu->addAction(i18n("SELECT"), this, &SchemaWidget::generateSelect);
submenu->addAction(i18n("UPDATE"), this, &SchemaWidget::generateUpdate);
submenu->addAction(i18n("INSERT"), this, &SchemaWidget::generateInsert);
submenu->addAction(i18n("DELETE"), this, &SchemaWidget::generateDelete);
submenu->addAction(i18n("SELECT"), this, &SchemaWidget::generateSelectIntoView);
submenu->addAction(i18n("UPDATE"), this, &SchemaWidget::generateUpdateIntoView);
submenu->addAction(i18n("INSERT"), this, &SchemaWidget::generateInsertIntoView);
submenu->addAction(i18n("DELETE"), this, &SchemaWidget::generateDeleteIntoView);
}
}
menu.exec(QCursor::pos());
}
void SchemaWidget::generateStatement(QSqlDriver::StatementType statementType)
QString SchemaWidget::generateStatement(QSqlDriver::StatementType statementType)
{
if (!isConnectionValidAndOpen()) {
return;
return {};
}
QSqlDatabase db = QSqlDatabase::database(m_connectionName);
......@@ -295,13 +299,13 @@ void SchemaWidget::generateStatement(QSqlDriver::StatementType statementType)
QSqlDriver *drv = db.driver();
if (!drv) {
return;
return {};
}
QTreeWidgetItem *item = currentItem();
if (!item) {
return;
return {};
}
QString statement;
......@@ -348,37 +352,55 @@ void SchemaWidget::generateStatement(QSqlDriver::StatementType statementType)
} break;
}
KTextEditor::MainWindow *mw = KTextEditor::Editor::instance()->application()->activeMainWindow();
KTextEditor::View *kv = mw->activeView();
// replace NULL with a more generic '?'
statement.replace(QLatin1String("NULL"), QLatin1String("?"));
return statement;
}
if (kv) {
// paste statement in the active view
kv->insertText(statement);
kv->setFocus();
}
void SchemaWidget::pasteStatementIntoActiveView(const QString &statement)
{
KTextEditor::MainWindow *mw = KTextEditor::Editor::instance()->application()->activeMainWindow();
KTextEditor::View *kv = mw->activeView();
qDebug() << "Generated statement:" << statement;
if (!kv) {
return;
}
// paste statement in the active view
kv->insertText(statement);
kv->setFocus();
}
void SchemaWidget::executeStatement(const QString &statement)
{
m_manager->runQuery(statement, m_connectionName);
}
void SchemaWidget::executeSelect()
{
const QString select = generateStatement(QSqlDriver::SelectStatement);
executeStatement(select);
}
void SchemaWidget::generateSelect()
void SchemaWidget::generateAndPasteStatement(QSqlDriver::StatementType statementType)
{
QString statement = generateStatement(statementType);
pasteStatementIntoActiveView(statement);
}
void SchemaWidget::generateSelectIntoView()
{
generateStatement(QSqlDriver::SelectStatement);
generateAndPasteStatement(QSqlDriver::SelectStatement);
}
void SchemaWidget::generateUpdate()
void SchemaWidget::generateUpdateIntoView()
{
generateStatement(QSqlDriver::UpdateStatement);
generateAndPasteStatement(QSqlDriver::UpdateStatement);
}
void SchemaWidget::generateInsert()
void SchemaWidget::generateInsertIntoView()
{
generateStatement(QSqlDriver::InsertStatement);
generateAndPasteStatement(QSqlDriver::InsertStatement);
}
void SchemaWidget::generateDelete()
void SchemaWidget::generateDeleteIntoView()
{
generateStatement(QSqlDriver::DeleteStatement);
generateAndPasteStatement(QSqlDriver::DeleteStatement);
}
......@@ -40,11 +40,15 @@ public Q_SLOTS:
void buildTree(const QString &connection);
void refresh();
void generateSelect();
void generateUpdate();
void generateInsert();
void generateDelete();
void generateStatement(QSqlDriver::StatementType type);
void generateSelectIntoView();
void generateUpdateIntoView();
void generateInsertIntoView();
void generateDeleteIntoView();
void executeSelect();
QString generateStatement(QSqlDriver::StatementType statementType);
static void pasteStatementIntoActiveView(const QString &statement);
void generateAndPasteStatement(QSqlDriver::StatementType statementType);
void executeStatement(const QString &statement);
private Q_SLOTS:
void slotCustomContextMenuRequested(const QPoint &pos);
......@@ -55,7 +59,6 @@ private:
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
bool isConnectionValidAndOpen();
QString m_connectionName;
QPoint m_dragStartPosition;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment