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

Commit f4e6677

Browse files
committed
Merge pull request #276 from Lee337/master
JoinSqlBuilder.ToSQL() function did not cater for postgres table names
2 parents 78d4f36 + 282e5b3 commit f4e6677

File tree

1 file changed

+44
-45
lines changed

1 file changed

+44
-45
lines changed

src/ServiceStack.OrmLite/JoinSqlBuilder.cs

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public JoinSqlBuilder()
2323
baseTableName = basePocoType.GetModelDefinition().ModelName;
2424
}
2525

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)
2727
{
2828
var lst = ColumnList(tableName, func, withTablePrefix);
2929
if (lst == null || lst.Count != 1)
@@ -36,7 +36,7 @@ private List<string> ColumnList<T>(string tableName, Expression<Func<T, object>>
3636
List<string> result = new List<string>();
3737
if (func == null || func.Body == null)
3838
return result;
39-
PropertyList(tableName,func.Body, result,withTablePrefix);
39+
PropertyList(tableName, func.Body, result, withTablePrefix);
4040
return result;
4141
}
4242

@@ -55,15 +55,15 @@ private List<string> ColumnList<T>(bool withTablePrefix = true)
5555
return result;
5656
}
5757

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)
5959
{
6060
if (u.NodeType == ExpressionType.Convert)
6161
{
6262
if (u.Method != null)
6363
{
6464
throw new Exception("Invalid Expression provided");
6565
}
66-
PropertyList(tableName,u.Operand, lst,withTablePrefix );
66+
PropertyList(tableName, u.Operand, lst, withTablePrefix);
6767
return;
6868
}
6969
throw new Exception("Invalid Expression provided");
@@ -88,7 +88,7 @@ private void ProcessNew(string tableName, NewExpression nex, List<string> lst, b
8888
if (nex.Arguments == null || nex.Arguments.Count == 0)
8989
throw new Exception("Only column list allowed");
9090
foreach (var arg in nex.Arguments)
91-
PropertyList(tableName,arg, lst,withTablePrefix);
91+
PropertyList(tableName, arg, lst, withTablePrefix);
9292
return;
9393
}
9494

@@ -100,16 +100,16 @@ private void PropertyList(string tableName, Expression exp, List<string> lst, bo
100100
switch (exp.NodeType)
101101
{
102102
case ExpressionType.MemberAccess:
103-
ProcessMemberAccess(tableName,exp as MemberExpression, lst, withTablePrefix );
103+
ProcessMemberAccess(tableName, exp as MemberExpression, lst, withTablePrefix);
104104
return;
105105

106106
case ExpressionType.Convert:
107107
var ue = exp as UnaryExpression;
108-
ProcessUnary(tableName,ue, lst,withTablePrefix );
108+
ProcessUnary(tableName, ue, lst, withTablePrefix);
109109
return;
110110

111111
case ExpressionType.New:
112-
ProcessNew(tableName,exp as NewExpression, lst,withTablePrefix );
112+
ProcessNew(tableName, exp as NewExpression, lst, withTablePrefix);
113113
return;
114114
}
115115
throw new Exception("Only columns are allowed");
@@ -179,13 +179,13 @@ private JoinSqlBuilder<TNewPoco, TBasePoco> SelectGenericAggregate<T>(Expression
179179
isAggregateUsed = true;
180180

181181
CheckAggregateUsage(true);
182-
182+
183183
var columns = ColumnList(associatedType.GetModelDefinition().ModelName, selectColumn);
184184
if ((columns.Count == 0) || (columns.Count > 1))
185185
{
186-
throw new Exception("Expression should select only one Column ");
186+
throw new Exception("Expression should select only one Column ");
187187
}
188-
this.columnList.Add(string.Format(" {0}({1}) ",functionName.ToUpper(),columns[0]));
188+
this.columnList.Add(string.Format(" {0}({1}) ", functionName.ToUpper(), columns[0]));
189189
return this;
190190
}
191191

@@ -199,23 +199,23 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> Where<T>(Expression<Func<T, bool>> wh
199199
{
200200
return WhereInternal(WhereType.AND, where);
201201
}
202-
202+
203203
public JoinSqlBuilder<TNewPoco, TBasePoco> Or<T>(Expression<Func<T, bool>> where)
204204
{
205205
return WhereInternal(WhereType.OR, where);
206206
}
207-
207+
208208
public JoinSqlBuilder<TNewPoco, TBasePoco> And<T>(Expression<Func<T, bool>> where)
209209
{
210210
return WhereInternal(WhereType.AND, where);
211211
}
212212

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)
214214
{
215215
Type associatedType = this.PreviousAssociatedType(typeof(T), typeof(T));
216216
if (associatedType == null)
217217
{
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 ");
219219
}
220220
var ev = OrmLiteConfig.DialectProvider.ExpressionVisitor<T>();
221221
ev.WhereStatementWithoutWhereString = true;
@@ -224,7 +224,7 @@ private JoinSqlBuilder<TNewPoco, TBasePoco> WhereInternal<T>(WhereType whereType
224224
var str = ev.WhereExpression;
225225
if (String.IsNullOrEmpty(str) == false)
226226
{
227-
this.whereList.Add(new KeyValuePair<string,WhereType>(str,whereType));
227+
this.whereList.Add(new KeyValuePair<string, WhereType>(str, whereType));
228228
}
229229
return this;
230230
}
@@ -239,7 +239,7 @@ private JoinSqlBuilder<TNewPoco, TBasePoco> OrderByInternal<T>(bool byDesc, Expr
239239

240240
var lst = ColumnList(associatedType.GetModelDefinition().ModelName, orderByColumns);
241241
foreach (var item in lst)
242-
orderByList.Add(new KeyValuePair<string,bool>(item,!byDesc));
242+
orderByList.Add(new KeyValuePair<string, bool>(item, !byDesc));
243243
return this;
244244
}
245245

@@ -255,7 +255,7 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> Clear()
255255

256256
public JoinSqlBuilder<TNewPoco, TBasePoco> OrderBy<T>(Expression<Func<T, object>> sourceColumn)
257257
{
258-
return OrderByInternal<T>(false, sourceColumn);
258+
return OrderByInternal<T>(false, sourceColumn);
259259
}
260260

261261
public JoinSqlBuilder<TNewPoco, TBasePoco> OrderByDescending<T>(Expression<Func<T, object>> sourceColumn)
@@ -265,12 +265,12 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> OrderByDescending<T>(Expression<Func<
265265

266266
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)
267267
{
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);
269269
}
270270

271271
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)
272272
{
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);
274274
}
275275

