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

Commit

Permalink
Add support for auto mapping {Table}Id convention
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Jun 17, 2014
1 parent 0099b23 commit 880c84b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/ServiceStack.OrmLite/Expressions/SqlExpression.Join.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,29 @@ public string SelectInto<TModel>()
if (found)
break;
}

if (!found)
{
// Add support for auto mapping `{Table}Id` convention
foreach (var tableDef in tableDefs)
{
var primaryKey = tableDef.PrimaryKey;
if (primaryKey == null) continue;

var tableId = tableDef.Name + primaryKey.Name;
if (fieldDef.Name == tableId)
{
if (sbSelect.Length > 0)
sbSelect.Append(", ");

sbSelect.AppendFormat("{0}.{1} as {2}",
SqlTable(tableDef.ModelName),
primaryKey.GetQuotedName(DialectProvider),
fieldDef.Name);
break;
}
}
}
}

SelectExpression = "SELECT " + sbSelect;
Expand Down
40 changes: 38 additions & 2 deletions tests/ServiceStack.OrmLite.Tests/LoadReferencesJoinTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ public class FullCustomerInfo
{
public int Id { get; set; }
public string Name { get; set; }
public int CustomerAddressId { get; set; }
public string AddressLine1 { get; set; }
public string City { get; set; }
public int OrderId { get; set; }
public string LineItem { get; set; }
public decimal Cost { get; set; }
public string CountryCode { get; set; }
Expand Down Expand Up @@ -189,6 +191,8 @@ public void Can_do_joins_with_complex_wheres_using_SqlExpression()

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

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

db.GetLastSql().Print();

costs = countryResults.ConvertAll(x => x.Cost);
Assert.That(costs, Is.EquivalentTo(new[] { 20m }));
Assert.That(countryResults.ConvertAll(x => x.CountryCode), Is.EquivalentTo(new[] { "US" }));
Expand Down Expand Up @@ -362,5 +364,39 @@ public void Can_Join_on_matching_Alias_convention()

Assert.That(dbAddresses.Count, Is.EqualTo(3));
}

[Test]
public void Does_populate_PrimaryKey_ids_based_on_property_convention()
{
// Reset auto ids
db.DropAndCreateTable<Order>();
db.DropAndCreateTable<CustomerAddress>();
db.DropAndCreateTable<Customer>();

AddCustomerWithOrders();

var results = db.Select<FullCustomerInfo, Customer>(q => q
.Join<Customer, CustomerAddress>()
.Join<Customer, Order>());

var addressIds = results.ConvertAll(x => x.CustomerAddressId);
Assert.That(addressIds, Is.EquivalentTo(new[] { 1, 1 }));

var orderIds = results.ConvertAll(x => x.OrderId);
Assert.That(orderIds, Is.EquivalentTo(new[] { 1, 2 }));

var expr = db.From<Customer>()
.Join<Customer, CustomerAddress>()
.Join<Customer, Order>()
.Where<Order>(o => o.Cost > 2);

results = db.Select<FullCustomerInfo>(expr);

addressIds = results.ConvertAll(x => x.CustomerAddressId);
Assert.That(addressIds, Is.EquivalentTo(new[] { 1 }));

orderIds = results.ConvertAll(x => x.OrderId);
Assert.That(orderIds, Is.EquivalentTo(new[] { 2 }));
}
}
}

0 comments on commit 880c84b

Please sign in to comment.