-
Notifications
You must be signed in to change notification settings - Fork 0
/
Generator.cs
156 lines (139 loc) · 6.25 KB
/
Generator.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
150
151
152
153
154
155
156
using ORM.AST;
using ORM.Lexicography.Tokens;
namespace ORM.Generator
{
class Generator
{
private readonly Node ast;
private string sqlQuery;
public Generator(Node ast)
{
this.ast = ast;
this.sqlQuery = "";
}
public string Generate()
{
var currentNode = this.ast;
while (currentNode != null)
{
// SELECT SECTION
if (currentNode.TokenType() == Token.SELECT)
{
this.sqlQuery += "SELECT";
foreach (var child in currentNode.Children())
{
var token = child.GetTokens().First();
var chars = token.Characters()
.Split('.')
.Last();
this.sqlQuery += " " + chars + ",";
}
this.sqlQuery = this.sqlQuery.TrimEnd(',');
}
// FROM SECTION
else if (currentNode.TokenType() == Token.FROM)
{
this.sqlQuery += " FROM";
this.sqlQuery += " " + currentNode.Children()
.First()
.GetTokens()
.First()
.Characters();
}
// WHERE SECTION
else if (currentNode.TokenType() == Token.WHERE)
{
this.sqlQuery += " WHERE";
// Where clause only has one predicate. No extra AndAlso or OrElse.
if (currentNode.Children().Count() == 1)
{
this.sqlQuery += this.constructWherePredicateString(currentNode.Children().First());
}
// Where clause that contains AndAlso or OrElse.
else if (currentNode.Children().Count() == 0)
{
var innerWhereNode = currentNode.NextNode();
while (innerWhereNode != null && TokenCls.isTokenPartOfGroup(innerWhereNode.TokenType(), TokenCls.ComparisonTokenGroup))
{
int predicateChildrenCount = innerWhereNode.Children().Count();
// Small optimization reducing code duplication. If the count = 2, means we have a "predicate AndAlso/OrElse predicate" construct.
if (predicateChildrenCount == 1 || predicateChildrenCount == 2)
{
// LHS of predicate
this.sqlQuery += this.constructWherePredicateString(innerWhereNode.Children().First());
// AndAlso or OrElse predicate
switch (innerWhereNode.TokenType())
{
case Token.AND_ALSO:
this.sqlQuery += " AND";
break;
case Token.OR_ELSE:
this.sqlQuery += " OR";
break;
}
// RHS of combined predicate
if (predicateChildrenCount == 2)
{
this.sqlQuery += this.constructWherePredicateString(innerWhereNode.Children().Last());
}
}
else
{
throw new Exception("WHERE CLAUSE: invalid constructed.");
}
innerWhereNode = innerWhereNode.NextNode();
}
}
else if (currentNode.Children().Count() > 1)
{
throw new Exception("WHERE: incorrect construct of node. 2 or more children.");
}
}
else if (currentNode.TokenType() == Token.ORDER_BY)
{
var propertyIndex = currentNode.GetTokens().FindIndex(t => t.Type() == Token.OBJECT_PROPERTY);
var orderingIndex = currentNode.GetTokens().FindIndex(t => t.Type() == Token.ORDERING);
if (propertyIndex == -1 || orderingIndex == -1)
{
throw new Exception("ORDER BY: invalid node constructed");
}
var property = currentNode
.GetTokens()[propertyIndex]
.Characters()
.Split('.')[1];
var ordering = currentNode
.GetTokens()[orderingIndex]
.Characters() switch
{
"Ascending" => "ASC",
"Descending" => "DESC",
_ => throw new Exception("ORDER BY: can't determine ordering.")
};
this.sqlQuery += " ORDER BY " + property + " " + ordering;
}
currentNode = currentNode.NextNode();
}
return this.sqlQuery.Trim();
}
private string constructWherePredicateString(AST.Node node)
{
string property = node
.GetTokens()
.First()
.Characters()
.Split('.')
.Last();
string op = node.TokenType() switch
{
Token.IS_EQUAL => "=",
Token.NOT_EQUAL => "!=",
_ => throw new Exception("WHERE: can't find operator.")
};
string value = node
.GetTokens()
.Last()
.Characters();
return " " + property + " " + op + " " + value;
}
}
}