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

Commit 869afc9

Browse files
committed
added schema prefix to JoinSqlBuilder for building queries
1 parent 768d3aa commit 869afc9

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/ServiceStack.OrmLite/JoinSqlBuilder.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ public class JoinSqlBuilder<TNewPoco, TBasePoco>
1414
private bool isDistinct = false;
1515
private bool isAggregateUsed = false;
1616

17+
private string baseSchema = "";
1718
private string baseTableName = "";
1819
private Type basePocoType;
1920

2021
public JoinSqlBuilder()
2122
{
2223
basePocoType = typeof(TBasePoco);
24+
baseSchema = GetSchema(basePocoType);
2325
baseTableName = basePocoType.GetModelDefinition().ModelName;
2426
}
2527

@@ -89,7 +91,7 @@ private void ProcessNew(string tableName, NewExpression nex, List<string> lst, b
8991
throw new Exception("Only column list allowed");
9092

9193
var expressionProperties = nex.Type.GetProperties();
92-
for (int i=0; i< nex.Arguments.Count;i++)
94+
for (int i = 0; i < nex.Arguments.Count; i++)
9395
{
9496
var arg = nex.Arguments[i];
9597
var alias = expressionProperties[i].Name;
@@ -319,8 +321,11 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> CrossJoin<TSourceTable, TDestinationT
319321
else
320322
join.RefType = join.Class1Type;
321323

324+
join.Class1Schema = GetSchema(join.Class1Type);
322325
join.Class1TableName = join.Class1Type.GetModelDefinition().ModelName;
326+
join.Class2Schema = GetSchema(join.Class2Type);
323327
join.Class2TableName = join.Class2Type.GetModelDefinition().ModelName;
328+
join.RefTypeSchema = GetSchema(join.RefType);
324329
join.RefTypeTableName = join.RefType.GetModelDefinition().ModelName;
325330

326331
if (join.JoinType != JoinType.CROSS)
@@ -369,6 +374,11 @@ public JoinSqlBuilder<TNewPoco, TBasePoco> CrossJoin<TSourceTable, TDestinationT
369374
return this;
370375
}
371376

377+
private string GetSchema(Type type)
378+
{
379+
return string.IsNullOrEmpty(type.GetModelDefinition().Schema) ? string.Empty : string.Format("{0}.", type.GetModelDefinition().Schema);
380+
}
381+
372382
private Type PreviousAssociatedType(Type sourceTableType, Type destinationTableType)
373383
{
374384
if (sourceTableType == basePocoType || destinationTableType == basePocoType)
@@ -427,12 +437,12 @@ public string ToSql()
427437
colSB.AppendFormat("{0}{1}", colSB.Length > 0 ? "," : "", String.IsNullOrEmpty(fi.BelongToModelName) ? (fi.FieldName) : ((OrmLiteConfig.DialectProvider.GetQuotedTableName(fi.BelongToModelName) + "." + OrmLiteConfig.DialectProvider.GetQuotedColumnName(fi.FieldName))));
428438
}
429439
if (colSB.Length == 0)
430-
colSB.AppendFormat("\"{0}\".*", OrmLiteConfig.DialectProvider.GetQuotedTableName(baseTableName));
440+
colSB.AppendFormat("\"{0}{1}\".*", baseSchema, OrmLiteConfig.DialectProvider.GetQuotedTableName(baseTableName));
431441
}
432442

433443
sb.Append(colSB.ToString() + " \n");
434444

435-
sb.AppendFormat("FROM {0} \n", OrmLiteConfig.DialectProvider.GetQuotedTableName(baseTableName));
445+
sb.AppendFormat("FROM {0}{1} \n", baseSchema, OrmLiteConfig.DialectProvider.GetQuotedTableName(baseTableName));
436446
int i = 0;
437447
foreach (var join in joinList)
438448
{
@@ -452,17 +462,17 @@ public string ToSql()
452462

453463
if (join.JoinType == JoinType.CROSS)
454464
{
455-
sb.AppendFormat(" {0} ON {1} = {2} \n", OrmLiteConfig.DialectProvider.GetQuotedTableName(join.RefTypeTableName));
465+
sb.AppendFormat(" {0}{1} ON {2} = {3} \n", join.RefTypeSchema, OrmLiteConfig.DialectProvider.GetQuotedTableName(join.RefTypeTableName));
456466
}
457467
else
458468
{
459469
if (join.JoinType != JoinType.SELF)
460470
{
461-
sb.AppendFormat(" {0} ON {1} = {2} \n", OrmLiteConfig.DialectProvider.GetQuotedTableName(join.RefTypeTableName), join.Class1ColumnName, join.Class2ColumnName);
471+
sb.AppendFormat(" {0}{1} ON {2} = {3} \n", join.RefTypeSchema, OrmLiteConfig.DialectProvider.GetQuotedTableName(join.RefTypeTableName), join.Class1ColumnName, join.Class2ColumnName);
462472
}
463473
else
464474
{
465-
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);
475+
sb.AppendFormat(" {0}{1} AS {2} ON {2}.{3} = \"{1}\".{4} \n", join.RefTypeSchema, OrmLiteConfig.DialectProvider.GetQuotedTableName(join.RefTypeTableName), OrmLiteConfig.DialectProvider.GetQuotedTableName(join.RefTypeTableName) + "_" + i.ToString(), join.Class1ColumnName, join.Class2ColumnName);
466476
}
467477
}
468478
}
@@ -514,8 +524,11 @@ class Join
514524
public Type Class2Type { get; set; }
515525
public Type RefType { get; set; }
516526
public JoinType JoinType { get; set; }
527+
public string Class1Schema { get; set; }
528+
public string Class2Schema { get; set; }
517529
public string Class1TableName { get; set; }
518530
public string Class2TableName { get; set; }
531+
public string RefTypeSchema { get; set; }
519532
public string RefTypeTableName { get; set; }
520533
public string Class1ColumnName { get; set; }
521534
public string Class2ColumnName { get; set; }

0 commit comments

Comments
 (0)