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
Changes from 1 commit
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
21 changes: 13 additions & 8 deletions src/OhioBox.Moranbernate/Querying/QueryExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ public static T GetById<T>(this IDbConnection connection, object id)
return Run<T>(connection, sql, parameters, generator.GetColumns()).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<string> trackSql = null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets use an object instead of string + list of obj - it will enable us to extend it later without breaking the contract

where T : class, new()
{
var countByQuery = new CountByQuery<T>();

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

trackSql?.Invoke(sql);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing the parameters


using (var command = connection.CreateCommand())
{
command.CommandText = sql;
Expand All @@ -35,30 +37,33 @@ 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<string> 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);

trackSql?.Invoke(sql);

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

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<string> 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?.Invoke(sql);

using (var command = connection.CreateCommand())
{
command.CommandText = sql;
Expand Down Expand Up @@ -127,7 +132,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 Down