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

Commit 2b5b783

Browse files
committed
Add support for specifying and selecting typed join aliases
1 parent efb4529 commit 2b5b783

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

src/ServiceStack.OrmLite.SqlServer/SqlServerTableHint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
public class SqlServerTableHint
44
{
5-
public static JoinFormatDelegate ReadUncommitted = (table,expr) => "{0} WITH (READUNCOMMITTED) {1}".Fmt(table, expr);
5+
public static JoinFormatDelegate ReadUncommitted = (table, expr) => "{0} WITH (READUNCOMMITTED) {1}".Fmt(table, expr);
66
public static JoinFormatDelegate ReadCommitted = (table, expr) => "{0} WITH (READCOMMITTED) {1}".Fmt(table, expr);
77
public static JoinFormatDelegate ReadPast = (table, expr) => "{0} WITH (READPAST) {1}".Fmt(table, expr);
88
public static JoinFormatDelegate Serializable = (table, expr) => "{0} WITH (SERIALIZABLE) {1}".Fmt(table, expr);

src/ServiceStack.OrmLite.Sqlite/SqliteExpression.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected override object VisitSqlMethodCall(MethodCallExpression m)
4949
statement = string.Format("{0} DESC", quotedColName);
5050
break;
5151
case "As":
52-
statement = string.Format("{0} As {1}", quotedColName,
52+
statement = string.Format("{0} AS {1}", quotedColName,
5353
base.DialectProvider.GetQuotedColumnName(RemoveQuoteFromAlias(args[0].ToString())));
5454
break;
5555
case "Sum":
@@ -66,7 +66,7 @@ protected override object VisitSqlMethodCall(MethodCallExpression m)
6666
statement = string.Format("COUNT(DISTINCT {0})", quotedColName);
6767
break;
6868
default:
69-
throw new NotSupportedException();
69+
return base.VisitSqlMethodCall(m);
7070
}
7171

7272
return new PartialSqlString(statement);

src/ServiceStack.OrmLite/Expressions/Sql.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ public static string Avg(string value)
102102
{
103103
return "AVG({0})".Fmt(value);
104104
}
105+
106+
public static T AllFields<T>(T item)
107+
{
108+
return item;
109+
}
110+
111+
public static string JoinAlias<T>(T property, string tableAlias)
112+
{
113+
return tableAlias;
114+
}
105115
}
106116

107117
}

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,7 @@ private object SetAnonTypePropertyNamesForSelectExpression(object expr, Expressi
16121612
var methodCallExpr = arg as MethodCallExpression;
16131613
var mi = methodCallExpr != null ? methodCallExpr.Method : null;
16141614
var declareType = mi != null && mi.DeclaringType != null ? mi.DeclaringType : null;
1615-
if (declareType != null && declareType.Name == "Sql" && mi.Name != "Desc" && mi.Name != "Asc" && mi.Name != "As")
1615+
if (declareType != null && declareType.Name == "Sql" && mi.Name != "Desc" && mi.Name != "Asc" && mi.Name != "As" && mi.Name != "AllFields")
16161616
return new PartialSqlString(expr + " AS " + member.Name); // new { Count = Sql.Count("*") }
16171617

16181618
return expr;
@@ -2028,6 +2028,13 @@ protected virtual object VisitSqlMethodCall(MethodCallExpression m)
20282028
case "CountDistinct":
20292029
statement = string.Format("COUNT(DISTINCT {0})", quotedColName);
20302030
break;
2031+
case "AllFields":
2032+
var argDef = m.Arguments[0].Type.GetModelMetadata();
2033+
statement = DialectProvider.GetQuotedTableName(argDef) + ".*";
2034+
break;
2035+
case "JoinAlias":
2036+
statement = args[0] + "." + quotedColName.ToString().RightPart('.');
2037+
break;
20312038
default:
20322039
throw new NotSupportedException();
20332040
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,19 @@ public void Can_use_cusom_SqlExpression_to_add_multiple_self_Left_Joins()
106106
Assert.That(sales.Count, Is.EqualTo(1));
107107

108108
//Alternative
109-
db.GetLastSql().Print();
110109
q = db.From<Sale>()
111110
.LeftJoin<ContactIssue>((s,c) => s.SellerId == c.Id, db.JoinAlias<ContactIssue>("seller"))
112111
.LeftJoin<ContactIssue>((s,c) => s.BuyerId == c.Id, db.JoinAlias<ContactIssue>("buyer"))
113-
.Select(@"Sale.*
114-
, buyer.{0} AS BuyerFirstName
115-
, buyer.{1} AS BuyerLastName
116-
, seller.{0} AS SellerFirstName
117-
, seller.{1} AS SellerLastName"
118-
.Fmt("FirstName".SqlColumn(), "LastName".SqlColumn()));
112+
.Select<Sale, ContactIssue>((s,c) => new {
113+
sale = Sql.AllFields(s),
114+
BuyerFirstName = Sql.JoinAlias(c.FirstName, "buyer"),
115+
BuyerLastName = Sql.JoinAlias(c.LastName, "buyer"),
116+
SellerFirstName = Sql.JoinAlias(c.FirstName, "seller"),
117+
SellerLastName = Sql.JoinAlias(c.LastName, "seller"),
118+
});
119119

120120
q.Where(x => x.TenantId == tenantId);
121121

122-
q.ToSelectStatement().Print();
123-
124122
sales = db.Select<SaleView>(q);
125123
Assert.That(sales.Count, Is.EqualTo(1));
126124

0 commit comments

Comments
 (0)