From 89df7f9df9106f48eccd0296888c044b28676fdf Mon Sep 17 00:00:00 2001 From: igor Date: Wed, 24 Jan 2018 17:24:34 +0200 Subject: [PATCH 1/6] Add trackSql action --- src/OhioBox.Moranbernate/Querying/QueryExt.cs | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/OhioBox.Moranbernate/Querying/QueryExt.cs b/src/OhioBox.Moranbernate/Querying/QueryExt.cs index 693e836..6ff9daa 100644 --- a/src/OhioBox.Moranbernate/Querying/QueryExt.cs +++ b/src/OhioBox.Moranbernate/Querying/QueryExt.cs @@ -19,7 +19,7 @@ public static T GetById(this IDbConnection connection, object id) return Run(connection, sql, parameters, generator.GetColumns()).FirstOrDefault(); } - public static long Count(this IDbConnection connection, Action> restrictions = null) + public static long Count(this IDbConnection connection, Action> restrictions = null, Action trackSql = null) where T : class, new() { var countByQuery = new CountByQuery(); @@ -27,6 +27,8 @@ public static long Count(this IDbConnection connection, Action(); var sql = countByQuery.GetSql(restrictions, parameters); + trackSql?.Invoke(sql); + using (var command = connection.CreateCommand()) { command.CommandText = sql; @@ -35,30 +37,33 @@ public static long Count(this IDbConnection connection, Action Query(this IDbConnection connection, Action> query = null) + public static IEnumerable Query(this IDbConnection connection, Action> query = null, Action trackSql = null) where T : class, new() { var builder = new QueryBuilder(); - if (query != null) - query(builder); + query?.Invoke(builder); var parameters = new List(); var sql = builder.Build(parameters); + + trackSql?.Invoke(sql); + return Run(connection, sql, parameters, builder.Properties); } - public static IEnumerable> QueryAggregated(this IDbConnection connection, Action> query = null) + public static IEnumerable> QueryAggregated(this IDbConnection connection, Action> query = null, Action trackSql = null) where T : class, new() { var builder = new QueryBuilder(); - if (query != null) - query(builder); + query?.Invoke(builder); builder.RowCount(); var parameters = new List(); var sql = builder.Build(parameters); + trackSql?.Invoke(sql); + using (var command = connection.CreateCommand()) { command.CommandText = sql; @@ -127,7 +132,7 @@ private static IEnumerable Run(IDbConnection connection, string sql, List< } } - private static void HandleSetValueException(string sql, List parameters, IDataReader reader, IDbConnection connection, Exception ex) + private static void HandleSetValueException(string sql, IList parameters, IDataReader reader, IDbConnection connection, Exception ex) where T : class, new() { Exception e; From e88dd4dbb5d369290f4aca7a400a1d9760d7637b Mon Sep 17 00:00:00 2001 From: igor Date: Thu, 25 Jan 2018 13:01:30 +0200 Subject: [PATCH 2/6] Add parameters to the trackSql func as well --- src/OhioBox.Moranbernate/Querying/QueryExt.cs | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/OhioBox.Moranbernate/Querying/QueryExt.cs b/src/OhioBox.Moranbernate/Querying/QueryExt.cs index 6ff9daa..766cdee 100644 --- a/src/OhioBox.Moranbernate/Querying/QueryExt.cs +++ b/src/OhioBox.Moranbernate/Querying/QueryExt.cs @@ -10,16 +10,17 @@ namespace OhioBox.Moranbernate.Querying { public static class QueryExt { - public static T GetById(this IDbConnection connection, object id) + public static T GetById(this IDbConnection connection, object id, Action> trackSql = null) where T : class, new() { var generator = CrudOperator.GetById; var parameters = new List { id }; var sql = generator.GetSql(); - return Run(connection, sql, parameters, generator.GetColumns()).FirstOrDefault(); + + return Run(connection, sql, parameters, generator.GetColumns(), trackSql).FirstOrDefault(); } - public static long Count(this IDbConnection connection, Action> restrictions = null, Action trackSql = null) + public static long Count(this IDbConnection connection, Action> restrictions = null, Action> trackSql = null) where T : class, new() { var countByQuery = new CountByQuery(); @@ -27,7 +28,7 @@ public static long Count(this IDbConnection connection, Action(); var sql = countByQuery.GetSql(restrictions, parameters); - trackSql?.Invoke(sql); + trackSql?.Invoke(sql, parameters); using (var command = connection.CreateCommand()) { @@ -37,7 +38,7 @@ public static long Count(this IDbConnection connection, Action Query(this IDbConnection connection, Action> query = null, Action trackSql = null) + public static IEnumerable Query(this IDbConnection connection, Action> query = null, Action> trackSql = null) where T : class, new() { var builder = new QueryBuilder(); @@ -46,12 +47,10 @@ public static IEnumerable Query(this IDbConnection connection, Action(); var sql = builder.Build(parameters); - trackSql?.Invoke(sql); - - return Run(connection, sql, parameters, builder.Properties); + return Run(connection, sql, parameters, builder.Properties, trackSql); } - public static IEnumerable> QueryAggregated(this IDbConnection connection, Action> query = null, Action trackSql = null) + public static IEnumerable> QueryAggregated(this IDbConnection connection, Action> query = null, Action> trackSql = null) where T : class, new() { var builder = new QueryBuilder(); @@ -62,7 +61,7 @@ public static IEnumerable> QueryAggregated(this IDbConnection var parameters = new List(); var sql = builder.Build(parameters); - trackSql?.Invoke(sql); + trackSql?.Invoke(sql, parameters); using (var command = connection.CreateCommand()) { @@ -88,9 +87,11 @@ public static IEnumerable> QueryAggregated(this IDbConnection } } - private static IEnumerable Run(IDbConnection connection, string sql, List parameters, IEnumerable properties) + private static IEnumerable Run(IDbConnection connection, string sql, List parameters, IEnumerable properties, Action> trackSql = null) where T : class, new() { + trackSql?.Invoke(sql, parameters); + using (var command = connection.CreateCommand()) { command.CommandText = sql; From 708af3f6087437adcd878bc68b3fd9027e589554 Mon Sep 17 00:00:00 2001 From: igor Date: Thu, 25 Jan 2018 14:16:09 +0200 Subject: [PATCH 3/6] Add SqlDescriptor class and use it in trackSql func instead of the flat params --- src/OhioBox.Moranbernate/Querying/QueryExt.cs | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/OhioBox.Moranbernate/Querying/QueryExt.cs b/src/OhioBox.Moranbernate/Querying/QueryExt.cs index 766cdee..614c222 100644 --- a/src/OhioBox.Moranbernate/Querying/QueryExt.cs +++ b/src/OhioBox.Moranbernate/Querying/QueryExt.cs @@ -10,7 +10,7 @@ namespace OhioBox.Moranbernate.Querying { public static class QueryExt { - public static T GetById(this IDbConnection connection, object id, Action> trackSql = null) + public static T GetById(this IDbConnection connection, object id, Action trackSql = null) where T : class, new() { var generator = CrudOperator.GetById; @@ -20,7 +20,7 @@ public static T GetById(this IDbConnection connection, object id, Action(connection, sql, parameters, generator.GetColumns(), trackSql).FirstOrDefault(); } - public static long Count(this IDbConnection connection, Action> restrictions = null, Action> trackSql = null) + public static long Count(this IDbConnection connection, Action> restrictions = null, Action trackSql = null) where T : class, new() { var countByQuery = new CountByQuery(); @@ -28,7 +28,7 @@ public static long Count(this IDbConnection connection, Action(); var sql = countByQuery.GetSql(restrictions, parameters); - trackSql?.Invoke(sql, parameters); + trackSql?.Invoke(new SqlDescriptor(sql, parameters)); using (var command = connection.CreateCommand()) { @@ -38,7 +38,7 @@ public static long Count(this IDbConnection connection, Action Query(this IDbConnection connection, Action> query = null, Action> trackSql = null) + public static IEnumerable Query(this IDbConnection connection, Action> query = null, Action trackSql = null) where T : class, new() { var builder = new QueryBuilder(); @@ -50,7 +50,7 @@ public static IEnumerable Query(this IDbConnection connection, Action(connection, sql, parameters, builder.Properties, trackSql); } - public static IEnumerable> QueryAggregated(this IDbConnection connection, Action> query = null, Action> trackSql = null) + public static IEnumerable> QueryAggregated(this IDbConnection connection, Action> query = null, Action trackSql = null) where T : class, new() { var builder = new QueryBuilder(); @@ -61,7 +61,7 @@ public static IEnumerable> QueryAggregated(this IDbConnection var parameters = new List(); var sql = builder.Build(parameters); - trackSql?.Invoke(sql, parameters); + trackSql?.Invoke(new SqlDescriptor(sql, parameters)); using (var command = connection.CreateCommand()) { @@ -87,10 +87,10 @@ public static IEnumerable> QueryAggregated(this IDbConnection } } - private static IEnumerable Run(IDbConnection connection, string sql, List parameters, IEnumerable properties, Action> trackSql = null) + private static IEnumerable Run(IDbConnection connection, string sql, List parameters, IEnumerable properties, Action trackSql = null) where T : class, new() { - trackSql?.Invoke(sql, parameters); + trackSql?.Invoke(new SqlDescriptor(sql, parameters)); using (var command = connection.CreateCommand()) { @@ -155,6 +155,18 @@ public class QueryResult public int RowCount { get; set; } } + public class SqlDescriptor + { + public string Sql { get; } + public IList Parameters { get; } + + public SqlDescriptor(string sql, IList parameters) + { + Sql = sql; + Parameters = parameters; + } + } + public class MoranbernateQueryException : Exception { public MoranbernateQueryException(string sql, List parameters, Exception inner) : base(CreateMessage(sql, parameters), inner) From 4efc720b1af6cfc118e977ef155d6598ee54a2c9 Mon Sep 17 00:00:00 2001 From: igor Date: Thu, 25 Jan 2018 14:29:30 +0200 Subject: [PATCH 4/6] Extract TrackSql and suppress exception if it happens --- src/OhioBox.Moranbernate/Querying/QueryExt.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/OhioBox.Moranbernate/Querying/QueryExt.cs b/src/OhioBox.Moranbernate/Querying/QueryExt.cs index 614c222..a523618 100644 --- a/src/OhioBox.Moranbernate/Querying/QueryExt.cs +++ b/src/OhioBox.Moranbernate/Querying/QueryExt.cs @@ -28,7 +28,7 @@ public static long Count(this IDbConnection connection, Action(); var sql = countByQuery.GetSql(restrictions, parameters); - trackSql?.Invoke(new SqlDescriptor(sql, parameters)); + TrackSql(sql, parameters, trackSql); using (var command = connection.CreateCommand()) { @@ -61,7 +61,7 @@ public static IEnumerable> QueryAggregated(this IDbConnection var parameters = new List(); var sql = builder.Build(parameters); - trackSql?.Invoke(new SqlDescriptor(sql, parameters)); + TrackSql(sql, parameters, trackSql); using (var command = connection.CreateCommand()) { @@ -90,7 +90,7 @@ public static IEnumerable> QueryAggregated(this IDbConnection private static IEnumerable Run(IDbConnection connection, string sql, List parameters, IEnumerable properties, Action trackSql = null) where T : class, new() { - trackSql?.Invoke(new SqlDescriptor(sql, parameters)); + TrackSql(sql, parameters, trackSql); using (var command = connection.CreateCommand()) { @@ -147,6 +147,15 @@ private static void HandleSetValueException(string sql, IList paramet } throw e; } + + private static void TrackSql(string sql, IList parameters, Action trackSql) + { + try + { + trackSql?.Invoke(new SqlDescriptor(sql, parameters)); + } + catch (Exception) { } + } } public class QueryResult From a3fb301d38092268682750cfe7c96ba19ffc4b58 Mon Sep 17 00:00:00 2001 From: igor Date: Thu, 25 Jan 2018 14:48:46 +0200 Subject: [PATCH 5/6] Add to Sanity --- .../QueriesTests/SimpleQueryTests.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/OhioBox.Moranbernate.Tests/QueriesTests/SimpleQueryTests.cs b/src/OhioBox.Moranbernate.Tests/QueriesTests/SimpleQueryTests.cs index 3408aa9..2748a5e 100644 --- a/src/OhioBox.Moranbernate.Tests/QueriesTests/SimpleQueryTests.cs +++ b/src/OhioBox.Moranbernate.Tests/QueriesTests/SimpleQueryTests.cs @@ -84,12 +84,17 @@ public void Sanity() AnnoyingInterface = new AnnoyingInterface() }); + SqlDescriptor sqlDescriptor = null; + var dto = conn.Query( - q => q.Where(w => w.Equal(x => x.Extra, new[] { 10, 12 })) + q => q.Where(w => w.Equal(x => x.Extra, new[] { 10, 12 })), + descriptor => sqlDescriptor = descriptor ).FirstOrDefault(); Assert.That(dto.City, Is.EqualTo(city)); - } + Assert.That(sqlDescriptor.Sql, Is.EqualTo("SELECT `Zip`, `City`, `Longitude`, `Latitude`, `Extra`, `AnnoyingInterface` FROM `location` WHERE (`Extra` = ?p0);")); + Assert.That(sqlDescriptor.Parameters, Is.EquivalentTo(new [] {"10,12"})); + } } [Test] From edffb075e1224449775cf4ed45361b6f2c43e3c3 Mon Sep 17 00:00:00 2001 From: igor Date: Thu, 25 Jan 2018 15:55:06 +0200 Subject: [PATCH 6/6] Bump minor --- src/OhioBox.Moranbernate/OhioBox.Moranbernate.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OhioBox.Moranbernate/OhioBox.Moranbernate.csproj b/src/OhioBox.Moranbernate/OhioBox.Moranbernate.csproj index fd68eb6..ac5f41d 100644 --- a/src/OhioBox.Moranbernate/OhioBox.Moranbernate.csproj +++ b/src/OhioBox.Moranbernate/OhioBox.Moranbernate.csproj @@ -5,7 +5,7 @@ OhioBox.Moranbernate 0 -dev - 3.0.$(BuildNumber) + 3.1.$(BuildNumber) $(AssemblyVersion) $(AssemblyVersion)$(PackageVersionSuffix) Sears Israel