276276
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
306306
join.JoinType = joinType;
307307
join.Class1Type = typeof(TSourceTable);
308308
join.Class2Type = typeof(TDestinationTable);
309-
309+
310310
if (associatedType == join.Class1Type)
311311
join.RefType = join.Class2Type;
312312
else
@@ -315,29 +315,29 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> CrossJoin<TSourceTable, TDestinationT
315315
join.Class1TableName = join.Class1Type.GetModelDefinition().ModelName;
316316
join.Class2TableName = join.Class2Type.GetModelDefinition().ModelName;
317317
join.RefTypeTableName = join.RefType.GetModelDefinition().ModelName;
318-
318+
319319
if (join.JoinType != JoinType.CROSS)
320320
{
321321
if (join.JoinType == JoinType.SELF)
322322
{
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);
325325
}
326326
else
327327
{
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);
330330
}
331331
}
332332

333333
if (sourceTableColumnSelection != null)
334334
{
335-
columnList.AddRange(ColumnList<TSourceTable>(join.Class1TableName,sourceTableColumnSelection));
335+
columnList.AddRange(ColumnList<TSourceTable>(join.Class1TableName, sourceTableColumnSelection));
336336
}
337337

338338
if (destinationTableColumnSelection != null)
339339
{
340-
columnList.AddRange(ColumnList<TDestinationTable>(join.Class2TableName,destinationTableColumnSelection));
340+
columnList.AddRange(ColumnList<TDestinationTable>(join.Class2TableName, destinationTableColumnSelection));
341341
}
342342

343343
if (sourceWhere != null)
@@ -346,7 +346,7 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> CrossJoin<TSourceTable, TDestinationT
346346
ev.Where(sourceWhere);
347347
var where = ev.WhereExpression;
348348
if (!String.IsNullOrEmpty(where))
349-
whereList.Add(new KeyValuePair<string, WhereType>(where,WhereType.AND));
349+
whereList.Add(new KeyValuePair<string, WhereType>(where, WhereType.AND));
350350
}
351351

