-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSqlParser.cs
149 lines (121 loc) · 5.53 KB
/
SqlParser.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
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
namespace SqlGadgetry
{
public class SqlParser
{
private readonly SqlLexerOptions _lexerOptions;
public SqlParser()
: this(null)
{
}
public SqlParser(SqlLexerOptions lexerOptions)
{
_lexerOptions = lexerOptions;
}
public LambdaExpression Parse<TContext>(string sql)
{
SqlToken[] columnTokens;
SqlToken[] tableSourceTokens;
ParseCore(sql, out columnTokens, out tableSourceTokens);
if (columnTokens.Length == 0)
{
throw new InvalidOperationException();
}
if (tableSourceTokens.Length == 0)
{
throw new InvalidOperationException();
}
if (tableSourceTokens.Length > 1)
{
throw new NotSupportedException();
}
return CreateQueryExpression<TContext>(columnTokens, tableSourceTokens);
}
private void ParseCore(string sql, out SqlToken[] columnTokens, out SqlToken[] tableSourceTokens)
{
var localColumnTokens = new List<SqlToken>();
var localTableSourceTokens = new List<SqlToken>();
using (var lexer = CreateLexer(sql))
{
SqlToken token;
while ((token = lexer.Next()) != null)
{
switch (lexer.State)
{
case SqlLexerState.SelectList:
if (token.Type == SqlTokenType.SelectKeyword)
{
continue;
}
if (token.Type == SqlTokenType.SelectListColumn)
{
localColumnTokens.Add(token);
continue;
}
throw new NotSupportedException();
case SqlLexerState.TableSource:
if (token.Type == SqlTokenType.FromKeyword)
{
continue;
}
throw new NotSupportedException();
case SqlLexerState.End:
if (token.Type == SqlTokenType.TableSource)
{
localTableSourceTokens.Add(token);
continue;
}
throw new NotSupportedException();
}
}
}
columnTokens = localColumnTokens.ToArray();
tableSourceTokens = localTableSourceTokens.ToArray();
}
private LambdaExpression CreateQueryExpression<TContext>(SqlToken[] columnTokens, SqlToken[] tableSourceTokens)
{
ParameterExpression contextExp = Expression.Parameter(typeof(TContext));
MemberExpression sourceExp = Expression.PropertyOrField(contextExp, tableSourceTokens[0].Text);
Type sourceType = sourceExp.Type.GenericTypeArguments[0];
Type resultType = CreateDynamicAnonymousType(columnTokens.Select(columnToken => sourceType.GetProperty(columnToken.Text)));
NewExpression newResultExp = Expression.New(resultType);
ParameterExpression sourceParameterExp = Expression.Parameter(sourceType);
IEnumerable<MemberBinding> resultTypeBindings = from columnToken in columnTokens
select Expression.Bind(
resultType.GetField(columnToken.Text),
Expression.MakeMemberAccess(sourceParameterExp, sourceType.GetProperty(columnToken.Text)));
MemberInitExpression initExp = Expression.MemberInit(newResultExp, resultTypeBindings);
Expression selectorExp = Expression.Lambda(initExp, sourceParameterExp);
MethodCallExpression selectExp = Expression.Call(
typeof(Queryable),
"Select",
new[] { sourceType, resultType },
sourceExp,
Expression.Quote(selectorExp));
return Expression.Lambda(selectExp, contextExp);
}
private static Type CreateDynamicAnonymousType(IEnumerable<PropertyInfo> prototypeProperties)
{
var dynamicAssemblyName = new AssemblyName("DynamicAssembly");
AssemblyBuilder dynamicAssembly = AssemblyBuilder.DefineDynamicAssembly(dynamicAssemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder dynamicModule = dynamicAssembly.DefineDynamicModule("DynamicModule");
TypeBuilder dynamicAnonymousType = dynamicModule.DefineType("DynamicAnonymous_" + Guid.NewGuid());
foreach (var property in prototypeProperties)
{
dynamicAnonymousType.DefineField(property.Name, property.PropertyType, FieldAttributes.Public);
}
return dynamicAnonymousType.CreateType();
}
private SqlLexer CreateLexer(string sql)
{
return _lexerOptions != null
? new SqlLexer(sql, _lexerOptions)
: new SqlLexer(sql);
}
}
}