Permalink
Browse files

Convert to async/await

  • Loading branch information...
mythz committed Jan 7, 2019
1 parent a56f2d5 commit ff053afeba7be5241d0861f4f2b7397ccd4ebf0d
@@ -213,10 +213,10 @@ internal static Task<T> ScalarAsync<T>(this IDataReader reader, IOrmLiteDialectP
OrmLiteReadCommandExtensions.ToScalar<T>(dialectProvider, reader), token);
}

public static Task<long> LongScalarAsync(this IDbCommand dbCmd, CancellationToken token)
public static async Task<long> LongScalarAsync(this IDbCommand dbCmd, CancellationToken token)
{
return dbCmd.GetDialectProvider().ExecuteScalarAsync(dbCmd, token)
.Then(OrmLiteReadCommandExtensions.ToLong);
var ret = await dbCmd.GetDialectProvider().ExecuteScalarAsync(dbCmd, token);
return OrmLiteReadCommandExtensions.ToLong(ret);
}

internal static Task<List<T>> ColumnAsync<T>(this IDbCommand dbCmd, string sql, object anonType, CancellationToken token)
@@ -226,19 +226,17 @@ internal static Task<List<T>> ColumnAsync<T>(this IDbCommand dbCmd, string sql,
return dbCmd.ColumnAsync<T>(dbCmd.GetDialectProvider().ToSelectStatement(typeof(T), sql), token);
}

internal static Task<List<T>> ColumnAsync<T>(this IDataReader reader, IOrmLiteDialectProvider dialectProvider, CancellationToken token)
internal static async Task<List<T>> ColumnAsync<T>(this IDataReader reader, IOrmLiteDialectProvider dialectProvider, CancellationToken token)
{
return dialectProvider.ReaderEach(reader, () =>
var ret = await dialectProvider.ReaderEach(reader, () =>
{
var value = dialectProvider.FromDbValue(reader, 0, typeof(T));
return value == DBNull.Value ? default(T) : value;
}, token)
.Then(x =>
{
var columnValues = new List<T>();
x.Each(o => columnValues.Add((T)o));
return columnValues;
});
}, token);

var columnValues = new List<T>();
ret.Each(o => columnValues.Add((T)o));
return columnValues;
}

internal static Task<HashSet<T>> ColumnDistinctAsync<T>(this IDbCommand dbCmd, string sql, object anonType, CancellationToken token)
@@ -248,19 +246,17 @@ internal static Task<HashSet<T>> ColumnDistinctAsync<T>(this IDbCommand dbCmd, s
return dbCmd.ColumnDistinctAsync<T>(sql, token);
}

internal static Task<HashSet<T>> ColumnDistinctAsync<T>(this IDataReader reader, IOrmLiteDialectProvider dialectProvider, CancellationToken token)
internal static async Task<HashSet<T>> ColumnDistinctAsync<T>(this IDataReader reader, IOrmLiteDialectProvider dialectProvider, CancellationToken token)
{
return dialectProvider.ReaderEach(reader, () =>
var ret = await dialectProvider.ReaderEach(reader, () =>
{
var value = dialectProvider.FromDbValue(reader, 0, typeof(T));
return value == DBNull.Value ? default(T) : value;
}, token)
.Then(x =>
{
var columValues = new HashSet<T>();
x.Each(o => columValues.Add((T)o));
return columValues;
});
}, token);

var columnValues = new HashSet<T>();
ret.Each(o => columnValues.Add((T)o));
return columnValues;
}

internal static Task<Dictionary<K, List<V>>> LookupAsync<K, V>(this IDbCommand dbCmd, string sql, object anonType, CancellationToken token)
@@ -309,22 +305,23 @@ internal static Task<HashSet<T>> ColumnDistinctAsync<T>(this IDataReader reader,
}, map, token);
}

internal static Task<bool> ExistsAsync<T>(this IDbCommand dbCmd, object anonType, CancellationToken token)
internal static async Task<bool> ExistsAsync<T>(this IDbCommand dbCmd, object anonType, CancellationToken token)
{
string sql = null;
if (anonType != null) dbCmd.SetParameters(anonType.ToObjectDictionary(), excludeDefaults: true, sql:ref sql);

sql = dbCmd.GetFilterSql<T>();

return dbCmd.ScalarAsync(sql, token).Then(x => x != null);
var ret = await dbCmd.ScalarAsync(sql, token);
return ret != null;
}

