Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trackSql action to handle generated sql and params #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,17 @@ public void Sanity()
AnnoyingInterface = new AnnoyingInterface()
});

SqlDescriptor sqlDescriptor = null;

var dto = conn.Query<LocationDto>(
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]
Expand Down
51 changes: 39 additions & 12 deletions src/OhioBox.Moranbernate/Querying/QueryExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,26 @@ namespace OhioBox.Moranbernate.Querying
{
public static class QueryExt
{
public static T GetById<T>(this IDbConnection connection, object id)
public static T GetById<T>(this IDbConnection connection, object id, Action<SqlDescriptor> trackSql = null)
where T : class, new()
{
var generator = CrudOperator<T>.GetById;
var parameters = new List<object> { id };
var sql = generator.GetSql();
return Run<T>(connection, sql, parameters, generator.GetColumns()).FirstOrDefault();

return Run<T>(connection, sql, parameters, generator.GetColumns(), trackSql).FirstOrDefault();
}

public static long Count<T>(this IDbConnection connection, Action<IRestrictable<T>> restrictions = null)
public static long Count<T>(this IDbConnection connection, Action<IRestrictable<T>> restrictions = null, Action<SqlDescriptor> trackSql = null)
where T : class, new()
{
var countByQuery = new CountByQuery<T>();

var parameters = new List<object>();
var sql = countByQuery.GetSql(restrictions, parameters);

TrackSql(sql, parameters, trackSql);

using (var command = connection.CreateCommand())
{
command.CommandText = sql;
Expand All @@ -35,30 +38,31 @@ public static long Count<T>(this IDbConnection connection, Action<IRestrictable<
}
}

public static IEnumerable<T> Query<T>(this IDbConnection connection, Action<IQueryBuilder<T>> query = null)
public static IEnumerable<T> Query<T>(this IDbConnection connection, Action<IQueryBuilder<T>> query = null, Action<SqlDescriptor> trackSql = null)
where T : class, new()
{
var builder = new QueryBuilder<T>();
if (query != null)
query(builder);
query?.Invoke(builder);

var parameters = new List<object>();
var sql = builder.Build(parameters);
return Run<T>(connection, sql, parameters, builder.Properties);

return Run<T>(connection, sql, parameters, builder.Properties, trackSql);
}

public static IEnumerable<QueryResult<T>> QueryAggregated<T>(this IDbConnection connection, Action<IQueryBuilder<T>> query = null)
public static IEnumerable<QueryResult<T>> QueryAggregated<T>(this IDbConnection connection, Action<IQueryBuilder<T>> query = null, Action<SqlDescriptor> trackSql = null)
where T : class, new()
{
var builder = new QueryBuilder<T>();
if (query != null)
query(builder);
query?.Invoke(builder);

builder.RowCount();

var parameters = new List<object>();
var sql = builder.Build(parameters);

TrackSql(sql, parameters, trackSql);

using (var command = connection.CreateCommand())
{
command.CommandText = sql;
Expand All @@ -83,9 +87,11 @@ public static IEnumerable<QueryResult<T>> QueryAggregated<T>(this IDbConnection
}
}

private static IEnumerable<T> Run<T>(IDbConnection connection, string sql, List<object> parameters, IEnumerable<Property> properties)
private static IEnumerable<T> Run<T>(IDbConnection connection, string sql, List<object> parameters, IEnumerable<Property> properties, Action<SqlDescriptor> trackSql = null)
where T : class, new()
{
TrackSql(sql, parameters, trackSql);

using (var command = connection.CreateCommand())
{
command.CommandText = sql;
Expand Down Expand Up @@ -127,7 +133,7 @@ private static IEnumerable<T> Run<T>(IDbConnection connection, string sql, List<
}
}

private static void HandleSetValueException<T>(string sql, List<object> parameters, IDataReader reader, IDbConnection connection, Exception ex)
private static void HandleSetValueException<T>(string sql, IList<object> parameters, IDataReader reader, IDbConnection connection, Exception ex)
where T : class, new()
{
Exception e;
Expand All @@ -141,6 +147,15 @@ private static void HandleSetValueException<T>(string sql, List<object> paramete
}
throw e;
}

private static void TrackSql(string sql, IList<object> parameters, Action<SqlDescriptor> trackSql)
{
try
{
trackSql?.Invoke(new SqlDescriptor(sql, parameters));
}
catch (Exception) { }
}
}

public class QueryResult<T>
Expand All @@ -149,6 +164,18 @@ public class QueryResult<T>
public int RowCount { get; set; }
}

public class SqlDescriptor
{
public string Sql { get; }
public IList<object> Parameters { get; }

public SqlDescriptor(string sql, IList<object> parameters)
{
Sql = sql;
Parameters = parameters;
}
}

public class MoranbernateQueryException : Exception
{
public MoranbernateQueryException(string sql, List<object> parameters, Exception inner) : base(CreateMessage(sql, parameters), inner)
Expand Down