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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5c3f4ef668f82b48eec51d0df59388fd5e8fa98d
4
- data.tar.gz: 25e21fb88208f4d9fdadf7a7cead463d2a0fb17a
3
+ metadata.gz: cc217b63c8a06244155e7007377bca783c611b36
4
+ data.tar.gz: c284841ef57010e523b15405072726c7d5f6125f
5
5
  SHA512:
6
- metadata.gz: e889e9ec8ccb723c62f3cd8cecbe2450bc228336763c6eb27b13ae8acb4c8bb788cbf5a4cbf7fbd00654c14dfdd073aeb84aa2453a839479f613c1f464e5a3ef
7
- data.tar.gz: 76929d84b06a279b22572a0f0a056c6ebb509eb5224000ead47d60b27303d6393241d2c27d10f76be19cd9dd462f57bac3fc54d60f0536f5f21aa8d79863b55e
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 ( {{{ HEROES }}} AND status = :ALIVE)
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 ( {{ * HEROES }} )
47
+ id IN ( << * HEROES >> )
48
48
  OR
49
- id IN ( {{ ! VILLIANS }} )
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.0.0
1
+ 2.0.0
@@ -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
- OR
82
- (
83
- :AUDIENCE_ID NOT IN (BLOCKED)
84
- AND
85
- ( privacy = :WORLD
86
- OR
87
- (
88
- privacy = :PROTECTED
89
- AND
90
- :AUDIENCE_ID IN (
91
- ALLOWED
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
  )
@@ -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: #{k.inspect}" unless has_key?(name)
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.gsub!(/\{\{\s?(\!|\*)?\s?([a-zA-Z0-9\_]+)\s?\}\}/) do |match|
87
- key = $2.to_sym
88
- case $1
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
  ,
@@ -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)['WITH HEROES'].should == 'WITH HEROES'
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 "adds WITH: {{ * MY_NAME }}" do
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
- {{ ! MY_HERO }}
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
- {{ ! hero }}
90
- {{ ! name }}
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: 1.0.0
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-02 00:00:00.000000000 Z
11
+ date: 2015-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler