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

Commit f4889ba

Browse files
committed
Merge pull request #409 from BruceCowan-AI/FixSingleOnOracleTableWithCompositeKey
Fix single extension method on Oracle
2 parents 4a5a74c + 21c8535 commit f4889ba

File tree

4 files changed

+93
-6
lines changed

4 files changed

+93
-6
lines changed

src/ServiceStack.OrmLite.Oracle.Tests/ServiceStack.OrmLite.Oracle.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@
7575
<Reference Include="System.Xml" />
7676
</ItemGroup>
7777
<ItemGroup>
78+
<Compile Include="..\..\tests\ServiceStack.OrmLite.Tests\CompositeKeyTests.cs">
79+
<Link>CompositeKeyTests.cs</Link>
80+
</Compile>
7881
<Compile Include="..\..\tests\ServiceStack.OrmLite.Tests\CustomSqlTests.cs">
7982
<Link>CustomSqlTests.cs</Link>
8083
</Compile>

src/ServiceStack.OrmLite.Oracle/OracleOrmLiteDialectProvider.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,13 +1061,25 @@ public override string ToSelectStatement(ModelDefinition modelDef,
10611061
if (!offset.HasValue)
10621062
offset = 0;
10631063

1064-
if (string.IsNullOrEmpty(orderByExpression))
1064+
if (string.IsNullOrEmpty(orderByExpression) && rows.HasValue)
10651065
{
1066-
if (modelDef.PrimaryKey == null)
1067-
throw new ApplicationException("Malformed model, no PrimaryKey defined");
1068-
1069-
orderByExpression = string.Format("ORDER BY {0}",
1070-
OrmLiteConfig.DialectProvider.GetQuotedColumnName(modelDef.PrimaryKey.FieldName));
1066+
var primaryKey = modelDef.FieldDefinitions.FirstOrDefault(x => x.IsPrimaryKey);
1067+
if (primaryKey == null)
1068+
{
1069+
if (rows.Value == 1 && offset.Value == 0)
1070+
{
1071+
// Probably used Single<> extension method on a table with a composite key so let it through.
1072+
// Lack of an orderby expression will mean it returns a random matching row, but that is OK.
1073+
orderByExpression = "";
1074+
}
1075+
else
1076+
throw new ApplicationException("Malformed model, no PrimaryKey defined");
1077+
}
1078+
else
1079+
{
1080+
orderByExpression = string.Format("ORDER BY {0}",
1081+
OrmLiteConfig.DialectProvider.GetQuotedColumnName(primaryKey.FieldName));
1082+
}
10711083
}
10721084
sbInner.Append(" " + orderByExpression);
10731085

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Data;
3+
using NUnit.Framework;
4+
5+
namespace ServiceStack.OrmLite.Tests
6+
{
7+
[TestFixture]
8+
public class CompositeKeyTests : OrmLiteTestBase
9+
{
10+
const long SubId1Value = 1;
11+
const long SubId2Value = 1;
12+
13+
[Test]
14+
public void Can_select_single_from_empty_composite_key_table()
15+
{
16+
using (var db = OpenDbConnection())
17+
{
18+
db.DropAndCreateTable<CompositeKey>();
19+
20+
var result = db.Single<CompositeKey>(ck => ck.SubId1 == SubId1Value && ck.SubId2 == SubId2Value);
21+
Assert.That(result, Is.Null);
22+
}
23+
}
24+
25+
[Test]
26+
public void Can_select_single_from_composite_key_table_with_one_matching_row()
27+
{
28+
using (var db = OpenDbConnection())
29+
{
30+
db.DropAndCreateTable<CompositeKey>();
31+
InsertData(db, 1);
32+
33+
var result = db.Single<CompositeKey>(ck => ck.SubId1 == SubId1Value && ck.SubId2 == SubId2Value);
34+
Assert.That(result.SubId1, Is.EqualTo(SubId1Value));
35+
Assert.That(result.SubId2, Is.EqualTo(SubId2Value));
36+
}
37+
}
38+
39+
private void InsertData(IDbConnection db, int count)
40+
{
41+
for (var i = 0; i < count; i++)
42+
{
43+
var data = new CompositeKey {SubId1 = SubId1Value, SubId2 = SubId2Value, Data = Guid.NewGuid().ToString()};
44+
db.Insert(data);
45+
}
46+
}
47+
48+
[Test]
49+
public void Can_select_single_from_composite_key_table_with_several_matching_rows()
50+
{
51+
using (var db = OpenDbConnection())
52+
{
53+
db.DropAndCreateTable<CompositeKey>();
54+
InsertData(db, 4);
55+
56+
var result = db.Single<CompositeKey>(ck => ck.SubId1 == SubId1Value && ck.SubId2 == SubId2Value);
57+
Assert.That(result.SubId1, Is.EqualTo(SubId1Value));
58+
Assert.That(result.SubId2, Is.EqualTo(SubId2Value));
59+
}
60+
}
61+
62+
public class CompositeKey
63+
{
64+
[DataAnnotations.Ignore]
65+
public string Id { get { return SubId1 + "/" + SubId2; } }
66+
public long SubId1 { get; set; }
67+
public long SubId2 { get; set; }
68+
public string Data { get; set; }
69+
}
70+
}
71+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
<Compile Include="ApiSqlServerTests.cs" />
118118
<Compile Include="ApiSqliteTests.cs" />
119119
<Compile Include="CaptureSqlFilterTests.cs" />
120+
<Compile Include="CompositeKeyTests.cs" />
120121
<Compile Include="CustomSqlTests.cs" />
121122
<Compile Include="DateTimeOffsetTests.cs" />
122123
<Compile Include="Expression\ExpressionUsingCustomSerializedEnumTests.cs" />

0 commit comments

Comments
 (0)