Skip to content

Commit c9755ec

Browse files
committed
Initial support for aliased math expressions.
1 parent e999eab commit c9755ec

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

SqlServerSimulator.Tests/SelectTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ public void SelectAliasedExpression()
7676
Assert.AreEqual(1, reader.GetInt32(0));
7777
}
7878

79+
[TestMethod]
80+
public void SelectAliasedMathExpression()
81+
{
82+
using var reader = new Simulation().ExecuteReader("select 1 + 1 as c");
83+
84+
Assert.IsTrue(reader.Read());
85+
Assert.AreEqual("c", reader.GetName(0));
86+
Assert.AreEqual(2, reader.GetInt32(0));
87+
}
88+
7989
[TestMethod]
8090
public void SelectExpressionFromSystemTable()
8191
{

SqlServerSimulator/Parser/Expression.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ public static Expression Parse(Simulation simulation, IEnumerator<Token> tokens,
5555

5656
token = tokens.RequireNext();
5757

58-
expression = new Add(expression, Parse(simulation, tokens, ref token, getVariableValue));
58+
var parsed = Parse(simulation, tokens, ref token, getVariableValue);
59+
expression = new Add(expression, parsed);
60+
if (parsed is NamedExpression named)
61+
expression = named.TransferName(expression);
5962
break;
6063
case Period:
6164
if (expression is null)
@@ -88,13 +91,32 @@ private sealed class NamedExpression(Expression expression, string name) : Expre
8891
{
8992
private readonly Expression expression = expression;
9093
private readonly string name = name;
94+
#if DEBUG
95+
private bool transferred;
96+
#endif
9197

9298
public override string Name => this.name;
9399

94100
public override object? Run(Func<List<string>, object?> getColumnValue) => this.expression.Run(getColumnValue);
95101

96102
#if DEBUG
97-
public override string ToString() => $"{expression} {name}";
103+
/// <summary>
104+
/// Transfers the name to an outer expression.
105+
/// </summary>
106+
/// <param name="destination">The expression wrapping this<see cref="NamedExpression"/>.</param>
107+
/// <returns>A new <see cref="NamedExpression"/> wrapping <paramref name="destination"/> using <see cref="Name"/>.</returns>
108+
#endif
109+
public NamedExpression TransferName(Expression destination)
110+
{
111+
#if DEBUG
112+
transferred = true;
113+
#endif
114+
115+
return new(destination, this.name);
116+
}
117+
118+
#if DEBUG
119+
public override string ToString() => transferred ? expression.ToString() : $"{expression} {name}";
98120
#endif
99121
}
100122

0 commit comments

Comments
 (0)