@@ -19,11 +19,21 @@ public SimpleReferenceFormatter(DatabaseSchema schema, ICommandBuilder commandBu
1919
2020 public string FormatColumnClause ( SimpleReference reference )
2121 {
22- var formatted = TryFormatAsObjectReference ( reference as ObjectReference )
22+ return FormatColumnClause ( reference , false ) ;
23+ }
24+
25+ public string FormatColumnClauseWithoutAlias ( SimpleReference reference )
26+ {
27+ return FormatColumnClause ( reference , true ) ;
28+ }
29+
30+ private string FormatColumnClause ( SimpleReference reference , bool excludeAlias )
31+ {
32+ var formatted = TryFormatAsObjectReference ( reference as ObjectReference , excludeAlias )
2333 ??
24- TryFormatAsFunctionReference ( reference as FunctionReference )
34+ TryFormatAsFunctionReference ( reference as FunctionReference , excludeAlias )
2535 ??
26- TryFormatAsMathReference ( reference as MathReference ) ;
36+ TryFormatAsMathReference ( reference as MathReference , excludeAlias ) ;
2737
2838 if ( formatted != null ) return formatted ;
2939
@@ -36,12 +46,22 @@ private string FormatObject(object obj)
3646 return reference != null ? FormatColumnClause ( reference ) : obj . ToString ( ) ;
3747 }
3848
39- private string TryFormatAsMathReference ( MathReference mathReference )
49+ private string TryFormatAsMathReference ( MathReference mathReference , bool excludeAlias )
4050 {
4151 if ( ReferenceEquals ( mathReference , null ) ) return null ;
4252
43- return string . Format ( "{0} {1} {2}" , FormatObject ( mathReference . LeftOperand ) ,
44- MathOperatorToString ( mathReference . Operator ) , FormatObject ( mathReference . RightOperand ) ) ;
53+ if ( excludeAlias || mathReference . GetAlias ( ) == null )
54+ {
55+ return string . Format ( "{0} {1} {2}" , FormatObject ( mathReference . LeftOperand ) ,
56+ MathOperatorToString ( mathReference . Operator ) ,
57+ FormatObject ( mathReference . RightOperand ) ) ;
58+ }
59+
60+ return string . Format ( "{0} {1} {2} AS {3}" , FormatObject ( mathReference . LeftOperand ) ,
61+ MathOperatorToString ( mathReference . Operator ) ,
62+ FormatObject ( mathReference . RightOperand ) ,
63+ mathReference . GetAlias ( ) ) ;
64+
4565 }
4666
4767 private static string MathOperatorToString ( MathOperator @operator )
@@ -63,17 +83,20 @@ private static string MathOperatorToString(MathOperator @operator)
6383 }
6484 }
6585
66- private string TryFormatAsFunctionReference ( FunctionReference functionReference )
86+ private string TryFormatAsFunctionReference ( FunctionReference functionReference , bool excludeAlias )
6787 {
6888 if ( ReferenceEquals ( functionReference , null ) ) return null ;
6989
7090 var sqlName = _functionNameConverter . ConvertToSqlName ( functionReference . Name ) ;
71- return functionReference . GetAlias ( ) == null
72- ? string . Format ( "{0}({1}{2})" , sqlName ,
73- FormatColumnClause ( functionReference . Argument ) ,
74- FormatAdditionalArguments ( functionReference . AdditionalArguments ) )
75- : string . Format ( "{0}({1}) AS {2}" , sqlName ,
91+ if ( excludeAlias || functionReference . GetAlias ( ) == null )
92+ {
93+ return string . Format ( "{0}({1}{2})" , sqlName ,
94+ FormatColumnClause ( functionReference . Argument ) ,
95+ FormatAdditionalArguments ( functionReference . AdditionalArguments ) ) ;
96+ }
97+ return string . Format ( "{0}({1}{2}) AS {3}" , sqlName ,
7698 FormatColumnClause ( functionReference . Argument ) ,
99+ FormatAdditionalArguments ( functionReference . AdditionalArguments ) ,
77100 _schema . QuoteObjectName ( functionReference . GetAlias ( ) ) ) ;
78101 }
79102
@@ -88,7 +111,7 @@ private string FormatAdditionalArguments(IEnumerable<object> additionalArguments
88111 return builder != null ? builder . ToString ( ) : string . Empty ;
89112 }
90113
91- private string TryFormatAsObjectReference ( ObjectReference objectReference )
114+ private string TryFormatAsObjectReference ( ObjectReference objectReference , bool excludeAlias )
92115 {
93116 if ( ReferenceEquals ( objectReference , null ) ) return null ;
94117
@@ -97,7 +120,7 @@ private string TryFormatAsObjectReference(ObjectReference objectReference)
97120 ? table . QualifiedName
98121 : _schema . QuoteObjectName ( objectReference . GetOwner ( ) . GetAlias ( ) ) ;
99122 var column = table . FindColumn ( objectReference . GetName ( ) ) ;
100- if ( objectReference . GetAlias ( ) == null )
123+ if ( excludeAlias || objectReference . GetAlias ( ) == null )
101124 return string . Format ( "{0}.{1}" , tableName , column . QuotedName ) ;
102125 else
103126 return string . Format ( "{0}.{1} AS {2}" , tableName , column . QuotedName ,
0 commit comments