-
Notifications
You must be signed in to change notification settings - Fork 86
/
DbCommandExtension.tt
135 lines (123 loc) · 3.85 KB
/
DbCommandExtension.tt
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".generated.cs" #>
using System.Data;
using System.Data.Common;
namespace WeihanLi.Extensions;
#nullable enable
public static partial class DataExtension
{
<#
var commandNames = new string[]{
"Select",
};
foreach(var cmd in commandNames)
{
#>
public static IEnumerable<dynamic> <#= cmd #>(this DbCommand command)
{
using (var reader = command.ExecuteReader())
{
return reader.ToExpandoObjects();
}
}
public static async Task<IEnumerable<dynamic>> <#= cmd #>Async(this DbCommand command, CancellationToken cancellationToken = default)
{
using (var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false))
{
var list = new List<dynamic>();
while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
{
list.Add(reader.ToExpandoObject(true));
}
return list;
}
}
public static IEnumerable<T> <#= cmd #><T>(this DbCommand command)
{
using (var reader = command.ExecuteReader())
{
var list = new List<T>();
while (reader.Read())
{
list.Add(reader.ToEntity<T>(true)!);
}
return list;
}
}
public static async Task<IEnumerable<T>> <#= cmd #>Async<T>(this DbCommand command, CancellationToken cancellationToken = default)
{
using (var reader = await command.ExecuteReaderAsync(cancellationToken))
{
var list = new List<T>();
while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
{
list.Add(reader.ToEntity<T>(true)!);
}
return list;
}
}
<# }#>
<#
var commandNames1 = new string[]{
"Fetch"
};
foreach(var cmd in commandNames1)
{
#>
public static dynamic <#= cmd #>(this DbCommand command)
{
using (var reader = command.ExecuteReader())
{
return reader.ToExpandoObject();
}
}
public static async Task<dynamic> <#= cmd #>Async(this DbCommand command, CancellationToken cancellationToken = default)
{
using (var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false))
{
await reader.ReadAsync().ConfigureAwait(false);
return reader.ToExpandoObject(true);
}
}
public static T? <#= cmd #><T>(this DbCommand command)
{
using (var reader = command.ExecuteReader())
{
return reader.ToEntity<T>();
}
}
public static async Task<T?> <#= cmd #>Async<T>(this DbCommand command, CancellationToken cancellationToken = default)
{
using (var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false))
{
return reader.ToEntity<T>();
}
}
<# }#>
<#
var commandNames2 = new string[]{
"ExecuteDataTable"
};
foreach(var cmd in commandNames2)
{
#>
public static DataTable <#= cmd #>(this DbCommand command)
{
using (var reader = command.ExecuteReader())
{
return reader.ToDataTable();
}
}
public static async Task<DataTable> <#= cmd #>Async(this DbCommand command, CancellationToken cancellationToken = default)
{
using (var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false))
{
return reader.ToDataTable();
}
}
<# }#>
}