oracle-sql-parser 0.8.1 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/HISTORY.md +6 -0
  4. data/Rakefile +3 -11
  5. data/lib/oracle-sql-parser/ast.rb +5 -1
  6. data/lib/oracle-sql-parser/ast/cursor_expression.rb +13 -0
  7. data/lib/oracle-sql-parser/ast/datetime_expression.rb +8 -0
  8. data/lib/oracle-sql-parser/ast/interval_expression.rb +27 -0
  9. data/lib/oracle-sql-parser/ast/{simple_comparision_condition.rb → simple_comparison_condition.rb} +1 -1
  10. data/lib/oracle-sql-parser/ast/timezone_clause.rb +8 -0
  11. data/lib/oracle-sql-parser/grammar.rb +24 -0
  12. data/lib/oracle-sql-parser/grammar/condition.treetop +12 -289
  13. data/lib/oracle-sql-parser/grammar/condition/between.treetop +20 -0
  14. data/lib/oracle-sql-parser/grammar/condition/comparison.treetop +32 -0
  15. data/lib/oracle-sql-parser/grammar/condition/compound.treetop +11 -0
  16. data/lib/oracle-sql-parser/grammar/condition/exists.treetop +13 -0
  17. data/lib/oracle-sql-parser/grammar/condition/floating_point.treetop +20 -0
  18. data/lib/oracle-sql-parser/grammar/condition/in.treetop +19 -0
  19. data/lib/oracle-sql-parser/grammar/condition/is_of_type.treetop +56 -0
  20. data/lib/oracle-sql-parser/grammar/condition/multiset.treetop +85 -0
  21. data/lib/oracle-sql-parser/grammar/condition/null.treetop +17 -0
  22. data/lib/oracle-sql-parser/grammar/condition/pattern_matching.treetop +47 -0
  23. data/lib/oracle-sql-parser/grammar/delete.treetop +2 -10
  24. data/lib/oracle-sql-parser/grammar/expression.treetop +39 -214
  25. data/lib/oracle-sql-parser/grammar/expression/case.treetop +77 -0
  26. data/lib/oracle-sql-parser/grammar/expression/compound.treetop +22 -0
  27. data/lib/oracle-sql-parser/grammar/expression/cursor.treetop +15 -0
  28. data/lib/oracle-sql-parser/grammar/expression/datetime.treetop +36 -0
  29. data/lib/oracle-sql-parser/grammar/expression/function.treetop +47 -0
  30. data/lib/oracle-sql-parser/grammar/expression/interval.treetop +47 -0
  31. data/lib/oracle-sql-parser/grammar/expression/simple.treetop +29 -0
  32. data/lib/oracle-sql-parser/grammar/grammar.treetop +1 -1
  33. data/lib/oracle-sql-parser/grammar/reserved_word_generator.rb +14 -2
  34. data/lib/oracle-sql-parser/grammar/select.treetop +11 -513
  35. data/lib/oracle-sql-parser/grammar/select/for_update.treetop +48 -0
  36. data/lib/oracle-sql-parser/grammar/select/group.treetop +62 -0
  37. data/lib/oracle-sql-parser/grammar/select/join.treetop +165 -0
  38. data/lib/oracle-sql-parser/grammar/select/order.treetop +43 -0
  39. data/lib/oracle-sql-parser/grammar/select/query_block.treetop +116 -0
  40. data/lib/oracle-sql-parser/grammar/select/union.treetop +48 -0
  41. data/lib/oracle-sql-parser/grammar/update.treetop +1 -1
  42. data/lib/oracle-sql-parser/version.rb +1 -1
  43. data/oracle-sql-parser.gemspec +1 -0
  44. metadata +55 -3
