Skip to content

Commit eef4c5a

Browse files
committed
Switched to DebuggerDisplay instead of compiler directives.
1 parent 5780770 commit eef4c5a

32 files changed

Lines changed: 81 additions & 124 deletions

SqlServerSimulator/Parser/BooleanExpression.cs

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics;
12
using SqlServerSimulator.Parser.Tokens;
23
using SqlServerSimulator.Storage;
34
using System.Text;
@@ -8,6 +9,7 @@ namespace SqlServerSimulator.Parser;
89
/// <summary>
910
/// A specific type of expression used in WHERE clauses and similar branching scenarios.
1011
/// </summary>
12+
[DebuggerDisplay("{DebugDisplay(),nq}")]
1113
internal abstract class BooleanExpression
1214
{
1315
protected readonly Expression left, right;
@@ -75,9 +77,12 @@ private static LikeExpression ParseLike(Expression left, ParserContext context,
7577
/// <param name="getColumnValue">Provides the value for a column.</param>
7678
public abstract bool Run(Func<List<string>, SqlValue> getColumnValue);
7779

78-
#if DEBUG
79-
public abstract override string ToString();
80-
#endif
80+
/// <summary>
81+
/// Diagnostic-only string rendering, surfaced via
82+
/// <see cref="DebuggerDisplayAttribute"/>. Production paths must not call
83+
/// this — same convention as <see cref="Expression.DebugDisplay"/>.
84+
/// </summary>
85+
internal abstract string DebugDisplay();
8186

8287
/// <summary>
8388
/// Evaluates both sides, applies SQL Server type promotion to a common
@@ -109,59 +114,47 @@ private sealed class EqualityExpression(Expression left, ParserContext context)
109114
public override bool Run(Func<List<string>, SqlValue> getColumnValue) =>
110115
ComparePromoted(left, right, getColumnValue, "equal to", static (l, r) => l.Equals(r));
111116

112-
#if DEBUG
113-
public override string ToString() => $"{left} = {right}";
114-
#endif
117+
internal override string DebugDisplay() => $"{left.DebugDisplay()} = {right.DebugDisplay()}";
115118
}
116119

117120
private sealed class InequalityExpression(Expression left, ParserContext context) : BooleanExpression(left, context)
118121
{
119122
public override bool Run(Func<List<string>, SqlValue> getColumnValue) =>
120123
ComparePromoted(left, right, getColumnValue, "not equal to", static (l, r) => !l.Equals(r));
121124

122-
#if DEBUG
123-
public override string ToString() => $"{left} <> {right}";
124-
#endif
125+
internal override string DebugDisplay() => $"{left.DebugDisplay()} <> {right.DebugDisplay()}";
125126
}
126127

127128
private sealed class GreaterThanExpression(Expression left, Expression right) : BooleanExpression(left, right)
128129
{
129130
public override bool Run(Func<List<string>, SqlValue> getColumnValue) =>
130131
ComparePromoted(left, right, getColumnValue, "greater than", static (l, r) => l.CompareTo(r) > 0);
131132

132-
#if DEBUG
133-
public override string ToString() => $"{left} > {right}";
134-
#endif
133+
internal override string DebugDisplay() => $"{left.DebugDisplay()} > {right.DebugDisplay()}";
135134
}
136135

137136
private sealed class GreaterThanOrEqualExpression(Expression left, ParserContext context) : BooleanExpression(left, context)
138137
{
139138
public override bool Run(Func<List<string>, SqlValue> getColumnValue) =>
140139
ComparePromoted(left, right, getColumnValue, "greater than or equal to", static (l, r) => l.CompareTo(r) >= 0);
141140

142-
#if DEBUG
143-
public override string ToString() => $"{left} >= {right}";
144-
#endif
141+
internal override string DebugDisplay() => $"{left.DebugDisplay()} >= {right.DebugDisplay()}";
145142
}
146143

147144
private sealed class LessThanExpression(Expression left, Expression right) : BooleanExpression(left, right)
148145
{
149146
public override bool Run(Func<List<string>, SqlValue> getColumnValue) =>
150147
ComparePromoted(left, right, getColumnValue, "less than", static (l, r) => l.CompareTo(r) < 0);
151148

152-
#if DEBUG
153-
public override string ToString() => $"{left} < {right}";
154-
#endif
149+
internal override string DebugDisplay() => $"{left.DebugDisplay()} < {right.DebugDisplay()}";
155150
}
156151

157152
private sealed class LessThanOrEqualExpression(Expression left, ParserContext context) : BooleanExpression(left, context)
158153
{
159154
public override bool Run(Func<List<string>, SqlValue> getColumnValue) =>
160155
ComparePromoted(left, right, getColumnValue, "less than or equal to", static (l, r) => l.CompareTo(r) <= 0);
161156

162-
#if DEBUG
163-
public override string ToString() => $"{left} <= {right}";
164-
#endif
157+
internal override string DebugDisplay() => $"{left.DebugDisplay()} <= {right.DebugDisplay()}";
165158
}
166159

167160
/// <summary>
@@ -329,10 +322,8 @@ private static int TranslateClass(string pattern, int start, StringBuilder sb)
329322
return end + 1;
330323
}
331324

