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

Commit fa4499d

Browse files
committed
2 parents f72d4a6 + 59c1ec6 commit fa4499d

File tree

9 files changed

+227
-9
lines changed

9 files changed

+227
-9
lines changed

src/ServiceStack.OrmLite/OrmLiteConfig.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System;
1313
using System.Collections.Generic;
1414
using System.Data;
15+
using ServiceStack.Logging;
1516

1617
namespace ServiceStack.OrmLite
1718
{
@@ -147,6 +148,12 @@ public static IDbConnection ToDbConnection(this string dbConnectionStringOrFileP
147148
return dbConn;
148149
}
149150

151+
public static void ResetLogFactory(ILogFactory logFactory)
152+
{
153+
LogManager.LogFactory = logFactory;
154+
OrmLiteResultsFilterExtensions.Log = LogManager.LogFactory.GetLogger(typeof(OrmLiteResultsFilterExtensions));
155+
}
156+
150157
public static bool DisableColumnGuessFallback { get; set; }
151158
public static bool StripUpperInLike { get; set; }
152159
#if NETSTANDARD1_3

src/ServiceStack.OrmLite/OrmLiteExecFilter.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,20 @@ public virtual Task<T> Exec<T>(IDbConnection dbConn, Func<IDbCommand, Task<T>> f
105105
{
106106
var dbCmd = CreateCommand(dbConn);
107107

108-
return filter(dbCmd)
109-
.Then(t =>
110-
{
111-
DisposeCommand(dbCmd, dbConn);
112-
return t;
113-
});
108+
try
109+
{
110+
return filter(dbCmd)
111+
.Then(t =>
112+
{
113+
DisposeCommand(dbCmd, dbConn);
114+
return t;
115+
});
116+
}
117+
catch
118+
{
119+
DisposeCommand(dbCmd, dbConn);
120+
throw;
121+
}
114122
}
115123

116124
public virtual Task<IDbCommand> Exec(IDbConnection dbConn, Func<IDbCommand, Task<IDbCommand>> filter)

src/ServiceStack.OrmLite/OrmLiteResultsFilterExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ServiceStack.OrmLite
1212
{
1313
public static class OrmLiteResultsFilterExtensions
1414
{
15-
private static readonly ILog Log = LogManager.GetLogger(typeof(OrmLiteResultsFilterExtensions));
15+
internal static ILog Log = LogManager.GetLogger(typeof(OrmLiteResultsFilterExtensions));
1616

1717
public static int ExecNonQuery(this IDbCommand dbCmd, string sql, object anonType = null)
1818
{
@@ -246,6 +246,9 @@ public static long ExecLongScalar(this IDbCommand dbCmd, string sql = null)
246246
if (sql != null)
247247
dbCmd.CommandText = sql;
248248

249+
if (Log.IsDebugEnabled)
250+
Log.DebugCommand(dbCmd);
251+
249252
if (OrmLiteConfig.ResultsFilter != null)
250253
return OrmLiteConfig.ResultsFilter.GetLongScalar(dbCmd);
251254

tests/ServiceStack.OrmLite.Tests/CustomSqlExpressionTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Data;
44
using System.Linq;
55
using System.Linq.Expressions;
6+
using System.Reflection;
67
using NUnit.Framework;
78
using ServiceStack.DataAnnotations;
89
using ServiceStack.OrmLite.Sqlite;
@@ -112,7 +113,7 @@ private ModelDefinition GetCurrentTableDef(ModelDefinition tableDef, string memb
112113
var nonInheritedProperties = GetCurrentPropertiesWithoutBase(tableDef);
113114
while (curType != null && !nonInheritedProperties.Contains(memberName))
114115
{
115-
curType = curType.BaseType;
116+
curType = curType.BaseType();
116117
nonInheritedProperties = GetCurrentPropertiesWithoutBase(curType?.GetModelMetadata());
117118
}
118119

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using NUnit.Framework;
4+
using ServiceStack.DataAnnotations;
5+
using ServiceStack.Text;
6+
7+
namespace ServiceStack.OrmLite.Tests.Issues
8+
{
9+
public class Product
10+
{
11+
[AutoIncrement]
12+
public int Id { get; set; }
13+
public string ProductType { get; set; }
14+
public string Name { get; set; }
15+
public string Description { get; set; }
16+
public int DisplayOrder { get; set; }
17+
public bool LimitedToStores { get; set; }
18+
public string Sku { get; set; }
19+
public decimal Price { get; set; }
20+
public decimal OldPrice { get; set; }
21+
public decimal SpecialPrice { get; set; }
22+
public decimal DiscountPercentage { get; set; }
23+
public DateTime? DateChanged { get; set; }
24+
public DateTime? DateCreated { get; set; }
25+
26+
[Reference]
27+
public List<StockItem> StockItems { get; set; } = new List<StockItem>();
28+
}
29+
30+
public class StockItem
31+
{
32+
[AutoIncrement]
33+
public int Id { get; set; }
34+
[References(typeof(Product))]
35+
public int ProductId { get; set; }
36+
public string Size { get; set; }
37+
public int TotalStockQuantity { get; set; }
38+
public string Gtin { get; set; }
39+
public int DisplayOrder { get; set; }
40+
41+
[Reference]
42+
public Product Product { get; set; }
43+
}
44+
45+
public class AutoQueryJoinTests : OrmLiteTestBase
46+
{
47+
[Test]
48+
public void Can_select_references_with_join()
49+
{
50+
using (var db = OpenDbConnection())
51+
{
52+
db.DropTable<StockItem>();
53+
db.DropTable<Product>();
54+
db.CreateTable<Product>();
55+
db.CreateTable<StockItem>();
56+
57+
db.Save(new Product
58+
{
59+
ProductType = "A",
60+
Name = "Name A",
61+
DisplayOrder = 1,
62+
Sku = "SKU A",
63+
Price = 1,
64+
DateCreated = DateTime.UtcNow,
65+
StockItems = new List<StockItem>
66+
{
67+
new StockItem { Size = "1", TotalStockQuantity = 1, DisplayOrder = 1 },
68+
new StockItem { Size = "2", TotalStockQuantity = 2, DisplayOrder = 2 },
69+
}
70+
}, references: true);
71+
72+
db.Save(new Product
73+
{
74+
ProductType = "B",
75+
Name = "Name B",
76+
DisplayOrder = 2,
77+
Sku = "SKU B",
78+
Price = 2,
79+
DateCreated = DateTime.UtcNow,
80+
StockItems = new List<StockItem>
81+
{
82+
new StockItem { Size = "3", TotalStockQuantity = 3, DisplayOrder = 3 },
83+
new StockItem { Size = "4", TotalStockQuantity = 4, DisplayOrder = 4 },
84+
}
85+
}, references: true);
86+
87+
db.Insert(new Product
88+
{
89+
ProductType = "C",
90+
Name = "Name C",
91+
DisplayOrder = 3,
92+
Sku = "SKU C",
93+
Price = 3,
94+
DateCreated = DateTime.UtcNow,
95+
});
96+
97+
var results = db.LoadSelect<Product>();
98+
Assert.That(results.Count, Is.EqualTo(3));
99+
100+
var q = db.From<Product>().Join<StockItem>();
101+
var products = db.Select(q.SelectDistinct());
102+
var stockItems = db.Select<StockItem>();
103+
104+
products.Merge(stockItems);
105+
106+
Assert.That(products.Count, Is.EqualTo(2));
107+
Assert.That(products[0].StockItems.Count, Is.EqualTo(2));
108+
Assert.That(products[1].StockItems.Count, Is.EqualTo(2));
109+
}
110+
}
111+
}
112+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using NUnit.Framework;
2+
using ServiceStack.Logging;
3+
using ServiceStack.OrmLite.Tests.Shared;
4+
5+
namespace ServiceStack.OrmLite.Tests.Issues
6+
{
7+
public class NoSqlLoggingIssue : OrmLiteTestBase
8+
{
9+
[Test]
10+
public void Does_log_SQL_Insert_for_Saves()
11+
{
12+
var sbLogFactory = new StringBuilderLogFactory();
13+
OrmLiteConfig.ResetLogFactory(sbLogFactory);
14+
15+
using (var db = OpenDbConnection())
16+
{
17+
db.DropAndCreateTable<Person>();
18+
19+
db.Save(new Person { Id = 1, FirstName = "first", LastName = "last", Age = 27 });
20+
}
21+
22+
var sql = sbLogFactory.GetLogs();
23+
24+
Assert.That(sql, Does.Contain("INSERT INTO"));
25+
OrmLiteConfig.ResetLogFactory(null);
26+
}
27+
28+
[Test]
29+
public void Does_log_SQL_Insert_for_Saves_with_Auto_Ids()
30+
{
31+
var sbLogFactory = new StringBuilderLogFactory();
32+
OrmLiteConfig.ResetLogFactory(sbLogFactory);
33+
34+
using (var db = OpenDbConnection())
35+
{
36+
db.DropAndCreateTable<PersonWithAutoId>();
37+
38+
db.Save(new PersonWithAutoId { Id = 1, FirstName = "first", LastName = "last", Age = 27 });
39+
}
40+
41+
var sql = sbLogFactory.GetLogs();
42+
43+
Assert.That(sql, Does.Contain("INSERT INTO"));
44+
OrmLiteConfig.ResetLogFactory(null);
45+
}
46+
}
47+
}

tests/ServiceStack.OrmLite.Tests/OrmLiteExecFilterTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Data;
33
using System.Linq;
4+
using System.Threading.Tasks;
45
using NUnit.Framework;
56
using ServiceStack.Common.Tests.Models;
67
using ServiceStack.OrmLite.Tests.Expression;
@@ -50,6 +51,25 @@ public override T Exec<T>(IDbConnection dbConn, System.Func<IDbCommand, T> filte
5051
throw;
5152
}
5253
}
54+
55+
public override Task<T> Exec<T>(IDbConnection dbConn, Func<IDbCommand, Task<T>> filter)
56+
{
57+
try
58+
{
59+
return base.Exec(dbConn, filter);
60+
}
61+
catch (Exception)
62+
{
63+
var sql = dbConn.GetLastSql();
64+
if (sql == "exec sp_name @firstName, @age")
65+
{
66+
var tcs = new TaskCompletionSource<T>();
67+
tcs.SetResult((T)(object)new Person { FirstName = "Mocked" });
68+
return tcs.Task;
69+
}
70+
throw;
71+
}
72+
}
5373
}
5474

5575
[TestFixture]
@@ -93,6 +113,23 @@ public void Can_mock_store_procedure()
93113
OrmLiteConfig.DialectProvider.ExecFilter = holdExecFilter;
94114
}
95115

116+
[Test]
117+
public async System.Threading.Tasks.Task Can_mock_store_procedure_Async()
118+
{
119+
var holdExecFilter = OrmLiteConfig.DialectProvider.ExecFilter;
120+
OrmLiteConfig.DialectProvider.ExecFilter = new MockStoredProcExecFilter();
121+
122+
using (var db = OpenDbConnection())
123+
{
124+
var person = await db.SqlScalarAsync<Person>("exec sp_name @firstName, @age",
125+
new { firstName = "aName", age = 1 });
126+
127+
Assert.That(person.FirstName, Is.EqualTo("Mocked"));
128+
}
129+
130+
OrmLiteConfig.DialectProvider.ExecFilter = holdExecFilter;
131+
}
132+
96133
[Test]
97134
public void Does_use_StringFilter()
98135
{

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,13 @@
142142
<Compile Include="Expression\SqlExpressionDeleteTests.cs" />
143143
<Compile Include="Expression\SqlExpressionFilterTests.cs" />
144144
<Compile Include="Expression\SqlExpressionJoinTests.cs" />
145+
<Compile Include="Issues\AutoQueryJoinTests.cs" />
145146
<Compile Include="Issues\JoinAliasWithSchemaIssue.cs" />
146147
<Compile Include="Issues\JsonUpdateOnlyIssue.cs" />
147148
<Compile Include="Issues\LoadReferencesNullReferenceIssue.cs" />
148149
<Compile Include="Issues\MergeParamsIssue.cs" />
149150
<Compile Include="Issues\MultiThreadedUpdateTransactionIssue.cs" />
151+
<Compile Include="Issues\NoSqlLoggingIssue.cs" />
150152
<Compile Include="Issues\ParamNameIssues.cs" />
151153
<Compile Include="MultipleConnectionIdTests.cs" />
152154
<Compile Include="Shared\DefaultValues.cs" />

tests/ServiceStack.OrmLite.Tests/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"System.Runtime.Serialization.Xml": "4.*",
3939
"System.Reflection": "4.*",
4040
"System.Reflection.Primitives": "4.*",
41-
"System.Runtime.Serialization.Primitives": "4.*"
41+
"System.Runtime.Serialization.Primitives": "4.*",
42+
"System.Reflection.TypeExtensions": "4.*"
4243
}
4344
}
4445
}

0 commit comments

Comments
 (0)