@@ -0,0 +1,20 @@
1
+ module OracleSqlParser::Grammar::Condition
2
+ grammar Between
3
+ rule between_condition
4
+ target:expr space n:(not_keyword space)? between_keyword space from:expr space and_keyword space to:expr {
5
+ def ast
6
+ OracleSqlParser::Ast::BetweenCondition[
7
+ :target => target.ast,
8
+ :not => not_keyword.ast,
9
+ :from => from.ast,
10
+ :to => to.ast
11
+ ]
12
+ end
13
+
14
+ def not_keyword
15
+ n.elements && n.elements.first
16
+ end
17
+ }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ module OracleSqlParser::Grammar::Condition
2
+ grammar Comparison
3
+ rule comparison_condition
4
+ (
5
+ simple_comparison_condition /
6
+ group_comparison_condition
7
+ ) {
8
+ def ast
9
+ super
10
+ end
11
+ }
12
+ end
13
+
14
+ rule group_comparison_condition
15
+ 'group_comparison_condition'
16
+ end
17
+
18
+ rule simple_comparison_condition
19
+ (
20
+ left:expr space? op:('!=' / '^=' / '<>' / '>=' / '<=' / '=' / '>' / '<') space? right:expr /
21
+ '(' space? left:exprs space? ')' op:space? ('!=' / '^=' / '<>' / '=') space? '(' space? right:subquery space? ')'
22
+ ) {
23
+ def ast
24
+ OracleSqlParser::Ast::SimpleComparisonCondition[
25
+ :left => left.ast,
26
+ :op => op.text_value,
27
+ :right => right.ast]
28
+ end
29
+ }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ module OracleSqlParser::Grammar::Condition
2
+ grammar Compound
3
+ rule compound_condition
4
+ '(' logical_condition ')' {
5
+ def ast
6
+ OracleSqlParser::Ast::CompoundCondition[:condition => logical_condition.ast]
7
+ end
8
+ }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module OracleSqlParser::Grammar::Condition
2
+ grammar Exists
3
+ rule exists_condition
4
+ exists_keyword space? '(' space? subquery space? ')' {
5
+ def ast
6
+ OracleSqlParser::Ast::ExistsCondition[
7
+ :target => subquery.ast
8
+ ]
9
+ end
10
+ }
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ module OracleSqlParser::Grammar::Condition
2
+ grammar FloatingPoint
3
+ rule floating_point_condition
4
+ expr space? is_keyword space? not_keyword:not_keyword? space?
5
+ value:(
6
+ nan_keyword /
7
+ infinite_keyword
8
+ ) {
9
+ def ast
10
+ OracleSqlParser::Ast::FloatingPointCondition[
11
+ :target => expr.ast,
12
+ :is => is_keyword.ast,
13
+ :not => not_keyword.ast,
14
+ :value => value.ast
15
+ ]
16
+ end
17
+ }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ module OracleSqlParser::Grammar::Condition
2
+ grammar In
3
+ rule in_condition
4
+ target:expr space n:(not_keyword space)? in_keyword space? '(' space? values:( exprs / subquery ) space? ')' {
5
+ def ast
6
+ OracleSqlParser::Ast::InCondition[
7
+ :target => target.ast,
8
+ :not => not_keyword.ast,
9
+ :values => values.ast
10
+ ]
11
+ end
12
+
13
+ def not_keyword
14
+ n.elements && n.elements.first
15
+ end
16
+ }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,56 @@
1
+ module OracleSqlParser::Grammar::Condition
2
+ grammar IsOfType
3
+ rule is_of_type_condition
4
+ expr space?
5
+ is_keyword space?
6
+ not_keyword:not_keyword? space?
7
+ of_keyword space?
8
+ type_keyword:type_keyword? space?
9
+ '(' space? types space? ')' {
10
+ def ast
11
+ OracleSqlParser::Ast::IsOfTypeCondition[
12
+ :target => expr.ast,
13
+ :is => is_keyword.ast,
14
+ :not => not_keyword.ast,
15
+ :of => of_keyword.ast,
16
+ :type => type_keyword.ast,
17
+ :types => types.ast
18
+ ]
19
+ end
20
+ }
21
+ end
22
+
23
+ rule types
24
+ only_and_type space? more:(',' space? only_and_type space?)* {
25
+ def ast
26
+ OracleSqlParser::Ast::Array[only_and_type.ast, *more_only_and_types.map(&:ast)]
27
+ end
28
+
29
+ def more_only_and_types
30
+ more.elements.map(&:only_and_type)
31
+ end
32
+ }
33
+ end
34
+
35
+ rule only_and_type
36
+ only_keyword:only_keyword? space? type {
37
+ def ast
38
+ OracleSqlParser::Ast::OnlyAndType[
39
+ :only => only_keyword.ast,
40
+ :type => type.ast
41
+ ]
42
+ end
43
+ }
44
+ end
45
+
46
+ rule type
47
+ schema_name '.' ident /
48
+ ident {
49
+ def ast
50
+ OracleSqlParser::Ast::Identifier[:name => text_value]
51
+ end
52
+ }
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,85 @@
1
+ module OracleSqlParser::Grammar::Condition
2
+ grammar Multiset
3
+ rule mutiset_condition
4
+ is_a_set_condition /
5
+ is_empty_condition /
6
+ member_condition /
7
+ submultiset_condition {
8
+ def ast
9
+ super
10
+ end
11
+ }
12
+ end
13
+
14
+ rule is_a_set_condition
15
+ nested_table space? is_keyword space? not_keyword:not_keyword? space? a:[Aa] space? set_keyword {
16
+ def ast
17
+ OracleSqlParser::Ast::IsASetCondition[
18
+ :target => nested_table.ast,
19
+ :is => is_keyword.ast,
20
+ :not => not_keyword.ast,
21
+ :a => OracleSqlParser::Ast::Keyword[:name => a.text_value],
22
+ :set => set_keyword.ast
23
+ ]
24
+ end
25
+ }
26
+ end
27
+
28
+ rule is_empty_condition
29
+ nested_table space? is_keyword space? not_keyword:not_keyword? space? empty_keyword {
30
+ def ast
31
+ OracleSqlParser::Ast::IsEmptyCondition[
32
+ :target => nested_table.ast,
33
+ :is => is_keyword.ast,
34
+ :not => not_keyword.ast,
35
+ :empty => empty_keyword.ast
36
+ ]
37
+ end
38
+ }
39
+ end
40
+
41
+ rule member_condition
42
+ expr space? not_keyword:not_keyword? space? member_keyword space? of_keyword space? nested_table {
43
+ def ast
44
+ OracleSqlParser::Ast::MemberCondition[
45
+ :target => expr.ast,
46
+ :not => not_keyword.ast,
47
+ :member => member_keyword.ast,
48
+ :of => of_keyword.ast,
49
+ :table => nested_table.ast
50
+ ]
51
+ end
52
+ }
53
+ end
54
+
55
+ rule submultiset_condition
56
+ table1:nested_table space?
57
+ not_keyword:not_keyword? space?
58
+ submultiset_keyword space?
59
+ of_keyword:of_keyword? space?
60
+ table2:nested_table {
61
+ def ast
62
+ OracleSqlParser::Ast::SubmultisetCondition[
63
+ :target => table1.ast,
64
+ :not => not_keyword.ast,
65
+ :submultiset => submultiset_keyword.ast,
66
+ :of => of_keyword.ast,
67
+ :table => table2.ast
68
+ ]
69
+ end
70
+ }
71
+ end
72
+
73
+ rule nested_table
74
+ ident {
75
+ def ast
76
+ super
77
+ end
78
+ }
79
+ end
80
+
81
+
82
+
83
+ end
84
+ end
85
+
@@ -0,0 +1,17 @@
1
+ module OracleSqlParser::Grammar::Condition
2
+ grammar Null
3
+ rule null_condition
4
+ expr space is_keyword space n:(not_keyword space)? null_keyword {
5
+ def ast
6
+ OracleSqlParser::Ast::NullCondition[
7
+ :target => expr.ast,
8
+ :not => not_keyword.ast]
9
+ end
10
+
11
+ def not_keyword
12
+ n.elements && n.elements.first
13
+ end
14
+ }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,47 @@
1
+ module OracleSqlParser::Grammar::Condition
2
+ grammar PatternMatching
3
+ rule pattern_maching_condition
4
+ (
5
+ like_condition /
6
+ regexp_like_condition
7
+ ) {
8
+ def ast
9
+ super
10
+ end
11
+ }
12
+ end
13
+
14
+ rule like_condition
15
+ target:ident space
16
+ n:(not_keyword:not_keyword space)?
17
+ like:(like_keyword / like2_keyword / like4_keyword / likec_keyword) space
18
+ text:text_literal
19
+ e:(space escape_keyword space escape_text:text_literal)? {
20
+ def ast
21
+ OracleSqlParser::Ast::LikeCondition[
22
+ :target => target.ast,
23
+ :not => not_keyword.ast,
24
+ :like => like.ast,
25
+ :text => text.ast,
26
+ :escape => e.try(:escape_text).ast
27
+ ]
28
+ end
29
+
30
+ def not_keyword
31
+ n.elements && n.elements.first
32
+ end
33
+ }
34
+ end
35
+
36
+ rule regexp_like_condition
37
+ regexp_like_keyword '(' space? target:ident space? ',' space? regexp:text_literal space? ')' {
38
+ def ast
39
+ OracleSqlParser::Ast::RegexpCondition[
40
+ :target => target.ast,
41
+ :regexp => regexp.ast
42
+ ]
43
+ end
44
+ }
45
+ end
46
+ end
47
+ end
@@ -7,13 +7,9 @@ module OracleSqlParser::Grammar
7
7
  def ast
