Skip to content

A Swiss Army knife for developers.

Add SQL formatter to DevToys 2.0 (#1067)

Laurel Baudoux30e4e4189428
diff --git a/src/app/dev/DevToys.Tools/DevToys.Tools.csproj b/src/app/dev/DevToys.Tools/DevToys.Tools.csproj
index f76f47641..c0bfec7b8 100644
--- a/src/app/dev/DevToys.Tools/DevToys.Tools.csproj
+++ b/src/app/dev/DevToys.Tools/DevToys.Tools.csproj
@@ -53,6 +53,11 @@
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
</Compile>
+ <Compile Update="Tools\Formatters\Sql\SqlFormatter.Designer.cs">
+ <DependentUpon>SqlFormatter.resx</DependentUpon>
+ <DesignTime>True</DesignTime>
+ <AutoGen>True</AutoGen>
+ </Compile>
<Compile Update="Tools\Formatters\Xml\XmlFormatter.Designer.cs">
<DependentUpon>XmlFormatter.resx</DependentUpon>
<DesignTime>True</DesignTime>
@@ -225,6 +230,10 @@
<LastGenOutput>JsonFormatter.Designer.cs</LastGenOutput>
<Generator>ResXFileCodeGenerator</Generator>
</EmbeddedResource>
+ <EmbeddedResource Update="Tools\Formatters\Sql\SqlFormatter.resx">
+ <LastGenOutput>SqlFormatter.Designer.cs</LastGenOutput>
+ <Generator>ResXFileCodeGenerator</Generator>
+ </EmbeddedResource>
<EmbeddedResource Update="Tools\Formatters\Xml\XmlFormatter.resx">
<LastGenOutput>XmlFormatter.Designer.cs</LastGenOutput>
<Generator>ResXFileCodeGenerator</Generator>
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/Formatter.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/Formatter.cs
new file mode 100644
index 000000000..17b2bbd7f
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/Formatter.cs
@@ -0,0 +1,361 @@
+using System.Text;
+using System.Text.RegularExpressions;
+
+namespace DevToys.Tools.Helpers.SqlFormatter.Core;
+
+internal abstract class Formatter
+{
+ private static readonly Regex WhitespacesRegex = new(@"\s+$", RegexOptions.Compiled, RegexFactory.DefaultMatchTimeout);
+ private static readonly Regex CommentWhitespacesRegex = new(@"\n[ \t]*", RegexOptions.Compiled, RegexFactory.DefaultMatchTimeout);
+
+ private readonly InlineBlock _inlineBlock = new();
+ private readonly StringBuilder _queryBuilder = new();
+
+ private Indentation? _indentation = null;
+ private SqlFormatterOptions _options;
+ private Params? _params = null;
+ private IReadOnlyList<Token>? _tokens = null;
+ protected Token? _previousReservedToken = null;
+ private int _index;
+
+ /// <summary>
+ /// Formats whitespace in a SQL string to make it easier to read.
+ /// </summary>
+ internal string Format(string query)
+ {
+ return Format(query, new SqlFormatterOptions(indentation: Models.Indentation.TwoSpaces, uppercase: false));
+ }
+
+ /// <summary>
+ /// Formats whitespace in a SQL string to make it easier to read.
+ /// </summary>
+ internal string Format(string query, SqlFormatterOptions options)
+ {
+ _options = options;
+ _indentation = new Indentation(options.Indentation);
+ _params = new Params(options.PlaceholderParameters);
+
+ _tokens = GetTokenizer().Tokenize(query);
+ SetFormattedQueryFromTokens(query.AsSpan());
+ return _queryBuilder.ToString().Trim();
+ }
+
+ /// <summary>
+ /// SQL Tokenizer for this formatter, provided by subclasses.
+ /// </summary>
+ protected abstract Tokenizer GetTokenizer();
+
+ /// <summary>
+ /// Reprocess and modify a token based on parsed context.
+ /// </summary>
+ protected virtual Token TokenOverride(Token token, ReadOnlySpan<char> querySpan)
+ {
+ // subclasses can override this to modify tokens during formatting
+ return token;
+ }
+
+ protected Token? TokenLookBehind(int n = 1)
+ {
+ return _tokens?.ElementAtOrDefault(_index - n);
+ }
+
+ protected Token? TokenLookAhead(int n = 1)
+ {
+ if (_tokens is null || _tokens!.Count <= _index + n)
+ return null;
+ return _tokens![_index + n];
+ }
+
+ private void SetFormattedQueryFromTokens(ReadOnlySpan<char> querySpan)
+ {
+ _queryBuilder.Clear();
+
+ Guard.IsNotNull(_tokens);
+ for (int i = 0; i < _tokens!.Count; i++)
+ {
+ _index = i;
+
+ Token token = TokenOverride(_tokens[i], querySpan);
+ switch (token.Type)
+ {
+ case TokenType.LineComment:
+ FormatLineComment(token, querySpan);
+ break;
+ case TokenType.BlockComment:
+ FormatBlockComment(token, querySpan);
+ break;
+ case TokenType.ReservedTopLevel:
+ FormatTopLevelReservedWord(token, querySpan);
+ _previousReservedToken = token;
+ break;
+ case TokenType.ReservedTopLevelNoIndent:
+ FormatTopLevelReservedWordNoIndent(token, querySpan);
+ _previousReservedToken = token;
+ break;
+ case TokenType.ReservedNewLine:
+ FormatNewlineReservedWord(token, querySpan);
+ _previousReservedToken = token;
+ break;
+ case TokenType.Reserved:
+ FormatWithSpaces(token, querySpan);
+ _previousReservedToken = token;
+ break;
+ case TokenType.OpenParen:
+ FormatOpeningParentheses(token, querySpan);
+ break;
+ case TokenType.CloseParen:
+ FormatClosingParentheses(token, querySpan);
+ break;
+ case TokenType.PlaceHolder:
+ FormatPlaceholder(token, querySpan);
+ break;
+ default:
+ switch (token.Length)
+ {
+ case 1 when querySpan[token.Index] == ',':
+ FormatComma(token, querySpan);
+ break;
+ case 1 when querySpan[token.Index] == ':':
+ FormatWithSpaceAfter(token, querySpan);
+ break;
+ case 1 when querySpan[token.Index] == '.':
+ FormatWithoutSpaces(token, querySpan);
+ break;
+ case 1 when querySpan[token.Index] == ';':
+ FormatQuerySeparator(token, querySpan);
+ break;
+ default:
+ FormatWithSpaces(token, querySpan);
+ break;
+ }
+ break;
+ }
+ }
+ }
+
+ private void FormatLineComment(Token token, ReadOnlySpan<char> querySpan)
+ {
+ _queryBuilder.Append(Show(querySpan.Slice(token), token.Type));
+ AddNewLine();
+ }
+
+ private void FormatBlockComment(Token token, ReadOnlySpan<char> querySpan)
+ {
+ AddNewLine();
+ _queryBuilder.Append(IndentComment(querySpan.Slice(token)));
+ AddNewLine();
+ }
+
+ private string IndentComment(ReadOnlySpan<char> comment)
+ {
+ Guard.IsNotNull(_indentation);
+ return CommentWhitespacesRegex.Replace(comment.ToString(), $"\n{_indentation!.GetIndent()} ");
+ }
+
+ private void FormatTopLevelReservedWordNoIndent(Token token, ReadOnlySpan<char> querySpan)
+ {
+ Guard.IsNotNull(_indentation);
+ _indentation!.DecreaseTopLevel();
+
+ AddNewLine();
+
+ _queryBuilder.Append(EqualizeWhitespace(Show(querySpan.Slice(token), token.Type)));
+
+ AddNewLine();
+ }
+
+ private void FormatTopLevelReservedWord(Token token, ReadOnlySpan<char> querySpan)
+ {
+ Guard.IsNotNull(_indentation);
+ _indentation!.DecreaseTopLevel();
+
+ AddNewLine();
+
+ _indentation.IncreaseTopLevel();
+
+ _queryBuilder.Append(EqualizeWhitespace(Show(querySpan.Slice(token), token.Type)));
+
+ AddNewLine();
+ }
+
+ private void FormatNewlineReservedWord(Token token, ReadOnlySpan<char> querySpan)
+ {
+ if (token.IsAnd(querySpan.Slice(token)))
+ {
+ Token? t = TokenLookBehind(2);
+
+ if (t != null && t.Value.IsBetween(querySpan.Slice(t.Value)))
+ {
+ FormatWithSpaces(token, querySpan);
+ return;
+ }
+ }
+ AddNewLine();
+
+ _queryBuilder.Append(EqualizeWhitespace(Show(querySpan.Slice(token), token.Type)));
+
+ _queryBuilder.Append(' ');
+ }
+
+ /// <summary>
+ /// Replace any sequence of whitespace characters with single space
+ /// </summary>
+ private static string EqualizeWhitespace(string input)
+ {
+ return WhitespacesRegex.Replace(input, " ");
+ }
+
+ /// <summary>
+ /// Opening parentheses increase the block indent level and start a new line
+ /// </summary>
+ private void FormatOpeningParentheses(Token token, ReadOnlySpan<char> querySpan)
+ {
+ // Take out the preceding space unless there was whitespace there in the original query
+ // or another opening parens or line comment
+ if (token.PrecedingWitespaceLength == 0)
+ {
+ Token? behindToken = TokenLookBehind();
+
+ if (behindToken is not { Type: TokenType.OpenParen or TokenType.LineComment or TokenType.Operator }
+ && !behindToken?.IsValues(querySpan.Slice(behindToken!.Value)) == true)
+ {
+ _queryBuilder.TrimSpaceEnd();
+ }
+ }
+ _queryBuilder.Append(Show(querySpan.Slice(token), token.Type));
+
+ Guard.IsNotNull(token);
+ _inlineBlock.BeginIfPossible(_tokens!, _index, querySpan.Slice(token));
+
+ if (!_inlineBlock.IsActive())
+ {
+ Guard.IsNotNull(_indentation);
+ _indentation!.IncreaseBlockLevel();
+ AddNewLine();
+ }
+ }
+
+ /// <summary>
+ /// Closing parentheses decrease the block indent level
+ /// </summary>
+ private void FormatClosingParentheses(Token token, ReadOnlySpan<char> querySpan)
+ {
+ Guard.IsNotNull(_indentation);
+ if (_inlineBlock.IsActive())
+ {
+ _inlineBlock.End();
+ FormatWithSpaceAfter(token, querySpan);
+ }
+ else
+ {
+ _indentation!.DecreaseBlockLevel();
+ AddNewLine();
+ FormatWithSpaces(token, querySpan);
+ }
+ }
+
+ private void FormatPlaceholder(Token token, ReadOnlySpan<char> querySpan)
+ {
+ Guard.IsNotNull(_params);
+
+ string? value = null;
+ ReadOnlySpan<char> valueSpan = querySpan.Slice(token);
+ if (valueSpan.Length != 0)
+ // assumme the length of all placeholder is 1, since only char array is accepted
+ value = _params!.Get(valueSpan.Slice(0, 1).ToString());
+
+ _queryBuilder.Append(value ?? querySpan.Slice(token).ToString());
+
+ _queryBuilder.Append(' ');
+ }
+
+ /// <summary>
+ /// Commas start a new line (unless within inline parentheses or SQL "LIMIT" clause)
+ /// </summary>
+ private void FormatComma(Token token, ReadOnlySpan<char> querySpan)
+ {
+ FormatWithSpaceAfter(token, querySpan);
+
+ if (_inlineBlock.IsActive())
+ {
+ return;
+ }
+ else if (_previousReservedToken is not null
+ && _previousReservedToken.Value.IsLimit(querySpan.Slice(_previousReservedToken.Value)))
+ {
+ return;
+ }
+
+ AddNewLine();
+
+ }
+
+ private void FormatWithSpaceAfter(Token token, ReadOnlySpan<char> querySpan)
+ {
+ _queryBuilder.TrimSpaceEnd();
+ _queryBuilder.Append(Show(querySpan.Slice(token), token.Type));
+ _queryBuilder.Append(' ');
+ }
+
+ private void FormatWithoutSpaces(Token token, ReadOnlySpan<char> querySpan)
+ {
+ _queryBuilder.TrimSpaceEnd();
+ _queryBuilder.Append(Show(querySpan.Slice(token), token.Type));
+ }
+
+ private void FormatWithSpaces(Token token, ReadOnlySpan<char> querySpan)
+ {
+ _queryBuilder.Append(Show(querySpan.Slice(token), token.Type));
+ _queryBuilder.Append(' ');
+ }
+
+ private void FormatQuerySeparator(Token token, ReadOnlySpan<char> querySpan)
+ {
+ Guard.IsNotNull(_indentation);
+ _indentation!.ResetIndentation();
+
+ _queryBuilder.TrimSpaceEnd();
+ _queryBuilder.Append(Show(querySpan.Slice(token), token.Type));
+
+ int times = _options.LinesBetweenQueries;
+
+ while (times != 0)
+ {
+ _queryBuilder.AppendLine();
+ times--;
+ }
+ }
+
+ /// <summary>
+ /// Converts token to string (uppercasing it if needed)
+ /// </summary>
+ private string Show(ReadOnlySpan<char> value, TokenType tokenType)
+ {
+ if (_options.Uppercase
+ && (tokenType == TokenType.Reserved
+ || tokenType == TokenType.ReservedTopLevel
+ || tokenType == TokenType.ReservedTopLevelNoIndent
+ || tokenType == TokenType.ReservedNewLine
+ || tokenType == TokenType.OpenParen
+ || tokenType == TokenType.CloseParen))
+ {
+ return value.ToString().ToUpper();
+ }
+ else
+ {
+ return value.ToString();
+ }
+ }
+
+ private void AddNewLine()
+ {
+ Guard.IsNotNull(_indentation);
+
+ _queryBuilder.TrimSpaceEnd();
+
+ if (_queryBuilder.Length != 0 && _queryBuilder[_queryBuilder.Length - 1] != '\n')
+ _queryBuilder.AppendLine();
+
+ _queryBuilder.Append(_indentation!.GetIndent());
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/Indentation.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/Indentation.cs
new file mode 100644
index 000000000..f2954089a
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/Indentation.cs
@@ -0,0 +1,87 @@
+namespace DevToys.Tools.Helpers.SqlFormatter.Core;
+
+/// <summary>
+/// Manages indentation levels.
+/// </summary>
+internal sealed class Indentation
+{
+ private enum IndentationType
+ {
+ /// <summary>
+ /// increased by ReservedTopLevel words
+ /// </summary>
+ TopLevel,
+
+ /// <summary>
+ /// increased by open-parenthesis
+ /// </summary>
+ BlockLevel
+ }
+
+ private readonly Stack<IndentationType> _indentationTypes = new();
+ private readonly Models.Indentation _indentation;
+
+ public Indentation(Models.Indentation indentation)
+ {
+ _indentation = indentation;
+ }
+
+ /// <summary>
+ /// Returns current indentation string.
+ /// </summary>
+ internal string GetIndent()
+ {
+ return _indentation switch
+ {
+ Models.Indentation.TwoSpaces => new string(' ', 2 * _indentationTypes.Count),
+ Models.Indentation.FourSpaces => new string(' ', 4 * _indentationTypes.Count),
+ Models.Indentation.OneTab => new string('\t', 1 * _indentationTypes.Count),
+ _ => throw new NotSupportedException(),
+ };
+ }
+
+ /// <summary>
+ /// Increases indentation by one top-level indent.
+ /// </summary>
+ internal void IncreaseTopLevel()
+ {
+ _indentationTypes.Push(IndentationType.TopLevel);
+ }
+
+ /// <summary>
+ /// Increases indentation by one block-level indent.
+ /// </summary>
+ internal void IncreaseBlockLevel()
+ {
+ _indentationTypes.Push(IndentationType.BlockLevel);
+ }
+
+ /// <summary>
+ /// Decreases indentation by one top-level indent.
+ /// Does nothing when the previous indent is not top-level.
+ /// </summary>
+ internal void DecreaseTopLevel()
+ {
+ if (_indentationTypes.TryPeek(out IndentationType type) && type == IndentationType.TopLevel)
+ _indentationTypes.Pop();
+ }
+
+ /// <summary>
+ /// Decreases indentation by one block-level indent.
+ /// If there are top-level indents within the block-level indent, throws away these as well.
+ /// </summary>
+ internal void DecreaseBlockLevel()
+ {
+ while (_indentationTypes.Count > 0)
+ {
+ IndentationType type = _indentationTypes.Pop();
+ if (type != IndentationType.TopLevel)
+ break;
+ }
+ }
+
+ internal void ResetIndentation()
+ {
+ _indentationTypes.Clear();
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/InlineBlock.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/InlineBlock.cs
new file mode 100644
index 000000000..372fc0b26
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/InlineBlock.cs
@@ -0,0 +1,102 @@
+namespace DevToys.Tools.Helpers.SqlFormatter.Core;
+
+/// <summary>
+/// Bookkeeper for inline blocks.
+/// Inline blocks are parenthized expressions that are shorter than INLINE_MAX_LENGTH.
+/// These blocks are formatted on a single line, unlike longer parenthized expressions
+/// where open-parenthesis causes newline and increase of indentation.
+/// </summary>
+internal sealed class InlineBlock
+{
+ private const int InlineMaxLength = 50;
+
+ private int _level = 0;
+
+ /// <summary>
+ /// Begins inline block when lookahead through upcoming tokens determines that the
+ /// block would be smaller than INLINE_MAX_LENGTH.
+ /// </summary>
+ /// <param name="tokens">Array of all tokens</param>
+ /// <param name="index">Current token position</param>
+ internal void BeginIfPossible(IReadOnlyList<Token> tokens, int index, ReadOnlySpan<char> valueSpan)
+ {
+ if (_level == 0 && IsInlineBlock(tokens, index, valueSpan))
+ {
+ _level = 1;
+ }
+ else if (_level > 0)
+ {
+ _level++;
+ }
+ else
+ {
+ _level = 0;
+ }
+ }
+
+ /// <summary>
+ /// Finishes current inline block. There might be several nested ones.
+ /// </summary>
+ internal void End()
+ {
+ _level--;
+ }
+
+ /// <summary>
+ /// True when inside an inline block
+ /// </summary>
+ internal bool IsActive()
+ {
+ return _level > 0;
+ }
+
+ /// <summary>
+ /// Check if this should be an inline parentheses block.
+ /// Examples are "NOW()", "COUNT(*)", "int(10)", key(`somecolumn`), DECIMAL(7,2)
+ /// </summary>
+ private static bool IsInlineBlock(IReadOnlyList<Token> tokens, int index, ReadOnlySpan<char> valueSpan)
+ {
+ int length = 0;
+ int level = 0;
+
+ for (int i = index; i < tokens.Count; i++)
+ {
+ Token token = tokens[i];
+ length += token.Length;
+
+ // Overran max length
+ if (length > InlineMaxLength)
+ {
+ return false;
+ }
+
+ if (token.Type == TokenType.OpenParen)
+ {
+ level++;
+ }
+ else if (token.Type == TokenType.CloseParen)
+ {
+ level--;
+ if (level == 0)
+ return true;
+ }
+
+ if (IsForbiddenToken(token, valueSpan))
+ return false;
+ }
+ return false;
+ }
+
+ /// <summary>
+ /// Reserved words that cause newlines, comments and semicolons are not allowed inside inline parentheses block
+ /// </summary>
+ private static bool IsForbiddenToken(Token token, ReadOnlySpan<char> valueSpan)
+ {
+ return
+ token.Type == TokenType.ReservedTopLevel
+ || token.Type == TokenType.ReservedNewLine
+ // || token.Type == TokenType.LineComment
+ || token.Type == TokenType.BlockComment
+ || token.Length == 1 && valueSpan[0] == ';';
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/Params.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/Params.cs
new file mode 100644
index 000000000..91c1049df
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/Params.cs
@@ -0,0 +1,29 @@
+namespace DevToys.Tools.Helpers.SqlFormatter.Core;
+
+/// <summary>
+/// Handles placeholder replacement with given params.
+/// </summary>
+internal sealed class Params
+{
+ private readonly IReadOnlyDictionary<string, string>? _params;
+ private int _index;
+
+ public Params(IReadOnlyDictionary<string, string>? parameters)
+ {
+ _params = parameters;
+ }
+
+ internal string? Get(string key)
+ {
+ if (_params is null)
+ return null;
+
+ if (key is not null && key.Length != 0)
+ {
+ _params.TryGetValue(key, out string? paramValue);
+ return paramValue;
+ }
+
+ return _params.ElementAtOrDefault(_index++).Value ?? null;
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/RegexFactory.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/RegexFactory.cs
new file mode 100644
index 000000000..4650ef9ec
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/RegexFactory.cs
@@ -0,0 +1,108 @@
+using System.Text.RegularExpressions;
+
+namespace DevToys.Tools.Helpers.SqlFormatter.Core;
+
+internal static class RegexFactory
+{
+ internal static readonly TimeSpan DefaultMatchTimeout = TimeSpan.FromSeconds(1);
+ private static readonly Regex SpecialCharacterRegex = new(@"[.*+?^${}()|[\]\\]", RegexOptions.Compiled, DefaultMatchTimeout);
+ private static readonly Dictionary<string, string> Patterns = new()
+ {
+ { "``", "((`[^`]*($|`))+)" },
+ { "{}", "((\\{[^\\}]*($|\\}))+)" },
+ { "[]", "((\\[[^\\]]*($|\\]))(\\][^\\]]*($|\\]))*)" },
+ { "\"\"", "((\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*(\"|$))+)" },
+ { "''", "(('[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)" },
+ { "N''", "((N'[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)" },
+ { "U&''", "((U&'[^'\\\\]*(?:\\\\.[^'\\\\]*)*('|$))+)" },
+ { "U&\"\"", "((U&\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*(\"|$))+)" },
+ { "$$", "((?<tag>\\$\\w*\\$)[\\s\\S]*?(?:\\k<tag>|$))" }
+ };
+
+ internal static Regex CreateOperatorRegex(IEnumerable<string> multiLetterOperators)
+ {
+ IOrderedEnumerable<string> sortedOperators = SortByLengthDesc(multiLetterOperators);
+ IEnumerable<string> escapedOperators = sortedOperators.Select(item => EscapeSpecialCharacters(item));
+ string operators = string.Join("|", escapedOperators);
+ return new Regex(@$"^({operators}|.)", RegexOptions.None, DefaultMatchTimeout);
+ }
+
+ internal static Regex CreateLineCommentRegex(string[] lineCommentTypes)
+ {
+ return new Regex($"^((?:{string.Join('|', lineCommentTypes.Select(item => EscapeSpecialCharacters(item)))}).*?)(?:\\r\\n|\\r|\\n|$)", RegexOptions.Singleline, DefaultMatchTimeout);
+ }
+
+ internal static Regex CreateReservedWordRegex(string[] reservedWords)
+ {
+ if (reservedWords.Length == 0)
+ return new Regex(@"^\b$", RegexOptions.None, DefaultMatchTimeout);
+
+ string reservedWordsPattern = string.Join('|', SortByLengthDesc(reservedWords)).Replace(" ", "\\s+");
+ return new Regex(@$"^({reservedWordsPattern})\b", RegexOptions.IgnoreCase, DefaultMatchTimeout);
+ }
+
+ internal static Regex CreateWordRegex(string[] specialCharacters)
+ {
+ return new Regex(@"^([\p{L}\p{M}\p{Nd}\p{Pc}\p{Cf}\p{Cs}\p{Co}" + $"{string.Join(string.Empty, specialCharacters)}]+)", RegexOptions.None, DefaultMatchTimeout);
+ }
+
+ internal static Regex CreateStringRegex(string[] stringTypes)
+ {
+ return new Regex($"^({CreateStringPattern(stringTypes)})", RegexOptions.None, DefaultMatchTimeout);
+ }
+
+ /// <summary>
+ /// This enables the following string patterns:
+ /// 1. backtick quoted string using `` to escape
+ /// 2. square bracket quoted string (SQL Server) using ]] to escape
+ /// 3. double quoted string using "" or \" to escape
+ /// 4. single quoted string using '' or \' to escape
+ /// 5. national character quoted string using N'' or N\' to escape
+ /// 6. Unicode single-quoted string using \' to escape
+ /// 7. Unicode double-quoted string using \" to escape
+ /// 8. PostgreSQL dollar-quoted strings
+ /// </summary>
+ internal static string CreateStringPattern(string[] stringTypes)
+ {
+ return string.Join('|', stringTypes.Select(item => Patterns[item]));
+ }
+
+ internal static Regex? CreatePlaceholderRegex(char[] types, string pattern)
+ {
+ if (types is null || types.Length == 0)
+ return null;
+
+ string typesRegex = string.Join('|', types.Select(item => EscapeSpecialCharacters(item.ToString())));
+
+ return new Regex($"^((?:{typesRegex})(?:{pattern}))", RegexOptions.None, DefaultMatchTimeout);
+ }
+
+ internal static Regex CreateParenRegex(string[] parens)
+ {
+ return new Regex($"^({string.Join('|', parens.Select(item => EscapeParen(item)))})", RegexOptions.IgnoreCase, DefaultMatchTimeout);
+ }
+
+ private static string EscapeParen(string paren)
+ {
+ if (paren.Length == 1)
+ {
+ // A single punctuation character
+ return EscapeSpecialCharacters(paren);
+ }
+ else
+ {
+ // longer word
+ return $"\\b{paren}\\b";
+ }
+ }
+
+ private static IOrderedEnumerable<string> SortByLengthDesc(IEnumerable<string> strings)
+ {
+ return strings.OrderByDescending(s => s.Length);
+ }
+
+ private static string EscapeSpecialCharacters(string input)
+ {
+ return SpecialCharacterRegex.Replace(input, "\\$&");
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/Token.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/Token.cs
new file mode 100644
index 000000000..92a46c6ad
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/Token.cs
@@ -0,0 +1,17 @@
+namespace DevToys.Tools.Helpers.SqlFormatter.Core;
+
+internal struct Token
+{
+ internal readonly int Index { get; }
+ internal readonly int Length { get; }
+ internal int PrecedingWitespaceLength { get; set; }
+ internal readonly TokenType Type { get; }
+
+ public Token(int index, int length, TokenType type, int precedingWitespaceLength = 0)
+ {
+ Index = index;
+ Length = length;
+ Type = type;
+ PrecedingWitespaceLength = precedingWitespaceLength;
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/TokenHelper.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/TokenHelper.cs
new file mode 100644
index 000000000..71f995472
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/TokenHelper.cs
@@ -0,0 +1,51 @@
+namespace DevToys.Tools.Helpers.SqlFormatter.Core;
+
+internal static class TokenHelper
+{
+ internal static bool IsAnd(this Token token, ReadOnlySpan<char> tokenValueSpan)
+ {
+ return IsToken(token.Type, TokenType.ReservedNewLine, tokenValueSpan, "AND".AsSpan());
+ }
+
+ internal static bool IsBetween(this Token token, ReadOnlySpan<char> tokenValueSpan)
+ {
+ return IsToken(token.Type, TokenType.Reserved, tokenValueSpan, "BETWEEN".AsSpan());
+ }
+
+ internal static bool IsLimit(this Token token, ReadOnlySpan<char> tokenValueSpan)
+ {
+ return IsToken(token.Type, TokenType.ReservedTopLevel, tokenValueSpan, "LIMIT".AsSpan());
+ }
+
+ internal static bool IsSet(this Token token, ReadOnlySpan<char> tokenValueSpan)
+ {
+ return IsToken(token.Type, TokenType.ReservedTopLevel, tokenValueSpan, "SET".AsSpan());
+ }
+
+ internal static bool IsBy(this Token token, ReadOnlySpan<char> tokenValueSpan)
+ {
+ return IsToken(token.Type, TokenType.Reserved, tokenValueSpan, "BY".AsSpan());
+ }
+
+ internal static bool IsWindow(this Token token, ReadOnlySpan<char> tokenValueSpan)
+ {
+ return IsToken(token.Type, TokenType.ReservedTopLevel, tokenValueSpan, "WINDOW".AsSpan());
+ }
+
+ internal static bool IsEnd(this Token token, ReadOnlySpan<char> tokenValueSpan)
+ {
+ return IsToken(token.Type, TokenType.CloseParen, tokenValueSpan, "END".AsSpan());
+ }
+
+ internal static bool IsValues(this Token token, ReadOnlySpan<char> tokenValueSpan)
+ {
+ return IsToken(token.Type, TokenType.ReservedTopLevel, tokenValueSpan, "VALUES".AsSpan());
+ }
+
+ private static bool IsToken(TokenType type, TokenType otherType,
+ ReadOnlySpan<char> tokenValueSpan, ReadOnlySpan<char> otherSpan)
+ {
+ return type == otherType &&
+ tokenValueSpan.Equals(otherSpan, StringComparison.OrdinalIgnoreCase);
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/TokenType.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/TokenType.cs
new file mode 100644
index 000000000..6a2398737
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/TokenType.cs
@@ -0,0 +1,18 @@
+namespace DevToys.Tools.Helpers.SqlFormatter.Core;
+
+internal enum TokenType
+{
+ Word,
+ String,
+ Reserved,
+ ReservedTopLevel,
+ ReservedTopLevelNoIndent,
+ ReservedNewLine,
+ Operator,
+ OpenParen,
+ CloseParen,
+ LineComment,
+ BlockComment,
+ Number,
+ PlaceHolder
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/Tokenizer.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/Tokenizer.cs
new file mode 100644
index 000000000..5e13a6bf1
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Core/Tokenizer.cs
@@ -0,0 +1,254 @@
+using System.Text.RegularExpressions;
+
+namespace DevToys.Tools.Helpers.SqlFormatter.Core;
+
+internal class Tokenizer
+{
+ private static readonly Regex NumberRegex = new(@"^((-\s*)?[0-9]+(\.[0-9]+)?([eE]-?[0-9]+(\.[0-9]+)?)?|0x[0-9a-fA-F]+|0b[01]+)\b", RegexOptions.Compiled, RegexFactory.DefaultMatchTimeout);
+ private static readonly Regex BlockCommentRegex = new(@"^(\/\*(.*?)*?(?:\*\/|$))", RegexOptions.Singleline | RegexOptions.Compiled, RegexFactory.DefaultMatchTimeout);
+
+ private readonly Regex _operatorRegex;
+ private readonly Regex _lineCommentRegex;
+ private readonly Regex _reservedTopLevelRegex;
+ private readonly Regex _reservedTopLevelNoIndentRegex;
+ private readonly Regex _reservedNewLineRegex;
+ private readonly Regex _reservedPlainRegex;
+ private readonly Regex _wordRegex;
+ private readonly Regex _stringRegex;
+ private readonly Regex _openParenRegex;
+ private readonly Regex _closeParenRegex;
+ private readonly Regex? _indexedPlaceholderRegex;
+ private readonly Regex? _indentNamedPlaceholderRegex;
+
+ /// <summary>
+ /// Initializes a new instance of <see cref="Tokenizer"/> class.
+ /// </summary>
+ /// <param name="reservedWords">Reserved words in SQL</param>
+ /// <param name="reservedTopLevelWords">Words that are set to new line separately</param>
+ /// <param name="reservedNewlineWords">Words that are set to newline</param>
+ /// <param name="reservedTopLevelWordsNoIndent">Words that are top level but have no indentation</param>
+ /// <param name="stringTypes">String types to enable: "", '', ``, [], N''</param>
+ /// <param name="openParens">Opening parentheses to enable, like (, [</param>
+ /// <param name="closeParens">Closing parentheses to enable, like ), ]</param>
+ /// <param name="indexedPlaceholderTypes">Prefixes for indexed placeholders, like ?</param>
+ /// <param name="namedPlaceholderTypes">Prefixes for named placeholders, like @ and :</param>
+ /// <param name="lineCommentTypes">Line comments to enable, like # and --</param>
+ /// <param name="specialWordChars">Special chars that can be found inside of words, like @ and #</param>
+ /// <param name="operators">Additional operators to recognize</param>
+ public Tokenizer(
+ string[] reservedWords,
+ string[] reservedTopLevelWords,
+ string[] reservedNewlineWords,
+ string[] reservedTopLevelWordsNoIndent,
+ string[] stringTypes,
+ string[] openParens,
+ string[] closeParens,
+ char[] indexedPlaceholderTypes,
+ char[] namedPlaceholderTypes,
+ string[] lineCommentTypes,
+ string[] specialWordChars,
+ string[]? operators = null)
+ {
+ var operatorsParam = new List<string> { "<>", "<=", ">=", "&&", "||", "!=" };
+ if (operators is not null)
+ {
+ operatorsParam.AddRange(operators);
+ }
+ _operatorRegex = RegexFactory.CreateOperatorRegex(operatorsParam);
+
+ _lineCommentRegex = RegexFactory.CreateLineCommentRegex(lineCommentTypes);
+ _reservedTopLevelRegex = RegexFactory.CreateReservedWordRegex(reservedTopLevelWords);
+ _reservedTopLevelNoIndentRegex = RegexFactory.CreateReservedWordRegex(reservedTopLevelWordsNoIndent);
+ _reservedNewLineRegex = RegexFactory.CreateReservedWordRegex(reservedNewlineWords);
+ _reservedPlainRegex = RegexFactory.CreateReservedWordRegex(reservedWords);
+ _wordRegex = RegexFactory.CreateWordRegex(specialWordChars);
+ _stringRegex = RegexFactory.CreateStringRegex(stringTypes);
+ _openParenRegex = RegexFactory.CreateParenRegex(openParens);
+ _closeParenRegex = RegexFactory.CreateParenRegex(closeParens);
+ _indexedPlaceholderRegex = RegexFactory.CreatePlaceholderRegex(indexedPlaceholderTypes, "[0-9]*");
+ _indentNamedPlaceholderRegex = RegexFactory.CreatePlaceholderRegex(namedPlaceholderTypes, "[a-zA-Z0-9._$]+");
+ }
+
+ /// <summary>
+ /// Takes a SQL string and breaks it into tokens.
+ /// </summary>
+ /// <param name="input">The SQL string</param>
+ /// <returns></returns>
+ internal IReadOnlyList<Token> Tokenize(string input)
+ {
+ var tokens = new List<Token>();
+ Token? token = null;
+ int pointerIndex = 0;
+
+ // Keep processing the string until it is empty
+ while (pointerIndex != input.Length)
+ {
+ // grab any preceding whitespace length
+ int precedingWhitespaceLength = GetPrecedingWhitespaceLength(input, pointerIndex);
+
+ pointerIndex += precedingWhitespaceLength;
+
+ if (pointerIndex != input.Length)
+ {
+ // Get the next token and the token type
+ token = GetNextToken(input, pointerIndex, previousToken: token);
+
+ if (token is not null)
+ {
+ Token t = token.Value;
+ // Advance the index pointer string
+ pointerIndex += t.Length;
+ t.PrecedingWitespaceLength = precedingWhitespaceLength;
+ tokens.Add(t);
+ }
+ }
+ }
+
+ return tokens;
+ }
+
+ private static int GetPrecedingWhitespaceLength(string input, int pointerIndex)
+ {
+ int i = 0;
+ int len = input.Length - pointerIndex;
+ for (; i < len; i++)
+ {
+ if (!char.IsWhiteSpace(input[i + pointerIndex]))
+ {
+ break;
+ }
+ }
+ return i;
+ }
+
+ private Token? GetNextToken(string input, int pointerIndex, Token? previousToken)
+ {
+ return GetCommentToken(input, pointerIndex)
+ ?? GetStringToken(input, pointerIndex)
+ ?? GetOpenParenToken(input, pointerIndex)
+ ?? GetCloseParenToken(input, pointerIndex)
+ ?? GetPlaceholderToken(input, pointerIndex)
+ ?? GetNumberToken(input, pointerIndex)
+ ?? GetReservedWordToken(input, pointerIndex, previousToken)
+ ?? GetWordToken(input, pointerIndex)
+ ?? GetOperatorToken(input, pointerIndex);
+ }
+
+ private Token? GetCommentToken(string input, int pointerIndex)
+ {
+ return GetLineCommentToken(input, pointerIndex)
+ ?? GetBlockCommentToken(input, pointerIndex);
+ }
+
+ private Token? GetLineCommentToken(string input, int pointerIndex)
+ {
+ return GetTokenOnFirstMatch(input, pointerIndex, TokenType.LineComment, _lineCommentRegex);
+ }
+
+ private static Token? GetBlockCommentToken(string input, int pointerIndex)
+ {
+ return GetTokenOnFirstMatch(input, pointerIndex, TokenType.BlockComment, BlockCommentRegex);
+ }
+
+ private Token? GetStringToken(string input, int pointerIndex)
+ {
+ return GetTokenOnFirstMatch(input, pointerIndex, TokenType.String, _stringRegex);
+ }
+
+ private Token? GetOpenParenToken(string input, int pointerIndex)
+ {
+ return GetTokenOnFirstMatch(input, pointerIndex, TokenType.OpenParen, _openParenRegex);
+ }
+
+ private Token? GetCloseParenToken(string input, int pointerIndex)
+ {
+ return GetTokenOnFirstMatch(input, pointerIndex, TokenType.CloseParen, _closeParenRegex);
+ }
+
+ private Token? GetPlaceholderToken(string input, int pointerIndex)
+ {
+ return GetIdentNamedPlaceholderToken(input, pointerIndex)
+ ?? GetIndexedPlaceholderToken(input, pointerIndex);
+ }
+
+ private Token? GetIdentNamedPlaceholderToken(string input, int pointerIndex)
+ {
+ return GetPlaceholderTokenWithKey(input, pointerIndex, _indentNamedPlaceholderRegex);
+ }
+
+ private Token? GetIndexedPlaceholderToken(string input, int pointerIndex)
+ {
+ return GetPlaceholderTokenWithKey(input, pointerIndex, _indexedPlaceholderRegex);
+ }
+
+ private static Token? GetPlaceholderTokenWithKey(string input, int pointerIndex, Regex? regex)
+ {
+ return GetTokenOnFirstMatch(input, pointerIndex, TokenType.PlaceHolder, regex);
+ }
+
+ private static Token? GetNumberToken(string input, int pointerIndex)
+ {
+ // Decimal, binary, or hex numbers
+ return GetTokenOnFirstMatch(input, pointerIndex, TokenType.Number, NumberRegex);
+ }
+
+ private Token? GetWordToken(string input, int pointerIndex)
+ {
+ return GetTokenOnFirstMatch(input, pointerIndex, TokenType.Word, _wordRegex);
+ }
+
+ private Token? GetOperatorToken(string input, int pointerIndex)
+ {
+ // Punctuation and symbols
+ return GetTokenOnFirstMatch(input, pointerIndex, TokenType.Operator, _operatorRegex);
+ }
+
+ private Token? GetReservedWordToken(string input, int pointerIndex, Token? previousToken)
+ {
+ // A reserved word cannot be preceded by a "."
+ // this makes it so in "mytable.from", "from" is not considered a reserved word
+ if (previousToken is not null
+ && previousToken.Value.Length == 1
+ && input[previousToken.Value.Index] == '.')
+ {
+ return null;
+ }
+
+ return GetTopLevelReservedToken(input, pointerIndex)
+ ?? GetNewlineReservedToken(input, pointerIndex)
+ ?? GetTopLevelReservedTokenNoIndent(input, pointerIndex)
+ ?? GetPlainReservedToken(input, pointerIndex);
+ }
+
+ private Token? GetTopLevelReservedToken(string input, int pointerIndex)
+ {
+ return GetTokenOnFirstMatch(input, pointerIndex, TokenType.ReservedTopLevel, _reservedTopLevelRegex);
+ }
+
+ private Token? GetNewlineReservedToken(string input, int pointerIndex)
+ {
+ return GetTokenOnFirstMatch(input, pointerIndex, TokenType.ReservedNewLine, _reservedNewLineRegex);
+ }
+
+ private Token? GetTopLevelReservedTokenNoIndent(string input, int pointerIndex)
+ {
+ return GetTokenOnFirstMatch(input, pointerIndex, TokenType.ReservedTopLevelNoIndent, _reservedTopLevelNoIndentRegex);
+ }
+
+ private Token? GetPlainReservedToken(string input, int pointerIndex)
+ {
+ return GetTokenOnFirstMatch(input, pointerIndex, TokenType.Reserved, _reservedPlainRegex);
+ }
+
+ private static Token? GetTokenOnFirstMatch(string input, int pointerIndex, TokenType type, Regex? regex)
+ {
+ if (regex is null)
+ {
+ return null;
+ }
+
+ Match match = regex.Match(input, pointerIndex, input.Length - pointerIndex);
+
+ return match.Success ? new Token(pointerIndex, match.Length, type) : null;
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/Db2Formatter.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/Db2Formatter.cs
new file mode 100644
index 000000000..cd8c7dedd
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/Db2Formatter.cs
@@ -0,0 +1,578 @@
+using DevToys.Tools.Helpers.SqlFormatter.Core;
+
+namespace DevToys.Tools.Helpers.SqlFormatter.Languages;
+
+internal sealed class Db2Formatter : Formatter
+{
+ private static readonly string[] ReservedWords
+ =
+ {
+ "ABS",
+ "ACTIVATE",
+ "ALIAS",
+ "ALL",
+ "ALLOCATE",
+ "ALLOW",
+ "ALTER",
+ "ANY",
+ "ARE",
+ "ARRAY",
+ "AS",
+ "ASC",
+ "ASENSITIVE",
+ "ASSOCIATE",
+ "ASUTIME",
+ "ASYMMETRIC",
+ "AT",
+ "ATOMIC",
+ "ATTRIBUTES",
+ "AUDIT",
+ "AUTHORIZATION",
+ "AUX",
+ "AUXILIARY",
+ "AVG",
+ "BEFORE",
+ "BEGIN",
+ "BETWEEN",
+ "BIGINT",
+ "BINARY",
+ "BLOB",
+ "BOOLEAN",
+ "BOTH",
+ "BUFFERPOOL",
+ "BY",
+ "CACHE",
+ "CALL",
+ "CALLED",
+ "CAPTURE",
+ "CARDINALITY",
+ "CASCADED",
+ "CASE",
+ "CAST",
+ "CCSID",
+ "CEIL",
+ "CEILING",
+ "CHAR",
+ "CHARACTER",
+ "CHARACTER_LENGTH",
+ "CHAR_LENGTH",
+ "CHECK",
+ "CLOB",
+ "CLONE",
+ "CLOSE",
+ "CLUSTER",
+ "COALESCE",
+ "COLLATE",
+ "COLLECT",
+ "COLLECTION",
+ "COLLID",
+ "COLUMN",
+ "COMMENT",
+ "COMMIT",
+ "CONCAT",
+ "CONDITION",
+ "CONNECT",
+ "CONNECTION",
+ "CONSTRAINT",
+ "CONTAINS",
+ "CONTINUE",
+ "CONVERT",
+ "CORR",
+ "CORRESPONDING",
+ "COUNT",
+ "COUNT_BIG",
+ "COVAR_POP",
+ "COVAR_SAMP",
+ "CREATE",
+ "CROSS",
+ "CUBE",
+ "CUME_DIST",
+ "CURRENT",
+ "CURRENT_DATE",
+ "CURRENT_DEFAULT_TRANSFORM_GROUP",
+ "CURRENT_LC_CTYPE",
+ "CURRENT_PATH",
+ "CURRENT_ROLE",
+ "CURRENT_SCHEMA",
+ "CURRENT_SERVER",
+ "CURRENT_TIME",
+ "CURRENT_TIMESTAMP",
+ "CURRENT_TIMEZONE",
+ "CURRENT_TRANSFORM_GROUP_FOR_TYPE",
+ "CURRENT_USER",
+ "CURSOR",
+ "CYCLE",
+ "DATA",
+ "DATABASE",
+ "DATAPARTITIONNAME",
+ "DATAPARTITIONNUM",
+ "DATE",
+ "DAY",
+ "DAYS",
+ "DB2GENERAL",
+ "DB2GENRL",
+ "DB2SQL",
+ "DBINFO",
+ "DBPARTITIONNAME",
+ "DBPARTITIONNUM",
+ "DEALLOCATE",
+ "DEC",
+ "DECIMAL",
+ "DECLARE",
+ "DEFAULT",
+ "DEFAULTS",
+ "DEFINITION",
+ "DELETE",
+ "DENSERANK",
+ "DENSE_RANK",
+ "DEREF",
+ "DESCRIBE",
+ "DESCRIPTOR",
+ "DETERMINISTIC",
+ "DIAGNOSTICS",
+ "DISABLE",
+ "DISALLOW",
+ "DISCONNECT",
+ "DISTINCT",
+ "DO",
+ "DOCUMENT",
+ "DOUBLE",
+ "DROP",
+ "DSSIZE",
+ "DYNAMIC",
+ "EACH",
+ "EDITPROC",
+ "ELEMENT",
+ "ELSE",
+ "ELSEIF",
+ "ENABLE",
+ "ENCODING",
+ "ENCRYPTION",
+ "END",
+ "END-EXEC",
+ "ENDING",
+ "ERASE",
+ "ESCAPE",
+ "EVERY",
+ "EXCEPTION",
+ "EXCLUDING",
+ "EXCLUSIVE",
+ "EXEC",
+ "EXECUTE",
+ "EXISTS",
+ "EXIT",
+ "EXP",
+ "EXPLAIN",
+ "EXTENDED",
+ "EXTERNAL",
+ "EXTRACT",
+ "FALSE",
+ "FENCED",
+ "FETCH",
+ "FIELDPROC",
+ "FILE",
+ "FILTER",
+ "FINAL",
+ "FIRST",
+ "FLOAT",
+ "FLOOR",
+ "FOR",
+ "FOREIGN",
+ "FREE",
+ "FULL",
+ "FUNCTION",
+ "FUSION",
+ "GENERAL",
+ "GENERATED",
+ "GET",
+ "GLOBAL",
+ "GOTO",
+ "GRANT",
+ "GRAPHIC",
+ "GROUP",
+ "GROUPING",
+ "HANDLER",
+ "HASH",
+ "HASHED_VALUE",
+ "HINT",
+ "HOLD",
+ "HOUR",
+ "HOURS",
+ "IDENTITY",
+ "IF",
+ "IMMEDIATE",
+ "IN",
+ "INCLUDING",
+ "INCLUSIVE",
+ "INCREMENT",
+ "INDEX",
+ "INDICATOR",
+ "INDICATORS",
+ "INF",
+ "INFINITY",
+ "INHERIT",
+ "INNER",
+ "INOUT",
+ "INSENSITIVE",
+ "INSERT",
+ "INT",
+ "INTEGER",
+ "INTEGRITY",
+ "INTERSECTION",
+ "INTERVAL",
+ "INTO",
+ "IS",
+ "ISOBID",
+ "ISOLATION",
+ "ITERATE",
+ "JAR",
+ "JAVA",
+ "KEEP",
+ "KEY",
+ "LABEL",
+ "LANGUAGE",
+ "LARGE",
+ "LATERAL",
+ "LC_CTYPE",
+ "LEADING",
+ "LEAVE",
+ "LEFT",
+ "LIKE",
+ "LINKTYPE",
+ "LN",
+ "LOCAL",
+ "LOCALDATE",
+ "LOCALE",
+ "LOCALTIME",
+ "LOCALTIMESTAMP",
+ "LOCATOR",
+ "LOCATORS",
+ "LOCK",
+ "LOCKMAX",
+ "LOCKSIZE",
+ "LONG",
+ "LOOP",
+ "LOWER",
+ "MAINTAINED",
+ "MATCH",
+ "MATERIALIZED",
+ "MAX",
+ "MAXVALUE",
+ "MEMBER",
+ "MERGE",
+ "METHOD",
+ "MICROSECOND",
+ "MICROSECONDS",
+ "MIN",
+ "MINUTE",
+ "MINUTES",
+ "MINVALUE",
+ "MOD",
+ "MODE",
+ "MODIFIES",
+ "MODULE",
+ "MONTH",
+ "MONTHS",
+ "MULTISET",
+ "NAN",
+ "NATIONAL",
+ "NATURAL",
+ "NCHAR",
+ "NCLOB",
+ "NEW",
+ "NEW_TABLE",
+ "NEXTVAL",
+ "NO",
+ "NOCACHE",
+ "NOCYCLE",
+ "NODENAME",
+ "NODENUMBER",
+ "NOMAXVALUE",
+ "NOMINVALUE",
+ "NONE",
+ "NOORDER",
+ "NORMALIZE",
+ "NORMALIZED",
+ "NOT",
+ "NULL",
+ "NULLIF",
+ "NULLS",
+ "NUMERIC",
+ "NUMPARTS",
+ "OBID",
+ "OCTET_LENGTH",
+ "OF",
+ "OFFSET",
+ "OLD",
+ "OLD_TABLE",
+ "ON",
+ "ONLY",
+ "OPEN",
+ "OPTIMIZATION",
+ "OPTIMIZE",
+ "OPTION",
+ "ORDER",
+ "OUT",
+ "OUTER",
+ "OVER",
+ "OVERLAPS",
+ "OVERLAY",
+ "OVERRIDING",
+ "PACKAGE",
+ "PADDED",
+ "PAGESIZE",
+ "PARAMETER",
+ "PART",
+ "PARTITION",
+ "PARTITIONED",
+ "PARTITIONING",
+ "PARTITIONS",
+ "PASSWORD",
+ "PATH",
+ "PERCENTILE_CONT",
+ "PERCENTILE_DISC",
+ "PERCENT_RANK",
+ "PIECESIZE",
+ "PLAN",
+ "POSITION",
+ "POWER",
+ "PRECISION",
+ "PREPARE",
+ "PREVVAL",
+ "PRIMARY",
+ "PRIQTY",
+ "PRIVILEGES",
+ "PROCEDURE",
+ "PROGRAM",
+ "PSID",
+ "PUBLIC",
+ "QUERY",
+ "QUERYNO",
+ "RANGE",
+ "RANK",
+ "READ",
+ "READS",
+ "REAL",
+ "RECOVERY",
+ "RECURSIVE",
+ "REF",
+ "REFERENCES",
+ "REFERENCING",
+ "REFRESH",
+ "REGR_AVGX",
+ "REGR_AVGY",
+ "REGR_COUNT",
+ "REGR_INTERCEPT",
+ "REGR_R2",
+ "REGR_SLOPE",
+ "REGR_SXX",
+ "REGR_SXY",
+ "REGR_SYY",
+ "RELEASE",
+ "RENAME",
+ "REPEAT",
+ "RESET",
+ "RESIGNAL",
+ "RESTART",
+ "RESTRICT",
+ "RESULT",
+ "RESULT_SET_LOCATOR",
+ "RETURN",
+ "RETURNS",
+ "REVOKE",
+ "RIGHT",
+ "ROLE",
+ "ROLLBACK",
+ "ROLLUP",
+ "ROUND_CEILING",
+ "ROUND_DOWN",
+ "ROUND_FLOOR",
+ "ROUND_HALF_DOWN",
+ "ROUND_HALF_EVEN",
+ "ROUND_HALF_UP",
+ "ROUND_UP",
+ "ROUTINE",
+ "ROW",
+ "ROWNUMBER",
+ "ROWS",
+ "ROWSET",
+ "ROW_NUMBER",
+ "RRN",
+ "RUN",
+ "SAVEPOINT",
+ "SCHEMA",
+ "SCOPE",
+ "SCRATCHPAD",
+ "SCROLL",
+ "SEARCH",
+ "SECOND",
+ "SECONDS",
+ "SECQTY",
+ "SECURITY",
+ "SENSITIVE",
+ "SEQUENCE",
+ "SESSION",
+ "SESSION_USER",
+ "SIGNAL",
+ "SIMILAR",
+ "SIMPLE",
+ "SMALLINT",
+ "SNAN",
+ "SOME",
+ "SOURCE",
+ "SPECIFIC",
+ "SPECIFICTYPE",
+ "SQL",
+ "SQLEXCEPTION",
+ "SQLID",
+ "SQLSTATE",
+ "SQLWARNING",
+ "SQRT",
+ "STACKED",
+ "STANDARD",
+ "START",
+ "STARTING",
+ "STATEMENT",
+ "STATIC",
+ "STATMENT",
+ "STAY",
+ "STDDEV_POP",
+ "STDDEV_SAMP",
+ "STOGROUP",
+ "STORES",
+ "STYLE",
+ "SUBMULTISET",
+ "SUBSTRING",
+ "SUM",
+ "SUMMARY",
+ "SYMMETRIC",
+ "SYNONYM",
+ "SYSFUN",
+ "SYSIBM",
+ "SYSPROC",
+ "SYSTEM",
+ "SYSTEM_USER",
+ "TABLE",
+ "TABLESAMPLE",
+ "TABLESPACE",
+ "THEN",
+ "TIME",
+ "TIMESTAMP",
+ "TIMEZONE_HOUR",
+ "TIMEZONE_MINUTE",
+ "TO",
+ "TRAILING",
+ "TRANSACTION",
+ "TRANSLATE",
+ "TRANSLATION",
+ "TREAT",
+ "TRIGGER",
+ "TRIM",
+ "TRUE",
+ "TRUNCATE",
+ "TYPE",
+ "UESCAPE",
+ "UNDO",
+ "UNIQUE",
+ "UNKNOWN",
+ "UNNEST",
+ "UNTIL",
+ "UPPER",
+ "USAGE",
+ "USER",
+ "USING",
+ "VALIDPROC",
+ "VALUE",
+ "VARCHAR",
+ "VARIABLE",
+ "VARIANT",
+ "VARYING",
+ "VAR_POP",
+ "VAR_SAMP",
+ "VCAT",
+ "VERSION",
+ "VIEW",
+ "VOLATILE",
+ "VOLUMES",
+ "WHEN",
+ "WHENEVER",
+ "WHILE",
+ "WIDTH_BUCKET",
+ "WINDOW",
+ "WITH",
+ "WITHIN",
+ "WITHOUT",
+ "WLM",
+ "WRITE",
+ "XMLELEMENT",
+ "XMLEXISTS",
+ "XMLNAMESPACES",
+ "YEAR",
+ "YEARS",
+ };
+
+ private static readonly string[] ReservedTopLevelWords
+ =
+ {
+ "ADD",
+ "AFTER",
+ "ALTER COLUMN",
+ "ALTER TABLE",
+ "DELETE FROM",
+ "EXCEPT",
+ "FETCH FIRST",
+ "FROM",
+ "GROUP BY",
+ "GO",
+ "HAVING",
+ "INSERT INTO",
+ "INTERSECT",
+ "LIMIT",
+ "ORDER BY",
+ "SELECT",
+ "SET CURRENT SCHEMA",
+ "SET SCHEMA",
+ "SET",
+ "UPDATE",
+ "VALUES",
+ "WHERE",
+ };
+
+ private static readonly string[] ReservedTopLevelWordsNoIndent = new[] { "INTERSECT", "INTERSECT ALL", "MINUS", "UNION", "UNION ALL" };
+
+ private static readonly string[] ReservedNewlineWords
+ =
+ {
+ "AND",
+ "OR",
+ // joins
+ "JOIN",
+ "INNER JOIN",
+ "LEFT JOIN",
+ "LEFT OUTER JOIN",
+ "RIGHT JOIN",
+ "RIGHT OUTER JOIN",
+ "FULL JOIN",
+ "FULL OUTER JOIN",
+ "CROSS JOIN",
+ "NATURAL JOIN"
+ };
+
+ protected override Tokenizer GetTokenizer()
+ {
+ return
+ new Tokenizer(
+ ReservedWords,
+ ReservedTopLevelWords,
+ ReservedNewlineWords,
+ ReservedTopLevelWordsNoIndent,
+ stringTypes: new[] { "\"\"", "''", "``", "[]" },
+ openParens: new[] { "(" },
+ closeParens: new[] { ")" },
+ indexedPlaceholderTypes: new[] { '?' },
+ namedPlaceholderTypes: new[] { ':' },
+ lineCommentTypes: new[] { "--" },
+ specialWordChars: new[] { "#", "@" },
+ operators: new[] { "**", "!=", "!>", "!>", "||" });
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/MariaDbFormatter.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/MariaDbFormatter.cs
new file mode 100644
index 000000000..b1b4520d4
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/MariaDbFormatter.cs
@@ -0,0 +1,325 @@
+using DevToys.Tools.Helpers.SqlFormatter.Core;
+
+namespace DevToys.Tools.Helpers.SqlFormatter.Languages;
+
+internal sealed class MariaDbFormatter : Formatter
+{
+ private static readonly string[] ReservedWords
+ =
+ {
+ "ACCESSIBLE",
+ "ADD",
+ "ALL",
+ "ALTER",
+ "ANALYZE",
+ "AND",
+ "AS",
+ "ASC",
+ "ASENSITIVE",
+ "BEFORE",
+ "BETWEEN",
+ "BIGINT",
+ "BINARY",
+ "BLOB",
+ "BOTH",
+ "BY",
+ "CALL",
+ "CASCADE",
+ "CASE",
+ "CHANGE",
+ "CHAR",
+ "CHARACTER",
+ "CHECK",
+ "COLLATE",
+ "COLUMN",
+ "CONDITION",
+ "CONSTRAINT",
+ "CONTINUE",
+ "CONVERT",
+ "CREATE",
+ "CROSS",
+ "CURRENT_DATE",
+ "CURRENT_ROLE",
+ "CURRENT_TIME",
+ "CURRENT_TIMESTAMP",
+ "CURRENT_USER",
+ "CURSOR",
+ "DATABASE",
+ "DATABASES",
+ "DAY_HOUR",
+ "DAY_MICROSECOND",
+ "DAY_MINUTE",
+ "DAY_SECOND",
+ "DEC",
+ "DECIMAL",
+ "DECLARE",
+ "DEFAULT",
+ "DELAYED",
+ "DELETE",
+ "DESC",
+ "DESCRIBE",
+ "DETERMINISTIC",
+ "DISTINCT",
+ "DISTINCTROW",
+ "DIV",
+ "DO_DOMAIN_IDS",
+ "DOUBLE",
+ "DROP",
+ "DUAL",
+ "EACH",
+ "ELSE",
+ "ELSEIF",
+ "ENCLOSED",
+ "ESCAPED",
+ "EXCEPT",
+ "EXISTS",
+ "EXIT",
+ "EXPLAIN",
+ "FALSE",
+ "FETCH",
+ "FLOAT",
+ "FLOAT4",
+ "FLOAT8",
+ "FOR",
+ "FORCE",
+ "FOREIGN",
+ "FROM",
+ "FULLTEXT",
+ "GENERAL",
+ "GRANT",
+ "GROUP",
+ "HAVING",
+ "HIGH_PRIORITY",
+ "HOUR_MICROSECOND",
+ "HOUR_MINUTE",
+ "HOUR_SECOND",
+ "IF",
+ "IGNORE",
+ "IGNORE_DOMAIN_IDS",
+ "IGNORE_SERVER_IDS",
+ "IN",
+ "INDEX",
+ "INFILE",
+ "INNER",
+ "INOUT",
+ "INSENSITIVE",
+ "INSERT",
+ "INT",
+ "INT1",
+ "INT2",
+ "INT3",
+ "INT4",
+ "INT8",
+ "INTEGER",
+ "INTERSECT",
+ "INTERVAL",
+ "INTO",
+ "IS",
+ "ITERATE",
+ "JOIN",
+ "KEY",
+ "KEYS",
+ "KILL",
+ "LEADING",
+ "LEAVE",
+ "LEFT",
+ "LIKE",
+ "LIMIT",
+ "LINEAR",
+ "LINES",
+ "LOAD",
+ "LOCALTIME",
+ "LOCALTIMESTAMP",
+ "LOCK",
+ "LONG",
+ "LONGBLOB",
+ "LONGTEXT",
+ "LOOP",
+ "LOW_PRIORITY",
+ "MASTER_HEARTBEAT_PERIOD",
+ "MASTER_SSL_VERIFY_SERVER_CERT",
+ "MATCH",
+ "MAXVALUE",
+ "MEDIUMBLOB",
+ "MEDIUMINT",
+ "MEDIUMTEXT",
+ "MIDDLEINT",
+ "MINUTE_MICROSECOND",
+ "MINUTE_SECOND",
+ "MOD",
+ "MODIFIES",
+ "NATURAL",
+ "NOT",
+ "NO_WRITE_TO_BINLOG",
+ "NULL",
+ "NUMERIC",
+ "ON",
+ "OPTIMIZE",
+ "OPTION",
+ "OPTIONALLY",
+ "OR",
+ "ORDER",
+ "OUT",
+ "OUTER",
+ "OUTFILE",
+ "OVER",
+ "PAGE_CHECKSUM",
+ "PARSE_VCOL_EXPR",
+ "PARTITION",
+ "POSITION",
+ "PRECISION",
+ "PRIMARY",
+ "PROCEDURE",
+ "PURGE",
+ "RANGE",
+ "READ",
+ "READS",
+ "READ_WRITE",
+ "REAL",
+ "RECURSIVE",
+ "REF_SYSTEM_ID",
+ "REFERENCES",
+ "REGEXP",
+ "RELEASE",
+ "RENAME",
+ "REPEAT",
+ "REPLACE",
+ "REQUIRE",
+ "RESIGNAL",
+ "RESTRICT",
+ "RETURN",
+ "RETURNING",
+ "REVOKE",
+ "RIGHT",
+ "RLIKE",
+ "ROWS",
+ "SCHEMA",
+ "SCHEMAS",
+ "SECOND_MICROSECOND",
+ "SELECT",
+ "SENSITIVE",
+ "SEPARATOR",
+ "SET",
+ "SHOW",
+ "SIGNAL",
+ "SLOW",
+ "SMALLINT",
+ "SPATIAL",
+ "SPECIFIC",
+ "SQL",
+ "SQLEXCEPTION",
+ "SQLSTATE",
+ "SQLWARNING",
+ "SQL_BIG_RESULT",
+ "SQL_CALC_FOUND_ROWS",
+ "SQL_SMALL_RESULT",
+ "SSL",
+ "STARTING",
+ "STATS_AUTO_RECALC",
+ "STATS_PERSISTENT",
+ "STATS_SAMPLE_PAGES",
+ "STRAIGHT_JOIN",
+ "TABLE",
+ "TERMINATED",
+ "THEN",
+ "TINYBLOB",
+ "TINYINT",
+ "TINYTEXT",
+ "TO",
+ "TRAILING",
+ "TRIGGER",
+ "TRUE",
+ "UNDO",
+ "UNION",
+ "UNIQUE",
+ "UNLOCK",
+ "UNSIGNED",
+ "UPDATE",
+ "USAGE",
+ "USE",
+ "USING",
+ "UTC_DATE",
+ "UTC_TIME",
+ "UTC_TIMESTAMP",
+ "VALUES",
+ "VARBINARY",
+ "VARCHAR",
+ "VARCHARACTER",
+ "VARYING",
+ "WHEN",
+ "WHERE",
+ "WHILE",
+ "WINDOW",
+ "WITH",
+ "WRITE",
+ "XOR",
+ "YEAR_MONTH",
+ "ZEROFILL",
+ };
+
+ private static readonly string[] ReservedTopLevelWords
+ =
+ {
+ "ADD",
+ "ALTER COLUMN",
+ "ALTER TABLE",
+ "DELETE FROM",
+ "EXCEPT",
+ "FROM",
+ "GROUP BY",
+ "HAVING",
+ "INSERT INTO",
+ "INSERT",
+ "LIMIT",
+ "ORDER BY",
+ "SELECT",
+ "SET",
+ "UPDATE",
+ "VALUES",
+ "WHERE",
+ };
+
+ private static readonly string[] ReservedTopLevelWordsNoIndent = new[] { "INTERSECT", "INTERSECT ALL", "UNION", "UNION ALL" };
+
+ private static readonly string[] ReservedNewlineWords
+ =
+ {
+ "AND",
+ "ELSE",
+ "OR",
+ "WHEN",
+ // joins
+ "JOIN",
+ "INNER JOIN",
+ "LEFT JOIN",
+ "LEFT OUTER JOIN",
+ "RIGHT JOIN",
+ "RIGHT OUTER JOIN",
+ "CROSS JOIN",
+ "NATURAL JOIN",
+ // non-standard joins
+ "STRAIGHT_JOIN",
+ "NATURAL LEFT JOIN",
+ "NATURAL LEFT OUTER JOIN",
+ "NATURAL RIGHT JOIN",
+ "NATURAL RIGHT OUTER JOIN"
+ };
+
+ protected override Tokenizer GetTokenizer()
+ {
+ return
+ new Tokenizer(
+ ReservedWords,
+ ReservedTopLevelWords,
+ ReservedNewlineWords,
+ ReservedTopLevelWordsNoIndent,
+ stringTypes: new[] { "\"\"", "''", "``" },
+ openParens: new[] { "(", "CASE" },
+ closeParens: new[] { ")", "END" },
+ indexedPlaceholderTypes: new[] { '?' },
+ namedPlaceholderTypes: Array.Empty<char>(),
+ lineCommentTypes: new[] { "#", "--" },
+ specialWordChars: new[] { "@" },
+ operators: new[] { ":=", "<<", ">>", "!=", "<>", "<=>", "&&", "||" });
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/MySqlFormatter.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/MySqlFormatter.cs
new file mode 100644
index 000000000..f4b827bc4
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/MySqlFormatter.cs
@@ -0,0 +1,338 @@
+using DevToys.Tools.Helpers.SqlFormatter.Core;
+
+namespace DevToys.Tools.Helpers.SqlFormatter.Languages;
+
+internal sealed class MySqlFormatter : Formatter
+{
+ private static readonly string[] ReservedWords
+ =
+ {
+ "ACCESSIBLE",
+ "ADD",
+ "ALL",
+ "ALTER",
+ "ANALYZE",
+ "AND",
+ "AS",
+ "ASC",
+ "ASENSITIVE",
+ "BEFORE",
+ "BETWEEN",
+ "BIGINT",
+ "BINARY",
+ "BLOB",
+ "BOTH",
+ "BY",
+ "CALL",
+ "CASCADE",
+ "CASE",
+ "CHANGE",
+ "CHAR",
+ "CHARACTER",
+ "CHECK",
+ "COLLATE",
+ "COLUMN",
+ "CONDITION",
+ "CONSTRAINT",
+ "CONTINUE",
+ "CONVERT",
+ "CREATE",
+ "CROSS",
+ "CUBE",
+ "CUME_DIST",
+ "CURRENT_DATE",
+ "CURRENT_TIME",
+ "CURRENT_TIMESTAMP",
+ "CURRENT_USER",
+ "CURSOR",
+ "DATABASE",
+ "DATABASES",
+ "DAY_HOUR",
+ "DAY_MICROSECOND",
+ "DAY_MINUTE",
+ "DAY_SECOND",
+ "DEC",
+ "DECIMAL",
+ "DECLARE",
+ "DEFAULT",
+ "DELAYED",
+ "DELETE",
+ "DENSE_RANK",
+ "DESC",
+ "DESCRIBE",
+ "DETERMINISTIC",
+ "DISTINCT",
+ "DISTINCTROW",
+ "DIV",
+ "DOUBLE",
+ "DROP",
+ "DUAL",
+ "EACH",
+ "ELSE",
+ "ELSEIF",
+ "EMPTY",
+ "ENCLOSED",
+ "ESCAPED",
+ "EXCEPT",
+ "EXISTS",
+ "EXIT",
+ "EXPLAIN",
+ "FALSE",
+ "FETCH",
+ "FIRST_VALUE",
+ "FLOAT",
+ "FLOAT4",
+ "FLOAT8",
+ "FOR",
+ "FORCE",
+ "FOREIGN",
+ "FROM",
+ "FULLTEXT",
+ "FUNCTION",
+ "GENERATED",
+ "GET",
+ "GRANT",
+ "GROUP",
+ "GROUPING",
+ "GROUPS",
+ "HAVING",
+ "HIGH_PRIORITY",
+ "HOUR_MICROSECOND",
+ "HOUR_MINUTE",
+ "HOUR_SECOND",
+ "IF",
+ "IGNORE",
+ "IN",
+ "INDEX",
+ "INFILE",
+ "INNER",
+ "INOUT",
+ "INSENSITIVE",
+ "INSERT",
+ "INT",
+ "INT1",
+ "INT2",
+ "INT3",
+ "INT4",
+ "INT8",
+ "INTEGER",
+ "INTERVAL",
+ "INTO",
+ "IO_AFTER_GTIDS",
+ "IO_BEFORE_GTIDS",
+ "IS",
+ "ITERATE",
+ "JOIN",
+ "JSON_TABLE",
+ "KEY",
+ "KEYS",
+ "KILL",
+ "LAG",
+ "LAST_VALUE",
+ "LATERAL",
+ "LEAD",
+ "LEADING",
+ "LEAVE",
+ "LEFT",
+ "LIKE",
+ "LIMIT",
+ "LINEAR",
+ "LINES",
+ "LOAD",
+ "LOCALTIME",
+ "LOCALTIMESTAMP",
+ "LOCK",
+ "LONG",
+ "LONGBLOB",
+ "LONGTEXT",
+ "LOOP",
+ "LOW_PRIORITY",
+ "MASTER_BIND",
+ "MASTER_SSL_VERIFY_SERVER_CERT",
+ "MATCH",
+ "MAXVALUE",
+ "MEDIUMBLOB",
+ "MEDIUMINT",
+ "MEDIUMTEXT",
+ "MIDDLEINT",
+ "MINUTE_MICROSECOND",
+ "MINUTE_SECOND",
+ "MOD",
+ "MODIFIES",
+ "NATURAL",
+ "NOT",
+ "NO_WRITE_TO_BINLOG",
+ "NTH_VALUE",
+ "NTILE",
+ "NULL",
+ "NUMERIC",
+ "OF",
+ "ON",
+ "OPTIMIZE",
+ "OPTIMIZER_COSTS",
+ "OPTION",
+ "OPTIONALLY",
+ "OR",
+ "ORDER",
+ "OUT",
+ "OUTER",
+ "OUTFILE",
+ "OVER",
+ "PARTITION",
+ "PERCENT_RANK",
+ "PRECISION",
+ "PRIMARY",
+ "PROCEDURE",
+ "PURGE",
+ "RANGE",
+ "RANK",
+ "READ",
+ "READS",
+ "READ_WRITE",
+ "REAL",
+ "RECURSIVE",
+ "REFERENCES",
+ "REGEXP",
+ "RELEASE",
+ "RENAME",
+ "REPEAT",
+ "REPLACE",
+ "REQUIRE",
+ "RESIGNAL",
+ "RESTRICT",
+ "RETURN",
+ "REVOKE",
+ "RIGHT",
+ "RLIKE",
+ "ROW",
+ "ROWS",
+ "ROW_NUMBER",
+ "SCHEMA",
+ "SCHEMAS",
+ "SECOND_MICROSECOND",
+ "SELECT",
+ "SENSITIVE",
+ "SEPARATOR",
+ "SET",
+ "SHOW",
+ "SIGNAL",
+ "SMALLINT",
+ "SPATIAL",
+ "SPECIFIC",
+ "SQL",
+ "SQLEXCEPTION",
+ "SQLSTATE",
+ "SQLWARNING",
+ "SQL_BIG_RESULT",
+ "SQL_CALC_FOUND_ROWS",
+ "SQL_SMALL_RESULT",
+ "SSL",
+ "STARTING",
+ "STORED",
+ "STRAIGHT_JOIN",
+ "SYSTEM",
+ "TABLE",
+ "TERMINATED",
+ "THEN",
+ "TINYBLOB",
+ "TINYINT",
+ "TINYTEXT",
+ "TO",
+ "TRAILING",
+ "TRIGGER",
+ "TRUE",
+ "UNDO",
+ "UNION",
+ "UNIQUE",
+ "UNLOCK",
+ "UNSIGNED",
+ "UPDATE",
+ "USAGE",
+ "USE",
+ "USING",
+ "UTC_DATE",
+ "UTC_TIME",
+ "UTC_TIMESTAMP",
+ "VALUES",
+ "VARBINARY",
+ "VARCHAR",
+ "VARCHARACTER",
+ "VARYING",
+ "VIRTUAL",
+ "WHEN",
+ "WHERE",
+ "WHILE",
+ "WINDOW",
+ "WITH",
+ "WRITE",
+ "XOR",
+ "YEAR_MONTH",
+ "ZEROFILL",
+ };
+
+ private static readonly string[] ReservedTopLevelWords
+ =
+ {
+ "ADD",
+ "ALTER COLUMN",
+ "ALTER TABLE",
+ "DELETE FROM",
+ "EXCEPT",
+ "FROM",
+ "GROUP BY",
+ "HAVING",
+ "INSERT INTO",
+ "INSERT",
+ "LIMIT",
+ "ORDER BY",
+ "SELECT",
+ "SET",
+ "UPDATE",
+ "VALUES",
+ "WHERE",
+ };
+
+ private static readonly string[] ReservedTopLevelWordsNoIndent = new[] { "INTERSECT", "INTERSECT ALL", "UNION", "UNION ALL" };
+
+ private static readonly string[] ReservedNewlineWords
+ =
+ {
+ "AND",
+ "ELSE",
+ "OR",
+ "WHEN",
+ // joins
+ "JOIN",
+ "INNER JOIN",
+ "LEFT JOIN",
+ "LEFT OUTER JOIN",
+ "RIGHT JOIN",
+ "RIGHT OUTER JOIN",
+ "CROSS JOIN",
+ "NATURAL JOIN",
+ // non-standard joins
+ "STRAIGHT_JOIN",
+ "NATURAL LEFT JOIN",
+ "NATURAL LEFT OUTER JOIN",
+ "NATURAL RIGHT JOIN",
+ "NATURAL RIGHT OUTER JOIN",
+ };
+
+ protected override Tokenizer GetTokenizer()
+ {
+ return
+ new Tokenizer(
+ ReservedWords,
+ ReservedTopLevelWords,
+ ReservedNewlineWords,
+ ReservedTopLevelWordsNoIndent,
+ stringTypes: new[] { "\"\"", "''", "``" },
+ openParens: new[] { "(", "CASE" },
+ closeParens: new[] { ")", "END" },
+ indexedPlaceholderTypes: new[] { '?' },
+ namedPlaceholderTypes: Array.Empty<char>(),
+ lineCommentTypes: new[] { "#", "--" },
+ specialWordChars: new[] { "@" },
+ operators: new[] { ":=", "<<", ">>", "!=", "<>", "<=>", "&&", "||", "->", "->>" });
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/N1qlFormatter.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/N1qlFormatter.cs
new file mode 100644
index 000000000..5845414ef
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/N1qlFormatter.cs
@@ -0,0 +1,249 @@
+using DevToys.Tools.Helpers.SqlFormatter.Core;
+
+namespace DevToys.Tools.Helpers.SqlFormatter.Languages;
+
+// For reference: http://docs.couchbase.com.s3-website-us-west-1.amazonaws.com/server/6.0/n1ql/n1ql-language-reference/index.html
+internal sealed class N1qlFormatter : Formatter
+{
+ private static readonly string[] ReservedWords
+ =
+ {
+ "ALL",
+ "ALTER",
+ "ANALYZE",
+ "AND",
+ "ANY",
+ "ARRAY",
+ "AS",
+ "ASC",
+ "BEGIN",
+ "BETWEEN",
+ "BINARY",
+ "BOOLEAN",
+ "BREAK",
+ "BUCKET",
+ "BUILD",
+ "BY",
+ "CALL",
+ "CASE",
+ "CAST",
+ "CLUSTER",
+ "COLLATE",
+ "COLLECTION",
+ "COMMIT",
+ "CONNECT",
+ "CONTINUE",
+ "CORRELATE",
+ "COVER",
+ "CREATE",
+ "DATABASE",
+ "DATASET",
+ "DATASTORE",
+ "DECLARE",
+ "DECREMENT",
+ "DELETE",
+ "DERIVED",
+ "DESC",
+ "DESCRIBE",
+ "DISTINCT",
+ "DO",
+ "DROP",
+ "EACH",
+ "ELEMENT",
+ "ELSE",
+ "END",
+ "EVERY",
+ "EXCEPT",
+ "EXCLUDE",
+ "EXECUTE",
+ "EXISTS",
+ "EXPLAIN",
+ "FALSE",
+ "FETCH",
+ "FIRST",
+ "FLATTEN",
+ "FOR",
+ "FORCE",
+ "FROM",
+ "FUNCTION",
+ "GRANT",
+ "GROUP",
+ "GSI",
+ "HAVING",
+ "IF",
+ "IGNORE",
+ "ILIKE",
+ "IN",
+ "INCLUDE",
+ "INCREMENT",
+ "INDEX",
+ "INFER",
+ "INLINE",
+ "INNER",
+ "INSERT",
+ "INTERSECT",
+ "INTO",
+ "IS",
+ "JOIN",
+ "KEY",
+ "KEYS",
+ "KEYSPACE",
+ "KNOWN",
+ "LAST",
+ "LEFT",
+ "LET",
+ "LETTING",
+ "LIKE",
+ "LIMIT",
+ "LSM",
+ "MAP",
+ "MAPPING",
+ "MATCHED",
+ "MATERIALIZED",
+ "MERGE",
+ "MISSING",
+ "NAMESPACE",
+ "NEST",
+ "NOT",
+ "NULL",
+ "NUMBER",
+ "OBJECT",
+ "OFFSET",
+ "ON",
+ "OPTION",
+ "OR",
+ "ORDER",
+ "OUTER",
+ "OVER",
+ "PARSE",
+ "PARTITION",
+ "PASSWORD",
+ "PATH",
+ "POOL",
+ "PREPARE",
+ "PRIMARY",
+ "PRIVATE",
+ "PRIVILEGE",
+ "PROCEDURE",
+ "PUBLIC",
+ "RAW",
+ "REALM",
+ "REDUCE",
+ "RENAME",
+ "RETURN",
+ "RETURNING",
+ "REVOKE",
+ "RIGHT",
+ "ROLE",
+ "ROLLBACK",
+ "SATISFIES",
+ "SCHEMA",
+ "SELECT",
+ "SELF",
+ "SEMI",
+ "SET",
+ "SHOW",
+ "SOME",
+ "START",
+ "STATISTICS",
+ "STRING",
+ "SYSTEM",
+ "THEN",
+ "TO",
+ "TRANSACTION",
+ "TRIGGER",
+ "TRUE",
+ "TRUNCATE",
+ "UNDER",
+ "UNION",
+ "UNIQUE",
+ "UNKNOWN",
+ "UNNEST",
+ "UNSET",
+ "UPDATE",
+ "UPSERT",
+ "USE",
+ "USER",
+ "USING",
+ "VALIDATE",
+ "VALUE",
+ "VALUED",
+ "VALUES",
+ "VIA",
+ "VIEW",
+ "WHEN",
+ "WHERE",
+ "WHILE",
+ "WITH",
+ "WITHIN",
+ "WORK",
+ "XOR",
+ };
+
+ private static readonly string[] ReservedTopLevelWords
+ =
+ {
+ "DELETE FROM",
+ "EXCEPT ALL",
+ "EXCEPT",
+ "EXPLAIN DELETE FROM",
+ "EXPLAIN UPDATE",
+ "EXPLAIN UPSERT",
+ "FROM",
+ "GROUP BY",
+ "HAVING",
+ "INFER",
+ "INSERT INTO",
+ "LET",
+ "LIMIT",
+ "MERGE",
+ "NEST",
+ "ORDER BY",
+ "PREPARE",
+ "SELECT",
+ "SET CURRENT SCHEMA",
+ "SET SCHEMA",
+ "SET",
+ "UNNEST",
+ "UPDATE",
+ "UPSERT",
+ "USE KEYS",
+ "VALUES",
+ "WHERE",
+ };
+
+ private static readonly string[] ReservedTopLevelWordsNoIndent = new[] { "INTERSECT", "INTERSECT ALL", "MINUS", "UNION", "UNION ALL" };
+
+ private static readonly string[] ReservedNewlineWords
+ =
+ {
+ "AND",
+ "OR",
+ "XOR",
+ // joins
+ "JOIN",
+ "INNER JOIN",
+ "LEFT JOIN",
+ "LEFT OUTER JOIN",
+ "RIGHT JOIN",
+ "RIGHT OUTER JOIN",
+ };
+
+ protected override Tokenizer GetTokenizer()
+ {
+ return
+ new Tokenizer(
+ ReservedWords,
+ ReservedTopLevelWords,
+ ReservedNewlineWords,
+ ReservedTopLevelWordsNoIndent,
+ stringTypes: new[] { "\"\"", "''", "``" },
+ openParens: new[] { "(", "[", "{" },
+ closeParens: new[] { ")", "]", "}" },
+ indexedPlaceholderTypes: Array.Empty<char>(),
+ namedPlaceholderTypes: new[] { '$' },
+ lineCommentTypes: new[] { "#", "--" },
+ specialWordChars: Array.Empty<string>(),
+ operators: new[] { "==", "!=" });
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/PlSqlFormatter.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/PlSqlFormatter.cs
new file mode 100644
index 000000000..c32e2df25
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/PlSqlFormatter.cs
@@ -0,0 +1,454 @@
+using DevToys.Tools.Helpers.SqlFormatter.Core;
+
+namespace DevToys.Tools.Helpers.SqlFormatter.Languages;
+
+internal sealed class PlSqlFormatter : Formatter
+{
+ private static readonly string[] ReservedWords
+ =
+ {
+ "A",
+ "ACCESSIBLE",
+ "AGENT",
+ "AGGREGATE",
+ "ALL",
+ "ALTER",
+ "ANY",
+ "ARRAY",
+ "AS",
+ "ASC",
+ "AT",
+ "ATTRIBUTE",
+ "AUTHID",
+ "AVG",
+ "BETWEEN",
+ "BFILE_BASE",
+ "BINARY_INTEGER",
+ "BINARY",
+ "BLOB_BASE",
+ "BLOCK",
+ "BODY",
+ "BOOLEAN",
+ "BOTH",
+ "BOUND",
+ "BREADTH",
+ "BULK",
+ "BY",
+ "BYTE",
+ "C",
+ "CALL",
+ "CALLING",
+ "CASCADE",
+ "CASE",
+ "CHAR_BASE",
+ "CHAR",
+ "CHARACTER",
+ "CHARSET",
+ "CHARSETFORM",
+ "CHARSETID",
+ "CHECK",
+ "CLOB_BASE",
+ "CLONE",
+ "CLOSE",
+ "CLUSTER",
+ "CLUSTERS",
+ "COALESCE",
+ "COLAUTH",
+ "COLLECT",
+ "COLUMNS",
+ "COMMENT",
+ "COMMIT",
+ "COMMITTED",
+ "COMPILED",
+ "COMPRESS",
+ "CONNECT",
+ "CONSTANT",
+ "CONSTRUCTOR",
+ "CONTEXT",
+ "CONTINUE",
+ "CONVERT",
+ "COUNT",
+ "CRASH",
+ "CREATE",
+ "CREDENTIAL",
+ "CURRENT",
+ "CURRVAL",
+ "CURSOR",
+ "CUSTOMDATUM",
+ "DANGLING",
+ "DATA",
+ "DATE_BASE",
+ "DATE",
+ "DAY",
+ "DECIMAL",
+ "DEFAULT",
+ "DEFINE",
+ "DELETE",
+ "DEPTH",
+ "DESC",
+ "DETERMINISTIC",
+ "DIRECTORY",
+ "DISTINCT",
+ "DO",
+ "DOUBLE",
+ "DROP",
+ "DURATION",
+ "ELEMENT",
+ "ELSIF",
+ "EMPTY",
+ "END",
+ "ESCAPE",
+ "EXCEPTIONS",
+ "EXCLUSIVE",
+ "EXECUTE",
+ "EXISTS",
+ "EXIT",
+ "EXTENDS",
+ "EXTERNAL",
+ "EXTRACT",
+ "FALSE",
+ "FETCH",
+ "FINAL",
+ "FIRST",
+ "FIXED",
+ "FLOAT",
+ "FOR",
+ "FORALL",
+ "FORCE",
+ "FROM",
+ "FUNCTION",
+ "GENERAL",
+ "GOTO",
+ "GRANT",
+ "GROUP",
+ "HASH",
+ "HEAP",
+ "HIDDEN",
+ "HOUR",
+ "IDENTIFIED",
+ "IF",
+ "IMMEDIATE",
+ "IN",
+ "INCLUDING",
+ "INDEX",
+ "INDEXES",
+ "INDICATOR",
+ "INDICES",
+ "INFINITE",
+ "INSTANTIABLE",
+ "INT",
+ "INTEGER",
+ "INTERFACE",
+ "INTERVAL",
+ "INTO",
+ "INVALIDATE",
+ "IS",
+ "ISOLATION",
+ "JAVA",
+ "LANGUAGE",
+ "LARGE",
+ "LEADING",
+ "LENGTH",
+ "LEVEL",
+ "LIBRARY",
+ "LIKE",
+ "LIKE2",
+ "LIKE4",
+ "LIKEC",
+ "LIMITED",
+ "LOCAL",
+ "LOCK",
+ "LONG",
+ "MAP",
+ "MAX",
+ "MAXLEN",
+ "MEMBER",
+ "MERGE",
+ "MIN",
+ "MINUTE",
+ "MLSLABEL",
+ "MOD",
+ "MODE",
+ "MONTH",
+ "MULTISET",
+ "NAME",
+ "NAN",
+ "NATIONAL",
+ "NATIVE",
+ "NATURAL",
+ "NATURALN",
+ "NCHAR",
+ "NEW",
+ "NEXTVAL",
+ "NOCOMPRESS",
+ "NOCOPY",
+ "NOT",
+ "NOWAIT",
+ "NULL",
+ "NULLIF",
+ "NUMBER_BASE",
+ "NUMBER",
+ "OBJECT",
+ "OCICOLL",
+ "OCIDATE",
+ "OCIDATETIME",
+ "OCIDURATION",
+ "OCIINTERVAL",
+ "OCILOBLOCATOR",
+ "OCINUMBER",
+ "OCIRAW",
+ "OCIREF",
+ "OCIREFCURSOR",
+ "OCIROWID",
+ "OCISTRING",
+ "OCITYPE",
+ "OF",
+ "OLD",
+ "ON",
+ "ONLY",
+ "OPAQUE",
+ "OPEN",
+ "OPERATOR",
+ "OPTION",
+ "ORACLE",
+ "ORADATA",
+ "ORDER",
+ "ORGANIZATION",
+ "ORLANY",
+ "ORLVARY",
+ "OTHERS",
+ "OUT",
+ "OVERLAPS",
+ "OVERRIDING",
+ "PACKAGE",
+ "PARALLEL_ENABLE",
+ "PARAMETER",
+ "PARAMETERS",
+ "PARENT",
+ "PARTITION",
+ "PASCAL",
+ "PCTFREE",
+ "PIPE",
+ "PIPELINED",
+ "PLS_INTEGER",
+ "PLUGGABLE",
+ "POSITIVE",
+ "POSITIVEN",
+ "PRAGMA",
+ "PRECISION",
+ "PRIOR",
+ "PRIVATE",
+ "PROCEDURE",
+ "PUBLIC",
+ "RAISE",
+ "RANGE",
+ "RAW",
+ "READ",
+ "REAL",
+ "RECORD",
+ "REF",
+ "REFERENCE",
+ "RELEASE",
+ "RELIES_ON",
+ "REM",
+ "REMAINDER",
+ "RENAME",
+ "RESOURCE",
+ "RESULT_CACHE",
+ "RESULT",
+ "RETURN",
+ "RETURNING",
+ "REVERSE",
+ "REVOKE",
+ "ROLLBACK",
+ "ROW",
+ "ROWID",
+ "ROWNUM",
+ "ROWTYPE",
+ "SAMPLE",
+ "SAVE",
+ "SAVEPOINT",
+ "SB1",
+ "SB2",
+ "SB4",
+ "SEARCH",
+ "SECOND",
+ "SEGMENT",
+ "SELF",
+ "SEPARATE",
+ "SEQUENCE",
+ "SERIALIZABLE",
+ "SHARE",
+ "SHORT",
+ "SIZE_T",
+ "SIZE",
+ "SMALLINT",
+ "SOME",
+ "SPACE",
+ "SPARSE",
+ "SQL",
+ "SQLCODE",
+ "SQLDATA",
+ "SQLERRM",
+ "SQLNAME",
+ "SQLSTATE",
+ "STANDARD",
+ "START",
+ "STATIC",
+ "STDDEV",
+ "STORED",
+ "STRING",
+ "STRUCT",
+ "STYLE",
+ "SUBMULTISET",
+ "SUBPARTITION",
+ "SUBSTITUTABLE",
+ "SUBTYPE",
+ "SUCCESSFUL",
+ "SUM",
+ "SYNONYM",
+ "SYSDATE",
+ "TABAUTH",
+ "TABLE",
+ "TDO",
+ "THE",
+ "THEN",
+ "TIME",
+ "TIMESTAMP",
+ "TIMEZONE_ABBR",
+ "TIMEZONE_HOUR",
+ "TIMEZONE_MINUTE",
+ "TIMEZONE_REGION",
+ "TO",
+ "TRAILING",
+ "TRANSACTION",
+ "TRANSACTIONAL",
+ "TRIGGER",
+ "TRUE",
+ "TRUSTED",
+ "TYPE",
+ "UB1",
+ "UB2",
+ "UB4",
+ "UID",
+ "UNDER",
+ "UNIQUE",
+ "UNPLUG",
+ "UNSIGNED",
+ "UNTRUSTED",
+ "USE",
+ "USER",
+ "USING",
+ "VALIDATE",
+ "VALIST",
+ "VALUE",
+ "VARCHAR",
+ "VARCHAR2",
+ "VARIABLE",
+ "VARIANCE",
+ "VARRAY",
+ "VARYING",
+ "VIEW",
+ "VIEWS",
+ "VOID",
+ "WHENEVER",
+ "WHILE",
+ "WITH",
+ "WORK",
+ "WRAPPED",
+ "WRITE",
+ "YEAR",
+ "ZONE",
+ };
+
+ private static readonly string[] ReservedTopLevelWords
+ =
+ {
+ "ADD",
+ "ALTER COLUMN",
+ "ALTER TABLE",
+ "BEGIN",
+ "CONNECT BY",
+ "DECLARE",
+ "DELETE FROM",
+ "DELETE",
+ "END",
+ "EXCEPT",
+ "EXCEPTION",
+ "FETCH FIRST",
+ "FROM",
+ "GROUP BY",
+ "HAVING",
+ "INSERT INTO",
+ "INSERT",
+ "LIMIT",
+ "LOOP",
+ "MODIFY",
+ "ORDER BY",
+ "SELECT",
+ "SET CURRENT SCHEMA",
+ "SET SCHEMA",
+ "SET",
+ "START WITH",
+ "UPDATE",
+ "VALUES",
+ "WHERE",
+ };
+
+ private static readonly string[] ReservedTopLevelWordsNoIndent = new[] { "INTERSECT", "INTERSECT ALL", "MINUS", "UNION", "UNION ALL" };
+
+ private static readonly string[] ReservedNewlineWords
+ =
+ {
+ "AND",
+ "CROSS APPLY",
+ "ELSE",
+ "END",
+ "OR",
+ "OUTER APPLY",
+ "WHEN",
+ "XOR",
+ // joins
+ "JOIN",
+ "INNER JOIN",
+ "LEFT JOIN",
+ "LEFT OUTER JOIN",
+ "RIGHT JOIN",
+ "RIGHT OUTER JOIN",
+ "FULL JOIN",
+ "FULL OUTER JOIN",
+ "CROSS JOIN",
+ "NATURAL JOIN",
+ };
+
+ protected override Tokenizer GetTokenizer()
+ {
+ return
+ new Tokenizer(
+ ReservedWords,
+ ReservedTopLevelWords,
+ ReservedNewlineWords,
+ ReservedTopLevelWordsNoIndent,
+ stringTypes: new[] { "\"\"", "N''", "''", "``" },
+ openParens: new[] { "(", "CASE" },
+ closeParens: new[] { ")", "END" },
+ indexedPlaceholderTypes: new[] { '?' },
+ namedPlaceholderTypes: new[] { ':' },
+ lineCommentTypes: new[] { "--" },
+ specialWordChars: new[] { "_", "$", "#", ".", "@" },
+ operators: new[] { "||", "**", "!=", ":=" });
+ }
+
+ protected override Token TokenOverride(Token token, ReadOnlySpan<char> querySpan)
+ {
+
+ if (token.IsSet(querySpan.Slice(token))
+ && _previousReservedToken != null
+ && _previousReservedToken.Value.IsBy(querySpan.Slice(_previousReservedToken.Value)))
+ {
+ return new Token(token.Index, token.Length, TokenType.Reserved, token.PrecedingWitespaceLength);
+ }
+
+ return base.TokenOverride(token, querySpan);
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/PostgreSqlFormatter.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/PostgreSqlFormatter.cs
new file mode 100644
index 000000000..f5ca01623
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/PostgreSqlFormatter.cs
@@ -0,0 +1,547 @@
+using DevToys.Tools.Helpers.SqlFormatter.Core;
+
+namespace DevToys.Tools.Helpers.SqlFormatter.Languages;
+
+internal sealed class PostgreSqlFormatter : Formatter
+{
+ private static readonly string[] ReservedWords
+ =
+ {
+ "ABORT",
+ "ABSOLUTE",
+ "ACCESS",
+ "ACTION",
+ "ADD",
+ "ADMIN",
+ "AFTER",
+ "AGGREGATE",
+ "ALL",
+ "ALSO",
+ "ALTER",
+ "ALWAYS",
+ "ANALYSE",
+ "ANALYZE",
+ "AND",
+ "ANY",
+ "ARRAY",
+ "AS",
+ "ASC",
+ "ASSERTION",
+ "ASSIGNMENT",
+ "ASYMMETRIC",
+ "AT",
+ "ATTACH",
+ "ATTRIBUTE",
+ "AUTHORIZATION",
+ "BACKWARD",
+ "BEFORE",
+ "BEGIN",
+ "BETWEEN",
+ "BIGINT",
+ "BINARY",
+ "BIT",
+ "BOOLEAN",
+ "BOTH",
+ "BY",
+ "CACHE",
+ "CALL",
+ "CALLED",
+ "CASCADE",
+ "CASCADED",
+ "CASE",
+ "CAST",
+ "CATALOG",
+ "CHAIN",
+ "CHAR",
+ "CHARACTER",
+ "CHARACTERISTICS",
+ "CHECK",
+ "CHECKPOINT",
+ "CLASS",
+ "CLOSE",
+ "CLUSTER",
+ "COALESCE",
+ "COLLATE",
+ "COLLATION",
+ "COLUMN",
+ "COLUMNS",
+ "COMMENT",
+ "COMMENTS",
+ "COMMIT",
+ "COMMITTED",
+ "CONCURRENTLY",
+ "CONFIGURATION",
+ "CONFLICT",
+ "CONNECTION",
+ "CONSTRAINT",
+ "CONSTRAINTS",
+ "CONTENT",
+ "CONTINUE",
+ "CONVERSION",
+ "COPY",
+ "COST",
+ "CREATE",
+ "CROSS",
+ "CSV",
+ "CUBE",
+ "CURRENT",
+ "CURRENT_CATALOG",
+ "CURRENT_DATE",
+ "CURRENT_ROLE",
+ "CURRENT_SCHEMA",
+ "CURRENT_TIME",
+ "CURRENT_TIMESTAMP",
+ "CURRENT_USER",
+ "CURSOR",
+ "CYCLE",
+ "DATA",
+ "DATABASE",
+ "DAY",
+ "DEALLOCATE",
+ "DEC",
+ "DECIMAL",
+ "DECLARE",
+ "DEFAULT",
+ "DEFAULTS",
+ "DEFERRABLE",
+ "DEFERRED",
+ "DEFINER",
+ "DELETE",
+ "DELIMITER",
+ "DELIMITERS",
+ "DEPENDS",
+ "DESC",
+ "DETACH",
+ "DICTIONARY",
+ "DISABLE",
+ "DISCARD",
+ "DISTINCT",
+ "DO",
+ "DOCUMENT",
+ "DOMAIN",
+ "DOUBLE",
+ "DROP",
+ "EACH",
+ "ELSE",
+ "ENABLE",
+ "ENCODING",
+ "ENCRYPTED",
+ "END",
+ "ENUM",
+ "ESCAPE",
+ "EVENT",
+ "EXCEPT",
+ "EXCLUDE",
+ "EXCLUDING",
+ "EXCLUSIVE",
+ "EXECUTE",
+ "EXISTS",
+ "EXPLAIN",
+ "EXPRESSION",
+ "EXTENSION",
+ "EXTERNAL",
+ "EXTRACT",
+ "FALSE",
+ "FAMILY",
+ "FETCH",
+ "FILTER",
+ "FIRST",
+ "FLOAT",
+ "FOLLOWING",
+ "FOR",
+ "FORCE",
+ "FOREIGN",
+ "FORWARD",
+ "FREEZE",
+ "FROM",
+ "FULL",
+ "FUNCTION",
+ "FUNCTIONS",
+ "GENERATED",
+ "GLOBAL",
+ "GRANT",
+ "GRANTED",
+ "GREATEST",
+ "GROUP",
+ "GROUPING",
+ "GROUPS",
+ "HANDLER",
+ "HAVING",
+ "HEADER",
+ "HOLD",
+ "HOUR",
+ "IDENTITY",
+ "IF",
+ "ILIKE",
+ "IMMEDIATE",
+ "IMMUTABLE",
+ "IMPLICIT",
+ "IMPORT",
+ "IN",
+ "INCLUDE",
+ "INCLUDING",
+ "INCREMENT",
+ "INDEX",
+ "INDEXES",
+ "INHERIT",
+ "INHERITS",
+ "INITIALLY",
+ "INLINE",
+ "INNER",
+ "INOUT",
+ "INPUT",
+ "INSENSITIVE",
+ "INSERT",
+ "INSTEAD",
+ "INT",
+ "INTEGER",
+ "INTERSECT",
+ "INTERVAL",
+ "INTO",
+ "INVOKER",
+ "IS",
+ "ISNULL",
+ "ISOLATION",
+ "JOIN",
+ "KEY",
+ "LABEL",
+ "LANGUAGE",
+ "LARGE",
+ "LAST",
+ "LATERAL",
+ "LEADING",
+ "LEAKPROOF",
+ "LEAST",
+ "LEFT",
+ "LEVEL",
+ "LIKE",
+ "LIMIT",
+ "LISTEN",
+ "LOAD",
+ "LOCAL",
+ "LOCALTIME",
+ "LOCALTIMESTAMP",
+ "LOCATION",
+ "LOCK",
+ "LOCKED",
+ "LOGGED",
+ "MAPPING",
+ "MATCH",
+ "MATERIALIZED",
+ "MAXVALUE",
+ "METHOD",
+ "MINUTE",
+ "MINVALUE",
+ "MODE",
+ "MONTH",
+ "MOVE",
+ "NAME",
+ "NAMES",
+ "NATIONAL",
+ "NATURAL",
+ "NCHAR",
+ "NEW",
+ "NEXT",
+ "NFC",
+ "NFD",
+ "NFKC",
+ "NFKD",
+ "NO",
+ "NONE",
+ "NORMALIZE",
+ "NORMALIZED",
+ "NOT",
+ "NOTHING",
+ "NOTIFY",
+ "NOTNULL",
+ "NOWAIT",
+ "NULL",
+ "NULLIF",
+ "NULLS",
+ "NUMERIC",
+ "OBJECT",
+ "OF",
+ "OFF",
+ "OFFSET",
+ "OIDS",
+ "OLD",
+ "ON",
+ "ONLY",
+ "OPERATOR",
+ "OPTION",
+ "OPTIONS",
+ "OR",
+ "ORDER",
+ "ORDINALITY",
+ "OTHERS",
+ "OUT",
+ "OUTER",
+ "OVER",
+ "OVERLAPS",
+ "OVERLAY",
+ "OVERRIDING",
+ "OWNED",
+ "OWNER",
+ "PARALLEL",
+ "PARSER",
+ "PARTIAL",
+ "PARTITION",
+ "PASSING",
+ "PASSWORD",
+ "PLACING",
+ "PLANS",
+ "POLICY",
+ "POSITION",
+ "PRECEDING",
+ "PRECISION",
+ "PREPARE",
+ "PREPARED",
+ "PRESERVE",
+ "PRIMARY",
+ "PRIOR",
+ "PRIVILEGES",
+ "PROCEDURAL",
+ "PROCEDURE",
+ "PROCEDURES",
+ "PROGRAM",
+ "PUBLICATION",
+ "QUOTE",
+ "RANGE",
+ "READ",
+ "REAL",
+ "REASSIGN",
+ "RECHECK",
+ "RECURSIVE",
+ "REF",
+ "REFERENCES",
+ "REFERENCING",
+ "REFRESH",
+ "REINDEX",
+ "RELATIVE",
+ "RELEASE",
+ "RENAME",
+ "REPEATABLE",
+ "REPLACE",
+ "REPLICA",
+ "RESET",
+ "RESTART",
+ "RESTRICT",
+ "RETURNING",
+ "RETURNS",
+ "REVOKE",
+ "RIGHT",
+ "ROLE",
+ "ROLLBACK",
+ "ROLLUP",
+ "ROUTINE",
+ "ROUTINES",
+ "ROW",
+ "ROWS",
+ "RULE",
+ "SAVEPOINT",
+ "SCHEMA",
+ "SCHEMAS",
+ "SCROLL",
+ "SEARCH",
+ "SECOND",
+ "SECURITY",
+ "SELECT",
+ "SEQUENCE",
+ "SEQUENCES",
+ "SERIALIZABLE",
+ "SERVER",
+ "SESSION",
+ "SESSION_USER",
+ "SET",
+ "SETOF",
+ "SETS",
+ "SHARE",
+ "SHOW",
+ "SIMILAR",
+ "SIMPLE",
+ "SKIP",
+ "SMALLINT",
+ "SNAPSHOT",
+ "SOME",
+ "SQL",
+ "STABLE",
+ "STANDALONE",
+ "START",
+ "STATEMENT",
+ "STATISTICS",
+ "STDIN",
+ "STDOUT",
+ "STORAGE",
+ "STORED",
+ "STRICT",
+ "STRIP",
+ "SUBSCRIPTION",
+ "SUBSTRING",
+ "SUPPORT",
+ "SYMMETRIC",
+ "SYSID",
+ "SYSTEM",
+ "TABLE",
+ "TABLES",
+ "TABLESAMPLE",
+ "TABLESPACE",
+ "TEMP",
+ "TEMPLATE",
+ "TEMPORARY",
+ "TEXT",
+ "THEN",
+ "TIES",
+ "TIME",
+ "TIMESTAMP",
+ "TO",
+ "TRAILING",
+ "TRANSACTION",
+ "TRANSFORM",
+ "TREAT",
+ "TRIGGER",
+ "TRIM",
+ "TRUE",
+ "TRUNCATE",
+ "TRUSTED",
+ "TYPE",
+ "TYPES",
+ "UESCAPE",
+ "UNBOUNDED",
+ "UNCOMMITTED",
+ "UNENCRYPTED",
+ "UNION",
+ "UNIQUE",
+ "UNKNOWN",
+ "UNLISTEN",
+ "UNLOGGED",
+ "UNTIL",
+ "UPDATE",
+ "USER",
+ "USING",
+ "VACUUM",
+ "VALID",
+ "VALIDATE",
+ "VALIDATOR",
+ "VALUE",
+ "VALUES",
+ "VARCHAR",
+ "VARIADIC",
+ "VARYING",
+ "VERBOSE",
+ "VERSION",
+ "VIEW",
+ "VIEWS",
+ "VOLATILE",
+ "WHEN",
+ "WHERE",
+ "WHITESPACE",
+ "WINDOW",
+ "WITH",
+ "WITHIN",
+ "WITHOUT",
+ "WORK",
+ "WRAPPER",
+ "WRITE",
+ "XML",
+ "XMLATTRIBUTES",
+ "XMLCONCAT",
+ "XMLELEMENT",
+ "XMLEXISTS",
+ "XMLFOREST",
+ "XMLNAMESPACES",
+ "XMLPARSE",
+ "XMLPI",
+ "XMLROOT",
+ "XMLSERIALIZE",
+ "XMLTABLE",
+ "YEAR",
+ "YES",
+ "ZONE",
+ };
+
+ private static readonly string[] ReservedTopLevelWords
+ =
+ {
+ "ADD",
+ "AFTER",
+ "ALTER COLUMN",
+ "ALTER TABLE",
+ "CASE",
+ "DELETE FROM",
+ "END",
+ "EXCEPT",
+ "FETCH FIRST",
+ "FROM",
+ "GROUP BY",
+ "HAVING",
+ "INSERT INTO",
+ "INSERT",
+ "LIMIT",
+ "ORDER BY",
+ "SELECT",
+ "SET CURRENT SCHEMA",
+ "SET SCHEMA",
+ "SET",
+ "UPDATE",
+ "VALUES",
+ "WHERE",
+ };
+
+ private static readonly string[] ReservedTopLevelWordsNoIndent = new[] { "INTERSECT", "INTERSECT ALL", "UNION", "UNION ALL" };
+
+ private static readonly string[] ReservedNewlineWords
+ =
+ {
+ "AND",
+ "ELSE",
+ "OR",
+ "WHEN",
+ // joins
+ "JOIN",
+ "INNER JOIN",
+ "LEFT JOIN",
+ "LEFT OUTER JOIN",
+ "RIGHT JOIN",
+ "RIGHT OUTER JOIN",
+ "FULL JOIN",
+ "FULL OUTER JOIN",
+ "CROSS JOIN",
+ "NATURAL JOIN",
+ };
+
+ protected override Tokenizer GetTokenizer()
+ {
+ return
+ new Tokenizer(
+ ReservedWords,
+ ReservedTopLevelWords,
+ ReservedNewlineWords,
+ ReservedTopLevelWordsNoIndent,
+ stringTypes: new[] { "\"\"", "''", "U&''", "U&\"\"", "$$" },
+ openParens: new[] { "(", "CASE" },
+ closeParens: new[] { ")", "END" },
+ indexedPlaceholderTypes: new[] { '$' },
+ namedPlaceholderTypes: new[] { ':' },
+ lineCommentTypes: new[] { "--" },
+ specialWordChars: Array.Empty<string>(),
+ operators: new[]
+ {
+ "!=",
+ "<<",
+ ">>",
+ "||/",
+ "|/",
+ "::",
+ "->>",
+ "->",
+ "~~*",
+ "~~",
+ "!~~*",
+ "!~~",
+ "~*",
+ "!~*",
+ "!~",
+ "!!"
+ });
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/RedshiftFormatter.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/RedshiftFormatter.cs
new file mode 100644
index 000000000..a13647447
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/RedshiftFormatter.cs
@@ -0,0 +1,402 @@
+using DevToys.Tools.Helpers.SqlFormatter.Core;
+
+namespace DevToys.Tools.Helpers.SqlFormatter.Languages;
+
+internal sealed class RedshiftFormatter : Formatter
+{
+ private static readonly string[] ReservedWords
+ =
+ {
+ "AES128",
+ "AES256",
+ "ALLOWOVERWRITE",
+ "ANALYSE",
+ "ARRAY",
+ "AS",
+ "ASC",
+ "AUTHORIZATION",
+ "BACKUP",
+ "BINARY",
+ "BLANKSASNULL",
+ "BOTH",
+ "BYTEDICT",
+ "BZIP2",
+ "CAST",
+ "CHECK",
+ "COLLATE",
+ "COLUMN",
+ "CONSTRAINT",
+ "CREATE",
+ "CREDENTIALS",
+ "CURRENT_DATE",
+ "CURRENT_TIME",
+ "CURRENT_TIMESTAMP",
+ "CURRENT_USER",
+ "CURRENT_USER_ID",
+ "DEFAULT",
+ "DEFERRABLE",
+ "DEFLATE",
+ "DEFRAG",
+ "DELTA",
+ "DELTA32K",
+ "DESC",
+ "DISABLE",
+ "DISTINCT",
+ "DO",
+ "ELSE",
+ "EMPTYASNULL",
+ "ENABLE",
+ "ENCODE",
+ "ENCRYPT",
+ "ENCRYPTION",
+ "END",
+ "EXPLICIT",
+ "FALSE",
+ "FOR",
+ "FOREIGN",
+ "FREEZE",
+ "FULL",
+ "GLOBALDICT256",
+ "GLOBALDICT64K",
+ "GRANT",
+ "GZIP",
+ "IDENTITY",
+ "IGNORE",
+ "ILIKE",
+ "INITIALLY",
+ "INTO",
+ "LEADING",
+ "LOCALTIME",
+ "LOCALTIMESTAMP",
+ "LUN",
+ "LUNS",
+ "LZO",
+ "LZOP",
+ "MINUS",
+ "MOSTLY13",
+ "MOSTLY32",
+ "MOSTLY8",
+ "NATURAL",
+ "NEW",
+ "NULLS",
+ "OFF",
+ "OFFLINE",
+ "OFFSET",
+ "OLD",
+ "ON",
+ "ONLY",
+ "OPEN",
+ "ORDER",
+ "OVERLAPS",
+ "PARALLEL",
+ "PARTITION",
+ "PERCENT",
+ "PERMISSIONS",
+ "PLACING",
+ "PRIMARY",
+ "RAW",
+ "READRATIO",
+ "RECOVER",
+ "REFERENCES",
+ "REJECTLOG",
+ "RESORT",
+ "RESTORE",
+ "SESSION_USER",
+ "SIMILAR",
+ "SYSDATE",
+ "SYSTEM",
+ "TABLE",
+ "TAG",
+ "TDES",
+ "TEXT255",
+ "TEXT32K",
+ "THEN",
+ "TIMESTAMP",
+ "TO",
+ "TOP",
+ "TRAILING",
+ "TRUE",
+ "TRUNCATECOLUMNS",
+ "UNIQUE",
+ "USER",
+ "USING",
+ "VERBOSE",
+ "WALLET",
+ "WHEN",
+ "WITH",
+ "WITHOUT",
+ "PREDICATE",
+ "COLUMNS",
+ "COMPROWS",
+ "COMPRESSION",
+ "COPY",
+ "FORMAT",
+ "DELIMITER",
+ "FIXEDWIDTH",
+ "AVRO",
+ "JSON",
+ "ENCRYPTED",
+ "BZIP2",
+ "GZIP",
+ "LZOP",
+ "PARQUET",
+ "ORC",
+ "ACCEPTANYDATE",
+ "ACCEPTINVCHARS",
+ "BLANKSASNULL",
+ "DATEFORMAT",
+ "EMPTYASNULL",
+ "ENCODING",
+ "ESCAPE",
+ "EXPLICIT_IDS",
+ "FILLRECORD",
+ "IGNOREBLANKLINES",
+ "IGNOREHEADER",
+ "NULL AS",
+ "REMOVEQUOTES",
+ "ROUNDEC",
+ "TIMEFORMAT",
+ "TRIMBLANKS",
+ "TRUNCATECOLUMNS",
+ "COMPROWS",
+ "COMPUPDATE",
+ "MAXERROR",
+ "NOLOAD",
+ "STATUPDATE",
+ "MANIFEST",
+ "REGION",
+ "IAM_ROLE",
+ "MASTER_SYMMETRIC_KEY",
+ "SSH",
+ "ACCEPTANYDATE",
+ "ACCEPTINVCHARS",
+ "ACCESS_KEY_ID",
+ "SECRET_ACCESS_KEY",
+ "AVRO",
+ "BLANKSASNULL",
+ "BZIP2",
+ "COMPROWS",
+ "COMPUPDATE",
+ "CREDENTIALS",
+ "DATEFORMAT",
+ "DELIMITER",
+ "EMPTYASNULL",
+ "ENCODING",
+ "ENCRYPTED",
+ "ESCAPE",
+ "EXPLICIT_IDS",
+ "FILLRECORD",
+ "FIXEDWIDTH",
+ "FORMAT",
+ "IAM_ROLE",
+ "GZIP",
+ "IGNOREBLANKLINES",
+ "IGNOREHEADER",
+ "JSON",
+ "LZOP",
+ "MANIFEST",
+ "MASTER_SYMMETRIC_KEY",
+ "MAXERROR",
+ "NOLOAD",
+ "NULL AS",
+ "READRATIO",
+ "REGION",
+ "REMOVEQUOTES",
+ "ROUNDEC",
+ "SSH",
+ "STATUPDATE",
+ "TIMEFORMAT",
+ "SESSION_TOKEN",
+ "TRIMBLANKS",
+ "TRUNCATECOLUMNS",
+ "EXTERNAL",
+ "DATA CATALOG",
+ "HIVE METASTORE",
+ "CATALOG_ROLE",
+ "VACUUM",
+ "COPY",
+ "UNLOAD",
+ "EVEN",
+ "ALL",
+ };
+
+ private static readonly string[] ReservedTopLevelWords
+ =
+ {
+ "ADD",
+ "AFTER",
+ "ALTER COLUMN",
+ "ALTER TABLE",
+ "DELETE FROM",
+ "EXCEPT",
+ "FROM",
+ "GROUP BY",
+ "HAVING",
+ "INSERT INTO",
+ "INSERT",
+ "INTERSECT",
+ "TOP",
+ "LIMIT",
+ "MODIFY",
+ "ORDER BY",
+ "SELECT",
+ "SET CURRENT SCHEMA",
+ "SET SCHEMA",
+ "SET",
+ "UNION ALL",
+ "UNION",
+ "UPDATE",
+ "VALUES",
+ "WHERE",
+ "VACUUM",
+ "COPY",
+ "UNLOAD",
+ "ANALYZE",
+ "ANALYSE",
+ "DISTKEY",
+ "SORTKEY",
+ "COMPOUND",
+ "INTERLEAVED",
+ "FORMAT",
+ "DELIMITER",
+ "FIXEDWIDTH",
+ "AVRO",
+ "JSON",
+ "ENCRYPTED",
+ "BZIP2",
+ "GZIP",
+ "LZOP",
+ "PARQUET",
+ "ORC",
+ "ACCEPTANYDATE",
+ "ACCEPTINVCHARS",
+ "BLANKSASNULL",
+ "DATEFORMAT",
+ "EMPTYASNULL",
+ "ENCODING",
+ "ESCAPE",
+ "EXPLICIT_IDS",
+ "FILLRECORD",
+ "IGNOREBLANKLINES",
+ "IGNOREHEADER",
+ "NULL AS",
+ "REMOVEQUOTES",
+ "ROUNDEC",
+ "TIMEFORMAT",
+ "TRIMBLANKS",
+ "TRUNCATECOLUMNS",
+ "COMPROWS",
+ "COMPUPDATE",
+ "MAXERROR",
+ "NOLOAD",
+ "STATUPDATE",
+ "MANIFEST",
+ "REGION",
+ "IAM_ROLE",
+ "MASTER_SYMMETRIC_KEY",
+ "SSH",
+ "ACCEPTANYDATE",
+ "ACCEPTINVCHARS",
+ "ACCESS_KEY_ID",
+ "SECRET_ACCESS_KEY",
+ "AVRO",
+ "BLANKSASNULL",
+ "BZIP2",
+ "COMPROWS",
+ "COMPUPDATE",
+ "CREDENTIALS",
+ "DATEFORMAT",
+ "DELIMITER",
+ "EMPTYASNULL",
+ "ENCODING",
+ "ENCRYPTED",
+ "ESCAPE",
+ "EXPLICIT_IDS",
+ "FILLRECORD",
+ "FIXEDWIDTH",
+ "FORMAT",
+ "IAM_ROLE",
+ "GZIP",
+ "IGNOREBLANKLINES",
+ "IGNOREHEADER",
+ "JSON",
+ "LZOP",
+ "MANIFEST",
+ "MASTER_SYMMETRIC_KEY",
+ "MAXERROR",
+ "NOLOAD",
+ "NULL AS",
+ "READRATIO",
+ "REGION",
+ "REMOVEQUOTES",
+ "ROUNDEC",
+ "SSH",
+ "STATUPDATE",
+ "TIMEFORMAT",
+ "SESSION_TOKEN",
+ "TRIMBLANKS",
+ "TRUNCATECOLUMNS",
+ "EXTERNAL",
+ "DATA CATALOG",
+ "HIVE METASTORE",
+ "CATALOG_ROLE",
+ };
+
+ private static readonly string[] ReservedTopLevelWordsNoIndent = Array.Empty<string>();
+
+ private static readonly string[] ReservedNewlineWords
+ =
+ {
+ "AND",
+ "ELSE",
+ "OR",
+ "OUTER APPLY",
+ "WHEN",
+ "VACUUM",
+ "COPY",
+ "UNLOAD",
+ "ANALYZE",
+ "ANALYSE",
+ "DISTKEY",
+ "SORTKEY",
+ "COMPOUND",
+ "INTERLEAVED",
+ // joins
+ "JOIN",
+ "INNER JOIN",
+ "LEFT JOIN",
+ "LEFT OUTER JOIN",
+ "RIGHT JOIN",
+ "RIGHT OUTER JOIN",
+ "FULL JOIN",
+ "FULL OUTER JOIN",
+ "CROSS JOIN",
+ "NATURAL JOIN",
+ };
+
+ protected override Tokenizer GetTokenizer()
+ {
+ return
+ new Tokenizer(
+ ReservedWords,
+ ReservedTopLevelWords,
+ ReservedNewlineWords,
+ ReservedTopLevelWordsNoIndent,
+ stringTypes: new[] { "\"\"", "''", "``" },
+ openParens: new[] { "(" },
+ closeParens: new[] { ")" },
+ indexedPlaceholderTypes: new[] { '?' },
+ namedPlaceholderTypes: new[] { '@', '#', '$' },
+ lineCommentTypes: new[] { "--" },
+ specialWordChars: Array.Empty<string>(),
+ operators: new[]
+ {
+ "|/",
+ "||/",
+ "<<",
+ ">>",
+ "!=",
+ "||"
+ });
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/SparkSqlFormatter.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/SparkSqlFormatter.cs
new file mode 100644
index 000000000..fe6348af1
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/SparkSqlFormatter.cs
@@ -0,0 +1,296 @@
+using DevToys.Tools.Helpers.SqlFormatter.Core;
+
+namespace DevToys.Tools.Helpers.SqlFormatter.Languages;
+
+internal sealed class SparkSqlFormatter : Formatter
+{
+ private static readonly string[] ReservedWords
+ =
+ {
+ "ALL",
+ "ALTER",
+ "ANALYSE",
+ "ANALYZE",
+ "ARRAY_ZIP",
+ "ARRAY",
+ "AS",
+ "ASC",
+ "AVG",
+ "BETWEEN",
+ "CASCADE",
+ "CASE",
+ "CAST",
+ "COALESCE",
+ "COLLECT_LIST",
+ "COLLECT_SET",
+ "COLUMN",
+ "COLUMNS",
+ "COMMENT",
+ "CONSTRAINT",
+ "CONTAINS",
+ "CONVERT",
+ "COUNT",
+ "CUME_DIST",
+ "CURRENT ROW",
+ "CURRENT_DATE",
+ "CURRENT_TIMESTAMP",
+ "DATABASE",
+ "DATABASES",
+ "DATE_ADD",
+ "DATE_SUB",
+ "DATE_TRUNC",
+ "DAY_HOUR",
+ "DAY_MINUTE",
+ "DAY_SECOND",
+ "DAY",
+ "DAYS",
+ "DECODE",
+ "DEFAULT",
+ "DELETE",
+ "DENSE_RANK",
+ "DESC",
+ "DESCRIBE",
+ "DISTINCT",
+ "DISTINCTROW",
+ "DIV",
+ "DROP",
+ "ELSE",
+ "ENCODE",
+ "END",
+ "EXISTS",
+ "EXPLAIN",
+ "EXPLODE_OUTER",
+ "EXPLODE",
+ "FILTER",
+ "FIRST_VALUE",
+ "FIRST",
+ "FIXED",
+ "FLATTEN",
+ "FOLLOWING",
+ "FROM_UNIXTIME",
+ "FULL",
+ "GREATEST",
+ "GROUP_CONCAT",
+ "HOUR_MINUTE",
+ "HOUR_SECOND",
+ "HOUR",
+ "HOURS",
+ "IF",
+ "IFNULL",
+ "IN",
+ "INSERT",
+ "INTERVAL",
+ "INTO",
+ "IS",
+ "LAG",
+ "LAST_VALUE",
+ "LAST",
+ "LEAD",
+ "LEADING",
+ "LEAST",
+ "LEVEL",
+ "LIKE",
+ "MAX",
+ "MERGE",
+ "MIN",
+ "MINUTE_SECOND",
+ "MINUTE",
+ "MONTH",
+ "NATURAL",
+ "NOT",
+ "NOW()",
+ "NTILE",
+ "NULL",
+ "NULLIF",
+ "OFFSET",
+ "ON DELETE",
+ "ON UPDATE",
+ "ON",
+ "ONLY",
+ "OPTIMIZE",
+ "OVER",
+ "PERCENT_RANK",
+ "PRECEDING",
+ "RANGE",
+ "RANK",
+ "REGEXP",
+ "RENAME",
+ "RLIKE",
+ "ROW",
+ "ROWS",
+ "SECOND",
+ "SEPARATOR",
+ "SEQUENCE",
+ "SIZE",
+ "STRING",
+ "STRUCT",
+ "SUM",
+ "TABLE",
+ "TABLES",
+ "TEMPORARY",
+ "THEN",
+ "TO_DATE",
+ "TO_JSON",
+ "TO",
+ "TRAILING",
+ "TRANSFORM",
+ "TRUE",
+ "TRUNCATE",
+ "TYPE",
+ "TYPES",
+ "UNBOUNDED",
+ "UNIQUE",
+ "UNIX_TIMESTAMP",
+ "UNLOCK",
+ "UNSIGNED",
+ "USING",
+ "VARIABLES",
+ "VIEW",
+ "WHEN",
+ "WITH",
+ "YEAR_MONTH",
+ };
+
+ private static readonly string[] ReservedTopLevelWords
+ =
+ {
+ "ADD",
+ "AFTER",
+ "ALTER COLUMN",
+ "ALTER DATABASE",
+ "ALTER SCHEMA",
+ "ALTER TABLE",
+ "CLUSTER BY",
+ "CLUSTERED BY",
+ "DELETE FROM",
+ "DISTRIBUTE BY",
+ "FROM",
+ "GROUP BY",
+ "HAVING",
+ "INSERT INTO",
+ "INSERT",
+ "LIMIT",
+ "OPTIONS",
+ "ORDER BY",
+ "PARTITION BY",
+ "PARTITIONED BY",
+ "RANGE",
+ "ROWS",
+ "SELECT",
+ "SET CURRENT SCHEMA",
+ "SET SCHEMA",
+ "SET",
+ "TBLPROPERTIES",
+ "UPDATE",
+ "USING",
+ "VALUES",
+ "WHERE",
+ "WINDOW",
+ };
+
+ private static readonly string[] ReservedTopLevelWordsNoIndent
+ =
+ {
+ "EXCEPT ALL",
+ "EXCEPT",
+ "INTERSECT ALL",
+ "INTERSECT",
+ "UNION ALL",
+ "UNION"
+ };
+
+ private static readonly string[] ReservedNewlineWords
+ =
+ {
+ "AND",
+ "CREATE OR",
+ "CREATE",
+ "ELSE",
+ "LATERAL VIEW",
+ "OR",
+ "OUTER APPLY",
+ "WHEN",
+ "XOR",
+ // joins
+ "JOIN",
+ "INNER JOIN",
+ "LEFT JOIN",
+ "LEFT OUTER JOIN",
+ "RIGHT JOIN",
+ "RIGHT OUTER JOIN",
+ "FULL JOIN",
+ "FULL OUTER JOIN",
+ "CROSS JOIN",
+ "NATURAL JOIN",
+ // non-standard-joins
+ "ANTI JOIN",
+ "SEMI JOIN",
+ "LEFT ANTI JOIN",
+ "LEFT SEMI JOIN",
+ "RIGHT OUTER JOIN",
+ "RIGHT SEMI JOIN",
+ "NATURAL ANTI JOIN",
+ "NATURAL FULL OUTER JOIN",
+ "NATURAL INNER JOIN",
+ "NATURAL LEFT ANTI JOIN",
+ "NATURAL LEFT OUTER JOIN",
+ "NATURAL LEFT SEMI JOIN",
+ "NATURAL OUTER JOIN",
+ "NATURAL RIGHT OUTER JOIN",
+ "NATURAL RIGHT SEMI JOIN",
+ "NATURAL SEMI JOIN",
+ };
+
+ protected override Tokenizer GetTokenizer()
+ {
+ return
+ new Tokenizer(
+ ReservedWords,
+ ReservedTopLevelWords,
+ ReservedNewlineWords,
+ ReservedTopLevelWordsNoIndent,
+ stringTypes: new[] { "\"\"", "''", "``", "{}" },
+ openParens: new[] { "(", "CASE" },
+ closeParens: new[] { ")", "END" },
+ indexedPlaceholderTypes: new[] { '?' },
+ namedPlaceholderTypes: new[] { '$' },
+ lineCommentTypes: new[] { "--" },
+ specialWordChars: Array.Empty<string>(),
+ operators: new[]
+ {
+ "!=",
+ "<=>",
+ "&&",
+ "||",
+ "=="
+ });
+ }
+
+ protected override Token TokenOverride(Token token, ReadOnlySpan<char> querySpan)
+ {
+ // Fix cases where names are ambiguously keywords or functions
+ if (token.IsWindow(querySpan.Slice(token)))
+ {
+ Token? aheadToken = TokenLookAhead();
+ if (aheadToken is { Type: TokenType.OpenParen })
+ // This is a function call, treat it as a reserved word
+ return new Token(token.Index, token.Length, TokenType.Reserved, token.PrecedingWitespaceLength);
+ }
+
+ // Fix cases where names are ambiguously keywords or properties
+ if (token.IsEnd(querySpan.Slice(token)))
+ {
+ Token? backToken = TokenLookBehind();
+ if (backToken is not null
+ && backToken.Value.Type == TokenType.Operator
+ && backToken.Value.Length == 1
+ && querySpan[backToken.Value.Index] == '.')
+ {
+ // This is window().end (or similar) not CASE ... END
+ return new Token(token.Index, token.Length, TokenType.Word, token.PrecedingWitespaceLength);
+ }
+ }
+
+ return token;
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/StandardSqlFormatter.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/StandardSqlFormatter.cs
new file mode 100644
index 000000000..0f482e503
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/StandardSqlFormatter.cs
@@ -0,0 +1,386 @@
+using DevToys.Tools.Helpers.SqlFormatter.Core;
+
+namespace DevToys.Tools.Helpers.SqlFormatter.Languages;
+
+internal sealed class StandardSqlFormatter : Formatter
+{
+ private static readonly string[] ReservedWords
+ =
+ {
+ "ABS",
+ "ALL",
+ "ALLOCATE",
+ "ALTER",
+ "AND",
+ "ANY",
+ "ARE",
+ "ARRAY",
+ "AS",
+ "ASENSITIVE",
+ "ASYMMETRIC",
+ "AT",
+ "ATOMIC",
+ "AUTHORIZATION",
+ "AVG",
+ "BEGIN",
+ "BETWEEN",
+ "BIGINT",
+ "BINARY",
+ "BLOB",
+ "BOOLEAN",
+ "BOTH",
+ "BY",
+ "CALL",
+ "CALLED",
+ "CARDINALITY",
+ "CASCADED",
+ "CASE",
+ "CAST",
+ "CEIL",
+ "CEILING",
+ "CHAR",
+ "CHAR_LENGTH",
+ "CHARACTER",
+ "CHARACTER_LENGTH",
+ "CHECK",
+ "CLOB",
+ "CLOSE",
+ "COALESCE",
+ "COLLATE",
+ "COLLECT",
+ "COLUMN",
+ "COMMIT",
+ "CONDITION",
+ "CONNECT",
+ "CONSTRAINT",
+ "CONVERT",
+ "CORR",
+ "CORRESPONDING",
+ "COUNT",
+ "COVAR_POP",
+ "COVAR_SAMP",
+ "CREATE",
+ "CROSS",
+ "CUBE",
+ "CUME_DIST",
+ "CURRENT",
+ "CURRENT_CATALOG",
+ "CURRENT_DATE",
+ "CURRENT_DEFAULT_TRANSFORM_GROUP",
+ "CURRENT_PATH",
+ "CURRENT_ROLE",
+ "CURRENT_SCHEMA",
+ "CURRENT_TIME",
+ "CURRENT_TIMESTAMP",
+ "CURRENT_TRANSFORM_GROUP_FOR_TYPE",
+ "CURRENT_USER",
+ "CURSOR",
+ "CYCLE",
+ "DATE",
+ "DAY",
+ "DEALLOCATE",
+ "DEC",
+ "DECIMAL",
+ "DECLARE",
+ "DEFAULT",
+ "DELETE",
+ "DENSE_RANK",
+ "DEREF",
+ "DESCRIBE",
+ "DETERMINISTIC",
+ "DISCONNECT",
+ "DISTINCT",
+ "DOUBLE",
+ "DROP",
+ "DYNAMIC",
+ "EACH",
+ "ELEMENT",
+ "ELSE",
+ "END",
+ "END-EXEC",
+ "ESCAPE",
+ "EVERY",
+ "EXCEPT",
+ "EXEC",
+ "EXECUTE",
+ "EXISTS",
+ "EXP",
+ "EXTERNAL",
+ "EXTRACT",
+ "FALSE",
+ "FETCH",
+ "FILTER",
+ "FLOAT",
+ "FLOOR",
+ "FOR",
+ "FOREIGN",
+ "FREE",
+ "FROM",
+ "FULL",
+ "FUNCTION",
+ "FUSION",
+ "GET",
+ "GLOBAL",
+ "GRANT",
+ "GROUP",
+ "GROUPING",
+ "HAVING",
+ "HOLD",
+ "HOUR",
+ "IDENTITY",
+ "IN",
+ "INDICATOR",
+ "INNER",
+ "INOUT",
+ "INSENSITIVE",
+ "INSERT",
+ "INT",
+ "INTEGER",
+ "INTERSECT",
+ "INTERSECTION",
+ "INTERVAL",
+ "INTO",
+ "IS",
+ "JOIN",
+ "LANGUAGE",
+ "LARGE",
+ "LATERAL",
+ "LEADING",
+ "LEFT",
+ "LIKE",
+ "LIKE_REGEX",
+ "LN",
+ "LOCAL",
+ "LOCALTIME",
+ "LOCALTIMESTAMP",
+ "LOWER",
+ "MATCH",
+ "MAX",
+ "MEMBER",
+ "MERGE",
+ "METHOD",
+ "MIN",
+ "MINUTE",
+ "MOD",
+ "MODIFIES",
+ "MODULE",
+ "MONTH",
+ "MULTISET",
+ "NATIONAL",
+ "NATURAL",
+ "NCHAR",
+ "NCLOB",
+ "NEW",
+ "NO",
+ "NONE",
+ "NORMALIZE",
+ "NOT",
+ "NULL",
+ "NULLIF",
+ "NUMERIC",
+ "OCTET_LENGTH",
+ "OCCURRENCES_REGEX",
+ "OF",
+ "OLD",
+ "ON",
+ "ONLY",
+ "OPEN",
+ "OR",
+ "ORDER",
+ "OUT",
+ "OUTER",
+ "OVER",
+ "OVERLAPS",
+ "OVERLAY",
+ "PARAMETER",
+ "PARTITION",
+ "PERCENT_RANK",
+ "PERCENTILE_CONT",
+ "PERCENTILE_DISC",
+ "POSITION",
+ "POSITION_REGEX",
+ "POWER",
+ "PRECISION",
+ "PREPARE",
+ "PRIMARY",
+ "PROCEDURE",
+ "RANGE",
+ "RANK",
+ "READS",
+ "REAL",
+ "RECURSIVE",
+ "REF",
+ "REFERENCES",
+ "REFERENCING",
+ "REGR_AVGX",
+ "REGR_AVGY",
+ "REGR_COUNT",
+ "REGR_INTERCEPT",
+ "REGR_R2",
+ "REGR_SLOPE",
+ "REGR_SXX",
+ "REGR_SXY",
+ "REGR_SYY",
+ "RELEASE",
+ "RESULT",
+ "RETURN",
+ "RETURNS",
+ "REVOKE",
+ "RIGHT",
+ "ROLLBACK",
+ "ROLLUP",
+ "ROW",
+ "ROW_NUMBER",
+ "ROWS",
+ "SAVEPOINT",
+ "SCOPE",
+ "SCROLL",
+ "SEARCH",
+ "SECOND",
+ "SELECT",
+ "SENSITIVE",
+ "SESSION_USER",
+ "SET",
+ "SIMILAR",
+ "SMALLINT",
+ "SOME",
+ "SPECIFIC",
+ "SPECIFICTYPE",
+ "SQL",
+ "SQLEXCEPTION",
+ "SQLSTATE",
+ "SQLWARNING",
+ "SQRT",
+ "START",
+ "STATIC",
+ "STDDEV_POP",
+ "STDDEV_SAMP",
+ "SUBMULTISET",
+ "SUBSTRING",
+ "SUBSTRING_REGEX",
+ "SUM",
+ "SYMMETRIC",
+ "SYSTEM",
+ "SYSTEM_USER",
+ "TABLE",
+ "TABLESAMPLE",
+ "THEN",
+ "TIME",
+ "TIMESTAMP",
+ "TIMEZONE_HOUR",
+ "TIMEZONE_MINUTE",
+ "TO",
+ "TRAILING",
+ "TRANSLATE",
+ "TRANSLATE_REGEX",
+ "TRANSLATION",
+ "TREAT",
+ "TRIGGER",
+ "TRIM",
+ "TRUE",
+ "UESCAPE",
+ "UNION",
+ "UNIQUE",
+ "UNKNOWN",
+ "UNNEST",
+ "UPDATE",
+ "UPPER",
+ "USER",
+ "USING",
+ "VALUE",
+ "VALUES",
+ "VAR_POP",
+ "VAR_SAMP",
+ "VARBINARY",
+ "VARCHAR",
+ "VARYING",
+ "WHEN",
+ "WHENEVER",
+ "WHERE",
+ "WIDTH_BUCKET",
+ "WINDOW",
+ "WITH",
+ "WITHIN",
+ "WITHOUT",
+ "YEAR",
+ };
+
+ private static readonly string[] ReservedTopLevelWords
+ =
+ {
+ "ADD",
+ "ALTER COLUMN",
+ "ALTER TABLE",
+ "CASE",
+ "DELETE FROM",
+ "END",
+ "FETCH FIRST",
+ "FETCH NEXT",
+ "FETCH PRIOR",
+ "FETCH LAST",
+ "FETCH ABSOLUTE",
+ "FETCH RELATIVE",
+ "FROM",
+ "GROUP BY",
+ "HAVING",
+ "INSERT INTO",
+ "LIMIT",
+ "ORDER BY",
+ "SELECT",
+ "SET SCHEMA",
+ "SET",
+ "UPDATE",
+ "VALUES",
+ "WHERE",
+ };
+
+ private static readonly string[] ReservedTopLevelWordsNoIndent
+ =
+ {
+ "INTERSECT",
+ "INTERSECT ALL",
+ "INTERSECT DISTINCT",
+ "UNION",
+ "UNION ALL",
+ "UNION DISTINCT",
+ "EXCEPT",
+ "EXCEPT ALL",
+ "EXCEPT DISTINCT",
+ };
+
+ private static readonly string[] ReservedNewlineWords
+ =
+ {
+ "AND",
+ "ELSE",
+ "OR",
+ "WHEN",
+ "JOIN",
+ "INNER JOIN",
+ "LEFT JOIN",
+ "LEFT OUTER JOIN",
+ "RIGHT JOIN",
+ "RIGHT OUTER JOIN",
+ "FULL JOIN",
+ "FULL OUTER JOIN",
+ "CROSS JOIN",
+ "NATURAL JOIN",
+ };
+
+ protected override Tokenizer GetTokenizer()
+ {
+ return
+ new Tokenizer(
+ ReservedWords,
+ ReservedTopLevelWords,
+ ReservedNewlineWords,
+ ReservedTopLevelWordsNoIndent,
+ stringTypes: new[] { "\"\"", "N''", "''", "``", "[]" },
+ openParens: new[] { "(", "CASE" },
+ closeParens: new[] { ")", "END" },
+ indexedPlaceholderTypes: new[] { '?' },
+ namedPlaceholderTypes: Array.Empty<char>(),
+ lineCommentTypes: new[] { "--" },
+ specialWordChars: Array.Empty<string>());
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/TSqlFormatter.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/TSqlFormatter.cs
new file mode 100644
index 000000000..af3553d09
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/Languages/TSqlFormatter.cs
@@ -0,0 +1,286 @@
+using DevToys.Tools.Helpers.SqlFormatter.Core;
+
+namespace DevToys.Tools.Helpers.SqlFormatter.Languages;
+
+internal sealed class TSqlFormatter : Formatter
+{
+ private static readonly string[] ReservedWords
+ =
+ {
+ "ADD",
+ "EXTERNAL",
+ "PROCEDURE",
+ "ALL",
+ "FETCH",
+ "PUBLIC",
+ "ALTER",
+ "FILE",
+ "RAISERROR",
+ "AND",
+ "FILLFACTOR",
+ "READ",
+ "ANY",
+ "FOR",
+ "READTEXT",
+ "AS",
+ "FOREIGN",
+ "RECONFIGURE",
+ "ASC",
+ "FREETEXT",
+ "REFERENCES",
+ "AUTHORIZATION",
+ "FREETEXTTABLE",
+ "REPLICATION",
+ "BACKUP",
+ "FROM",
+ "RESTORE",
+ "BEGIN",
+ "FULL",
+ "RESTRICT",
+ "BETWEEN",
+ "FUNCTION",
+ "RETURN",
+ "BREAK",
+ "GOTO",
+ "REVERT",
+ "BROWSE",
+ "GRANT",
+ "REVOKE",
+ "BULK",
+ "GROUP",
+ "RIGHT",
+ "BY",
+ "HAVING",
+ "ROLLBACK",
+ "CASCADE",
+ "HOLDLOCK",
+ "ROWCOUNT",
+ "CASE",
+ "IDENTITY",
+ "ROWGUIDCOL",
+ "CHECK",
+ "IDENTITY_INSERT",
+ "RULE",
+ "CHECKPOINT",
+ "IDENTITYCOL",
+ "SAVE",
+ "CLOSE",
+ "IF",
+ "SCHEMA",
+ "CLUSTERED",
+ "IN",
+ "SECURITYAUDIT",
+ "COALESCE",
+ "INDEX",
+ "SELECT",
+ "COLLATE",
+ "INNER",
+ "SEMANTICKEYPHRASETABLE",
+ "COLUMN",
+ "INSERT",
+ "SEMANTICSIMILARITYDETAILSTABLE",
+ "COMMIT",
+ "INTERSECT",
+ "SEMANTICSIMILARITYTABLE",
+ "COMPUTE",
+ "INTO",
+ "SESSION_USER",
+ "CONSTRAINT",
+ "IS",
+ "SET",
+ "CONTAINS",
+ "JOIN",
+ "SETUSER",
+ "CONTAINSTABLE",
+ "KEY",
+ "SHUTDOWN",
+ "CONTINUE",
+ "KILL",
+ "SOME",
+ "CONVERT",
+ "LEFT",
+ "STATISTICS",
+ "CREATE",
+ "LIKE",
+ "SYSTEM_USER",
+ "CROSS",
+ "LINENO",
+ "TABLE",
+ "CURRENT",
+ "LOAD",
+ "TABLESAMPLE",
+ "CURRENT_DATE",
+ "MERGE",
+ "TEXTSIZE",
+ "CURRENT_TIME",
+ "NATIONAL",
+ "THEN",
+ "CURRENT_TIMESTAMP",
+ "NOCHECK",
+ "TO",
+ "CURRENT_USER",
+ "NONCLUSTERED",
+ "TOP",
+ "CURSOR",
+ "NOT",
+ "TRAN",
+ "DATABASE",
+ "NULL",
+ "TRANSACTION",
+ "DBCC",
+ "NULLIF",
+ "TRIGGER",
+ "DEALLOCATE",
+ "OF",
+ "TRUNCATE",
+ "DECLARE",
+ "OFF",
+ "TRY_CONVERT",
+ "DEFAULT",
+ "OFFSETS",
+ "TSEQUAL",
+ "DELETE",
+ "ON",
+ "UNION",
+ "DENY",
+ "OPEN",
+ "UNIQUE",
+ "DESC",
+ "OPENDATASOURCE",
+ "UNPIVOT",
+ "DISK",
+ "OPENQUERY",
+ "UPDATE",
+ "DISTINCT",
+ "OPENROWSET",
+ "UPDATETEXT",
+ "DISTRIBUTED",
+ "OPENXML",
+ "USE",
+ "DOUBLE",
+ "OPTION",
+ "USER",
+ "DROP",
+ "OR",
+ "VALUES",
+ "DUMP",
+ "ORDER",
+ "VARYING",
+ "ELSE",
+ "OUTER",
+ "VIEW",
+ "END",
+ "OVER",
+ "WAITFOR",
+ "ERRLVL",
+ "PERCENT",
+ "WHEN",
+ "ESCAPE",
+ "PIVOT",
+ "WHERE",
+ "EXCEPT",
+ "PLAN",
+ "WHILE",
+ "EXEC",
+ "PRECISION",
+ "WITH",
+ "EXECUTE",
+ "PRIMARY",
+ "WITHIN GROUP",
+ "EXISTS",
+ "PRINT",
+ "WRITETEXT",
+ "EXIT",
+ "PROC",
+ };
+
+ private static readonly string[] ReservedTopLevelWords
+ =
+ {
+ "ADD",
+ "ALTER COLUMN",
+ "ALTER TABLE",
+ "CASE",
+ "DELETE FROM",
+ "END",
+ "EXCEPT",
+ "FROM",
+ "GROUP BY",
+ "HAVING",
+ "INSERT INTO",
+ "INSERT",
+ "LIMIT",
+ "ORDER BY",
+ "SELECT",
+ "SET CURRENT SCHEMA",
+ "SET SCHEMA",
+ "SET",
+ "UPDATE",
+ "VALUES",
+ "WHERE",
+ };
+
+ private static readonly string[] ReservedTopLevelWordsNoIndent
+ =
+ {
+ "INTERSECT",
+ "INTERSECT ALL",
+ "MINUS",
+ "UNION",
+ "UNION ALL"
+ };
+
+ private static readonly string[] ReservedNewlineWords
+ =
+ {
+ "AND",
+ "ELSE",
+ "OR",
+ "WHEN",
+ "JOIN",
+ "INNER JOIN",
+ "LEFT JOIN",
+ "LEFT OUTER JOIN",
+ "RIGHT JOIN",
+ "RIGHT OUTER JOIN",
+ "FULL JOIN",
+ "FULL OUTER JOIN",
+ "CROSS JOIN",
+ "NATURAL JOIN",
+ };
+
+ protected override Tokenizer GetTokenizer()
+ {
+ return
+ new Tokenizer(
+ ReservedWords,
+ ReservedTopLevelWords,
+ ReservedNewlineWords,
+ ReservedTopLevelWordsNoIndent,
+ stringTypes: new[] { "\"\"", "N''", "''", "``", "[]" },
+ openParens: new[] { "(", "CASE" },
+ closeParens: new[] { ")", "END" },
+ indexedPlaceholderTypes: Array.Empty<char>(),
+ namedPlaceholderTypes: new[] { '@' },
+ lineCommentTypes: new[] { "--" },
+ specialWordChars: new[] { "#", "@" },
+ operators: new[]
+ {
+ ">=",
+ "<=",
+ "<>",
+ "!=",
+ "!<",
+ "!>",
+ "+=",
+ "-=",
+ "*=",
+ "/=",
+ "%=",
+ "|=",
+ "&=",
+ "^=",
+ "::"
+ });
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/SqlFormatterHelper.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/SqlFormatterHelper.cs
new file mode 100644
index 000000000..69879f7f1
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/SqlFormatterHelper.cs
@@ -0,0 +1,57 @@
+using System.Text;
+using DevToys.Tools.Helpers.SqlFormatter.Core;
+using DevToys.Tools.Helpers.SqlFormatter.Languages;
+
+namespace DevToys.Tools.Helpers.SqlFormatter;
+
+internal static class SqlFormatterHelper
+{
+ internal static string Format(string sql, SqlLanguage language, SqlFormatterOptions options)
+ {
+ if (string.IsNullOrEmpty(sql))
+ {
+ return string.Empty;
+
+ }
+
+ Formatter formatter = language switch
+ {
+ SqlLanguage.Sql => new StandardSqlFormatter(),
+ SqlLanguage.Tsql => new TSqlFormatter(),
+ SqlLanguage.Spark => new SparkSqlFormatter(),
+ SqlLanguage.RedShift => new RedshiftFormatter(),
+ SqlLanguage.PostgreSql => new PostgreSqlFormatter(),
+ SqlLanguage.PlSql => new PlSqlFormatter(),
+ SqlLanguage.N1ql => new N1qlFormatter(),
+ SqlLanguage.MySql => new MySqlFormatter(),
+ SqlLanguage.MariaDb => new MariaDbFormatter(),
+ SqlLanguage.Db2 => new Db2Formatter(),
+ _ => throw new NotSupportedException(),
+ };
+
+ return formatter.Format(sql, options);
+ }
+
+ internal static ReadOnlySpan<char> Slice(this ReadOnlySpan<char> span, Token token)
+ {
+ return span.Slice(token.Index, token.Length);
+ }
+
+ internal static void TrimSpaceEnd(this StringBuilder sb)
+ {
+ if (sb is null)
+ return;
+
+ int lastIndex = sb.Length - 1;
+ int i = lastIndex;
+
+ for (; i >= 0; i--)
+ {
+ if (sb[i] != ' ')
+ break;
+ }
+ int newLen = sb.Length - (lastIndex - i);
+
+ sb.Length = newLen < 0 ? 0 : newLen;
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/SqlFormatterOptions.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/SqlFormatterOptions.cs
new file mode 100644
index 000000000..86423e8c3
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/SqlFormatterOptions.cs
@@ -0,0 +1,24 @@
+namespace DevToys.Tools.Helpers.SqlFormatter;
+
+internal readonly struct SqlFormatterOptions
+{
+ public Models.Indentation Indentation { get; }
+
+ public bool Uppercase { get; }
+
+ public int LinesBetweenQueries { get; }
+
+ public IReadOnlyDictionary<string, string>? PlaceholderParameters { get; }
+
+ public SqlFormatterOptions(
+ Models.Indentation indentation,
+ bool uppercase,
+ int linesBetweenQueries = 1,
+ IReadOnlyDictionary<string, string>? placeholderParameters = null)
+ {
+ Indentation = indentation;
+ Uppercase = uppercase;
+ LinesBetweenQueries = linesBetweenQueries;
+ PlaceholderParameters = placeholderParameters;
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/SqlLanguage.cs b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/SqlLanguage.cs
new file mode 100644
index 000000000..3c41e8793
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Helpers/SqlFormatter/SqlLanguage.cs
@@ -0,0 +1,15 @@
+namespace DevToys.Tools.Helpers.SqlFormatter;
+
+public enum SqlLanguage
+{
+ Sql,
+ Tsql,
+ Spark,
+ RedShift,
+ PostgreSql,
+ PlSql,
+ N1ql,
+ MySql,
+ MariaDb,
+ Db2
+}
diff --git a/src/app/dev/DevToys.Tools/Tools/Formatters/Json/JsonFormatter.Designer.cs b/src/app/dev/DevToys.Tools/Tools/Formatters/Json/JsonFormatter.Designer.cs
index d4c7fa1f1..99bb284bc 100644
--- a/src/app/dev/DevToys.Tools/Tools/Formatters/Json/JsonFormatter.Designer.cs
+++ b/src/app/dev/DevToys.Tools/Tools/Formatters/Json/JsonFormatter.Designer.cs
@@ -205,7 +205,7 @@ namespace DevToys.Tools.Tools.Formatters.Json {
}
/// <summary>
- /// Looks up a localized string similar to Sort Properties.
+ /// Looks up a localized string similar to Sort JSON Properties alphabetically.
/// </summary>
internal static string SortProperties {
get {
diff --git a/src/app/dev/DevToys.Tools/Tools/Formatters/Json/JsonFormatter.resx b/src/app/dev/DevToys.Tools/Tools/Formatters/Json/JsonFormatter.resx
index 78d5e3400..12b1097c6 100644
--- a/src/app/dev/DevToys.Tools/Tools/Formatters/Json/JsonFormatter.resx
+++ b/src/app/dev/DevToys.Tools/Tools/Formatters/Json/JsonFormatter.resx
@@ -166,7 +166,7 @@
<value>JSON</value>
</data>
<data name="SortProperties" xml:space="preserve">
- <value>Sort Properties</value>
+ <value>Sort JSON Properties alphabetically</value>
</data>
<data name="SortPropertiesOptionDescription" xml:space="preserve">
<value>Whether properties within the JSON should be sorted alphabetically</value>
diff --git a/src/app/dev/DevToys.Tools/Tools/Formatters/Json/JsonFormatterGuiTool.cs b/src/app/dev/DevToys.Tools/Tools/Formatters/Json/JsonFormatterGuiTool.cs
index 383541a34..b5dac4192 100644
--- a/src/app/dev/DevToys.Tools/Tools/Formatters/Json/JsonFormatterGuiTool.cs
+++ b/src/app/dev/DevToys.Tools/Tools/Formatters/Json/JsonFormatterGuiTool.cs
@@ -92,9 +92,8 @@ internal sealed partial class JsonFormatterGuiTool : IGuiTool, IDisposable
Item(JsonFormatter.Minified, Indentation.Minified)
),
Setting("json-text-sortProperties-setting")
- .Icon("FluentSystemIcons", '\uf7ed')
+ .Icon("FluentSystemIcons", '\uf802')
.Title(JsonFormatter.SortProperties)
- .Description(JsonFormatter.SortPropertiesOptionDescription)
.Handle(
_settingsProvider,
sortProperties,
diff --git a/src/app/dev/DevToys.Tools/Tools/Formatters/Sql/SqlFormatter.Designer.cs b/src/app/dev/DevToys.Tools/Tools/Formatters/Sql/SqlFormatter.Designer.cs
new file mode 100644
index 000000000..abedea4ff
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Tools/Formatters/Sql/SqlFormatter.Designer.cs
@@ -0,0 +1,324 @@
+//------------------------------------------------------------------------------
+// <auto-generated>
+// This code was generated by a tool.
+// Runtime Version:4.0.30319.42000
+//
+// Changes to this file may cause incorrect behavior and will be lost if
+// the code is regenerated.
+// </auto-generated>
+//------------------------------------------------------------------------------
+
+namespace DevToys.Tools.Tools.Formatters.Sql {
+ using System;
+
+
+ /// <summary>
+ /// A strongly-typed resource class, for looking up localized strings, etc.
+ /// </summary>
+ // This class was auto-generated by the StronglyTypedResourceBuilder
+ // class via a tool like ResGen or Visual Studio.
+ // To add or remove a member, edit your .ResX file then rerun ResGen
+ // with the /str option, or rebuild your VS project.
+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
+ [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
+ internal class SqlFormatter {
+
+ private static global::System.Resources.ResourceManager resourceMan;
+
+ private static global::System.Globalization.CultureInfo resourceCulture;
+
+ [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
+ internal SqlFormatter() {
+ }
+
+ /// <summary>
+ /// Returns the cached ResourceManager instance used by this class.
+ /// </summary>
+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+ internal static global::System.Resources.ResourceManager ResourceManager {
+ get {
+ if (object.ReferenceEquals(resourceMan, null)) {
+ global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DevToys.Tools.Tools.Formatters.Sql.SqlFormatter", typeof(SqlFormatter).Assembly);
+ resourceMan = temp;
+ }
+ return resourceMan;
+ }
+ }
+
+ /// <summary>
+ /// Overrides the current thread's CurrentUICulture property for all
+ /// resource lookups using this strongly typed resource class.
+ /// </summary>
+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
+ internal static global::System.Globalization.CultureInfo Culture {
+ get {
+ return resourceCulture;
+ }
+ set {
+ resourceCulture = value;
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to SQL Formatter tool.
+ /// </summary>
+ internal static string AccessibleName {
+ get {
+ return ResourceManager.GetString("AccessibleName", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Configuration.
+ /// </summary>
+ internal static string Configuration {
+ get {
+ return ResourceManager.GetString("Configuration", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Db2.
+ /// </summary>
+ internal static string Db2 {
+ get {
+ return ResourceManager.GetString("Db2", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Format SQL data.
+ /// </summary>
+ internal static string Description {
+ get {
+ return ResourceManager.GetString("Description", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to 4 spaces.
+ /// </summary>
+ internal static string FourSpaces {
+ get {
+ return ResourceManager.GetString("FourSpaces", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Indentation.
+ /// </summary>
+ internal static string Indentation {
+ get {
+ return ResourceManager.GetString("Indentation", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Select which indentation you want to use.
+ /// </summary>
+ internal static string IndentationOptionDescription {
+ get {
+ return ResourceManager.GetString("IndentationOptionDescription", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Input.
+ /// </summary>
+ internal static string Input {
+ get {
+ return ResourceManager.GetString("Input", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Input file selected doesn&apos;t exist..
+ /// </summary>
+ internal static string InputFileNotFound {
+ get {
+ return ResourceManager.GetString("InputFileNotFound", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Text or path to a file containing the sql to format.
+ /// </summary>
+ internal static string InputOptionDescription {
+ get {
+ return ResourceManager.GetString("InputOptionDescription", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Please specify an input text or an input file.
+ /// </summary>
+ internal static string InvalidInputOrFileCommand {
+ get {
+ return ResourceManager.GetString("InvalidInputOrFileCommand", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to SQL Formatter.
+ /// </summary>
+ internal static string LongDisplayTitle {
+ get {
+ return ResourceManager.GetString("LongDisplayTitle", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to MariaDB.
+ /// </summary>
+ internal static string MariaDb {
+ get {
+ return ResourceManager.GetString("MariaDb", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to MySQL.
+ /// </summary>
+ internal static string MySql {
+ get {
+ return ResourceManager.GetString("MySql", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to N1QL.
+ /// </summary>
+ internal static string N1ql {
+ get {
+ return ResourceManager.GetString("N1ql", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to 1 tab.
+ /// </summary>
+ internal static string OneTab {
+ get {
+ return ResourceManager.GetString("OneTab", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Output.
+ /// </summary>
+ internal static string Output {
+ get {
+ return ResourceManager.GetString("Output", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Path to the output file.
+ /// </summary>
+ internal static string OutputFileOptionDescription {
+ get {
+ return ResourceManager.GetString("OutputFileOptionDescription", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to PL/SQL.
+ /// </summary>
+ internal static string PlSql {
+ get {
+ return ResourceManager.GetString("PlSql", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to PostgreSQL.
+ /// </summary>
+ internal static string PostgreSql {
+ get {
+ return ResourceManager.GetString("PostgreSql", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Amazon Redshift.
+ /// </summary>
+ internal static string RedShift {
+ get {
+ return ResourceManager.GetString("RedShift", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to SQL Formatter.
+ /// </summary>
+ internal static string SearchDisplayName {
+ get {
+ return ResourceManager.GetString("SearchDisplayName", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to SQL.
+ /// </summary>
+ internal static string ShortDisplayTitle {
+ get {
+ return ResourceManager.GetString("ShortDisplayTitle", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Spark SQL.
+ /// </summary>
+ internal static string SparkSql {
+ get {
+ return ResourceManager.GetString("SparkSql", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Language.
+ /// </summary>
+ internal static string SqlLanguage {
+ get {
+ return ResourceManager.GetString("SqlLanguage", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Which SQL language to use.
+ /// </summary>
+ internal static string SqlLanguageDescription {
+ get {
+ return ResourceManager.GetString("SqlLanguageDescription", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Standard SQL.
+ /// </summary>
+ internal static string StandardSql {
+ get {
+ return ResourceManager.GetString("StandardSql", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Transact-SQL.
+ /// </summary>
+ internal static string TransactSql {
+ get {
+ return ResourceManager.GetString("TransactSql", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to 2 spaces.
+ /// </summary>
+ internal static string TwoSpaces {
+ get {
+ return ResourceManager.GetString("TwoSpaces", resourceCulture);
+ }
+ }
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Tools/Formatters/Sql/SqlFormatter.resx b/src/app/dev/DevToys.Tools/Tools/Formatters/Sql/SqlFormatter.resx
new file mode 100644
index 000000000..f3a53e4b9
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Tools/Formatters/Sql/SqlFormatter.resx
@@ -0,0 +1,207 @@
+<?xml version="1.0" encoding="utf-8"?>
+<root>
+ <!--
+ Microsoft ResX Schema
+
+ Version 2.0
+
+ The primary goals of this format is to allow a simple XML format
+ that is mostly human readable. The generation and parsing of the
+ various data types are done through the TypeConverter classes
+ associated with the data types.
+
+ Example:
+
+ ... ado.net/XML headers & schema ...
+ <resheader name="resmimetype">text/microsoft-resx</resheader>
+ <resheader name="version">2.0</resheader>
+ <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
+ <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
+ <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
+ <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
+ <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
+ <value>[base64 mime encoded serialized .NET Framework object]</value>
+ </data>
+ <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
+ <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
+ <comment>This is a comment</comment>
+ </data>
+
+ There are any number of "resheader" rows that contain simple
+ name/value pairs.
+
+ Each data row contains a name, and value. The row also contains a
+ type or mimetype. Type corresponds to a .NET class that support
+ text/value conversion through the TypeConverter architecture.
+ Classes that don't support this are serialized and stored with the
+ mimetype set.
+
+ The mimetype is used for serialized objects, and tells the
+ ResXResourceReader how to depersist the object. This is currently not
+ extensible. For a given mimetype the value must be set accordingly:
+
+ Note - application/x-microsoft.net.object.binary.base64 is the format
+ that the ResXResourceWriter will generate, however the reader can
+ read any of the formats listed below.
+
+ mimetype: application/x-microsoft.net.object.binary.base64
+ value : The object must be serialized with
+ : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
+ : and then encoded with base64 encoding.
+
+ mimetype: application/x-microsoft.net.object.soap.base64
+ value : The object must be serialized with
+ : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
+ : and then encoded with base64 encoding.
+
+ mimetype: application/x-microsoft.net.object.bytearray.base64
+ value : The object must be serialized into a byte array
+ : using a System.ComponentModel.TypeConverter
+ : and then encoded with base64 encoding.
+ -->
+ <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
+ <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
+ <xsd:element name="root" msdata:IsDataSet="true">
+ <xsd:complexType>
+ <xsd:choice maxOccurs="unbounded">
+ <xsd:element name="metadata">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="value" type="xsd:string" minOccurs="0" />
+ </xsd:sequence>
+ <xsd:attribute name="name" use="required" type="xsd:string" />
+ <xsd:attribute name="type" type="xsd:string" />
+ <xsd:attribute name="mimetype" type="xsd:string" />
+ <xsd:attribute ref="xml:space" />
+ </xsd:complexType>
+ </xsd:element>
+ <xsd:element name="assembly">
+ <xsd:complexType>
+ <xsd:attribute name="alias" type="xsd:string" />
+ <xsd:attribute name="name" type="xsd:string" />
+ </xsd:complexType>
+ </xsd:element>
+ <xsd:element name="data">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+ <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
+ </xsd:sequence>
+ <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
+ <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
+ <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
+ <xsd:attribute ref="xml:space" />
+ </xsd:complexType>
+ </xsd:element>
+ <xsd:element name="resheader">
+ <xsd:complexType>
+ <xsd:sequence>
+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
+ </xsd:sequence>
+ <xsd:attribute name="name" type="xsd:string" use="required" />
+ </xsd:complexType>
+ </xsd:element>
+ </xsd:choice>
+ </xsd:complexType>
+ </xsd:element>
+ </xsd:schema>
+ <resheader name="resmimetype">
+ <value>text/microsoft-resx</value>
+ </resheader>
+ <resheader name="version">
+ <value>2.0</value>
+ </resheader>
+ <resheader name="reader">
+ <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+ </resheader>
+ <resheader name="writer">
+ <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
+ </resheader>
+ <data name="AccessibleName" xml:space="preserve">
+ <value>SQL Formatter tool</value>
+ </data>
+ <data name="Configuration" xml:space="preserve">
+ <value>Configuration</value>
+ </data>
+ <data name="Db2" xml:space="preserve">
+ <value>Db2</value>
+ </data>
+ <data name="Description" xml:space="preserve">
+ <value>Format SQL data</value>
+ </data>
+ <data name="FourSpaces" xml:space="preserve">
+ <value>4 spaces</value>
+ </data>
+ <data name="Indentation" xml:space="preserve">
+ <value>Indentation</value>
+ </data>
+ <data name="IndentationOptionDescription" xml:space="preserve">
+ <value>Select which indentation you want to use</value>
+ </data>
+ <data name="Input" xml:space="preserve">
+ <value>Input</value>
+ </data>
+ <data name="InputFileNotFound" xml:space="preserve">
+ <value>Input file selected doesn't exist.</value>
+ </data>
+ <data name="InputOptionDescription" xml:space="preserve">
+ <value>Text or path to a file containing the sql to format</value>
+ </data>
+ <data name="InvalidInputOrFileCommand" xml:space="preserve">
+ <value>Please specify an input text or an input file</value>
+ </data>
+ <data name="LongDisplayTitle" xml:space="preserve">
+ <value>SQL Formatter</value>
+ </data>
+ <data name="MariaDb" xml:space="preserve">
+ <value>MariaDB</value>
+ </data>
+ <data name="MySql" xml:space="preserve">
+ <value>MySQL</value>
+ </data>
+ <data name="N1ql" xml:space="preserve">
+ <value>N1QL</value>
+ </data>
+ <data name="OneTab" xml:space="preserve">
+ <value>1 tab</value>
+ </data>
+ <data name="Output" xml:space="preserve">
+ <value>Output</value>
+ </data>
+ <data name="OutputFileOptionDescription" xml:space="preserve">
+ <value>Path to the output file</value>
+ </data>
+ <data name="PlSql" xml:space="preserve">
+ <value>PL/SQL</value>
+ </data>
+ <data name="PostgreSql" xml:space="preserve">
+ <value>PostgreSQL</value>
+ </data>
+ <data name="RedShift" xml:space="preserve">
+ <value>Amazon Redshift</value>
+ </data>
+ <data name="SearchDisplayName" xml:space="preserve">
+ <value>SQL Formatter</value>
+ </data>
+ <data name="ShortDisplayTitle" xml:space="preserve">
+ <value>SQL</value>
+ </data>
+ <data name="SparkSql" xml:space="preserve">
+ <value>Spark SQL</value>
+ </data>
+ <data name="SqlLanguage" xml:space="preserve">
+ <value>Language</value>
+ </data>
+ <data name="SqlLanguageDescription" xml:space="preserve">
+ <value>Which SQL language to use</value>
+ </data>
+ <data name="StandardSql" xml:space="preserve">
+ <value>Standard SQL</value>
+ </data>
+ <data name="TransactSql" xml:space="preserve">
+ <value>Transact-SQL</value>
+ </data>
+ <data name="TwoSpaces" xml:space="preserve">
+ <value>2 spaces</value>
+ </data>
+</root>
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Tools/Tools/Formatters/Sql/SqlFormatterCommandLineTool.cs b/src/app/dev/DevToys.Tools/Tools/Formatters/Sql/SqlFormatterCommandLineTool.cs
new file mode 100644
index 000000000..00455ad29
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Tools/Formatters/Sql/SqlFormatterCommandLineTool.cs
@@ -0,0 +1,80 @@
+using DevToys.Tools.Helpers.SqlFormatter;
+using DevToys.Tools.Helpers;
+using DevToys.Tools.Models;
+using Microsoft.Extensions.Logging;
+using OneOf;
+
+namespace DevToys.Tools.Tools.Formatters.Sql;
+
+[Export(typeof(ICommandLineTool))]
+[Name("SqlFormatter")]
+[CommandName(
+ Name = "sqlFormatter",
+ Alias = "sqlf",
+ ResourceManagerBaseName = "DevToys.Tools.Tools.Formatters.Sql.SqlFormatter",
+ DescriptionResourceName = nameof(SqlFormatter.Description))]
+internal sealed class SqlFormatterCommandLineTool : ICommandLineTool
+{
+#pragma warning disable IDE0044 // Add readonly modifier
+ [Import]
+ private IFileStorage _fileStorage = null!;
+#pragma warning restore IDE0044 // Add readonly modifier
+
+ [CommandLineOption(
+ Name = "input",
+ Alias = "i",
+ IsRequired = true,
+ DescriptionResourceName = nameof(SqlFormatter.InputOptionDescription))]
+ internal OneOf<FileInfo, string>? Input { get; set; }
+
+ [CommandLineOption(
+ Name = "outputFile",
+ Alias = "o",
+ DescriptionResourceName = nameof(SqlFormatter.OutputFileOptionDescription))]
+ internal FileInfo? OutputFile { get; set; }
+
+ [CommandLineOption(
+ Name = "indentation",
+ DescriptionResourceName = nameof(SqlFormatter.IndentationOptionDescription))]
+ internal Indentation IndentationMode { get; set; } = Indentation.TwoSpaces;
+
+ [CommandLineOption(
+ Name = "language",
+ DescriptionResourceName = nameof(SqlFormatter.SqlLanguageDescription))]
+ internal SqlLanguage SqlLanguage { get; set; } = SqlLanguage.Sql;
+
+ public async ValueTask<int> InvokeAsync(ILogger logger, CancellationToken cancellationToken)
+ {
+ if (!Input.HasValue)
+ {
+ Console.Error.WriteLine(SqlFormatter.InvalidInputOrFileCommand);
+ return -1;
+ }
+
+ ResultInfo<string> input = await Input.Value.ReadAllTextAsync(_fileStorage, cancellationToken);
+ if (!input.HasSucceeded)
+ {
+ Console.Error.WriteLine(SqlFormatter.InputFileNotFound);
+ return -1;
+ }
+
+ Guard.IsNotNull(input.Data);
+ string formatResult = SqlFormatterHelper.Format(
+ input.Data,
+ SqlLanguage,
+ new SqlFormatterOptions(
+ IndentationMode,
+ uppercase: true,
+ linesBetweenQueries: 2));
+
+ cancellationToken.ThrowIfCancellationRequested();
+
+ if (string.IsNullOrEmpty(formatResult))
+ {
+ return -1;
+ }
+
+ await FileHelper.WriteOutputAsync(formatResult, OutputFile, cancellationToken);
+ return 0;
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Tools/Formatters/Sql/SqlFormatterGuiTool.cs b/src/app/dev/DevToys.Tools/Tools/Formatters/Sql/SqlFormatterGuiTool.cs
new file mode 100644
index 000000000..83bddc0d0
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/Tools/Formatters/Sql/SqlFormatterGuiTool.cs
@@ -0,0 +1,185 @@
+using DevToys.Tools.Helpers.SqlFormatter;
+using DevToys.Tools.Models;
+using Microsoft.Extensions.Logging;
+
+namespace DevToys.Tools.Tools.Formatters.Sql;
+
+[Export(typeof(IGuiTool))]
+[Name("SqlFormatter")]
+[ToolDisplayInformation(
+ IconFontName = "DevToys-Tools-Icons",
+ IconGlyph = '\u0114',
+ GroupName = PredefinedCommonToolGroupNames.Formatters,
+ ResourceManagerAssemblyIdentifier = nameof(DevToysToolsResourceManagerAssemblyIdentifier),
+ ResourceManagerBaseName = "DevToys.Tools.Tools.Formatters.Sql.SqlFormatter",
+ ShortDisplayTitleResourceName = nameof(SqlFormatter.ShortDisplayTitle),
+ LongDisplayTitleResourceName = nameof(SqlFormatter.LongDisplayTitle),
+ DescriptionResourceName = nameof(SqlFormatter.Description),
+ AccessibleNameResourceName = nameof(SqlFormatter.AccessibleName))]
+internal sealed partial class SqlFormatterGuiTool : IGuiTool, IDisposable
+{
+ /// <summary>
+ /// Which indentation the tool should use.
+ /// </summary>
+ private static readonly SettingDefinition<Indentation> indentationMode
+ = new(name: $"{nameof(SqlFormatterGuiTool)}.{nameof(indentationMode)}", defaultValue: Indentation.TwoSpaces);
+
+ /// <summary>
+ /// The SQL language to consider when formatting.
+ /// </summary>
+ private static readonly SettingDefinition<SqlLanguage> sqlLanguage
+ = new(name: $"{nameof(SqlFormatterGuiTool)}.{nameof(sqlLanguage)}", defaultValue: SqlLanguage.Sql);
+
+ private enum GridColumn
+ {
+ Content
+ }
+
+ private enum GridRow
+ {
+ Header,
+ Content,
+ Footer
+ }
+
+ private readonly DisposableSemaphore _semaphore = new();
+ private readonly ILogger _logger;
+ private readonly ISettingsProvider _settingsProvider;
+ private readonly IUIMultiLineTextInput _inputTextArea = MultilineTextInput("sql-input-text-area");
+ private readonly IUIMultiLineTextInput _outputTextArea = MultilineTextInput("sql-output-text-area");
+
+ private CancellationTokenSource? _cancellationTokenSource;
+
+ [ImportingConstructor]
+ public SqlFormatterGuiTool(ISettingsProvider settingsProvider)
+ {
+ _logger = this.Log();
+ _settingsProvider = settingsProvider;
+ }
+
+ internal Task? WorkTask { get; private set; }
+
+ public UIToolView View
+ => new(
+ isScrollable: true,
+ Grid()
+ .ColumnLargeSpacing()
+ .RowLargeSpacing()
+ .Rows(
+ (GridRow.Header, Auto),
+ (GridRow.Content, new UIGridLength(1, UIGridUnitType.Fraction))
+ )
+ .Columns(
+ (GridColumn.Content, new UIGridLength(1, UIGridUnitType.Fraction))
+ )
+ .Cells(
+ Cell(
+ GridRow.Header,
+ GridColumn.Content,
+ Stack().Vertical().WithChildren(
+ Label().Text(SqlFormatter.Configuration),
+ Setting("sql-text-newLineOnAttributes-setting")
+ .Icon("FluentSystemIcons", '\uF2EF')
+ .Title(SqlFormatter.SqlLanguage)
+ .Handle(
+ _settingsProvider,
+ sqlLanguage,
+ OnSqlLanguageChanged,
+ Item(SqlFormatter.Db2, SqlLanguage.Db2),
+ Item(SqlFormatter.MariaDb, SqlLanguage.MariaDb),
+ Item(SqlFormatter.MySql, SqlLanguage.MySql),
+ Item(SqlFormatter.N1ql, SqlLanguage.N1ql),
+ Item(SqlFormatter.PlSql, SqlLanguage.PlSql),
+ Item(SqlFormatter.PostgreSql, SqlLanguage.PostgreSql),
+ Item(SqlFormatter.RedShift, SqlLanguage.RedShift),
+ Item(SqlFormatter.SparkSql, SqlLanguage.Spark),
+ Item(SqlFormatter.StandardSql, SqlLanguage.Sql),
+ Item(SqlFormatter.TransactSql, SqlLanguage.Tsql)
+ ),
+ Setting("sql-text-indentation-setting")
+ .Icon("FluentSystemIcons", '\uF6F8')
+ .Title(SqlFormatter.Indentation)
+ .Handle(
+ _settingsProvider,
+ indentationMode,
+ OnIndentationModelChanged,
+ Item(SqlFormatter.TwoSpaces, Indentation.TwoSpaces),
+ Item(SqlFormatter.FourSpaces, Indentation.FourSpaces),
+ Item(SqlFormatter.OneTab, Indentation.OneTab)
+ )
+ )
+ ),
+ Cell(
+ GridRow.Content,
+ GridColumn.Content,
+ SplitGrid()
+ .Vertical()
+ .WithLeftPaneChild(
+ _inputTextArea
+ .Language("sql")
+ .Title(SqlFormatter.Input)
+ .OnTextChanged(OnInputTextChanged))
+ .WithRightPaneChild(
+ _outputTextArea
+ .Language("sql")
+ .Title(SqlFormatter.Output)
+ .ReadOnly()
+ .Extendable())
+ )
+ )
+ );
+
+ // Smart detection handler.
+ public void OnDataReceived(string dataTypeName, object? parsedData)
+ {
+ }
+
+ public void Dispose()
+ {
+ _cancellationTokenSource?.Cancel();
+ _cancellationTokenSource?.Dispose();
+ _semaphore.Dispose();
+ }
+
+ private void OnIndentationModelChanged(Indentation indentationMode)
+ {
+ StartFormat(_inputTextArea.Text);
+ }
+
+ private void OnSqlLanguageChanged(SqlLanguage sqlLanguage)
+ {
+ StartFormat(_inputTextArea.Text);
+ }
+
+ private void OnInputTextChanged(string text)
+ {
+ StartFormat(text);
+ }
+
+ private void StartFormat(string text)
+ {
+ _cancellationTokenSource?.Cancel();
+ _cancellationTokenSource?.Dispose();
+ _cancellationTokenSource = new CancellationTokenSource();
+
+ WorkTask = FormatAsync(text, _settingsProvider.GetSetting(indentationMode), _settingsProvider.GetSetting(sqlLanguage), _cancellationTokenSource.Token);
+ }
+
+ private async Task FormatAsync(string input, Indentation indentationSetting, SqlLanguage sqlLanguageSetting, CancellationToken cancellationToken)
+ {
+ using (await _semaphore.WaitAsync(cancellationToken))
+ {
+ await TaskSchedulerAwaiter.SwitchOffMainThreadAsync(cancellationToken);
+
+ string formatResult = SqlFormatterHelper.Format(
+ input,
+ sqlLanguageSetting,
+ new SqlFormatterOptions(
+ indentationSetting,
+ uppercase: true,
+ linesBetweenQueries: 2));
+
+ _outputTextArea.Text(formatResult);
+ }
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Tools/Formatters/Xml/XmlFormatter.Designer.cs b/src/app/dev/DevToys.Tools/Tools/Formatters/Xml/XmlFormatter.Designer.cs
index d5ffad83a..a0ee79d2f 100644
--- a/src/app/dev/DevToys.Tools/Tools/Formatters/Xml/XmlFormatter.Designer.cs
+++ b/src/app/dev/DevToys.Tools/Tools/Formatters/Xml/XmlFormatter.Designer.cs
@@ -169,7 +169,7 @@ namespace DevToys.Tools.Tools.Formatters.Xml {
}
/// <summary>
- /// Looks up a localized string similar to New Line on Attributes.
+ /// Looks up a localized string similar to Put attributes on a new line.
/// </summary>
internal static string NewLineOnAttributes {
get {
diff --git a/src/app/dev/DevToys.Tools/Tools/Formatters/Xml/XmlFormatter.resx b/src/app/dev/DevToys.Tools/Tools/Formatters/Xml/XmlFormatter.resx
index d83df3e17..4c7fb2cda 100644
--- a/src/app/dev/DevToys.Tools/Tools/Formatters/Xml/XmlFormatter.resx
+++ b/src/app/dev/DevToys.Tools/Tools/Formatters/Xml/XmlFormatter.resx
@@ -154,7 +154,7 @@
<value>Minified</value>
</data>
<data name="NewLineOnAttributes" xml:space="preserve">
- <value>New Line on Attributes</value>
+ <value>Put attributes on a new line</value>
</data>
<data name="NewLineOnAttributesDescription" xml:space="preserve">
<value>Whether to put attributes on a new line</value>
diff --git a/src/app/tests/DevToys.UnitTests/Tools/Formatters/Json/JsonFormatterCommandLineToolTests.cs b/src/app/tests/DevToys.UnitTests/Tools/Formatters/Json/JsonFormatterCommandLineToolTests.cs
index 5d24ba72d..e08f01736 100644
--- a/src/app/tests/DevToys.UnitTests/Tools/Formatters/Json/JsonFormatterCommandLineToolTests.cs
+++ b/src/app/tests/DevToys.UnitTests/Tools/Formatters/Json/JsonFormatterCommandLineToolTests.cs
@@ -2,9 +2,7 @@
using System.Threading.Tasks;
using DevToys.CLI;
using DevToys.Tools.Models;
-using DevToys.Tools.Tools.Converters.JsonYaml;
using DevToys.Tools.Tools.Formatters.Json;
-using DevToys.UnitTests.Mocks;
using Microsoft.Extensions.Logging;
namespace DevToys.UnitTests.Tools.Formatters.Json;
diff --git a/src/app/tests/DevToys.UnitTests/Tools/Formatters/Sql/SqlFormatterCommandLineToolTests.cs b/src/app/tests/DevToys.UnitTests/Tools/Formatters/Sql/SqlFormatterCommandLineToolTests.cs
new file mode 100644
index 000000000..bb137af05
--- /dev/null
+++ b/src/app/tests/DevToys.UnitTests/Tools/Formatters/Sql/SqlFormatterCommandLineToolTests.cs
@@ -0,0 +1,99 @@
+using System.IO;
+using System.Threading.Tasks;
+using DevToys.CLI;
+using DevToys.Tools.Helpers.SqlFormatter;
+using DevToys.Tools.Models;
+using DevToys.Tools.Tools.Formatters.Sql;
+using Microsoft.Extensions.Logging;
+
+namespace DevToys.UnitTests.Tools.Formatters.Sql;
+
+[Collection(nameof(TestParallelizationDisabled))]
+public class SqlFormatterCommandLineToolTests : MefBasedTest
+{
+ private readonly StringWriter _consoleWriter = new();
+ private readonly StringWriter _consoleErrorWriter = new();
+ private readonly SqlFormatterCommandLineTool _tool;
+ private readonly Mock<ILogger> _loggerMock;
+ private readonly string _baseTestDataDirectory = Path.Combine("Tools", "TestData", "Sql");
+
+ public SqlFormatterCommandLineToolTests()
+ : base(typeof(SqlFormatterCommandLineTool).Assembly)
+ {
+ _tool = (SqlFormatterCommandLineTool)MefProvider.ImportMany<ICommandLineTool, CommandLineToolMetadata>()
+ .Single(t => t.Metadata.InternalComponentName == "SqlFormatter").Value;
+
+ _loggerMock = new Mock<ILogger>();
+ Console.SetOut(_consoleWriter);
+ Console.SetError(_consoleErrorWriter);
+ }
+
+ [Theory(DisplayName = "Format with invalid input should return error exit code")]
+ [InlineData("")]
+ [InlineData(" ")]
+ public async Task FormatSqlInvalidInputShouldReturnErrorExitCode(string input)
+ {
+ _tool.Input = input;
+
+ int result = await _tool.InvokeAsync(_loggerMock.Object, default);
+ result.Should().Be(-1);
+ string consoleOutput = _consoleErrorWriter.ToString().Trim();
+ consoleOutput.Should().Be("");
+ }
+
+ [Theory(DisplayName = "Format sql with valid standard sql should output valid sql")]
+ [InlineData(SqlLanguage.Sql, "SELECT * FETCH FIRST 2 ROWS ONLY;", @"SELECT
+ *
+FETCH FIRST
+ 2 ROWS ONLY;")]
+ [InlineData(SqlLanguage.N1ql, "SELECT order_lines[0].productId FROM orders;", @"SELECT
+ order_lines[0].productId
+FROM
+ orders;")]
+ public async Task FormatSqlWithValidSqlAndTwoSpacesShouldOutputValidSql(SqlLanguage language, string input, string expectedResult)
+ {
+ _tool.Input = input;
+ _tool.SqlLanguage = language;
+
+ int result = await _tool.InvokeAsync(_loggerMock.Object, default);
+ result.Should().Be(0);
+ string consoleOutput = _consoleWriter.ToString().Trim();
+ consoleOutput.Should().Be(expectedResult);
+ }
+
+ [Theory(DisplayName = "Format sql with valid standard sql should output valid sql")]
+ [InlineData(SqlLanguage.Sql, "SELECT * FETCH FIRST 2 ROWS ONLY;", @"SELECT
+ *
+FETCH FIRST
+ 2 ROWS ONLY;")]
+ [InlineData(SqlLanguage.Tsql, "INSERT Customers (ID, MoneyBalance, Address, City) VALUES (12,-123.4, 'Skagen 2111','Stv');", @"INSERT
+ Customers (ID, MoneyBalance, Address, City)
+VALUES
+ (12, -123.4, 'Skagen 2111', 'Stv');")]
+ public async Task FormatSqlWithValidSqlAndFourSpacesShouldOutputValidSql(SqlLanguage language, string input, string expectedResult)
+ {
+ _tool.Input = input;
+ _tool.SqlLanguage = language;
+ _tool.IndentationMode = Indentation.FourSpaces;
+
+ int result = await _tool.InvokeAsync(_loggerMock.Object, default);
+ result.Should().Be(0);
+ string consoleOutput = _consoleWriter.ToString().Trim();
+ consoleOutput.Should().Be(expectedResult);
+ }
+
+ [Theory(DisplayName = "Format sql with valid standard sql should output valid sql")]
+ [InlineData(SqlLanguage.Sql, "SELECT * FETCH FIRST 2 ROWS ONLY;", "SELECT\r\n\t*\r\nFETCH FIRST\r\n\t2 ROWS ONLY;")]
+ [InlineData(SqlLanguage.Db2, "SELECT col1 FROM tbl ORDER BY col2 DESC FETCH FIRST 20 ROWS ONLY;", "SELECT\r\n\tcol1\r\nFROM\r\n\ttbl\r\nORDER BY\r\n\tcol2 DESC\r\nFETCH FIRST\r\n\t20 ROWS ONLY;")]
+ public async Task FormatSqlWithValidSqlAndTabShouldOutputValidSql(SqlLanguage language, string input, string expectedResult)
+ {
+ _tool.Input = input;
+ _tool.SqlLanguage = language;
+ _tool.IndentationMode = Indentation.OneTab;
+
+ int result = await _tool.InvokeAsync(_loggerMock.Object, default);
+ result.Should().Be(0);
+ string consoleOutput = _consoleWriter.ToString().Trim();
+ consoleOutput.Should().Be(expectedResult);
+ }
+}
diff --git a/src/app/tests/DevToys.UnitTests/Tools/Formatters/Sql/SqlFormatterGuiToolTests.cs b/src/app/tests/DevToys.UnitTests/Tools/Formatters/Sql/SqlFormatterGuiToolTests.cs
new file mode 100644
index 000000000..9705fb4d4
--- /dev/null
+++ b/src/app/tests/DevToys.UnitTests/Tools/Formatters/Sql/SqlFormatterGuiToolTests.cs
@@ -0,0 +1,83 @@
+using System.Globalization;
+using System.Threading.Tasks;
+using DevToys.Core.Tools.Metadata;
+using DevToys.Tools.Tools.Formatters.Sql;
+
+namespace DevToys.UnitTests.Tools.Formatters.Sql;
+
+public class SqlFormatterGuiToolTests : MefBasedTest
+{
+ private readonly UIToolView _toolView;
+ private readonly SqlFormatterGuiTool _tool;
+ private readonly IUIMultiLineTextInput _inputTextArea;
+ private readonly IUIMultiLineTextInput _outputTextArea;
+
+ public SqlFormatterGuiToolTests()
+ : base(typeof(SqlFormatterGuiTool).Assembly)
+ {
+ _tool = (SqlFormatterGuiTool)MefProvider.ImportMany<IGuiTool, GuiToolMetadata>()
+ .Single(t => t.Metadata.InternalComponentName == "SqlFormatter")
+ .Value;
+
+ _toolView = _tool.View;
+
+ _inputTextArea = (IUIMultiLineTextInput)_toolView.GetChildElementById("sql-input-text-area");
+ _outputTextArea = (IUIMultiLineTextInput)_toolView.GetChildElementById("sql-output-text-area");
+ CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
+ CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
+ }
+
+ [Theory(DisplayName = "Format sql with invalid sql should return empty string")]
+ [InlineData(null)]
+ [InlineData("")]
+ [InlineData(" ")]
+ public async Task FormatSqlWithInvalidSqlShouldReturnSqlError(string input)
+ {
+ _inputTextArea.Text("abc");
+ _inputTextArea.Text(input);
+ await _tool.WorkTask;
+ _outputTextArea.Text.Should().Be("");
+ }
+
+ [Fact(DisplayName = "Format sql with valid sql and two spaces indentation should return valid sql")]
+ public async Task FormatSqlWithValidSqlAndTwoSpacesShouldReturnValidSql()
+ {
+ _inputTextArea.Text("SELECT * FETCH FIRST 2 ROWS ONLY;");
+
+ var indentationOptions = (IUISelectDropDownList)((IUISetting)_toolView.GetChildElementById("sql-text-indentation-setting")).InteractiveElement;
+ indentationOptions.Select(0); // Select TwoSpaces
+
+ await _tool.WorkTask;
+ _outputTextArea.Text.Should().Be(@"SELECT
+ *
+FETCH FIRST
+ 2 ROWS ONLY;");
+ }
+
+ [Fact(DisplayName = "Format sql with valid sql and four spaces indentation should return valid sql")]
+ public async Task FormatSqlWithValidSqlAndFourSpacesShouldReturnValidSql()
+ {
+ _inputTextArea.Text("SELECT * FETCH FIRST 2 ROWS ONLY;");
+
+ var indentationOptions = (IUISelectDropDownList)((IUISetting)_toolView.GetChildElementById("sql-text-indentation-setting")).InteractiveElement;
+ indentationOptions.Select(1); // Select FourSpaces
+
+ await _tool.WorkTask;
+ _outputTextArea.Text.Should().Be(@"SELECT
+ *
+FETCH FIRST
+ 2 ROWS ONLY;");
+ }
+
+ [Fact(DisplayName = "Format sql with valid sql and one tab indentation should return valid sql")]
+ public async Task FormatSqlWithValidSqlAndOneTabShouldReturnValidSql()
+ {
+ _inputTextArea.Text("SELECT * FETCH FIRST 2 ROWS ONLY;");
+
+ var indentationOptions = (IUISelectDropDownList)((IUISetting)_toolView.GetChildElementById("sql-text-indentation-setting")).InteractiveElement;
+ indentationOptions.Select(2); // Select OneTab
+
+ await _tool.WorkTask;
+ _outputTextArea.Text.Should().Be("SELECT\r\n\t*\r\nFETCH FIRST\r\n\t2 ROWS ONLY;");
+ }
+}
diff --git a/src/app/tests/DevToys.UnitTests/Tools/Formatters/Sql/SqlFormatterTestHelpers.cs b/src/app/tests/DevToys.UnitTests/Tools/Formatters/Sql/SqlFormatterTestHelpers.cs
new file mode 100644
index 000000000..1714943bc
--- /dev/null
+++ b/src/app/tests/DevToys.UnitTests/Tools/Formatters/Sql/SqlFormatterTestHelpers.cs
@@ -0,0 +1,1003 @@
+using System.Text.RegularExpressions;
+using DevToys.Tools.Helpers.SqlFormatter.Core;
+using DevToys.Tools.Helpers.SqlFormatter;
+using Indentation = DevToys.Tools.Models.Indentation;
+
+namespace DevToys.UnitTests.Tools.Formatters.Sql;
+internal static class SqlFormatterTestHelpers
+{
+ /// <summary>
+ /// Core tests for all SQL formatters
+ /// </summary>
+ internal static void BehavesLikeSqlFormatter(Formatter formatter)
+ {
+ SupportsComments(formatter);
+ SupportsConfigOptions(formatter);
+ SupportsOperators(formatter, "=", "+", "-", "*", "/", "<>", ">", "<", ">=", "<=");
+
+ // does nothing with empty input
+ string input = string.Empty;
+ string expectedResult = string.Empty;
+ string output = formatter.Format(input);
+ output.Should().Be(expectedResult);
+
+ // formats lonely semicolon
+ input = ";";
+ expectedResult = ";";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats simple SELECT query
+ input = "SELECT count(*),Column1 FROM Table1;";
+ expectedResult =
+@"SELECT
+ count(*),
+ Column1
+FROM
+ Table1;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats complex SELECT
+ input = "SELECT DISTINCT name, ROUND(age/7) field1, 18 + 20 AS field2, 'some string' FROM foo;";
+ expectedResult =
+@"SELECT
+ DISTINCT name,
+ ROUND(age / 7) field1,
+ 18 + 20 AS field2,
+ 'some string'
+FROM
+ foo;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats SELECT with complex WHERE
+ input =
+@"SELECT * FROM foo WHERE Column1 = 'testing'
+AND ( (Column2 = Column3 OR Column4 >= NOW()) );";
+ expectedResult =
+@"SELECT
+ *
+FROM
+ foo
+WHERE
+ Column1 = 'testing'
+ AND (
+ (
+ Column2 = Column3
+ OR Column4 >= NOW()
+ )
+ );";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats SELECT with top level reserved words
+ input =
+@"SELECT * FROM foo WHERE name = 'John' GROUP BY some_column
+HAVING column > 10 ORDER BY other_column LIMIT 5;";
+ expectedResult =
+@"SELECT
+ *
+FROM
+ foo
+WHERE
+ name = 'John'
+GROUP BY
+ some_column
+HAVING
+ column > 10
+ORDER BY
+ other_column
+LIMIT
+ 5;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats LIMIT with two comma-separated values on single line
+ input = @"LIMIT 5, 10;";
+ expectedResult =
+@"LIMIT
+ 5, 10;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats LIMIT of single value followed by another SELECT using commas
+ input = @"LIMIT 5; SELECT foo, bar;";
+ expectedResult =
+@"LIMIT
+ 5;
+SELECT
+ foo,
+ bar;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats LIMIT of single value and OFFSET
+ input = @"LIMIT 5 OFFSET 8;";
+ expectedResult =
+@"LIMIT
+ 5 OFFSET 8;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // recognizes LIMIT in lowercase
+ input = @"limit 5, 10;";
+ expectedResult =
+@"limit
+ 5, 10;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // preserves case of keywords
+ input = @"select distinct * frOM foo WHERe a > 1 and b = 3";
+ expectedResult =
+@"select
+ distinct *
+frOM
+ foo
+WHERe
+ a > 1
+ and b = 3";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats SELECT query with SELECT query inside it
+ input = @"SELECT *, SUM(*) AS sum FROM (SELECT * FROM Posts LIMIT 30) WHERE a > b";
+ expectedResult =
+@"SELECT
+ *,
+ SUM(*) AS sum
+FROM
+ (
+ SELECT
+ *
+ FROM
+ Posts
+ LIMIT
+ 30
+ )
+WHERE
+ a > b";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats simple INSERT query
+ input = @"INSERT INTO Customers (ID, MoneyBalance, Address, City) VALUES (12,-123.4, 'Skagen 2111','Stv');";
+ expectedResult =
+@"INSERT INTO
+ Customers (ID, MoneyBalance, Address, City)
+VALUES
+ (12, -123.4, 'Skagen 2111', 'Stv');";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats open paren after comma
+ input = @"WITH TestIds AS (VALUES (4),(5), (6),(7),(9),(10),(11)) SELECT * FROM TestIds;";
+ expectedResult =
+@"WITH TestIds AS (
+ VALUES
+ (4),
+ (5),
+ (6),
+ (7),
+ (9),
+ (10),
+ (11)
+)
+SELECT
+ *
+FROM
+ TestIds;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // keeps short parenthesized list with nested parenthesis on single line
+ input = @"SELECT (a + b * (c - NOW()));";
+ expectedResult =
+@"SELECT
+ (a + b * (c - NOW()));";
+ AssertFormat(formatter, input, expectedResult);
+
+ // breaks long parenthesized lists to multiple lines
+ input =
+@"INSERT INTO some_table (id_product, id_shop, id_currency, id_country, id_registration) (
+SELECT IF(dq.id_discounter_shopping = 2, dq.value, dq.value / 100),
+IF (dq.id_discounter_shopping = 2, 'amount', 'percentage') FROM foo);";
+ expectedResult =
+@"INSERT INTO
+ some_table (
+ id_product,
+ id_shop,
+ id_currency,
+ id_country,
+ id_registration
+ ) (
+ SELECT
+ IF(
+ dq.id_discounter_shopping = 2,
+ dq.value,
+ dq.value / 100
+ ),
+ IF (
+ dq.id_discounter_shopping = 2,
+ 'amount',
+ 'percentage'
+ )
+ FROM
+ foo
+ );";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats simple UPDATE query
+ input = @"UPDATE Customers SET ContactName='Alfred Schmidt', City='Hamburg' WHERE CustomerName='Alfreds Futterkiste';";
+ expectedResult =
+@"UPDATE
+ Customers
+SET
+ ContactName = 'Alfred Schmidt',
+ City = 'Hamburg'
+WHERE
+ CustomerName = 'Alfreds Futterkiste';";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats simple DELETE query
+ input = @"DELETE FROM Customers WHERE CustomerName='Alfred' AND Phone=5002132;";
+ expectedResult =
+@"DELETE FROM
+ Customers
+WHERE
+ CustomerName = 'Alfred'
+ AND Phone = 5002132;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats simple DROP query
+ input = @"DROP TABLE IF EXISTS admin_role;";
+ expectedResult = @"DROP TABLE IF EXISTS admin_role;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats incomplete query
+ input = @"SELECT count(";
+ expectedResult =
+@"SELECT
+ count(";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats UPDATE query with AS part
+ input = @"UPDATE customers SET total_orders = order_summary.total FROM ( SELECT * FROM bank) AS order_summary";
+ expectedResult =
+@"UPDATE
+ customers
+SET
+ total_orders = order_summary.total
+FROM
+ (
+ SELECT
+ *
+ FROM
+ bank
+ ) AS order_summary";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats top-level and newline multi-word reserved words with inconsistent spacing
+ input = "SELECT * FROM foo LEFT \t \r\n JOIN bar ORDER \r\n BY blah";
+ expectedResult = "SELECT\r\n *\r\nFROM\r\n foo\r\n LEFT \t \r\n JOIN bar\r\nORDER \r\n BY\r\n blah";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats long double parenthized queries to multiple lines
+ input = @"((foo = '0123456789-0123456789-0123456789-0123456789'))";
+ expectedResult =
+@"(
+ (
+ foo = '0123456789-0123456789-0123456789-0123456789'
+ )
+)";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats short double parenthized queries to one line
+ input = @"((foo = 'bar'))";
+ expectedResult = @"((foo = 'bar'))";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats logical operators
+ AssertFormat(formatter, @"foo ALL bar", @"foo ALL bar");
+ AssertFormat(formatter, @"foo = ANY (1, 2, 3)", @"foo = ANY (1, 2, 3)");
+ AssertFormat(formatter, @"EXISTS bar", @"EXISTS bar");
+ AssertFormat(formatter, @"foo IN (1, 2, 3)", @"foo IN (1, 2, 3)");
+ AssertFormat(formatter, @"foo LIKE 'hello%'", @"foo LIKE 'hello%'");
+ AssertFormat(formatter, @"foo IS NULL", @"foo IS NULL");
+ AssertFormat(formatter, @"UNIQUE foo", @"UNIQUE foo");
+
+ // formats AND/OR operators
+ AssertFormat(formatter, "foo AND bar", "foo\r\nAND bar");
+ AssertFormat(formatter, "foo OR bar", "foo\r\nOR bar");
+
+ // keeps separation between multiple statements
+ AssertFormat(formatter, "foo;bar;", "foo;\r\nbar;");
+ AssertFormat(formatter, "foo\r\n;bar;", "foo;\r\nbar;");
+ AssertFormat(formatter, "foo\n\n\n;bar;\n\n", "foo;\r\nbar;");
+ input =
+@"SELECT count(*),Column1 FROM Table1;
+SELECT count(*),Column1 FROM Table2;";
+ expectedResult =
+@"SELECT
+ count(*),
+ Column1
+FROM
+ Table1;
+SELECT
+ count(*),
+ Column1
+FROM
+ Table2;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats unicode correctly
+ input = @"SELECT 结合使用, тест FROM table;";
+ expectedResult =
+@"SELECT
+ 结合使用,
+ тест
+FROM
+ table;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // correctly indents create statement after select
+ input =
+@"SELECT * FROM test;
+CREATE TABLE TEST(id NUMBER NOT NULL, col1 VARCHAR2(20), col2 VARCHAR2(20));";
+ expectedResult =
+@"SELECT
+ *
+FROM
+ test;
+CREATE TABLE TEST(
+ id NUMBER NOT NULL,
+ col1 VARCHAR2(20),
+ col2 VARCHAR2(20)
+);";
+ AssertFormat(formatter, input, expectedResult);
+
+ // correctly handles floats as single tokens
+ input = @"SELECT 1e-9 AS a, 1.5e-10 AS b, 3.5E12 AS c, 3.5e12 AS d;";
+ expectedResult =
+@"SELECT
+ 1e-9 AS a,
+ 1.5e-10 AS b,
+ 3.5E12 AS c,
+ 3.5e12 AS d;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // does not split UNION ALL in half
+ input =
+@"SELECT * FROM tbl1
+UNION ALL
+SELECT * FROM tbl2;";
+ expectedResult =
+@"SELECT
+ *
+FROM
+ tbl1
+UNION ALL
+SELECT
+ *
+FROM
+ tbl2;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // correctly formats hardcoded values in from statement
+ input =
+@"SELECT Id FROM (values(1),(2),(3), (4)) as b (id)";
+ expectedResult =
+@"SELECT
+ Id
+FROM
+ (
+ values
+ (1),
+ (2),
+ (3),
+ (4)
+ ) as b (id)";
+ AssertFormat(formatter, input, expectedResult);
+ }
+
+ /// <summary>
+ /// Shared tests for MySQL and MariaDB
+ /// </summary>
+ internal static void BehavesLikeMariaDbFormatter(Formatter formatter)
+ {
+ BehavesLikeSqlFormatter(formatter);
+ SupportsCase(formatter);
+ SupportsCreateTable(formatter);
+ SupportsAlterTable(formatter);
+ SupportsStrings(formatter, "\"\"", "''", "``");
+ SupportsBetween(formatter);
+ SupportsComments(formatter);
+ SupportsConfigOptions(formatter);
+ SupportsOperators(
+ formatter,
+ "%",
+ "&",
+ "|",
+ "^",
+ "~",
+ "!=",
+ "!",
+ "<=>",
+ "<<",
+ ">>",
+ "&&",
+ "||",
+ ":=");
+ SupportsJoin(
+ formatter,
+ without: new[] { "FULL" },
+ additionally: new[]
+ {
+ "STRAIGHT_JOIN",
+ "NATURAL LEFT JOIN",
+ "NATURAL LEFT OUTER JOIN",
+ "NATURAL RIGHT JOIN",
+ "NATURAL RIGHT OUTER JOIN"
+ });
+
+ // supports # comments
+ string input = "SELECT a # comment\r\nFROM b # comment";
+ string expectedResult =
+@"SELECT
+ a # comment
+FROM
+ b # comment";
+ AssertFormat(formatter, input, expectedResult);
+
+ // supports @variables
+ input = "SELECT @foo, @bar";
+ expectedResult =
+@"SELECT
+ @foo,
+ @bar";
+ AssertFormat(formatter, input, expectedResult);
+
+ // supports setting variables: @var :=
+ input = "SET @foo := (SELECT * FROM tbl);";
+ expectedResult =
+@"SET
+ @foo := (
+ SELECT
+ *
+ FROM
+ tbl
+ );";
+ AssertFormat(formatter, input, expectedResult);
+ }
+
+ /// <summary>
+ /// Tests for standard -- and /* *\/ comments
+ /// </summary>
+ internal static void SupportsComments(Formatter formatter)
+ {
+ // formats SELECT query with different comments
+ string input = @"
+SELECT
+/*
+ * This is a block comment
+ */
+* FROM
+-- This is another comment
+MyTable -- One final comment
+WHERE 1 = 2;";
+
+ string expectedResult =
+@"SELECT
+ /*
+ * This is a block comment
+ */
+ *
+FROM
+ -- This is another comment
+ MyTable -- One final comment
+WHERE
+ 1 = 2;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // maintains block comment indentation
+ input =
+@"SELECT
+ /*
+ * This is a block comment
+ */
+ *
+FROM
+ MyTable
+WHERE
+ 1 = 2";
+
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats tricky line comments
+ input = "SELECT a--comment, here\r\nFROM b--comment";
+
+ expectedResult =
+@"SELECT
+ a --comment, here
+FROM
+ b --comment";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats line comments followed by semicolon
+ input = @"
+SELECT a FROM b
+--comment
+;";
+
+ expectedResult =
+@"SELECT
+ a
+FROM
+ b --comment
+;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats line comments followed by comma
+ input = @"
+SELECT a --comment
+, b";
+
+ expectedResult =
+@"SELECT
+ a --comment
+,
+ b";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats line comments followed by close-paren
+ input = "SELECT ( a --comment\r\n )";
+
+ expectedResult =
+@"SELECT
+ (a --comment
+)";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats line comments followed by open-paren
+ input = "SELECT a --comment\r\n()";
+
+ expectedResult =
+@"SELECT
+ a --comment
+ ()";
+ AssertFormat(formatter, input, expectedResult);
+
+ // recognizes line-comments with Windows line-endings (converts them to UNIX)
+ input = "SELECT * FROM\r\n-- line comment 1\r\nMyTable -- line comment 2\r\n";
+ expectedResult = "SELECT\r\n *\r\nFROM\r\n -- line comment 1\r\n MyTable -- line comment 2";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats query that ends with open comment
+ input = @"
+SELECT count(*)
+/*Comment";
+
+ expectedResult =
+@"SELECT
+ count(*)
+ /*Comment";
+ AssertFormat(formatter, input, expectedResult);
+ }
+
+ /// <summary>
+ /// Tests for all the config options
+ /// </summary>
+ internal static void SupportsConfigOptions(Formatter formatter)
+ {
+ // supports indent option
+ string input = @"SELECT count(*),Column1 FROM Table1;";
+
+ string expectedResult =
+@"SELECT
+ count(*),
+ Column1
+FROM
+ Table1;";
+
+ string output = formatter.Format(input, new SqlFormatterOptions(indentation: Indentation.FourSpaces, uppercase: false));
+ output.Should().Be(expectedResult);
+
+ // supports linesBetweenQueries option
+ input = @"SELECT * FROM foo; SELECT * FROM bar;";
+
+ expectedResult =
+@"SELECT
+ *
+FROM
+ foo;
+
+SELECT
+ *
+FROM
+ bar;";
+
+ output = formatter.Format(input, new SqlFormatterOptions(indentation: Indentation.TwoSpaces, uppercase: false, linesBetweenQueries: 2));
+ output.Should().Be(expectedResult);
+
+ // supports uppercase option
+ input = @"select distinct * frOM foo left join bar WHERe cola > 1 and colb = 3";
+
+ expectedResult =
+@"SELECT
+ DISTINCT *
+FROM
+ foo
+ LEFT JOIN bar
+WHERE
+ cola > 1
+ AND colb = 3";
+
+ output = formatter.Format(input, new SqlFormatterOptions(indentation: Indentation.TwoSpaces, uppercase: true));
+ output.Should().Be(expectedResult);
+
+ // supports indent option one tab
+ input = @"SELECT count(*),Column1 FROM Table1;";
+
+ expectedResult =
+string.Format(@"SELECT
+{0}count(*),
+{1}Column1
+FROM
+{2}Table1;", "\t", "\t", "\t");
+
+ output = formatter.Format(input, new SqlFormatterOptions(indentation: Indentation.OneTab, uppercase: false));
+ output.Should().Be(expectedResult);
+ }
+
+ /// <summary>
+ /// Tests support for various operators
+ /// </summary>
+ internal static void SupportsOperators(Formatter formatter, params string[] operators)
+ {
+ foreach (string op in operators)
+ {
+ string input = $"foo{op}bar";
+ string expectedResult = $"foo {op} bar";
+ AssertFormat(formatter, input, expectedResult);
+ }
+ }
+
+ /// <summary>
+ /// Tests support for CASE [WHEN...] END syntax
+ /// </summary>
+ internal static void SupportsCase(Formatter formatter)
+ {
+ // formats CASE ... WHEN with a blank expression
+ string input = "CASE WHEN option = 'foo' THEN 1 WHEN option = 'bar' THEN 2 WHEN option = 'baz' THEN 3 ELSE 4 END;";
+
+ string expectedResult =
+@"CASE
+ WHEN option = 'foo' THEN 1
+ WHEN option = 'bar' THEN 2
+ WHEN option = 'baz' THEN 3
+ ELSE 4
+END;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats CASE ... WHEN with an expression
+ input = "CASE toString(getNumber()) WHEN 'one' THEN 1 WHEN 'two' THEN 2 WHEN 'three' THEN 3 ELSE 4 END;";
+
+ expectedResult =
+@"CASE
+ toString(getNumber())
+ WHEN 'one' THEN 1
+ WHEN 'two' THEN 2
+ WHEN 'three' THEN 3
+ ELSE 4
+END;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats CASE ... WHEN inside SELECT
+ input = "SELECT foo, bar, CASE baz WHEN 'one' THEN 1 WHEN 'two' THEN 2 ELSE 3 END FROM table";
+
+ expectedResult =
+@"SELECT
+ foo,
+ bar,
+ CASE
+ baz
+ WHEN 'one' THEN 1
+ WHEN 'two' THEN 2
+ ELSE 3
+ END
+FROM
+ table";
+ AssertFormat(formatter, input, expectedResult);
+
+ // recognizes lowercase CASE ... END
+ input = "case when option = 'foo' then 1 else 2 end;";
+
+ expectedResult =
+@"case
+ when option = 'foo' then 1
+ else 2
+end;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // ignores words CASE and END inside other strings
+ input = "SELECT CASEDATE, ENDDATE FROM table1;";
+
+ expectedResult =
+@"SELECT
+ CASEDATE,
+ ENDDATE
+FROM
+ table1;";
+ AssertFormat(formatter, input, expectedResult);
+
+ // properly converts to uppercase in case statements
+ input = "case toString(getNumber()) when 'one' then 1 when 'two' then 2 when 'three' then 3 else 4 end;";
+
+ expectedResult =
+@"CASE
+ toString(getNumber())
+ WHEN 'one' THEN 1
+ WHEN 'two' THEN 2
+ WHEN 'three' THEN 3
+ ELSE 4
+END;";
+ string output = formatter.Format(input, new SqlFormatterOptions(indentation: Indentation.TwoSpaces, uppercase: true));
+ output.Should().Be(expectedResult);
+ }
+
+ /// <summary>
+ /// Tests support for CREATE TABLE syntax
+ /// </summary>
+ internal static void SupportsCreateTable(Formatter formatter)
+ {
+ // formats short CREATE TABLE
+ string input = "CREATE TABLE items (a INT PRIMARY KEY, b TEXT);";
+
+ string expectedResult =
+@"CREATE TABLE items (a INT PRIMARY KEY, b TEXT);";
+ AssertFormat(formatter, input, expectedResult);
+
+ // formats long CREATE TABLE
+ input = "CREATE TABLE items (a INT PRIMARY KEY, b TEXT, c INT NOT NULL, doggie INT NOT NULL);";
+
+ expectedResult =
+@"CREATE TABLE items (
+ a INT PRIMARY KEY,
+ b TEXT,
+ c INT NOT NULL,
+ doggie INT NOT NULL
+);";
+ AssertFormat(formatter, input, expectedResult);
+ }
+
+ /// <summary>
+ /// Tests support for ALTER TABLE syntax
+ /// </summary>
+ internal static void SupportsAlterTable(Formatter formatter)
+ {
+ // formats ALTER TABLE ... ALTER COLUMN query
+ string input = "ALTER TABLE supplier ALTER COLUMN supplier_name VARCHAR(100) NOT NULL;";
+
+ string expectedResult =
+@"ALTER TABLE
+ supplier
+ALTER COLUMN
+ supplier_name VARCHAR(100) NOT NULL;";
+ AssertFormat(formatter, input, expectedResult);
+ }
+
+ /// <summary>
+ /// Tests support for ALTER TABLE ... MODIFY syntax
+ /// </summary>
+ internal static void SupportsAlterTableModify(Formatter formatter)
+ {
+ // formats ALTER TABLE ... MODIFY statement
+ string input = "ALTER TABLE supplier MODIFY supplier_name char(100) NOT NULL;";
+
+ string expectedResult =
+@"ALTER TABLE
+ supplier
+MODIFY
+ supplier_name char(100) NOT NULL;";
+ AssertFormat(formatter, input, expectedResult);
+ }
+
+ /// <summary>
+ /// Tests support for various string syntax
+ /// </summary>
+ internal static void SupportsStrings(Formatter formatter, params string[] stringTypes)
+ {
+ string input;
+ string expectedResult;
+ if (stringTypes.Contains("\"\""))
+ {
+ // supports double-quoted strings
+ input = "\"foo JOIN bar\"";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+
+ input = "\"foo \\\" JOIN bar\"";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+ }
+
+ if (stringTypes.Contains("''"))
+ {
+ // supports single-quoted strings
+ input = "'foo JOIN bar'";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+
+ input = "'foo \\' JOIN bar'";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+ }
+
+ if (stringTypes.Contains("``"))
+ {
+ // supports backtick-quoted strings
+ input = "`foo JOIN bar`";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+
+ input = "`foo `` JOIN bar`";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+ }
+
+ if (stringTypes.Contains("U&\"\""))
+ {
+ // supports unicode double-quoted strings
+ input = "U&\"foo JOIN bar\"";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+
+ input = "U&\"foo \\\" JOIN bar\"";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+ }
+
+ if (stringTypes.Contains("U&''"))
+ {
+ // supports single-quoted strings
+ input = "U&'foo JOIN bar'";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+
+ input = "U&'foo \\' JOIN bar'";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+ }
+
+ if (stringTypes.Contains("$$"))
+ {
+ // supports dollar-quoted strings
+ input = "$xxx$foo $$ LEFT JOIN $yyy$ bar$xxx$";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+
+ input = "$$foo JOIN bar$$";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+
+ input = "$$foo $ JOIN bar$$";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+
+ input = "$$foo \r\n bar$$";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+ }
+
+ if (stringTypes.Contains("[]"))
+ {
+ // supports [bracket-quoted identifiers]
+ input = "[foo JOIN bar]";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+
+ input = "[foo ]] JOIN bar]";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+ }
+
+ if (stringTypes.Contains("N''"))
+ {
+ // supports T-SQL unicode strings
+ input = "N'foo JOIN bar'";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+
+ input = "N'foo \\' JOIN bar'";
+ expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+ }
+ }
+
+ /// <summary>
+ /// Tests support for BETWEEN _ AND _ syntax
+ /// </summary>
+ internal static void SupportsBetween(Formatter formatter)
+ {
+ // formats BETWEEN _ AND _ on single line
+ string input = "foo BETWEEN bar AND baz";
+ string expectedResult = input;
+ AssertFormat(formatter, input, expectedResult);
+ }
+
+ /// <summary>
+ /// Tests support for SET SCHEMA syntax
+ /// </summary>
+ internal static void SupportsSchema(Formatter formatter)
+ {
+ // formats simple SET SCHEMA statements
+ string input = "SET SCHEMA schema1;";
+ string expectedResult =
+@"SET SCHEMA
+ schema1;";
+ AssertFormat(formatter, input, expectedResult);
+ }
+
+ /// <summary>
+ /// Tests support for various joins
+ /// </summary>
+ internal static void SupportsJoin(Formatter formatter, string[] without = null, string[] additionally = null)
+ {
+ Regex unsupportedJoinRegex
+ = without != null
+ ? new Regex(string.Join("|", without), RegexOptions.Compiled)
+ : new Regex(@"^whateve_!%&$");
+
+ bool isSupportedJoin(string join) => !unsupportedJoinRegex.Match(join).Success;
+
+ string[] joins = new[] { "CROSS JOIN", "NATURAL JOIN" };
+ foreach (string join in joins)
+ {
+ if (isSupportedJoin(join))
+ {
+ string input = $"SELECT * FROM tbl1 {join} tbl2";
+ string expectedResult =
+$@"SELECT
+ *
+FROM
+ tbl1
+ {join} tbl2";
+ AssertFormat(formatter, input, expectedResult);
+ }
+ }
+
+ // <join> ::= [ <join type> ] JOIN
+ //
+ // <join type> ::= INNER | <outer join type> [ OUTER ]
+ //
+ // <outer join type> ::= LEFT | RIGHT | FULL
+
+ joins = new[]
+ {
+ "JOIN",
+ "INNER JOIN",
+ "LEFT JOIN",
+ "LEFT OUTER JOIN",
+ "RIGHT JOIN",
+ "RIGHT OUTER JOIN",
+ "FULL JOIN",
+ "FULL OUTER JOIN"
+ };
+
+ if (additionally != null)
+ joins = joins.Concat(additionally).ToArray();
+
+ foreach (string join in joins)
+ {
+ if (isSupportedJoin(join))
+ {
+ string input =
+$@"SELECT customer_id.from, COUNT(order_id) AS total FROM customers
+{join} orders ON customers.customer_id = orders.customer_id;";
+ string expectedResult =
+$@"SELECT
+ customer_id.from,
+ COUNT(order_id) AS total
+FROM
+ customers
+ {join} orders ON customers.customer_id = orders.customer_id;";
+ AssertFormat(formatter, input, expectedResult);
+ }
+ }
+ }
+
+ internal static void AssertFormat(Formatter formatter, string input, string expectedOutput)
+ {
+ string output = formatter.Format(input);
+ output.Should().Be(expectedOutput);
+ }
+}
diff --git a/src/app/tests/DevToys.UnitTests/Tools/Formatters/Sql/SqlFormatterTests.cs b/src/app/tests/DevToys.UnitTests/Tools/Formatters/Sql/SqlFormatterTests.cs
new file mode 100644
index 000000000..a049b5ea0
--- /dev/null
+++ b/src/app/tests/DevToys.UnitTests/Tools/Formatters/Sql/SqlFormatterTests.cs
@@ -0,0 +1,677 @@
+using DevToys.Tools.Helpers.SqlFormatter.Languages;
+
+namespace DevToys.UnitTests.Tools.Formatters.Sql;
+
+[Collection(nameof(TestParallelizationDisabled))]
+public class SqlFormatterTests : MefBasedTest
+{
+ [Fact]
+ public void StandardSqlFormatterTest()
+ {
+ var formatter = new StandardSqlFormatter();
+ SqlFormatterTestHelpers.BehavesLikeSqlFormatter(formatter);
+ SqlFormatterTestHelpers.SupportsCase(formatter);
+ SqlFormatterTestHelpers.SupportsCreateTable(formatter);
+ SqlFormatterTestHelpers.SupportsAlterTable(formatter);
+ SqlFormatterTestHelpers.SupportsStrings(formatter, "\"\"", "''");
+ SqlFormatterTestHelpers.SupportsBetween(formatter);
+ SqlFormatterTestHelpers.SupportsSchema(formatter);
+ SqlFormatterTestHelpers.SupportsJoin(formatter);
+
+ // formats FETCH FIRST like LIMIT
+ string input = "SELECT * FETCH FIRST 2 ROWS ONLY;";
+ string expectedResult =
+@"SELECT
+ *
+FETCH FIRST
+ 2 ROWS ONLY;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+ }
+
+ [Fact]
+ public void TSqlFormatterTest()
+ {
+ var formatter = new TSqlFormatter();
+ SqlFormatterTestHelpers.BehavesLikeSqlFormatter(formatter);
+ SqlFormatterTestHelpers.SupportsCase(formatter);
+ SqlFormatterTestHelpers.SupportsCreateTable(formatter);
+ SqlFormatterTestHelpers.SupportsAlterTable(formatter);
+ SqlFormatterTestHelpers.SupportsStrings(formatter, "\"\"", "''", "N''", "[]");
+ SqlFormatterTestHelpers.SupportsBetween(formatter);
+ SqlFormatterTestHelpers.SupportsSchema(formatter);
+ SqlFormatterTestHelpers.SupportsOperators(formatter, "%", "&", "|", "^", "~", "!=", "!<", "!>", "+=", "-=", "*=", "/=", "%=", "|=", "&=", "^=", "::");
+ SqlFormatterTestHelpers.SupportsJoin(formatter, without: new[] { "NATURAL" });
+
+ // formats INSERT without INTO
+ string input = "INSERT Customers (ID, MoneyBalance, Address, City) VALUES (12,-123.4, 'Skagen 2111','Stv');";
+ string expectedResult =
+@"INSERT
+ Customers (ID, MoneyBalance, Address, City)
+VALUES
+ (12, -123.4, 'Skagen 2111', 'Stv');";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // recognizes @variables
+ input = "SELECT @variable;";
+ expectedResult = "SELECT\r\n @variable;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats SELECT query with CROSS JOIN
+ input = "SELECT a, b FROM t CROSS JOIN t2 on t.id = t2.id_t";
+ expectedResult =
+@"SELECT
+ a,
+ b
+FROM
+ t
+ CROSS JOIN t2 on t.id = t2.id_t";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+ }
+
+ [Fact]
+ public void SparkSqlFormatterTest()
+ {
+ var formatter = new SparkSqlFormatter();
+ SqlFormatterTestHelpers.BehavesLikeSqlFormatter(formatter);
+ SqlFormatterTestHelpers.SupportsCase(formatter);
+ SqlFormatterTestHelpers.SupportsCreateTable(formatter);
+ SqlFormatterTestHelpers.SupportsAlterTable(formatter);
+ SqlFormatterTestHelpers.SupportsStrings(formatter, "\"\"", "''", "``");
+ SqlFormatterTestHelpers.SupportsBetween(formatter);
+ SqlFormatterTestHelpers.SupportsSchema(formatter);
+ SqlFormatterTestHelpers.SupportsOperators(formatter, "!=", "%", "|", "&", "^", "~", "!", "<=>", "%", "&&", "||", "==");
+ SqlFormatterTestHelpers.SupportsJoin(
+ formatter,
+ additionally: new[]
+ {
+ "ANTI JOIN",
+ "SEMI JOIN",
+ "LEFT ANTI JOIN",
+ "LEFT SEMI JOIN",
+ "RIGHT OUTER JOIN",
+ "RIGHT SEMI JOIN",
+ "NATURAL ANTI JOIN",
+ "NATURAL FULL OUTER JOIN",
+ "NATURAL INNER JOIN",
+ "NATURAL LEFT ANTI JOIN",
+ "NATURAL LEFT OUTER JOIN",
+ "NATURAL LEFT SEMI JOIN",
+ "NATURAL OUTER JOIN",
+ "NATURAL RIGHT OUTER JOIN",
+ "NATURAL RIGHT SEMI JOIN",
+ "NATURAL SEMI JOIN"
+ });
+
+ // formats WINDOW specification as top level
+ string input = "SELECT *, LAG(value) OVER wnd AS next_value FROM tbl WINDOW wnd as (PARTITION BY id ORDER BY time);";
+ string expectedResult =
+@"SELECT
+ *,
+ LAG(value) OVER wnd AS next_value
+FROM
+ tbl
+WINDOW
+ wnd as (
+ PARTITION BY
+ id
+ ORDER BY
+ time
+ );";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats window function and end as inline
+ input = "SELECT window(time, \"1 hour\").start AS window_start, window(time, \"1 hour\").end AS window_end FROM tbl;";
+ expectedResult = "SELECT\r\n window(time, \"1 hour\").start AS window_start,\r\n window(time, \"1 hour\").end AS window_end\r\nFROM\r\n tbl;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+ }
+
+ [Fact]
+ public void RedshiftFormatterTest()
+ {
+ var formatter = new RedshiftFormatter();
+ SqlFormatterTestHelpers.BehavesLikeSqlFormatter(formatter);
+ SqlFormatterTestHelpers.SupportsCreateTable(formatter);
+ SqlFormatterTestHelpers.SupportsAlterTable(formatter);
+ SqlFormatterTestHelpers.SupportsAlterTableModify(formatter);
+ SqlFormatterTestHelpers.SupportsStrings(formatter, "\"\"", "''", "``");
+ SqlFormatterTestHelpers.SupportsSchema(formatter);
+ SqlFormatterTestHelpers.SupportsOperators(formatter, "%", "^", "|/", "||/", "<<", ">>", "&", "|", "~", "!", "!=", "||");
+ SqlFormatterTestHelpers.SupportsJoin(formatter);
+
+ // formats LIMIT
+ string input = "SELECT col1 FROM tbl ORDER BY col2 DESC LIMIT 10;";
+ string expectedResult =
+@"SELECT
+ col1
+FROM
+ tbl
+ORDER BY
+ col2 DESC
+LIMIT
+ 10;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats only -- as a line comment
+ input =
+@"SELECT col FROM
+-- This is a comment
+MyTable;";
+ expectedResult =
+@"SELECT
+ col
+FROM
+ -- This is a comment
+ MyTable;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // recognizes @ as part of identifiers
+ input = @"SELECT @col1 FROM tbl";
+ expectedResult =
+@"SELECT
+ @col1
+FROM
+ tbl";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats DISTKEY and SORTKEY after CREATE TABLE
+ input = @"CREATE TABLE items (a INT PRIMARY KEY, b TEXT, c INT NOT NULL, d INT NOT NULL) DISTKEY(created_at) SORTKEY(created_at);";
+ expectedResult =
+@"CREATE TABLE items (a INT PRIMARY KEY, b TEXT, c INT NOT NULL, d INT NOT NULL)
+DISTKEY
+(created_at)
+SORTKEY
+(created_at);";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats COPY
+ input =
+@"COPY schema.table
+FROM 's3://bucket/file.csv'
+IAM_ROLE 'arn:aws:iam::123456789:role/rolename'
+FORMAT AS CSV DELIMITER ',' QUOTE ''
+REGION AS 'us-east-1'";
+ expectedResult =
+@"COPY
+ schema.table
+FROM
+ 's3://bucket/file.csv'
+IAM_ROLE
+ 'arn:aws:iam::123456789:role/rolename'
+FORMAT
+ AS CSV
+DELIMITER
+ ',' QUOTE ''
+REGION
+ AS 'us-east-1'";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+ }
+
+ [Fact]
+ public void PostgreSqlFormatterTest()
+ {
+ var formatter = new PostgreSqlFormatter();
+ SqlFormatterTestHelpers.BehavesLikeSqlFormatter(formatter);
+ SqlFormatterTestHelpers.SupportsCase(formatter);
+ SqlFormatterTestHelpers.SupportsCreateTable(formatter);
+ SqlFormatterTestHelpers.SupportsAlterTable(formatter);
+ SqlFormatterTestHelpers.SupportsStrings(formatter, "\"\"", "''", "U&\"\"", "U&''", "$$");
+ SqlFormatterTestHelpers.SupportsBetween(formatter);
+ SqlFormatterTestHelpers.SupportsSchema(formatter);
+ SqlFormatterTestHelpers.SupportsOperators(
+ formatter,
+ "%",
+ "^",
+ "!",
+ "!!",
+ "@",
+ "!=",
+ "&",
+ "|",
+ "~",
+ "#",
+ "<<",
+ ">>",
+ "||/",
+ "|/",
+ "::",
+ "->>",
+ "->",
+ "~~*",
+ "~~",
+ "!~~*",
+ "!~~",
+ "~*",
+ "!~*",
+ "!~");
+ SqlFormatterTestHelpers.SupportsJoin(formatter);
+
+ // supports $n placeholders
+ string input = "SELECT $1, $2 FROM tbl";
+ string expectedResult =
+@"SELECT
+ $1,
+ $2
+FROM
+ tbl";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // supports :name placeholders
+ input = "foo = :bar";
+ expectedResult = input;
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+ }
+
+ [Fact]
+ public void PlSqlFormatterTest()
+ {
+ var formatter = new PlSqlFormatter();
+ SqlFormatterTestHelpers.BehavesLikeSqlFormatter(formatter);
+ SqlFormatterTestHelpers.SupportsCase(formatter);
+ SqlFormatterTestHelpers.SupportsCreateTable(formatter);
+ SqlFormatterTestHelpers.SupportsAlterTable(formatter);
+ SqlFormatterTestHelpers.SupportsAlterTableModify(formatter);
+ SqlFormatterTestHelpers.SupportsStrings(formatter, "\"\"", "''", "``");
+ SqlFormatterTestHelpers.SupportsBetween(formatter);
+ SqlFormatterTestHelpers.SupportsSchema(formatter);
+ SqlFormatterTestHelpers.SupportsOperators(formatter, "||", "**", "!=", ":=");
+ SqlFormatterTestHelpers.SupportsJoin(formatter);
+
+ // formats FETCH FIRST like LIMIT
+ string input = "SELECT col1 FROM tbl ORDER BY col2 DESC FETCH FIRST 20 ROWS ONLY;";
+ string expectedResult =
+@"SELECT
+ col1
+FROM
+ tbl
+ORDER BY
+ col2 DESC
+FETCH FIRST
+ 20 ROWS ONLY;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats only -- as a line comment
+ input = "SELECT col FROM\r\n-- This is a comment\r\nMyTable;\r\n";
+ expectedResult =
+@"SELECT
+ col
+FROM
+ -- This is a comment
+ MyTable;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // recognizes _, $, #, . and @ as part of identifiers
+ input = "SELECT my_col$1#, col.2@ FROM tbl\r\n";
+ expectedResult =
+@"SELECT
+ my_col$1#,
+ col.2@
+FROM
+ tbl";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats INSERT without INTO
+ input = "INSERT Customers (ID, MoneyBalance, Address, City) VALUES (12,-123.4, 'Skagen 2111','Stv');";
+ expectedResult =
+@"INSERT
+ Customers (ID, MoneyBalance, Address, City)
+VALUES
+ (12, -123.4, 'Skagen 2111', 'Stv');";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // recognizes ?[0-9]* placeholders
+ input = "SELECT ?1, ?25, ?;";
+ expectedResult =
+@"SELECT
+ ?1,
+ ?25,
+ ?;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats SELECT query with CROSS APPLY
+ input = "SELECT a, b FROM t CROSS APPLY fn(t.id)";
+ expectedResult =
+@"SELECT
+ a,
+ b
+FROM
+ t
+ CROSS APPLY fn(t.id)";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats simple SELECT
+ input = "SELECT N, M FROM t";
+ expectedResult =
+@"SELECT
+ N,
+ M
+FROM
+ t";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats simple SELECT with national characters
+ input = "SELECT N'value'";
+ expectedResult =
+@"SELECT
+ N'value'";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats SELECT query with OUTER APPLY
+ input = "SELECT a, b FROM t OUTER APPLY fn(t.id)";
+ expectedResult =
+@"SELECT
+ a,
+ b
+FROM
+ t
+ OUTER APPLY fn(t.id)";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats Oracle recursive sub queries
+ input =
+@"WITH t1(id, parent_id) AS (
+ -- Anchor member.
+ SELECT
+ id,
+ parent_id
+ FROM
+ tab1
+ WHERE
+ parent_id IS NULL
+ MINUS
+ -- Recursive member.
+ SELECT
+ t2.id,
+ t2.parent_id
+ FROM
+ tab1 t2,
+ t1
+ WHERE
+ t2.parent_id = t1.id
+) SEARCH BREADTH FIRST BY id SET order1,
+another AS (SELECT * FROM dual)
+SELECT id, parent_id FROM t1 ORDER BY order1;";
+ expectedResult =
+@"WITH t1(id, parent_id) AS (
+ -- Anchor member.
+ SELECT
+ id,
+ parent_id
+ FROM
+ tab1
+ WHERE
+ parent_id IS NULL
+ MINUS
+ -- Recursive member.
+ SELECT
+ t2.id,
+ t2.parent_id
+ FROM
+ tab1 t2,
+ t1
+ WHERE
+ t2.parent_id = t1.id
+) SEARCH BREADTH FIRST BY id SET order1,
+another AS (
+ SELECT
+ *
+ FROM
+ dual
+)
+SELECT
+ id,
+ parent_id
+FROM
+ t1
+ORDER BY
+ order1;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats Oracle recursive sub queries regardless of capitalization
+ input =
+@"WITH t1(id, parent_id) AS (
+ -- Anchor member.
+ SELECT
+ id,
+ parent_id
+ FROM
+ tab1
+ WHERE
+ parent_id IS NULL
+ MINUS
+ -- Recursive member.
+ SELECT
+ t2.id,
+ t2.parent_id
+ FROM
+ tab1 t2,
+ t1
+ WHERE
+ t2.parent_id = t1.id
+) SEARCH BREADTH FIRST by id set order1,
+another AS (SELECT * FROM dual)
+SELECT id, parent_id FROM t1 ORDER BY order1;";
+ expectedResult =
+@"WITH t1(id, parent_id) AS (
+ -- Anchor member.
+ SELECT
+ id,
+ parent_id
+ FROM
+ tab1
+ WHERE
+ parent_id IS NULL
+ MINUS
+ -- Recursive member.
+ SELECT
+ t2.id,
+ t2.parent_id
+ FROM
+ tab1 t2,
+ t1
+ WHERE
+ t2.parent_id = t1.id
+) SEARCH BREADTH FIRST by id set order1,
+another AS (
+ SELECT
+ *
+ FROM
+ dual
+)
+SELECT
+ id,
+ parent_id
+FROM
+ t1
+ORDER BY
+ order1;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+ }
+
+ [Fact]
+ public void N1qlFormatterTest()
+ {
+ var formatter = new N1qlFormatter();
+ SqlFormatterTestHelpers.BehavesLikeSqlFormatter(formatter);
+ SqlFormatterTestHelpers.SupportsStrings(formatter, "\"\"", "''", "``");
+ SqlFormatterTestHelpers.SupportsBetween(formatter);
+ SqlFormatterTestHelpers.SupportsSchema(formatter);
+ SqlFormatterTestHelpers.SupportsOperators(formatter, "%", "==", "!=");
+ SqlFormatterTestHelpers.SupportsJoin(formatter, without: new[] { "FULL", "CROSS", "NATURAL" });
+
+ // formats SELECT query with element selection expression
+ string input = "SELECT order_lines[0].productId FROM orders;";
+ string expectedResult =
+@"SELECT
+ order_lines[0].productId
+FROM
+ orders;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats SELECT query with primary key querying
+ input = "SELECT fname, email FROM tutorial USE KEYS ['dave', 'ian'];";
+ expectedResult =
+@"SELECT
+ fname,
+ email
+FROM
+ tutorial
+USE KEYS
+ ['dave', 'ian'];";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats INSERT with {} object literal
+ input = "INSERT INTO heroes (KEY, VALUE) VALUES ('123', {'id':1,'type':'Tarzan'});";
+ expectedResult =
+@"INSERT INTO
+ heroes (KEY, VALUE)
+VALUES
+ ('123', {'id': 1, 'type': 'Tarzan'});";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats INSERT with large object and array literals
+ input = "INSERT INTO heroes (KEY, VALUE) VALUES ('123', {'id': 1, 'type': 'Tarzan', 'array': [123456789, 123456789, 123456789, 123456789, 123456789], 'hello': 'world'});";
+ expectedResult =
+@"INSERT INTO
+ heroes (KEY, VALUE)
+VALUES
+ (
+ '123',
+ {
+ 'id': 1,
+ 'type': 'Tarzan',
+ 'array': [
+ 123456789,
+ 123456789,
+ 123456789,
+ 123456789,
+ 123456789
+ ],
+ 'hello': 'world'
+ }
+ );";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats SELECT query with UNNEST top level reserver word
+ input = "SELECT * FROM tutorial UNNEST tutorial.children c;";
+ expectedResult =
+@"SELECT
+ *
+FROM
+ tutorial
+UNNEST
+ tutorial.children c;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats SELECT query with NEST and USE KEYS
+ input =
+@"SELECT * FROM usr
+USE KEYS 'Elinor_33313792' NEST orders_with_users orders
+ON KEYS ARRAY s.order_id FOR s IN usr.shipped_order_history END;";
+ expectedResult =
+@"SELECT
+ *
+FROM
+ usr
+USE KEYS
+ 'Elinor_33313792'
+NEST
+ orders_with_users orders ON KEYS ARRAY s.order_id FOR s IN usr.shipped_order_history END;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats explained DELETE query with USE KEYS and RETURNING
+ input = "EXPLAIN DELETE FROM tutorial t USE KEYS 'baldwin' RETURNING t";
+ expectedResult =
+@"EXPLAIN DELETE FROM
+ tutorial t
+USE KEYS
+ 'baldwin' RETURNING t";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats UPDATE query with USE KEYS and RETURNING
+ input = "UPDATE tutorial USE KEYS 'baldwin' SET type = 'actor' RETURNING tutorial.type";
+ expectedResult =
+@"UPDATE
+ tutorial
+USE KEYS
+ 'baldwin'
+SET
+ type = 'actor' RETURNING tutorial.type";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // recognizes $variables
+ input = "SELECT $variable;";
+ expectedResult = "SELECT\r\n $variable;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+ }
+
+ [Fact]
+ public void MySqlFormatterTest()
+ {
+ var formatter = new MySqlFormatter();
+ SqlFormatterTestHelpers.BehavesLikeMariaDbFormatter(formatter);
+ SqlFormatterTestHelpers.SupportsOperators(formatter, "->", "->>");
+ }
+
+ [Fact]
+ public void MariaDbFormatterTest()
+ {
+ var formatter = new MariaDbFormatter();
+ SqlFormatterTestHelpers.BehavesLikeMariaDbFormatter(formatter);
+ }
+
+ [Fact]
+ public void Db2FormatterTest()
+ {
+ var formatter = new Db2Formatter();
+ SqlFormatterTestHelpers.BehavesLikeSqlFormatter(formatter);
+ SqlFormatterTestHelpers.SupportsCreateTable(formatter);
+ SqlFormatterTestHelpers.SupportsAlterTable(formatter);
+ SqlFormatterTestHelpers.SupportsStrings(formatter, "\"\"", "''", "``");
+ SqlFormatterTestHelpers.SupportsBetween(formatter);
+ SqlFormatterTestHelpers.SupportsSchema(formatter);
+ SqlFormatterTestHelpers.SupportsOperators(formatter, "%", "**", "!=", "!>", "!>", "||");
+
+ // formats FETCH FIRST like LIMIT
+ string input = "SELECT col1 FROM tbl ORDER BY col2 DESC FETCH FIRST 20 ROWS ONLY;";
+ string expectedResult =
+@"SELECT
+ col1
+FROM
+ tbl
+ORDER BY
+ col2 DESC
+FETCH FIRST
+ 20 ROWS ONLY;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // formats only -- as a line comment
+ input =
+@"SELECT col FROM
+-- This is a comment
+MyTable;";
+ expectedResult =
+@"SELECT
+ col
+FROM
+ -- This is a comment
+ MyTable;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // recognizes @ and # as part of identifiers
+ input = "SELECT col#1, @col2 FROM tbl";
+ expectedResult =
+@"SELECT
+ col#1,
+ @col2
+FROM
+ tbl";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+
+ // recognizes :variables
+ input = "SELECT :variable;";
+ expectedResult =
+@"SELECT
+ :variable;";
+ SqlFormatterTestHelpers.AssertFormat(formatter, input, expectedResult);
+ }
+}
diff --git a/src/app/tests/DevToys.UnitTests/Tools/Formatters/Xml/XmlFormatterCommandLineToolTests.cs b/src/app/tests/DevToys.UnitTests/Tools/Formatters/Xml/XmlFormatterCommandLineToolTests.cs
index 1611e04a0..b0fb7ec3c 100644
--- a/src/app/tests/DevToys.UnitTests/Tools/Formatters/Xml/XmlFormatterCommandLineToolTests.cs
+++ b/src/app/tests/DevToys.UnitTests/Tools/Formatters/Xml/XmlFormatterCommandLineToolTests.cs
@@ -3,7 +3,6 @@ using System.Threading.Tasks;
using DevToys.CLI;
using DevToys.Tools.Models;
using DevToys.Tools.Tools.Formatters.Xml;
-using DevToys.UnitTests.Mocks;
using Microsoft.Extensions.Logging;
namespace DevToys.UnitTests.Tools.Formatters;