oracle-sql-parser 0.8.1 → 0.9.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.
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,48 @@
1
+ module OracleSqlParser::Grammar::Select
2
+ grammar ForUpdate
3
+ rule for_update_clause
4
+ for_keyword space update_keyword
5
+ of:(space of_keyword space for_update_clause_columns)?
6
+ wait:(
7
+ space w:wait_keyword space time:integer /
8
+ space w:nowait_keyword
9
+ )? {
10
+ def ast
11
+ OracleSqlParser::Ast::ForUpdateClause[
12
+ :columns => of.try(:for_update_clause_columns).ast,
13
+ :wait => wait.try(:w).ast,
14
+ :time => wait.try(:time).ast
15
+ ].remove_nil_values!
16
+ end
17
+ }
18
+ end
19
+
20
+ rule for_update_clause_columns
21
+ for_update_clause_column more:(space? ',' space? for_update_clause_column)* {
22
+ def ast
23
+ OracleSqlParser::Ast::Array[
24
+ for_update_clause_column.ast, *more_for_update_clause_columns.map(&:ast)
25
+ ]
26
+ end
27
+
28
+ def more_for_update_clause_columns
29
+ more.elements.map(&:for_update_clause_column)
30
+ end
31
+ }
32
+ end
33
+
34
+ rule for_update_clause_column
35
+ (
36
+ schema_name space? '.' space? table_name space? '.' space? column_name /
37
+ table_name space? '.' space? column_name /
38
+ space? column_name
39
+ ) {
40
+ def ast
41
+ OracleSqlParser::Ast::Identifier[:name => text_value]
42
+ end
43
+ }
44
+ end
45
+
46
+
47
+ end
48
+ end
@@ -0,0 +1,62 @@
1
+ module OracleSqlParser::Grammar::Select
2
+ grammar Group
3
+ # group
4
+ rule group_by_clause
5
+ group_keyword space by_keyword space target:group_column space?
6
+ t:(',' space? more_target:group_column space?)*
7
+ h:(having_keyword space condition:condition)? {
8
+ def ast
9
+ OracleSqlParser::Ast::GroupByClause[
10
+ :targets => OracleSqlParser::Ast::Array[
11
+ target.ast, *more_targets.map(&:ast)
12
+ ],
13
+ :having => h.try(:condition).ast
14
+ ]
15
+ end
16
+
17
+ def more_targets
18
+ t.elements.map{|e| e.more_target}
19
+ end
20
+ }
21
+ end
22
+
23
+ rule group_column
24
+ (
25
+ expr /
26
+ rollup_cube_clause /
27
+ grouping_sets_clause
28
+ ) {
29
+ def ast
30
+ super
31
+ end
32
+ }
33
+ end
34
+
35
+ rule rollup_cube_clause
36
+ func_name:(rollup_keyword / cube_keyword) space? grouping_expression_list {
37
+ def ast
38
+ OracleSqlParser::Ast::RollupCubeClause[
39
+ :func_name => func_name.ast,
40
+ :args => grouping_expression_list.ast
41
+ ]
42
+ end
43
+ }
44
+ end
45
+
46
+ rule grouping_sets_clause
47
+ 'grouping_sets_clause' { # not implemented
48
+ def ast
49
+ 'grouping_sets_clause'
50
+ end
51
+ }
52
+ end
53
+
54
+ rule grouping_expression_list
55
+ expression_list {
56
+ def ast
57
+ super
58
+ end
59
+ }
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,165 @@
1
+ module OracleSqlParser::Grammar::Select
2
+ grammar Join
3
+ rule join_clause
4
+ table_reference space
5
+ join:(
6
+ outer_join_clause /
7
+ inner_cross_join_clause
8
+ ) {
9
+ def ast
10
+ result = join.ast
11
+ result.table1 = table_reference.ast
12
+ result
13
+ end
14
+ }
15
+ end
16
+
17
+ rule inner_cross_join_clause
18
+ inner_join_clause /
19
+ cross_join_clause {
20
+ def ast
21
+ super
22
+ end
23
+ }
24
+ end
25
+
26
+ rule inner_join_clause
27
+ inner_join space? table_reference space? on_or_using_clause {
28
+ def ast
29
+ OracleSqlParser::Ast::InnerJoinClause[
30
+ :inner => inner_join.inner_keyword.ast,
31
+ :join => inner_join.try(:join_keyword).ast,
32
+ :table2 => table_reference.ast,
33
+ :on_or_using_clause => on_or_using_clause.ast
34
+ ]
35
+ end
36
+ }
37
+ end
38
+
39
+ rule inner_join
40
+ inner_keyword space? join_keyword
41
+ end
42
+
43
+ rule on_or_using_clause
44
+ on_clause /
45
+ using_clause {
46
+ def ast
47
+ super
48
+ end
49
+ }
50
+ end
51
+
52
+ rule on_clause
53
+ on_keyword space? condition {
54
+ def ast
55
+ OracleSqlParser::Ast::OnClause[
56
+ :on => on_keyword.ast,
57
+ :condition => condition.ast
58
+ ]
59
+ end
60
+ }
61
+ end
62
+
63
+ rule using_clause
64
+ using_keyword space? '(' space? column_list space? ')' {
65
+ def ast
66
+ OracleSqlParser::Ast::UsingClause[
67
+ :using => using_keyword.ast,
68
+ :column_list => column_list.ast
69
+ ]
70
+ end
71
+ }
72
+ end
73
+
74
+ rule cross_join_clause
75
+ cross_natural_join space table_reference {
76
+ def ast
77
+ OracleSqlParser::Ast::CrossNaturalJoinClause[
78
+ :cross => cross_natural_join.cross_keyword.ast,
79
+ :natural => cross_natural_join.natural_keyword.ast,
80
+ :inner => cross_natural_join.inner_keyword.ast,
81
+ :join => cross_natural_join.try(:join_keyword).ast,
82
+ :table2 => table_reference.ast
83
+ ]
84
+ end
85
+ }
86
+ end
87
+
88
+ rule cross_natural_join
89
+ (
90
+ cross_keyword:cross_keyword /
91
+ natural_keyword:natural_keyword inner:(space inner_keyword)?
92
+ ) space join_keyword {
93
+
94
+ def cross_keyword
95
+ elements.first.try(:cross_keyword)
96
+ end
97
+
98
+ def natural_keyword
99
+ elements.first.try(:natural_keyword)
100
+ end
101
+
102
+ def inner_keyword
103
+ elements.first.try(:inner).try(:inner_keyword)
104
+ end
105
+ }
106
+ end
107
+
108
+ rule column_list
109
+ column_name more:(space? ',' space? column_name)* {
110
+ def ast
111
+ OracleSqlParser::Ast::Array[
112
+ column_name.ast,
113
+ *more_column_names.map(&:ast)
114
+ ]
115
+ end
116
+
117
+ def more_column_names
118
+ more.elements.map(&:column_name)
119
+ end
120
+ }
121
+ end
122
+
123
+ rule outer_join_clause
124
+ query_partition_clause?
125
+ join_type:(
126
+ natural_keyword:natural_keyword? space? outer_join_type:outer_join_type? space? join_keyword
127
+ ) space
128
+ table_reference space
129
+ (query_partition_clause space)?
130
+ on_or_using_clause {
131
+ def ast
132
+ OracleSqlParser::Ast::OuterJoinClause[
133
+ :natural => join_type.try(:natural_keyword).ast,
134
+ :join_type => join_type.outer_join_type.try(:type).ast,
135
+ :outer => join_type.outer_join_type.try(:outer).ast,
136
+ :join => join_type.join_keyword.ast,
137
+ :table2 => table_reference.ast,
138
+ :on_or_using_clause => on_or_using_clause.ast
139
+ ]
140
+ end
141
+ }
142
+ end
143
+
144
+ rule query_partition_clause
145
+ 'query_partition_clause' {
146
+ def ast
147
+ 'query_partition_clause' # do not supported
148
+ end
149
+ }
150
+ end
151
+
152
+ rule outer_join_type
153
+ type:(
154
+ full_keyword /
155
+ left_keyword /
156
+ right_keyword
157
+ ) o:(space outer_keyword)? {
158
+ def outer
159
+ o.try(:outer_keyword)
160
+ end
161
+ }
162
+ end
163
+
164
+ end
165
+ end
@@ -0,0 +1,43 @@
1
+ module OracleSqlParser::Grammar::Select
2
+ grammar Order
3
+ rule order_by_clause
4
+ order_keyword space siblings:siblings_keyword? space? by_keyword space order_by_clause_items {
5
+ def ast
6
+ OracleSqlParser::Ast::OrderByClause[
7
+ :siblings => siblings.ast,
8
+ :items => order_by_clause_items.ast
9
+ ]
10
+ end
11
+ }
12
+ end
13
+
14
+ rule order_by_clause_items
15
+ order_by_clause_item
16
+ more:(space? ',' space? order_by_clause_item space?)* {
17
+ def ast
18
+ OracleSqlParser::Ast::Array[
19
+ order_by_clause_item.ast, *more_order_by_clause_items.map(&:ast)
20
+ ]
21
+ end
22
+
23
+ def more_order_by_clause_items
24
+ more.elements.map(&:order_by_clause_item)
25
+ end
26
+ }
27
+ end
28
+
29
+ rule order_by_clause_item
30
+ target:(expr / position / c_alias) space?
31
+ asc:(asc_keyword / desc_keyword)? space?
32
+ nulls:(nulls_keyword space first_or_last:(first_keyword / last_keyword))? {
33
+ def ast
34
+ OracleSqlParser::Ast::OrderByClauseItem[
35
+ :target => target.ast,
36
+ :asc => asc.ast,
37
+ :nulls => nulls.try(:first_or_last).ast,
38
+ ]
39
+ end
40
+ }
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,116 @@
1
+ module OracleSqlParser::Grammar::Select
2
+ grammar QueryBlock
3
+ rule query_block
4
+ (subquery_factoring_clause:subquery_factoring_clause space)?
5
+ select_keyword space
6
+ hint:hint?
7
+ mod:(modifier:(all_keyword / distinct_keyword / unique_keyword) space)?
8
+ select_list space
9
+ from_keyword
10
+ select:(space select_sources)?
11
+ where:(space where_clause)?
12
+ group:(space group_by_clause)?
13
+ model:(space model_clause)? {
14
+ def ast
15
+ OracleSqlParser::Ast::QueryBlock[
16
+ :hint => hint.ast,
17
+ :modifier => mod.try(:modifier).ast,
18
+ :select_list => select_list.ast,
19
+ :select_sources => select.try(:select_sources).ast,
20
+ :where_clause => where.try(:where_clause).ast,
21
+ :group_by_clause => group.try(:group_by_clause).ast,
22
+ :model_clause => model.try(:model_clause).ast]
23
+ end
24
+ }
25
+ end
26
+
27
+ rule select_sources
28
+ select_source more:(space? ',' space? select_source)* {
29
+ def ast
30
+ OracleSqlParser::Ast::Array[
31
+ select_source.ast,
32
+ *more_select_sources.map(&:ast),
33
+ ]
34
+ end
35
+
36
+ def more_select_sources
37
+ more.elements.map(&:select_source)
38
+ end
39
+ }
40
+ end
41
+
42
+ rule select_source
43
+ join_clause /
44
+ '(' space? join_clause:join_clause space? ')' /
45
+ table_reference /
46
+ subquery
47
+ {
48
+ def ast
49
+ if respond_to? :join_clause
50
+ join_clause.ast
51
+ else
52
+ super
53
+ end
54
+ end
55
+ }
56
+ end
57
+
58
+ rule subquery_factoring_clause
59
+ 'subquery_factoring_clause' { # not implemented
60
+ def ast
61
+ 'subquery_factoring_clause'
62
+ end
63
+ }
64
+ end
65
+
66
+ rule select_list
67
+ select_one more_list:( space? ',' space? select_one)* {
68
+ def ast
69
+ OracleSqlParser::Ast::Array[select_one.ast, *more_columns.map(&:ast)]
70
+ end
71
+
72
+ def more_columns
73
+ more_list.elements.map(&:select_one)
74
+ end
75
+ }
76
+ end
77
+
78
+ rule select_one
79
+ select_table /
80
+ select_column
81
+ {
82
+ def ast
83
+ super
84
+ end
85
+ }
86
+ end
87
+
88
+ rule select_table
89
+ (table_name '.')? '*' {
90
+ def ast
91
+ OracleSqlParser::Ast::Identifier[:name => text_value]
92
+ end
93
+ }
94
+ end
95
+
96
+ rule select_column
97
+ expr _alias:( space as:( as_keyword space )? c_alias )? {
98
+ def ast
99
+ OracleSqlParser::Ast::SelectColumn[
100
+ :expr => expr.ast,
101
+ :as => self.try(:_alias).try(:as).try(:as_keyword).ast,
102
+ :c_alias => _alias.try(:c_alias).ast,
103
+ ]
104
+ end
105
+ }
106
+ end
107
+
108
+ rule model_clause
109
+ 'model_clause' {
110
+ def ast
111
+ 'model_clause'
112
+ end
113
+ }
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,48 @@
1
+ module OracleSqlParser::Grammar::Select
2
+ grammar Union
3
+ rule subqueries_with_union
4
+ query_block space? more:(union_or_intersect_or_minus space? subquery)+
5
+ {
6
+ def ast
7
+ OracleSqlParser::Ast::Array[
8
+ query_block.ast,
9
+ *more_queries.map(&:ast),
10
+ ]
11
+ end
12
+
13
+ def more_queries
14
+ more.elements.map{|e| [e.union_or_intersect_or_minus, e.subquery]}.flatten
15
+ end
16
+ }
17
+ end
18
+
19
+ rule union_or_intersect_or_minus
20
+ (
21
+ union:union_all /
22
+ intersect:intersect_keyword /
23
+ minus:minus_keyword
24
+ ) {
25
+ def ast
26
+ if respond_to? :union
27
+ union.ast
28
+ elsif respond_to? :intersect
29
+ OracleSqlParser::Ast::Array[intersect.ast]
30
+ elsif respond_to? :minus
31
+ OracleSqlParser::Ast::Array[minus.ast]
32
+ end
33
+ end
34
+ }
35
+ end
36
+
37
+ rule union_all
38
+ union_keyword all:(space all_keyword)? {
39
+ def ast
40
+ OracleSqlParser::Ast::Array[
41
+ union_keyword.ast,
42
+ all.try(:all_keyword).ast
43
+ ]
44
+ end
45
+ }
46
+ end
47
+ end
48
+ end