8
8
  OracleSqlParser::Ast::DeleteStatement[
9
9
  :target => delete_from_clause.ast,
10
- :where_clause => delete_condition.ast
10
+ :where_clause => condition.try(:delete_condition).ast
11
11
  ]
12
12
  end
13
-
14
- def delete_condition
15
- condition.delete_condition if condition.respond_to? :delete_condition
16
- end
17
13
  }
18
14
  end
19
15
 
@@ -38,14 +34,10 @@ module OracleSqlParser::Grammar
38
34
  else
39
35
  OracleSqlParser::Ast::DeleteTarget[
40
36
  :name => t.ast,
41
- :alias => t_alias.ast
37
+ :alias => a.try(:t_alias).ast,
42
38
  ]
43
39
  end
44
40
  end
45
-
46
- def t_alias
47
- a.t_alias if a.respond_to? :t_alias
48
- end
49
41
  }
50
42
  end
51
43
 
@@ -1,64 +1,42 @@
1
1
  module OracleSqlParser::Grammar
2
2
  grammar Expression
3
+ include OracleSqlParser::Grammar::Expression::Compound
4
+ include OracleSqlParser::Grammar::Expression::Function
5
+ include OracleSqlParser::Grammar::Expression::Case
6
+ include OracleSqlParser::Grammar::Expression::Simple
7
+ include OracleSqlParser::Grammar::Expression::Cursor
8
+ include OracleSqlParser::Grammar::Expression::Datetime
9
+ include OracleSqlParser::Grammar::Expression::Interval
10
+
3
11
  rule sql_expression