332-
#if DEBUG
333-
public override string ToString() => this.escape is null
334-
? $"{left} {(this.negated ? "NOT LIKE" : "LIKE")} {right}"
335-
: $"{left} {(this.negated ? "NOT LIKE" : "LIKE")} {right} ESCAPE {this.escape}";
336-
#endif
325+
internal override string DebugDisplay() => this.escape is null
326+
? $"{left.DebugDisplay()} {(this.negated ? "NOT LIKE" : "LIKE")} {right.DebugDisplay()}"
327+
: $"{left.DebugDisplay()} {(this.negated ? "NOT LIKE" : "LIKE")} {right.DebugDisplay()} ESCAPE {this.escape.DebugDisplay()}";
337328
}
338329
}

SqlServerSimulator/Parser/Expression.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
using SqlServerSimulator.Parser.Expressions;
1+
using System.Diagnostics;
2+
using SqlServerSimulator.Parser.Expressions;
23
using SqlServerSimulator.Parser.Tokens;
34

45
namespace SqlServerSimulator.Parser;
56

67
/// <summary>
78
/// Contains the logic described by a SQL command and computes its results.
89
/// </summary>
10+
[DebuggerDisplay("{DebugDisplay(),nq}")]
911
internal abstract class Expression
1012
{
1113
private protected Expression()
@@ -153,9 +155,14 @@ public static Expression Parse(ParserContext context)
153155
/// <param name="resolveColumnType">Callback that, given a multi-part column name, returns its declared type or throws if unresolvable.</param>
154156
public abstract Storage.SqlType GetSqlType(Func<List<string>, Storage.SqlType> resolveColumnType);
155157

156-
#if DEBUG
157-
public abstract override string ToString();
158-
#endif
158+
/// <summary>
159+
/// Diagnostic-only string rendering, surfaced to debuggers via
160+
/// <see cref="DebuggerDisplayAttribute"/>. Production paths must not call
161+
/// this — they should produce purpose-built formats (Msg-shaped error
162+
/// text, CAST-to-varchar, etc.) instead. Kept non-public so accidental
163+
/// production use fails to compile.
164+
/// </summary>
165+
internal abstract string DebugDisplay();
159166

160167
private static Expression ResolveBuiltIn(string name, ParserContext context)
161168
{

SqlServerSimulator/Parser/Expressions/AbsoluteValue.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,5 @@ public override SqlValue Run(Func<List<string>, SqlValue> getColumnValue)
3232

3333
public override SqlType GetSqlType(Func<List<string>, SqlType> resolveColumnType) => source.GetSqlType(resolveColumnType);
3434

35-
#if DEBUG
36-
public override string ToString() => $"ABS({source})";
37-
#endif
35+
internal override string DebugDisplay() => $"ABS({source.DebugDisplay()})";
3836
}

SqlServerSimulator/Parser/Expressions/Cast.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ public override SqlValue Run(Func<List<string>, SqlValue> getColumnValue) =>
4444

4545
public override SqlType GetSqlType(Func<List<string>, SqlType> resolveColumnType) => targetType;
4646

47-
#if DEBUG
48-
public override string ToString() => $"CAST({source} AS {targetType})";
49-
#endif
47+
internal override string DebugDisplay() => $"CAST({source.DebugDisplay()} AS {targetType})";
5048

5149
/// <summary>
5250
/// Parses the optional <c>(length)</c> or <c>(precision, scale)</c> spec

SqlServerSimulator/Parser/Expressions/CharIndex.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public override SqlValue Run(Func<List<string>, SqlValue> getColumnValue)
5454

5555
public override SqlType GetSqlType(Func<List<string>, SqlType> resolveColumnType) => SqlType.Int32;
5656

57-
#if DEBUG
58-
public override string ToString() => start is null ? $"CHARINDEX({needle}, {haystack})" : $"CHARINDEX({needle}, {haystack}, {start})";
59-
#endif
57+
internal override string DebugDisplay() => start is null
58+
? $"CHARINDEX({needle.DebugDisplay()}, {haystack.DebugDisplay()})"
59+
: $"CHARINDEX({needle.DebugDisplay()}, {haystack.DebugDisplay()}, {start.DebugDisplay()})";
6060
}

