@@ -23,7 +23,7 @@ public JoinSqlBuilder()
23
23
baseTableName = basePocoType . GetModelDefinition ( ) . ModelName ;
24
24
}
25
25
26
- private string Column < T > ( string tableName , Expression < Func < T , object > > func , bool withTablePrefix )
26
+ private string Column < T > ( string tableName , Expression < Func < T , object > > func , bool withTablePrefix )
27
27
{
28
28
var lst = ColumnList ( tableName , func , withTablePrefix ) ;
29
29
if ( lst == null || lst . Count != 1 )
@@ -36,7 +36,7 @@ private List<string> ColumnList<T>(string tableName, Expression<Func<T, object>>
36
36
List < string > result = new List < string > ( ) ;
37
37
if ( func == null || func . Body == null )
38
38
return result ;
39
- PropertyList ( tableName , func . Body , result , withTablePrefix ) ;
39
+ PropertyList ( tableName , func . Body , result , withTablePrefix ) ;
40
40
return result ;
41
41
}
42
42
@@ -55,15 +55,15 @@ private List<string> ColumnList<T>(bool withTablePrefix = true)
55
55
return result ;
56
56
}
57
57
58
- private void ProcessUnary ( string tableName , UnaryExpression u , List < string > lst , bool withTablePrefix )
58
+ private void ProcessUnary ( string tableName , UnaryExpression u , List < string > lst , bool withTablePrefix )
59
59
{
60
60
if ( u . NodeType == ExpressionType . Convert )
61
61
{
62
62
if ( u . Method != null )
63
63
{
64
64
throw new Exception ( "Invalid Expression provided" ) ;
65
65
}
66
- PropertyList ( tableName , u . Operand , lst , withTablePrefix ) ;
66
+ PropertyList ( tableName , u . Operand , lst , withTablePrefix ) ;
67
67
return ;
68
68
}
69
69
throw new Exception ( "Invalid Expression provided" ) ;
@@ -88,7 +88,7 @@ private void ProcessNew(string tableName, NewExpression nex, List<string> lst, b
88
88
if ( nex . Arguments == null || nex . Arguments . Count == 0 )
89
89
throw new Exception ( "Only column list allowed" ) ;
90
90
foreach ( var arg in nex . Arguments )
91
- PropertyList ( tableName , arg , lst , withTablePrefix ) ;
91
+ PropertyList ( tableName , arg , lst , withTablePrefix ) ;
92
92
return ;
93
93
}
94
94
@@ -100,16 +100,16 @@ private void PropertyList(string tableName, Expression exp, List<string> lst, bo
100
100
switch ( exp . NodeType )
101
101
{
102
102
case ExpressionType . MemberAccess :
103
- ProcessMemberAccess ( tableName , exp as MemberExpression , lst , withTablePrefix ) ;
103
+ ProcessMemberAccess ( tableName , exp as MemberExpression , lst , withTablePrefix ) ;
104
104
return ;
105
105
106
106
case ExpressionType . Convert :
107
107
var ue = exp as UnaryExpression ;
108
- ProcessUnary ( tableName , ue , lst , withTablePrefix ) ;
108
+ ProcessUnary ( tableName , ue , lst , withTablePrefix ) ;
109
109
return ;
110
110
111
111
case ExpressionType . New :
112
- ProcessNew ( tableName , exp as NewExpression , lst , withTablePrefix ) ;
112
+ ProcessNew ( tableName , exp as NewExpression , lst , withTablePrefix ) ;
113
113
return ;
114
114
}
115
115
throw new Exception ( "Only columns are allowed" ) ;
@@ -179,13 +179,13 @@ private JoinSqlBuilder<TNewPoco, TBasePoco> SelectGenericAggregate<T>(Expression
179
179
isAggregateUsed = true ;
180
180
181
181
CheckAggregateUsage ( true ) ;
182
-
182
+
183
183
var columns = ColumnList ( associatedType . GetModelDefinition ( ) . ModelName , selectColumn ) ;
184
184
if ( ( columns . Count == 0 ) || ( columns . Count > 1 ) )
185
185
{
186
- throw new Exception ( "Expression should select only one Column " ) ;
186
+ throw new Exception ( "Expression should select only one Column " ) ;
187
187
}
188
- this . columnList . Add ( string . Format ( " {0}({1}) " , functionName . ToUpper ( ) , columns [ 0 ] ) ) ;
188
+ this . columnList . Add ( string . Format ( " {0}({1}) " , functionName . ToUpper ( ) , columns [ 0 ] ) ) ;
189
189
return this ;
190
190
}
191
191
@@ -199,23 +199,23 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> Where<T>(Expression<Func<T, bool>> wh
199
199
{
200
200
return WhereInternal ( WhereType . AND , where ) ;
201
201
}
202
-
202
+
203
203
public JoinSqlBuilder < TNewPoco , TBasePoco > Or < T > ( Expression < Func < T , bool > > where )
204
204
{
205
205
return WhereInternal ( WhereType . OR , where ) ;
206
206
}
207
-
207
+
208
208
public JoinSqlBuilder < TNewPoco , TBasePoco > And < T > ( Expression < Func < T , bool > > where )
209
209
{
210
210
return WhereInternal ( WhereType . AND , where ) ;
211
211
}
212
212
213
- private JoinSqlBuilder < TNewPoco , TBasePoco > WhereInternal < T > ( WhereType whereType , Expression < Func < T , bool > > where )
213
+ private JoinSqlBuilder < TNewPoco , TBasePoco > WhereInternal < T > ( WhereType whereType , Expression < Func < T , bool > > where )
214
214
{
215
215
Type associatedType = this . PreviousAssociatedType ( typeof ( T ) , typeof ( T ) ) ;
216
216
if ( associatedType == null )
217
217
{
218
- throw new Exception ( "Either the source or destination table should be associated " ) ;
218
+ throw new Exception ( "Either the source or destination table should be associated " ) ;
219
219
}
220
220
var ev = OrmLiteConfig . DialectProvider . ExpressionVisitor < T > ( ) ;
221
221
ev . WhereStatementWithoutWhereString = true ;
@@ -224,7 +224,7 @@ private JoinSqlBuilder<TNewPoco, TBasePoco> WhereInternal<T>(WhereType whereType
224
224
var str = ev . WhereExpression ;
225
225
if ( String . IsNullOrEmpty ( str ) == false )
226
226
{
227
- this . whereList . Add ( new KeyValuePair < string , WhereType > ( str , whereType ) ) ;
227
+ this . whereList . Add ( new KeyValuePair < string , WhereType > ( str , whereType ) ) ;
228
228
}
229
229
return this ;
230
230
}
@@ -239,7 +239,7 @@ private JoinSqlBuilder<TNewPoco, TBasePoco> OrderByInternal<T>(bool byDesc, Expr
239
239
240
240
var lst = ColumnList ( associatedType . GetModelDefinition ( ) . ModelName , orderByColumns ) ;
241
241
foreach ( var item in lst )
242
- orderByList . Add ( new KeyValuePair < string , bool > ( item , ! byDesc ) ) ;
242
+ orderByList . Add ( new KeyValuePair < string , bool > ( item , ! byDesc ) ) ;
243
243
return this ;
244
244
}
245
245
@@ -255,7 +255,7 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> Clear()
255
255
256
256
public JoinSqlBuilder < TNewPoco , TBasePoco > OrderBy < T > ( Expression < Func < T , object > > sourceColumn )
257
257
{
258
- return OrderByInternal < T > ( false , sourceColumn ) ;
258
+ return OrderByInternal < T > ( false , sourceColumn ) ;
259
259
}
260
260
261
261
public JoinSqlBuilder < TNewPoco , TBasePoco > OrderByDescending < T > ( Expression < Func < T , object > > sourceColumn )
@@ -265,12 +265,12 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> OrderByDescending<T>(Expression<Func<
265
265
266
266
public JoinSqlBuilder < TNewPoco , TBasePoco > Join < TSourceTable , TDestinationTable > ( Expression < Func < TSourceTable , object > > sourceColumn , Expression < Func < TDestinationTable , object > > destinationColumn , Expression < Func < TSourceTable , object > > sourceTableColumnSelection = null , Expression < Func < TDestinationTable , object > > destinationTableColumnSelection = null , Expression < Func < TSourceTable , bool > > sourceWhere = null , Expression < Func < TDestinationTable , bool > > destinationWhere = null )
267
267
{
268
- return JoinInternal < Join , TSourceTable , TDestinationTable > ( JoinType . INNER , joinList , sourceColumn , destinationColumn , sourceTableColumnSelection , destinationTableColumnSelection , sourceWhere , destinationWhere ) ;
268
+ return JoinInternal < Join , TSourceTable , TDestinationTable > ( JoinType . INNER , joinList , sourceColumn , destinationColumn , sourceTableColumnSelection , destinationTableColumnSelection , sourceWhere , destinationWhere ) ;
269
269
}
270
270
271
271
public JoinSqlBuilder < TNewPoco , TBasePoco > LeftJoin < TSourceTable , TDestinationTable > ( Expression < Func < TSourceTable , object > > sourceColumn , Expression < Func < TDestinationTable , object > > destinationColumn , Expression < Func < TSourceTable , object > > sourceTableColumnSelection = null , Expression < Func < TDestinationTable , object > > destinationTableColumnSelection = null , Expression < Func < TSourceTable , bool > > sourceWhere = null , Expression < Func < TDestinationTable , bool > > destinationWhere = null )
272
272
{
273
- return JoinInternal < Join , TSourceTable , TDestinationTable > ( JoinType . LEFTOUTER , joinList , sourceColumn , destinationColumn , sourceTableColumnSelection , destinationTableColumnSelection , sourceWhere , destinationWhere ) ;
273
+ return JoinInternal < Join , TSourceTable , TDestinationTable > ( JoinType . LEFTOUTER , joinList , sourceColumn , destinationColumn , sourceTableColumnSelection , destinationTableColumnSelection , sourceWhere , destinationWhere ) ;
274
274
}
275
275
276
276
public JoinSqlBuilder < TNewPoco , TBasePoco > RightJoin < TSourceTable , TDestinationTable > ( Expression < Func < TSourceTable , object > > sourceColumn , Expression < Func < TDestinationTable , object > > destinationColumn , Expression < Func < TSourceTable , object > > sourceTableColumnSelection = null , Expression < Func < TDestinationTable , object > > destinationTableColumnSelection = null , Expression < Func < TSourceTable , bool > > sourceWhere = null , Expression < Func < TDestinationTable , bool > > destinationWhere = null )
@@ -306,7 +306,7 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> CrossJoin<TSourceTable, TDestinationT
306
306
join . JoinType = joinType ;
307
307
join . Class1Type = typeof ( TSourceTable ) ;
308
308
join . Class2Type = typeof ( TDestinationTable ) ;
309
-
309
+
310
310
if ( associatedType == join . Class1Type )
311
311
join . RefType = join . Class2Type ;
312
312
else
@@ -315,29 +315,29 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> CrossJoin<TSourceTable, TDestinationT
315
315
join . Class1TableName = join . Class1Type . GetModelDefinition ( ) . ModelName ;
316
316
join . Class2TableName = join . Class2Type . GetModelDefinition ( ) . ModelName ;
317
317
join . RefTypeTableName = join . RefType . GetModelDefinition ( ) . ModelName ;
318
-
318
+
319
319
if ( join . JoinType != JoinType . CROSS )
320
320
{
321
321
if ( join . JoinType == JoinType . SELF )
322
322
{
323
- join . Class1ColumnName = Column < TSourceTable > ( join . Class1TableName , sourceColumn , false ) ;
324
- join . Class2ColumnName = Column < TDestinationTable > ( join . Class2TableName , destinationColumn , false ) ;
323
+ join . Class1ColumnName = Column < TSourceTable > ( join . Class1TableName , sourceColumn , false ) ;
324
+ join . Class2ColumnName = Column < TDestinationTable > ( join . Class2TableName , destinationColumn , false ) ;
325
325
}
326
326
else
327
327
{
328
- join . Class1ColumnName = Column < TSourceTable > ( join . Class1TableName , sourceColumn , true ) ;
329
- join . Class2ColumnName = Column < TDestinationTable > ( join . Class2TableName , destinationColumn , true ) ;
328
+ join . Class1ColumnName = Column < TSourceTable > ( join . Class1TableName , sourceColumn , true ) ;
329
+ join . Class2ColumnName = Column < TDestinationTable > ( join . Class2TableName , destinationColumn , true ) ;
330
330
}
331
331
}
332
332
333
333
if ( sourceTableColumnSelection != null )
334
334
{
335
- columnList . AddRange ( ColumnList < TSourceTable > ( join . Class1TableName , sourceTableColumnSelection ) ) ;
335
+ columnList . AddRange ( ColumnList < TSourceTable > ( join . Class1TableName , sourceTableColumnSelection ) ) ;
336
336
}
337
337
338
338
if ( destinationTableColumnSelection != null )
339
339
{
340
- columnList . AddRange ( ColumnList < TDestinationTable > ( join . Class2TableName , destinationTableColumnSelection ) ) ;
340
+ columnList . AddRange ( ColumnList < TDestinationTable > ( join . Class2TableName , destinationTableColumnSelection ) ) ;
341
341
}
342
342
343
343
if ( sourceWhere != null )
@@ -346,7 +346,7 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> CrossJoin<TSourceTable, TDestinationT
346
346
ev . Where ( sourceWhere ) ;
347
347
var where = ev . WhereExpression ;
348
348
if ( ! String . IsNullOrEmpty ( where ) )
349
- whereList . Add ( new KeyValuePair < string , WhereType > ( where , WhereType . AND ) ) ;
349
+ whereList . Add ( new KeyValuePair < string , WhereType > ( where , WhereType . AND ) ) ;
350
350
}
351
351
352
352
if ( destinationWhere != null )
@@ -357,7 +357,7 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> CrossJoin<TSourceTable, TDestinationT
357
357
if ( ! String . IsNullOrEmpty ( where ) )
358
358
whereList . Add ( new KeyValuePair < string , WhereType > ( where , WhereType . AND ) ) ;
359
359
}
360
-
360
+
361
361
joinObjList . Add ( join ) ;
362
362
return this ;
363
363
}
@@ -385,7 +385,7 @@ private Type PreviousAssociatedType(Type sourceTableType, Type destinationTableT
385
385
386
386
private void CheckAggregateUsage ( bool ignoreCurrentItem )
387
387
{
388
- if ( ( columnList . Count > ( ignoreCurrentItem ? 0 : 1 ) ) && ( isAggregateUsed == true ) )
388
+ if ( ( columnList . Count > ( ignoreCurrentItem ? 0 : 1 ) ) && ( isAggregateUsed == true ) )
389
389
{
390
390
throw new Exception ( "Aggregate function cannot be used with non aggregate select columns" ) ;
391
391
}
@@ -415,17 +415,17 @@ public string ToSql()
415
415
if ( isDistinct && typeof ( TNewPoco ) . GetModelDefinition ( ) . FieldDefinitions . Count > 0 )
416
416
sb . Append ( " DISTINCT " ) ;
417
417
418
- foreach ( var fi in typeof ( TNewPoco ) . GetModelDefinition ( ) . FieldDefinitions )
418
+ foreach ( var fi in typeof ( TNewPoco ) . GetModelDefinition ( ) . FieldDefinitions )
419
419
{
420
420
colSB . AppendFormat ( "{0}{1}" , colSB . Length > 0 ? "," : "" , String . IsNullOrEmpty ( fi . BelongToModelName ) ? ( fi . FieldName ) : ( ( OrmLiteConfig . DialectProvider . GetQuotedTableName ( fi . BelongToModelName ) + "." + OrmLiteConfig . DialectProvider . GetQuotedColumnName ( fi . FieldName ) ) ) ) ;
421
421
}
422
422
if ( colSB . Length == 0 )
423
- colSB . AppendFormat ( "\" {0}\" .*" , baseTableName ) ;
423
+ colSB . AppendFormat ( "\" {0}\" .*" , OrmLiteConfig . DialectProvider . GetQuotedTableName ( baseTableName ) ) ;
424
424
}
425
425
426
426
sb . Append ( colSB . ToString ( ) + " \n " ) ;
427
427
428
- sb . AppendFormat ( "FROM {0} \n " , baseTableName ) ;
428
+ sb . AppendFormat ( "FROM {0} \n " , OrmLiteConfig . DialectProvider . GetQuotedTableName ( baseTableName ) ) ;
429
429
int i = 0 ;
430
430
foreach ( var join in joinList )
431
431
{
@@ -445,17 +445,17 @@ public string ToSql()
445
445
446
446
if ( join . JoinType == JoinType . CROSS )
447
447
{
448
- sb . AppendFormat ( " {0} ON {1} = {2} \n " , join . RefTypeTableName ) ;
448
+ sb . AppendFormat ( " {0} ON {1} = {2} \n " , OrmLiteConfig . DialectProvider . GetQuotedTableName ( join . RefTypeTableName ) ) ;
449
449
}
450
450
else
451
451
{
452
452
if ( join . JoinType != JoinType . SELF )
453
453
{
454
- sb . AppendFormat ( " {0} ON {1} = {2} \n " , join . RefTypeTableName , join . Class1ColumnName , join . Class2ColumnName ) ;
454
+ sb . AppendFormat ( " {0} ON {1} = {2} \n " , OrmLiteConfig . DialectProvider . GetQuotedTableName ( join . RefTypeTableName ) , join . Class1ColumnName , join . Class2ColumnName ) ;
455
455
}
456
456
else
457
457
{
458
- sb . AppendFormat ( " {0} AS {1} ON {1}.{2} = \" {0}\" .{3} \n " , join . RefTypeTableName , join . RefTypeTableName + "_" + i . ToString ( ) , join . Class1ColumnName , join . Class2ColumnName ) ;
458
+ sb . AppendFormat ( " {0} AS {1} ON {1}.{2} = \" {0}\" .{3} \n " , OrmLiteConfig . DialectProvider . GetQuotedTableName ( join . RefTypeTableName ) , OrmLiteConfig . DialectProvider . GetQuotedTableName ( join . RefTypeTableName ) + "_" + i . ToString ( ) , join . Class1ColumnName , join . Class2ColumnName ) ;
459
459
}
460
460
}
461
461
}
@@ -470,14 +470,14 @@ public string ToSql()
470
470
sb . Append ( "WHERE " + whereSB . ToString ( ) + " \n " ) ;
471
471
}
472
472
473
- if ( orderByList . Count > 0 )
473
+ if ( orderByList . Count > 0 )
474
474
{
475
475
var orderBySB = new StringBuilder ( ) ;
476
476
foreach ( var ob in orderByList )
477
477
{
478
478
orderBySB . AppendFormat ( "{0}{1} {2} " , orderBySB . Length > 0 ? "," : "" , ob . Key , ob . Value ? "ASC" : "DESC" ) ;
479
479
}
480
- sb . Append ( "ORDER BY " + orderBySB . ToString ( ) + " \n " ) ;
480
+ sb . Append ( "ORDER BY " + orderBySB . ToString ( ) + " \n " ) ;
481
481
}
482
482
483
483
return sb . ToString ( ) ;
@@ -503,15 +503,14 @@ enum JoinType
503
503
504
504
class Join
505
505
{
506
- public Type Class1Type { get ; set ; }
507
- public Type Class2Type { get ; set ; }
508
- public Type RefType { get ; set ; }
506
+ public Type Class1Type { get ; set ; }
507
+ public Type Class2Type { get ; set ; }
508
+ public Type RefType { get ; set ; }
509
509
public JoinType JoinType { get ; set ; }
510
510
public string Class1TableName { get ; set ; }
511
511
public string Class2TableName { get ; set ; }
512
512
public string RefTypeTableName { get ; set ; }
513
- public string Class1ColumnName { get ; set ; }
514
- public string Class2ColumnName { get ; set ; }
513
+ public string Class1ColumnName { get ; set ; }
514
+ public string Class2ColumnName { get ; set ; }
515
515
}
516
-
517
516
}
0 commit comments