i_dig_sql 1.0.0 → 2.0.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 +5 -3
- data/VERSION +1 -1
- data/englishy_sql.rb +14 -11
- data/lib/i_dig_sql.rb +20 -11
- data/specs/i_dig_sql.rb +74 -26
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc217b63c8a06244155e7007377bca783c611b36
|
4
|
+
data.tar.gz: c284841ef57010e523b15405072726c7d5f6125f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afd3204cc8130e91f844b192641cd44829ba544d92cec68fd6b7766d688f11564de76ecaa3ae16aa5472515234f6c1962d06447e0b448d69869c0ef01a54e1fd
|
7
|
+
data.tar.gz: e403fe4f10c7b8ec7e9b323fa8c3907f216682ac4fd5159af99505e92f0cf5ee0140df7acb42896e7148c1e4aee99efca0785f6964f155efa74b96f5647a224b
|
data/README.md
CHANGED
@@ -40,13 +40,15 @@ Please note that none of this is ready yet.
|
|
40
40
|
SELECT *
|
41
41
|
FROM people
|
42
42
|
WHERE
|
43
|
-
id IN (
|
43
|
+
id IN ( << HEROES >> AND status = :ALIVE)
|
44
44
|
OR
|
45
45
|
id IN (SELECT ID FROM {{ HEROES }} AND status = :ALIVE)
|
46
46
|
OR
|
47
|
-
id IN (
|
47
|
+
id IN ( << * HEROES >> )
|
48
48
|
OR
|
49
|
-
id IN (
|
49
|
+
id IN ( << patron_id VILLIANS >> )
|
50
|
+
OR
|
51
|
+
id IN ( << VILLIANS >> )
|
50
52
|
^
|
51
53
|
|
52
54
|
sql.to_sql
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
2.0.0
|
data/englishy_sql.rb
CHANGED
@@ -77,18 +77,21 @@ puts(Englishy_Sql.new {
|
|
77
77
|
SELECT *
|
78
78
|
FROM screen_name
|
79
79
|
WHERE
|
80
|
+
id = (SELECT id FROM screen_name WHERE screen_name = 'MEANIE_6294')
|
81
|
+
AND (
|
80
82
|
owner_id = :AUDIENCE_ID
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
83
|
+
OR
|
84
|
+
(
|
85
|
+
:AUDIENCE_ID NOT IN (BLOCKED)
|
86
|
+
AND
|
87
|
+
( privacy = :WORLD
|
88
|
+
OR
|
89
|
+
(
|
90
|
+
privacy = :PROTECTED
|
91
|
+
AND
|
92
|
+
:AUDIENCE_ID IN (
|
93
|
+
ALLOWED
|
94
|
+
)
|
92
95
|
)
|
93
96
|
)
|
94
97
|
)
|
data/lib/i_dig_sql.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
class I_Dig_Sql
|
3
3
|
|
4
4
|
include Enumerable
|
5
|
+
HAS_VAR = /(\{\{|\<\<)[^\}\>]+(\}\}|\>\>)/
|
5
6
|
|
6
7
|
Duplicate = Class.new RuntimeError
|
7
8
|
|
@@ -11,7 +12,7 @@ class I_Dig_Sql
|
|
11
12
|
class H < Hash
|
12
13
|
|
13
14
|
def [] name
|
14
|
-
fail ArgumentError, "Unknown key: #{
|
15
|
+
fail ArgumentError, "Unknown key: #{name.inspect}" unless has_key?(name)
|
15
16
|
super
|
16
17
|
end
|
17
18
|
|
@@ -83,25 +84,33 @@ class I_Dig_Sql
|
|
83
84
|
s = @string.dup
|
84
85
|
ctes = []
|
85
86
|
|
86
|
-
s
|
87
|
-
|
88
|
-
|
89
|
-
when "!"
|
90
|
-
self[key]
|
91
|
-
when "*"
|
92
|
-
ctes << key
|
93
|
-
"SELECT * FROM #{key}"
|
94
|
-
else
|
87
|
+
while s[HAS_VAR]
|
88
|
+
s.gsub!(/\{\{\s?([a-zA-Z0-9\_]+)\s?\}\}/) do |match|
|
89
|
+
key = $1.to_sym
|
95
90
|
ctes << key
|
96
91
|
key
|
97
92
|
end
|
93
|
+
|
94
|
+
s.gsub!(/\<\<\s?([a-zA-Z0-9\_\-\ \*]+)\s?\>\>/) do |match|
|
95
|
+
tokens = $1.split
|
96
|
+
key = tokens.pop.to_sym
|
97
|
+
field = tokens.empty? ? nil : tokens.join(' ')
|
98
|
+
|
99
|
+
case
|
100
|
+
when field
|
101
|
+
ctes << key
|
102
|
+
"SELECT #{field} FROM #{key}"
|
103
|
+
else
|
104
|
+
self[key]
|
105
|
+
end
|
106
|
+
end
|
98
107
|
end
|
99
108
|
|
100
109
|
return s if ctes.empty?
|
101
110
|
|
102
111
|
%^
|
103
112
|
WITH
|
104
|
-
#{ctes.map { |k| "#{k} AS (
|
113
|
+
#{ctes.uniq.map { |k| "#{k} AS (
|
105
114
|
#{self[k]}
|
106
115
|
)" }.join "
|
107
116
|
,
|
data/specs/i_dig_sql.rb
CHANGED
@@ -21,7 +21,22 @@ describe :I_Dig_Sql do
|
|
21
21
|
line = 0
|
22
22
|
readme.split("\n").detect { |l| line = line + 1; l['```ruby'] }
|
23
23
|
result = eval(code, nil, 'README.md', line)
|
24
|
-
sql(result)
|
24
|
+
sql(result).should == sql(%^
|
25
|
+
WITH
|
26
|
+
HEROES AS ( SELECT id FROM hero WHERE id = :PERSON_ID ) ,
|
27
|
+
VILLIANS AS ( SELECT id FROM villian WHERE id = :PERSON_ID )
|
28
|
+
SELECT * FROM people
|
29
|
+
WHERE
|
30
|
+
id IN ( SELECT id FROM hero WHERE id = :PERSON_ID AND status = :ALIVE)
|
31
|
+
OR
|
32
|
+
id IN (SELECT ID FROM HEROES AND status = :ALIVE)
|
33
|
+
OR
|
34
|
+
id IN ( SELECT * FROM HEROES )
|
35
|
+
OR
|
36
|
+
id IN ( SELECT patron_id FROM VILLIANS )
|
37
|
+
OR
|
38
|
+
id IN ( SELECT id FROM villian WHERE id = :PERSON_ID )
|
39
|
+
^)
|
25
40
|
end # === it
|
26
41
|
|
27
42
|
it "adds WITH: {{MY_NAME}}" do
|
@@ -46,36 +61,38 @@ describe :I_Dig_Sql do
|
|
46
61
|
^)
|
47
62
|
end
|
48
63
|
|
49
|
-
it "
|
50
|
-
sql = I_Dig_Sql.new
|
51
|
-
sql[:MY_HERO] = "SELECT * FROM hero"
|
52
|
-
sql[:MY_NAME] = "SELECT * FROM name"
|
53
|
-
sql << %^
|
54
|
-
{{ * MY_NAME }}
|
55
|
-
{{ * MY_HERO }}
|
56
|
-
^
|
57
|
-
sql(sql).should == sql(%^
|
58
|
-
WITH
|
59
|
-
MY_NAME AS (
|
60
|
-
SELECT * FROM name
|
61
|
-
) ,
|
62
|
-
MY_HERO AS (
|
63
|
-
SELECT * FROM hero
|
64
|
-
)
|
65
|
-
SELECT * FROM MY_NAME
|
66
|
-
SELECT * FROM MY_HERO
|
67
|
-
^)
|
68
|
-
end # === it
|
69
|
-
|
70
|
-
it "replaces text with content: {{ ! MY_NAME }}" do
|
64
|
+
it "replaces text with content: << MY_NAME >>" do
|
71
65
|
sql = I_Dig_Sql.new
|
72
66
|
sql[:MY_HERO] = "SELECT * FROM hero"
|
73
67
|
sql << %^
|
74
|
-
|
68
|
+
<< MY_HERO >>
|
75
69
|
^
|
76
70
|
sql(sql).should == "SELECT * FROM hero"
|
77
71
|
end # === it
|
78
72
|
|
73
|
+
%w{ * id }.each { |s|
|
74
|
+
it "adds WITH: << #{s} MY_NAME >>" do
|
75
|
+
sql = I_Dig_Sql.new
|
76
|
+
sql[:MY_HERO] = "SELECT id, p_id FROM hero"
|
77
|
+
sql[:MY_NAME] = "SELECT id, n_id FROM name"
|
78
|
+
sql << %^
|
79
|
+
<< #{s} MY_NAME >>
|
80
|
+
<< #{s} MY_HERO >>
|
81
|
+
^
|
82
|
+
sql(sql).should == sql(%^
|
83
|
+
WITH
|
84
|
+
MY_NAME AS (
|
85
|
+
SELECT id, n_id FROM name
|
86
|
+
) ,
|
87
|
+
MY_HERO AS (
|
88
|
+
SELECT id, p_id FROM hero
|
89
|
+
)
|
90
|
+
SELECT #{s} FROM MY_NAME
|
91
|
+
SELECT #{s} FROM MY_HERO
|
92
|
+
^)
|
93
|
+
end # === it
|
94
|
+
}
|
95
|
+
|
79
96
|
end # === describe :I_Dig_Sql
|
80
97
|
|
81
98
|
describe '.new' do
|
@@ -86,8 +103,8 @@ describe '.new' do
|
|
86
103
|
second = I_Dig_Sql.new(first)
|
87
104
|
second[:name] = "SELECT * FROM name"
|
88
105
|
second << %^
|
89
|
-
|
90
|
-
|
106
|
+
<< hero >>
|
107
|
+
<< name >>
|
91
108
|
^
|
92
109
|
sql(second).should == sql(%^
|
93
110
|
SELECT * FROM hero
|
@@ -153,3 +170,34 @@ describe :to_pair do
|
|
153
170
|
end # === it
|
154
171
|
|
155
172
|
end # === describe :to_pair
|
173
|
+
|
174
|
+
|
175
|
+
describe :to_sql do
|
176
|
+
|
177
|
+
it "renders nested replacements" do
|
178
|
+
i = I_Dig_Sql.new <<-EOF
|
179
|
+
<< FIRST >>
|
180
|
+
EOF
|
181
|
+
i[:FIRST] = " << SECOND >> "
|
182
|
+
i[:SECOND] = " << THIRD >> "
|
183
|
+
i[:THIRD] = "FINAL"
|
184
|
+
sql(i).should == "FINAL"
|
185
|
+
end # === it
|
186
|
+
|
187
|
+
it "prints CTE definitions once, if used multiple times" do
|
188
|
+
i = I_Dig_Sql.new <<-EOF
|
189
|
+
{{my_cte}}
|
190
|
+
{{my_cte}}
|
191
|
+
EOF
|
192
|
+
i[:my_cte] = "SELECT *"
|
193
|
+
sql(i).should == sql(%^
|
194
|
+
WITH
|
195
|
+
my_cte AS (
|
196
|
+
SELECT *
|
197
|
+
)
|
198
|
+
my_cte
|
199
|
+
my_cte
|
200
|
+
^)
|
201
|
+
end # === it
|
202
|
+
|
203
|
+
end # === describe :to_sql
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: i_dig_sql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- da99
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|