Skip to content

Commit

Permalink
dapper IDbConnectionExtensions added
Browse files Browse the repository at this point in the history
  • Loading branch information
vahid committed Sep 28, 2023
1 parent eb3e211 commit 9062806
Showing 1 changed file with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
using System.Data;
using Dapper;
using Jinget.Core.Contracts;
using Jinget.Core.ExpressionToSql.Internal;
using System.Collections.Generic;
using System;
using System.Data;
using System.Threading.Tasks;
using System.Linq;
using Jinget.Core.ExtensionMethods.ExpressionToSql;
using Jinget.Core.ExtensionMethods.Collections;

namespace Jinget.Core.ExtensionMethods.Database.SqlClient
{
Expand All @@ -15,5 +24,74 @@ public static void SafeOpen(this IDbConnection connection)
connection.Close();
connection.Open();
}

private static (string queryText, Dictionary<string, object> queryParameters) PrepareQuery(Query sql,
object param)
{
var query = sql.ToSql();
var queryText = query.query.ToString();

if (queryText.IndexOf("SELECT FROM ", StringComparison.InvariantCultureIgnoreCase) > 0)
queryText = queryText.Replace("SELECT FROM ", "SELECT * FROM ");

var orderBy = (param as IOrderBy)?.OrderBy;
var paging = (param as IPaginated)?.PagingConfig;

string countQueryText = "";
if (paging != null)
{
countQueryText = queryText[queryText.IndexOf(" FROM ", StringComparison.CurrentCultureIgnoreCase)..];
countQueryText = "Select Count(*) " + countQueryText;
}

queryText = string.Join(" ", queryText, paging.GetPaging(orderBy));

var queryParams = query.parameters;
if (!query.parameters.Any())
queryParams.Merge(param.ToDictionary());

return (countQueryText == "" ? queryText : string.Join(";", queryText, countQueryText), queryParams);
}

private static async Task<(IEnumerable<R> Data, int TotalCount)> ExecQueryAsync<R>(IDbConnection cnn, Query sql, object param, IDbTransaction transaction, int? commandTimeout, CommandType? commandType)
{
var (queryText, queryParameters) = PrepareQuery(sql, param);

var result =
await cnn.QueryMultipleAsync(queryText, queryParameters, transaction, commandTimeout,
commandType);

return (Data: result.Read<R>(),
TotalCount: param is IPaginated paginated && paginated.PagingConfig != null
? result.Read<int>().FirstOrDefault()
: 0);
}

private static (IEnumerable<R> Data, int TotalCount) ExecQuery<R>(IDbConnection cnn, Query sql, object param,
#pragma warning disable IDE0060 // Remove unused parameter
IDbTransaction transaction, bool buffered, int? commandTimeout, CommandType? commandType)
#pragma warning restore IDE0060 // Remove unused parameter
{
var (queryText, queryParameters) = PrepareQuery(sql, param);

var result = cnn.QueryMultiple(queryText, queryParameters, transaction, commandTimeout,
commandType);
return (Data: result.Read<R>(),
TotalCount: param is IPaginated paginated && paginated.PagingConfig != null
? result.Read<int>().FirstOrDefault()
: 0);
}

public static async Task<(IEnumerable<R> Data, int TotalCount)> QueryAsync<T, R>(this IDbConnection cnn, Select<T, R> sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)

Check warning on line 85 in 01-Core/Jinget.Core/ExtensionMethods/Database/SqlClient/IDbConnectionExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 85 in 01-Core/Jinget.Core/ExtensionMethods/Database/SqlClient/IDbConnectionExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 85 in 01-Core/Jinget.Core/ExtensionMethods/Database/SqlClient/IDbConnectionExtensions.cs

View workflow job for this annotation

GitHub Actions / release

Cannot convert null literal to non-nullable reference type.

Check warning on line 85 in 01-Core/Jinget.Core/ExtensionMethods/Database/SqlClient/IDbConnectionExtensions.cs

View workflow job for this annotation

GitHub Actions / release

Cannot convert null literal to non-nullable reference type.
=> await ExecQueryAsync<R>(cnn, sql, param, transaction, commandTimeout, commandType);

public static async Task<(IEnumerable<R> Data, int TotalCount)> QueryAsync<T, R>(this IDbConnection cnn, Where<T, R> sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)

Check warning on line 88 in 01-Core/Jinget.Core/ExtensionMethods/Database/SqlClient/IDbConnectionExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 88 in 01-Core/Jinget.Core/ExtensionMethods/Database/SqlClient/IDbConnectionExtensions.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 88 in 01-Core/Jinget.Core/ExtensionMethods/Database/SqlClient/IDbConnectionExtensions.cs

View workflow job for this annotation

GitHub Actions / release

Cannot convert null literal to non-nullable reference type.

Check warning on line 88 in 01-Core/Jinget.Core/ExtensionMethods/Database/SqlClient/IDbConnectionExtensions.cs

View workflow job for this annotation

GitHub Actions / release

Cannot convert null literal to non-nullable reference type.
=> await ExecQueryAsync<R>(cnn, sql, param, transaction, commandTimeout, commandType);

public static (IEnumerable<R> Data, int TotalCount) Query<T, R>(this IDbConnection cnn, Select<T, R> sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
=> ExecQuery<R>(cnn, sql, param, transaction, buffered, commandTimeout, commandType);

public static (IEnumerable<R> Data, int TotalCount) Query<T, R>(this IDbConnection cnn, Where<T, R> sql, object param = null, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = null, CommandType? commandType = null)
=> ExecQuery<R>(cnn, sql, param, transaction, buffered, commandTimeout, commandType);
}
}

0 comments on commit 9062806

Please sign in to comment.