forked from IoTSharp/IoTSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EFCoreExtension.cs
143 lines (129 loc) · 8.11 KB
/
EFCoreExtension.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using IoTSharp.Extensions.EFCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Threading.Tasks;
namespace Microsoft.EntityFrameworkCore
{
public static class EFCoreExtension
{
private static IRelationalDatabaseFacadeDependencies GetFacadeDependencies(DatabaseFacade databaseFacade)
{
if (((IDatabaseFacadeDependenciesAccessor)databaseFacade).Dependencies is IRelationalDatabaseFacadeDependencies relationalDatabaseFacadeDependencies)
{
return relationalDatabaseFacadeDependencies;
}
throw new InvalidOperationException(RelationalStrings.RelationalNotInUse);
}
public static int ExecuteNonQuery(this DatabaseFacade databaseFacade, string sql, params object[] parameters)
{
IRelationalDatabaseFacadeDependencies facadeDependencies = GetFacadeDependencies(databaseFacade);
IConcurrencyDetector concurrencyDetector = ((IDatabaseFacadeDependencies)facadeDependencies).ConcurrencyDetector;
using (concurrencyDetector.EnterCriticalSection())
{
var rawSqlCommand = databaseFacade.Build(sql, parameters);
return rawSqlCommand.RelationalCommand.ExecuteNonQuery(new RelationalCommandParameterObject(facadeDependencies.RelationalConnection, rawSqlCommand.ParameterValues, null, ((IDatabaseFacadeDependenciesAccessor)databaseFacade).Context, facadeDependencies.CommandLogger));
}
}
public static async Task<int> ExecuteNonQueryAsync(this DatabaseFacade databaseFacade, string sql, params object[] parameters)
{
IRelationalDatabaseFacadeDependencies facadeDependencies = GetFacadeDependencies(databaseFacade);
IConcurrencyDetector concurrencyDetector = ((IDatabaseFacadeDependencies)facadeDependencies).ConcurrencyDetector;
var commandLogger = facadeDependencies.CommandLogger;
using (concurrencyDetector.EnterCriticalSection())
{
var rawSqlCommand = databaseFacade.Build(sql, parameters);
return await rawSqlCommand.RelationalCommand.ExecuteNonQueryAsync(new RelationalCommandParameterObject(facadeDependencies.RelationalConnection, rawSqlCommand.ParameterValues, null, ((IDatabaseFacadeDependenciesAccessor)databaseFacade).Context, commandLogger));
}
}
internal static T ExecuteReader<T>(this DatabaseFacade databaseFacade, string sql, object[] parameters, Func<DbDataReader, T> func)
{
T result = default(T);
IRelationalDatabaseFacadeDependencies facadeDependencies = GetFacadeDependencies(databaseFacade);
IConcurrencyDetector concurrencyDetector = ((IDatabaseFacadeDependencies)facadeDependencies).ConcurrencyDetector;
var commandLogger = (facadeDependencies).CommandLogger;
using (concurrencyDetector.EnterCriticalSection())
{
var rawSqlCommand = databaseFacade.Build(sql, parameters);
using (var reader = rawSqlCommand.RelationalCommand.ExecuteReader(new RelationalCommandParameterObject(facadeDependencies.RelationalConnection, rawSqlCommand.ParameterValues, null, ((IDatabaseFacadeDependenciesAccessor)databaseFacade).Context, commandLogger)))
{
result = func.Invoke(reader.DbDataReader);
}
}
return result;
}
internal class RAWSQLCommand
{
internal IRelationalCommand RelationalCommand { get; set; }
internal IReadOnlyDictionary<string, object> ParameterValues { get; set; }
}
private static RAWSQLCommand Build(this DatabaseFacade databaseFacade, string sql, params object[] parameters)
{
var builder = GetFacadeDependencies(databaseFacade).RawSqlCommandBuilder;
RAWSQLCommand command;
if (parameters == null || parameters.Length == 0)
{
command = new RAWSQLCommand { RelationalCommand = builder.Build(sql) };
}
else
{
var cmdx = builder.Build(sql, parameters);
command = new RAWSQLCommand() { RelationalCommand = cmdx.RelationalCommand, ParameterValues = cmdx.ParameterValues };
}
return command;
}
public static SqlQuery<T> SqlQuery<T>(this DatabaseFacade databaseFacade, string sql)
{
return new SqlQuery<T>(databaseFacade, sql, Array.Empty<object>());
}
public static SqlQuery<T> SqlQuery<T>(this DatabaseFacade databaseFacade, string sql, params object[] parameters)
{
return new SqlQuery<T>(databaseFacade, sql, parameters);
}
internal static async Task<T> ExecuteReaderAsync<T>(this DatabaseFacade databaseFacade, string sql, object[] parameters, Func<DbDataReader, Task<T>> func)
{
T result = default(T);
IRelationalDatabaseFacadeDependencies facadeDependencies = GetFacadeDependencies(databaseFacade);
IConcurrencyDetector concurrencyDetector = ((IDatabaseFacadeDependencies)facadeDependencies).ConcurrencyDetector;
var commandLogger = facadeDependencies.CommandLogger;
using (concurrencyDetector.EnterCriticalSection())
{
var rawSqlCommand = databaseFacade.Build(sql, parameters);
using (var reader = await rawSqlCommand.RelationalCommand.ExecuteReaderAsync(new RelationalCommandParameterObject(facadeDependencies.RelationalConnection, rawSqlCommand.ParameterValues, null, ((IDatabaseFacadeDependenciesAccessor)databaseFacade).Context, commandLogger)))
{
result = await func.Invoke(reader.DbDataReader);
}
}
return result;
}
public static async Task<T> ExecuteScalarAsync<T>(this DatabaseFacade databaseFacade, string sql, params object[] parameters)
=> (T)await ExecuteScalarAsync(databaseFacade, sql, parameters);
public static T ExecuteScalar<T>(this DatabaseFacade databaseFacade, string sql, params object[] parameters)
=> (T)ExecuteScalar(databaseFacade, sql, parameters);
public static async Task<object> ExecuteScalarAsync(this DatabaseFacade databaseFacade, string sql, params object[] parameters)
{
IRelationalDatabaseFacadeDependencies facadeDependencies = GetFacadeDependencies(databaseFacade);
IConcurrencyDetector concurrencyDetector = ((IDatabaseFacadeDependencies)facadeDependencies).ConcurrencyDetector;
var commandLogger = facadeDependencies.CommandLogger;
using (concurrencyDetector.EnterCriticalSection())
{
var rawSqlCommand = databaseFacade.Build(sql, parameters);
return await rawSqlCommand.RelationalCommand.ExecuteScalarAsync(new RelationalCommandParameterObject(facadeDependencies.RelationalConnection, rawSqlCommand.ParameterValues, null, ((IDatabaseFacadeDependenciesAccessor)databaseFacade).Context, commandLogger));
}
}
public static object ExecuteScalar(this DatabaseFacade databaseFacade, string sql, params object[] parameters)
{
IRelationalDatabaseFacadeDependencies facadeDependencies = GetFacadeDependencies(databaseFacade);
IConcurrencyDetector concurrencyDetector = ((IDatabaseFacadeDependencies)facadeDependencies).ConcurrencyDetector;
var commandLogger = facadeDependencies.CommandLogger;
using (concurrencyDetector.EnterCriticalSection())
{
var rawSqlCommand = databaseFacade.Build(sql, parameters);
return rawSqlCommand.RelationalCommand.ExecuteScalar(new RelationalCommandParameterObject(facadeDependencies.RelationalConnection, rawSqlCommand.ParameterValues, null, ((IDatabaseFacadeDependenciesAccessor)databaseFacade).Context, commandLogger));
}
}
}
}