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

Commit aa952a9

Browse files
committed
Add support for BelongsTo to SqlExpression
1 parent 6cd0bec commit aa952a9

File tree

3 files changed

+100
-6
lines changed

3 files changed

+100
-6
lines changed

src/ServiceStack.OrmLite/Expressions/SqlExpression.Join.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,29 @@ public string SelectInto<TModel>()
175175
{
176176
var found = false;
177177

178+
if (fieldDef.BelongToModelName != null)
179+
{
180+
var tableDef = orderedDefs.FirstOrDefault(x => x.Name == fieldDef.BelongToModelName);
181+
if (tableDef != null)
182+
{
183+
var matchingField = FindWeakMatch(tableDef, fieldDef);
184+
if (matchingField != null)
185+
{
186+
if (OnlyFields == null || OnlyFields.Contains(fieldDef.Name))
187+
{
188+
if (sbSelect.Length > 0)
189+
sbSelect.Append(", ");
190+
191+
sbSelect.AppendFormat("{0} as {1}",
192+
DialectProvider.GetQuotedColumnName(tableDef, matchingField),
193+
SqlColumn(fieldDef.Name));
194+
195+
continue;
196+
}
197+
}
198+
}
199+
}
200+
178201
foreach (var tableDef in orderedDefs)
179202
{
180203
foreach (var tableFieldDef in tableDef.FieldDefinitions)
@@ -184,7 +207,6 @@ public string SelectInto<TModel>()
184207
if (OnlyFields != null && !OnlyFields.Contains(fieldDef.Name))
185208
continue;
186209

187-
found = true;
188210
if (sbSelect.Length > 0)
189211
sbSelect.Append(", ");
190212

@@ -195,6 +217,7 @@ public string SelectInto<TModel>()
195217
if (tableFieldDef.Alias != null)
196218
sbSelect.Append(" AS ").Append(SqlColumn(fieldDef.Name));
197219

220+
found = true;
198221
break;
199222
}
200223
}
@@ -208,11 +231,7 @@ public string SelectInto<TModel>()
208231
// Add support for auto mapping `{Table}{Field}` convention
209232
foreach (var tableDef in orderedDefs)
210233
{
211-
var matchingField = tableDef.FieldDefinitions
212-
.FirstOrDefault(x =>
213-
string.Compare(tableDef.Name + x.Name, fieldDef.Name, StringComparison.OrdinalIgnoreCase) == 0
214-
|| string.Compare(tableDef.ModelName + x.FieldName, fieldDef.Name, StringComparison.OrdinalIgnoreCase) == 0);
215-
234+
var matchingField = FindWeakMatch(tableDef, fieldDef);
216235
if (matchingField != null)
217236
{
218237
if (OnlyFields != null && !OnlyFields.Contains(fieldDef.Name))
@@ -237,6 +256,14 @@ public string SelectInto<TModel>()
237256
return ToSelectStatement();
238257
}
239258

259+
private static FieldDefinition FindWeakMatch(ModelDefinition tableDef, FieldDefinition fieldDef)
260+
{
261+
return tableDef.FieldDefinitions
262+
.FirstOrDefault(x =>
263+
string.Compare(tableDef.Name + x.Name, fieldDef.Name, StringComparison.OrdinalIgnoreCase) == 0
264+
|| string.Compare(tableDef.ModelName + x.FieldName, fieldDef.Name, StringComparison.OrdinalIgnoreCase) == 0);
265+
}
266+
240267
public virtual SqlExpression<T> Where<Target>(Expression<Func<Target, bool>> predicate)
241268
{
242269
AppendToWhere("AND", predicate);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using NUnit.Framework;
2+
using ServiceStack.DataAnnotations;
3+
using ServiceStack.Text;
4+
5+
namespace ServiceStack.OrmLite.Tests.Issues
6+
{
7+
class A
8+
{
9+
public int Id { get; set; }
10+
public string SomeAProperty { get; set; }
11+
}
12+
class B
13+
{
14+
public int Id { get; set; }
15+
public string SomeBProperty { get; set; }
16+
public int AId { get; set; }
17+
}
18+
class C
19+
{
20+
public int Id { get; set; }
21+
public string SomeCProperty { get; set; }
22+
public int BId { get; set; }
23+
}
24+
class D
25+
{
26+
public int Id { get; set; }
27+
28+
[BelongTo(typeof(B))]
29+
public int BId { get; set; }
30+
31+
public int CId { get; set; }
32+
33+
public string SomeAProperty { get; set; }
34+
public string SomeBProperty { get; set; }
35+
public string SomeCProperty { get; set; }
36+
}
37+
38+
public class BelongsToIssue : OrmLiteTestBase
39+
{
40+
[Test]
41+
public void Does_use_BelongsTo_to_specify_Attribute()
42+
{
43+
using (var db = OpenDbConnection())
44+
{
45+
db.DropAndCreateTable<A>();
46+
db.DropAndCreateTable<B>();
47+
db.DropAndCreateTable<C>();
48+
49+
db.Insert(new A { Id = 1, SomeAProperty = "A1" });
50+
db.Insert(new A { Id = 2, SomeAProperty = "A2" });
51+
db.Insert(new A { Id = 3, SomeAProperty = "A3" });
52+
db.Insert(new B { Id = 10, SomeBProperty = "B1", AId = 1 });
53+
db.Insert(new B { Id = 11, SomeBProperty = "B2", AId = 2 });
54+
db.Insert(new C { Id = 20, SomeCProperty = "C", BId = 10 });
55+
56+
var q = db.From<A>()
57+
.Join<B>()
58+
.LeftJoin<B,C>();
59+
60+
var results = db.Select<D>(q);
61+
Assert.That(results.Count, Is.EqualTo(2));
62+
Assert.That(results.Map(x => x.BId), Is.EquivalentTo(new[] { 10, 11 }));
63+
}
64+
}
65+
}
66+
}

tests/ServiceStack.OrmLite.Tests/ServiceStack.OrmLite.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
<Compile Include="DateTimeOffsetTests.cs" />
130130
<Compile Include="DateTimeTests.cs" />
131131
<Compile Include="DefaultValueTests.cs" />
132+
<Compile Include="Issues\BelongsToIssue.cs" />
132133
<Compile Include="UseCase\ExpressionsAuthorTests.cs" />
133134
<Compile Include="Expression\ComplexJoinTests.cs" />
134135
<Compile Include="Expression\ExpressionUsingCustomSerializedEnumTests.cs" />

0 commit comments

Comments
 (0)