-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbQueryProvider.cs
46 lines (40 loc) · 1.46 KB
/
DbQueryProvider.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
using System.Data.Common;
using System.Linq.Expressions;
using System.Reflection;
namespace QueryProviderExample
{
public class DbQueryProvider(DbConnection connection) : MyQueryProvider
{
public override object Execute(Expression expression)
{
DbCommand command = connection.CreateCommand();
var result = Translate(expression);
command.CommandText = result.CommandText;
DbDataReader reader = command.ExecuteReader();
var elementType = GetEntityType(expression);
var projector = result.Projector?.Compile();
return Activator.CreateInstance(
typeof(ProjectionReader<>).MakeGenericType(elementType),
BindingFlags.Default,
null,
[reader, projector],
null)!;
}
public override string GetQueryText(Expression expression)
{
return Translate(expression).CommandText;
}
private TranslateResult Translate(Expression expression)
{
throw new NotImplementedException();
}
private Type GetEntityType(Expression expression)
{
if (expression is MethodCallExpression m)
{
return m.Method.ReturnType.GetGenericArguments().First();
}
throw new NotSupportedException($"The expression of type '{expression.NodeType}' is not supported");
}
}
}