diff options
Diffstat (limited to 'Plugins/SqlEnterpriseFormatter')
23 files changed, 388 insertions, 111 deletions
diff --git a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter.pro b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter.pro index a00ab89..0515768 100644 --- a/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter.pro +++ b/Plugins/SqlEnterpriseFormatter/SqlEnterpriseFormatter.pro @@ -14,10 +14,12 @@ TEMPLATE = lib DEFINES += SQLENTERPRISEFORMATTER_LIBRARY SOURCES += sqlenterpriseformatter.cpp \ + formatfilterover.cpp \ formatstatement.cpp \ formatselect.cpp \ formatexpr.cpp \ formatlimit.cpp \ + formatwindowdefinition.cpp \ formatwith.cpp \ formatraise.cpp \ formatcreatetable.cpp \ @@ -53,6 +55,8 @@ SOURCES += sqlenterpriseformatter.cpp \ formatupsert.cpp HEADERS += sqlenterpriseformatter.h\ + formatfilterover.h \ + formatwindowdefinition.h \ sqlenterpriseformatter_global.h \ formatstatement.h \ formatselect.h \ diff --git a/Plugins/SqlEnterpriseFormatter/formatbegintrans.cpp b/Plugins/SqlEnterpriseFormatter/formatbegintrans.cpp index 55144dc..118f702 100644 --- a/Plugins/SqlEnterpriseFormatter/formatbegintrans.cpp +++ b/Plugins/SqlEnterpriseFormatter/formatbegintrans.cpp @@ -21,5 +21,5 @@ void FormatBeginTrans::formatInternal() withId(bt->name); } - withConflict(bt->onConflict).withSemicolon(); + withSemicolon(); } diff --git a/Plugins/SqlEnterpriseFormatter/formatcreateindex.cpp b/Plugins/SqlEnterpriseFormatter/formatcreateindex.cpp index ac5fd7c..7a984bd 100644 --- a/Plugins/SqlEnterpriseFormatter/formatcreateindex.cpp +++ b/Plugins/SqlEnterpriseFormatter/formatcreateindex.cpp @@ -19,25 +19,13 @@ void FormatCreateIndex::formatInternal() if (createIndex->ifNotExistsKw) withKeyword("IF").withKeyword("NOT").withKeyword("EXISTS"); - if (dialect == Dialect::Sqlite2) - { - withId(createIndex->index).withKeyword("ON"); + if (!createIndex->database.isNull()) + withId(createIndex->database).withIdDot(); - if (!createIndex->database.isNull()) - withId(createIndex->database).withIdDot(); + withId(createIndex->index).withKeyword("ON").withId(createIndex->table).withParDefLeft().withStatementList(createIndex->indexedColumns).withParDefRight(); - withId(createIndex->table).withParDefLeft().withStatementList(createIndex->indexedColumns).withParDefRight().withConflict(createIndex->onConflict); - } - else - { - if (!createIndex->database.isNull()) - withId(createIndex->database).withIdDot(); - - withId(createIndex->index).withKeyword("ON").withId(createIndex->table).withParDefLeft().withStatementList(createIndex->indexedColumns).withParDefRight(); - - if (createIndex->where) - withKeyword("WHERE").withStatement(createIndex->where); - } + if (createIndex->where) + withKeyword("WHERE").withStatement(createIndex->where); withSemicolon(); } diff --git a/Plugins/SqlEnterpriseFormatter/formatcreatetable.cpp b/Plugins/SqlEnterpriseFormatter/formatcreatetable.cpp index 6ac6226..e6fd189 100644 --- a/Plugins/SqlEnterpriseFormatter/formatcreatetable.cpp +++ b/Plugins/SqlEnterpriseFormatter/formatcreatetable.cpp @@ -20,7 +20,7 @@ void FormatCreateTable::formatInternal() if (createTable->ifNotExistsKw) withKeyword("IF").withKeyword("NOT").withKeyword("EXISTS"); - if (dialect == Dialect::Sqlite3 && !createTable->database.isNull()) + if (!createTable->database.isNull()) withId(createTable->database).withIdDot(); withId(createTable->table); @@ -81,9 +81,9 @@ void FormatCreateTable::formatColumns(const QList<SqliteCreateTable::Column*>& c int FormatCreateTable::getColNameLength(const QString& name) { if (cfg->SqlEnterpriseFormatter.AlwaysUseNameWrapping.get()) - return wrapObjName(name, dialect, wrapper).length(); + return wrapObjName(name, wrapper).length(); else - return wrapObjIfNeeded(name, dialect, wrapper).length(); + return wrapObjIfNeeded(name, wrapper).length(); } FormatCreateTableColumn::FormatCreateTableColumn(SqliteCreateTable::Column* column) : @@ -173,6 +173,17 @@ void FormatCreateTableColumnConstraint::formatInternal() withStatement(constr->foreignKey); break; } + case SqliteCreateTable::Column::Constraint::GENERATED: + { + if (constr->generatedKw) + withKeyword("GENERATED").withKeyword("ALWAYS"); + + withKeyword("AS").withParExprLeft().withStatement(constr->expr).withParExprRight(); + if (constr->generatedType != SqliteCreateTable::Column::Constraint::GeneratedType::null) + withId(SqliteCreateTable::Column::Constraint::toString(constr->generatedType), false); + + break; + } case SqliteCreateTable::Column::Constraint::NULL_: case SqliteCreateTable::Column::Constraint::NAME_ONLY: case SqliteCreateTable::Column::Constraint::DEFERRABLE_ONLY: diff --git a/Plugins/SqlEnterpriseFormatter/formatcreatetrigger.cpp b/Plugins/SqlEnterpriseFormatter/formatcreatetrigger.cpp index ee45541..76154a5 100644 --- a/Plugins/SqlEnterpriseFormatter/formatcreatetrigger.cpp +++ b/Plugins/SqlEnterpriseFormatter/formatcreatetrigger.cpp @@ -28,7 +28,7 @@ void FormatCreateTrigger::formatInternal() for (const QString& kw : keywords) withKeyword(kw); - if (dialect == Dialect::Sqlite3 && !createTrig->database.isNull()) + if (!createTrig->database.isNull()) withId(createTrig->database).withIdDot(); withId(createTrig->trigger).withNewLine(); @@ -55,9 +55,6 @@ void FormatCreateTrigger::formatInternal() withStatement(createTrig->event, QString(), eventStmtEnricher).withNewLine(); withLinedUpKeyword("ON", TRIGGER_MARK); - if (dialect == Dialect::Sqlite2 && !createTrig->database.isNull()) - withId(createTrig->database).withIdDot(); - withId(createTrig->table).withNewLine(); switch (createTrig->scope) diff --git a/Plugins/SqlEnterpriseFormatter/formatcreateview.cpp b/Plugins/SqlEnterpriseFormatter/formatcreateview.cpp index 52a49a2..5ed7b95 100644 --- a/Plugins/SqlEnterpriseFormatter/formatcreateview.cpp +++ b/Plugins/SqlEnterpriseFormatter/formatcreateview.cpp @@ -21,7 +21,7 @@ void FormatCreateView::formatInternal() if (createView->ifNotExists) withKeyword("IF").withKeyword("NOT").withKeyword("EXISTS"); - if (dialect == Dialect::Sqlite3 && !createView->database.isNull()) + if (!createView->database.isNull()) withId(createView->database).withIdDot(); withId(createView->view); diff --git a/Plugins/SqlEnterpriseFormatter/formatcreatevirtualtable.cpp b/Plugins/SqlEnterpriseFormatter/formatcreatevirtualtable.cpp index 7525667..f6495a0 100644 --- a/Plugins/SqlEnterpriseFormatter/formatcreatevirtualtable.cpp +++ b/Plugins/SqlEnterpriseFormatter/formatcreatevirtualtable.cpp @@ -27,7 +27,7 @@ void FormatCreateVirtualTable::formatInternal() if (i > 0) withListComma(); - for (const TokenPtr& tk : Lexer::tokenize(arg, Dialect::Sqlite3)) + for (const TokenPtr& tk : Lexer::tokenize(arg)) handleToken(tk); i++; diff --git a/Plugins/SqlEnterpriseFormatter/formatexpr.cpp b/Plugins/SqlEnterpriseFormatter/formatexpr.cpp index e79995f..53c7421 100644 --- a/Plugins/SqlEnterpriseFormatter/formatexpr.cpp +++ b/Plugins/SqlEnterpriseFormatter/formatexpr.cpp @@ -4,6 +4,7 @@ #include "parser/ast/sqlitecolumntype.h" #include "parser/ast/sqliteselect.h" #include "parser/ast/sqliteraise.h" +#include "parser/ast/sqlitefilterover.h" #include "sqlenterpriseformatter.h" QRegularExpression FormatExpr::WORD_ONLY_RE = QRegularExpression("^[a-zA-Z]+$"); @@ -82,6 +83,8 @@ void FormatExpr::formatInternal() withFuncId(expr->function).withParFuncLeft(); if (expr->distinctKw) withKeyword("DISTINCT"); + else if (expr->allKw) + withKeyword("ALL"); if (expr->star) withOperator("*").withParFuncRight(); @@ -90,6 +93,24 @@ void FormatExpr::formatInternal() break; } + case SqliteExpr::Mode::WINDOW_FUNCTION: + { + withFuncId(expr->function).withParFuncLeft(); + if (expr->distinctKw) + withKeyword("DISTINCT"); + else if (expr->allKw) + withKeyword("ALL"); + + if (expr->star) + withOperator("*").withParFuncRight(); + else + withStatementList(expr->exprList, "funcArgs", FormatStatement::ListSeparator::EXPR_COMMA).withParFuncRight(); + + if (expr->filterOver) + withStatement(expr->filterOver); + + break; + } case SqliteExpr::Mode::SUB_EXPR: withParExprLeft().withStatement(expr->expr1).withParExprRight(); break; diff --git a/Plugins/SqlEnterpriseFormatter/formatfilterover.cpp b/Plugins/SqlEnterpriseFormatter/formatfilterover.cpp new file mode 100644 index 0000000..f08f4ca --- /dev/null +++ b/Plugins/SqlEnterpriseFormatter/formatfilterover.cpp @@ -0,0 +1,46 @@ +#include "formatfilterover.h" +#include "parser/ast/sqliteexpr.h" + +FormatFilterOver::FormatFilterOver(SqliteFilterOver* filterOver) : + filterOver(filterOver) +{ +} + +void FormatFilterOver::formatInternal() +{ + if (filterOver->filter) + withStatement(filterOver->filter); + + if (filterOver->over) + withStatement(filterOver->over); +} + +FormatFilterOverFilter::FormatFilterOverFilter(SqliteFilterOver::Filter* filter) : + filter(filter) +{ +} + +void FormatFilterOverFilter::formatInternal() +{ + withKeyword("FILTER").withParExprLeft().withKeyword("WHERE").withStatement(filter->expr).withParExprRight(); +} + +FormatFilterOverOver::FormatFilterOverOver(SqliteFilterOver::Over* over) : + over(over) +{ +} + +void FormatFilterOverOver::formatInternal() +{ + withKeyword("OVER"); + + switch (over->mode) + { + case SqliteFilterOver::Over::Mode::WINDOW: + withParExprLeft().withStatement(over->window).withParExprRight(); + break; + case SqliteFilterOver::Over::Mode::NAME: + withId(over->name); + break; + } +} diff --git a/Plugins/SqlEnterpriseFormatter/formatfilterover.h b/Plugins/SqlEnterpriseFormatter/formatfilterover.h new file mode 100644 index 0000000..ab646fb --- /dev/null +++ b/Plugins/SqlEnterpriseFormatter/formatfilterover.h @@ -0,0 +1,43 @@ +#ifndef FORMATFILTEROVER_H +#define FORMATFILTEROVER_H + +#include "formatstatement.h" +#include "parser/ast/sqlitefilterover.h" + +class FormatFilterOver : public FormatStatement +{ + public: + FormatFilterOver(SqliteFilterOver* filterOver); + + protected: + void formatInternal(); + + private: + SqliteFilterOver* filterOver = nullptr; +}; + +class FormatFilterOverFilter : public FormatStatement +{ + public: + FormatFilterOverFilter(SqliteFilterOver::Filter* filter); + + protected: + void formatInternal(); + + private: + SqliteFilterOver::Filter* filter = nullptr; +}; + +class FormatFilterOverOver: public FormatStatement +{ + public: + FormatFilterOverOver(SqliteFilterOver::Over* over); + + protected: + void formatInternal(); + + private: + SqliteFilterOver::Over* over = nullptr; +}; + +#endif // FORMATFILTEROVER_H diff --git a/Plugins/SqlEnterpriseFormatter/formatinsert.cpp b/Plugins/SqlEnterpriseFormatter/formatinsert.cpp index 2a49cfd..709ee1b 100644 --- a/Plugins/SqlEnterpriseFormatter/formatinsert.cpp +++ b/Plugins/SqlEnterpriseFormatter/formatinsert.cpp @@ -46,10 +46,6 @@ void FormatInsert::formatInternal() { withStatement(insert->select); } - else if (dialect == Dialect::Sqlite2) // Sqlite2 uses classic single row values - { - withKeyword("VALUES").withParDefLeft().withStatementList(insert->values).withParDefRight(); - } if (insert->upsert) withStatement(insert->upsert); diff --git a/Plugins/SqlEnterpriseFormatter/formatorderby.cpp b/Plugins/SqlEnterpriseFormatter/formatorderby.cpp index d7b2def..b70e0ab 100644 --- a/Plugins/SqlEnterpriseFormatter/formatorderby.cpp +++ b/Plugins/SqlEnterpriseFormatter/formatorderby.cpp @@ -12,4 +12,7 @@ void FormatOrderBy::formatInternal() withStatement(orderBy->expr); if (orderBy->order != SqliteSortOrder::null) withKeyword(sqliteSortOrder(orderBy->order)); + + if (orderBy->nulls != SqliteNulls::null) + withKeyword("NULLS").withKeyword(sqliteNulls(orderBy->nulls)); } diff --git a/Plugins/SqlEnterpriseFormatter/formatselect.cpp b/Plugins/SqlEnterpriseFormatter/formatselect.cpp index f5aa55e..37559eb 100644 --- a/Plugins/SqlEnterpriseFormatter/formatselect.cpp +++ b/Plugins/SqlEnterpriseFormatter/formatselect.cpp @@ -1,6 +1,7 @@ #include "formatselect.h" #include "formatwith.h" #include "parser/ast/sqlitewith.h" +#include "parser/ast/sqlitewindowdefinition.h" FormatSelect::FormatSelect(SqliteSelect* select) : select(select) @@ -86,6 +87,13 @@ void FormatSelectCore::formatInternal() if (core->having) withNewLine().withLinedUpKeyword("HAVING", "selectCore").withStatement(core->having, "having"); + if (core->windows.size() > 0) + { + withNewLine().withLinedUpKeyword("WINDOW", "selectCore"); + markKeywordLineUp("WINDOW", "selectWindow"); + withStatementList(core->windows, "selectWindow"); + } + if (core->orderBy.size() > 0) withNewLine().withLinedUpKeyword("ORDER", "selectCore").withKeyword("BY").withStatementList(core->orderBy, "order"); @@ -197,50 +205,21 @@ void FormatSelectCoreJoinOp::formatInternal() withNewLine(); QStringList keywords; - switch (dialect) - { - case Dialect::Sqlite3: - { - if (joinOp->naturalKw) - keywords << "NATURAL"; + if (joinOp->naturalKw) + keywords << "NATURAL"; - if (joinOp->leftKw) - { - keywords << "LEFT"; - if (joinOp->outerKw) - keywords << "OUTER"; - } - else if (joinOp->innerKw) - keywords << "INNER"; - else if (joinOp->crossKw) - keywords << "CROSS"; - - keywords << "JOIN"; - break; - } - case Dialect::Sqlite2: - { - if (joinOp->naturalKw) - keywords << "NATURAL"; - - if (joinOp->leftKw) - keywords << "LEFT"; - else if (joinOp->rightKw) - keywords << "RIGHT"; - else if (joinOp->fullKw) - keywords << "FULL"; - - if (joinOp->innerKw) - keywords << "INNER"; - else if (joinOp->crossKw) - keywords << "CROSS"; - else if (joinOp->outerKw) - keywords << "OUTER"; - - keywords << "JOIN"; - break; - } + if (joinOp->leftKw) + { + keywords << "LEFT"; + if (joinOp->outerKw) + keywords << "OUTER"; } + else if (joinOp->innerKw) + keywords << "INNER"; + else if (joinOp->crossKw) + keywords << "CROSS"; + + keywords << "JOIN"; if (keywords.size() == 0) return; diff --git a/Plugins/SqlEnterpriseFormatter/formatstatement.cpp b/Plugins/SqlEnterpriseFormatter/formatstatement.cpp index fbfec2b..be924e1 100644 --- a/Plugins/SqlEnterpriseFormatter/formatstatement.cpp +++ b/Plugins/SqlEnterpriseFormatter/formatstatement.cpp @@ -29,6 +29,8 @@ #include "formatdropview.h" #include "formatorderby.h" #include "formatupsert.h" +#include "formatwindowdefinition.h" +#include "formatfilterover.h" #include "parser/ast/sqliteselect.h" #include "parser/ast/sqliteexpr.h" #include "parser/ast/sqlitelimit.h" @@ -59,6 +61,8 @@ #include "parser/ast/sqliteorderby.h" #include "parser/ast/sqlitepragma.h" #include "parser/ast/sqliteupsert.h" +#include "parser/ast/sqlitewindowdefinition.h" +#include "parser/ast/sqlitefilterover.h" #include "sqlenterpriseformatter.h" #include "common/utils_sql.h" #include "common/global.h" @@ -155,13 +159,21 @@ FormatStatement *FormatStatement::forQuery(SqliteStatement *query) FORMATTER_FACTORY_ENTRY(query, SqliteDropView, FormatDropView); FORMATTER_FACTORY_ENTRY(query, SqliteOrderBy, FormatOrderBy); FORMATTER_FACTORY_ENTRY(query, SqlitePragma, FormatPragma); + FORMATTER_FACTORY_ENTRY(query, SqliteWindowDefinition, FormatWindowDefinition); + FORMATTER_FACTORY_ENTRY(query, SqliteWindowDefinition::Window, FormatWindowDefinitionWindow); + FORMATTER_FACTORY_ENTRY(query, SqliteWindowDefinition::Window::Frame, FormatWindowDefinitionWindowFrame); + FORMATTER_FACTORY_ENTRY(query, SqliteWindowDefinition::Window::Frame::Bound, FormatWindowDefinitionWindowFrameBound); + FORMATTER_FACTORY_ENTRY(query, SqliteFilterOver, FormatFilterOver); + FORMATTER_FACTORY_ENTRY(query, SqliteFilterOver::Filter, FormatFilterOverFilter); + FORMATTER_FACTORY_ENTRY(query, SqliteFilterOver::Over, FormatFilterOverOver); - if (stmt) - stmt->dialect = query->dialect; - else if (query) - qWarning() << "Unhandled query passed to enterprise formatter!"; - else - qWarning() << "Null query passed to enterprise formatter!"; + if (!stmt) + { + if (query) + qWarning() << "Unhandled query passed to enterprise formatter!"; + else + qWarning() << "Null query passed to enterprise formatter!"; + } return stmt; } @@ -188,6 +200,12 @@ FormatStatement& FormatStatement::withId(const QString& id) return *this; } +FormatStatement& FormatStatement::withId(const QString& id, bool wrapIfNeeded) +{ + withToken(wrapIfNeeded ? FormatToken::ID : FormatToken::ID_NO_WRAP, id); + return *this; +} + FormatStatement& FormatStatement::withOperator(const QString& oper, FormatToken::Flags flags) { withToken(FormatToken::OPERATOR, oper, flags); @@ -338,6 +356,12 @@ FormatStatement& FormatStatement::withLiteral(const QVariant& value) if (value.isNull()) return *this; + if (value.userType() == QVariant::String) + { + withString(value.toString()); + return *this; + } + bool ok; if (value.userType() == QVariant::Double) { @@ -372,7 +396,7 @@ FormatStatement& FormatStatement::withStatement(SqliteStatement* stmt, const QSt if (!stmt) return *this; - FormatStatement* formatStmt = forQuery(stmt, dialect, wrapper, cfg); + FormatStatement* formatStmt = forQuery(stmt, wrapper, cfg); if (!formatStmt) return *this; @@ -559,6 +583,7 @@ QString FormatStatement::detokenize() { // No 'break', so we go to next case, the regular KEYWORD } + __attribute__((__fallthrough__)); } case FormatToken::KEYWORD: { @@ -570,13 +595,19 @@ QString FormatStatement::detokenize() case FormatToken::DATA_TYPE: { applyIndent(); - line += wrapObjIfNeeded(token->value.toString(), dialect, wrapper); + line += wrapObjIfNeeded(token->value.toString(), wrapper); break; } case FormatToken::ID: { applyIndent(); - formatId(token->value.toString()); + formatId(token->value.toString(), true); + break; + } + case FormatToken::ID_NO_WRAP: + { + applyIndent(); + formatId(token->value.toString(), false); break; } case FormatToken::STRING_OR_ID: @@ -584,17 +615,22 @@ QString FormatStatement::detokenize() applyIndent(); QString val = token->value.toString(); if (val.contains("\"")) - formatId(token->value.toString()); + formatId(token->value.toString(), true); else line += wrapObjName(token->value.toString(), NameWrapper::DOUBLE_QUOTE); break; } - case FormatToken::STAR: - case FormatToken::INTEGER: + case FormatToken::STRING: + { + applyIndent(); + line += wrapString(token->value.toString()); + break; + } case FormatToken::BLOB: case FormatToken::BIND_PARAM: - case FormatToken::STRING: + case FormatToken::STAR: + case FormatToken::INTEGER: { applyIndent(); line += token->value.toString(); @@ -816,6 +852,7 @@ bool FormatStatement::isSpaceExpectingType(FormatStatement::FormatToken::Type ty case FormatToken::KEYWORD: case FormatToken::LINED_UP_KEYWORD: case FormatToken::ID: + case FormatToken::ID_NO_WRAP: case FormatToken::STRING_OR_ID: case FormatToken::FLOAT: case FormatToken::STRING: @@ -861,6 +898,7 @@ bool FormatStatement::isMetaType(FormatStatement::FormatToken::Type type) case FormatToken::KEYWORD: case FormatToken::LINED_UP_KEYWORD: case FormatToken::ID: + case FormatToken::ID_NO_WRAP: case FormatToken::STRING_OR_ID: case FormatToken::FLOAT: case FormatToken::STRING: @@ -1079,20 +1117,25 @@ bool FormatStatement::willStartWithNewLine(FormatStatement::FormatToken* token) (token->type == FormatToken::NEW_LINE); } -void FormatStatement::formatId(const QString& value) +void FormatStatement::formatId(const QString& value, bool applyWrapping) { + if (!applyWrapping) + { + line += value; + return; + } + if (cfg->SqlEnterpriseFormatter.AlwaysUseNameWrapping.get()) - line += wrapObjName(value, dialect, true, wrapper); + line += wrapObjName(value, true, wrapper); else - line += wrapObjIfNeeded(value, dialect, true, wrapper); + line += wrapObjIfNeeded(value, true, wrapper); } -FormatStatement* FormatStatement::forQuery(SqliteStatement* query, Dialect dialect, NameWrapper wrapper, Cfg::SqlEnterpriseFormatterConfig* cfg) +FormatStatement* FormatStatement::forQuery(SqliteStatement* query, NameWrapper wrapper, Cfg::SqlEnterpriseFormatterConfig* cfg) { FormatStatement* formatStmt = forQuery(query); if (formatStmt) { - formatStmt->dialect = dialect; formatStmt->wrapper = wrapper; formatStmt->cfg = cfg; } diff --git a/Plugins/SqlEnterpriseFormatter/formatstatement.h b/Plugins/SqlEnterpriseFormatter/formatstatement.h index 171b2a2..d7a432f 100644 --- a/Plugins/SqlEnterpriseFormatter/formatstatement.h +++ b/Plugins/SqlEnterpriseFormatter/formatstatement.h @@ -23,6 +23,7 @@ class FormatStatement KEYWORD, LINED_UP_KEYWORD, ID, + ID_NO_WRAP, STRING_OR_ID, OPERATOR, STAR, @@ -91,6 +92,7 @@ class FormatStatement FormatStatement& withKeyword(const QString& kw); FormatStatement& withLinedUpKeyword(const QString& kw, const QString& lineUpName = QString()); FormatStatement& withId(const QString& id); + FormatStatement& withId(const QString& id, bool wrapIfNeeded); FormatStatement& withIdList(const QStringList& names, const QString& indentName = QString(), ListSeparator sep = ListSeparator::COMMA); FormatStatement& withOperator(const QString& oper, FormatToken::Flags flags = FormatToken::Flag::NO_FLAG); FormatStatement& withStringOrId(const QString& id); @@ -157,7 +159,7 @@ class FormatStatement template <class T> T* getFormatStatement(SqliteStatement* stmt) { - return dynamic_cast<T*>(forQuery(stmt, dialect, wrapper, cfg)); + return dynamic_cast<T*>(forQuery(stmt, wrapper, cfg)); } protected: @@ -166,7 +168,6 @@ class FormatStatement virtual void formatInternal() = 0; virtual void resetInternal(); - Dialect dialect = Dialect::Sqlite3; NameWrapper wrapper = NameWrapper::BRACKET; Cfg::SqlEnterpriseFormatterConfig* cfg = nullptr; @@ -196,10 +197,10 @@ class FormatStatement QString getFinalLineUpName(const QString& lineUpName); int predictCurrentIndent(FormatToken* currentMetaToken); bool willStartWithNewLine(FormatToken* token); - void formatId(const QString& value); + void formatId(const QString& value, bool wrappingRequested); int getLineUpValue(const QString& lineUpName); - static FormatStatement* forQuery(SqliteStatement *query, Dialect dialect, NameWrapper wrapper, Cfg::SqlEnterpriseFormatterConfig* cfg); + static FormatStatement* forQuery(SqliteStatement *query, NameWrapper wrapper, Cfg::SqlEnterpriseFormatterConfig* cfg); QHash<QString,int> kwLineUpPosition; QHash<QString,int> namedIndents; diff --git a/Plugins/SqlEnterpriseFormatter/formatupdate.cpp b/Plugins/SqlEnterpriseFormatter/formatupdate.cpp index 641c895..0adeb17 100644 --- a/Plugins/SqlEnterpriseFormatter/formatupdate.cpp +++ b/Plugins/SqlEnterpriseFormatter/formatupdate.cpp @@ -49,6 +49,9 @@ void FormatUpdate::formatInternal() withDecrIndent(); + if (upd->from) + withNewLine().withLinedUpKeyword("FROM").withStatement(upd->from, "updateColumns"); + if (upd->where) withNewLine().withLinedUpKeyword("WHERE").withStatement(upd->where); diff --git a/Plugins/SqlEnterpriseFormatter/formatvacuum.cpp b/Plugins/SqlEnterpriseFormatter/formatvacuum.cpp index 8cbff57..2bea793 100644 --- a/Plugins/SqlEnterpriseFormatter/formatvacuum.cpp +++ b/Plugins/SqlEnterpriseFormatter/formatvacuum.cpp @@ -1,5 +1,6 @@ #include "formatvacuum.h" #include "parser/ast/sqlitevacuum.h" +#include "parser/ast/sqliteexpr.h" FormatVacuum::FormatVacuum(SqliteVacuum* vacuum) : vacuum(vacuum) @@ -10,4 +11,9 @@ void FormatVacuum::formatInternal() { handleExplainQuery(vacuum); withKeyword("VACUUM").withSemicolon(); + if (!vacuum->database.isNull()) + withId(vacuum->database); + + if (vacuum->expr) + withKeyword("INTO").withStatement(vacuum->expr); } diff --git a/Plugins/SqlEnterpriseFormatter/formatwindowdefinition.cpp b/Plugins/SqlEnterpriseFormatter/formatwindowdefinition.cpp new file mode 100644 index 0000000..5808af4 --- /dev/null +++ b/Plugins/SqlEnterpriseFormatter/formatwindowdefinition.cpp @@ -0,0 +1,91 @@ +#include "formatwindowdefinition.h" +#include "parser/ast/sqliteexpr.h" +#include "parser/ast/sqliteorderby.h" + +FormatWindowDefinition::FormatWindowDefinition(SqliteWindowDefinition* windowDef) : + windowDef(windowDef) +{ +} + +void FormatWindowDefinition::formatInternal() +{ + withId(windowDef->name).withKeyword("AS").withParExprLeft().withStatement(windowDef->window).withParExprRight(); +} + +FormatWindowDefinitionWindow::FormatWindowDefinitionWindow(SqliteWindowDefinition::Window* window) : + window(window) +{ +} + +void FormatWindowDefinitionWindow::formatInternal() +{ + if (!window->name.isNull()) + withId(window->name); + + switch (window->mode) + { + case SqliteWindowDefinition::Window::Mode::PARTITION_BY: + withKeyword("PARTITION").withKeyword("BY").withStatementList(window->exprList); + break; + case SqliteWindowDefinition::Window::Mode::ORDER_BY: + break; + case SqliteWindowDefinition::Window::Mode::null: + break; + } + + if (window->orderBy.size() > 0) + withKeyword("ORDER").withKeyword("BY").withStatementList(window->orderBy); + + if (window->frame) + withStatement(window->frame); +} + +FormatWindowDefinitionWindowFrame::FormatWindowDefinitionWindowFrame(SqliteWindowDefinition::Window::Frame* frame) : + frame(frame) +{ +} + +void FormatWindowDefinitionWindowFrame::formatInternal() +{ + if (frame->rangeOrRows != SqliteWindowDefinition::Window::Frame::RangeOrRows::null) + withKeyword(SqliteWindowDefinition::Window::Frame::fromRangeOrRows(frame->rangeOrRows)); + + if (frame->endBound) + withKeyword("BETWEEN").withStatement(frame->startBound).withKeyword("AND").withStatement(frame->endBound); + else + withStatement(frame->startBound); + + if (frame->exclude != SqliteWindowDefinition::Window::Frame::Exclude::null) + { + withKeyword("EXCLUDE"); + for (const QString& kw : SqliteWindowDefinition::Window::Frame::fromExclude(frame->exclude).split(" ")) + withKeyword(kw); + } +} + +FormatWindowDefinitionWindowFrameBound::FormatWindowDefinitionWindowFrameBound(SqliteWindowDefinition::Window::Frame::Bound* bound) : + bound(bound) +{ +} + +void FormatWindowDefinitionWindowFrameBound::formatInternal() +{ + switch (bound->type) + { + case SqliteWindowDefinition::Window::Frame::Bound::Type::UNBOUNDED_PRECEDING: + withKeyword("UNBOUNDED").withKeyword("PRECEDING"); + break; + case SqliteWindowDefinition::Window::Frame::Bound::Type::UNBOUNDED_FOLLOWING: + withKeyword("UNBOUNDED").withKeyword("FOLLOWING"); + break; + case SqliteWindowDefinition::Window::Frame::Bound::Type::EXPR_PRECEDING: + withStatement(bound->expr).withKeyword("PRECEDING"); + break; + case SqliteWindowDefinition::Window::Frame::Bound::Type::EXPR_FOLLOWING: + withStatement(bound->expr).withKeyword("FOLLOWING"); + break; + case SqliteWindowDefinition::Window::Frame::Bound::Type::CURRENT_ROW: + withKeyword("CURRENT").withKeyword("ROW"); + break; + } +} diff --git a/Plugins/SqlEnterpriseFormatter/formatwindowdefinition.h b/Plugins/SqlEnterpriseFormatter/formatwindowdefinition.h new file mode 100644 index 0000000..1918b81 --- /dev/null +++ b/Plugins/SqlEnterpriseFormatter/formatwindowdefinition.h @@ -0,0 +1,55 @@ +#ifndef FORMATWINDOWDEFINITION_H +#define FORMATWINDOWDEFINITION_H + +#include "formatstatement.h" +#include "parser/ast/sqlitewindowdefinition.h" + +class FormatWindowDefinition : public FormatStatement +{ + public: + FormatWindowDefinition(SqliteWindowDefinition* windowDef); + + protected: + void formatInternal(); + + private: + SqliteWindowDefinition* windowDef = nullptr; +}; + +class FormatWindowDefinitionWindow : public FormatStatement +{ + public: + FormatWindowDefinitionWindow(SqliteWindowDefinition::Window* window); + + protected: + void formatInternal(); + + private: + SqliteWindowDefinition::Window* window = nullptr; +}; + +class FormatWindowDefinitionWindowFrame : public FormatStatement +{ + public: + FormatWindowDefinitionWindowFrame(SqliteWindowDefinition::Window::Frame* frame); + + protected: + void formatInternal(); + + private: + SqliteWindowDefinition::Window::Frame* frame = nullptr; +}; + +class FormatWindowDefinitionWindowFrameBound : public FormatStatement +{ + public: + FormatWindowDefinitionWindowFrameBound(SqliteWindowDefinition::Window::Frame::Bound* bound); + + protected: + void formatInternal(); + + private: + SqliteWindowDefinition::Window::Frame::Bound* bound = nullptr; +}; + +#endif // FORMATWINDOWDEFINITION_H diff --git a/Plugins/SqlEnterpriseFormatter/package.xml b/Plugins/SqlEnterpriseFormatter/package.xml deleted file mode 100644 index 643cd6a..0000000 --- a/Plugins/SqlEnterpriseFormatter/package.xml +++ /dev/null @@ -1,10 +0,0 @@ -<?xml version="1.0"?>
-<Package>
- <DisplayName>SQL enterprise formatter plugin</DisplayName>
- <Description>Plugin for advanced, highly customizable SQL formatting.</Description>
- <Version>%VERSION%</Version>
- <ReleaseDate>%DATE%</ReleaseDate>
- <Name>pl.com.salsoft.sqlitestudio.plugins.sqlenterpriseformatter</Name>
- <Dependencies>pl.com.salsoft.sqlitestudio.plugins</Dependencies>
- <Default>true</Default>
-</Package>
\ No newline at end of file diff --git a/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.cpp b/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.cpp index 8bed056..62517c6 100644 --- a/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.cpp +++ b/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.cpp @@ -27,7 +27,7 @@ QString SqlEnterpriseFormatter::format(SqliteQueryPtr query) QString formatted = formatStmt->format(); delete formatStmt; - QString formattedWithComments = applyComments(formatted, comments, query->dialect); + QString formattedWithComments = applyComments(formatted, comments); for (Comment* c : comments) delete c; @@ -46,7 +46,7 @@ bool SqlEnterpriseFormatter::init() ");"); static_qstring(query4, "CREATE UNIQUE INDEX IF NOT EXISTS dbName.idx1 ON [messages column] (id COLLATE x ASC, lang DESC, description);"); - Parser parser(Dialect::Sqlite3); + Parser parser; for (const QString& q : {query1, query2, query3, query4}) { @@ -271,14 +271,14 @@ void SqlEnterpriseFormatter::wrapComment(const TokenPtr &token, bool isAtLineEnd token->value = multiCommentTpl.arg(token->value); } -QString SqlEnterpriseFormatter::applyComments(const QString& formatted, QList<SqlEnterpriseFormatter::Comment*> comments, Dialect dialect) +QString SqlEnterpriseFormatter::applyComments(const QString& formatted, QList<SqlEnterpriseFormatter::Comment*> comments) { if (comments.size() == 0) return formatted; int currentCommentPosition = comments.first()->position; - TokenList allTokens = Lexer::tokenize(formatted, dialect); + TokenList allTokens = Lexer::tokenize(formatted); TokenList newTokens; int currentTokenPosition = 0; for (const TokenPtr& token : allTokens) diff --git a/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.h b/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.h index 1f9b6d8..f0dd964 100644 --- a/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.h +++ b/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.h @@ -87,7 +87,7 @@ class SQLENTERPRISEFORMATTERSHARED_EXPORT SqlEnterpriseFormatter : public Generi }; QList<Comment*> collectComments(const TokenList& tokens); - QString applyComments(const QString& formatted, QList<Comment *> comments, Dialect dialect); + QString applyComments(const QString& formatted, QList<Comment *> comments); QList<TokenList> tokensByLines(const TokenList& tokens, bool includeSpaces = false); TokenList adjustCommentsToEnd(const TokenList& inputTokens); TokenList wrapOnlyComments(const TokenList& inputTokens); diff --git a/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.json b/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.json index c0ba2df..da01e41 100644 --- a/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.json +++ b/Plugins/SqlEnterpriseFormatter/sqlenterpriseformatter.json @@ -2,6 +2,6 @@ "type": "CodeFormatterPlugin", "title": "SQL Enterprise", "description": "Advanced SQL formatter.", - "version": 10006, + "version": 10007, "author": "SalSoft" } |
