abstract-sql 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/abstract-sql.gemspec +3 -1
- data/lib/abstract-sql/parser/statement.rb +3 -1
- data/lib/abstract-sql/transform/statement.rb +50 -17
- data/lib/abstract-sql/version.rb +1 -1
- data/test/test_abstract.rb +17 -4
- metadata +89 -55
data/abstract-sql.gemspec
CHANGED
@@ -7,10 +7,12 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.version = Abstract::Sql::VERSION
|
8
8
|
s.authors = ["Hallelujah"]
|
9
9
|
s.email = ["hery@rails-royce.org"]
|
10
|
-
s.homepage = "https://github.com/hallelujah/sql
|
10
|
+
s.homepage = "https://github.com/hallelujah/abstract-sql"
|
11
11
|
s.summary = %q{Transform a SQL statement to Perl SQL::Abstract JSON format}
|
12
12
|
s.description = %q{It reverse an SQL statement to a Perl SQL::Abstract JSON format.}
|
13
13
|
|
14
|
+
s.rubyforge_project = "abstract-sql"
|
15
|
+
|
14
16
|
|
15
17
|
s.add_dependency 'parslet'
|
16
18
|
s.add_development_dependency 'test-unit', '>= 2.1.0'
|
@@ -9,6 +9,8 @@ module SQL
|
|
9
9
|
rule(:spaces?){ spaces.maybe}
|
10
10
|
rule(:digit) { match('[0-9]') }
|
11
11
|
rule(:int){ digit.repeat(1).as(:int) }
|
12
|
+
rule(:bool_not) { spaces >> (str('not') | str('NOT')).as(:not) >> spaces}
|
13
|
+
|
12
14
|
rule(:float) do
|
13
15
|
(str('-').maybe >> (
|
14
16
|
str('0') | (match('[1-9]') >> digit.repeat)
|
@@ -46,7 +48,7 @@ module SQL
|
|
46
48
|
end
|
47
49
|
|
48
50
|
rule(:entity) do
|
49
|
-
|
51
|
+
bool_not.maybe >> (value.as(:lvalue) >> operator >> value.as(:rvalue))
|
50
52
|
end
|
51
53
|
|
52
54
|
rule(:statement) do
|
@@ -7,32 +7,57 @@ module SQL
|
|
7
7
|
rule(:val =>{:int => simple(:x)}){ String(x).to_i }
|
8
8
|
rule(:val =>{:string => simple(:x)}){ String(x) }
|
9
9
|
# rule(:boolean_operator => simple(:x)){{ :boolean_operator => String(x).downcase.to_sym }}
|
10
|
-
|
11
|
-
rule(:lvalue => {:column => simple(:x) }, :operator => simple(:op), :rvalue =>
|
12
|
-
|
10
|
+
|
11
|
+
rule(:lvalue => {:column => simple(:x) }, :operator => simple(:op), :rvalue => {:column => simple(:z)}) do
|
12
|
+
{ String(x).to_sym => { operator(op) => String(z).to_sym } }
|
13
|
+
end
|
14
|
+
|
15
|
+
rule(:lvalue => {:column => simple(:x) }, :operator => simple(:op), :rvalue => simple(:z) ) do
|
16
|
+
{String(x).to_sym => { operator(op) => z } }
|
17
|
+
end
|
18
|
+
|
19
|
+
rule(:lvalue => simple(:x) , :operator => simple(:op), :rvalue => {:column => simple(:z)}) do
|
20
|
+
{ String(z).to_sym => { operator(op) => x } }
|
21
|
+
end
|
13
22
|
|
14
23
|
# Statement right or left without right leaf
|
15
|
-
rule(:lstatement => { :lstatement => subtree(:y) })
|
16
|
-
|
24
|
+
rule(:lstatement => { :lstatement => subtree(:y) })do
|
25
|
+
{:lstatement => y }
|
26
|
+
end
|
27
|
+
|
28
|
+
rule(:rstatement => { :lstatement => subtree(:y) })do
|
29
|
+
{:rstatement => y}
|
30
|
+
end
|
17
31
|
|
18
|
-
rule({:lstatement=>{:lstatement=> subtree(:l)},
|
19
|
-
|
20
|
-
|
32
|
+
rule({:lstatement=>{:lstatement=> subtree(:l)}, :boolean_operator => simple(:op), :rstatement=>{:lstatement => subtree(:r)}}) do
|
33
|
+
{:lstatement => l, :boolean_operator => op, :rstatement => r}
|
34
|
+
end
|
21
35
|
|
22
36
|
# Statement right or left without right leaf and a boolean operator
|
23
|
-
rule(:lstatement => { :lstatement => subtree(:y) })
|
24
|
-
|
37
|
+
rule(:lstatement => { :lstatement => subtree(:y) }) do
|
38
|
+
{:lstatement => y }
|
39
|
+
end
|
40
|
+
|
41
|
+
rule(:rstatement => { :lstatement => subtree(:y) }) do
|
42
|
+
{:rstatement => y}
|
43
|
+
end
|
44
|
+
|
45
|
+
rule({:lstatement => subtree(:l), :boolean_operator=> simple(:op), :rstatement=>{:lstatement=>subtree(:r)}}) do
|
46
|
+
{:lstatement => l, :boolean_operator => operator(op), :rstatement => r}
|
47
|
+
end
|
25
48
|
|
26
|
-
rule({:
|
27
|
-
|
28
|
-
|
49
|
+
rule({:rstatement => subtree(:r), :boolean_operator=> simple(:op), :lstatement=>{:lstatement=>subtree(:l)}}) do
|
50
|
+
{:lstatement => l, :boolean_operator => operator(op), :rstatement => r}
|
51
|
+
end
|
29
52
|
|
30
|
-
rule({:rstatement => subtree(:r),
|
31
|
-
:boolean_operator=> simple(:op),
|
32
|
-
:lstatement=>{:lstatement=>subtree(:l)}}) do {:lstatement => l, :boolean_operator => operator(op), :rstatement => r} end
|
33
53
|
|
54
|
+
rule(:boolean_operator => simple(:op), :lstatement => subtree(:l), :rstatement => subtree(:r) ) do
|
55
|
+
{operator(op) => [l, r] }
|
56
|
+
end
|
34
57
|
|
35
|
-
rule(:
|
58
|
+
rule(:not => simple(:n), :lvalue=> subtree(:l), :operator=> simple(:op), :rvalue=> subtree(:r)) do
|
59
|
+
{bool(n) => {:lvalue => l, :operator => op, :rvalue => r}}
|
60
|
+
end
|
36
61
|
|
37
62
|
|
38
63
|
end
|
@@ -48,4 +73,12 @@ class Parslet::Pattern::Context
|
|
48
73
|
end
|
49
74
|
end
|
50
75
|
|
76
|
+
def bool(n)
|
77
|
+
if (b = String(n).downcase) == 'not'
|
78
|
+
operator('not_bool')
|
79
|
+
else
|
80
|
+
n
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
51
84
|
end
|
data/lib/abstract-sql/version.rb
CHANGED
data/test/test_abstract.rb
CHANGED
@@ -7,22 +7,35 @@ class TestAbstract < Test::Unit::TestCase
|
|
7
7
|
end
|
8
8
|
|
9
9
|
test "parse a simple condition" do
|
10
|
-
assert_equal( {:"-and" => [{:
|
11
|
-
assert_equal( {:"-and" => [{:
|
10
|
+
assert_equal( {:"-and" => [{:id => { :"-=" => 1}}, {:label => {:"-like" => '%webo%'}}]}, @abstract.parse("id = 1 and label like '%webo%'"))
|
11
|
+
assert_equal( {:"-and" => [{:id => { :"-=" => 1}}, {:label => {:"-like" => '%webo%'}}]}, @abstract.parse("((id = 1 )) and label like '%webo%'"))
|
12
12
|
end
|
13
13
|
|
14
14
|
test "parse a complex condition" do
|
15
15
|
assert_equal(
|
16
16
|
{
|
17
17
|
:"-or" => [
|
18
|
-
{:"-and" => [{:
|
19
|
-
{:"-and" => [{:
|
18
|
+
{:"-and" => [{:id => { :"-=" => 1}}, {:label => {:"-like" => '%webo%'}}]},
|
19
|
+
{:"-and" => [{:id => { :"-!=" => 1}}, {:label => {:"-like" => '%api%'}}]}
|
20
20
|
]
|
21
21
|
},
|
22
22
|
@abstract.parse("(id = 1 AND label like '%webo%') OR (id !=1 and label like '%api%')")
|
23
23
|
)
|
24
24
|
end
|
25
25
|
|
26
|
+
test "Very complex condition" do
|
27
|
+
assert_equal(
|
28
|
+
{
|
29
|
+
:"-and" => [
|
30
|
+
{ :"-or" => [ {:criterion_7 => {:"->=" => 8}}, {:criterion_1 => {:"-<" => 8}} ] },
|
31
|
+
{ :"-or" => [ {:segment_1 => {:"-<" => 4}}, {:"-not_bool" => { :segment_2 => {:"-<" => 1} }}] }
|
32
|
+
]
|
33
|
+
},
|
34
|
+
@abstract.parse("( criterion_7 >= 8 or criterion_1 < 8 ) and ( segment_1 < 4 or not segment_2 < 1 )")
|
35
|
+
)
|
36
|
+
|
37
|
+
end
|
38
|
+
|
26
39
|
test "raise error when bad formatted sql" do
|
27
40
|
assert_raise Parslet::ParseFailed do
|
28
41
|
# No AND
|
metadata
CHANGED
@@ -1,67 +1,91 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: abstract-sql
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Hallelujah
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
17
|
+
|
18
|
+
date: 2011-08-01 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
15
22
|
name: parslet
|
16
|
-
|
23
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
17
24
|
none: false
|
18
|
-
requirements:
|
19
|
-
- -
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
22
|
-
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
23
32
|
prerelease: false
|
24
|
-
|
25
|
-
|
33
|
+
requirement: *id001
|
34
|
+
type: :runtime
|
35
|
+
- !ruby/object:Gem::Dependency
|
26
36
|
name: test-unit
|
27
|
-
|
37
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
28
38
|
none: false
|
29
|
-
requirements:
|
30
|
-
- -
|
31
|
-
- !ruby/object:Gem::Version
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 11
|
43
|
+
segments:
|
44
|
+
- 2
|
45
|
+
- 1
|
46
|
+
- 0
|
32
47
|
version: 2.1.0
|
33
|
-
type: :development
|
34
48
|
prerelease: false
|
35
|
-
|
36
|
-
|
49
|
+
requirement: *id002
|
50
|
+
type: :development
|
51
|
+
- !ruby/object:Gem::Dependency
|
37
52
|
name: rcov
|
38
|
-
|
53
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
39
54
|
none: false
|
40
|
-
requirements:
|
41
|
-
- -
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
|
44
|
-
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
45
62
|
prerelease: false
|
46
|
-
|
47
|
-
|
63
|
+
requirement: *id003
|
64
|
+
type: :development
|
65
|
+
- !ruby/object:Gem::Dependency
|
48
66
|
name: rake
|
49
|
-
|
67
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
50
68
|
none: false
|
51
|
-
requirements:
|
52
|
-
- -
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
|
55
|
-
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
56
76
|
prerelease: false
|
57
|
-
|
77
|
+
requirement: *id004
|
78
|
+
type: :development
|
58
79
|
description: It reverse an SQL statement to a Perl SQL::Abstract JSON format.
|
59
|
-
email:
|
80
|
+
email:
|
60
81
|
- hery@rails-royce.org
|
61
82
|
executables: []
|
83
|
+
|
62
84
|
extensions: []
|
85
|
+
|
63
86
|
extra_rdoc_files: []
|
64
|
-
|
87
|
+
|
88
|
+
files:
|
65
89
|
- .gitignore
|
66
90
|
- .rvmrc
|
67
91
|
- Gemfile
|
@@ -74,30 +98,40 @@ files:
|
|
74
98
|
- lib/abstract-sql/version.rb
|
75
99
|
- test/helper.rb
|
76
100
|
- test/test_abstract.rb
|
77
|
-
|
101
|
+
has_rdoc: true
|
102
|
+
homepage: https://github.com/hallelujah/abstract-sql
|
78
103
|
licenses: []
|
104
|
+
|
79
105
|
post_install_message:
|
80
106
|
rdoc_options: []
|
81
|
-
|
107
|
+
|
108
|
+
require_paths:
|
82
109
|
- lib
|
83
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
111
|
none: false
|
85
|
-
requirements:
|
86
|
-
- -
|
87
|
-
- !ruby/object:Gem::Version
|
88
|
-
|
89
|
-
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
hash: 3
|
116
|
+
segments:
|
117
|
+
- 0
|
118
|
+
version: "0"
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
120
|
none: false
|
91
|
-
requirements:
|
92
|
-
- -
|
93
|
-
- !ruby/object:Gem::Version
|
94
|
-
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
hash: 3
|
125
|
+
segments:
|
126
|
+
- 0
|
127
|
+
version: "0"
|
95
128
|
requirements: []
|
96
|
-
|
97
|
-
|
129
|
+
|
130
|
+
rubyforge_project: abstract-sql
|
131
|
+
rubygems_version: 1.3.7
|
98
132
|
signing_key:
|
99
133
|
specification_version: 3
|
100
134
|
summary: Transform a SQL statement to Perl SQL::Abstract JSON format
|
101
|
-
test_files:
|
135
|
+
test_files:
|
102
136
|
- test/helper.rb
|
103
137
|
- test/test_abstract.rb
|