Repository navigation
Expand file tree
/
Copy pathSqlScriptGeneratorVisitor.CaseExpression.cs
More file actions
91 lines (76 loc) · 2.91 KB
/
Copy pathSqlScriptGeneratorVisitor.CaseExpression.cs
File metadata and controls
91 lines (76 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//------------------------------------------------------------------------------
// <copyright file="SqlScriptGeneratorVisitor.CaseExpression.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System.Collections.Generic;
using Microsoft.SqlServer.TransactSql.ScriptDom;
namespace Microsoft.SqlServer.TransactSql.ScriptDom.ScriptGenerator
{
partial class SqlScriptGeneratorVisitor
{
public override void ExplicitVisit(SimpleCaseExpression node)
{
bool multiline = BeginCaseExpression();
GenerateSpaceAndFragmentIfNotNull(node.InputExpression);
GenerateCaseBody(node.WhenClauses, node.ElseExpression, multiline);
GenerateSpaceAndCollation(node.Collation);
}
public override void ExplicitVisit(SearchedCaseExpression node)
{
bool multiline = BeginCaseExpression();
GenerateCaseBody(node.WhenClauses, node.ElseExpression, multiline);
GenerateSpaceAndCollation(node.Collation);
}
// Writes CASE. With MultilineCaseExpression it first pushes an alignment point at the CASE
// keyword, which GenerateCaseBody pops after END, so that every line of the body starts
// from the CASE column. Without it no point is pushed, which keeps the single-line output
// unchanged.
private bool BeginCaseExpression()
{
bool multiline = _options.MultilineCaseExpression;
if (multiline)
{
MarkAndPushAlignmentPoint(new AlignmentPoint());
}
GenerateKeyword(TSqlTokenType.Case);
return multiline;
}
private void GenerateCaseBody<TWhenClause>(IList<TWhenClause> whenClauses, ScalarExpression elseExpression, bool multiline)
where TWhenClause : WhenClause
{
foreach (TWhenClause when in whenClauses)
{
GenerateCaseBodyLineStart(multiline);
GenerateFragmentIfNotNull(when);
}
if (elseExpression != null)
{
GenerateCaseBodyLineStart(multiline);
GenerateKeyword(TSqlTokenType.Else);
GenerateSpaceAndFragmentIfNotNull(elseExpression);
}
if (multiline)
{
NewLine();
GenerateKeyword(TSqlTokenType.End);
PopAlignmentPoint();
}
else
{
GenerateSpaceAndKeyword(TSqlTokenType.End);
}
}
private void GenerateCaseBodyLineStart(bool multiline)
{
if (multiline)
{
NewLineAndIndent();
}
else
{
GenerateSpace();
}
}
}
}