4
12
  ex:(
13
+ interval_expression /
5
14
  compound_expression /
6
15
  function_expression /
7
16
  case_expression /
8
17
  cursor_expression /
9
18
  datetime_expression /
10
- interval_expression /
11
19
  object_access_expression /
12
20
  scalar_subquery_expression /
13
21
  model_expression /
14
22
  type_constructor_expression /
15
23
  simple_expression /
16
- variable_expression
24
+ variable_expression /
25
+ column_expression /
26
+ json_object_access_expression /
27
+ object_access_expression
17
28
  ) {
18
29
  def ast
19
30
  ex.ast
20
- # super
21
- # tree = ex.ast
22
- ## tree[:as] = alias_name.try(:as_keyword).try(:ast)
23
- # tree[:alias] = alias_name.try(:ident).try(:ast)
24
- # tree
25
- end
26
- }
27
- end
28
-
29
- rule simple_expression
30
- rownum_keyword /
31
- sysdate_keyword /
32
- systimestamp_keyword /
33
- null_keyword /
34
- text_literal /
35
- number_literal /
36
- sequence /
37
- simple_expression_column {
38
- def ast
39
- super
40
- end
41
- }
42
- end
43
-
44
- rule simple_expression_column
45
- name:(
46
- ( schema_name space? '.' space? table_name '.' / table_name space? '.' space? )?
47
- ( rowid_keyword / column_name )
48
- ) {
49
- def ast
50
- OracleSqlParser::Ast::Identifier.new(:name => text_value)
51
31
  end
52
32
  }
