oracle-sql-parser 0.1.1 → 0.2.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/README.md +9 -3
  3. data/lib/oracle-sql-parser/ast.rb +4 -0
  4. data/lib/oracle-sql-parser/ast/array.rb +12 -4
  5. data/lib/oracle-sql-parser/ast/base.rb +24 -4
  6. data/lib/oracle-sql-parser/ast/between_condition.rb +10 -0
  7. data/lib/oracle-sql-parser/ast/compound_condition.rb +7 -0
  8. data/lib/oracle-sql-parser/ast/current_of.rb +3 -0
  9. data/lib/oracle-sql-parser/ast/delete_statement.rb +8 -1
  10. data/lib/oracle-sql-parser/ast/delete_target.rb +11 -1
  11. data/lib/oracle-sql-parser/ast/exists_condition.rb +3 -0
  12. data/lib/oracle-sql-parser/ast/for_update_clause.rb +7 -0
  13. data/lib/oracle-sql-parser/ast/function_expression.rb +10 -0
  14. data/lib/oracle-sql-parser/ast/group_by_clause.rb +7 -0
  15. data/lib/oracle-sql-parser/ast/hash.rb +2 -2
  16. data/lib/oracle-sql-parser/ast/identifier.rb +1 -1
  17. data/lib/oracle-sql-parser/ast/in_condition.rb +8 -0
  18. data/lib/oracle-sql-parser/ast/insert_statement.rb +9 -1
  19. data/lib/oracle-sql-parser/ast/insert_values_clause.rb +7 -0
  20. data/lib/oracle-sql-parser/ast/like_condition.rb +13 -0
  21. data/lib/oracle-sql-parser/ast/null_condition.rb +8 -0
  22. data/lib/oracle-sql-parser/ast/order_by_clause.rb +8 -0
  23. data/lib/oracle-sql-parser/ast/order_by_clause_item.rb +5 -0
  24. data/lib/oracle-sql-parser/ast/query_block.rb +1 -1
  25. data/lib/oracle-sql-parser/ast/regexp_condition.rb +3 -0
  26. data/lib/oracle-sql-parser/ast/rollup_cube_clause.rb +3 -0
  27. data/lib/oracle-sql-parser/ast/searched_case_expression.rb +13 -1
  28. data/lib/oracle-sql-parser/ast/select_statement.rb +5 -1
  29. data/lib/oracle-sql-parser/ast/simple_case_expression.rb +13 -0
  30. data/lib/oracle-sql-parser/ast/simple_comparision_condition.rb +3 -0
  31. data/lib/oracle-sql-parser/ast/table_reference.rb +16 -0
  32. data/lib/oracle-sql-parser/ast/text_literal.rb +4 -0
  33. data/lib/oracle-sql-parser/ast/update_set_clause.rb +7 -0
  34. data/lib/oracle-sql-parser/ast/update_set_column.rb +3 -0
  35. data/lib/oracle-sql-parser/ast/update_statement.rb +8 -0
  36. data/lib/oracle-sql-parser/ast/where_clause.rb +3 -0
  37. data/lib/oracle-sql-parser/grammar/condition.treetop +1 -1
  38. data/lib/oracle-sql-parser/grammar/delete.treetop +20 -12
  39. data/lib/oracle-sql-parser/grammar/grammar.treetop +20 -10
  40. data/lib/oracle-sql-parser/grammar/insert.treetop +10 -8
  41. data/lib/oracle-sql-parser/grammar/select.treetop +8 -20
  42. data/lib/oracle-sql-parser/grammar/update.treetop +18 -14
  43. data/lib/oracle-sql-parser/version.rb +1 -1
  44. metadata +6 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 82f6a4205d237428121e76ccf0673b91fa2593eb
4
- data.tar.gz: c171a4f9d250597bb80c751fbe4d34a3b876d06e
3
+ metadata.gz: c837374a732fc2668270aeda336b61a01370e4bb
4
+ data.tar.gz: 7b3a68ffe18ef9433fdaed74261474e555d944a2
5
5
  SHA512:
