Skip to content

Commit a571670

Browse files
committed
Allow alias on MathReference (fix issue ThatRendle#209)
1 parent b182e0a commit a571670

3 files changed

Lines changed: 23 additions & 2 deletions

File tree

Simple.Data.Ado/SimpleReferenceFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ private string TryFormatAsMathReference(MathReference mathReference, bool exclud
7777
FormatObject(mathReference.RightOperand));
7878
}
7979

80-
return string.Format("{0} {1} {2} AS {3}", FormatObject(mathReference.LeftOperand),
80+
return string.Format("({0} {1} {2}) AS {3}", FormatObject(mathReference.LeftOperand),
8181
MathOperatorToString(mathReference.Operator),
8282
FormatObject(mathReference.RightOperand),
83-
mathReference.GetAlias());
83+
_schema.QuoteObjectName(mathReference.GetAlias()));
8484

8585
}
8686

Simple.Data.BehaviourTest/Query/QueryTest.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ public void SpecifyingColumnWithAliasShouldAddAsClause()
7171
.ToList();
7272
GeneratedSqlIs("select [dbo].[users].[name],[dbo].[users].[password] as [supersecretpassword] from [dbo].[users]");
7373
}
74+
75+
[Test]
76+
public void SpecifyingColumnMathsWithAliasShouldAddAsClause()
77+
{
78+
_db.Users.All()
79+
.Select(_db.Users.Name, (_db.Users.Id + _db.Users.Age).As("Nonsense"))
80+
.ToList();
81+
GeneratedSqlIs("select [dbo].[users].[name],([dbo].[users].[id] + [dbo].[users].[age]) as [nonsense] from [dbo].[users]");
82+
}
7483

7584
[Test]
7685
public void SpecifyingColumnsFromOtherTablesShouldAddJoin()

Simple.Data/MathReference.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ public MathReference(object leftOperand, object rightOperand, MathOperator @oper
1818
_operator = @operator;
1919
}
2020

21+
private MathReference(object leftOperand, object rightOperand, MathOperator @operator, string alias) : base(alias)
22+
{
23+
_leftOperand = leftOperand;
24+
_rightOperand = rightOperand;
25+
_operator = @operator;
26+
}
27+
2128
public MathOperator Operator
2229
{
2330
get { return _operator; }
@@ -33,6 +40,11 @@ public object LeftOperand
3340
get { return _leftOperand; }
3441
}
3542

43+
public MathReference As(string alias)
44+
{
45+
return new MathReference(_leftOperand, _rightOperand, _operator, alias);
46+
}
47+
3648
/// <summary>
3749
/// Implements the operator == to create a <see cref="SimpleExpression"/> with the type <see cref="SimpleExpressionType.Equal"/>.
3850
/// </summary>

0 commit comments

Comments
 (0)