SqlServerSimulator/Parser/Expressions/Convert.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,10 @@ public override SqlValue Run(Func<List<string>, SqlValue> getColumnValue)
100100

101101
public override SqlType GetSqlType(Func<List<string>, SqlType> resolveColumnType) => this.targetType;
102102

103-
#if DEBUG
104-
public override string ToString() =>
103+
internal override string DebugDisplay() =>
105104
this.style is null
106-
? $"{(this.tryMode ? "TRY_CONVERT" : "CONVERT")}({this.targetType}, {this.source})"
107-
: $"{(this.tryMode ? "TRY_CONVERT" : "CONVERT")}({this.targetType}, {this.source}, {this.style})";
108-
#endif
105+
? $"{(this.tryMode ? "TRY_CONVERT" : "CONVERT")}({this.targetType}, {this.source.DebugDisplay()})"
106+
: $"{(this.tryMode ? "TRY_CONVERT" : "CONVERT")}({this.targetType}, {this.source.DebugDisplay()}, {this.style.DebugDisplay()})";
109107

110108
/// <summary>
111109
/// Set of <see cref="SimulatedSqlException.Number"/> values that

SqlServerSimulator/Parser/Expressions/DataLength.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,5 @@ public override SqlValue Run(Func<List<string>, SqlValue> getColumnValue)
2121

2222
public override SqlType GetSqlType(Func<List<string>, SqlType> resolveColumnType) => SqlType.Int32;
2323

24-
#if DEBUG
25-
public override string ToString() => $"DATALENGTH({source})";
26-
#endif
24+
internal override string DebugDisplay() => $"DATALENGTH({source.DebugDisplay()})";
2725
}

SqlServerSimulator/Parser/Expressions/IdentCurrent.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,5 @@ public override SqlValue Run(Func<List<string>, SqlValue> getColumnValue)
4242

4343
public override SqlType GetSqlType(Func<List<string>, SqlType> resolveColumnType) => ResultType;
4444

45-
#if DEBUG
46-
public override string ToString() => $"IDENT_CURRENT('{this.tableName}')";
47-
#endif
45+
internal override string DebugDisplay() => $"IDENT_CURRENT('{this.tableName}')";
4846
}

SqlServerSimulator/Parser/Expressions/LastIdentityExpression.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,5 @@ this.simulation.LastIdentity is decimal v
3333

3434
public override SqlType GetSqlType(Func<List<string>, SqlType> resolveColumnType) => ResultType;
3535

36-
#if DEBUG
37-
public override string ToString() => "SCOPE_IDENTITY()";
38-
#endif
36+
internal override string DebugDisplay() => "SCOPE_IDENTITY()";
3937
}

SqlServerSimulator/Parser/Expressions/Left.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,5 @@ public override SqlValue Run(Func<List<string>, SqlValue> getColumnValue)
3939

4040
public override SqlType GetSqlType(Func<List<string>, SqlType> resolveColumnType) => source.GetSqlType(resolveColumnType);
4141

42-
#if DEBUG
43-
public override string ToString() => $"LEFT({source}, {count})";
44-
#endif
42+
internal override string DebugDisplay() => $"LEFT({source.DebugDisplay()}, {count.DebugDisplay()})";
4543
}

0 commit comments

Comments
 (0)