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

Commit fb6bf7f

Browse files
committed
Merge pull request #277 from Lee337/master
JoinSqlBuilder - Added Alias for queries
2 parents f4e6677 + 21412ba commit fb6bf7f

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/ServiceStack.OrmLite/JoinSqlBuilder.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ private void ProcessUnary(string tableName, UnaryExpression u, List<string> lst,
6969
throw new Exception("Invalid Expression provided");
7070
}
7171

72-
protected void ProcessMemberAccess(string tableName, MemberExpression m, List<string> lst, bool withTablePrefix)
72+
protected void ProcessMemberAccess(string tableName, MemberExpression m, List<string> lst, bool withTablePrefix, string alias = "")
7373
{
7474
if (m.Expression != null
7575
&& (m.Expression.NodeType == ExpressionType.Parameter || m.Expression.NodeType == ExpressionType.Convert))
7676
{
7777
if (withTablePrefix)
78-
lst.Add(string.Format("{0}.{1}", OrmLiteConfig.DialectProvider.GetQuotedTableName(tableName), OrmLiteConfig.DialectProvider.GetQuotedColumnName(m.Member.Name)));
78+
lst.Add(string.Format("{0}.{1}{2}", OrmLiteConfig.DialectProvider.GetQuotedTableName(tableName), OrmLiteConfig.DialectProvider.GetQuotedColumnName(m.Member.Name), string.IsNullOrEmpty(alias) ? string.Empty : string.Format(" AS {0}", OrmLiteConfig.DialectProvider.GetQuotedColumnName(alias))));
7979
else
80-
lst.Add(string.Format("{0}", OrmLiteConfig.DialectProvider.GetQuotedColumnName(m.Member.Name)));
80+
lst.Add(string.Format("{0}{1}", OrmLiteConfig.DialectProvider.GetQuotedColumnName(m.Member.Name), string.IsNullOrEmpty(alias) ? string.Empty : string.Format(" AS {0}", OrmLiteConfig.DialectProvider.GetQuotedColumnName(alias))));
8181
return;
8282
}
8383
throw new Exception("Only Members are allowed");
@@ -87,20 +87,27 @@ private void ProcessNew(string tableName, NewExpression nex, List<string> lst, b
8787
{
8888
if (nex.Arguments == null || nex.Arguments.Count == 0)
8989
throw new Exception("Only column list allowed");
90-
foreach (var arg in nex.Arguments)
91-
PropertyList(tableName, arg, lst, withTablePrefix);
90+
91+
var expressionProperties = nex.Type.GetProperties();
92+
for (int i=0; i< nex.Arguments.Count;i++)
93+
{
94+
var arg = nex.Arguments[i];
95+
var alias = expressionProperties[i].Name;
96+
97+
PropertyList(tableName, arg, lst, withTablePrefix, alias);
98+
}
9299
return;
93100
}
94101

95-
private void PropertyList(string tableName, Expression exp, List<string> lst, bool withTablePrefix)
102+
private void PropertyList(string tableName, Expression exp, List<string> lst, bool withTablePrefix, string alias = "")
96103
{
97104
if (exp == null)
98105
return;
99106

100107
switch (exp.NodeType)
101108
{
102109
case ExpressionType.MemberAccess:
103-
ProcessMemberAccess(tableName, exp as MemberExpression, lst, withTablePrefix);
110+
ProcessMemberAccess(tableName, exp as MemberExpression, lst, withTablePrefix, alias);
104111
return;
105112

106113
case ExpressionType.Convert:

0 commit comments

Comments
 (0)