From 5c7238d3d096caa81dc4aa2d220006068563174e Mon Sep 17 00:00:00 2001 From: mgrojo Date: Mon, 18 Jun 2018 17:49:12 +0200 Subject: [PATCH] Preference for quoting identifier mechanism A new option is added to the SQL tab in Preferences for choosing which quoting characters must be used by the application when it inserts an identifier in SQL code. The three options supported by SQLite are available. Default quoting characters have been changed from `Grave accents` to "Double quotes" (SQL standard). This options also affect the highlighting of double quoted strings: when the SQL standard is selected, double quoted expressions are highlighted as identifiers, otherwise as literal strings. --- src/MainWindow.cpp | 2 + src/PreferencesDialog.cpp | 2 + src/PreferencesDialog.ui | 211 ++++++++++++++++++++++---------------- src/Settings.cpp | 4 + src/sqlitetypes.cpp | 19 +++- src/sqlitetypes.h | 9 ++ src/sqltextedit.cpp | 13 ++- 7 files changed, 170 insertions(+), 90 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 01f3a3f8..9037ad2e 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -1990,6 +1990,8 @@ void MainWindow::reloadSettings() // Reload remote dock settings remoteDock->reloadSettings(); + + sqlb::setIdentifierQuoting(static_cast(Settings::getValue("editor", "identifier_quotes").toInt())); } void MainWindow::checkNewVersion(const QString& versionstring, const QString& url) diff --git a/src/PreferencesDialog.cpp b/src/PreferencesDialog.cpp index c9299364..7a7dc1ba 100644 --- a/src/PreferencesDialog.cpp +++ b/src/PreferencesDialog.cpp @@ -168,6 +168,7 @@ void PreferencesDialog::loadSettings() ui->spinTabSize->setValue(Settings::getValue("editor", "tabsize").toInt()); ui->spinLogFontSize->setValue(Settings::getValue("log", "fontsize").toInt()); ui->wrapComboBox->setCurrentIndex(Settings::getValue("editor", "wrap_lines").toInt()); + ui->quoteComboBox->setCurrentIndex(Settings::getValue("editor", "identifier_quotes").toInt()); ui->checkAutoCompletion->setChecked(Settings::getValue("editor", "auto_completion").toBool()); ui->checkCompleteUpper->setEnabled(Settings::getValue("editor", "auto_completion").toBool()); ui->checkCompleteUpper->setChecked(Settings::getValue("editor", "upper_keywords").toBool()); @@ -223,6 +224,7 @@ void PreferencesDialog::saveSettings() Settings::setValue("editor", "tabsize", ui->spinTabSize->value()); Settings::setValue("log", "fontsize", ui->spinLogFontSize->value()); Settings::setValue("editor", "wrap_lines", ui->wrapComboBox->currentIndex()); + Settings::setValue("editor", "identifier_quotes", ui->quoteComboBox->currentIndex()); Settings::setValue("editor", "auto_completion", ui->checkAutoCompletion->isChecked()); Settings::setValue("editor", "upper_keywords", ui->checkCompleteUpper->isChecked()); Settings::setValue("editor", "error_indicators", ui->checkErrorIndicators->isChecked()); diff --git a/src/PreferencesDialog.ui b/src/PreferencesDialog.ui index 20ab6828..8323ec03 100644 --- a/src/PreferencesDialog.ui +++ b/src/PreferencesDialog.ui @@ -920,6 +920,19 @@ + + + + SQL editor &font + + + comboEditorFont + + + + + + @@ -977,93 +990,13 @@ - - + + - SQL editor &font + Wrap lines - comboEditorFont - - - - - - - - - - Code co&mpletion - - - checkAutoCompletion - - - - - - - enabled - - - - - - - Keywords in &UPPER CASE - - - checkCompleteUpper - - - - - - - When set, the SQL keywords are completed in UPPER CASE letters. - - - enabled - - - - - - - Error indicators - - - checkErrorIndicators - - - - - - - When set, the SQL code lines that caused errors during the last execution are highlighted and the results frame indicates the error in the background - - - enabled - - - - - - - Hori&zontal tiling - - - checkHorizontalTiling - - - - - - - If enabled the SQL code editor and the result table view are shown side by side instead of one over the other. - - - enabled + wrapComboBox @@ -1091,13 +1024,115 @@ - - + + + + Choose the quoting mechanism used by the application for identifiers in SQL code. + + + + + + + "Double quotes" + + + + + `Grave accents` + + + + + [Square brackets] + + + + + + - Wrap lines + Code co&mpletion - wrapComboBox + checkAutoCompletion + + + + + + + enabled + + + + + + + Keywords in &UPPER CASE + + + checkCompleteUpper + + + + + + + When set, the SQL keywords are completed in UPPER CASE letters. + + + enabled + + + + + + + Error indicators + + + checkErrorIndicators + + + + + + + When set, the SQL code lines that caused errors during the last execution are highlighted and the results frame indicates the error in the background + + + enabled + + + + + + + Hori&zontal tiling + + + checkHorizontalTiling + + + + + + + If enabled the SQL code editor and the result table view are shown side by side instead of one over the other. + + + enabled + + + + + + + Quotes for identifiers + + + quoteComboBox diff --git a/src/Settings.cpp b/src/Settings.cpp index 3e581b5d..64a0de6a 100644 --- a/src/Settings.cpp +++ b/src/Settings.cpp @@ -294,6 +294,10 @@ QVariant Settings::getDefaultValue(const QString& group, const QString& name) if(group == "editor" && name == "wrap_lines") return 0; // QsciScintilla::WrapNone + // editor/identifier_quotes + if(group == "editor" && name == "identifier_quotes") + return 0; // sqlb::DoubleQuotes + // editor/auto_completion? if(group == "editor" && name == "auto_completion") return true; diff --git a/src/sqlitetypes.cpp b/src/sqlitetypes.cpp index 0e398bc0..2271c6be 100644 --- a/src/sqlitetypes.cpp +++ b/src/sqlitetypes.cpp @@ -11,9 +11,26 @@ namespace sqlb { QStringList Field::Datatypes = QStringList() << "INTEGER" << "TEXT" << "BLOB" << "REAL" << "NUMERIC"; +escapeQuoting customQuoting = DoubleQuotes; + +void setIdentifierQuoting(escapeQuoting toQuoting) +{ + customQuoting = toQuoting; +} + QString escapeIdentifier(QString id) { - return '`' + id.replace('`', "``") + '`'; + switch(customQuoting) { + case DoubleQuotes: + return '"' + id.replace('"', "\"\"") + '"'; + case GraveAccents: + return '`' + id.replace('`', "``") + '`'; + case SquareBrackets: + // There aren't any escaping possibilities for square brackets inside the identifier, + // so we rely on the user to not enter these characters when this kind of quoting is + // selected. + return '[' + id + ']'; + } } QStringList fieldVectorToFieldNames(const FieldVector& vector) diff --git a/src/sqlitetypes.h b/src/sqlitetypes.h index b1e72b7c..4b40f3a8 100644 --- a/src/sqlitetypes.h +++ b/src/sqlitetypes.h @@ -38,6 +38,15 @@ uint qHash(const QVector& key, uint seed = 0) } #endif +enum escapeQuoting { + DoubleQuotes, + GraveAccents, + SquareBrackets +}; + +// Set quoting style for escapeIdentifier +void setIdentifierQuoting(escapeQuoting toQuoting); + QString escapeIdentifier(QString id); class ObjectIdentifier diff --git a/src/sqltextedit.cpp b/src/sqltextedit.cpp index a8302d2a..6a44c32a 100644 --- a/src/sqltextedit.cpp +++ b/src/sqltextedit.cpp @@ -1,3 +1,4 @@ +#include "sqlitetypes.h" #include "sqltextedit.h" #include "Settings.h" #include "SqlUiLexer.h" @@ -49,8 +50,18 @@ void SqlTextEdit::reloadSettings() setupSyntaxHighlightingFormat(sqlLexer, "keyword", QsciLexerSQL::Keyword); setupSyntaxHighlightingFormat(sqlLexer, "table", QsciLexerSQL::KeywordSet6); setupSyntaxHighlightingFormat(sqlLexer, "function", QsciLexerSQL::KeywordSet7); - setupSyntaxHighlightingFormat(sqlLexer, "string", QsciLexerSQL::DoubleQuotedString); setupSyntaxHighlightingFormat(sqlLexer, "string", QsciLexerSQL::SingleQuotedString); + + // Highlight double quote strings as identifier or as literal string depending on user preference + switch(static_cast(Settings::getValue("editor", "identifier_quotes").toInt())) { + case sqlb::DoubleQuotes: + setupSyntaxHighlightingFormat(sqlLexer, "identifier", QsciLexerSQL::DoubleQuotedString); + break; + case sqlb::GraveAccents: + case sqlb::SquareBrackets: + setupSyntaxHighlightingFormat(sqlLexer, "string", QsciLexerSQL::DoubleQuotedString); + break; + } setupSyntaxHighlightingFormat(sqlLexer, "identifier", QsciLexerSQL::Identifier); setupSyntaxHighlightingFormat(sqlLexer, "identifier", QsciLexerSQL::QuotedIdentifier); }