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

Commit 880c84b

Browse files
committed
Add support for auto mapping {Table}Id convention
1 parent 0099b23 commit 880c84b

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,29 @@ public string SelectInto<TModel>()
151151
if (found)
152152
break;
153153
}
154+
155+
if (!found)
156+
{
157+
// Add support for auto mapping `{Table}Id` convention
158+
foreach (var tableDef in tableDefs)
159+
{
160+
var primaryKey = tableDef.PrimaryKey;
161+
if (primaryKey == null) continue;
162+
163+
var tableId = tableDef.Name + primaryKey.Name;
164+
if (fieldDef.Name == tableId)
165+
{
166+
if (sbSelect.Length > 0)
167+
sbSelect.Append(", ");
168+
169+
sbSelect.AppendFormat("{0}.{1} as {2}",
170+
SqlTable(tableDef.ModelName),
171+
primaryKey.GetQuotedName(DialectProvider),
172+
fieldDef.Name);
173+
break;
174+
}
175+
}
176+
}
154177
}
155178

156179
SelectExpression = "SELECT " + sbSelect;

tests/ServiceStack.OrmLite.Tests/LoadReferencesJoinTests.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ public class FullCustomerInfo
6464
{
6565
public int Id { get; set; }
6666
public string Name { get; set; }
67+
public int CustomerAddressId { get; set; }
6768
public string AddressLine1 { get; set; }
6869
public string City { get; set; }
70+
public int OrderId { get; set; }
6971
public string LineItem { get; set; }
7072
public decimal Cost { get; set; }
7173
public string CountryCode { get; set; }
@@ -189,6 +191,8 @@ public void Can_do_joins_with_complex_wheres_using_SqlExpression()
189191

190192
var costs = results.ConvertAll(x => x.Cost);
191193
Assert.That(costs, Is.EquivalentTo(new[] { 1.99m, 1.49m, 9.99m }));
194+
var orderIds = results.ConvertAll(x => x.OrderId);
195+
Assert.That(orderIds, Is.EquivalentTo(new[] { 1, 3, 5 }));
192196

193197
//Same as above using using db.From<Customer>()
194198
results = db.Select<FullCustomerInfo>(db.From<Customer>()
@@ -217,8 +221,6 @@ public void Can_do_joins_with_complex_wheres_using_SqlExpression()
217221
.Where(c => c.Name == "Customer 2") //implicit condition with Customer
218222
.And<CustomerAddress, Order>((a, o) => a.Country == o.LineItem));
219223

220-
db.GetLastSql().Print();
221-
222224
costs = countryResults.ConvertAll(x => x.Cost);
223225
Assert.That(costs, Is.EquivalentTo(new[] { 20m }));
224226
Assert.That(countryResults.ConvertAll(x => x.CountryCode), Is.EquivalentTo(new[] { "US" }));
@@ -362,5 +364,39 @@ public void Can_Join_on_matching_Alias_convention()
362364

363365
Assert.That(dbAddresses.Count, Is.EqualTo(3));
364366
}
367+
368+
[Test]
369+
public void Does_populate_PrimaryKey_ids_based_on_property_convention()
370+
{
371+
// Reset auto ids
372+
db.DropAndCreateTable<Order>();
373+
db.DropAndCreateTable<CustomerAddress>();
374+
db.DropAndCreateTable<Customer>();
375+
376+
AddCustomerWithOrders();
377+
378+
var results = db.Select<FullCustomerInfo, Customer>(q => q
379+
.Join<Customer, CustomerAddress>()
380+
.Join<Customer, Order>());
381+
382+
var addressIds = results.ConvertAll(x => x.CustomerAddressId);
383+
Assert.That(addressIds, Is.EquivalentTo(new[] { 1, 1 }));
384+
385+
var orderIds = results.ConvertAll(x => x.OrderId);
386+
Assert.That(orderIds, Is.EquivalentTo(new[] { 1, 2 }));
387+
388+
var expr = db.From<Customer>()
389+
.Join<Customer, CustomerAddress>()
390+
.Join<Customer, Order>()
391+
.Where<Order>(o => o.Cost > 2);
392+
393+
results = db.Select<FullCustomerInfo>(expr);
394+
395+
addressIds = results.ConvertAll(x => x.CustomerAddressId);
396+
Assert.That(addressIds, Is.EquivalentTo(new[] { 1 }));
397+
398+
orderIds = results.ConvertAll(x => x.OrderId);
399+
Assert.That(orderIds, Is.EquivalentTo(new[] { 2 }));
400+
}
365401
}
366402
}

0 commit comments

Comments
 (0)