-
Notifications
You must be signed in to change notification settings - Fork 40
/
DbContext.Async.cs
113 lines (92 loc) · 5.95 KB
/
DbContext.Async.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) Mondol. All rights reserved.
//
// Author: frank
// Email: [email protected]
// Created: 2017-01-22
//
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
namespace Mondol.DapperPoco
{
public partial class DbContext
{
#region Poco
public Task<int> InsertAsync<T>(T entity, string tableName = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class
{
return Task.Run(() => Insert(entity, tableName), cancellationToken);
}
public Task<int> UpdateAsync<T>(T entity, Expression<Func<T, object>> columns, string tableName = null, string primaryKeyName = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class
{
return Task.Run(() => Update(entity, columns, tableName, primaryKeyName), cancellationToken);
}
public Task<int> UpdateAsync<T>(T entity, ICollection<string> columns = null, string tableName = null, string primaryKeyName = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class
{
return Task.Run(() => Update(entity, columns, tableName, primaryKeyName), cancellationToken);
}
public Task<int> UpdateAsync<T>(IEnumerable<T> entities, Expression<Func<T, object>> columns = null, string tableName = null, string primaryKeyName = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class
{
return Task.Run(() => Update(entities, columns, tableName, primaryKeyName), cancellationToken);
}
public Task<int> DeleteAsync<T>(T entity, string tableName = null, string primaryKeyName = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class
{
return Task.Run(() => Delete(entity, tableName, primaryKeyName), cancellationToken);
}
public Task<int> DeleteByColumnsAsync<T>(T entity, Expression<Func<T, object>> columns, string tableName = null, CancellationToken cancellationToken = default(CancellationToken))
where T : class
{
return Task.Run(() => DeleteByColumns(entity, columns, tableName), cancellationToken);
}
public Task<int> SaveAsync<T>(T entity, string[] columns = null, string tableName = null, string primaryKeyName = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class
{
return Task.Run(() => Save(entity, columns, tableName, primaryKeyName), cancellationToken);
}
public Task<int> SaveAsync<T>(T entity, Expression<Func<T, object>> columns, string tableName = null, string primaryKeyName = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class
{
return Task.Run(() => Save(entity, columns, tableName, primaryKeyName), cancellationToken);
}
public Task<List<T>> FetchAllAsync<T>(string tableName = null, CancellationToken cancellationToken = default(CancellationToken)) where T : class
{
return Task.Run(() => FetchAll<T>(tableName), cancellationToken);
}
public Task<List<T>> FetchByPropertyAsync<T>(T entity, Expression<Func<T, object>> properties, string tableName = null, CancellationToken cancellationToken = default(CancellationToken))
where T : class
{
return Task.Run(() => FetchByProperty(entity, properties, tableName), cancellationToken);
}
public Task<T> ExecuteScalarAsync<T>(string sql, object sqlArgs = null, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.Run(() => ExecuteScalar<T>(sql, sqlArgs), cancellationToken);
}
#endregion
#region Query/Execute
public Task<T> FirstOrDefaultAsync<T>(string sql, object sqlArgs = null, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.Run(() => FirstOrDefault<T>(sql, sqlArgs), cancellationToken);
}
public Task<List<T>> FetchAsync<T>(string sql, object sqlArgs = null, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.Run(() => Fetch<T>(sql, sqlArgs), cancellationToken);
}
public Task<List<TReturn>> FetchAsync<TFirst, TSecond, TReturn>(Func<TFirst, TSecond, TReturn> map, string sql, object sqlArgs = null, string splitOn = "Id", CancellationToken cancellationToken = default(CancellationToken))
{
return Task.Run(() => Fetch(map, sql, sqlArgs, splitOn), cancellationToken);
}
public Task<int> ExecuteAsync(string sql, object sqlArgs = null, CancellationToken cancellationToken = default(CancellationToken))
{
return Task.Run(() => Execute(sql, sqlArgs), cancellationToken);
}
public Task<Paged<T>> PagedAsync<T>(int page, int itemsPerPage, string pageSql, object pageSqlArgs = null, string countSql = null, object countSqlArgs = null, CancellationToken cancellationToken = default(CancellationToken)) where T : new()
{
return Task.Run(() => Paged<T>(page, itemsPerPage, pageSql, pageSqlArgs, countSql, countSqlArgs), cancellationToken);
}
public Task<Paged<TReturn>> PagedAsync<TFirst, TSecond, TReturn>(Func<TFirst, TSecond, TReturn> map, int page, int itemsPerPage, string pageSql, object pageSqlArgs = null,
string countSql = null, object countSqlArgs = null, string splitOn = "Id", CancellationToken cancellationToken = default(CancellationToken)) where TReturn : new()
{
return Task.Run(() => Paged(map, page, itemsPerPage, pageSql, pageSqlArgs, countSql, countSqlArgs, splitOn), cancellationToken);
}
#endregion
}
}