internal static Task<bool> ExistsAsync<T>(this IDbCommand dbCmd, string sql, object anonType, CancellationToken token)
internal static async Task<bool> ExistsAsync<T>(this IDbCommand dbCmd, string sql, object anonType, CancellationToken token)
{
if (anonType != null) dbCmd.SetParameters(anonType.ToObjectDictionary(), (bool)false, sql:ref sql);

return dbCmd.ScalarAsync(dbCmd.GetDialectProvider().ToSelectStatement(typeof(T), sql), token)
.Then(x => x != null);
var ret = await dbCmd.ScalarAsync(dbCmd.GetDialectProvider().ToSelectStatement(typeof(T), sql), token);
return ret != null;
}

// procedures ...
@@ -52,84 +52,94 @@ public static Task<List<T>> ConvertToListAsync<T>(this IDbCommand dbCmd)
return dbCmd.ConvertToListAsync<T>(null, default(CancellationToken));
}

public static Task<List<T>> ConvertToListAsync<T>(this IDbCommand dbCmd, string sql, CancellationToken token)
public static async Task<List<T>> ConvertToListAsync<T>(this IDbCommand dbCmd, string sql, CancellationToken token)
{
if (sql != null)
dbCmd.CommandText = sql;

if (OrmLiteConfig.ResultsFilter != null)
return OrmLiteConfig.ResultsFilter.GetList<T>(dbCmd).InTask();
return OrmLiteConfig.ResultsFilter.GetList<T>(dbCmd);

var dialectProvider = dbCmd.GetDialectProvider();
return dbCmd.ExecReaderAsync(dbCmd.CommandText, token)
.Then(reader =>
OrmLiteUtils.IsScalar<T>()
? reader.ColumnAsync<T>(dialectProvider, token)
: reader.ConvertToListAsync<T>(dialectProvider, null, token)).Unwrap();
using (var reader = await dbCmd.ExecReaderAsync(dbCmd.CommandText, token))
{
if (OrmLiteUtils.IsScalar<T>())
return await reader.ColumnAsync<T>(dialectProvider, token);

return await reader.ConvertToListAsync<T>(dialectProvider, null, token);
}
}

public static Task<IList> ConvertToListAsync(this IDbCommand dbCmd, Type refType)
{
return dbCmd.ConvertToListAsync(refType, null, default(CancellationToken));
}

public static Task<IList> ConvertToListAsync(this IDbCommand dbCmd, Type refType, string sql, CancellationToken token)
public static async Task<IList> ConvertToListAsync(this IDbCommand dbCmd, Type refType, string sql, CancellationToken token)
{
if (sql != null)
dbCmd.CommandText = sql;

if (OrmLiteConfig.ResultsFilter != null)
return OrmLiteConfig.ResultsFilter.GetRefList(dbCmd, refType).InTask();
return OrmLiteConfig.ResultsFilter.GetRefList(dbCmd, refType);

var dialectProvider = dbCmd.GetDialectProvider();
return dbCmd.ExecReaderAsync(dbCmd.CommandText, token)
.Then(reader => reader.ConvertToListAsync(dialectProvider, refType, token)).Unwrap();
using (var reader = await dbCmd.ExecReaderAsync(dbCmd.CommandText, token))
{
return await reader.ConvertToListAsync(dialectProvider, refType, token);
}
}

internal static Task<List<T>> ExprConvertToListAsync<T>(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams, HashSet<string> onlyFields, CancellationToken token)
internal static async Task<List<T>> ExprConvertToListAsync<T>(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams, HashSet<string> onlyFields, CancellationToken token)
{
if (sql != null)
dbCmd.CommandText = sql;

dbCmd.SetParameters(sqlParams);

if (OrmLiteConfig.ResultsFilter != null)
return OrmLiteConfig.ResultsFilter.GetList<T>(dbCmd).InTask();
return OrmLiteConfig.ResultsFilter.GetList<T>(dbCmd);

var dialectProvider = dbCmd.GetDialectProvider();
return dbCmd.ExecReaderAsync(dbCmd.CommandText, token)
.Then(reader => reader.ConvertToListAsync<T>(dialectProvider, onlyFields, token)).Unwrap();
using (var reader = await dbCmd.ExecReaderAsync(dbCmd.CommandText, token))
{
return await reader.ConvertToListAsync<T>(dialectProvider, onlyFields, token);
}
}

public static Task<T> ConvertToAsync<T>(this IDbCommand dbCmd)
{
return dbCmd.ConvertToAsync<T>(null, default(CancellationToken));
}

