Skip to content

Commit a1bec30

Browse files
authored
Format in clauses and add tests (#751)
* Format IN clauses and add tests Break long IN/NOT IN value lists into multi-line, indented form when generating SQL scripts by adding InPredicateCollector and updating FormattedScriptGenerator to replace single-line value lists. Add tests CommandInClause and CommandNotInClause plus their verified outputs. Update readme snippet anchors to match new source line ranges. * Update Directory.Build.props
1 parent 7845b98 commit a1bec30

7 files changed

Lines changed: 91 additions & 6 deletions

readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ await Verify(connection)
6565
// include only tables and views
6666
.SchemaIncludes(DbObjects.Tables | DbObjects.Views);
6767
```
68-
<sup><a href='/src/Tests/Tests.cs#L423-L429' title='Snippet source file'>snippet source</a> | <a href='#snippet-SchemaInclude' title='Start of snippet'>anchor</a></sup>
68+
<sup><a href='/src/Tests/Tests.cs#L443-L449' title='Snippet source file'>snippet source</a> | <a href='#snippet-SchemaInclude' title='Start of snippet'>anchor</a></sup>
6969
<!-- endSnippet -->
7070

7171
Available values:
@@ -103,7 +103,7 @@ await Verify(connection)
103103
_ => _ is TableViewBase ||
104104
_.Name == "MyTrigger");
105105
```
106-
<sup><a href='/src/Tests/Tests.cs#L448-L456' title='Snippet source file'>snippet source</a> | <a href='#snippet-SchemaFilter' title='Start of snippet'>anchor</a></sup>
106+
<sup><a href='/src/Tests/Tests.cs#L468-L476' title='Snippet source file'>snippet source</a> | <a href='#snippet-SchemaFilter' title='Start of snippet'>anchor</a></sup>
107107
<!-- endSnippet -->
108108

109109

@@ -125,7 +125,7 @@ command.CommandText = "select Value from MyTable";
125125
var value = await command.ExecuteScalarAsync();
126126
await Verify(value!);
127127
```
128-
<sup><a href='/src/Tests/Tests.cs#L232-L242' title='Snippet source file'>snippet source</a> | <a href='#snippet-Recording' title='Start of snippet'>anchor</a></sup>
128+
<sup><a href='/src/Tests/Tests.cs#L252-L262' title='Snippet source file'>snippet source</a> | <a href='#snippet-Recording' title='Start of snippet'>anchor</a></sup>
129129
<!-- endSnippet -->
130130

131131
Will result in the following verified file:
@@ -180,7 +180,7 @@ await Verify(
180180
sqlEntries = entries
181181
});
182182
```
183-
<sup><a href='/src/Tests/Tests.cs#L309-L339' title='Snippet source file'>snippet source</a> | <a href='#snippet-RecordingSpecific' title='Start of snippet'>anchor</a></sup>
183+
<sup><a href='/src/Tests/Tests.cs#L329-L359' title='Snippet source file'>snippet source</a> | <a href='#snippet-RecordingSpecific' title='Start of snippet'>anchor</a></sup>
184184
<!-- endSnippet -->
185185

186186

@@ -208,7 +208,7 @@ var sqlErrorsViaType = entries
208208
.Select(_ => _.Data)
209209
.OfType<ErrorEntry>();
210210
```
211-
<sup><a href='/src/Tests/Tests.cs#L365-L384' title='Snippet source file'>snippet source</a> | <a href='#snippet-RecordingReadingResults' title='Start of snippet'>anchor</a></sup>
211+
<sup><a href='/src/Tests/Tests.cs#L385-L404' title='Snippet source file'>snippet source</a> | <a href='#snippet-RecordingReadingResults' title='Start of snippet'>anchor</a></sup>
212212
<!-- endSnippet -->
213213

214214

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Project>
33
<PropertyGroup>
44
<NoWarn>CS1591;CS0649;CS8632;NU1608;NU1109</NoWarn>
5-
<Version>11.3.0</Version>
5+
<Version>11.3.1</Version>
66
<LangVersion>preview</LangVersion>
77
<AssemblyVersion>1.0.0</AssemblyVersion>
88
<PackageTags>SqlServer, Verify</PackageTags>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
Text:
3+
select Id,
4+
Name
5+
from MyTable
6+
where Id in (1,
7+
2,
8+
4,
9+
6),
10+
HasTransaction: false
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
Text:
3+
select Id,
4+
Name
5+
from MyTable
6+
where Id not in (1,
7+
2,
8+
4),
9+
HasTransaction: false
10+
}

src/Tests/Tests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,26 @@ public async Task CommandOrderByMultipleColumnsDesc()
194194
await Verify(command);
195195
}
196196

197+
[Test]
198+
public async Task CommandInClause()
199+
{
200+
var command = new SqlCommand
201+
{
202+
CommandText = "select Id, Name from MyTable where Id in (1, 2, 4, 6)"
203+
};
204+
await Verify(command);
205+
}
206+
207+
[Test]
208+
public async Task CommandNotInClause()
209+
{
210+
var command = new SqlCommand
211+
{
212+
CommandText = "select Id, Name from MyTable where Id not in (1, 2, 4)"
213+
};
214+
await Verify(command);
215+
}
216+
197217
[Test]
198218
public async Task Exception()
199219
{

src/Verify.SqlServer/FormattedScriptGenerator.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,43 @@ public string GenerateScript(TSqlFragment fragment)
5050
#endif
5151
}
5252

53+
var inCollector = new InPredicateCollector();
54+
fragment.Accept(inCollector);
55+
56+
foreach (var inPredicate in inCollector.Predicates)
57+
{
58+
if (inPredicate.Values.Count <= 1)
59+
{
60+
continue;
61+
}
62+
63+
var values = new List<string>(inPredicate.Values.Count);
64+
foreach (var value in inPredicate.Values)
65+
{
66+
generator.GenerateScript(value, out var text);
67+
values.Add(text);
68+
}
69+
70+
var singleLine = string.Join(", ", values);
71+
72+
var pos = script.IndexOf(singleLine, StringComparison.Ordinal);
73+
if (pos == -1)
74+
{
75+
continue;
76+
}
77+
78+
var lineStart = script.LastIndexOf('\n', pos) + 1;
79+
var indent = new string(' ', pos - lineStart);
80+
81+
#if NET48
82+
var multiLine = string.Join(",\n" + indent, values);
83+
script = script.Substring(0, pos) + multiLine + script.Substring(pos + singleLine.Length);
84+
#else
85+
var multiLine = string.Join(",\n" + indent, values);
86+
script = string.Concat(script.AsSpan(0, pos), multiLine, script.AsSpan(pos + singleLine.Length));
87+
#endif
88+
}
89+
5390
return script;
5491
}
5592
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class InPredicateCollector : TSqlFragmentVisitor
2+
{
3+
public List<InPredicate> Predicates { get; } = [];
4+
5+
public override void Visit(InPredicate node) =>
6+
Predicates.Add(node);
7+
}

0 commit comments

Comments
 (0)