@@ -11,6 +11,8 @@ public class JoinSqlBuilder<TNewPoco, TBasePoco>
11
11
private List < KeyValuePair < string , WhereType > > whereList = new List < KeyValuePair < string , WhereType > > ( ) ;
12
12
private List < string > columnList = new List < string > ( ) ;
13
13
private List < KeyValuePair < string , bool > > orderByList = new List < KeyValuePair < string , bool > > ( ) ;
14
+ private bool isDistinct = false ;
15
+ private bool isAggregateUsed = false ;
14
16
15
17
private string baseTableName = "" ;
16
18
private Type basePocoType ;
@@ -38,6 +40,21 @@ private List<string> ColumnList<T>(string tableName, Expression<Func<T, object>>
38
40
return result ;
39
41
}
40
42
43
+ private List < string > ColumnList < T > ( bool withTablePrefix = true )
44
+ {
45
+ var pocoType = typeof ( T ) ;
46
+ var tableName = pocoType . GetModelDefinition ( ) . ModelName ;
47
+ List < string > result = new List < string > ( pocoType . GetModelDefinition ( ) . FieldDefinitions . Count ) ;
48
+ foreach ( var item in pocoType . GetModelDefinition ( ) . FieldDefinitions )
49
+ {
50
+ if ( withTablePrefix )
51
+ result . Add ( string . Format ( "{0}.{1}" , OrmLiteConfig . DialectProvider . GetQuotedTableName ( tableName ) , OrmLiteConfig . DialectProvider . GetQuotedColumnName ( item . Name ) ) ) ;
52
+ else
53
+ result . Add ( string . Format ( "{0}" , OrmLiteConfig . DialectProvider . GetQuotedColumnName ( item . Name ) ) ) ;
54
+ }
55
+ return result ;
56
+ }
57
+
41
58
private void ProcessUnary ( string tableName , UnaryExpression u , List < string > lst , bool withTablePrefix )
42
59
{
43
60
if ( u . NodeType == ExpressionType . Convert )
@@ -110,6 +127,74 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> Select<T>(Expression<Func<T, object>>
110
127
return this ;
111
128
}
112
129
130
+ public JoinSqlBuilder < TNewPoco , TBasePoco > SelectAll < T > ( )
131
+ {
132
+ Type associatedType = this . PreviousAssociatedType ( typeof ( T ) , typeof ( T ) ) ;
133
+ if ( associatedType == null )
134
+ {
135
+ throw new Exception ( "Either the source or destination table should be associated " ) ;
136
+ }
137
+ this . columnList . AddRange ( ColumnList < T > ( ) ) ;
138
+ return this ;
139
+ }
140
+
141
+ public JoinSqlBuilder < TNewPoco , TBasePoco > SelectDistinct ( )
142
+ {
143
+ isDistinct = true ;
144
+ return this ;
145
+ }
146
+
147
+ public JoinSqlBuilder < TNewPoco , TBasePoco > SelectMax < T > ( Expression < Func < T , object > > selectColumn )
148
+ {
149
+ return SelectGenericAggregate < T > ( selectColumn , "MAX" ) ;
150
+ }
151
+
152
+ public JoinSqlBuilder < TNewPoco , TBasePoco > SelectMin < T > ( Expression < Func < T , object > > selectColumn )
153
+ {
154
+ return SelectGenericAggregate < T > ( selectColumn , "MIN" ) ;
155
+ }
156
+
157
+ public JoinSqlBuilder < TNewPoco , TBasePoco > SelectCount < T > ( Expression < Func < T , object > > selectColumn )
158
+ {
159
+ return SelectGenericAggregate < T > ( selectColumn , "COUNT" ) ;
160
+ }
161
+
162
+ public JoinSqlBuilder < TNewPoco , TBasePoco > SelectAverage < T > ( Expression < Func < T , object > > selectColumn )
163
+ {
164
+ return SelectGenericAggregate < T > ( selectColumn , "AVG" ) ;
165
+ }
166
+
167
+ public JoinSqlBuilder < TNewPoco , TBasePoco > SelectSum < T > ( Expression < Func < T , object > > selectColumn )
168
+ {
169
+ return SelectGenericAggregate < T > ( selectColumn , "SUM" ) ;
170
+ }
171
+
172
+ private JoinSqlBuilder < TNewPoco , TBasePoco > SelectGenericAggregate < T > ( Expression < Func < T , object > > selectColumn , string functionName )
173
+ {
174
+ Type associatedType = this . PreviousAssociatedType ( typeof ( T ) , typeof ( T ) ) ;
175
+ if ( associatedType == null )
176
+ {
177
+ throw new Exception ( "Either the source or destination table should be associated " ) ;
178
+ }
179
+ isAggregateUsed = true ;
180
+
181
+ CheckAggregateUsage ( true ) ;
182
+
183
+ var columns = ColumnList ( associatedType . GetModelDefinition ( ) . ModelName , selectColumn ) ;
184
+ if ( ( columns . Count == 0 ) || ( columns . Count > 1 ) )
185
+ {
186
+ throw new Exception ( "Expression should select only one Column " ) ;
187
+ }
188
+ this . columnList . Add ( string . Format ( " {0}({1}) " , functionName . ToUpper ( ) , columns [ 0 ] ) ) ;
189
+ return this ;
190
+ }
191
+
192
+ public JoinSqlBuilder < TNewPoco , TBasePoco > SelectMin ( )
193
+ {
194
+ isDistinct = true ;
195
+ return this ;
196
+ }
197
+
113
198
public JoinSqlBuilder < TNewPoco , TBasePoco > Where < T > ( Expression < Func < T , bool > > where )
114
199
{
115
200
return WhereInternal ( WhereType . AND , where ) ;
@@ -298,22 +383,38 @@ private Type PreviousAssociatedType(Type sourceTableType, Type destinationTableT
298
383
return null ;
299
384
}
300
385
386
+ private void CheckAggregateUsage ( bool ignoreCurrentItem )
387
+ {
388
+ if ( ( columnList . Count > ( ignoreCurrentItem ? 0 : 1 ) ) && ( isAggregateUsed == true ) )
389
+ {
390
+ throw new Exception ( "Aggregate function cannot be used with non aggregate select columns" ) ;
391
+ }
392
+ }
393
+
301
394
public string ToSql ( )
302
395
{
396
+ CheckAggregateUsage ( false ) ;
397
+
303
398
var sb = new StringBuilder ( ) ;
304
399
sb . Append ( "SELECT " ) ;
305
400
306
401
var colSB = new StringBuilder ( ) ;
307
402
308
403
if ( columnList . Count > 0 )
309
404
{
405
+ if ( isDistinct )
406
+ sb . Append ( " DISTINCT " ) ;
407
+
310
408
foreach ( var col in columnList )
311
409
{
312
410
colSB . AppendFormat ( "{0}{1}" , colSB . Length > 0 ? "," : "" , col ) ;
313
411
}
314
412
}
315
413
else
316
414
{
415
+ if ( isDistinct && typeof ( TNewPoco ) . GetModelDefinition ( ) . FieldDefinitions . Count > 0 )
416
+ sb . Append ( " DISTINCT " ) ;
417
+
317
418
foreach ( var fi in typeof ( TNewPoco ) . GetModelDefinition ( ) . FieldDefinitions )
318
419
{
319
420
colSB . AppendFormat ( "{0}{1}" , colSB . Length > 0 ? "," : "" , String . IsNullOrEmpty ( fi . BelongToModelName ) ? ( fi . FieldName ) : ( ( OrmLiteConfig . DialectProvider . GetQuotedTableName ( fi . BelongToModelName ) + "." + OrmLiteConfig . DialectProvider . GetQuotedColumnName ( fi . FieldName ) ) ) ) ;
0 commit comments