Skip to content

Support for OuterSqlTemplate in DapperGriddlyResult #43

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

Merged
merged 2 commits into from
Apr 7, 2015
Merged
Changes from all 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
16 changes: 11 additions & 5 deletions Griddly.Mvc/DapperGriddlyResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class DapperGriddlyResult<T> : GriddlyResult<T>
Func<IDbConnection> _getConnection;
Func<IDbTransaction> _getTransaction;
string _sql;
string _outerSqlTemplate;
object _param;
Func<IDbConnection, IDbTransaction, string, object, IEnumerable<T>> _map;
Action<IDbConnection, IDbTransaction, IList<T>> _massage;
Expand All @@ -21,12 +22,13 @@ public class DapperGriddlyResult<T> : GriddlyResult<T>
bool _fixedSort;
static readonly bool _hasOverallCount = typeof(IHasOverallCount).IsAssignableFrom(typeof(T));

public DapperGriddlyResult(Func<IDbConnection> getConnection, string sql, object param, Func<IDbConnection, IDbTransaction, string, object, IEnumerable<T>> map = null, Action<IDbConnection, IDbTransaction, IList<T>> massage = null, bool fixedSort = false, Func<IDbTransaction> getTransaction = null)
public DapperGriddlyResult(Func<IDbConnection> getConnection, string sql, object param, Func<IDbConnection, IDbTransaction, string, object, IEnumerable<T>> map = null, Action<IDbConnection, IDbTransaction, IList<T>> massage = null, bool fixedSort = false, Func<IDbTransaction> getTransaction = null, string outerSqlTemplate = "{0}")
: base(null)
{
_getConnection = getConnection;
_sql = sql;
_param = param;
_outerSqlTemplate = outerSqlTemplate;

if (map == null)
_map = DefaultMap;
Expand Down Expand Up @@ -59,7 +61,8 @@ public override void PopulateSummaryValues(GriddlySettings<T> settings)
aggregateExpression.AppendFormat("{0}({1}) AS _a{2}", col.SummaryFunction, col.ExpressionString, i);
}

string sql = string.Format("{0} FROM ({1}) [_proj]", aggregateExpression.ToString(), _sql);
string sql = string.Format(_outerSqlTemplate,
string.Format("{0} FROM ({1}) [_proj]", aggregateExpression.ToString(), _sql));

try
{
Expand All @@ -82,7 +85,8 @@ public override long GetCount()
{
if (_overallCount == null)
{
string sql = string.Format("SELECT CAST(COUNT(*) as bigint) FROM ({0}) [_proj]", _sql);
string sql = string.Format(_outerSqlTemplate,
string.Format("SELECT CAST(COUNT(*) as bigint) FROM ({0}) [_proj]", _sql));

try
{
Expand Down Expand Up @@ -118,14 +122,16 @@ SELECT COUNT(0) AS OverallCount FROM _data
)
SELECT * FROM _data CROSS APPLY _count " + (_fixedSort ? "" : "ORDER BY {1}") + " OFFSET {2} ROWS FETCH NEXT {3} ROWS ONLY";

string sql = string.Format(format, _sql, BuildSortClause(sortFields), pageNumber * pageSize, pageSize);
string sql = string.Format(_outerSqlTemplate,
string.Format(format, _sql, BuildSortClause(sortFields), pageNumber * pageSize, pageSize));

return ExecuteQuery(sql, _param);
}

public override IEnumerable<T> GetAll(SortField[] sortFields)
{
string sql = _fixedSort ? _sql : string.Format("{0} ORDER BY {1}", _sql, BuildSortClause(sortFields));
string sql = string.Format(_outerSqlTemplate,
_fixedSort ? _sql : string.Format("{0} ORDER BY {1}", _sql, BuildSortClause(sortFields)));

return ExecuteQuery(sql, _param);
}
Expand Down