oracle-sql-parser 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c837374a732fc2668270aeda336b61a01370e4bb
4
- data.tar.gz: 7b3a68ffe18ef9433fdaed74261474e555d944a2
3
+ metadata.gz: 9a9130b843c652c403f5d864ded3d2e23d7bf1c4
4
+ data.tar.gz: 1c80886a500a13230d17feaf356eee6e54377fe9
5
5
  SHA512:
6
- metadata.gz: 06d395ede2b2bd56ab772adb3fd648371a8725de851475afd7de5547c229feb8532bd732a9320d041b39ea3a20d104494829ac9142c4f8664c913d56525eec99
7
- data.tar.gz: e2e5bf837cbf9a41066becb3ba9f3bb4b098005114d342a2b314b50021defea7a9999aeeae60ea05d9f31b90f4dd9a824eeb517f7e50bb0625d188eaa781d521
6
+ metadata.gz: 7e7933ce7a6b024a0c8e8d338017c76289a7004cf2015e82c86eb05d9ca25ff9e4d579648cca4a22328a639cbc9453f9b08fcb2c6b271cfce390cf91ce6db779
7
+ data.tar.gz: a2b5f51fc41a1bbcb5d7408318abb922b178aca68e74789ee4f4ab835d747eea655652346f9e68a84eb550d6566454b85c6598c20ee01bf46bc566b367e5f107
data/README.md CHANGED
@@ -37,17 +37,17 @@ ast = syntax_tree.ast
37
37
  :hint => nil,
38
38
  :modifier => nil,
39
39
  :select_list => #<OracleSqlParser::Ast::Array [
40
- #<OracleSqlParser::Ast::NumberLiteral {:value=>"1"}>
41
- ]>
40
+ #<OracleSqlParser::Ast::NumberLiteral {:value=>"1"}>
41
+ ]>
42
42
  ,
43
- :select_sources => #<OracleSqlParser::Ast::Identifier {:name=>"dual"}>,
43
+ :select_sources => #<OracleSqlParser::Ast::TableReference {:schema_name=>nil, :table_name=>#<OracleSqlParser::Ast::Identifier {:name=>"dual"}>, :dblink=>nil}>,
44
44
  :where_clause => nil,
45
45
  :group_by_clause => nil,
46
- :model_clause => nil}>
46
+ :model_clause => nil}>
47
47
  ,
48
- :order_by_clause => nil}>
48
+ :order_by_clause => nil}>
49
49
  ,
50
- :for_update_clause => nil}>
50
+ :for_update_clause => nil}>
51
51
  </pre>
52
52
 