public static Task<T> ConvertToAsync<T>(this IDbCommand dbCmd, string sql, CancellationToken token)
public static async Task<T> ConvertToAsync<T>(this IDbCommand dbCmd, string sql, CancellationToken token)
{
if (sql != null)
dbCmd.CommandText = sql;

if (OrmLiteConfig.ResultsFilter != null)
return OrmLiteConfig.ResultsFilter.GetSingle<T>(dbCmd).InTask();
return OrmLiteConfig.ResultsFilter.GetSingle<T>(dbCmd);

var dialectProvider = dbCmd.GetDialectProvider();
return dbCmd.ExecReaderAsync(dbCmd.CommandText, token)
.Then(reader => reader.ConvertToAsync<T>(dialectProvider, token)).Unwrap();
using (var reader = await dbCmd.ExecReaderAsync(dbCmd.CommandText, token))
{
return await reader.ConvertToAsync<T>(dialectProvider, token);
}
}

internal static Task<object> ConvertToAsync(this IDbCommand dbCmd, Type refType, string sql, CancellationToken token)
internal static async Task<object> ConvertToAsync(this IDbCommand dbCmd, Type refType, string sql, CancellationToken token)
{
if (sql != null)
dbCmd.CommandText = sql;

if (OrmLiteConfig.ResultsFilter != null)
return OrmLiteConfig.ResultsFilter.GetRefSingle(dbCmd, refType).InTask();
return OrmLiteConfig.ResultsFilter.GetRefSingle(dbCmd, refType);

var dialectProvider = dbCmd.GetDialectProvider();
return dbCmd.ExecReaderAsync(dbCmd.CommandText, token)
.Then(reader => reader.ConvertToAsync(dialectProvider, refType, token)).Unwrap();
using (var reader = await dbCmd.ExecReaderAsync(dbCmd.CommandText, token))
{
return await reader.ConvertToAsync(dialectProvider, refType, token);
}
}

public static Task<T> ScalarAsync<T>(this IDbCommand dbCmd)
@@ -142,17 +152,19 @@ public static Task<T> ScalarAsync<T>(this IDbCommand dbCmd, string sql, IEnumera
return dbCmd.SetParameters(sqlParams).ScalarAsync<T>(sql, token);
}

public static Task<T> ScalarAsync<T>(this IDbCommand dbCmd, string sql, CancellationToken token)
public static async Task<T> ScalarAsync<T>(this IDbCommand dbCmd, string sql, CancellationToken token)
{
if (sql != null)
dbCmd.CommandText = sql;

if (OrmLiteConfig.ResultsFilter != null)
return OrmLiteConfig.ResultsFilter.GetScalar<T>(dbCmd).InTask();
return OrmLiteConfig.ResultsFilter.GetScalar<T>(dbCmd);

var dialectProvider = dbCmd.GetDialectProvider();
return dbCmd.ExecReaderAsync(dbCmd.CommandText, token)
.Then(reader => reader.ScalarAsync<T>(dialectProvider, token)).Unwrap();
using (var reader = await dbCmd.ExecReaderAsync(dbCmd.CommandText, token))
{
return await reader.ScalarAsync<T>(dialectProvider, token);
}
}

public static Task<object> ScalarAsync(this IDbCommand dbCmd)
@@ -197,91 +209,101 @@ public static Task<long> ExecLongScalarAsync(this IDbCommand dbCmd, string sql,
return dbCmd.LongScalarAsync(token);
}

internal static Task<T> ExprConvertToAsync<T>(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams, CancellationToken token)
internal static async Task<T> ExprConvertToAsync<T>(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams, CancellationToken token)
{
if (sql != null)
dbCmd.CommandText = sql;

dbCmd.SetParameters(sqlParams);

if (OrmLiteConfig.ResultsFilter != null)
return OrmLiteConfig.ResultsFilter.GetSingle<T>(dbCmd).InTask();
return OrmLiteConfig.ResultsFilter.GetSingle<T>(dbCmd);

var dialectProvider = dbCmd.GetDialectProvider();
return dbCmd.ExecReaderAsync(dbCmd.CommandText, token)
.Then(reader => reader.ConvertToAsync<T>(dialectProvider, token)).Unwrap();
using (var reader = await dbCmd.ExecReaderAsync(dbCmd.CommandText, token))
{
return await reader.ConvertToAsync<T>(dialectProvider, token);
}
}

internal static Task<List<T>> ColumnAsync<T>(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams, CancellationToken token)
{
return dbCmd.SetParameters(sqlParams).ColumnAsync<T>(sql, token);
}