6
- metadata.gz: f893677527f57c266f24dac86b181eb25dbde97706e1dc9af5adc3f3a39184dbbd1ba9c6466878a6e643c5d43638d27ad2068a6ab676851306ed4781a65bae86
7
- data.tar.gz: cfac1b47be19e92e10072fab6386e8edcd5494e61d201d3889aa9c3d27feb39de05e7158e0dbd2d33aa40d4331bb07df4b50e40de70866b0d6af880e9891506d
6
+ metadata.gz: 06d395ede2b2bd56ab772adb3fd648371a8725de851475afd7de5547c229feb8532bd732a9320d041b39ea3a20d104494829ac9142c4f8664c913d56525eec99
7
+ data.tar.gz: e2e5bf837cbf9a41066becb3ba9f3bb4b098005114d342a2b314b50021defea7a9999aeeae60ea05d9f31b90f4dd9a824eeb517f7e50bb0625d188eaa781d521
data/README.md CHANGED
@@ -24,15 +24,14 @@ Or install it yourself as:
24
24
  query = "select 1 from dual"
25
25
  parser = OracleSqlParser::Grammar::GrammarParser.new
26
26
  syntax_tree = parser.parse query
27
- if syntax_tree
27
+ if syntax_tree.nil?
28
28
  message = "\n#{query}\n" + " " * (parser.failure_column.to_i-1) + "*\n"
29
29
  raise parser.failure_reason + message
30
30
  end
31
31
  ast = syntax_tree.ast
