Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit dce264a

Browse files
committed
Include select alias for null expressions
1 parent 2c3d50f commit dce264a

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,9 +2192,11 @@ private object SetAnonTypePropertyNamesForSelectExpression(object expr, Expressi
21922192
return new PartialSqlString(OrmLiteUtils.UnquotedColumnName(strExpr) != member.Name
21932193
? strExpr + " AS " + member.Name
21942194
: strExpr);
2195-
}
2195+
}
21962196

2197-
return UseSelectPropertiesAsAliases
2197+
var usePropertyAlias = UseSelectPropertiesAsAliases ||
2198+
(expr is PartialSqlString p && Equals(p, PartialSqlString.Null)); // new { Alias = (DateTime?)null }
2199+
return usePropertyAlias
21982200
? new SelectItemExpression(DialectProvider, expr.ToString(), member.Name)
21992201
: expr;
22002202
}
@@ -2256,7 +2258,7 @@ protected virtual object VisitParameter(ParameterExpression p)
22562258
protected virtual object VisitConstant(ConstantExpression c)
22572259
{
22582260
if (c.Value == null)
2259-
return new PartialSqlString("null");
2261+
return PartialSqlString.Null;
22602262

22612263
return c.Value;
22622264
}
@@ -3049,12 +3051,24 @@ public interface IHasDialectProvider
30493051

30503052
public class PartialSqlString
30513053
{
3054+
public static PartialSqlString Null = new("null");
3055+
30523056
public PartialSqlString(string text)
30533057
{
30543058
Text = text;
30553059
}
30563060
public string Text { get; internal set; }
30573061
public override string ToString() => Text;
3062+
3063+
protected bool Equals(PartialSqlString other) => Text == other.Text;
3064+
public override bool Equals(object obj)
3065+
{
3066+
if (ReferenceEquals(null, obj)) return false;
3067+
if (ReferenceEquals(this, obj)) return true;
3068+
if (obj.GetType() != this.GetType()) return false;
3069+
return Equals((PartialSqlString) obj);
3070+
}
3071+
public override int GetHashCode() => (Text != null ? Text.GetHashCode() : 0);
30583072
}
30593073

30603074
public class EnumMemberAccess : PartialSqlString

tests/ServiceStack.OrmLite.Tests/ExpressionVisitorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ public void Can_Where_using_filter_with_Concat()
493493
public void Can_Where_using_constant_filter()
494494
{
495495
object left = null;
496-
var right = new PartialSqlString("null");
496+
var right = PartialSqlString.Null;
497497

498498
System.Linq.Expressions.Expression<Func<TestType, bool>> filter = x => left == right;
499499
var q = Db.From<TestType>().Where(filter);//todo: here Where: null is NULL. May be need to change to 1=1 ?

tests/ServiceStack.OrmLite.Tests/Issues/CustomInsertIntoSelectJoinIssue.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public void Can_custom_join_into_select()
7373
UserAuthCustomId = w.UserAuthCustomId,
7474
DomainKey = w.DomainKey,
7575
CreateDate = DateTime.UtcNow,
76+
DeferUntil = (DateTime?) null,
7677
TargetDomainKey = t.DomainKey,
7778
Tries = 0,
7879
TargetDomainRecordId = w.TargetDomainRecordId,

0 commit comments

Comments
 (0)