352352
if (destinationWhere != null)
@@ -357,7 +357,7 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> CrossJoin<TSourceTable, TDestinationT
357357
if (!String.IsNullOrEmpty(where))
358358
whereList.Add(new KeyValuePair<string, WhereType>(where, WhereType.AND));
359359
}
360-
360+
361361
joinObjList.Add(join);
362362
return this;
363363
}
@@ -385,7 +385,7 @@ private Type PreviousAssociatedType(Type sourceTableType, Type destinationTableT
385385

386386
private void CheckAggregateUsage(bool ignoreCurrentItem)
387387
{
388-
if ((columnList.Count > ( ignoreCurrentItem ? 0 : 1)) && (isAggregateUsed == true))
388+
if ((columnList.Count > (ignoreCurrentItem ? 0 : 1)) && (isAggregateUsed == true))
389389
{
390390
throw new Exception("Aggregate function cannot be used with non aggregate select columns");
391391
}
@@ -415,17 +415,17 @@ public string ToSql()
415415
if (isDistinct && typeof(TNewPoco).GetModelDefinition().FieldDefinitions.Count > 0)
416416
sb.Append(" DISTINCT ");
417417

418-
foreach ( var fi in typeof(TNewPoco).GetModelDefinition().FieldDefinitions)
418+
foreach (var fi in typeof(TNewPoco).GetModelDefinition().FieldDefinitions)
419419
{
420420
colSB.AppendFormat("{0}{1}", colSB.Length > 0 ? "," : "", String.IsNullOrEmpty(fi.BelongToModelName) ? (fi.FieldName) : ((OrmLiteConfig.DialectProvider.GetQuotedTableName(fi.BelongToModelName) + "." + OrmLiteConfig.DialectProvider.GetQuotedColumnName(fi.FieldName))));
421421
}
422422
if (colSB.Length == 0)
423-
colSB.AppendFormat("\"{0}\".*", baseTableName);
423+
colSB.AppendFormat("\"{0}\".*", OrmLiteConfig.DialectProvider.GetQuotedTableName(baseTableName));
424424
}
425425

426426
sb.Append(colSB.ToString() + " \n");
427427

428-
sb.AppendFormat("FROM {0} \n", baseTableName);
428+
sb.AppendFormat("FROM {0} \n", OrmLiteConfig.DialectProvider.GetQuotedTableName(baseTableName));
429429
int i = 0;
430430
foreach (var join in joinList)
431431
{
@@ -445,17 +445,17 @@ public string ToSql()
445445

446446
if (join.JoinType == JoinType.CROSS)
447447
{
448-
sb.AppendFormat(" {0} ON {1} = {2} \n", join.RefTypeTableName);
448+
sb.AppendFormat(" {0} ON {1} = {2} \n", OrmLiteConfig.DialectProvider.GetQuotedTableName(join.RefTypeTableName));
449449
}
450450
else
451451
{
452452
if (join.JoinType != JoinType.SELF)
453453
{
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);
455455
}
456456
else
457457
{
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);
459459
}
460460
}
461461
}
@@ -470,14 +470,14 @@ public string ToSql()
470470
sb.Append("WHERE " + whereSB.ToString() + " \n");
471471
}
472472

473-
if (orderByList.Count > 0 )
473+
if (orderByList.Count > 0)
474474
{
475475
var orderBySB = new StringBuilder();
476476
foreach (var ob in orderByList)
477477
{
478478
orderBySB.AppendFormat("{0}{1} {2} ", orderBySB.Length > 0 ? "," : "", ob.Key, ob.Value ? "ASC" : "DESC");
479479
}
480-
sb.Append("ORDER BY " + orderBySB.ToString()+" \n");
480+
sb.Append("ORDER BY " + orderBySB.ToString() + " \n");
481481
}
482482

483483
return sb.ToString();
@@ -503,15 +503,14 @@ enum JoinType
503503

504504
class Join
505505
{
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; }
509509
public JoinType JoinType { get; set; }
510510
public string Class1TableName { get; set; }
511511
public string Class2TableName { get; set; }
512512
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; }
515515
}
516-
517516
}

0 commit comments

Comments
 (0)