Skip to content

Commit 9fcd372

Browse files
asherberhogimn
andauthored
Add Case property to FormatConfig (#2)
* Add Case property to FormatConfig * Update README * Lint and minor code cleanup --------- Co-authored-by: Hoki Min <hogimn@gmail.com>
1 parent e6bcc48 commit 9fcd372

5 files changed

Lines changed: 224 additions & 8 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ You can also pass `FormatConfig` object built by builder:
4646
SqlFormatter.Format("SELECT * FROM tbl",
4747
FormatConfig.Builder()
4848
.Indent(" ") // Defaults to two spaces
49-
.Uppercase(true) // Defaults to false (not safe to use when SQL dialect has case-sensitive identifiers)
49+
.Case(CaseTypes.UPPER) // Defaults to NONE (not safe to use when SQL dialect has case-sensitive identifiers)
5050
.LinesBetweenQueries(2) // Defaults to 1
5151
.MaxColumnLength(100) // Defaults to 50
5252
.Params(new List<string>{"a", "b", "c"}) // Dictionary or List. See Placeholders replacement.
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
using SQL.Formatter.Core;
2+
using SQL.Formatter.Language;
3+
using Xunit;
4+
5+
namespace SQL.Formatter.Test
6+
{
7+
public class FormatConfigTest
8+
{
9+
private static readonly SqlFormatter.Formatter s_formatter = SqlFormatter.Of(Dialect.StandardSql);
10+
11+
[Fact]
12+
public void Upper_FormatsKeywordsInUppercase()
13+
{
14+
var cfg = FormatConfig.Builder().Case(CaseTypes.UPPER).Build();
15+
var result = s_formatter.Format("select id from users where id = 1", cfg);
16+
17+
Assert.Equal(
18+
"SELECT\n"
19+
+ " id\n"
20+
+ "FROM\n"
21+
+ " users\n"
22+
+ "WHERE\n"
23+
+ " id = 1",
24+
result);
25+
}
26+
27+
[Fact]
28+
public void Uppercase_FormatsKeywordsInUppercase()
29+
{
30+
#pragma warning disable CS0618
31+
var cfg = FormatConfig.Builder().Uppercase(true).Build();
32+
#pragma warning restore CS0618
33+
var result = s_formatter.Format("select id from users where id = 1", cfg);
34+
35+
Assert.Equal(
36+
"SELECT\n"
37+
+ " id\n"
38+
+ "FROM\n"
39+
+ " users\n"
40+
+ "WHERE\n"
41+
+ " id = 1",
42+
result);
43+
}
44+
45+
[Fact]
46+
public void Uppercase_False_PreservesKeywordCase()
47+
{
48+
#pragma warning disable CS0618
49+
var cfg = FormatConfig.Builder().Uppercase(false).Build();
50+
#pragma warning restore CS0618
51+
var result = s_formatter.Format("select id FROM users", cfg);
52+
53+
Assert.Equal(
54+
"select\n"
55+
+ " id\n"
56+
+ "FROM\n"
57+
+ " users",
58+
result);
59+
}
60+
61+
[Fact]
62+
public void Lower_FormatsKeywordsInLowercase()
63+
{
64+
var cfg = FormatConfig.Builder().Case(CaseTypes.LOWER).Build();
65+
var result = s_formatter.Format("SELECT id FROM Users WHERE id = 1", cfg);
66+
67+
Assert.Equal(
68+
"select\n"
69+
+ " id\n"
70+
+ "from\n"
71+
+ " Users\n"
72+
+ "where\n"
73+
+ " id = 1",
74+
result);
75+
}
76+
77+
[Fact]
78+
public void Upper_ClearsLower()
79+
{
80+
var cfg = FormatConfig.Builder()
81+
.Case(CaseTypes.LOWER)
82+
.Case(CaseTypes.UPPER)
83+
.Build();
84+
85+
Assert.Equal(CaseTypes.UPPER, cfg.Case);
86+
}
87+
88+
[Fact]
89+
public void Lower_ClearsUpper()
90+
{
91+
var cfg = FormatConfig.Builder()
92+
.Case(CaseTypes.UPPER)
93+
.Case(CaseTypes.LOWER)
94+
.Build();
95+
96+
Assert.Equal(CaseTypes.LOWER, cfg.Case);
97+
}
98+
99+
[Fact]
100+
public void Upper_ClearsLower_FormatsUppercase()
101+
{
102+
var cfg = FormatConfig.Builder()
103+
.Case(CaseTypes.LOWER)
104+
.Case(CaseTypes.UPPER)
105+
.Build();
106+
107+
var result = s_formatter.Format("select id from users", cfg);
108+
109+
Assert.Equal(
110+
"SELECT\n"
111+
+ " id\n"
112+
+ "FROM\n"
113+
+ " users",
114+
result);
115+
}
116+
117+
[Fact]
118+
public void Lower_ClearsUpper_FormatsLowercase()
119+
{
120+
var cfg = FormatConfig.Builder()
121+
.Case(CaseTypes.UPPER)
122+
.Case(CaseTypes.LOWER)
123+
.Build();
124+
125+
var result = s_formatter.Format("SELECT id FROM users", cfg);
126+
127+
Assert.Equal(
128+
"select\n"
129+
+ " id\n"
130+
+ "from\n"
131+
+ " users",
132+
result);
133+
}
134+
135+
[Fact]
136+
public void NeitherUppercaseNorLowercase_PreservesKeywordCase()
137+
{
138+
var cfg = FormatConfig.Builder().Build();
139+
var result = s_formatter.Format("SeLeCt id FrOm users", cfg);
140+
141+
Assert.Equal(
142+
"SeLeCt\n"
143+
+ " id\n"
144+
+ "FrOm\n"
145+
+ " users",
146+
result);
147+
}
148+
149+
[Fact]
150+
public void None_PreservesKeywordCase()
151+
{
152+
var cfg = FormatConfig.Builder().Case(CaseTypes.NONE).Build();
153+
var result = s_formatter.Format("SeLeCt id FrOm users", cfg);
154+
155+
Assert.Equal(
156+
"SeLeCt\n"
157+
+ " id\n"
158+
+ "FrOm\n"
159+
+ " users",
160+
result);
161+
}
162+
163+
[Theory]
164+
[InlineData(CaseTypes.NONE, false)]
165+
[InlineData(CaseTypes.UPPER, true)]
166+
[InlineData(CaseTypes.LOWER, false)]
167+
public void Case_SetsUppercaseCorrectly(CaseTypes caseType, bool expectedUppercase)
168+
{
169+
var cfg = FormatConfig.Builder().Case(caseType).Build();
170+
#pragma warning disable CS0618
171+
Assert.Equal(expectedUppercase, cfg.Uppercase);
172+
#pragma warning restore CS0618
173+
}
174+
}
175+
}

SQL.Formatter/Core/AbstractFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ protected virtual void FormatQuerySeparator(Token token, StringBuilder query)
282282

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

297297
return token.Value;

SQL.Formatter/Core/CaseTypes.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace SQL.Formatter.Core
2+
{
3+
public enum CaseTypes
4+
{
5+
NONE,
6+
UPPER,
7+
LOWER,
8+
}
9+
}

SQL.Formatter/Core/FormatConfig.cs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

34
namespace SQL.Formatter.Core
45
{
@@ -10,22 +11,45 @@ public class FormatConfig
1011
public readonly string Indent;
1112
public readonly int MaxColumnLength;
1213
public readonly Params Parameters;
14+
[Obsolete("Use Case instead of Uppercase")]
1315
public readonly bool Uppercase;
16+
public readonly CaseTypes Case;
1417
public readonly int LinesBetweenQueries;
1518
public readonly bool SkipWhitespaceNearBlockParentheses;
1619

20+
[Obsolete("Use the constructor with caseType instead of uppercase")]
1721
public FormatConfig(
1822
string indent,
1923
int maxColumnLength,
2024
Params parameters,
2125
bool uppercase,
2226
int linesBetweenQueries,
2327
bool skipWhitespaceNearBlockParentheses)
28+
: this(
29+
indent,
30+
maxColumnLength,
31+
parameters,
32+
uppercase ? CaseTypes.UPPER : CaseTypes.NONE,
33+
linesBetweenQueries,
34+
skipWhitespaceNearBlockParentheses)
35+
{
36+
}
37+
38+
public FormatConfig(
39+
string indent,
40+
int maxColumnLength,
41+
Params parameters,
42+
CaseTypes caseType,
43+
int linesBetweenQueries,
44+
bool skipWhitespaceNearBlockParentheses)
2445
{
2546
Indent = indent;
2647
MaxColumnLength = maxColumnLength;
2748
Parameters = parameters == null ? Params.Empty : parameters;
28-
Uppercase = uppercase;
49+
Case = caseType;
50+
#pragma warning disable CS0618
51+
Uppercase = caseType == CaseTypes.UPPER;
52+
#pragma warning restore CS0618
2953
LinesBetweenQueries = linesBetweenQueries;
3054
SkipWhitespaceNearBlockParentheses = skipWhitespaceNearBlockParentheses;
3155
}
@@ -40,7 +64,7 @@ public class FormatConfigBuilder
4064
private string _indent = DefaultIndent;
4165
private int _maxColumnLength = DefaultColumnMaxLength;
4266
private Params _parameters;
43-
private bool _uppercase;
67+
private CaseTypes _case;
4468
private int _linesBetweenQueries;
4569
private bool _skipWhitespaceNearBlockParentheses;
4670

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

103+
[Obsolete("Use Case instead of Uppercase")]
79104
public FormatConfigBuilder Uppercase(bool uppercase)
80105
{
81-
_uppercase = uppercase;
106+
_case = uppercase ? CaseTypes.UPPER : CaseTypes.NONE;
107+
108+
return this;
109+
}
110+
111+
public FormatConfigBuilder Case(CaseTypes caseType)
112+
{
113+
_case = caseType;
82114
return this;
83115
}
84116

@@ -100,7 +132,7 @@ public FormatConfig Build()
100132
_indent,
101133
_maxColumnLength,
102134
_parameters,
103-
_uppercase,
135+
_case,
104136
_linesBetweenQueries,
105137
_skipWhitespaceNearBlockParentheses);
106138
}

0 commit comments

Comments
 (0)