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 +4 -4
- data/README.md +6 -6
- data/bin/console +1 -1
- data/lib/oracle-sql-parser.rb +1 -0
- data/lib/oracle-sql-parser/ast.rb +1 -0
- data/lib/oracle-sql-parser/ast/array.rb +9 -0
- data/lib/oracle-sql-parser/ast/base.rb +67 -3
- data/lib/oracle-sql-parser/ast/hash.rb +13 -2
- data/lib/oracle-sql-parser/ast/order_by_clause.rb +1 -1
- data/lib/oracle-sql-parser/ast/order_by_clause_item.rb +1 -1
- data/lib/oracle-sql-parser/ast/query_block.rb +1 -1
- data/lib/oracle-sql-parser/ast/variable.rb +7 -0
- data/lib/oracle-sql-parser/util.rb +2 -0
- data/lib/oracle-sql-parser/util/parameternizable.rb +7 -0
- data/lib/oracle-sql-parser/util/parameternized_query.rb +28 -0
- data/lib/oracle-sql-parser/version.rb +1 -1
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a9130b843c652c403f5d864ded3d2e23d7bf1c4
|
4
|
+
data.tar.gz: 1c80886a500a13230d17feaf356eee6e54377fe9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
data/lib/oracle-sql-parser.rb
CHANGED
@@ -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.
|
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
|
24
|
-
|
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(/^/, ' ') +
|
@@ -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
|
-
|
5
|
+
sql += " nulls #{@ast[:nulls].to_sql}" if @ast[:nulls]
|
6
6
|
sql
|
7
7
|
end
|
8
8
|
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
|
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.
|
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-
|
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
|