aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/guiSQLiteStudio/windows/editorwindow.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'SQLiteStudio3/guiSQLiteStudio/windows/editorwindow.cpp')
-rw-r--r--SQLiteStudio3/guiSQLiteStudio/windows/editorwindow.cpp83
1 files changed, 53 insertions, 30 deletions
diff --git a/SQLiteStudio3/guiSQLiteStudio/windows/editorwindow.cpp b/SQLiteStudio3/guiSQLiteStudio/windows/editorwindow.cpp
index f0f980d..23cb651 100644
--- a/SQLiteStudio3/guiSQLiteStudio/windows/editorwindow.cpp
+++ b/SQLiteStudio3/guiSQLiteStudio/windows/editorwindow.cpp
@@ -113,6 +113,12 @@ void EditorWindow::init()
resultsModel->setDb(currentDb);
ui->sqlEdit->setDb(currentDb);
+ connect(CFG_UI.General.SqlEditorCurrQueryHighlight, SIGNAL(changed(QVariant)), this, SLOT(queryHighlightingConfigChanged(QVariant)));
+ if (CFG_UI.General.SqlEditorCurrQueryHighlight.get())
+ ui->sqlEdit->setCurrentQueryHighlighting(true);
+
+ connect(ui->sqlEdit, SIGNAL(textChanged()), this, SLOT(checkTextChangedForSession()));
+
connect(resultsModel, SIGNAL(executionSuccessful()), this, SLOT(executionSuccessful()));
connect(resultsModel, SIGNAL(executionFailed(QString)), this, SLOT(executionFailed(QString)));
connect(resultsModel, SIGNAL(storeExecutionInHistory()), this, SLOT(storeExecutionInHistory()));
@@ -205,7 +211,7 @@ QAction* EditorWindow::getAction(EditorWindow::Action action)
return ExtActionContainer::getAction(action);
}
-QString EditorWindow::getQueryToExecute(bool doSelectCurrentQuery)
+QString EditorWindow::getQueryToExecute(bool doSelectCurrentQuery, QueryExecMode querySelectionMode)
{
QString sql;
if (ui->sqlEdit->textCursor().hasSelection())
@@ -213,7 +219,11 @@ QString EditorWindow::getQueryToExecute(bool doSelectCurrentQuery)
sql = ui->sqlEdit->textCursor().selectedText();
fixTextCursorSelectedText(sql);
}
- else if (CFG_UI.General.ExecuteCurrentQueryOnly.get())
+ else if (querySelectionMode == ALL)
+ {
+ sql = ui->sqlEdit->toPlainText();
+ }
+ else if (CFG_UI.General.ExecuteCurrentQueryOnly.get() || querySelectionMode == SINGLE)
{
ui->sqlEdit->saveSelection();
selectCurrentQuery(true);
@@ -237,7 +247,9 @@ bool EditorWindow::setCurrentDb(Db *db)
void EditorWindow::setContents(const QString &sql)
{
+ settingSqlContents = true;
ui->sqlEdit->setPlainText(sql);
+ settingSqlContents = false;
}
QString EditorWindow::getContents() const
@@ -394,6 +406,8 @@ void EditorWindow::createActions()
createAction(FOCUS_RESULTS_BELOW, tr("Focus results below", "sql editor"), this, SLOT(focusResultsBelow()), this);
createAction(FOCUS_EDITOR_ABOVE, tr("Focus SQL editor above", "sql editor"), this, SLOT(focusEditorAbove()), this);
createAction(DELETE_SINGLE_HISTORY_SQL, tr("Delete selected SQL history entries", "sql editor"), this, SLOT(deleteSelectedSqlHistory()), ui->historyList);
+ createAction(EXEC_ONE_QUERY, ICONS.EXEC_QUERY, tr("Execute single query under cursor"), this, SLOT(execOneQuery()), this);
+ createAction(EXEC_ALL_QUERIES, ICONS.EXEC_QUERY, tr("Execute all queries in editor"), this, SLOT(execAllQueries()), this);
// Static action triggers
connect(staticActions[RESULTS_IN_TAB], SIGNAL(triggered()), this, SLOT(updateResultsDisplayMode()));
@@ -421,35 +435,19 @@ void EditorWindow::selectCurrentQuery(bool fallBackToPreviousIfNecessary)
{
QTextCursor cursor = ui->sqlEdit->textCursor();
int pos = cursor.position();
- int queryStartPos;
- QString contents = ui->sqlEdit->toPlainText();
- QString query = getQueryWithPosition(contents, pos, &queryStartPos);
- TokenList tokens = Lexer::tokenize(query);
- tokens.trim();
- tokens.trimRight(Token::OPERATOR, ";");
- if (tokens.size() == 0 && fallBackToPreviousIfNecessary)
- {
- // Fallback
- pos = contents.lastIndexOf(";", pos - 1);
- if (pos > -1)
- {
- query = getQueryWithPosition(contents, pos, &queryStartPos);
- tokens = Lexer::tokenize(query);
- tokens.trim();
- tokens.trimRight(Token::OPERATOR, ";");
- }
- }
+ QString contents = ui->sqlEdit->toPlainText();
+ QPair boundries = getQueryBoundriesForPosition(contents, pos, fallBackToPreviousIfNecessary);
- if (tokens.size() == 0)
+ if (boundries.second < 0)
{
qWarning() << "No tokens to select in EditorWindow::selectCurrentQuery().";
return;
}
cursor.clearSelection();
- cursor.setPosition(tokens.first()->start + queryStartPos);
- cursor.setPosition(tokens.last()->end + 1 + queryStartPos, QTextCursor::KeepAnchor);
+ cursor.setPosition(boundries.first);
+ cursor.setPosition(boundries.second, QTextCursor::KeepAnchor);
ui->sqlEdit->setTextCursor(cursor);
}
@@ -459,13 +457,13 @@ void EditorWindow::updateShortcutTips()
{
QString prevDbKey = actionMap[PREV_DB]->shortcut().toString(QKeySequence::NativeText);
QString nextDbKey = actionMap[NEXT_DB]->shortcut().toString(QKeySequence::NativeText);
- dbCombo->setToolTip(tr("Active database (%1/%2)").arg(prevDbKey).arg(nextDbKey));
+ dbCombo->setToolTip(tr("Active database (%1/%2)").arg(prevDbKey, nextDbKey));
}
}
-void EditorWindow::execQuery(bool explain)
+void EditorWindow::execQuery(bool explain, QueryExecMode querySelectionMode)
{
- QString sql = getQueryToExecute(true);
+ QString sql = getQueryToExecute(true, querySelectionMode);
QHash<QString, QVariant> bindParams;
bool proceed = processBindParams(sql, bindParams);
if (!proceed)
@@ -487,6 +485,16 @@ void EditorWindow::execQuery(bool explain)
}
}
+void EditorWindow::execOneQuery()
+{
+ execQuery(false, SINGLE);
+}
+
+void EditorWindow::execAllQueries()
+{
+ execQuery(false, ALL);
+}
+
void EditorWindow::explainQuery()
{
execQuery(true);
@@ -504,7 +512,6 @@ bool EditorWindow::processBindParams(QString& sql, QHash<QString, QVariant>& que
// Process bind tokens, prepare list for a dialog.
static_qstring(paramTpl, ":arg%1");
- QString arg;
QVector<BindParam*> bindParams;
QHash<QString, QString> namedBindParams;
BindParam* bindParam = nullptr;
@@ -539,14 +546,14 @@ bool EditorWindow::processBindParams(QString& sql, QHash<QString, QVariant>& que
// Transfer values from dialog to arguments for query
if (accepted)
{
- for (BindParam* bindParam : bindParams)
+ for (BindParam*& bindParam : bindParams)
queryParams[bindParam->newName] = bindParam->value;
sql = tokens.detokenize();
}
// Cleanup
- for (BindParam* bindParam : bindParams)
+ for (BindParam*& bindParam : bindParams)
delete bindParam;
return accepted;
@@ -676,7 +683,7 @@ void EditorWindow::deleteSelectedSqlHistory()
return;
QList<qint64> ids;
- for (const QModelIndex& idx : ui->historyList->selectionModel()->selectedRows(0))
+ for (QModelIndex& idx : ui->historyList->selectionModel()->selectedRows(0))
ids += idx.data().toLongLong();
CFG->deleteSqlHistory(ids);
@@ -747,6 +754,22 @@ void EditorWindow::updateState()
actionMap[EXPLAIN_QUERY]->setEnabled(!executionInProgress);
}
+void EditorWindow::checkTextChangedForSession()
+{
+ if (!ui->sqlEdit->getHighlightingSyntax() && !settingSqlContents)
+ emit sessionValueChanged();
+}
+
+void EditorWindow::queryHighlightingConfigChanged(const QVariant& enabled)
+{
+ ui->sqlEdit->setCurrentQueryHighlighting(enabled.toBool());
+}
+
+void EditorWindow::refreshValidDbObjects()
+{
+ ui->sqlEdit->refreshValidObjects();
+}
+
int qHash(EditorWindow::ActionGroup actionGroup)
{
return static_cast<int>(actionGroup);