32
32
  ```
33
33
  <pre>
34
- irb(main):008:0> ast
35
- => #&lt;OracleSqlParser::Ast::SelectStatement
34
+ =&gt; #&lt;OracleSqlParser::Ast::SelectStatement
36
35
  :subquery =&gt; #&lt;OracleSqlParser::Ast::Subquery
37
36
  :query_block =&gt; #&lt;OracleSqlParser::Ast::QueryBlock
38
37
  :hint =&gt; nil,
@@ -51,6 +50,13 @@ irb(main):008:0> ast
51
50
  :for_update_clause =&gt; nil}>
52
51
  </pre>
53
52
 
53
+ ```ruby
54
+ ast.to_sql
55
+ ```
56
+
57
+ <pre>
58
+ =&gt; "select 1 from dual"
59
+ </pre>
54
60
 
55
61
  ## Contributing
56
62
 
@@ -25,6 +25,7 @@ require 'oracle-sql-parser/ast/order_by_clause.rb'
25
25
  require 'oracle-sql-parser/ast/order_by_clause_item.rb'
26
26
  require 'oracle-sql-parser/ast/update_statement.rb'
27
27
  require 'oracle-sql-parser/ast/update_set_column.rb'
28
+ require 'oracle-sql-parser/ast/update_set_clause.rb'
28
29
  require 'oracle-sql-parser/ast/simple_case_expression.rb'
29
30
  require 'oracle-sql-parser/ast/searched_case_expression.rb'
30
31
  require 'oracle-sql-parser/ast/function_expression.rb'
@@ -32,7 +33,10 @@ require 'oracle-sql-parser/ast/current_of.rb'
32
33
  require 'oracle-sql-parser/ast/delete_statement.rb'
33
34
  require 'oracle-sql-parser/ast/delete_target.rb'
34
35
  require 'oracle-sql-parser/ast/insert_statement.rb'
36
+ require 'oracle-sql-parser/ast/insert_values_clause.rb'
35
37
  require 'oracle-sql-parser/ast/identifier.rb'
36
38
  require 'oracle-sql-parser/ast/text_literal.rb'
37
39
  require 'oracle-sql-parser/ast/number_literal.rb'
40
+ require 'oracle-sql-parser/ast/table_reference.rb'
41
+ require 'oracle-sql-parser/ast/compound_condition.rb'
38
42
  require 'oracle-sql-parser/ast/keyword.rb'
@@ -2,8 +2,16 @@ require 'forwardable'
2
2
 
3
3
  module OracleSqlParser::Ast
4
4
  class Array < Base
5
- extend Forwardable
6
- def_delegator :@ast, :each, :[]
5
+ include Enumerable
6
+
7
+ def each(&block)
8
+ @ast.each(&block)
9
+ end
10
+
11
+ def [](index)
12
+ @ast[index]
13
+ end
14
+
7
15
  def self.[](*values)
8
16
  self.new(*values)
9
17
  end
@@ -12,14 +20,14 @@ module OracleSqlParser::Ast
12
20
  @ast = args
13
21
  end
14
22
 
15
- def to_sql
23
+ def to_sql(options = {:separator => ' '})
16
24
  @ast.map do |v|
17
25
  if v.respond_to? :to_sql
18
26
  v.to_sql
19
27
  else
20
28
  v.to_s
21
29
  end
22
- end.compact.join(" ")
30
+ end.compact.join(options[:separator])
23
31
  end
24
32
 
25
33
  def remove_nil_values!
@@ -2,12 +2,32 @@ def nil.ast
2
2
  nil
3
3
  end
4
4
 
5
- def nil.to_sql
5
+ def nil.to_sql(options = {})
6
6
  nil
7
7
  end
8
8
 
9
+ def nil.until_nil(&block)
10
+
11
+ end
12
+
13
+ unless Object.respond_to? :try
14
+ class Object
15
+ def try(name, *args)
16
+ if respond_to? name
17
+ send(name, *args)
18
+ else
19
+ nil
20
+ end
21
+ end
22
+
23
+ def until_nil(&block)
24
+ block.call(self)
25
+ end
26
+ end
27
+ end
28
+
9
29
  class String
10
- def to_sql
30
+ def to_sql(options ={})
11
31
  self
12
32
  end
13
33
  end
@@ -35,9 +55,9 @@ module OracleSqlParser::Ast
35
55
  raise "do not call ast method"
36
56
  end
37
57
 
38
- def to_sql
58
+ def to_sql(options ={})
39
59
  if @ast.respond_to? :to_sql
40
- @ast.to_sql
60
+ @ast.to_sql(options)
41
61
  else
42
62
  @ast.to_s
43
63
  end
@@ -1,4 +1,14 @@
1
1
  module OracleSqlParser::Ast
2
2
  class BetweenCondition < Hash
3
+ def to_sql(options = {})
4
+ [
5
+ @ast[:target],
6
+ @ast[:not],
7
+ "between",
8
+ @ast[:from],
9
+ "and",
10
+ @ast[:to]
11
+ ].map(&:to_sql).compact.join(' ')
12
+ end
3
13
  end
4
14
  end
@@ -0,0 +1,7 @@
1
+ module OracleSqlParser::Ast
2
+ class CompoundCondition < Base
3
+ def to_sql(options = {})
4
+ "(#{@ast.to_sql})"
5
+ end
6
+ end
7
+ end
@@ -1,4 +1,7 @@
1
1
  module OracleSqlParser::Ast
2
2
  class CurrentOf < Hash
3
+ def to_sql
4
+ "current_of #{@ast[:cursor].to_sql}"
5
+ end
3
6
  end
4
7
  end
@@ -1,5 +1,12 @@
1
1
  module OracleSqlParser::Ast
2
2
  class DeleteStatement < Hash
3
-
3
+ def to_sql
4
+ sql = []
5
+ sql << "delete"
6
+ sql << "from"
7
+ sql << @ast[:target]
8
+ sql << @ast[:where_clause] if @ast[:where_clause]
9
+ sql.map(&:to_sql).join(' ')
10
+ end
4
11
  end
5
12
  end
@@ -1,6 +1,16 @@
1
1
  module OracleSqlParser::Ast
2
2
  class DeleteTarget < Hash
3
-
3
+ def to_sql
4
+ result = []
5
+ result << @ast[:table] if @ast[:table]
6
+ if @ast[:name].instance_of? Subquery
7
+ result << "(#{@ast[:name].to_sql})"
8
+ else
9
+ result << @ast[:name]
10
+ end
11
+ result << @ast[:alias] if @ast[:alias]
12
+ result.map(&:to_sql).join(" ")
13
+ end
4
14
  end
5
15
  end
6
16
 
@@ -1,4 +1,7 @@
1
1
  module OracleSqlParser::Ast
2
2
  class ExistsCondition < Hash
3
+ def to_sql(options = {})
4
+ "exists (#{@ast[:target].to_sql})"
5
+ end
3
6
  end
4
7
  end
@@ -1,4 +1,11 @@
1
1
  module OracleSqlParser::Ast
2
2
  class ForUpdateClause < Hash
3
+ def to_sql(options = {})
4
+ sql = []
5
+ sql << "for update"
6
+ sql << "of #{@ast[:columns].to_sql(:separator => ',')}" if @ast[:columns]
7
+ sql << @ast.values_at(:wait, :time).map(&:to_sql).compact.join(" ") if @ast[:wait]
8
+ sql.join(" ")
9
+ end
3
10
  end
4
11
  end
@@ -1,4 +1,14 @@
1
1
  module OracleSqlParser::Ast
2
2
  class FunctionExpressoin < Hash
3
+ def to_sql(options = {})
4
+ sql = []
5
+ sql << @ast[:name].to_sql
6
+ sql << '('
7
+ if @ast[:args]
8
+ sql << @ast[:args].map(&:to_sql).join(',')
9
+ end
10
+ sql << ')'
11
+ sql.join()
12
+ end
3
13
  end
4
14
  end
@@ -1,4 +1,11 @@
1
1
  module OracleSqlParser::Ast
2
2
  class GroupByClause < Hash
3
+ def to_sql(options ={})
4
+ result = "group by #{@ast[:targets].to_sql(:separator => ',')}"
5
+ if @ast[:having]
6
+ result += " having #{@ast[:having].to_sql}"
7
+ end
8
+ result
9
+ end
3
10
  end
4
11
  end
@@ -22,14 +22,14 @@ module OracleSqlParser::Ast
22
22
  "}>\n"
23
23
  end
24
24
 
25
- def to_sql
25
+ def to_sql(options = {:separator => ' '})
26
26
  @ast.map do |k,v|
27
27
  if v.respond_to? :to_sql
28
28
  v.to_sql
29
29
  else
30
30
  v.to_s
31
31
  end
32
- end.compact.join(" ")
32
+ end.compact.join(options[:separator])
33
33
  end
34
34
 
35
35
  def self.[](value)
@@ -8,7 +8,7 @@ module OracleSqlParser::Ast
8
8
  @ast[:quoted] == true
9
9
  end
10
10
 
11
- def to_sql
11
+ def to_sql(options = {})
12
12
  if quoted?
13
13
  "\"#{@ast[:name]}\""
14
14
  else
@@ -1,4 +1,12 @@
1
1
  module OracleSqlParser::Ast
2
2
  class InCondition < Hash
3
+ def to_sql(options = {})
4
+ [
5
+ @ast[:target],
6
+ @ast[:not],
7
+ "in",
8
+ "(#{@ast[:values].to_sql(:separator => ",")})"
9
+ ].map(&:to_sql).compact.join(' ')
10
+ end
3
11
  end
4
12
  end
@@ -1,5 +1,13 @@
1
1
  module OracleSqlParser::Ast
2
2
  class InsertStatement < Hash
3
-
3
+ def to_sql
4
+ result = []
5
+ result << "insert into"
6
+ result << @ast[:insert]
7
+ result << @ast[:alias]
8
+ result << "(#{@ast[:columns].map(&:to_sql).join(',')})" if @ast[:columns]
9
+ result << @ast[:values]
10
+ result.compact.map(&:to_sql).join(' ')
11
+ end
4
12
  end
5
13
  end
@@ -0,0 +1,7 @@
1
+ module OracleSqlParser::Ast
2
+ class InsertValuesClause < Array
3
+ def to_sql
4
+ "values(#{@ast.map(&:to_sql).join(',')})"
5
+ end
6
+ end
7
+ end
@@ -1,4 +1,17 @@
1
1
  module OracleSqlParser::Ast
2
2
  class LikeCondition < Hash
3
+ def to_sql(options = {})
4
+ sql = [
5
+ @ast[:target],
6
+ @ast[:not],
7
+ @ast[:like],
8
+ @ast[:text],
9
+ ]
10
+ if @ast[:escape]
11
+ sql << "escape"
12
+ sql << @ast[:escape]
13
+ end
14
+ sql.map(&:to_sql).compact.join(' ')
15
+ end
3
16
  end
4
17
  end
@@ -1,4 +1,12 @@
1
1
  module OracleSqlParser::Ast
2
2
  class NullCondition < Hash
3
+ def to_sql(options = {})
4
+ [
5
+ @ast[:target],
6
+ "is",
7
+ @ast[:not],
8
+ "null"
9
+ ].compact.map(&:to_sql).join(' ')
10
+ end
3
11
  end
4
12
  end
@@ -1,4 +1,12 @@
1
1
  module OracleSqlParser::Ast
2
2
  class OrderByClause < Hash
3
+ def to_sql
4
+ sql = []
5
+ sql << "order"
6
+ @ast[:siblings].until_nil {|v| sql << v.to_sql}
7
+ sql << "by"
8
+ sql << @ast[:items].to_sql(:separator => ',')
9
+ sql.join(" ")
10
+ end
3
11
  end
4
12
  end
@@ -1,4 +1,9 @@
1
1
  module OracleSqlParser::Ast
2
2
  class OrderByClauseItem < Hash
3
+ def to_sql(options = {})
4
+ sql = @ast.values_at(:target, :asc).map(&:to_sql).compact.join(" ")
5
+ @ast[:nulls].until_nil {|v| sql += " nulls #{v.to_sql}"}
6
+ sql
7
+ end
3
8
  end
4
9
  end
@@ -1,6 +1,6 @@
1
1
  module OracleSqlParser::Ast
2
2
  class QueryBlock < Hash
3
- def to_sql
3
+ def to_sql(options = {})
4
4
  [
5
5
  "select",
6
6
  @ast[:hint],
@@ -1,4 +1,7 @@
1
1
  module OracleSqlParser::Ast
2
2
  class RegexpCondition < Hash
3
+ def to_sql(options = {})
4
+ "regexp_like(#{@ast[:target].to_sql},#{@ast[:regexp].to_sql})"
5
+ end
3
6
  end
4
7
  end
@@ -1,4 +1,7 @@
1
1
  module OracleSqlParser::Ast
2
2
  class RollupCubeClause < Hash
3
+ def to_sql(options = {})
4
+ "#{@ast[:func_name].to_sql}(#{@ast[:args].to_sql(:separator => ',')})"
5
+ end
3
6
  end
4
7
  end
@@ -1,8 +1,20 @@
1
1
  module OracleSqlParser::Ast
2
2
  class SearchedCaseExpression < Base
3
-
4
3
  def else_clause=(ast)
5
4
  @ast[:else_clause] = ast
6
5
  end
6
+
7
+ def to_sql(options = {})
8
+ sql = []
9
+ sql << 'case'
10
+ sql << 'when'
11
+ sql << @ast[:when_condition]
12
+ sql << 'then'
13
+ sql << @ast[:return_expr]
14
+ sql << 'else'
15
+ sql << @ast[:else_clause]
16
+ sql << 'end'
17
+ sql.compact.map(&:to_sql).join(' ')
18
+ end
7
19
  end
8
20
  end
@@ -1,5 +1,9 @@
1
1
  module OracleSqlParser::Ast
2
2
  class SelectStatement < Hash
3
-
3
+ def to_sql(options = {})
4
+ sql = @ast[:subquery].to_sql
5
+ sql += " #{@ast[:for_update_clause].to_sql}" if @ast[:for_update_clause]
6
+ sql
7
+ end
4
8
  end
5
9
  end
@@ -3,5 +3,18 @@ module OracleSqlParser::Ast
3
3
  def else_clause=(ast)
4
4
  @ast[:else_clause] = ast
5
5
  end
6
+
7
+ def to_sql(options = {})
8
+ sql = []
9
+ sql << 'case'
10
+ sql << @ast[:condition]
11
+ sql << @ast[:when_clauses].map{|v| "when #{v.when_expr.to_sql} then #{v.return_expr.to_sql}"}.join(' ')
12
+ if @ast[:else_clause]
13
+ sql << 'else'
14
+ sql << @ast[:else_clause]
15
+ end
16
+ sql << 'end'
17
+ sql.compact.map(&:to_sql).join(' ')
18
+ end
6
19
  end
7
20
  end
@@ -1,4 +1,7 @@
1
1
  module OracleSqlParser::Ast
2
2
  class SimpleComparisionCondition < Hash
3
+ def to_sql(options ={})
4
+ @ast.values_at(:left, :op, :right).map(&:to_sql).join(' ')
5
+ end
3
6
  end
4
7
  end
@@ -0,0 +1,16 @@
1
+ module OracleSqlParser::Ast
2
+ class TableReference < Hash
3
+
4
+ def inspect
5
+ "#<#{self.class.name} #{@ast.inspect}>"
6
+ end
7
+
8
+ def to_sql(options = {})
9
+ result = ''
10
+ result += "#{@ast[:schema_name].to_sql}." if @ast[:schema_name]
11
+ result += @ast[:table_name].to_sql
12
+ result += "@#{@ast[:dblink].to_sql}" if @ast[:dblink]
13
+ result
14
+ end
15
+ end
16
+ end
@@ -3,5 +3,9 @@ module OracleSqlParser::Ast
3
3
  def inspect
4
4
  "#<#{self.class.name} #{@ast.inspect}>"
5
5
  end
6
+
7
+ def to_sql(options = {})
8
+ "'#{@ast[:value]}'"
9
+ end
6
10
  end
7
11
  end
@@ -0,0 +1,7 @@
1
+ module OracleSqlParser::Ast
2
+ class UpdateSetClause < Array
3
+ def to_sql
4
+ "set #{@ast.map(&:to_sql).join(',')}"
5
+ end
6
+ end
7
+ end
@@ -1,4 +1,7 @@
1
1
  module OracleSqlParser::Ast
2
2
  class UpdateSetColumn < Hash
3
+ def to_sql
4
+ @ast.values_at(:column_name, :op, :value).map(&:to_sql).join(' ')
5
+ end
3
6
  end
4
7
  end
@@ -1,4 +1,12 @@
1
1
  module OracleSqlParser::Ast
2
2
  class UpdateStatement < Hash
3
+ def to_sql
4
+ result = []
5
+ result << "update"
6
+ result << @ast[:target]
7
+ result << @ast[:set]
8
+ result << @ast[:where_clause]
9
+ result.compact.map(&:to_sql).join(' ')
10
+ end
3
11
  end
4
12
  end
@@ -1,4 +1,7 @@
1
1
  module OracleSqlParser::Ast
2
2
  class WhereClause < Hash
3
+ def to_sql(options = {})
4
+ "where #{@ast[:condition].to_sql}"
5
+ end
3
6
  end
4
7
  end
@@ -169,7 +169,7 @@ module OracleSqlParser::Grammar
169
169
  rule compound_condition
170
170
  '(' logical_condition ')' {
171
171
  def ast
172
- logical_condition.ast
172
+ OracleSqlParser::Ast::CompoundCondition[logical_condition.ast]
173
173
  end
174
174
  }
175
175
  end
@@ -8,7 +8,7 @@ module OracleSqlParser::Grammar
8
8
  def ast
9
9
  OracleSqlParser::Ast::DeleteStatement[
10
10
  :target => delete_from_clause.ast,
11
- :condition => delete_condition.ast
11
+ :where_clause => delete_condition.ast
12
12
  ]
13
13
  end
14
14
  }
@@ -29,18 +29,26 @@ module OracleSqlParser::Grammar
29
29
  ) space?
30
30
  t_alias:t_alias? {
31
31
  def ast
32
- OracleSqlParser::Ast::DeleteTarget[
33
- :name => t.ast,
34
- :alias => t_alias.ast
35
- ]
32
+ table_or_subquery = t.ast
33
+ if table_or_subquery.instance_of? OracleSqlParser::Ast::DeleteTarget
34
+ table_or_subquery
35
+ else
36
+ OracleSqlParser::Ast::DeleteTarget[
37
+ :name => t.ast,
38
+ :alias => t_alias.ast
39
+ ]
40
+ end
36
41
  end
37
42
  }
38
43
  end
39
44
 
40
45
  rule delete_target_subquery
41
- (table_keyword space)? '(' space? subquery space? ')' {
46
+ t:(table_keyword space)? '(' space? subquery space? ')' {
42
47
  def ast
43
- subquery.ast
48
+ OracleSqlParser::Ast::DeleteTarget[
49
+ :table => t.try(:table_keyword).ast,
50
+ :name => subquery.ast
51
+ ]
44
52
  end
45
53
  }
46
54
  end
@@ -49,18 +57,18 @@ module OracleSqlParser::Grammar
49
57
  where_keyword space?
50
58
  where:(
51
59
  search_condition /
52
- current_of
60
+ delete_current_of
53
61
  ) {
54
62
  def ast
55
- where.ast
63
+ OracleSqlParser::Ast::WhereClause[:condition => where.ast]
56
64
  end
57
65
  }
58
66
  end
59
67
 
60
- rule current_of
61
- current_of_keyword space? cursor_name {
68
+ rule delete_current_of
69
+ current_of {
62
70
  def ast
63
- cursor_name.ast
71
+ super
64
72
  end
65
73
  }
66
74
  end
@@ -133,16 +133,26 @@ module OracleSqlParser::Grammar
133
133
  end
134
134
 
135
135
  rule table_reference
136
- (
137
- schema_name '.' table_name /
138
- table_name
139
- ) ('@' dblink)? {
136
+ t:(
137
+ schema_name:schema_name '.' table_name:table_name /
138
+ table_name:table_name
139
+ ) l:('@' dblink)? {
140
140
  def ast
141
- if(!respond_to?(:schema_name) || !respond_to?(:dblink))
142
- OracleSqlParser::Ast::Identifier.new(:name => text_value)
143
- else
144
- table_name.ast
145
- end
141
+ OracleSqlParser::Ast::TableReference[
142
+ :schema_name => t.try(:schema_name).ast,
143
+ :table_name => t.try(:table_name).ast,
144
+ :dblink => l.try(:dblink).ast
145
+ ]
146
+ end
147
+ }
148
+ end
149
+
150
+ rule current_of
151
+ current_of_keyword space? cursor_name {
152
+ def ast
153
+ OracleSqlParser::Ast::CurrentOf[
154
+ :cursor => cursor_name.ast
155
+ ]
146
156
  end
147
157
  }
148
158
  end
@@ -166,7 +176,7 @@ module OracleSqlParser::Grammar
166
176
  rule sequence
167
177
  ident space? '.' space? n:(currval_keyword / nextval_keyword) {
168
178
  def ast
169
- OracleSqlParser::Ast::Identifier[:value => text_value]
179
+ OracleSqlParser::Ast::Identifier[:name => text_value]
170
180
  end
171
181
  }
172
182
  end
@@ -24,6 +24,7 @@ module OracleSqlParser::Grammar
24
24
  insert_into_ast = insert_into_clause.ast
25
25
  OracleSqlParser::Ast::InsertStatement[
26
26
  :insert => insert_into_ast.table,
27
+ :alias => insert_into_ast.alias,
27
28
  :columns => insert_into_ast.column,
28
29
  :values => values.ast
29
30
  ]
@@ -38,11 +39,12 @@ module OracleSqlParser::Grammar
38
39
  rule insert_into_clause
39
40
  into_keyword space
40
41
  dml_table_expression_clause space
41
- (t_alias space)?
42
+ table_alias:(t_alias space)?
42
43
  insert_column_names:insert_column_names? {
43
44
  def ast
44
45
  OracleSqlParser::Ast::Hash[
45
46
  :table => dml_table_expression_clause.ast,
47
+ :alias => try(:table_alias).try(:t_alias).ast,
46
48
  :column => insert_column_names.ast
47
49
  ]
48
50
  end
@@ -72,7 +74,10 @@ module OracleSqlParser::Grammar
72
74
  rule insert_values
73
75
  expr_or_default more:(space? ',' space? e:expr_or_default)* {
74
76
  def ast
75
- OracleSqlParser::Ast::Array[expr_or_default.ast, *more_expr_or_defaults.map(&:ast)]
77
+ OracleSqlParser::Ast::InsertValuesClause[
78
+ expr_or_default.ast,
79
+ *more_expr_or_defaults.map(&:ast)
80
+ ]
76
81
  end
77
82
 
78
83
  def more_expr_or_defaults
@@ -90,13 +95,10 @@ module OracleSqlParser::Grammar
90
95
  end
91
96
 
92
97
  rule dml_table_expression_clause
93
- (
94
- schema_name '.' table_name '@' dblink /
95
- schema_name '.' table_name /
96
- table_name
97
- ) {
98
+ table_reference
99
+ {
98
100
  def ast
99
- OracleSqlParser::Ast::Identifier[:name => text_value]
101
+ super
100
102
  end
101
103
  }
102
104
  end
@@ -162,30 +162,18 @@ module OracleSqlParser::Grammar
162
162
  rule for_update_clause
163
163
  for_keyword space?
164
164
  update_keyword space?
165
- f:(of_keyword space for_update_clause_columns)?
166
- wait:(wait_with_integer / nowait_keyword)? {
165
+ of:(of_keyword space for_update_clause_columns space?)?
166
+ wait:(
167
+ w:wait_keyword space? time:integer /
168
+ w:nowait_keyword
169
+ )? {
167
170
  def ast
168
171
  OracleSqlParser::Ast::ForUpdateClause[
169
- :columns => for_update_clause_columns.ast,
170
- :wait => wait.ast
172
+ :columns => of.try(:for_update_clause_columns).ast,
173
+ :wait => wait.try(:w).ast,
174
+ :time => wait.try(:time).ast
171
175
  ].remove_nil_values!
172
176
  end
173
-
174
- def for_update_clause_columns
175
- if f.respond_to? :for_update_clause_columns
176
- f.for_update_clause_columns
177
- else
178
- nil
179
- end
180
- end
181
- }
182
- end
183
-
184
- rule wait_with_integer
185
- wait_keyword space? integer {
186
- def ast
187
- integer.ast
188
- end
189
177
  }
190
178
  end
191
179
 
@@ -42,17 +42,11 @@ module OracleSqlParser::Grammar
42
42
  end
43
43
 
44
44
  rule update_set_clause
45
- set_keyword space update_target_columns space? {
45
+ set_keyword space update_target_column more:(space? ',' space? c:update_target_column)* {
46
46
  def ast
47
- update_target_columns.ast
48
- end
49
- }
50
- end
51
-
52
- rule update_target_columns
53
- update_target_column more:(space? ',' space? c:update_target_column)* {
54
- def ast
55
- OracleSqlParser::Ast::Array[update_target_column.ast, *more_update_target_columns.map(&:ast)]
47
+ OracleSqlParser::Ast::UpdateSetClause[
48
+ update_target_column.ast, *more_update_target_columns.map(&:ast)
49
+ ]
56
50
  end
57
51
 
58
52
  def more_update_target_columns
@@ -61,6 +55,18 @@ module OracleSqlParser::Grammar
61
55
  }
62
56
  end
63
57
 
58
+ # rule update_target_columns
59
+ # update_target_column more:(space? ',' space? c:update_target_column)* {
60
+ # def ast
61
+ # OracleSqlParser::Ast::Array[update_target_column.ast, *more_update_target_columns.map(&:ast)]
62
+ # end
63
+ #
64
+ # def more_update_target_columns
65
+ # more.elements.map(&:c)
66
+ # end
67
+ # }
68
+ # end
69
+
64
70
  rule update_target_column
65
71
  column_name space? op:'=' space? sql_expression {
66
72
  def ast
@@ -93,11 +99,9 @@ module OracleSqlParser::Grammar
93
99
  end
94
100
 
95
101
  rule update_current_of
96
- current_of_keyword space? cursor_name {
102
+ current_of {
97
103
  def ast
98
- OracleSqlParser::Ast::CurrentOf[
99
- :cursor_name => cursor_name.ast
100
- ]
104
+ super
101
105
  end
102
106
  }
103
107
  end
@@ -1,3 +1,3 @@
1
1
  module OracleSqlParser
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oracle-sql-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Junichiro Kasuya
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-08-12 00:00:00.000000000 Z
11
+ date: 2015-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -99,6 +99,7 @@ files:
99
99
  - lib/oracle-sql-parser/ast/array.rb
100
100
  - lib/oracle-sql-parser/ast/base.rb
101
101
  - lib/oracle-sql-parser/ast/between_condition.rb
102
+ - lib/oracle-sql-parser/ast/compound_condition.rb
102
103
  - lib/oracle-sql-parser/ast/current_of.rb
103
104
  - lib/oracle-sql-parser/ast/delete_statement.rb
104
105
  - lib/oracle-sql-parser/ast/delete_target.rb
@@ -110,6 +111,7 @@ files:
110
111
  - lib/oracle-sql-parser/ast/identifier.rb
111
112
  - lib/oracle-sql-parser/ast/in_condition.rb
112
113
  - lib/oracle-sql-parser/ast/insert_statement.rb
114
+ - lib/oracle-sql-parser/ast/insert_values_clause.rb
113
115
  - lib/oracle-sql-parser/ast/keyword.rb
114
116
  - lib/oracle-sql-parser/ast/like_condition.rb
115
117
  - lib/oracle-sql-parser/ast/logical_condition.rb
@@ -125,7 +127,9 @@ files:
125
127
  - lib/oracle-sql-parser/ast/simple_case_expression.rb
126
128
  - lib/oracle-sql-parser/ast/simple_comparision_condition.rb
127
129
  - lib/oracle-sql-parser/ast/subquery.rb
130
+ - lib/oracle-sql-parser/ast/table_reference.rb
128
131
  - lib/oracle-sql-parser/ast/text_literal.rb
132
+ - lib/oracle-sql-parser/ast/update_set_clause.rb
129
133
  - lib/oracle-sql-parser/ast/update_set_column.rb
130
134
  - lib/oracle-sql-parser/ast/update_statement.rb
131
135
  - lib/oracle-sql-parser/ast/where_clause.rb