Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ You can also pass `FormatConfig` object built by builder:
SqlFormatter.Format("SELECT * FROM tbl",
FormatConfig.Builder()
.Indent(" ") // Defaults to two spaces
.Uppercase(true) // Defaults to false (not safe to use when SQL dialect has case-sensitive identifiers)
.Case(CaseTypes.UPPER) // Defaults to NONE (not safe to use when SQL dialect has case-sensitive identifiers)
.LinesBetweenQueries(2) // Defaults to 1
.MaxColumnLength(100) // Defaults to 50
.Params(new List<string>{"a", "b", "c"}) // Dictionary or List. See Placeholders replacement.
Expand Down
175 changes: 175 additions & 0 deletions SQL.Formatter.Test/FormatConfigTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
using SQL.Formatter.Core;
using SQL.Formatter.Language;
using Xunit;

namespace SQL.Formatter.Test
{
public class FormatConfigTest
{
private static readonly SqlFormatter.Formatter s_formatter = SqlFormatter.Of(Dialect.StandardSql);

[Fact]
public void Upper_FormatsKeywordsInUppercase()
{
var cfg = FormatConfig.Builder().Case(CaseTypes.UPPER).Build();
var result = s_formatter.Format("select id from users where id = 1", cfg);

Assert.Equal(
"SELECT\n"
+ " id\n"
+ "FROM\n"
+ " users\n"
+ "WHERE\n"
+ " id = 1",
result);
}

[Fact]
public void Uppercase_FormatsKeywordsInUppercase()
{
#pragma warning disable CS0618
var cfg = FormatConfig.Builder().Uppercase(true).Build();
#pragma warning restore CS0618
var result = s_formatter.Format("select id from users where id = 1", cfg);

Assert.Equal(
"SELECT\n"
+ " id\n"
+ "FROM\n"
+ " users\n"
+ "WHERE\n"
+ " id = 1",
result);
}

[Fact]
public void Uppercase_False_PreservesKeywordCase()
{
#pragma warning disable CS0618
var cfg = FormatConfig.Builder().Uppercase(false).Build();
#pragma warning restore CS0618
var result = s_formatter.Format("select id FROM users", cfg);

Assert.Equal(
"select\n"
+ " id\n"
+ "FROM\n"
+ " users",
result);
}

[Fact]
public void Lower_FormatsKeywordsInLowercase()
{
var cfg = FormatConfig.Builder().Case(CaseTypes.LOWER).Build();
var result = s_formatter.Format("SELECT id FROM Users WHERE id = 1", cfg);

Assert.Equal(
"select\n"
+ " id\n"
+ "from\n"
+ " Users\n"
+ "where\n"
+ " id = 1",
result);
}

[Fact]
public void Upper_ClearsLower()
{
var cfg = FormatConfig.Builder()
.Case(CaseTypes.LOWER)
.Case(CaseTypes.UPPER)
.Build();

Assert.Equal(CaseTypes.UPPER, cfg.Case);
}

[Fact]
public void Lower_ClearsUpper()
{
var cfg = FormatConfig.Builder()
.Case(CaseTypes.UPPER)
.Case(CaseTypes.LOWER)
.Build();

Assert.Equal(CaseTypes.LOWER, cfg.Case);
}

[Fact]
public void Upper_ClearsLower_FormatsUppercase()
{
var cfg = FormatConfig.Builder()
.Case(CaseTypes.LOWER)
.Case(CaseTypes.UPPER)
.Build();

var result = s_formatter.Format("select id from users", cfg);

Assert.Equal(
"SELECT\n"
+ " id\n"
+ "FROM\n"
+ " users",
result);
}

[Fact]
public void Lower_ClearsUpper_FormatsLowercase()
{
var cfg = FormatConfig.Builder()
.Case(CaseTypes.UPPER)
.Case(CaseTypes.LOWER)
.Build();

var result = s_formatter.Format("SELECT id FROM users", cfg);

Assert.Equal(
"select\n"
+ " id\n"
+ "from\n"
+ " users",
result);
}

[Fact]
public void NeitherUppercaseNorLowercase_PreservesKeywordCase()
{
var cfg = FormatConfig.Builder().Build();
var result = s_formatter.Format("SeLeCt id FrOm users", cfg);

Assert.Equal(
"SeLeCt\n"
+ " id\n"
+ "FrOm\n"
+ " users",
result);
}

[Fact]
public void None_PreservesKeywordCase()
{
var cfg = FormatConfig.Builder().Case(CaseTypes.NONE).Build();
var result = s_formatter.Format("SeLeCt id FrOm users", cfg);

Assert.Equal(
"SeLeCt\n"
+ " id\n"
+ "FrOm\n"
+ " users",
result);
}

[Theory]
[InlineData(CaseTypes.NONE, false)]
[InlineData(CaseTypes.UPPER, true)]
[InlineData(CaseTypes.LOWER, false)]
public void Case_SetsUppercaseCorrectly(CaseTypes caseType, bool expectedUppercase)
{
var cfg = FormatConfig.Builder().Case(caseType).Build();
#pragma warning disable CS0618
Assert.Equal(expectedUppercase, cfg.Uppercase);
#pragma warning restore CS0618
}
}
}
4 changes: 2 additions & 2 deletions SQL.Formatter/Core/AbstractFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ protected virtual void FormatQuerySeparator(Token token, StringBuilder query)

protected virtual string Show(Token token)
{
if (_cfg.Uppercase
if (_cfg.Case > CaseTypes.NONE
&& (token.Type == TokenTypes.RESERVED
|| token.Type == TokenTypes.RESERVED_TOP_LEVEL
|| token.Type == TokenTypes.RESERVED_TOP_LEVEL_NO_INDENT
Expand All @@ -291,7 +291,7 @@ protected virtual string Show(Token token)
|| token.Type == TokenTypes.CLOSE_PAREN))
{
// Note: If memory is still tight, caching upper-case values at the token generation stage is even better.
return token.Value.ToUpper();
return _cfg.Case == CaseTypes.UPPER ? token.Value.ToUpper() : token.Value.ToLower();
}

return token.Value;
Expand Down
9 changes: 9 additions & 0 deletions SQL.Formatter/Core/CaseTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SQL.Formatter.Core
{
public enum CaseTypes
{
NONE,
UPPER,
LOWER,
}
}
42 changes: 37 additions & 5 deletions SQL.Formatter/Core/FormatConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace SQL.Formatter.Core
{
Expand All @@ -10,22 +11,45 @@ public class FormatConfig
public readonly string Indent;
public readonly int MaxColumnLength;
public readonly Params Parameters;
[Obsolete("Use Case instead of Uppercase")]
public readonly bool Uppercase;
public readonly CaseTypes Case;
public readonly int LinesBetweenQueries;
public readonly bool SkipWhitespaceNearBlockParentheses;

[Obsolete("Use the constructor with caseType instead of uppercase")]
public FormatConfig(
string indent,
int maxColumnLength,
Params parameters,
bool uppercase,
int linesBetweenQueries,
bool skipWhitespaceNearBlockParentheses)
: this(
indent,
maxColumnLength,
parameters,
uppercase ? CaseTypes.UPPER : CaseTypes.NONE,
linesBetweenQueries,
skipWhitespaceNearBlockParentheses)
{
}

public FormatConfig(
string indent,
int maxColumnLength,
Params parameters,
CaseTypes caseType,
int linesBetweenQueries,
bool skipWhitespaceNearBlockParentheses)
{
Indent = indent;
MaxColumnLength = maxColumnLength;
Parameters = parameters == null ? Params.Empty : parameters;
Uppercase = uppercase;
Case = caseType;
#pragma warning disable CS0618
Uppercase = caseType == CaseTypes.UPPER;
#pragma warning restore CS0618
LinesBetweenQueries = linesBetweenQueries;
SkipWhitespaceNearBlockParentheses = skipWhitespaceNearBlockParentheses;
}
Expand All @@ -40,7 +64,7 @@ public class FormatConfigBuilder
private string _indent = DefaultIndent;
private int _maxColumnLength = DefaultColumnMaxLength;
private Params _parameters;
private bool _uppercase;
private CaseTypes _case;
private int _linesBetweenQueries;
private bool _skipWhitespaceNearBlockParentheses;

Expand Down Expand Up @@ -76,9 +100,17 @@ public FormatConfigBuilder Params<T>(List<T> parameters)
return Params(Core.Params.Of(parameters));
}

[Obsolete("Use Case instead of Uppercase")]
public FormatConfigBuilder Uppercase(bool uppercase)
{
_uppercase = uppercase;
_case = uppercase ? CaseTypes.UPPER : CaseTypes.NONE;

return this;
}

public FormatConfigBuilder Case(CaseTypes caseType)
{
_case = caseType;
return this;
}

Expand All @@ -100,7 +132,7 @@ public FormatConfig Build()
_indent,
_maxColumnLength,
_parameters,
_uppercase,
_case,
_linesBetweenQueries,
_skipWhitespaceNearBlockParentheses);
}
Expand Down
Loading