oracle-sql-parser 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/README.md +58 -0
- data/Rakefile +41 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/oracle-sql-parser.rb +7 -0
- data/lib/oracle-sql-parser/ast.rb +38 -0
- data/lib/oracle-sql-parser/ast/array.rb +37 -0
- data/lib/oracle-sql-parser/ast/base.rb +89 -0
- data/lib/oracle-sql-parser/ast/between_condition.rb +4 -0
- data/lib/oracle-sql-parser/ast/current_of.rb +4 -0
- data/lib/oracle-sql-parser/ast/delete_statement.rb +5 -0
- data/lib/oracle-sql-parser/ast/delete_target.rb +6 -0
- data/lib/oracle-sql-parser/ast/exists_condition.rb +4 -0
- data/lib/oracle-sql-parser/ast/for_update_clause.rb +4 -0
- data/lib/oracle-sql-parser/ast/function_expression.rb +4 -0
- data/lib/oracle-sql-parser/ast/group_by_clause.rb +4 -0
- data/lib/oracle-sql-parser/ast/hash.rb +44 -0
- data/lib/oracle-sql-parser/ast/identifier.rb +7 -0
- data/lib/oracle-sql-parser/ast/in_condition.rb +4 -0
- data/lib/oracle-sql-parser/ast/insert_statement.rb +5 -0
- data/lib/oracle-sql-parser/ast/keyword.rb +8 -0
- data/lib/oracle-sql-parser/ast/like_condition.rb +4 -0
- data/lib/oracle-sql-parser/ast/logical_condition.rb +4 -0
- data/lib/oracle-sql-parser/ast/null_condition.rb +4 -0
- data/lib/oracle-sql-parser/ast/number_literal.rb +8 -0
- data/lib/oracle-sql-parser/ast/order_by_clause.rb +4 -0
- data/lib/oracle-sql-parser/ast/order_by_clause_item.rb +4 -0
- data/lib/oracle-sql-parser/ast/query_block.rb +17 -0
- data/lib/oracle-sql-parser/ast/regexp_condition.rb +4 -0
- data/lib/oracle-sql-parser/ast/rollup_cube_clause.rb +4 -0
- data/lib/oracle-sql-parser/ast/searched_case_expression.rb +8 -0
- data/lib/oracle-sql-parser/ast/select_statement.rb +5 -0
- data/lib/oracle-sql-parser/ast/simple_case_expression.rb +7 -0
- data/lib/oracle-sql-parser/ast/simple_comparision_condition.rb +4 -0
- data/lib/oracle-sql-parser/ast/subquery.rb +5 -0
- data/lib/oracle-sql-parser/ast/text_literal.rb +7 -0
- data/lib/oracle-sql-parser/ast/update_set_column.rb +4 -0
- data/lib/oracle-sql-parser/ast/update_statement.rb +4 -0
- data/lib/oracle-sql-parser/ast/where_clause.rb +4 -0
- data/lib/oracle-sql-parser/grammar.rb +10 -0
- data/lib/oracle-sql-parser/grammar/condition.treetop +224 -0
- data/lib/oracle-sql-parser/grammar/delete.treetop +68 -0
- data/lib/oracle-sql-parser/grammar/expression.treetop +236 -0
- data/lib/oracle-sql-parser/grammar/grammar.treetop +166 -0
- data/lib/oracle-sql-parser/grammar/insert.treetop +112 -0
- data/lib/oracle-sql-parser/grammar/reserved_word_generator.rb +233 -0
- data/lib/oracle-sql-parser/grammar/select.treetop +388 -0
- data/lib/oracle-sql-parser/grammar/update.treetop +113 -0
- data/lib/oracle-sql-parser/treetop_ext.rb +11 -0
- data/lib/oracle-sql-parser/version.rb +3 -0
- data/oracle-sql-parser.gemspec +28 -0
- metadata +176 -0
@@ -0,0 +1,17 @@
|
|
1
|
+
module OracleSqlParser::Ast
|
2
|
+
class QueryBlock < Hash
|
3
|
+
def to_sql
|
4
|
+
[
|
5
|
+
"select",
|
6
|
+
@ast[:hint],
|
7
|
+
@ast[:modifier],
|
8
|
+
@ast[:select_list],
|
9
|
+
"from",
|
10
|
+
@ast[:select_sources],
|
11
|
+
@ast[:where_clause],
|
12
|
+
@ast[:group_by_clause],
|
13
|
+
@ast[:model_clause]
|
14
|
+
].compact.map(&:to_sql).join(" ")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module OracleSqlParser::Grammar
|
2
|
+
end
|
3
|
+
require "oracle-sql-parser/grammar/reserved_word.rb"
|
4
|
+
require "oracle-sql-parser/grammar/expression.rb"
|
5
|
+
require "oracle-sql-parser/grammar/condition.rb"
|
6
|
+
require "oracle-sql-parser/grammar/select.rb"
|
7
|
+
require "oracle-sql-parser/grammar/update.rb"
|
8
|
+
require "oracle-sql-parser/grammar/delete.rb"
|
9
|
+
require "oracle-sql-parser/grammar/insert.rb"
|
10
|
+
require "oracle-sql-parser/grammar/grammar.rb"
|
@@ -0,0 +1,224 @@
|
|
1
|
+
module OracleSqlParser::Grammar
|
2
|
+
grammar Condition
|
3
|
+
# where
|
4
|
+
rule where_clause
|
5
|
+
where_keyword space? logical_condition {
|
6
|
+
def ast
|
7
|
+
OracleSqlParser::Ast::WhereClause[:condition => logical_condition.ast]
|
8
|
+
end
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
rule condition
|
13
|
+
comparision_condition /
|
14
|
+
floating_point_condition /
|
15
|
+
model_condition /
|
16
|
+
mutiset_condition /
|
17
|
+
pattern_maching_condition /
|
18
|
+
range_condition /
|
19
|
+
null_condition /
|
20
|
+
xml_condition /
|
21
|
+
compound_condition /
|
22
|
+
between_condition /
|
23
|
+
exists_condition /
|
24
|
+
in_condition /
|
25
|
+
is_of_type_condition {
|
26
|
+
def ast
|
27
|
+
super
|
28
|
+
end
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
rule logical_condition
|
33
|
+
(
|
34
|
+
op:not_keyword space? right:condition /
|
35
|
+
left:condition space? op:and_keyword space? right:condition /
|
36
|
+
left:condition space? op:or_keyword space? right:condition /
|
37
|
+
cond:condition
|
38
|
+
) {
|
39
|
+
def ast
|
40
|
+
if respond_to? :op
|
41
|
+
if respond_to? :left
|
42
|
+
OracleSqlParser::Ast::LogicalCondition[
|
43
|
+
:left => left.ast,
|
44
|
+
:op => op.ast,
|
45
|
+
:right => right.ast]
|
46
|
+
else
|
47
|
+
OracleSqlParser::Ast::LogicalCondition[
|
48
|
+
:op => op.ast,
|
49
|
+
:right => right.ast]
|
50
|
+
end
|
51
|
+
else
|
52
|
+
cond.ast
|
53
|
+
end
|
54
|
+
end
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
rule comparision_condition
|
59
|
+
(
|
60
|
+
simple_comparision_condition /
|
61
|
+
group_comparision_condition
|
62
|
+
) {
|
63
|
+
def ast
|
64
|
+
super
|
65
|
+
end
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
rule group_comparision_condition
|
70
|
+
'group_comparision_condition'
|
71
|
+
end
|
72
|
+
|
73
|
+
rule simple_comparision_condition
|
74
|
+
(
|
75
|
+
left:expr space? op:('!=' / '^=' / '<>' / '>=' / '<=' / '=' / '>' / '<') space? right:expr /
|
76
|
+
'(' space? left:exprs space? ')' op:space? ('!=' / '^=' / '<>' / '=') space? '(' space? right:subquery space? ')'
|
77
|
+
) {
|
78
|
+
def ast
|
79
|
+
OracleSqlParser::Ast::SimpleComparisionCondition[
|
80
|
+
:left => left.ast,
|
81
|
+
:op => op.text_value,
|
82
|
+
:right => right.ast]
|
83
|
+
end
|
84
|
+
}
|
85
|
+
end
|
86
|
+
|
87
|
+
rule floating_point_condition
|
88
|
+
'floating_point_condition'
|
89
|
+
end
|
90
|
+
|
91
|
+
rule model_condition
|
92
|
+
'model_condition'
|
93
|
+
end
|
94
|
+
|
95
|
+
rule mutiset_condition
|
96
|
+
'mutiset_condition'
|
97
|
+
end
|
98
|
+
|
99
|
+
rule pattern_maching_condition
|
100
|
+
(
|
101
|
+
like_condition /
|
102
|
+
regexp_like_condition
|
103
|
+
) {
|
104
|
+
def ast
|
105
|
+
super
|
106
|
+
end
|
107
|
+
}
|
108
|
+
end
|
109
|
+
|
110
|
+
rule like_condition
|
111
|
+
target:ident space
|
112
|
+
n:(not_keyword:not_keyword space)?
|
113
|
+
like:(like_keyword / like2_keyword / like4_keyword / likec_keyword) space
|
114
|
+
text:text_literal
|
115
|
+
e:(space escape_keyword space escape_text:text_literal)? {
|
116
|
+
def ast
|
117
|
+
OracleSqlParser::Ast::LikeCondition[
|
118
|
+
:target => target.ast,
|
119
|
+
:not => not_keyword.ast,
|
120
|
+
:like => like.ast,
|
121
|
+
:text => text.ast,
|
122
|
+
:escape => escape_text.ast]
|
123
|
+
end
|
124
|
+
|
125
|
+
def not_keyword
|
126
|
+
n.elements && n.elements.first
|
127
|
+
end
|
128
|
+
|
129
|
+
def escape_text
|
130
|
+
if e.respond_to? :escape_text
|
131
|
+
e.escape_text
|
132
|
+
end
|
133
|
+
end
|
134
|
+
}
|
135
|
+
end
|
136
|
+
|
137
|
+
rule regexp_like_condition
|
138
|
+
regexp_like_keyword '(' space? target:ident space? ',' space? regexp:text_literal space? ')' {
|
139
|
+
def ast
|
140
|
+
OracleSqlParser::Ast::RegexpCondition[
|
141
|
+
:target => target.ast,
|
142
|
+
:regexp => regexp.ast]
|
143
|
+
end
|
144
|
+
}
|
145
|
+
end
|
146
|
+
|
147
|
+
rule range_condition
|
148
|
+
'range_condition'
|
149
|
+
end
|
150
|
+
|
151
|
+
rule null_condition
|
152
|
+
expr space is_keyword space n:(not_keyword space)? null_keyword {
|
153
|
+
def ast
|
154
|
+
OracleSqlParser::Ast::NullCondition[
|
155
|
+
:target => expr.ast,
|
156
|
+
:not => not_keyword.ast]
|
157
|
+
end
|
158
|
+
|
159
|
+
def not_keyword
|
160
|
+
n.elements && n.elements.first
|
161
|
+
end
|
162
|
+
}
|
163
|
+
end
|
164
|
+
|
165
|
+
rule xml_condition
|
166
|
+
'xml_condition'
|
167
|
+
end
|
168
|
+
|
169
|
+
rule compound_condition
|
170
|
+
'(' logical_condition ')' {
|
171
|
+
def ast
|
172
|
+
logical_condition.ast
|
173
|
+
end
|
174
|
+
}
|
175
|
+
end
|
176
|
+
|
177
|
+
rule between_condition
|
178
|
+
target:expr space n:(not_keyword space)? between_keyword space from:expr space and_keyword space to:expr {
|
179
|
+
def ast
|
180
|
+
OracleSqlParser::Ast::BetweenCondition[
|
181
|
+
:target => target.ast,
|
182
|
+
:not => not_keyword.ast,
|
183
|
+
:from => from.ast,
|
184
|
+
:to => to.ast
|
185
|
+
]
|
186
|
+
end
|
187
|
+
|
188
|
+
def not_keyword
|
189
|
+
n.elements && n.elements.first
|
190
|
+
end
|
191
|
+
}
|
192
|
+
end
|
193
|
+
|
194
|
+
rule exists_condition
|
195
|
+
exists_keyword space? '(' space? subquery space? ')' {
|
196
|
+
def ast
|
197
|
+
OracleSqlParser::Ast::ExistsCondition[
|
198
|
+
:target => subquery.ast
|
199
|
+
]
|
200
|
+
end
|
201
|
+
}
|
202
|
+
end
|
203
|
+
|
204
|
+
rule in_condition
|
205
|
+
target:expr space n:(not_keyword space)? in_keyword space? '(' space? values:( exprs / subquery ) space? ')' {
|
206
|
+
def ast
|
207
|
+
OracleSqlParser::Ast::InCondition[
|
208
|
+
:target => target.ast,
|
209
|
+
:not => not_keyword.ast,
|
210
|
+
:values => values.ast
|
211
|
+
]
|
212
|
+
end
|
213
|
+
|
214
|
+
def not_keyword
|
215
|
+
n.elements && n.elements.first
|
216
|
+
end
|
217
|
+
}
|
218
|
+
end
|
219
|
+
|
220
|
+
rule is_of_type_condition
|
221
|
+
'is_of_type_condition'
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module OracleSqlParser::Grammar
|
2
|
+
grammar Delete
|
3
|
+
rule delete_statement
|
4
|
+
delete_keyword space?
|
5
|
+
delete_from_clause space?
|
6
|
+
delete_condition:delete_condition?
|
7
|
+
returning_clause:returning_clause? {
|
8
|
+
def ast
|
9
|
+
OracleSqlParser::Ast::DeleteStatement[
|
10
|
+
:target => delete_from_clause.ast,
|
11
|
+
:condition => delete_condition.ast
|
12
|
+
]
|
13
|
+
end
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
rule delete_from_clause
|
18
|
+
from_keyword space? delete_target {
|
19
|
+
def ast
|
20
|
+
delete_target.ast
|
21
|
+
end
|
22
|
+
}
|
23
|
+
end
|
24
|
+
|
25
|
+
rule delete_target
|
26
|
+
t:(
|
27
|
+
table_reference /
|
28
|
+
delete_target_subquery
|
29
|
+
) space?
|
30
|
+
t_alias:t_alias? {
|
31
|
+
def ast
|
32
|
+
OracleSqlParser::Ast::DeleteTarget[
|
33
|
+
:name => t.ast,
|
34
|
+
:alias => t_alias.ast
|
35
|
+
]
|
36
|
+
end
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
rule delete_target_subquery
|
41
|
+
(table_keyword space)? '(' space? subquery space? ')' {
|
42
|
+
def ast
|
43
|
+
subquery.ast
|
44
|
+
end
|
45
|
+
}
|
46
|
+
end
|
47
|
+
|
48
|
+
rule delete_condition
|
49
|
+
where_keyword space?
|
50
|
+
where:(
|
51
|
+
search_condition /
|
52
|
+
current_of
|
53
|
+
) {
|
54
|
+
def ast
|
55
|
+
where.ast
|
56
|
+
end
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
rule current_of
|
61
|
+
current_of_keyword space? cursor_name {
|
62
|
+
def ast
|
63
|
+
cursor_name.ast
|
64
|
+
end
|
65
|
+
}
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|