53
33
  end
54
34
 
55
- rule not_compound_expressions
35
+ rule not_loop_sql_expression
56
36
  ex:(
57
37
  function_expression /
58
38
  case_expression /
59
39
  cursor_expression /
60
- datetime_expression /
61
- interval_expression /
62
40
  object_access_expression /
63
41
  scalar_subquery_expression /
64
42
  model_expression /
@@ -72,197 +50,45 @@ module OracleSqlParser::Grammar
72
50
  }
73
51
  end
74
52
 
75
- rule compound_expression
76
- ex:(
77
- left_parenthesis:'(' space? left:expr space? right_parenthesis:')' /
78
- left:not_compound_expressions space? op:('*' / '/' / '+' / '-' / '||') space? right:expr /
79
- !number_literal op:(prior_keyword / '-' / '+') space? right:expr
80
- )
81
-
82
- {
83
- def ast
84
- OracleSqlParser::Ast::CompoundExpression[
85
- :has_parenthesis => ex.respond_to?(:left_parenthesis) ? true : nil,
86
- :left => left.ast,
87
- :op => op.ast,
88
- :right => right.ast,
89
- ]
90
- end
91
-
92
- def left
93
- ex.left if ex.respond_to? :left
94
- end
95
-
96
- def not_compound_expressions
97
- ex.not_compound_expressions if ex.respond_to? :not_compound_expressions
98
- end
99
-
100
- def op
101
- ex.op if ex.respond_to? :op
102
- end
103
-
104
- def right
105
- ex.right if ex.respond_to? :right
106
- end
107
-
108
- def right_parenthesis
109
- ex.right_parenthesis if ex.respond_to? :right_parenthesis
110
- end
111
- }
112
- end
113
-
114
- rule case_expression
115
- case_keyword space? case_expression:(simple_case_expression / searched_case_expression) space?
116
- else_clause:else_clause? space? end_keyword {
117
- def ast
118
- ast = case_expression.ast
119
- ast.else_clause = else_clause.ast
120
- ast
121
- end
122
- }
123
- end
124
-
125
- rule simple_case_expression
126
- expr space w:(when_keyword space comparison_expr space then_keyword space? return_expr space?)+ {
127
- def ast
128
- OracleSqlParser::Ast::SimpleCaseExpression[
129
- :condition => expr.ast,
130
- :when_clauses => OracleSqlParser::Ast::Array[*when_clauses]
131
- ]
132
- end
133
-
134
- def when_clauses
135
- w.elements.map do |elem|
136
- OracleSqlParser::Ast::Hash[
137
- :when_expr => elem.comparison_expr.ast,
138
- :return_expr => elem.return_expr.ast
139
- ]
140
- end
141
- end
142
- }
143
- end
144
-
145
- rule comparison_expr
146
- expr {
147
- def ast
148
- super
149
- end
150
- }
53
+ rule object_access_expression
54
+ 'object_access_expression' # not implemented
151
55
  end