53
53
  ```ruby
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require "sql_parser"
4
+ require "oracle-sql-parser"
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -2,6 +2,7 @@ module OracleSqlParser
2
2
  end
3
3
  require 'treetop'
4
4
  require 'oracle-sql-parser/version.rb'
5
+ require 'oracle-sql-parser/util.rb'
5
6
  require 'oracle-sql-parser/ast.rb'
6
7
  require 'oracle-sql-parser/treetop_ext.rb'
7
8
  require 'oracle-sql-parser/grammar.rb'
@@ -39,4 +39,5 @@ require 'oracle-sql-parser/ast/text_literal.rb'
39
39
  require 'oracle-sql-parser/ast/number_literal.rb'
40
40
  require 'oracle-sql-parser/ast/table_reference.rb'
41
41
  require 'oracle-sql-parser/ast/compound_condition.rb'
42
+ require 'oracle-sql-parser/ast/variable.rb'
42
43
  require 'oracle-sql-parser/ast/keyword.rb'
@@ -16,6 +16,15 @@ module OracleSqlParser::Ast
16
16
  self.new(*values)
17
17
  end
18
18
 
19
+ def map_ast!(&block)
20
+ @ast = @ast.map do |v|
21
+ if v.is_a? OracleSqlParser::Ast::Base
22
+ v.map_ast!(&block)
23
+ end
24
+ block.call(v)
25
+ end
26
+ end
27
+
19
28
  def initialize(*args)
20
29
  @ast = args
21
30
  end
@@ -6,8 +6,28 @@ def nil.to_sql(options = {})
6
6
  nil
7
7
  end
8
8
 
9
- def nil.until_nil(&block)
9
+ def nil.duplicable?
10
+ false
11
+ end
12
+
13
+ def true.duplicable?
14
+ false
15
+ end
16
+
17
+ def false.duplicable?
18
+ false
19
+ end
20
+
21
+ class Symbol
22
+ def duplicable?
23
+ false
24
+ end
25
+ end
10
26
 
27
+ class Numeric
28
+ def duplicable?
29
+ false
30
+ end
11
31
  end
12
32
 
13
33
  unless Object.respond_to? :try
@@ -20,8 +40,8 @@ unless Object.respond_to? :try
20
40
  end
21
41
  end
22
42
 
23
- def until_nil(&block)
24
- block.call(self)
43
+ def duplicable?
44
+ true
25
45
  end
26
46
  end
27
47
  end
@@ -34,6 +54,7 @@ end
34
54
 
35
55
  module OracleSqlParser::Ast
36
56
  class Base
57
+ include OracleSqlParser::Util::Parameternizable
37
58
  def initialize(arg)
38
59
  if arg.instance_of?(Array) || arg.instance_of?(Hash)
39
60
  raise "cant assign #{arg.class} Base.new()"
@@ -41,6 +62,49 @@ module OracleSqlParser::Ast
41
62
  @ast = arg
42
63
  end
43
64
 
65
+ def initialize_copy(original)
66
+ if @ast.nil?
67
+ self.class.new
68
+ else
69
+ self.class.new(original.instance_variable_get(:@ast).dup)
70
+ end
71
+ end
72
+
73
+ def self.deep_dup(original)
74
+ if original.is_a? OracleSqlParser::Ast::Base
75
+ original.deep_dup
76
+ elsif original.is_a? ::Hash
77
+ ::Hash[ original.map {|k, v| [k, deep_dup(v)]} ]
78
+ elsif original.is_a? ::Array
79
+ original.map {|v| deep_dup(v)}
80
+ elsif original.duplicable?
81
+ original.dup
82
+ else
83
+ original
84
+ end
85
+ end
86
+
87
+ def deep_dup
88
+ copy = self.class.new
89
+ original_ast = self.instance_variable_get(:@ast)
90
+ copy_ast = self.class.deep_dup(original_ast)
91
+ copy.instance_variable_set(:@ast, copy_ast)
92
+ copy
93
+ end
94
+
95
+ def map_ast(&block)
96
+ duplicated = self.deep_dup
97
+ duplicated.map_ast!(&block)
98
+ duplicated
99
+ end
100
+
101
+ def map_ast!(&block)
102
+ if @ast.is_a? OracleSqlParser::Ast::Base
103
+ @ast.map_ast!(&block)
104
+ end
105
+ @ast = block.call(@ast)
106
+ end
107
+
44
108
  def remove_nil_values!
45
109
  @ast.remove_nil_values! if @ast.respond_to? :remove_nil_values!
46
110
  self
@@ -5,8 +5,8 @@ module OracleSqlParser::Ast
5
5
  extend Forwardable
6
6
  def_delegator :@ast, :keys, :[]
7
7
 
8
- def initialize(value)
9
- raise "only ::Hash instance" unless value.instance_of? ::Hash
8
+ def initialize(value = {})
9
+ raise "only ::Hash instance #{value.inspect}" unless value.instance_of? ::Hash
10
10
  @ast = value
11
11
  end
12
12
 
@@ -16,6 +16,17 @@ module OracleSqlParser::Ast
16
16
  self
17
17
  end
18
18
 
19
+ def map_ast!(&block)
20
+ mapped = @ast.class.new
21
+ @ast.each do |k, v|
22
+ if v.is_a? OracleSqlParser::Ast::Base
23
+ v.map_ast!(&block)
24
+ end
25
+ mapped[k] = block.call(v)
26
+ end
27
+ @ast = mapped
28
+ end
29
+
19
30
  def inspect
20
31
  "#<#{self.class.name}\n" +
21
32
  @ast.map{|k,v| "#{k.inspect} => #{v.inspect}"}.join(",\n").gsub(/^/, ' ') +
@@ -3,7 +3,7 @@ module OracleSqlParser::Ast
3
3
  def to_sql
4
4
  sql = []
5
5
  sql << "order"
6
- @ast[:siblings].until_nil {|v| sql << v.to_sql}
6
+ sql << @ast[:siblings].to_sql if @ast[:siblings]
7
7
  sql << "by"
8
8
  sql << @ast[:items].to_sql(:separator => ',')
9
9
  sql.join(" ")
@@ -2,7 +2,7 @@ module OracleSqlParser::Ast
2
2
  class OrderByClauseItem < Hash
3
3
  def to_sql(options = {})
4
4
  sql = @ast.values_at(:target, :asc).map(&:to_sql).compact.join(" ")
5
- @ast[:nulls].until_nil {|v| sql += " nulls #{v.to_sql}"}
5
+ sql += " nulls #{@ast[:nulls].to_sql}" if @ast[:nulls]
6
6
  sql
7
7
  end
8
8
  end
@@ -5,7 +5,7 @@ module OracleSqlParser::Ast
5
5
  "select",
6
6
  @ast[:hint],
7
7
  @ast[:modifier],
8
- @ast[:select_list],
8
+ @ast[:select_list].map(&:to_sql).join(','),
9
9
  "from",
10
10
  @ast[:select_sources],
11
11
  @ast[:where_clause],
@@ -0,0 +1,7 @@
1
+ module OracleSqlParser::Ast
2
+ class Variable < Base
3
+ def to_sql
4
+ ":#{@ast[:name]}"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ require 'oracle-sql-parser/util/parameternizable.rb'
2
+ require 'oracle-sql-parser/util/parameternized_query.rb'
@@ -0,0 +1,7 @@
1
+ module OracleSqlParser::Util
2
+ module Parameternizable
3
+ def to_parameternized
4
+ ParameternizedQuery.new(self)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ module OracleSqlParser::Util
2
+ class ParameternizedQuery
3
+ attr_reader :ast, :params
4
+ def initialize(original)
5
+ @index = 0
6
+ @params = {}
7
+ @ast = original.map_ast do |v|
8
+ case v
9
+ when OracleSqlParser::Ast::NumberLiteral, OracleSqlParser::Ast::TextLiteral
10
+ assign_parameter(v)
11
+ else
12
+ v
13
+ end
14
+ end
15
+ end
16
+
17
+ def query
18
+ ast.to_sql
19
+ end
20
+
21
+ def assign_parameter(value)
22
+ name = "a#{@index}"
23
+ @index += 1
24
+ @params[name] = value
25
+ OracleSqlParser::Ast::Variable[:name =>name]
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module OracleSqlParser
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.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.2.0
4
+ version: 0.3.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-24 00:00:00.000000000 Z
11
+ date: 2015-08-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -132,6 +132,7 @@ files:
132
132
  - lib/oracle-sql-parser/ast/update_set_clause.rb
133
133
  - lib/oracle-sql-parser/ast/update_set_column.rb
134
134
  - lib/oracle-sql-parser/ast/update_statement.rb
135
+ - lib/oracle-sql-parser/ast/variable.rb
135
136
  - lib/oracle-sql-parser/ast/where_clause.rb
136
137
  - lib/oracle-sql-parser/grammar.rb
137
138
  - lib/oracle-sql-parser/grammar/condition.rb
@@ -151,6 +152,9 @@ files:
151
152
  - lib/oracle-sql-parser/grammar/update.rb
152
153
  - lib/oracle-sql-parser/grammar/update.treetop
153
154
  - lib/oracle-sql-parser/treetop_ext.rb
155
+ - lib/oracle-sql-parser/util.rb
156
+ - lib/oracle-sql-parser/util/parameternizable.rb
157
+ - lib/oracle-sql-parser/util/parameternized_query.rb
154
158
  - lib/oracle-sql-parser/version.rb
155
159
  - oracle-sql-parser.gemspec
156
160
  homepage: https://github.com/jksy/sql_parser