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

Commit aaf6c25

Browse files
committed
Don't try to select unmatched columns, allows re-use and partial population of same model
1 parent ba210f2 commit aaf6c25

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

src/ServiceStack.OrmLite/Expressions/ReadConnectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static SqlExpression<T> From<T>(this IDbConnection dbConn)
4646
public static SqlExpression<T> From<T, JoinWith>(this IDbConnection dbConn, Expression<Func<T, JoinWith, bool>> joinExpr=null)
4747
{
4848
var sql = OrmLiteConfig.ExecFilter.SqlExpression<T>(dbConn);
49-
sql.Join(joinExpr);
49+
sql.Join<T,JoinWith>(joinExpr);
5050
return sql;
5151
}
5252

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public abstract partial class SqlExpression<T> : ISqlExpression
99
{
1010
List<ModelDefinition> tableDefs = new List<ModelDefinition>();
1111

12+
public SqlExpression<T> Join<Target>(Expression<Func<T, Target, bool>> joinExpr = null)
13+
{
14+
return InternalJoin("INNER JOIN", joinExpr);
15+
}
16+
1217
public SqlExpression<T> Join<Source, Target>(Expression<Func<Source, Target, bool>> joinExpr = null)
1318
{
1419
return InternalJoin("INNER JOIN", joinExpr);
@@ -124,16 +129,6 @@ public string SelectInto<TModel>()
124129
if (found)
125130
break;
126131
}
127-
128-
if (!found)
129-
{
130-
if (sbSelect.Length > 0)
131-
sbSelect.Append(", ");
132-
133-
sbSelect.AppendFormat("{0}.{1}",
134-
modelDef.ModelName.SqlTable(),
135-
fieldDef.GetQuotedName());
136-
}
137132
}
138133

139134
SelectExpression = "SELECT " + sbSelect;

tests/ServiceStack.OrmLite.Tests/LoadReferencesTests.cs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ public class Order
5050
public decimal Cost { get; set; }
5151
}
5252

53+
public class Country
54+
{
55+
[AutoIncrement]
56+
public int Id { get; set; }
57+
public string CountryName { get; set; }
58+
public string CountryCode { get; set; }
59+
}
60+
5361
/// <summary>
5462
/// Test POCOs using table aliases and an alias on the foreign key reference
5563
/// </summary>
@@ -143,6 +151,7 @@ public class LoadReferencesTests
143151
db.DropAndCreateTable<Order>();
144152
db.DropAndCreateTable<Customer>();
145153
db.DropAndCreateTable<CustomerAddress>();
154+
db.DropAndCreateTable<Country>();
146155
db.DropAndCreateTable<AliasedCustomer>();
147156
db.DropAndCreateTable<AliasedCustomerAddress>();
148157
db.DropAndCreateTable<OldAliasedCustomer>();
@@ -366,6 +375,7 @@ public class CustomerJoin
366375
public string City { get; set; }
367376
public string LineItem { get; set; }
368377
public decimal Cost { get; set; }
378+
public string CountryCode { get; set; }
369379
}
370380

371381
[Test]
@@ -461,7 +471,7 @@ public void Can_do_joins_with_complex_wheres_using_SqlExpression()
461471
};
462472

463473
db.Save(customer1, references: true);
464-
474+
465475
var customer2 = new Customer
466476
{
467477
Name = "Customer 2",
@@ -480,6 +490,10 @@ public void Can_do_joins_with_complex_wheres_using_SqlExpression()
480490

481491
db.Save(customer2, references: true);
482492

493+
db.Insert(
494+
new Country { CountryName = "Australia", CountryCode = "AU" },
495+
new Country { CountryName = "USA", CountryCode = "US" });
496+
483497
var results = db.Select<CustomerJoin, Customer>(q => q
484498
.Join<Customer, CustomerAddress>()
485499
.Join<Customer, Order>()
@@ -496,12 +510,23 @@ public void Can_do_joins_with_complex_wheres_using_SqlExpression()
496510
.Where(c => c.Name == "Customer 2")
497511
.And<CustomerAddress, Order>((a, o) => a.Country == o.LineItem));
498512

513+
costs = results.ConvertAll(x => x.Cost);
514+
Assert.That(costs, Is.EquivalentTo(new[] { 20m }));
515+
516+
var countryResults = db.Select<CustomerJoin>(db.From<Customer>()
517+
.Join<Order>((c, o) => c.Id == o.CustomerId) //explicit join condition
518+
.Join<CustomerAddress>() //implicit join with Customer
519+
.Join<CustomerAddress,Country>((ca, c) => ca.Country == c.CountryName)
520+
.Where(c => c.Name == "Customer 2") //implicit condition with Customer
521+
.And<CustomerAddress, Order>((a, o) => a.Country == o.LineItem));
522+
499523
db.GetLastSql().Print();
500524

501-
costs = results.ConvertAll(x => x.Cost);
525+
costs = countryResults.ConvertAll(x => x.Cost);
502526
Assert.That(costs, Is.EquivalentTo(new[] { 20m }));
527+
Assert.That(countryResults.ConvertAll(x => x.CountryCode), Is.EquivalentTo(new[] { "US" }));
503528
}
504-
529+
505530
}
506531

507532
}

0 commit comments

Comments
 (0)