From 498285840a26559c9b347122622d45a68bbc11ff Mon Sep 17 00:00:00 2001 From: hogimn Date: Sun, 15 Sep 2024 08:11:48 +0900 Subject: [PATCH] Clean up code --- .../Behavior/BehavesLikeSqlFormatter.cs | 4 +- SQL.Formatter.Test/Db2FormatterTest.cs | 16 +- SQL.Formatter.Test/MariaDbFormatterTest.cs | 4 +- SQL.Formatter.Test/MySqlFormatterTest.cs | 6 +- SQL.Formatter.Test/N1qlFormatterTest.cs | 36 ++--- SQL.Formatter.Test/PlSqlFormatterTest.cs | 48 +++--- SQL.Formatter.Test/PostgreSqlFormatterTest.cs | 28 ++-- SQL.Formatter.Test/RedShiftFormatterTest.cs | 28 ++-- SQL.Formatter.Test/SparkSqlFormatter.cs | 28 ++-- SQL.Formatter.Test/SqlFormatterTest.cs | 6 +- .../StandardSqlFormatterTest.cs | 26 ++-- SQL.Formatter.Test/TSqlFormatterTest.cs | 28 ++-- SQL.Formatter/Core/AbstractFormatter.cs | 138 +++++++++--------- SQL.Formatter/Core/DialectConfig.cs | 96 ++++++------ SQL.Formatter/Core/FormatConfig.cs | 60 ++++---- SQL.Formatter/Core/Indentation.cs | 28 ++-- SQL.Formatter/Core/InlineBlock.cs | 38 ++--- SQL.Formatter/Core/Params.cs | 26 ++-- SQL.Formatter/Core/Token.cs | 56 +++---- SQL.Formatter/Core/Tokenizer.cs | 128 ++++++++-------- SQL.Formatter/Core/Util/JSLikeList.cs | 22 +-- SQL.Formatter/Core/Util/RegexUtil.cs | 6 +- SQL.Formatter/Language/Db2Formatter.cs | 16 +- SQL.Formatter/Language/Dialect.cs | 18 +-- SQL.Formatter/Language/MariaDbFormatter.cs | 16 +- SQL.Formatter/Language/MySqlFormatter.cs | 16 +- SQL.Formatter/Language/N1qlFormatter.cs | 16 +- SQL.Formatter/Language/PlSqlFormatter.cs | 20 +-- SQL.Formatter/Language/PostgreSqlFormatter.cs | 16 +- SQL.Formatter/Language/RedshiftFormatter.cs | 16 +- SQL.Formatter/Language/SparkSqlFormatter.cs | 26 ++-- .../Language/StandardSqlFormatter.cs | 16 +- SQL.Formatter/Language/StringLiteral.cs | 18 +-- SQL.Formatter/Language/TSqlFormatter.cs | 16 +- SQL.Formatter/SqlFormatter.cs | 4 +- 35 files changed, 536 insertions(+), 534 deletions(-) diff --git a/SQL.Formatter.Test/Behavior/BehavesLikeSqlFormatter.cs b/SQL.Formatter.Test/Behavior/BehavesLikeSqlFormatter.cs index 107921c..342beec 100644 --- a/SQL.Formatter.Test/Behavior/BehavesLikeSqlFormatter.cs +++ b/SQL.Formatter.Test/Behavior/BehavesLikeSqlFormatter.cs @@ -452,8 +452,8 @@ public static void FormatsLonelySemicolon(SqlFormatter.Formatter formatter) public static void DoesNothingWithEmptyInput(SqlFormatter.Formatter formatter) { - var result = formatter.Format(""); - Assert.Equal("", result); + var result = formatter.Format(string.Empty); + Assert.Equal(string.Empty, result); } } } diff --git a/SQL.Formatter.Test/Db2FormatterTest.cs b/SQL.Formatter.Test/Db2FormatterTest.cs index 44f96ed..917dc1b 100644 --- a/SQL.Formatter.Test/Db2FormatterTest.cs +++ b/SQL.Formatter.Test/Db2FormatterTest.cs @@ -8,30 +8,30 @@ namespace SQL.Formatter.Test { public class Db2FormatterTest { - public readonly SqlFormatter.Formatter formatter = SqlFormatter.Of(Dialect.Db2); + public readonly SqlFormatter.Formatter Formatter = SqlFormatter.Of(Dialect.Db2); [Fact] public void BehavesLikeSqlFormatterTest() { - BehavesLikeSqlFormatter.Test(formatter); + BehavesLikeSqlFormatter.Test(Formatter); } [Fact] public void CreateTableTest() { - CreateTable.Test(formatter); + CreateTable.Test(Formatter); } [Fact] public void AlterTableTest() { - AlterTable.Test(formatter); + AlterTable.Test(Formatter); } [Fact] public void StringsTest() { - Strings.Test(formatter, new List + Strings.Test(Formatter, new List { StringLiteral.DoubleQuote, StringLiteral.SingleQuote, @@ -42,13 +42,13 @@ public void StringsTest() [Fact] public void BetweenTest() { - Between.Test(formatter); + Between.Test(Formatter); } [Fact] public void SchemaTest() { - Schema.Test(formatter); + Schema.Test(Formatter); } [Fact] @@ -63,7 +63,7 @@ public void FormatsFetchFirstLikeLimit() + " col2 DESC\n" + "FETCH FIRST\n" + " 20 ROWS ONLY;", - formatter.Format( + Formatter.Format( "SELECT col1 FROM tbl ORDER BY col2 DESC FETCH FIRST 20 ROWS ONLY;")); } } diff --git a/SQL.Formatter.Test/MariaDbFormatterTest.cs b/SQL.Formatter.Test/MariaDbFormatterTest.cs index 8e04ee8..7e8c014 100644 --- a/SQL.Formatter.Test/MariaDbFormatterTest.cs +++ b/SQL.Formatter.Test/MariaDbFormatterTest.cs @@ -6,12 +6,12 @@ namespace SQL.Formatter.Test { public class MariaDbFormatterTest { - public readonly SqlFormatter.Formatter formatter = SqlFormatter.Of(Dialect.MariaDb); + public readonly SqlFormatter.Formatter Formatter = SqlFormatter.Of(Dialect.MariaDb); [Fact] public void BehavesLikeMariaDbFormatterTest() { - BehavesLikeMariaDbFormatter.Test(formatter); + BehavesLikeMariaDbFormatter.Test(Formatter); } } } diff --git a/SQL.Formatter.Test/MySqlFormatterTest.cs b/SQL.Formatter.Test/MySqlFormatterTest.cs index ca65e25..11785c6 100644 --- a/SQL.Formatter.Test/MySqlFormatterTest.cs +++ b/SQL.Formatter.Test/MySqlFormatterTest.cs @@ -8,18 +8,18 @@ namespace SQL.Formatter.Test { public class MySqlFormatterTest { - public readonly SqlFormatter.Formatter formatter = SqlFormatter.Of(Dialect.MySql); + public readonly SqlFormatter.Formatter Formatter = SqlFormatter.Of(Dialect.MySql); [Fact] public void BehavesLikeMariaDbFormatterTest() { - BehavesLikeMariaDbFormatter.Test(formatter); + BehavesLikeMariaDbFormatter.Test(Formatter); } [Fact] public void AdditionalMySqlOperator() { - Operators.Test(formatter, new List + Operators.Test(Formatter, new List { "->", "->>" diff --git a/SQL.Formatter.Test/N1qlFormatterTest.cs b/SQL.Formatter.Test/N1qlFormatterTest.cs index fa3e06a..bd5ae98 100644 --- a/SQL.Formatter.Test/N1qlFormatterTest.cs +++ b/SQL.Formatter.Test/N1qlFormatterTest.cs @@ -8,18 +8,18 @@ namespace SQL.Formatter.Test { public class N1qlFormatterTest { - public readonly SqlFormatter.Formatter formatter = SqlFormatter.Of(Dialect.N1ql); + public readonly SqlFormatter.Formatter Formatter = SqlFormatter.Of(Dialect.N1ql); [Fact] public void BehavesLikeSqlFormatterTest() { - BehavesLikeSqlFormatter.Test(formatter); + BehavesLikeSqlFormatter.Test(Formatter); } [Fact] public void StringsTest() { - Strings.Test(formatter, new List + Strings.Test(Formatter, new List { StringLiteral.DoubleQuote, StringLiteral.SingleQuote, @@ -30,19 +30,19 @@ public void StringsTest() [Fact] public void BetweenTest() { - Between.Test(formatter); + Between.Test(Formatter); } [Fact] public void SchemaTest() { - Schema.Test(formatter); + Schema.Test(Formatter); } [Fact] public void OperatorsTest() { - Operators.Test(formatter, new List + Operators.Test(Formatter, new List { "%", "==", @@ -53,7 +53,7 @@ public void OperatorsTest() [Fact] public void JoinTest() { - Join.Test(formatter, new List + Join.Test(Formatter, new List { "FULL", "CROSS", "NATURAL" }); @@ -67,7 +67,7 @@ public void ReplacesDollarNumberedPlaceholdersWithParamValues() + " second,\n" + " third,\n" + " first;", - formatter.Format( + Formatter.Format( "SELECT $1, $2, $0;", new Dictionary { @@ -86,7 +86,7 @@ public void ReplacesDollarVariablesWithParamValues() + @" 'var value'," + "\n" + @" 'var value'," + "\n" + @" 'var value';", - formatter.Format( + Formatter.Format( @"SELECT $variable, $'var name', $""var name"", $`var name`;", new Dictionary { @@ -104,7 +104,7 @@ public void RecognizesDollarVariables() + @" $'var name'," + "\n" + @" $""var name""," + "\n" + @" $`var name`;", - formatter.Format( + Formatter.Format( @"SELECT $variable, $'var name', $""var name"", $`var name`;")); } @@ -118,7 +118,7 @@ public void FormatsUpdateQueryWithUseKeysAndReturning() + " 'baldwin'\n" + "SET\n" + " type = 'actor' RETURNING tutorial.type", - formatter.Format( + Formatter.Format( "UPDATE tutorial USE KEYS 'baldwin' SET type = 'actor' RETURNING tutorial.type")); } @@ -130,7 +130,7 @@ public void FormatsExplainedDeleteQueryWithUseKeysAndReturning() + " tutorial t\n" + "USE KEYS\n" + " 'baldwin' RETURNING t", - formatter.Format( + Formatter.Format( "EXPLAIN DELETE FROM tutorial t USE KEYS 'baldwin' RETURNING t")); } @@ -146,7 +146,7 @@ public void FormatsSelectQueryWithNestAndUseKeys() + " 'Elinor_33313792'\n" + "NEST\n" + " orders_with_users orders ON KEYS ARRAY s.order_id FOR s IN usr.shipped_order_history END;", - formatter.Format( + Formatter.Format( "SELECT * FROM usr\n" + "USE KEYS 'Elinor_33313792' NEST orders_with_users orders\n" + "ON KEYS ARRAY s.order_id FOR s IN usr.shipped_order_history END;")); @@ -162,7 +162,7 @@ public void FormatsSelectQueryWithUnnestTopLevelReservedWord() + " tutorial\n" + "UNNEST\n" + " tutorial.children c;", - formatter.Format( + Formatter.Format( "SELECT * FROM tutorial UNNEST tutorial.children c;")); } @@ -188,7 +188,7 @@ public void FormatsInsertWithLargeObjectAndArrayLiterals() + " 'hello': 'world'\n" + " }\n" + " );", - formatter.Format( + Formatter.Format( "INSERT INTO heroes (KEY, VALUE) VALUES ('123', {'id': 1, 'type': 'Tarzan',\n" + "'array': [123456789, 123456789, 123456789, 123456789, 123456789], 'hello': 'world'});")); } @@ -201,7 +201,7 @@ public void FormatsInsertWithCurlyBracketObjectLiteral() + " heroes (KEY, VALUE)\n" + "VALUES\n" + " ('123', {'id': 1, 'type': 'Tarzan'});", - formatter.Format( + Formatter.Format( "INSERT INTO heroes (KEY, VALUE) VALUES ('123', {'id':1,'type':'Tarzan'});")); } @@ -215,7 +215,7 @@ public void FormatsSelectQueryWithPrimaryKeyQuerying() + " tutorial\n" + "USE KEYS\n" + " ['dave', 'ian'];", - formatter.Format( + Formatter.Format( "SELECT fname, email FROM tutorial USE KEYS ['dave', 'ian'];")); } @@ -227,7 +227,7 @@ public void FormatsSelectQueryWithElementSelectionExpression() + " order_lines[0].productId\n" + "FROM\n" + " orders;", - formatter.Format( + Formatter.Format( "SELECT order_lines[0].productId FROM orders;")); } } diff --git a/SQL.Formatter.Test/PlSqlFormatterTest.cs b/SQL.Formatter.Test/PlSqlFormatterTest.cs index 5e74439..827f2bc 100644 --- a/SQL.Formatter.Test/PlSqlFormatterTest.cs +++ b/SQL.Formatter.Test/PlSqlFormatterTest.cs @@ -8,42 +8,42 @@ namespace SQL.Formatter.Test { public class PlSqlFormatterTest { - public readonly SqlFormatter.Formatter formatter = SqlFormatter.Of(Dialect.PlSql); + public readonly SqlFormatter.Formatter Formatter = SqlFormatter.Of(Dialect.PlSql); [Fact] public void BehavesLikeSqlFormatterTest() { - BehavesLikeSqlFormatter.Test(formatter); + BehavesLikeSqlFormatter.Test(Formatter); } [Fact] public void CaseTest() { - Case.Test(formatter); + Case.Test(Formatter); } [Fact] public void CreateTableTest() { - CreateTable.Test(formatter); + CreateTable.Test(Formatter); } [Fact] public void AlterTableTest() { - AlterTable.Test(formatter); + AlterTable.Test(Formatter); } [Fact] public void AlterTableModifyTest() { - AlterTableModify.Test(formatter); + AlterTableModify.Test(Formatter); } [Fact] public void StringsTest() { - Strings.Test(formatter, new List + Strings.Test(Formatter, new List { StringLiteral.DoubleQuote, StringLiteral.SingleQuote, @@ -55,19 +55,19 @@ public void StringsTest() [Fact] public void BetweenTest() { - Between.Test(formatter); + Between.Test(Formatter); } [Fact] public void SchemaTest() { - Schema.Test(formatter); + Schema.Test(Formatter); } [Fact] public void OperatorsTest() { - Operators.Test(formatter, new List + Operators.Test(Formatter, new List { "||", "**", @@ -79,7 +79,7 @@ public void OperatorsTest() [Fact] public void JoinTest() { - Join.Test(formatter); + Join.Test(Formatter); } [Fact] @@ -119,7 +119,7 @@ public void FormatsOracleRecursiveSubQueriesRegardelessOfCapitalization() + " t1\n" + "ORDER BY\n" + " order1;", - formatter.Format( + Formatter.Format( "WITH t1(id, parent_id) AS (\n" + " -- Anchor member.\n" + " SELECT\n" @@ -181,7 +181,7 @@ public void FormatsOracleRecursiveSubQueries() + " t1\n" + "ORDER BY\n" + " order1;", - formatter.Format( + Formatter.Format( "WITH t1(id, parent_id) AS (\n" + " -- Anchor member.\n" + " SELECT\n" @@ -216,7 +216,7 @@ public void FormatsSelectQueryWithOuterApply() + "FROM\n" + " t\n" + " OUTER APPLY fn(t.id)", - formatter.Format( + Formatter.Format( "SELECT a, b FROM t OUTER APPLY fn(t.id)")); } @@ -226,7 +226,7 @@ public void FormatsSimpleSelectWithNationalCharacters() Assert.Equal( "SELECT\n" + " N'value'", - formatter.Format( + Formatter.Format( "SELECT N'value'")); } @@ -239,7 +239,7 @@ public void FormatsSimpleSelect() + " M\n" + "FROM\n" + " t", - formatter.Format( + Formatter.Format( "SELECT N, M FROM t")); } @@ -253,7 +253,7 @@ public void FormatsSelectQueryWithCrossApply() + "FROM\n" + " t\n" + " CROSS APPLY fn(t.id)", - formatter.Format( + Formatter.Format( "SELECT a, b FROM t CROSS APPLY fn(t.id)")); } @@ -265,7 +265,7 @@ public void ReplacesQuestionmarkIndexedPlaceholdersWithParamValues() + " first,\n" + " second,\n" + " third;", - formatter.Format( + Formatter.Format( "SELECT ?, ?, ?;", new List { "first", @@ -282,7 +282,7 @@ public void ReplacesQuestionmarkNumberedPlaceholdersWithParamValues() + " second,\n" + " third,\n" + " first;", - formatter.Format( + Formatter.Format( "SELECT ?1, ?2, ?0;", new Dictionary { { "0", "first" }, @@ -299,7 +299,7 @@ public void RecognizesQuestionmarkPlaceholders() + " ?1,\n" + " ?25,\n" + " ?;", - formatter.Format( + Formatter.Format( "SELECT ?1, ?25, ?;")); } @@ -311,7 +311,7 @@ public void FormatsInsertWithoutInto() + " Customers (ID, MoneyBalance, Address, City)\n" + "VALUES\n" + " (12, -123.4, 'Skagen 2111', 'Stv');", - formatter.Format( + Formatter.Format( "INSERT Customers (ID, MoneyBalance, Address, City) VALUES (12,-123.4, 'Skagen 2111','Stv');")); } @@ -324,7 +324,7 @@ public void RecognizeSpecialCharactersAsPartOfIdentifiers() + " col.2@\n" + "FROM\n" + " tbl", - formatter.Format( + Formatter.Format( "SELECT my_col$1#, col.2@ FROM tbl\n")); } @@ -337,7 +337,7 @@ public void FormatsOnlyAsALineComment() + "FROM\n" + " -- This is a comment\n" + " MyTable;", - formatter.Format( + Formatter.Format( "SELECT col FROM\n-- This is a comment\nMyTable;\n")); } @@ -353,7 +353,7 @@ public void FormatsFetchFirstLikeLimit() + " col2 DESC\n" + "FETCH FIRST\n" + " 20 ROWS ONLY;", - formatter.Format( + Formatter.Format( "SELECT col1 FROM tbl ORDER BY col2 DESC FETCH FIRST 20 ROWS ONLY;")); } } diff --git a/SQL.Formatter.Test/PostgreSqlFormatterTest.cs b/SQL.Formatter.Test/PostgreSqlFormatterTest.cs index e082c12..9377914 100644 --- a/SQL.Formatter.Test/PostgreSqlFormatterTest.cs +++ b/SQL.Formatter.Test/PostgreSqlFormatterTest.cs @@ -8,36 +8,36 @@ namespace SQL.Formatter.Test { public class PostgreSqlFormatterTest { - public readonly SqlFormatter.Formatter formatter = SqlFormatter.Of(Dialect.PostgreSql); + public readonly SqlFormatter.Formatter Formatter = SqlFormatter.Of(Dialect.PostgreSql); [Fact] public void BehavesLikeSqlFormatterTest() { - BehavesLikeSqlFormatter.Test(formatter); + BehavesLikeSqlFormatter.Test(Formatter); } [Fact] public void CaseTest() { - Case.Test(formatter); + Case.Test(Formatter); } [Fact] public void CreateTableTest() { - CreateTable.Test(formatter); + CreateTable.Test(Formatter); } [Fact] public void AlterTableTest() { - AlterTable.Test(formatter); + AlterTable.Test(Formatter); } [Fact] public void StringsTest() { - Strings.Test(formatter, new List + Strings.Test(Formatter, new List { StringLiteral.DoubleQuote, StringLiteral.SingleQuote, @@ -50,19 +50,19 @@ public void StringsTest() [Fact] public void BetweenTest() { - Between.Test(formatter); + Between.Test(Formatter); } [Fact] public void SchemaTest() { - Schema.Test(formatter); + Schema.Test(Formatter); } [Fact] public void OperatorsTest() { - Operators.Test(formatter, new List + Operators.Test(Formatter, new List { "%", "^", @@ -96,7 +96,7 @@ public void OperatorsTest() [Fact] public void JoinTest() { - Join.Test(formatter); + Join.Test(Formatter); } [Fact] @@ -108,7 +108,7 @@ public void SupportDollarPlaceholders() + " $2\n" + "FROM\n" + " tbl", - formatter.Format( + Formatter.Format( "SELECT $1, $2 FROM tbl")); } @@ -121,7 +121,7 @@ public void ReplacesDollarPlaceholdersWithParamValues() + @" ""blah""" + "\n" + "FROM\n" + " tbl", - formatter.Format( + Formatter.Format( "SELECT $1, $2 FROM tbl", new Dictionary { { "1", @"""variable value""" }, @@ -134,7 +134,7 @@ public void SupportsColonNamePlaceholders() { Assert.Equal( "foo = :bar", - formatter.Format( + Formatter.Format( "foo = :bar")); } @@ -145,7 +145,7 @@ public void ReplacesColonNamePlaceholdersWithParamValues() "foo = 'Hello'\n" + "AND some_col = 10\n" + "OR col = 7", - formatter.Format( + Formatter.Format( @"foo = :bar AND :""field"" = 10 OR col = :'val'", new Dictionary { { "bar", "'Hello'" }, diff --git a/SQL.Formatter.Test/RedShiftFormatterTest.cs b/SQL.Formatter.Test/RedShiftFormatterTest.cs index 247b3de..306e804 100644 --- a/SQL.Formatter.Test/RedShiftFormatterTest.cs +++ b/SQL.Formatter.Test/RedShiftFormatterTest.cs @@ -8,36 +8,36 @@ namespace SQL.Formatter.Test { public class RedShiftFormatterTest { - public readonly SqlFormatter.Formatter formatter = SqlFormatter.Of(Dialect.Redshift); + public readonly SqlFormatter.Formatter Formatter = SqlFormatter.Of(Dialect.Redshift); [Fact] public void BehaviesLikeSqlFormatterTest() { - BehavesLikeSqlFormatter.Test(formatter); + BehavesLikeSqlFormatter.Test(Formatter); } [Fact] public void CreateTableTest() { - CreateTable.Test(formatter); + CreateTable.Test(Formatter); } [Fact] public void AlterTableTest() { - AlterTable.Test(formatter); + AlterTable.Test(Formatter); } [Fact] public void AlterTableModifyTest() { - AlterTableModify.Test(formatter); + AlterTableModify.Test(Formatter); } [Fact] public void StringsTest() { - Strings.Test(formatter, new List + Strings.Test(Formatter, new List { StringLiteral.DoubleQuote, StringLiteral.SingleQuote, @@ -48,13 +48,13 @@ public void StringsTest() [Fact] public void SchemaTest() { - Schema.Test(formatter); + Schema.Test(Formatter); } [Fact] public void OperatorsTest() { - Operators.Test(formatter, new List + Operators.Test(Formatter, new List { "%", "^", @@ -74,7 +74,7 @@ public void OperatorsTest() [Fact] public void JoinTest() { - Join.Test(formatter); + Join.Test(Formatter); } [Fact] @@ -89,7 +89,7 @@ public void FormatsLimit() + " col2 DESC\n" + "LIMIT\n" + " 10;", - formatter.Format( + Formatter.Format( "SELECT col1 FROM tbl ORDER BY col2 DESC LIMIT 10;")); } @@ -102,7 +102,7 @@ public void FormatsOnlyDoubleHypenAsALineComment() + "FROM\n" + " -- This is a comment\n" + " MyTable;", - formatter.Format( + Formatter.Format( "SELECT col FROM\n" + "-- This is a comment\n" + "MyTable;")); @@ -116,7 +116,7 @@ public void RecognizesAtAsPartOfIdentifiers() + " @col1\n" + "FROM\n" + " tbl", - formatter.Format( + Formatter.Format( "SELECT @col1 FROM tbl")); } @@ -132,7 +132,7 @@ public void FormatsDiskeyAndSortkeyAfterCreateTable() + ")\n" + "DISTKEY(created_at)\n" + "SORTKEY(created_at);", - formatter.Format( + Formatter.Format( "CREATE TABLE items (a INT PRIMARY KEY, b TEXT, c INT NOT NULL, d INT NOT NULL) DISTKEY(created_at) SORTKEY(created_at);")); } @@ -152,7 +152,7 @@ public void FormatsCopy() + " ',' QUOTE '\"'\n" + "REGION\n" + " AS 'us-east-1'", - formatter.Format( + Formatter.Format( "COPY schema.table\n" + "FROM 's3://bucket/file.csv'\n" + "IAM_ROLE 'arn:aws:iam::123456789:role/rolename'\n" diff --git a/SQL.Formatter.Test/SparkSqlFormatter.cs b/SQL.Formatter.Test/SparkSqlFormatter.cs index b2344fd..6a80d62 100644 --- a/SQL.Formatter.Test/SparkSqlFormatter.cs +++ b/SQL.Formatter.Test/SparkSqlFormatter.cs @@ -8,36 +8,36 @@ namespace SQL.Formatter.Test { public class SparkSqlFormatter { - public readonly SqlFormatter.Formatter formatter = SqlFormatter.Of(Dialect.SparkSql); + public readonly SqlFormatter.Formatter Formatter = SqlFormatter.Of(Dialect.SparkSql); [Fact] public void BehavesLikeSqlFormatterTest() { - BehavesLikeSqlFormatter.Test(formatter); + BehavesLikeSqlFormatter.Test(Formatter); } [Fact] public void CaseTest() { - Case.Test(formatter); + Case.Test(Formatter); } [Fact] public void CreateTableTest() { - CreateTable.Test(formatter); + CreateTable.Test(Formatter); } [Fact] public void AlterTableTest() { - AlterTable.Test(formatter); + AlterTable.Test(Formatter); } [Fact] public void StringsTest() { - Strings.Test(formatter, new List + Strings.Test(Formatter, new List { StringLiteral.DoubleQuote, StringLiteral.SingleQuote, @@ -48,19 +48,19 @@ public void StringsTest() [Fact] public void BetweenTest() { - Between.Test(formatter); + Between.Test(Formatter); } [Fact] public void SchemaTest() { - Schema.Test(formatter); + Schema.Test(Formatter); } [Fact] public void OperatorsTest() { - Operators.Test(formatter, new List + Operators.Test(Formatter, new List { "!=", "%", @@ -81,7 +81,7 @@ public void OperatorsTest() [Fact] public void JoinTest() { - Join.Test(formatter, null, new List + Join.Test(Formatter, null, new List { "ANTI JOIN", "SEMI JOIN", @@ -118,7 +118,7 @@ public void FormatsWindowSpecificationAsTopLevel() + " ORDER BY\n" + " time\n" + " );", - formatter.Format( + Formatter.Format( "SELECT *, LAG(value) OVER wnd AS next_value FROM tbl WINDOW wnd as (PARTITION BY id ORDER BY time);")); } @@ -131,7 +131,7 @@ public void FormatsWindowFunctionAndEndAsInline() + " window(time, \"1 hour\").end AS window_end\n" + "FROM\n" + " tbl;", - formatter.Format( + Formatter.Format( "SELECT window(time, \"1 hour\").start AS window_start, window(time, \"1 hour\").end AS window_end FROM tbl;")); } @@ -141,7 +141,7 @@ public void DoesNotAddSpacesAroundDollarParams() Assert.Equal( "SELECT\n" + " ${var_name};", - formatter.Format( + Formatter.Format( "SELECT ${var_name};")); } @@ -152,7 +152,7 @@ public void ReplacesDollarVariablesAndDollarCurlyBracketVariablesWithParamValues "SELECT\n" + " 'var one',\n" + " 'var two';", - formatter.Format( + Formatter.Format( "SELECT $var1, ${var2};", new Dictionary { { "var1", "'var one'" }, diff --git a/SQL.Formatter.Test/SqlFormatterTest.cs b/SQL.Formatter.Test/SqlFormatterTest.cs index 0474b06..23fa8f2 100644 --- a/SQL.Formatter.Test/SqlFormatterTest.cs +++ b/SQL.Formatter.Test/SqlFormatterTest.cs @@ -51,8 +51,10 @@ public void WithIndent() [Fact] public void WithNamedParams() { - var namedParams = new Dictionary(); - namedParams.Add("foo", "'bar'"); + var namedParams = new Dictionary + { + { "foo", "'bar'" } + }; var format = SqlFormatter.Of(Dialect.TSql).Format("SELECT * FROM tbl WHERE foo = @foo", namedParams); diff --git a/SQL.Formatter.Test/StandardSqlFormatterTest.cs b/SQL.Formatter.Test/StandardSqlFormatterTest.cs index 52ae066..0f0dc6a 100644 --- a/SQL.Formatter.Test/StandardSqlFormatterTest.cs +++ b/SQL.Formatter.Test/StandardSqlFormatterTest.cs @@ -8,36 +8,36 @@ namespace SQL.Formatter.Test { public class StandardSqlFormatterTest { - private readonly SqlFormatter.Formatter formatter = SqlFormatter.Standard(); + private readonly SqlFormatter.Formatter _formatter = SqlFormatter.Standard(); [Fact] public void BehavesLikeSqlFormatterTest() { - BehavesLikeSqlFormatter.Test(formatter); + BehavesLikeSqlFormatter.Test(_formatter); } [Fact] public void CaseTest() { - Case.Test(formatter); + Case.Test(_formatter); } [Fact] public void CreateTableTest() { - CreateTable.Test(formatter); + CreateTable.Test(_formatter); } [Fact] public void AlterTableTest() { - AlterTable.Test(formatter); + AlterTable.Test(_formatter); } [Fact] public void StringsTest() { - Strings.Test(formatter, new List + Strings.Test(_formatter, new List { StringLiteral.DoubleQuote, StringLiteral.SingleQuote @@ -47,19 +47,19 @@ public void StringsTest() [Fact] public void BetweenTest() { - Between.Test(formatter); + Between.Test(_formatter); } [Fact] public void SchemaTest() { - Schema.Test(formatter); + Schema.Test(_formatter); } [Fact] public void JoinTest() { - Join.Test(formatter); + Join.Test(_formatter); } [Fact] @@ -70,7 +70,7 @@ public void ReplacesIndexedPlaceholdersWithParamValues() + " first,\n" + " second,\n" + " third;", - formatter.Format( + _formatter.Format( "SELECT ?, ?, ?;", new List { "first", "second", "third" })); } @@ -82,7 +82,7 @@ public void FormatsFatchFirstLikeLimit() + " *\n" + "FETCH FIRST\n" + " 2 ROWS ONLY;", - formatter.Format( + _formatter.Format( "SELECT * FETCH FIRST 2 ROWS ONLY;")); } @@ -98,7 +98,7 @@ public void SqlAndCommentsInSingleLine() + "WHERE\n" + " date BETWEEN '2001-01-01' AND '2010-12-31';\n" + "-- comment", - formatter.Format( + _formatter.Format( "SELECT * FROM table/* comment */ WHERE date BETWEEN '2001-01-01' AND '2010-12-31'; -- comment")); } @@ -144,7 +144,7 @@ public void FormatsMerge() + " source.m_usageType,\n" + " source.m_category\n" + " )", - formatter.Format( + _formatter.Format( "merge into DW_STG_USER.ACCOUNT_DIM target using ( select COMMON_NAME m_commonName, ORIGIN m_origin, USAGE_TYPE m_usageType, CATEGORY m_category from MY_TABLE where USAGE_TYPE = :value ) source on source.m_usageType = target.USAGE_TYPE when matched then update set target.COMMON_NAME = source.m_commonName, target.ORIGIN = source.m_origin, target.USAGE_TYPE = source.m_usageType, target.CATEGORY = source.m_category where ((source.m_commonName <> target.COMMON_NAME)or(source.m_origin <> target.ORIGIN)or(source.m_usageType <> target.USAGE_TYPE)or(source.m_category <> target.CATEGORY)) when not matched then insert ( target.COMMON_NAME, target.ORIGIN, target.USAGE_TYPE, target.CATEGORY) values (source.m_commonName, source.m_origin, source.m_usageType, source.m_category)")); } } diff --git a/SQL.Formatter.Test/TSqlFormatterTest.cs b/SQL.Formatter.Test/TSqlFormatterTest.cs index b72f746..d15515a 100644 --- a/SQL.Formatter.Test/TSqlFormatterTest.cs +++ b/SQL.Formatter.Test/TSqlFormatterTest.cs @@ -8,36 +8,36 @@ namespace SQL.Formatter.Test { public class TSqlFormatterTest { - public readonly SqlFormatter.Formatter formatter = SqlFormatter.Of(Dialect.TSql); + public readonly SqlFormatter.Formatter Formatter = SqlFormatter.Of(Dialect.TSql); [Fact] public void BehavesLikeSqlFormatterTest() { - BehavesLikeSqlFormatter.Test(formatter); + BehavesLikeSqlFormatter.Test(Formatter); } [Fact] public void CaseTest() { - Case.Test(formatter); + Case.Test(Formatter); } [Fact] public void CreateTableTest() { - CreateTable.Test(formatter); + CreateTable.Test(Formatter); } [Fact] public void AlterTableTest() { - AlterTable.Test(formatter); + AlterTable.Test(Formatter); } [Fact] public void StringsTest() { - Strings.Test(formatter, new List + Strings.Test(Formatter, new List { StringLiteral.DoubleQuote, StringLiteral.SingleQuote, @@ -49,19 +49,19 @@ public void StringsTest() [Fact] public void BetweenTest() { - Between.Test(formatter); + Between.Test(Formatter); } [Fact] public void SchemaTest() { - Schema.Test(formatter); + Schema.Test(Formatter); } [Fact] public void OperatorsTest() { - Operators.Test(formatter, new List + Operators.Test(Formatter, new List { "%", "&", @@ -86,7 +86,7 @@ public void OperatorsTest() [Fact] public void JoinTest() { - Join.Test(formatter, new List + Join.Test(Formatter, new List { "NATURAL" }); @@ -100,7 +100,7 @@ public void FormatsInsertWithoutInto() + " Customers (ID, MoneyBalance, Address, City)\n" + "VALUES\n" + " (12, -123.4, 'Skagen 2111', 'Stv');", - formatter.Format( + Formatter.Format( "INSERT Customers (ID, MoneyBalance, Address, City) VALUES (12,-123.4, 'Skagen 2111','Stv');")); } @@ -112,7 +112,7 @@ public void RecognizesAtVariables() + " @variable,\n" + " @\"var name\",\n" + " @[var name];", - formatter.Format( + Formatter.Format( "SELECT @variable, @\"var name\", @[var name];")); } @@ -126,7 +126,7 @@ public void ReplacesAtVariablesWithParamValues() + "FROM\n" + " t\n" + " CROSS JOIN t2 on t.id = t2.id_t", - formatter.Format( + Formatter.Format( "SELECT a, b FROM t CROSS JOIN t2 on t.id = t2.id_t")); } @@ -138,7 +138,7 @@ public void FormatsSelectQueryWithCrossJoin() + " 'var value',\n" + " 'var value1',\n" + " 'var value2';", - formatter.Format( + Formatter.Format( "SELECT @variable, @\"var name1\", @[var name2];", new Dictionary { { "variable", "'var value'"}, diff --git a/SQL.Formatter/Core/AbstractFormatter.cs b/SQL.Formatter/Core/AbstractFormatter.cs index 5f069ef..2daaf5c 100644 --- a/SQL.Formatter/Core/AbstractFormatter.cs +++ b/SQL.Formatter/Core/AbstractFormatter.cs @@ -8,22 +8,22 @@ namespace SQL.Formatter.Core { public class AbstractFormatter : IDialectConfigurator { - private readonly FormatConfig cfg; - private readonly Indentation indentation; - private readonly InlineBlock inlineBlock; - private readonly Params parameters; - protected Token previousReservedToken; - private JSLikeList tokens; - private int index; + private readonly FormatConfig _cfg; + private readonly Indentation _indentation; + private readonly InlineBlock _inlineBlock; + private readonly Params _parameters; + protected Token _previousReservedToken; + private JSLikeList _tokens; + private int _index; public AbstractFormatter(FormatConfig cfg) { - this.cfg = cfg; - indentation = new Indentation(cfg.indent); - inlineBlock = new InlineBlock(cfg.maxColumnLength); - parameters = cfg.parameters; - previousReservedToken = null; - index = 0; + _cfg = cfg; + _indentation = new Indentation(cfg.Indent); + _inlineBlock = new InlineBlock(cfg.MaxColumnLength); + _parameters = cfg.Parameters; + _previousReservedToken = null; + _index = 0; } public Tokenizer Tokenizer() @@ -38,7 +38,7 @@ protected virtual Token TokenOverride(Token token) public string Format(string query) { - tokens = Tokenizer().Tokenize(query); + _tokens = Tokenizer().Tokenize(query); var formattedQuery = GetFormattedQueryFromTokens(); return formattedQuery.Trim(); @@ -46,68 +46,68 @@ public string Format(string query) private string GetFormattedQueryFromTokens() { - var formattedQuery = ""; + var formattedQuery = string.Empty; - var _index = -1; - foreach (Token t in tokens) + var index = -1; + foreach (Token t in _tokens) { - index = ++_index; + _index = ++index; var token = TokenOverride(t); - if (token.type == TokenTypes.LINE_COMMENT) + if (token.Type == TokenTypes.LINE_COMMENT) { formattedQuery = FormatLineComment(token, formattedQuery); } - else if (token.type == TokenTypes.BLOCK_COMMENT) + else if (token.Type == TokenTypes.BLOCK_COMMENT) { formattedQuery = FormatBlockComment(token, formattedQuery); } - else if (token.type == TokenTypes.RESERVED_TOP_LEVEL) + else if (token.Type == TokenTypes.RESERVED_TOP_LEVEL) { formattedQuery = FormatToplevelReservedWord(token, formattedQuery); - previousReservedToken = token; + _previousReservedToken = token; } - else if (token.type == TokenTypes.RESERVED_TOP_LEVEL_NO_INDENT) + else if (token.Type == TokenTypes.RESERVED_TOP_LEVEL_NO_INDENT) { formattedQuery = FormatTopLevelReservedWordNoIndent(token, formattedQuery); - previousReservedToken = token; + _previousReservedToken = token; } - else if (token.type == TokenTypes.RESERVED_NEWLINE) + else if (token.Type == TokenTypes.RESERVED_NEWLINE) { formattedQuery = FormatNewlineReservedWord(token, formattedQuery); - previousReservedToken = token; + _previousReservedToken = token; } - else if (token.type == TokenTypes.RESERVED) + else if (token.Type == TokenTypes.RESERVED) { formattedQuery = FormatWithSpaces(token, formattedQuery); - previousReservedToken = token; + _previousReservedToken = token; } - else if (token.type == TokenTypes.OPEN_PAREN) + else if (token.Type == TokenTypes.OPEN_PAREN) { formattedQuery = FormatOpeningParentheses(token, formattedQuery); } - else if (token.type == TokenTypes.CLOSE_PAREN) + else if (token.Type == TokenTypes.CLOSE_PAREN) { formattedQuery = FormatClosingParentheses(token, formattedQuery); } - else if (token.type == TokenTypes.PLACEHOLDER) + else if (token.Type == TokenTypes.PLACEHOLDER) { formattedQuery = FormatPlaceholder(token, formattedQuery); } - else if (token.value.Equals(",")) + else if (token.Value.Equals(",")) { formattedQuery = FormatComma(token, formattedQuery); } - else if (token.value.Equals(":")) + else if (token.Value.Equals(":")) { formattedQuery = FormatWithSpaceAfter(token, formattedQuery); } - else if (token.value.Equals(".")) + else if (token.Value.Equals(".")) { formattedQuery = FormatWithoutSpaces(token, formattedQuery); } - else if (token.value.Equals(";")) + else if (token.Value.Equals(";")) { formattedQuery = FormatQuerySeparator(token, formattedQuery); } @@ -127,28 +127,28 @@ private string FormatLineComment(Token token, string query) private string FormatBlockComment(Token token, string query) { - return AddNewline(AddNewline(query) + IndentComment(token.value)); + return AddNewline(AddNewline(query) + IndentComment(token.Value)); } private string IndentComment(string comment) { - return comment.Replace("\n", "\n" + indentation.GetIndent()); + return comment.Replace("\n", "\n" + _indentation.GetIndent()); } private string FormatTopLevelReservedWordNoIndent(Token token, string query) { - indentation.DecreaseTopLevel(); + _indentation.DecreaseTopLevel(); query = AddNewline(query) + EqualizeWhitespace(Show(token)); return AddNewline(query); } private string FormatToplevelReservedWord(Token token, string query) { - indentation.DecreaseTopLevel(); + _indentation.DecreaseTopLevel(); query = AddNewline(query); - indentation.IncreaseTopLevel(); + _indentation.IncreaseTopLevel(); query += EqualizeWhitespace(Show(token)); return AddNewline(query); @@ -169,7 +169,7 @@ private static string EqualizeWhitespace(string str) return Regex.Replace(str, @"\s+", " "); } - private static readonly HashSet PreserveWhitespaceFor = + private static readonly HashSet s_preserveWhitespaceFor = new HashSet { TokenTypes.OPEN_PAREN, TokenTypes.LINE_COMMENT, @@ -178,20 +178,20 @@ private static string EqualizeWhitespace(string str) private string FormatOpeningParentheses(Token token, string query) { - if (string.IsNullOrEmpty(token.whitespaceBefore) - && (TokenLookBehind() == default || !PreserveWhitespaceFor.Contains(TokenLookBehind().type))) + if (string.IsNullOrEmpty(token.WhitespaceBefore) + && (TokenLookBehind() == default || !s_preserveWhitespaceFor.Contains(TokenLookBehind().Type))) { query = query.TrimEnd(); } query += Show(token); - inlineBlock.BeginIfPossible(tokens, index); + _inlineBlock.BeginIfPossible(_tokens, _index); - if (!inlineBlock.IsActive()) + if (!_inlineBlock.IsActive()) { - indentation.IncreaseBlockLevel(); - if (!cfg.skipWhitespaceNearBlockParentheses) + _indentation.IncreaseBlockLevel(); + if (!_cfg.SkipWhitespaceNearBlockParentheses) { query = AddNewline(query); } @@ -202,16 +202,16 @@ private string FormatOpeningParentheses(Token token, string query) private string FormatClosingParentheses(Token token, string query) { - if (inlineBlock.IsActive()) + if (_inlineBlock.IsActive()) { - inlineBlock.End(); + _inlineBlock.End(); return FormatWithSpaceAfter(token, query); } else { - indentation.DecreaseBlockLevel(); + _indentation.DecreaseBlockLevel(); - if (!cfg.skipWhitespaceNearBlockParentheses) + if (!_cfg.SkipWhitespaceNearBlockParentheses) { return FormatWithSpaces(token, AddNewline(query)); } @@ -222,13 +222,13 @@ private string FormatClosingParentheses(Token token, string query) private string FormatPlaceholder(Token token, string query) { - return query + parameters.Get(token) + " "; + return query + _parameters.Get(token) + " "; } private string FormatComma(Token token, string query) { query = query.TrimEnd() + Show(token) + " "; - return inlineBlock.IsActive() || Token.IsLimit(previousReservedToken) ? query : AddNewline(query); + return _inlineBlock.IsActive() || Token.IsLimit(_previousReservedToken) ? query : AddNewline(query); } private string FormatWithSpaceAfter(Token token, string query) @@ -248,26 +248,26 @@ private string FormatWithSpaces(Token token, string query) private string FormatQuerySeparator(Token token, string query) { - indentation.ResetIndentation(); + _indentation.ResetIndentation(); return query.TrimEnd() + Show(token) - + Utils.Repeat("\n", cfg.linesBetweenQueries == default ? 1 : cfg.linesBetweenQueries); + + Utils.Repeat("\n", _cfg.LinesBetweenQueries == default ? 1 : _cfg.LinesBetweenQueries); } private string Show(Token token) { - if (cfg.uppercase - && (token.type == TokenTypes.RESERVED - || token.type == TokenTypes.RESERVED_TOP_LEVEL - || token.type == TokenTypes.RESERVED_TOP_LEVEL_NO_INDENT - || token.type == TokenTypes.RESERVED_NEWLINE - || token.type == TokenTypes.OPEN_PAREN - || token.type == TokenTypes.CLOSE_PAREN)) + if (_cfg.Uppercase + && (token.Type == TokenTypes.RESERVED + || token.Type == TokenTypes.RESERVED_TOP_LEVEL + || token.Type == TokenTypes.RESERVED_TOP_LEVEL_NO_INDENT + || token.Type == TokenTypes.RESERVED_NEWLINE + || token.Type == TokenTypes.OPEN_PAREN + || token.Type == TokenTypes.CLOSE_PAREN)) { - return token.value.ToUpper(); + return token.Value.ToUpper(); } - return token.value; + return token.Value; } private string AddNewline(string query) @@ -278,7 +278,7 @@ private string AddNewline(string query) query += "\n"; } - return query + indentation.GetIndent(); + return query + _indentation.GetIndent(); } protected Token TokenLookBehind() @@ -288,7 +288,7 @@ protected Token TokenLookBehind() protected Token TokenLookBehind(int n) { - return tokens.Get(index - n); + return _tokens.Get(_index - n); } protected Token TokenLookAhead() @@ -298,15 +298,15 @@ protected Token TokenLookAhead() protected Token TokenLookAhead(int n) { - return tokens.Get(index + n); + return _tokens.Get(_index + n); } public virtual DialectConfig DoDialectConfig() { - return doDialectConfigFunc.Invoke(); + return _doDialectConfigFunc.Invoke(); } - public Func doDialectConfigFunc; + public Func _doDialectConfigFunc; } } diff --git a/SQL.Formatter/Core/DialectConfig.cs b/SQL.Formatter/Core/DialectConfig.cs index 610c22c..81c87ff 100644 --- a/SQL.Formatter/Core/DialectConfig.cs +++ b/SQL.Formatter/Core/DialectConfig.cs @@ -6,18 +6,18 @@ namespace SQL.Formatter.Core { public class DialectConfig { - public readonly List lineCommentTypes; - public readonly List reservedTopLevelWords; - public readonly List reservedTopLevelWordsNoIndent; - public readonly List reservedNewlineWords; - public readonly List reservedWords; - public readonly List specialWordChars; - public readonly List stringTypes; - public readonly List openParens; - public readonly List closeParens; - public readonly List indexedPlaceholderTypes; - public readonly List namedPlaceholderTypes; - public readonly List operators; + public readonly List LineCommentTypes; + public readonly List ReservedTopLevelWords; + public readonly List ReservedTopLevelWordsNoIndent; + public readonly List ReservedNewlineWords; + public readonly List ReservedWords; + public readonly List SpecialWordChars; + public readonly List StringTypes; + public readonly List OpenParens; + public readonly List CloseParens; + public readonly List IndexedPlaceholderTypes; + public readonly List NamedPlaceholderTypes; + public readonly List Operators; private DialectConfig( List lineCommentTypes, @@ -33,18 +33,18 @@ private DialectConfig( List namedPlaceholderTypes, List operators) { - this.lineCommentTypes = Utils.NullToEmpty(lineCommentTypes); - this.reservedTopLevelWords = Utils.NullToEmpty(reservedTopLevelWords); - this.reservedTopLevelWordsNoIndent = Utils.NullToEmpty(reservedTopLevelWordsNoIndent); - this.reservedNewlineWords = Utils.NullToEmpty(reservedNewlineWords); - this.reservedWords = Utils.NullToEmpty(reservedWords); - this.specialWordChars = Utils.NullToEmpty(specialWordChars); - this.stringTypes = Utils.NullToEmpty(stringTypes); - this.openParens = Utils.NullToEmpty(openParens); - this.closeParens = Utils.NullToEmpty(closeParens); - this.indexedPlaceholderTypes = Utils.NullToEmpty(indexedPlaceholderTypes); - this.namedPlaceholderTypes = Utils.NullToEmpty(namedPlaceholderTypes); - this.operators = Utils.NullToEmpty(operators); + LineCommentTypes = Utils.NullToEmpty(lineCommentTypes); + ReservedTopLevelWords = Utils.NullToEmpty(reservedTopLevelWords); + ReservedTopLevelWordsNoIndent = Utils.NullToEmpty(reservedTopLevelWordsNoIndent); + ReservedNewlineWords = Utils.NullToEmpty(reservedNewlineWords); + ReservedWords = Utils.NullToEmpty(reservedWords); + SpecialWordChars = Utils.NullToEmpty(specialWordChars); + StringTypes = Utils.NullToEmpty(stringTypes); + OpenParens = Utils.NullToEmpty(openParens); + CloseParens = Utils.NullToEmpty(closeParens); + IndexedPlaceholderTypes = Utils.NullToEmpty(indexedPlaceholderTypes); + NamedPlaceholderTypes = Utils.NullToEmpty(namedPlaceholderTypes); + Operators = Utils.NullToEmpty(operators); } public DialectConfig WithLineCommentTypes(List lineCommentTypes) { @@ -61,7 +61,7 @@ public DialectConfig PlusLineCommentTypes(params string[] lineCommentTypes) public DialectConfig PlusLineCommentTypes(List lineCommentTypes) { return ToBuilder() - .LineCommentTypes(Utils.Concat(this.lineCommentTypes, lineCommentTypes)) + .LineCommentTypes(Utils.Concat(LineCommentTypes, lineCommentTypes)) .Build(); } @@ -80,7 +80,7 @@ public DialectConfig PlusReservedTopLevelWords(params string[] reservedTopLevelW public DialectConfig PlusReservedTopLevelWords(List reservedTopLevelWords) { return ToBuilder() - .ReservedTopLevelWords(Utils.Concat(this.reservedTopLevelWords, reservedTopLevelWords)) + .ReservedTopLevelWords(Utils.Concat(ReservedTopLevelWords, reservedTopLevelWords)) .Build(); } @@ -99,7 +99,7 @@ public DialectConfig PlusReservedNewlineWords(params string[] reservedNewLineWor public DialectConfig PlusReservedNewlineWords(List reservedNewlineWords) { return ToBuilder() - .ReservedNewlineWords(Utils.Concat(this.reservedNewlineWords, reservedNewlineWords)) + .ReservedNewlineWords(Utils.Concat(ReservedNewlineWords, reservedNewlineWords)) .Build(); } @@ -118,7 +118,7 @@ public DialectConfig PlusReservedTopLevelWordsNoIndent(params string[] reservedT public DialectConfig PlusReservedTopLevelWordsNoIndent(List reservedTopLevelWordsNoIndent) { return ToBuilder() - .ReservedTopLevelWordsNoIndent(Utils.Concat(this.reservedTopLevelWordsNoIndent, reservedTopLevelWordsNoIndent)) + .ReservedTopLevelWordsNoIndent(Utils.Concat(ReservedTopLevelWordsNoIndent, reservedTopLevelWordsNoIndent)) .Build(); } @@ -137,7 +137,7 @@ public DialectConfig PlusReservedWords(params string[] reservedWords) public DialectConfig PlusReservedWords(List reservedWords) { return ToBuilder() - .ReservedWords(Utils.Concat(this.reservedWords, reservedWords)) + .ReservedWords(Utils.Concat(ReservedWords, reservedWords)) .Build(); } @@ -156,7 +156,7 @@ public DialectConfig PlusSpecialWordChars(params string[] specialWordChars) public DialectConfig PlusSpecialWordChars(List specialWordChars) { return ToBuilder() - .SpecialWordChars(Utils.Concat(this.specialWordChars, specialWordChars)) + .SpecialWordChars(Utils.Concat(SpecialWordChars, specialWordChars)) .Build(); } @@ -175,7 +175,7 @@ public DialectConfig PlusStringTypes(params string[] stringTypes) public DialectConfig PlusStringTypes(List stringTypes) { return ToBuilder() - .StringTypes(Utils.Concat(this.stringTypes, stringTypes)) + .StringTypes(Utils.Concat(StringTypes, stringTypes)) .Build(); } @@ -194,7 +194,7 @@ public DialectConfig PlusOpenParens(params string[] openParens) public DialectConfig PlusOpenParens(List openParens) { return ToBuilder() - .OpenParens(Utils.Concat(this.openParens, openParens)) + .OpenParens(Utils.Concat(OpenParens, openParens)) .Build(); } @@ -213,7 +213,7 @@ public DialectConfig PlusCloseParens(params string[] closeParens) public DialectConfig PlusCloseParens(List closeParens) { return ToBuilder() - .CloseParens(Utils.Concat(this.closeParens, closeParens)) + .CloseParens(Utils.Concat(CloseParens, closeParens)) .Build(); } @@ -232,7 +232,7 @@ public DialectConfig PlusIndexedPlaceholderTypes(params string[] indexedPlacehol public DialectConfig PlusIndexedPlaceholderTypes(List indexedPlaceholderTypes) { return ToBuilder() - .IndexedPlaceholderTypes(Utils.Concat(this.indexedPlaceholderTypes, indexedPlaceholderTypes)) + .IndexedPlaceholderTypes(Utils.Concat(IndexedPlaceholderTypes, indexedPlaceholderTypes)) .Build(); } @@ -251,7 +251,7 @@ public DialectConfig PlusNamedPlaceholderTypes(params string[] namedPlaceholderT public DialectConfig PlusNamedPlaceholderTypes(List namedPlaceholderTypes) { return ToBuilder() - .NamedPlaceholderTypes(Utils.Concat(this.namedPlaceholderTypes, namedPlaceholderTypes)) + .NamedPlaceholderTypes(Utils.Concat(NamedPlaceholderTypes, namedPlaceholderTypes)) .Build(); } @@ -270,25 +270,25 @@ public DialectConfig PlusOperators(params string[] operators) public DialectConfig PlusOperators(List operators) { return ToBuilder() - .Operators(Utils.Concat(this.operators, operators)) + .Operators(Utils.Concat(Operators, operators)) .Build(); } public DialectConfigBuilder ToBuilder() { return Builder() - .LineCommentTypes(lineCommentTypes) - .ReservedTopLevelWords(reservedTopLevelWords) - .ReservedTopLevelWordsNoIndent(reservedTopLevelWordsNoIndent) - .ReservedNewlineWords(reservedNewlineWords) - .ReservedWords(reservedWords) - .SpecialWordChars(specialWordChars) - .StringTypes(stringTypes) - .OpenParens(openParens) - .CloseParens(closeParens) - .IndexedPlaceholderTypes(indexedPlaceholderTypes) - .NamedPlaceholderTypes(namedPlaceholderTypes) - .Operators(operators); + .LineCommentTypes(LineCommentTypes) + .ReservedTopLevelWords(ReservedTopLevelWords) + .ReservedTopLevelWordsNoIndent(ReservedTopLevelWordsNoIndent) + .ReservedNewlineWords(ReservedNewlineWords) + .ReservedWords(ReservedWords) + .SpecialWordChars(SpecialWordChars) + .StringTypes(StringTypes) + .OpenParens(OpenParens) + .CloseParens(CloseParens) + .IndexedPlaceholderTypes(IndexedPlaceholderTypes) + .NamedPlaceholderTypes(NamedPlaceholderTypes) + .Operators(Operators); } public static DialectConfigBuilder Builder() diff --git a/SQL.Formatter/Core/FormatConfig.cs b/SQL.Formatter/Core/FormatConfig.cs index ceeff93..f95c357 100644 --- a/SQL.Formatter/Core/FormatConfig.cs +++ b/SQL.Formatter/Core/FormatConfig.cs @@ -7,12 +7,12 @@ public class FormatConfig public static readonly string DefaultIndent = " "; public static readonly int DefaultColumnMaxLength = 50; - public readonly string indent; - public readonly int maxColumnLength; - public readonly Params parameters; - public readonly bool uppercase; - public readonly int linesBetweenQueries; - public readonly bool skipWhitespaceNearBlockParentheses; + public readonly string Indent; + public readonly int MaxColumnLength; + public readonly Params Parameters; + public readonly bool Uppercase; + public readonly int LinesBetweenQueries; + public readonly bool SkipWhitespaceNearBlockParentheses; public FormatConfig( string indent, @@ -22,12 +22,12 @@ public FormatConfig( int linesBetweenQueries, bool skipWhitespaceNearBlockParentheses) { - this.indent = indent; - this.maxColumnLength = maxColumnLength; - this.parameters = parameters == null ? Params.Empty : parameters; - this.uppercase = uppercase; - this.linesBetweenQueries = linesBetweenQueries; - this.skipWhitespaceNearBlockParentheses = skipWhitespaceNearBlockParentheses; + Indent = indent; + MaxColumnLength = maxColumnLength; + Parameters = parameters == null ? Params.Empty : parameters; + Uppercase = uppercase; + LinesBetweenQueries = linesBetweenQueries; + SkipWhitespaceNearBlockParentheses = skipWhitespaceNearBlockParentheses; } public static FormatConfigBuilder Builder() @@ -37,12 +37,12 @@ public static FormatConfigBuilder Builder() public class FormatConfigBuilder { - private string indent = DefaultIndent; - private int maxColumnLength = DefaultColumnMaxLength; - private Params parameters; - private bool uppercase; - private int linesBetweenQueries; - private bool skipWhitespaceNearBlockParentheses; + private string _indent = DefaultIndent; + private int _maxColumnLength = DefaultColumnMaxLength; + private Params _parameters; + private bool _uppercase; + private int _linesBetweenQueries; + private bool _skipWhitespaceNearBlockParentheses; public FormatConfigBuilder() { @@ -50,19 +50,19 @@ public FormatConfigBuilder() public FormatConfigBuilder Indent(string indent) { - this.indent = indent; + _indent = indent; return this; } public FormatConfigBuilder MaxColumnLength(int maxColumnLength) { - this.maxColumnLength = maxColumnLength; + _maxColumnLength = maxColumnLength; return this; } public FormatConfigBuilder Params(Params parameters) { - this.parameters = parameters; + _parameters = parameters; return this; } @@ -78,31 +78,31 @@ public FormatConfigBuilder Params(List parameters) public FormatConfigBuilder Uppercase(bool uppercase) { - this.uppercase = uppercase; + _uppercase = uppercase; return this; } public FormatConfigBuilder LinesBetweenQueries(int linesBetweenQueries) { - this.linesBetweenQueries = linesBetweenQueries; + _linesBetweenQueries = linesBetweenQueries; return this; } public FormatConfigBuilder SkipWhitespaceNearBlockParentheses(bool skipWhitespaceNearBlockParentheses) { - this.skipWhitespaceNearBlockParentheses = skipWhitespaceNearBlockParentheses; + _skipWhitespaceNearBlockParentheses = skipWhitespaceNearBlockParentheses; return this; } public FormatConfig Build() { return new FormatConfig( - indent, - maxColumnLength, - parameters, - uppercase, - linesBetweenQueries, - skipWhitespaceNearBlockParentheses); + _indent, + _maxColumnLength, + _parameters, + _uppercase, + _linesBetweenQueries, + _skipWhitespaceNearBlockParentheses); } } } diff --git a/SQL.Formatter/Core/Indentation.cs b/SQL.Formatter/Core/Indentation.cs index b26ea8a..b86b4dd 100644 --- a/SQL.Formatter/Core/Indentation.cs +++ b/SQL.Formatter/Core/Indentation.cs @@ -11,43 +11,43 @@ private enum IndentTypes INDENT_TYPE_BLOCK_LEVEL } - private readonly string indent; - private readonly Stack indentTypes; + private readonly string _indent; + private readonly Stack _indentTypes; public Indentation(string indent) { - this.indent = indent; - indentTypes = new Stack(); + _indent = indent; + _indentTypes = new Stack(); } public string GetIndent() => - string.Concat(Enumerable.Range(0, indentTypes.Count) - .Select(_ => indent)); + string.Concat(Enumerable.Range(0, _indentTypes.Count) + .Select(_ => _indent)); public void IncreaseTopLevel() { - indentTypes.Push(IndentTypes.INDENT_TYPE_TOP_LEVEL); + _indentTypes.Push(IndentTypes.INDENT_TYPE_TOP_LEVEL); } public void IncreaseBlockLevel() { - indentTypes.Push(IndentTypes.INDENT_TYPE_BLOCK_LEVEL); + _indentTypes.Push(IndentTypes.INDENT_TYPE_BLOCK_LEVEL); } public void DecreaseTopLevel() { - if (indentTypes.Count != 0 && - indentTypes.Peek() == IndentTypes.INDENT_TYPE_TOP_LEVEL) + if (_indentTypes.Count != 0 && + _indentTypes.Peek() == IndentTypes.INDENT_TYPE_TOP_LEVEL) { - indentTypes.Pop(); + _indentTypes.Pop(); } } public void DecreaseBlockLevel() { - while (indentTypes.Count > 0) + while (_indentTypes.Count > 0) { - var type = indentTypes.Pop(); + var type = _indentTypes.Pop(); if (type != IndentTypes.INDENT_TYPE_TOP_LEVEL) { break; @@ -57,7 +57,7 @@ public void DecreaseBlockLevel() public void ResetIndentation() { - indentTypes.Clear(); + _indentTypes.Clear(); } } } diff --git a/SQL.Formatter/Core/InlineBlock.cs b/SQL.Formatter/Core/InlineBlock.cs index 29ea11e..68f6bfa 100644 --- a/SQL.Formatter/Core/InlineBlock.cs +++ b/SQL.Formatter/Core/InlineBlock.cs @@ -4,39 +4,39 @@ namespace SQL.Formatter.Core { public class InlineBlock { - private int level; - private readonly int maxColumnLength; + private int _level; + private readonly int _maxColumnLength; public InlineBlock(int maxColumnLength) { - this.maxColumnLength = maxColumnLength; - level = 0; + _maxColumnLength = maxColumnLength; + _level = 0; } public void BeginIfPossible(JSLikeList tokens, int index) { - if (level == 0 && IsInlineBlock(tokens, index)) + if (_level == 0 && IsInlineBlock(tokens, index)) { - level = 1; + _level = 1; } - else if (level > 0) + else if (_level > 0) { - level++; + _level++; } else { - level = 0; + _level = 0; } } public void End() { - level--; + _level--; } public bool IsActive() { - return level > 0; + return _level > 0; } private bool IsInlineBlock(JSLikeList tokens, int index) @@ -47,18 +47,18 @@ private bool IsInlineBlock(JSLikeList tokens, int index) for (var i = index; i < tokens.Size(); i++) { var token = tokens.Get(i); - length += token.value.Length; + length += token.Value.Length; - if (length > maxColumnLength) + if (length > _maxColumnLength) { return false; } - if (token.type == TokenTypes.OPEN_PAREN) + if (token.Type == TokenTypes.OPEN_PAREN) { level++; } - else if (token.type == TokenTypes.CLOSE_PAREN) + else if (token.Type == TokenTypes.CLOSE_PAREN) { level--; if (level == 0) @@ -78,10 +78,10 @@ private bool IsInlineBlock(JSLikeList tokens, int index) private static bool IsForbiddenToken(Token token) { - return token.type == TokenTypes.RESERVED_TOP_LEVEL - || token.type == TokenTypes.RESERVED_NEWLINE - || token.type == TokenTypes.BLOCK_COMMENT - || token.value.Equals(";"); + return token.Type == TokenTypes.RESERVED_TOP_LEVEL + || token.Type == TokenTypes.RESERVED_NEWLINE + || token.Type == TokenTypes.BLOCK_COMMENT + || token.Value.Equals(";"); } } } diff --git a/SQL.Formatter/Core/Params.cs b/SQL.Formatter/Core/Params.cs index 443018d..dbdb08c 100644 --- a/SQL.Formatter/Core/Params.cs +++ b/SQL.Formatter/Core/Params.cs @@ -26,12 +26,12 @@ public object Get(Token token) { if (IsEmpty()) { - return token.value; + return token.Value; } - if (!(token.key == null || string.IsNullOrEmpty(token.key))) + if (!(token.Key == null || string.IsNullOrEmpty(token.Key))) { - return GetByName(token.key); + return GetByName(token.Key); } return Get(); @@ -40,16 +40,16 @@ public object Get(Token token) internal class NamedParams : Params { - private readonly Dictionary parameters; + private readonly Dictionary _parameters; public NamedParams(Dictionary parameters) { - this.parameters = parameters; + _parameters = parameters; } public override bool IsEmpty() { - return parameters.Count == 0; + return _parameters.Count == 0; } public override object Get() @@ -59,32 +59,32 @@ public override object Get() public override object GetByName(string key) { - return parameters[key]; + return _parameters[key]; } public override string ToString() { - return parameters.ToString(); + return _parameters.ToString(); } } internal class IndexedParams : Params { - private readonly Queue parameters; + private readonly Queue _parameters; public IndexedParams(Queue parameters) { - this.parameters = parameters; + _parameters = parameters; } public override bool IsEmpty() { - return parameters.Count == 0; + return _parameters.Count == 0; } public override object Get() { - return parameters.Dequeue(); + return _parameters.Dequeue(); } public override object GetByName(string key) @@ -94,7 +94,7 @@ public override object GetByName(string key) public override string ToString() { - return parameters.ToString(); + return _parameters.ToString(); } } diff --git a/SQL.Formatter/Core/Token.cs b/SQL.Formatter/Core/Token.cs index d9fc543..0e6d137 100644 --- a/SQL.Formatter/Core/Token.cs +++ b/SQL.Formatter/Core/Token.cs @@ -5,19 +5,19 @@ namespace SQL.Formatter.Core { public class Token { - public readonly TokenTypes type; - public readonly string value; - public readonly string regex; - public readonly string whitespaceBefore; - public readonly string key; + public readonly TokenTypes Type; + public readonly string Value; + public readonly string Regex; + public readonly string WhitespaceBefore; + public readonly string Key; public Token(TokenTypes type, string value, string regex, string whitespaceBefore, string key) { - this.type = type; - this.value = value; - this.regex = regex; - this.whitespaceBefore = whitespaceBefore; - this.key = key; + Type = type; + Value = value; + Regex = regex; + WhitespaceBefore = whitespaceBefore; + Key = key; } public Token(TokenTypes type, string value, string regex, string whitespaceBefore) @@ -31,72 +31,72 @@ public Token(TokenTypes type, string value) public Token WithWhitespaceBefore(string whitespaceBefore) { - return new Token(type, value, regex, whitespaceBefore, key); + return new Token(Type, Value, Regex, whitespaceBefore, Key); } public Token WithKey(string key) { - return new Token(type, value, regex, whitespaceBefore, key); + return new Token(Type, Value, Regex, WhitespaceBefore, key); } public override string ToString() { - return "type: " + type + ", value: [" + value + "], regex: /" + regex + "/, key:" + key; + return "type: " + Type + ", value: [" + Value + "], regex: /" + Regex + "/, key:" + Key; } - private static readonly Regex And = + private static readonly Regex s_and = new Regex("^AND$", RegexOptions.IgnoreCase); - private static readonly Regex Between = + private static readonly Regex s_between = new Regex("^BETWEEN$", RegexOptions.IgnoreCase); - private static readonly Regex Limit = + private static readonly Regex s_limit = new Regex("^LIMIT$", RegexOptions.IgnoreCase); - private static readonly Regex Set = + private static readonly Regex s_set = new Regex("^SET$", RegexOptions.IgnoreCase); - private static readonly Regex By = + private static readonly Regex s_by = new Regex("^BY$", RegexOptions.IgnoreCase); - private static readonly Regex Window = + private static readonly Regex s_window = new Regex("^WINDOW$", RegexOptions.IgnoreCase); - private static readonly Regex End = + private static readonly Regex s_end = new Regex("^END$", RegexOptions.IgnoreCase); private static Func IsToken(TokenTypes type, Regex regex) { - return token => token?.type == type && regex.IsMatch(token.value); + return token => token?.Type == type && regex.IsMatch(token.Value); } public static bool IsAnd(Token token) { - return IsToken(TokenTypes.RESERVED_NEWLINE, And).Invoke(token); + return IsToken(TokenTypes.RESERVED_NEWLINE, s_and).Invoke(token); } public static bool IsBetween(Token token) { - return IsToken(TokenTypes.RESERVED, Between).Invoke(token); + return IsToken(TokenTypes.RESERVED, s_between).Invoke(token); } public static bool IsLimit(Token token) { - return IsToken(TokenTypes.RESERVED_TOP_LEVEL, Limit).Invoke(token); + return IsToken(TokenTypes.RESERVED_TOP_LEVEL, s_limit).Invoke(token); } public static bool IsSet(Token token) { - return IsToken(TokenTypes.RESERVED_TOP_LEVEL, Set).Invoke(token); + return IsToken(TokenTypes.RESERVED_TOP_LEVEL, s_set).Invoke(token); } public static bool IsBy(Token token) { - return IsToken(TokenTypes.RESERVED, By).Invoke(token); + return IsToken(TokenTypes.RESERVED, s_by).Invoke(token); } public static bool IsWindow(Token token) { - return IsToken(TokenTypes.RESERVED_TOP_LEVEL, Window).Invoke(token); + return IsToken(TokenTypes.RESERVED_TOP_LEVEL, s_window).Invoke(token); } public static bool IsEnd(Token token) { - return IsToken(TokenTypes.CLOSE_PAREN, End).Invoke(token); + return IsToken(TokenTypes.CLOSE_PAREN, s_end).Invoke(token); } } } diff --git a/SQL.Formatter/Core/Tokenizer.cs b/SQL.Formatter/Core/Tokenizer.cs index 06a109b..a76819b 100644 --- a/SQL.Formatter/Core/Tokenizer.cs +++ b/SQL.Formatter/Core/Tokenizer.cs @@ -8,72 +8,72 @@ namespace SQL.Formatter.Core { public class Tokenizer { - private readonly Regex NUMBER_PATTERN; - private readonly Regex OPERATOR_PATTERN; + private readonly Regex _numberPattern; + private readonly Regex _operatorPattern; - private readonly Regex BLOCK_COMMENT_PATTERN; - private readonly Regex LINE_COMMENT_PATTERN; + private readonly Regex _blockCommentPattern; + private readonly Regex _lineCommentPattern; - private readonly Regex RESERVED_TOP_LEVEL_PATTERN; - private readonly Regex RESERVED_TOP_LEVEL_NO_INDENT_PATTERN; - private readonly Regex RESERVED_NEWLINE_PATTERN; - private readonly Regex RESERVED_PLAIN_PATTERN; + private readonly Regex _reservedTopLevelPattern; + private readonly Regex _reservedTopLevelNoIndentPattern; + private readonly Regex _reservedNewLinePattern; + private readonly Regex _reservedPlainPattern; - private readonly Regex WORD_PATTERN; - private readonly Regex STRING_PATTERN; + private readonly Regex _wordPattern; + private readonly Regex _stringPattern; - private readonly Regex OPEN_PAREN_PATTERN; - private readonly Regex CLOSE_PAREN_PATTERN; + private readonly Regex _openParenPattern; + private readonly Regex _closeParenPattern; - private readonly Regex INDEXED_PLACEHOLDER_PATTERN; - private readonly Regex IDENT_NAMED_PLACEHOLDER_PATTERN; - private readonly Regex STRING_NAMED_PLACEHOLDER_PATTERN; + private readonly Regex _indexedPlaceholderPattern; + private readonly Regex _indentNamedPlaceholderPattern; + private readonly Regex _stringNamedPlaceholderPattern; public Tokenizer(DialectConfig cfg) { - NUMBER_PATTERN = new Regex( + _numberPattern = new Regex( "^((-\\s*)?[0-9]+(\\.[0-9]+)?([eE]-?[0-9]+(\\.[0-9]+)?)?|0x[0-9a-fA-F]+|0b[01]+)\\b"); - OPERATOR_PATTERN = new Regex( + _operatorPattern = new Regex( RegexUtil.CreateOperatorRegex( - new JSLikeList(new List { "<>", "<=", ">=" }).With(cfg.operators))); + new JSLikeList(new List { "<>", "<=", ">=" }).With(cfg.Operators))); - BLOCK_COMMENT_PATTERN = new Regex("^(/\\*(?s).*?(?:\\*/|$))"); - LINE_COMMENT_PATTERN = new Regex( - RegexUtil.CreateLineCommentRegex(new JSLikeList(cfg.lineCommentTypes))); + _blockCommentPattern = new Regex("^(/\\*(?s).*?(?:\\*/|$))"); + _lineCommentPattern = new Regex( + RegexUtil.CreateLineCommentRegex(new JSLikeList(cfg.LineCommentTypes))); - RESERVED_TOP_LEVEL_PATTERN = + _reservedTopLevelPattern = new Regex( - RegexUtil.CreateReservedWordRegex(new JSLikeList(cfg.reservedTopLevelWords))); - RESERVED_TOP_LEVEL_NO_INDENT_PATTERN = + RegexUtil.CreateReservedWordRegex(new JSLikeList(cfg.ReservedTopLevelWords))); + _reservedTopLevelNoIndentPattern = new Regex( - RegexUtil.CreateReservedWordRegex(new JSLikeList(cfg.reservedTopLevelWordsNoIndent))); - RESERVED_NEWLINE_PATTERN = + RegexUtil.CreateReservedWordRegex(new JSLikeList(cfg.ReservedTopLevelWordsNoIndent))); + _reservedNewLinePattern = new Regex( - RegexUtil.CreateReservedWordRegex(new JSLikeList(cfg.reservedNewlineWords))); - RESERVED_PLAIN_PATTERN = - new Regex(RegexUtil.CreateReservedWordRegex(new JSLikeList(cfg.reservedWords))); + RegexUtil.CreateReservedWordRegex(new JSLikeList(cfg.ReservedNewlineWords))); + _reservedPlainPattern = + new Regex(RegexUtil.CreateReservedWordRegex(new JSLikeList(cfg.ReservedWords))); - WORD_PATTERN = - new Regex(RegexUtil.CreateWordRegex(new JSLikeList(cfg.specialWordChars))); - STRING_PATTERN = - new Regex(RegexUtil.CreateStringRegex(new JSLikeList(cfg.stringTypes))); + _wordPattern = + new Regex(RegexUtil.CreateWordRegex(new JSLikeList(cfg.SpecialWordChars))); + _stringPattern = + new Regex(RegexUtil.CreateStringRegex(new JSLikeList(cfg.StringTypes))); - OPEN_PAREN_PATTERN = - new Regex(RegexUtil.CreateParenRegex(new JSLikeList(cfg.openParens))); - CLOSE_PAREN_PATTERN = - new Regex(RegexUtil.CreateParenRegex(new JSLikeList(cfg.closeParens))); + _openParenPattern = + new Regex(RegexUtil.CreateParenRegex(new JSLikeList(cfg.OpenParens))); + _closeParenPattern = + new Regex(RegexUtil.CreateParenRegex(new JSLikeList(cfg.CloseParens))); - INDEXED_PLACEHOLDER_PATTERN = + _indexedPlaceholderPattern = RegexUtil.CreatePlaceholderRegexPattern( - new JSLikeList(cfg.indexedPlaceholderTypes), "[0-9]*"); - IDENT_NAMED_PLACEHOLDER_PATTERN = + new JSLikeList(cfg.IndexedPlaceholderTypes), "[0-9]*"); + _indentNamedPlaceholderPattern = RegexUtil.CreatePlaceholderRegexPattern( - new JSLikeList(cfg.namedPlaceholderTypes), "[a-zA-Z0-9._$]+"); - STRING_NAMED_PLACEHOLDER_PATTERN = + new JSLikeList(cfg.NamedPlaceholderTypes), "[a-zA-Z0-9._$]+"); + _stringNamedPlaceholderPattern = RegexUtil.CreatePlaceholderRegexPattern( - new JSLikeList(cfg.namedPlaceholderTypes), - RegexUtil.CreateStringPattern(new JSLikeList(cfg.stringTypes))); + new JSLikeList(cfg.NamedPlaceholderTypes), + RegexUtil.CreateStringPattern(new JSLikeList(cfg.StringTypes))); } public JSLikeList Tokenize(string input) @@ -90,7 +90,7 @@ public JSLikeList Tokenize(string input) if (!string.IsNullOrEmpty(input)) { token = GetNextToken(input, token); - input = input.Substring(token.value.Length); + input = input[token.Value.Length..]; tokens.Add(token.WithWhitespaceBefore(whitespaceBefore)); } } @@ -101,7 +101,7 @@ public JSLikeList Tokenize(string input) private static string[] FindBeforeWhitespace(string input) { var index = input.TakeWhile(char.IsWhiteSpace).Count(); - return new[] { input.Substring(0, index), input.Substring(index) }; + return new[] { input[..index], input[index..] }; } private Token GetNextToken(string input, Token previousToken) @@ -127,27 +127,27 @@ private Token GetCommentToken(string input) private Token GetLineCommentToken(string input) { - return GetTokenOnFirstMatch(input, TokenTypes.LINE_COMMENT, LINE_COMMENT_PATTERN); + return GetTokenOnFirstMatch(input, TokenTypes.LINE_COMMENT, _lineCommentPattern); } private Token GetBlockCommentToken(string input) { - return GetTokenOnFirstMatch(input, TokenTypes.BLOCK_COMMENT, BLOCK_COMMENT_PATTERN); + return GetTokenOnFirstMatch(input, TokenTypes.BLOCK_COMMENT, _blockCommentPattern); } private Token GetStringToken(string input) { - return GetTokenOnFirstMatch(input, TokenTypes.STRING, STRING_PATTERN); + return GetTokenOnFirstMatch(input, TokenTypes.STRING, _stringPattern); } private Token GetOpenParenToken(string input) { - return GetTokenOnFirstMatch(input, TokenTypes.OPEN_PAREN, OPEN_PAREN_PATTERN); + return GetTokenOnFirstMatch(input, TokenTypes.OPEN_PAREN, _openParenPattern); } private Token GetCloseParenToken(string input) { - return GetTokenOnFirstMatch(input, TokenTypes.CLOSE_PAREN, CLOSE_PAREN_PATTERN); + return GetTokenOnFirstMatch(input, TokenTypes.CLOSE_PAREN, _closeParenPattern); } private Token GetPlaceholderToken(string input) @@ -161,28 +161,28 @@ private Token GetPlaceholderToken(string input) private Token GetIdentNamedPlaceholderToken(string input) { return GetPlaceholderTokenWithKey( - input, IDENT_NAMED_PLACEHOLDER_PATTERN, v => v.Substring(1)); + input, _indentNamedPlaceholderPattern, v => v[1..]); } private Token GetStringNamedPlaceholderToken(string input) { return GetPlaceholderTokenWithKey( input, - STRING_NAMED_PLACEHOLDER_PATTERN, + _stringNamedPlaceholderPattern, v => GetEscapedPlaceholderKey( - v.Substring(2, v.Length - 3), v.Substring(v.Length - 1))); + v[2..^1], v[^1..])); } private Token GetIndexedPlaceholderToken(string input) { return GetPlaceholderTokenWithKey( - input, INDEXED_PLACEHOLDER_PATTERN, v => v.Substring(1)); + input, _indexedPlaceholderPattern, v => v[1..]); } private static Token GetPlaceholderTokenWithKey(string input, Regex regex, Func parseKey) { var token = GetTokenOnFirstMatch(input, TokenTypes.PLACEHOLDER, regex); - return token?.WithKey(parseKey.Invoke(token.value)); + return token?.WithKey(parseKey.Invoke(token.Value)); } private static string GetEscapedPlaceholderKey(string key, string quoteChar) @@ -192,17 +192,17 @@ private static string GetEscapedPlaceholderKey(string key, string quoteChar) private Token GetNumberToken(string input) { - return GetTokenOnFirstMatch(input, TokenTypes.NUMBER, NUMBER_PATTERN); + return GetTokenOnFirstMatch(input, TokenTypes.NUMBER, _numberPattern); } private Token GetOperatorToken(string input) { - return GetTokenOnFirstMatch(input, TokenTypes.OPERATOR, OPERATOR_PATTERN); + return GetTokenOnFirstMatch(input, TokenTypes.OPERATOR, _operatorPattern); } private Token GetReservedWordToken(string input, Token previousToken) { - if (previousToken?.value != null && previousToken.value.Equals(".")) + if (previousToken?.Value != null && previousToken.Value.Equals(".")) { return null; } @@ -217,29 +217,29 @@ private Token GetReservedWordToken(string input, Token previousToken) private Token GetToplevelReservedToken(string input) { return GetTokenOnFirstMatch( - input, TokenTypes.RESERVED_TOP_LEVEL, RESERVED_TOP_LEVEL_PATTERN); + input, TokenTypes.RESERVED_TOP_LEVEL, _reservedTopLevelPattern); } private Token GetNewlineReservedToken(string input) { return GetTokenOnFirstMatch( - input, TokenTypes.RESERVED_NEWLINE, RESERVED_NEWLINE_PATTERN); + input, TokenTypes.RESERVED_NEWLINE, _reservedNewLinePattern); } private Token GetTopLevelReservedTokenNoIndent(string input) { return GetTokenOnFirstMatch( - input, TokenTypes.RESERVED_TOP_LEVEL_NO_INDENT, RESERVED_TOP_LEVEL_NO_INDENT_PATTERN); + input, TokenTypes.RESERVED_TOP_LEVEL_NO_INDENT, _reservedTopLevelNoIndentPattern); } private Token GetPlainReservedToken(string input) { - return GetTokenOnFirstMatch(input, TokenTypes.RESERVED, RESERVED_PLAIN_PATTERN); + return GetTokenOnFirstMatch(input, TokenTypes.RESERVED, _reservedPlainPattern); } private Token GetWordToken(string input) { - return GetTokenOnFirstMatch(input, TokenTypes.WORD, WORD_PATTERN); + return GetTokenOnFirstMatch(input, TokenTypes.WORD, _wordPattern); } private static string GetFirstMatch(string input, Regex regex) diff --git a/SQL.Formatter/Core/Util/JSLikeList.cs b/SQL.Formatter/Core/Util/JSLikeList.cs index 7d11ab7..f205387 100644 --- a/SQL.Formatter/Core/Util/JSLikeList.cs +++ b/SQL.Formatter/Core/Util/JSLikeList.cs @@ -7,35 +7,35 @@ namespace SQL.Formatter.Core.Util { public class JSLikeList : IEnumerable { - private readonly List tList; + private readonly List _tList; public JSLikeList(List tList) { - this.tList = tList ?? new List(); + _tList = tList ?? new List(); } public List ToList() { - return tList; + return _tList; } public JSLikeList Map(Func mapper) { return new JSLikeList( - tList + _tList .Select(mapper.Invoke) .ToList()); } public string Join(string delimiter) { - return string.Join(delimiter, tList); + return string.Join(delimiter, _tList); } public JSLikeList With(List other) { return new JSLikeList( - tList + _tList .Concat(other) .ToList()); } @@ -47,27 +47,27 @@ public string Join() public bool IsEmpty() { - return tList == null || tList.Count == 0; + return _tList == null || _tList.Count == 0; } public T Get(int index) { - if (index < 0 || tList.Count <= index) + if (index < 0 || _tList.Count <= index) { return default; } - return tList.ElementAt(index); + return _tList.ElementAt(index); } public IEnumerator GetEnumerator() { - return tList.GetEnumerator(); + return _tList.GetEnumerator(); } public int Size() { - return tList.Count; + return _tList.Count; } } } diff --git a/SQL.Formatter/Core/Util/RegexUtil.cs b/SQL.Formatter/Core/Util/RegexUtil.cs index c6a0688..a1d6e70 100644 --- a/SQL.Formatter/Core/Util/RegexUtil.cs +++ b/SQL.Formatter/Core/Util/RegexUtil.cs @@ -7,12 +7,12 @@ namespace SQL.Formatter.Core.Util { public class RegexUtil { - private static readonly string EscapeRegex = + private static readonly string s_escapeRegex = string.Join("|", new List { "^", "$", "\\", ".", "*", "+", "*", "?", "(", ")", "[", "]", "{", "}", "|" } .Select(spChr => "(\\" + spChr + ")")); - public static readonly Regex EscapeRegexPattern = new Regex(EscapeRegex); + public static readonly Regex EscapeRegexPattern = new Regex(s_escapeRegex); public static string EscapeRegExp(string s) { @@ -47,7 +47,7 @@ public static string CreateReservedWordRegex(JSLikeList reservedWords) public static string CreateWordRegex(JSLikeList specialChars) { return "^([\\p{L}\\p{Nd}\\p{Mn}\\p{Pc}" - + specialChars.Join("") + + specialChars.Join(string.Empty) + "]+)"; } diff --git a/SQL.Formatter/Language/Db2Formatter.cs b/SQL.Formatter/Language/Db2Formatter.cs index c785f3a..8ce62d9 100644 --- a/SQL.Formatter/Language/Db2Formatter.cs +++ b/SQL.Formatter/Language/Db2Formatter.cs @@ -5,7 +5,7 @@ namespace SQL.Formatter.Language { public class Db2Formatter : AbstractFormatter { - private static readonly List ReservedWords = new List{ + private static readonly List s_reservedWords = new List{ "ABS", "ACTIVATE", "ALIAS", @@ -509,7 +509,7 @@ public class Db2Formatter : AbstractFormatter "YEAR", "YEARS"}; - private static readonly List ReservedTopLevelWords = + private static readonly List s_reservedTopLevelWords = new List{ "ADD", "AFTER", @@ -534,10 +534,10 @@ public class Db2Formatter : AbstractFormatter "VALUES", "WHERE"}; - private static readonly List ReservedTopLevelWordsNoIndent = + private static readonly List s_reservedTopLevelWordsNoIndent = new List { "INTERSECT", "INTERSECT ALL", "MINUS", "UNION", "UNION ALL" }; - private static readonly List ReservedNewlineWords = + private static readonly List s_reservedNewlineWords = new List{ "AND", "OR", @@ -555,10 +555,10 @@ public class Db2Formatter : AbstractFormatter public override DialectConfig DoDialectConfig() { return DialectConfig.Builder() - .ReservedWords(ReservedWords) - .ReservedTopLevelWords(ReservedTopLevelWords) - .ReservedTopLevelWordsNoIndent(ReservedTopLevelWordsNoIndent) - .ReservedNewlineWords(ReservedNewlineWords) + .ReservedWords(s_reservedWords) + .ReservedTopLevelWords(s_reservedTopLevelWords) + .ReservedTopLevelWordsNoIndent(s_reservedTopLevelWordsNoIndent) + .ReservedNewlineWords(s_reservedNewlineWords) .StringTypes( new List{ StringLiteral.DoubleQuote, diff --git a/SQL.Formatter/Language/Dialect.cs b/SQL.Formatter/Language/Dialect.cs index 4f39e99..a98d1ed 100644 --- a/SQL.Formatter/Language/Dialect.cs +++ b/SQL.Formatter/Language/Dialect.cs @@ -35,28 +35,28 @@ public static IEnumerable Values } } - public readonly string name; - public readonly Func func; - public readonly List aliases; + public readonly string Name; + public readonly Func Func; + public readonly List Aliases; private Dialect(Func func, string name, params string[] aliases) { - this.func = func; - this.name = name; - this.aliases = new List(aliases); + Func = func; + Name = name; + Aliases = new List(aliases); } private bool Matches(string name) { - return this.name.ToLower().Equals(name.ToLower()) - || aliases.Select(s => s.ToLower()).Intersect(new string[] { name.ToLower() }).Any(); + return Name.ToLower().Equals(name.ToLower()) + || Aliases.Select(s => s.ToLower()).Intersect(new string[] { name.ToLower() }).Any(); } public static Dialect NameOf(string name) { var dialects = Values.Where(d => d.Matches(name)); - if (dialects.Count() == 0) + if (!dialects.Any()) { return null; } diff --git a/SQL.Formatter/Language/MariaDbFormatter.cs b/SQL.Formatter/Language/MariaDbFormatter.cs index de893d6..a687726 100644 --- a/SQL.Formatter/Language/MariaDbFormatter.cs +++ b/SQL.Formatter/Language/MariaDbFormatter.cs @@ -5,7 +5,7 @@ namespace SQL.Formatter.Language { public class MariaDbFormatter : AbstractFormatter { - private static readonly List ReservedWords = new List{ + private static readonly List s_reservedWords = new List{ "ACCESSIBLE", "ADD", "ALL", @@ -255,7 +255,7 @@ public class MariaDbFormatter : AbstractFormatter "YEAR_MONTH", "ZEROFILL"}; - private static readonly List ReservedTopLevelWords = + private static readonly List s_reservedTopLevelWords = new List{ "ADD", "ALTER COLUMN", @@ -275,10 +275,10 @@ public class MariaDbFormatter : AbstractFormatter "VALUES", "WHERE"}; - private static readonly List ReservedTopLevelWordsNoIndent = + private static readonly List s_reservedTopLevelWordsNoIndent = new List { "INTERSECT", "INTERSECT ALL", "UNION", "UNION ALL" }; - private static readonly List ReservedNewlineWords = + private static readonly List s_reservedNewlineWords = new List{ "AND", "ELSE", @@ -301,10 +301,10 @@ public class MariaDbFormatter : AbstractFormatter public override DialectConfig DoDialectConfig() { return DialectConfig.Builder() - .ReservedWords(ReservedWords) - .ReservedTopLevelWords(ReservedTopLevelWords) - .ReservedTopLevelWordsNoIndent(ReservedTopLevelWordsNoIndent) - .ReservedNewlineWords(ReservedNewlineWords) + .ReservedWords(s_reservedWords) + .ReservedTopLevelWords(s_reservedTopLevelWords) + .ReservedTopLevelWordsNoIndent(s_reservedTopLevelWordsNoIndent) + .ReservedNewlineWords(s_reservedNewlineWords) .StringTypes( new List{ StringLiteral.DoubleQuote, diff --git a/SQL.Formatter/Language/MySqlFormatter.cs b/SQL.Formatter/Language/MySqlFormatter.cs index a62c663..61c2abe 100644 --- a/SQL.Formatter/Language/MySqlFormatter.cs +++ b/SQL.Formatter/Language/MySqlFormatter.cs @@ -5,7 +5,7 @@ namespace SQL.Formatter.Language { public class MySqlFormatter : AbstractFormatter { - private static readonly List ReservedWords = new List{ + private static readonly List s_reservedWords = new List{ "ACCESSIBLE", "ADD", "ALL", @@ -268,7 +268,7 @@ public class MySqlFormatter : AbstractFormatter "YEAR_MONTH", "ZEROFILL"}; - private static readonly List ReservedTopLevelWords = + private static readonly List s_reservedTopLevelWords = new List{ "ADD", "ALTER COLUMN", @@ -288,10 +288,10 @@ public class MySqlFormatter : AbstractFormatter "VALUES", "WHERE"}; - private static readonly List ReservedTopLevelWordsNoIndent = + private static readonly List s_reservedTopLevelWordsNoIndent = new List { "INTERSECT", "INTERSECT ALL", "MINUS", "UNION", "UNION ALL" }; - private static readonly List ReservedNewlineWords = + private static readonly List s_reservedNewlineWords = new List{ "AND", "ELSE", @@ -314,10 +314,10 @@ public class MySqlFormatter : AbstractFormatter public override DialectConfig DoDialectConfig() { return DialectConfig.Builder() - .ReservedWords(ReservedWords) - .ReservedTopLevelWords(ReservedTopLevelWords) - .ReservedTopLevelWordsNoIndent(ReservedTopLevelWordsNoIndent) - .ReservedNewlineWords(ReservedNewlineWords) + .ReservedWords(s_reservedWords) + .ReservedTopLevelWords(s_reservedTopLevelWords) + .ReservedTopLevelWordsNoIndent(s_reservedTopLevelWordsNoIndent) + .ReservedNewlineWords(s_reservedNewlineWords) .StringTypes( new List{ StringLiteral.DoubleQuote, diff --git a/SQL.Formatter/Language/N1qlFormatter.cs b/SQL.Formatter/Language/N1qlFormatter.cs index 6abc0fd..9e17dd0 100644 --- a/SQL.Formatter/Language/N1qlFormatter.cs +++ b/SQL.Formatter/Language/N1qlFormatter.cs @@ -5,7 +5,7 @@ namespace SQL.Formatter.Language { public class N1qlFormatter : AbstractFormatter { - private static readonly List ReservedWords = new List{ + private static readonly List s_reservedWords = new List{ "ALL", "ALTER", "ANALYZE", @@ -177,7 +177,7 @@ public class N1qlFormatter : AbstractFormatter "WORK", "XOR"}; - private static readonly List ReservedTopLevelWords = + private static readonly List s_reservedTopLevelWords = new List{ "DELETE FROM", "EXCEPT ALL", @@ -207,10 +207,10 @@ public class N1qlFormatter : AbstractFormatter "VALUES", "WHERE"}; - private static readonly List ReservedTopLevelWordsNoIndent = + private static readonly List s_reservedTopLevelWordsNoIndent = new List { "INTERSECT", "INTERSECT ALL", "MINUS", "UNION", "UNION ALL" }; - private static readonly List ReservedNewlineWords = + private static readonly List s_reservedNewlineWords = new List{ "AND", "OR", @@ -225,10 +225,10 @@ public class N1qlFormatter : AbstractFormatter public override DialectConfig DoDialectConfig() { return DialectConfig.Builder() - .ReservedWords(ReservedWords) - .ReservedTopLevelWords(ReservedTopLevelWords) - .ReservedTopLevelWordsNoIndent(ReservedTopLevelWordsNoIndent) - .ReservedNewlineWords(ReservedNewlineWords) + .ReservedWords(s_reservedWords) + .ReservedTopLevelWords(s_reservedTopLevelWords) + .ReservedTopLevelWordsNoIndent(s_reservedTopLevelWordsNoIndent) + .ReservedNewlineWords(s_reservedNewlineWords) .StringTypes( new List{ StringLiteral.DoubleQuote, StringLiteral.SingleQuote, StringLiteral.BackQuote}) diff --git a/SQL.Formatter/Language/PlSqlFormatter.cs b/SQL.Formatter/Language/PlSqlFormatter.cs index bf21b6b..334e023 100644 --- a/SQL.Formatter/Language/PlSqlFormatter.cs +++ b/SQL.Formatter/Language/PlSqlFormatter.cs @@ -5,7 +5,7 @@ namespace SQL.Formatter.Language { public class PlSqlFormatter : AbstractFormatter { - private static readonly List ReservedWords = new List{ + private static readonly List s_reservedWords = new List{ "A", "ACCESSIBLE", "AGENT", @@ -359,7 +359,7 @@ public class PlSqlFormatter : AbstractFormatter "YEAR", "ZONE"}; - private static readonly List ReservedTopLevelWords = + private static readonly List s_reservedTopLevelWords = new List{ "ADD", "ALTER COLUMN", @@ -391,10 +391,10 @@ public class PlSqlFormatter : AbstractFormatter "VALUES", "WHERE"}; - private static readonly List ReservedTopLevelWordsNoIndent = + private static readonly List s_reservedTopLevelWordsNoIndent = new List { "INTERSECT", "INTERSECT ALL", "MINUS", "UNION", "UNION ALL" }; - private static readonly List ReservedNewlineWords = + private static readonly List s_reservedNewlineWords = new List{ "AND", "CROSS APPLY", @@ -418,10 +418,10 @@ public class PlSqlFormatter : AbstractFormatter public override DialectConfig DoDialectConfig() { return DialectConfig.Builder() - .ReservedWords(ReservedWords) - .ReservedTopLevelWords(ReservedTopLevelWords) - .ReservedTopLevelWordsNoIndent(ReservedTopLevelWordsNoIndent) - .ReservedNewlineWords(ReservedNewlineWords) + .ReservedWords(s_reservedWords) + .ReservedTopLevelWords(s_reservedTopLevelWords) + .ReservedTopLevelWordsNoIndent(s_reservedTopLevelWordsNoIndent) + .ReservedNewlineWords(s_reservedNewlineWords) .StringTypes( new List{ StringLiteral.DoubleQuote, @@ -441,9 +441,9 @@ public override DialectConfig DoDialectConfig() protected override Token TokenOverride(Token token) { - if (Token.IsSet(token) && Token.IsBy(previousReservedToken)) + if (Token.IsSet(token) && Token.IsBy(_previousReservedToken)) { - return new Token(TokenTypes.RESERVED, token.value); + return new Token(TokenTypes.RESERVED, token.Value); } return token; diff --git a/SQL.Formatter/Language/PostgreSqlFormatter.cs b/SQL.Formatter/Language/PostgreSqlFormatter.cs index fb8a670..e219cc3 100644 --- a/SQL.Formatter/Language/PostgreSqlFormatter.cs +++ b/SQL.Formatter/Language/PostgreSqlFormatter.cs @@ -5,7 +5,7 @@ namespace SQL.Formatter.Language { public class PostgreSqlFormatter : AbstractFormatter { - private static readonly List ReservedWords = new List{ + private static readonly List s_reservedWords = new List{ "ABORT", "ABSOLUTE", "ACCESS", @@ -457,7 +457,7 @@ public class PostgreSqlFormatter : AbstractFormatter "YES", "ZONE"}; - private static readonly List ReservedTopLevelWords = + private static readonly List s_reservedTopLevelWords = new List{ "ADD", "AFTER", @@ -483,10 +483,10 @@ public class PostgreSqlFormatter : AbstractFormatter "VALUES", "WHERE"}; - private static readonly List ReservedTopLevelWordsNoIndent = + private static readonly List s_reservedTopLevelWordsNoIndent = new List { "INTERSECT", "INTERSECT ALL", "UNION", "UNION ALL" }; - private static readonly List ReservedNewlineWords = + private static readonly List s_reservedNewlineWords = new List{ "AND", "ELSE", @@ -506,10 +506,10 @@ public class PostgreSqlFormatter : AbstractFormatter public override DialectConfig DoDialectConfig() { return DialectConfig.Builder() - .ReservedWords(ReservedWords) - .ReservedTopLevelWords(ReservedTopLevelWords) - .ReservedTopLevelWordsNoIndent(ReservedTopLevelWordsNoIndent) - .ReservedNewlineWords(ReservedNewlineWords) + .ReservedWords(s_reservedWords) + .ReservedTopLevelWords(s_reservedTopLevelWords) + .ReservedTopLevelWordsNoIndent(s_reservedTopLevelWordsNoIndent) + .ReservedNewlineWords(s_reservedNewlineWords) .StringTypes( new List{ StringLiteral.DoubleQuote, diff --git a/SQL.Formatter/Language/RedshiftFormatter.cs b/SQL.Formatter/Language/RedshiftFormatter.cs index 1deb8ff..16bfc75 100644 --- a/SQL.Formatter/Language/RedshiftFormatter.cs +++ b/SQL.Formatter/Language/RedshiftFormatter.cs @@ -5,7 +5,7 @@ namespace SQL.Formatter.Language { public class RedshiftFormatter : AbstractFormatter { - private static readonly List ReservedWords = new List{ + private static readonly List s_reservedWords = new List{ "AES128", "AES256", "ALLOWOVERWRITE", @@ -218,7 +218,7 @@ public class RedshiftFormatter : AbstractFormatter "EVEN", "ALL"}; - private static readonly List ReservedTopLevelWords = + private static readonly List s_reservedTopLevelWords = new List{ "ADD", "AFTER", @@ -338,9 +338,9 @@ public class RedshiftFormatter : AbstractFormatter "HIVE METASTORE", "CATALOG_ROLE"}; - private static readonly List ReservedTopLevelWordsNoIndent = new List(); + private static readonly List s_reservedTopLevelWordsNoIndent = new List(); - private static readonly List ReservedNewlineWords = + private static readonly List s_reservedNewlineWords = new List{ "AND", "ELSE", @@ -370,10 +370,10 @@ public class RedshiftFormatter : AbstractFormatter public override DialectConfig DoDialectConfig() { return DialectConfig.Builder() - .ReservedWords(ReservedWords) - .ReservedTopLevelWords(ReservedTopLevelWords) - .ReservedTopLevelWordsNoIndent(ReservedTopLevelWordsNoIndent) - .ReservedNewlineWords(ReservedNewlineWords) + .ReservedWords(s_reservedWords) + .ReservedTopLevelWords(s_reservedTopLevelWords) + .ReservedTopLevelWordsNoIndent(s_reservedTopLevelWordsNoIndent) + .ReservedNewlineWords(s_reservedNewlineWords) .StringTypes( new List{ StringLiteral.DoubleQuote, StringLiteral.SingleQuote, StringLiteral.BackQuote}) diff --git a/SQL.Formatter/Language/SparkSqlFormatter.cs b/SQL.Formatter/Language/SparkSqlFormatter.cs index f376178..4a9d476 100644 --- a/SQL.Formatter/Language/SparkSqlFormatter.cs +++ b/SQL.Formatter/Language/SparkSqlFormatter.cs @@ -5,7 +5,7 @@ namespace SQL.Formatter.Language { public class SparkSqlFormatter : AbstractFormatter { - private static readonly List ReservedWords = new List{ + private static readonly List s_reservedWords = new List{ "ALL", "ALTER", "ANALYSE", @@ -149,7 +149,7 @@ public class SparkSqlFormatter : AbstractFormatter "WITH", "YEAR_MONTH"}; - private static readonly List ReservedTopLevelWords = + private static readonly List s_reservedTopLevelWords = new List{ "ADD", "AFTER", @@ -184,10 +184,10 @@ public class SparkSqlFormatter : AbstractFormatter "WHERE", "WINDOW"}; - private static readonly List ReservedTopLevelWordsNoIndent = + private static readonly List s_reservedTopLevelWordsNoIndent = new List { "EXCEPT ALL", "EXCEPT", "INTERSECT ALL", "INTERSECT", "UNION ALL", "UNION" }; - private static readonly List ReservedNewlineWords = + private static readonly List s_reservedNewlineWords = new List{ "AND", "CREATE OR", @@ -228,10 +228,10 @@ public class SparkSqlFormatter : AbstractFormatter public override DialectConfig DoDialectConfig() { return DialectConfig.Builder() - .ReservedWords(ReservedWords) - .ReservedTopLevelWords(ReservedTopLevelWords) - .ReservedTopLevelWordsNoIndent(ReservedTopLevelWordsNoIndent) - .ReservedNewlineWords(ReservedNewlineWords) + .ReservedWords(s_reservedWords) + .ReservedTopLevelWords(s_reservedTopLevelWords) + .ReservedTopLevelWordsNoIndent(s_reservedTopLevelWordsNoIndent) + .ReservedNewlineWords(s_reservedNewlineWords) .StringTypes( new List{ StringLiteral.DoubleQuote, @@ -252,9 +252,9 @@ protected override Token TokenOverride(Token token) if (Token.IsWindow(token)) { var aheadToken = TokenLookAhead(); - if (aheadToken != null && aheadToken.type == TokenTypes.OPEN_PAREN) + if (aheadToken != null && aheadToken.Type == TokenTypes.OPEN_PAREN) { - return new Token(TokenTypes.RESERVED, token.value); + return new Token(TokenTypes.RESERVED, token.Value); } } @@ -262,10 +262,10 @@ protected override Token TokenOverride(Token token) { var backToken = TokenLookBehind(); if (backToken != null - && backToken.type == TokenTypes.OPERATOR - && backToken.value.Equals(".")) + && backToken.Type == TokenTypes.OPERATOR + && backToken.Value.Equals(".")) { - return new Token(TokenTypes.WORD, token.value); + return new Token(TokenTypes.WORD, token.Value); } } diff --git a/SQL.Formatter/Language/StandardSqlFormatter.cs b/SQL.Formatter/Language/StandardSqlFormatter.cs index 6b24398..cb54b7e 100644 --- a/SQL.Formatter/Language/StandardSqlFormatter.cs +++ b/SQL.Formatter/Language/StandardSqlFormatter.cs @@ -5,7 +5,7 @@ namespace SQL.Formatter.Language { public class StandardSqlFormatter : AbstractFormatter { - private static readonly List ReservedWords = new List{ + private static readonly List s_reservedWords = new List{ "ABS", "ALL", "ALLOCATE", @@ -303,7 +303,7 @@ public class StandardSqlFormatter : AbstractFormatter "WITHOUT", "YEAR"}; - private static readonly List ReservedTopLevelWords = + private static readonly List s_reservedTopLevelWords = new List{ "ADD", "ALTER COLUMN", @@ -330,7 +330,7 @@ public class StandardSqlFormatter : AbstractFormatter "VALUES", "WHERE"}; - private static readonly List ReservedTopLevelWordsNoIndent = + private static readonly List s_reservedTopLevelWordsNoIndent = new List{ "INTERSECT", "INTERSECT ALL", @@ -342,7 +342,7 @@ public class StandardSqlFormatter : AbstractFormatter "EXCEPT ALL", "EXCEPT DISTINCT"}; - private static readonly List ReservedNewlineWords = + private static readonly List s_reservedNewlineWords = new List{ "AND", "ELSE", @@ -362,10 +362,10 @@ public class StandardSqlFormatter : AbstractFormatter public override DialectConfig DoDialectConfig() { return DialectConfig.Builder() - .ReservedWords(ReservedWords) - .ReservedTopLevelWords(ReservedTopLevelWords) - .ReservedTopLevelWordsNoIndent(ReservedTopLevelWordsNoIndent) - .ReservedNewlineWords(ReservedNewlineWords) + .ReservedWords(s_reservedWords) + .ReservedTopLevelWords(s_reservedTopLevelWords) + .ReservedTopLevelWordsNoIndent(s_reservedTopLevelWordsNoIndent) + .ReservedNewlineWords(s_reservedNewlineWords) .StringTypes(new List { StringLiteral.DoubleQuote, StringLiteral.SingleQuote }) .OpenParens(new List { "(", "CASE" }) .CloseParens(new List { ")", "END" }) diff --git a/SQL.Formatter/Language/StringLiteral.cs b/SQL.Formatter/Language/StringLiteral.cs index 24b6387..bf659bd 100644 --- a/SQL.Formatter/Language/StringLiteral.cs +++ b/SQL.Formatter/Language/StringLiteral.cs @@ -17,15 +17,15 @@ public class StringLiteral public static readonly string Dollar = "$$"; public static readonly string Bracket = "[]"; - private static readonly Dictionary Literals; + private static readonly Dictionary s_literals; static StringLiteral() { - Literals = Preset.Presets.ToList() + s_literals = Preset.Presets.ToList() .ToDictionary(preset => preset.GetKey(), preset => preset.GetRegex()); } - public static string Get(string key) => Literals[key]; + public static string Get(string key) => s_literals[key]; private class Preset { @@ -108,18 +108,18 @@ public static IEnumerable Presets } } - public readonly string key; - public readonly string regex; + public readonly string Key; + public readonly string Regex; public Preset(string key, string regex) { - this.key = key; - this.regex = regex; + Key = key; + Regex = regex; } - public string GetKey() => key; + public string GetKey() => Key; - public string GetRegex() => regex; + public string GetRegex() => Regex; } } } diff --git a/SQL.Formatter/Language/TSqlFormatter.cs b/SQL.Formatter/Language/TSqlFormatter.cs index f897d03..ccee35d 100644 --- a/SQL.Formatter/Language/TSqlFormatter.cs +++ b/SQL.Formatter/Language/TSqlFormatter.cs @@ -5,7 +5,7 @@ namespace SQL.Formatter.Language { public class TSqlFormatter : AbstractFormatter { - private static readonly List ReservedWords = new List{ + private static readonly List s_reservedWords = new List{ "ADD", "EXTERNAL", "PROCEDURE", @@ -192,7 +192,7 @@ public class TSqlFormatter : AbstractFormatter "EXIT", "PROC"}; - private static readonly List ReservedTopLevelWords = + private static readonly List s_reservedTopLevelWords = new List{ "ADD", "ALTER COLUMN", @@ -216,10 +216,10 @@ public class TSqlFormatter : AbstractFormatter "VALUES", "WHERE"}; - private static readonly List ReservedTopLevelWordsNoIndent = + private static readonly List s_reservedTopLevelWordsNoIndent = new List { "INTERSECT", "INTERSECT ALL", "MINUS", "UNION", "UNION ALL" }; - private static readonly List ReservedNewlineWords = + private static readonly List s_reservedNewlineWords = new List{ "AND", "ELSE", @@ -238,10 +238,10 @@ public class TSqlFormatter : AbstractFormatter public override DialectConfig DoDialectConfig() { return DialectConfig.Builder() - .ReservedWords(ReservedWords) - .ReservedTopLevelWords(ReservedTopLevelWords) - .ReservedTopLevelWordsNoIndent(ReservedTopLevelWordsNoIndent) - .ReservedNewlineWords(ReservedNewlineWords) + .ReservedWords(s_reservedWords) + .ReservedTopLevelWords(s_reservedTopLevelWords) + .ReservedTopLevelWordsNoIndent(s_reservedTopLevelWordsNoIndent) + .ReservedNewlineWords(s_reservedNewlineWords) .StringTypes( new List{ StringLiteral.DoubleQuote, diff --git a/SQL.Formatter/SqlFormatter.cs b/SQL.Formatter/SqlFormatter.cs index f59ad9d..27c0d5b 100644 --- a/SQL.Formatter/SqlFormatter.cs +++ b/SQL.Formatter/SqlFormatter.cs @@ -74,7 +74,7 @@ public Formatter(Func underlying) this.underlying = underlying; } - public Formatter(Dialect dialect) : this(dialect.func) { } + public Formatter(Dialect dialect) : this(dialect.Func) { } public string Format(string query, FormatConfig cfg) { @@ -117,7 +117,7 @@ AbstractFormatter Func(FormatConfig config) { var abstractFormatter = new AbstractFormatter(config) { - doDialectConfigFunc = () => sqlOperator.Invoke(underlying.Invoke(config).DoDialectConfig()) + _doDialectConfigFunc = () => sqlOperator.Invoke(underlying.Invoke(config).DoDialectConfig()) }; return abstractFormatter; }