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

Commit 846d056

Browse files
committed
Check for field in all tables
1 parent 1af0ca6 commit 846d056

File tree

3 files changed

+75
-9
lines changed

3 files changed

+75
-9
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,10 +1309,16 @@ protected string RemoveQuoteFromAlias(string exp)
13091309
protected bool IsFieldName(object quotedExp)
13101310
{
13111311
var fieldExpr = quotedExp.ToString().StripTablePrefixes();
1312-
var fieldNames = modelDef.FieldDefinitions.Map(x =>
1313-
DialectProvider.GetQuotedColumnName(x.FieldName));
1312+
var unquotedExpr = fieldExpr.StripQuotes();
13141313

1315-
return fieldNames.Any(x => x == fieldExpr);
1314+
var isTableField = modelDef.FieldDefinitionsArray.Any(x => x.FieldName == unquotedExpr);
1315+
if (isTableField)
1316+
return true;
1317+
1318+
var isJoinedField = tableDefs.Any(t => t.FieldDefinitionsArray
1319+
.Any(x => x.FieldName == unquotedExpr));
1320+
1321+
return isJoinedField;
13161322
}
13171323

13181324
protected object GetTrueExpression()

src/ServiceStack.OrmLite/OrmLiteUtils.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public static string SqlTable(this string tableName, IOrmLiteDialectProvider dia
172172
return (dialect ?? OrmLiteConfig.DialectProvider).GetQuotedTableName(tableName);
173173
}
174174

175-
public static string SqlTableRaw(this string tableName, IOrmLiteDialectProvider dialect=null)
175+
public static string SqlTableRaw(this string tableName, IOrmLiteDialectProvider dialect = null)
176176
{
177177
return (dialect ?? OrmLiteConfig.DialectProvider).NamingStrategy.GetTableName(tableName);
178178
}
@@ -264,7 +264,7 @@ public static string SqlJoin(IEnumerable values, IOrmLiteDialectProvider dialect
264264
return sb.ToString();
265265
}
266266

267-
public static SqlInValues SqlInValues<T>(this T[] values, IOrmLiteDialectProvider dialect=null)
267+
public static SqlInValues SqlInValues<T>(this T[] values, IOrmLiteDialectProvider dialect = null)
268268
{
269269
return new SqlInValues(values, dialect);
270270
}
@@ -318,6 +318,13 @@ public static string StripTablePrefixes(this string selectExpression)
318318
return sb.ToString().Trim();
319319
}
320320

321+
public static char[] QuotedChars = new[] { '"', '`', '[', ']' };
322+
323+
public static string StripQuotes(this string quotedExpr)
324+
{
325+
return quotedExpr.Trim(QuotedChars);
326+
}
327+
321328
private const int NotFound = -1;
322329

323330
public static ModelDefinition GetModelDefinition(Type modelType)
@@ -352,9 +359,9 @@ public static ulong ConvertToULong(byte[] bytes)
352359

353360
public static List<Parent> Merge<Parent, Child>(this Parent parent, List<Child> children)
354361
{
355-
return new List<Parent>{ parent}.Merge(children);
362+
return new List<Parent> { parent }.Merge(children);
356363
}
357-
364+
358365
public static List<Parent> Merge<Parent, Child>(this List<Parent> parents, List<Child> children)
359366
{
360367
var modelDef = ModelDefinition<Parent>.Definition;
@@ -397,7 +404,7 @@ public static List<Parent> Merge<Parent, Child>(this List<Parent> parents, List<
397404
return parents;
398405
}
399406

400-
internal static void SetListChildResults<Parent>(List<Parent> parents, ModelDefinition modelDef,
407+
internal static void SetListChildResults<Parent>(List<Parent> parents, ModelDefinition modelDef,
401408
FieldDefinition fieldDef, Type refType, IList childResults, FieldDefinition refField)
402409
{
403410
var map = new Dictionary<object, List<object>>();
@@ -466,6 +473,6 @@ internal static void SetRefFieldChildResults<Parent>(List<Parent> parents, Model
466473
}
467474
}
468475
}
469-
476+
470477
}
471478
}

tests/ServiceStack.OrmLite.Tests/Expression/SqlExpressionTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,5 +546,58 @@ public void Can_perform_a_crossjoin_with_a_join_expression()
546546
Assert.That(result, Is.EquivalentTo(expected));
547547
}
548548
}
549+
550+
class JoinTest
551+
{
552+
public int Id { get; set; }
553+
}
554+
555+
class JoinTestChild
556+
{
557+
public int Id { get; set; }
558+
559+
public int ParentId { get; set; }
560+
561+
public bool IsActive { get; set; }
562+
}
563+
564+
[Test]
565+
public void Issue_Bool_JoinTable_Expression()
566+
{
567+
using (var db = OpenDbConnection())
568+
{
569+
db.DropAndCreateTable<JoinTest>();
570+
db.DropAndCreateTable<JoinTestChild>();
571+
572+
db.InsertAll(new[] {
573+
new JoinTest { Id = 1, },
574+
new JoinTest { Id = 2, }
575+
});
576+
577+
db.InsertAll(new[] {
578+
new JoinTestChild
579+
{
580+
Id = 1,
581+
ParentId = 1,
582+
IsActive = true
583+
},
584+
new JoinTestChild
585+
{
586+
Id = 2,
587+
ParentId = 2,
588+
IsActive = false
589+
}
590+
});
591+
592+
var q = db.From<JoinTestChild>();
593+
q.Where(x => !x.IsActive);
594+
Assert.That(db.Select(q).Count, Is.EqualTo(1));
595+
596+
var qSub = db.From<JoinTest>();
597+
qSub.Join<JoinTestChild>((x, y) => x.Id == y.ParentId);
598+
qSub.Where<JoinTestChild>(x => !x.IsActive); // This line is a bug!
599+
Assert.That(db.Select(qSub).Count, Is.EqualTo(1));
600+
}
601+
}
549602
}
550603
}

0 commit comments

Comments
 (0)