internal static Task<List<T>> ColumnAsync<T>(this IDbCommand dbCmd, string sql, CancellationToken token)
internal static async Task<List<T>> ColumnAsync<T>(this IDbCommand dbCmd, string sql, CancellationToken token)
{
if (sql != null)
dbCmd.CommandText = sql;

if (OrmLiteConfig.ResultsFilter != null)
return OrmLiteConfig.ResultsFilter.GetColumn<T>(dbCmd).InTask();
return OrmLiteConfig.ResultsFilter.GetColumn<T>(dbCmd);

var dialectProvider = dbCmd.GetDialectProvider();
return dbCmd.ExecReaderAsync(dbCmd.CommandText, token)
.Then(reader => reader.ColumnAsync<T>(dialectProvider, token)).Unwrap();
using (var reader = await dbCmd.ExecReaderAsync(dbCmd.CommandText, token))
{
return await reader.ColumnAsync<T>(dialectProvider, token);
}
}

internal static Task<HashSet<T>> ColumnDistinctAsync<T>(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams, CancellationToken token)
{
return dbCmd.SetParameters(sqlParams).ColumnDistinctAsync<T>(sql, token);
}

internal static Task<HashSet<T>> ColumnDistinctAsync<T>(this IDbCommand dbCmd, string sql, CancellationToken token)
internal static async Task<HashSet<T>> ColumnDistinctAsync<T>(this IDbCommand dbCmd, string sql, CancellationToken token)
{
if (sql != null)
dbCmd.CommandText = sql;

if (OrmLiteConfig.ResultsFilter != null)
return OrmLiteConfig.ResultsFilter.GetColumnDistinct<T>(dbCmd).InTask();
return OrmLiteConfig.ResultsFilter.GetColumnDistinct<T>(dbCmd);

var dialectProvider = dbCmd.GetDialectProvider();
return dbCmd.ExecReaderAsync(dbCmd.CommandText, token)
.Then(reader => reader.ColumnDistinctAsync<T>(dialectProvider, token)).Unwrap();
using (var reader = await dbCmd.ExecReaderAsync(dbCmd.CommandText, token))
{
return await reader.ColumnDistinctAsync<T>(dialectProvider, token);
}
}

internal static Task<Dictionary<K, V>> DictionaryAsync<K, V>(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams, CancellationToken token)
{
return dbCmd.SetParameters(sqlParams).DictionaryAsync<K, V>(sql, token);
}

internal static Task<Dictionary<K, V>> DictionaryAsync<K, V>(this IDbCommand dbCmd, string sql, CancellationToken token)
internal static async Task<Dictionary<K, V>> DictionaryAsync<K, V>(this IDbCommand dbCmd, string sql, CancellationToken token)
{
if (sql != null)
dbCmd.CommandText = sql;

if (OrmLiteConfig.ResultsFilter != null)
return OrmLiteConfig.ResultsFilter.GetDictionary<K, V>(dbCmd).InTask();
return OrmLiteConfig.ResultsFilter.GetDictionary<K, V>(dbCmd);

var dialectProvider = dbCmd.GetDialectProvider();
return dbCmd.ExecReaderAsync(dbCmd.CommandText, token)
.Then(reader => reader.DictionaryAsync<K, V>(dialectProvider, token)).Unwrap();
using (var reader = await dbCmd.ExecReaderAsync(dbCmd.CommandText, token))
{
return await reader.DictionaryAsync<K, V>(dialectProvider, token);
}
}

internal static Task<Dictionary<K, List<V>>> LookupAsync<K, V>(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams, CancellationToken token)
{
return dbCmd.SetParameters(sqlParams).LookupAsync<K, V>(sql, token);
}

internal static Task<Dictionary<K, List<V>>> LookupAsync<K, V>(this IDbCommand dbCmd, string sql, CancellationToken token)
internal static async Task<Dictionary<K, List<V>>> LookupAsync<K, V>(this IDbCommand dbCmd, string sql, CancellationToken token)
{
if (sql != null)
dbCmd.CommandText = sql;

if (OrmLiteConfig.ResultsFilter != null)
return OrmLiteConfig.ResultsFilter.GetLookup<K, V>(dbCmd).InTask();
return OrmLiteConfig.ResultsFilter.GetLookup<K, V>(dbCmd);

var dialectProvider = dbCmd.GetDialectProvider();
return dbCmd.ExecReaderAsync(dbCmd.CommandText, token)
.Then(reader => reader.LookupAsync<K, V>(dialectProvider, token)).Unwrap();
using (var reader = await dbCmd.ExecReaderAsync(dbCmd.CommandText, token))
{
return await reader.LookupAsync<K, V>(dialectProvider, token);
}
}
}
}
Oops, something went wrong.

0 comments on commit ff053af

Please sign in to comment.