152
56
 
153
- rule return_expr
154
- expr {
155
- def ast
156
- super
157
- end
158
- }
57
+ rule scalar_subquery_expression
58
+ 'scalar_subquery_expression' # not implemented
159
59
  end
160
60
 
161
- rule else_expr
162
- expr {
163
- def ast
164
- super
165
- end
166
- }
61
+ rule model_expression
62
+ 'model_expression' # not implemented
167
63
  end
168
64
 
169
- rule searched_case_expression
170
- when_keyword space? condition space? then_keyword space? return_expr {
171
- def ast
172
- OracleSqlParser::Ast::SearchedCaseExpression[
173
- :when_condition => condition.ast,
174
- :return_expr => return_expr.ast
175
- ]
176
- end
177
- }
65
+ rule type_constructor_expression
66
+ 'type_constructor_expression' # not implemented
178
67
  end
179
68
 
180
- rule else_clause
181
- else_keyword space? else_expr {
182
- def ast
183
- else_expr.ast
184
- end
185
- }
69
+ rule variable_expression
70
+ 'variable_expression' # not implemented
186
71
  end
187
72
 
188
- rule cursor_expression
189
- 'cursor_expression' # not implemented
73
+ rule column_expression
74
+ 'column_expression' # not implemented
190
75
  end
191
76
 
192
- rule datetime_expression
193
- 'datetime_expression' # not implemented
77
+ rule json_object_access_expression
78
+ 'json_object_access_expression' # not implemented
194
79
  end
195
80
 
196
- rule function_expression
197
- function_name space? '(' space? function_args:function_args? space? ')' {
198
- def ast
199
- OracleSqlParser::Ast::FunctionExpression[
200
- :name => function_name.ast,
201
- :args => function_args.ast
202
- ]
203
- end
204
- }
81
+ rule object_access_expression
82
+ 'object_access_expression' # not implemented
205
83
  end
206
84
 
207
- rule function_name
85
+ rule expression_list # from grouping_expression_list
208
86
  (
209
- package_name '.' procedure_name /
210
- procedure_name
87
+ '(' e:exprs ')' /
88
+ e:exprs
211
89
  ) {
212
90
  def ast
213
- OracleSqlParser::Ast::Identifier[:name => text_value]
214
- end
215
- }
216
- end
217
-
218
- rule function_args
219
- function_arg more:(space? ',' space? function_arg)* {
220
- def ast
221
- OracleSqlParser::Ast::Array[function_arg.ast, *more_function_args.map(&:ast)]
222
- end
223
-
224
- def more_function_args
225
- more.elements.map(&:function_arg)
226
- end
227
- }
228
- end
229
-
230
- rule function_arg
231
- expr {
232
- def ast
233
- super
234
- end
235
- }
236
- end
237
-
238
- rule interval_expression
239
- 'interval_expression' # not implemented
240
- end
241
-
242
- rule object_access_expression
243
- 'object_access_expression'
244
- end
245
-
246
- rule scalar_subquery_expression
247
- 'scalar_subquery_expression'
248
- end
249
-
250
- rule model_expression
251
- 'model_expression' #
252
- end
253
-
254
- rule type_constructor_expression
255
- 'type_constructor_expression'
256
- end
257
-
258
- rule variable_expression
259
- 'variable_expression'
260
- end
261
-
262
- rule expr
263
- sql_expression {
264
- def ast
265
- super
91
+ e.ast
266
92
  end
267
93
  }
268
94
  end
@@ -279,15 +105,14 @@ module OracleSqlParser::Grammar
279
105
  }
280
106
  end
281
107
 
282
- rule expression_list
283
- (
284
- '(' e:exprs ')' /
285
- e:exprs
286
- ) {
108
+ rule expr
109
+ sql_expression {
287
110
  def ast
288
- e.ast
111
+ super
289
112
  end
290
113
  }
291
114
  end
115
+
116
+
292
117
  end
293
118
  end