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

Commit e597cab

Browse files
committed
Added Aliases for selected columns on JoinSqlBuilder
1 parent 282e5b3 commit e597cab

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/ServiceStack.OrmLite/JoinSqlBuilder.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Linq.Expressions;
45
using System.Text;
6+
using System.Reflection;
57

68
namespace ServiceStack.OrmLite
79
{
@@ -69,15 +71,15 @@ private void ProcessUnary(string tableName, UnaryExpression u, List<string> lst,
6971
throw new Exception("Invalid Expression provided");
7072
}
7173

72-
protected void ProcessMemberAccess(string tableName, MemberExpression m, List<string> lst, bool withTablePrefix)
74+
protected void ProcessMemberAccess(string tableName, MemberExpression m, List<string> lst, bool withTablePrefix, string alias = "")
7375
{
7476
if (m.Expression != null
7577
&& (m.Expression.NodeType == ExpressionType.Parameter || m.Expression.NodeType == ExpressionType.Convert))
7678
{
7779
if (withTablePrefix)
78-
lst.Add(string.Format("{0}.{1}", OrmLiteConfig.DialectProvider.GetQuotedTableName(tableName), OrmLiteConfig.DialectProvider.GetQuotedColumnName(m.Member.Name)));
80+
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))));
7981
else
80-
lst.Add(string.Format("{0}", OrmLiteConfig.DialectProvider.GetQuotedColumnName(m.Member.Name)));
82+
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))));
8183
return;
8284
}
8385
throw new Exception("Only Members are allowed");
@@ -87,20 +89,26 @@ private void ProcessNew(string tableName, NewExpression nex, List<string> lst, b
8789
{
8890
if (nex.Arguments == null || nex.Arguments.Count == 0)
8991
throw new Exception("Only column list allowed");
90-
foreach (var arg in nex.Arguments)
91-
PropertyList(tableName, arg, lst, withTablePrefix);
92+
93+
for (int i=0; i< nex.Arguments.Count;i++)
94+
{
95+
var arg = nex.Arguments[i];
96+
var alias = nex.Type.GetProperties()[i].Name;
97+
98+
PropertyList(tableName, arg, lst, withTablePrefix, alias);
99+
}
92100
return;
93101
}
94102

95-
private void PropertyList(string tableName, Expression exp, List<string> lst, bool withTablePrefix)
103+
private void PropertyList(string tableName, Expression exp, List<string> lst, bool withTablePrefix, string alias = "")
96104
{
97105
if (exp == null)
98106
return;
99107

100108
switch (exp.NodeType)
101109
{
102110
case ExpressionType.MemberAccess:
103-
ProcessMemberAccess(tableName, exp as MemberExpression, lst, withTablePrefix);
111+
ProcessMemberAccess(tableName, exp as MemberExpression, lst, withTablePrefix, alias);
104112
return;
105113

106114
case ExpressionType.Convert:

0 commit comments

Comments
 (0)