i_dig_sql 0.1.1 → 1.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 +40 -8
- data/VERSION +1 -1
- data/bin/test +1 -1
- data/englishy_sql.rb +95 -0
- data/i_dig_sql.gemspec +1 -1
- data/lib/i_dig_sql.rb +77 -100
- data/specs/i_dig_sql.rb +121 -199
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c3f4ef668f82b48eec51d0df59388fd5e8fa98d
|
4
|
+
data.tar.gz: 25e21fb88208f4d9fdadf7a7cead463d2a0fb17a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e889e9ec8ccb723c62f3cd8cecbe2450bc228336763c6eb27b13ae8acb4c8bb788cbf5a4cbf7fbd00654c14dfdd073aeb84aa2453a839479f613c1f464e5a3ef
|
7
|
+
data.tar.gz: 76929d84b06a279b22572a0f0a056c6ebb509eb5224000ead47d60b27303d6393241d2c27d10f76be19cd9dd462f57bac3fc54d60f0536f5f21aa8d79863b55e
|
data/README.md
CHANGED
@@ -1,23 +1,55 @@
|
|
1
1
|
# I\_Dig\_Sql
|
2
2
|
|
3
|
-
My way of
|
3
|
+
My way of managing SQL fragments using Ruby.
|
4
4
|
|
5
5
|
# Warning:
|
6
6
|
|
7
|
-
|
7
|
+
You will hate using this.
|
8
|
+
Instead, use:
|
8
9
|
|
9
|
-
|
10
|
+
* [Arel](http://github.com/rails/arel).
|
11
|
+
* K'bam [https://github.com/vilnius-leopold/kbam](https://github.com/vilnius-leopold/kbam)
|
10
12
|
|
11
|
-
|
13
|
+
# History
|
12
14
|
|
13
|
-
|
15
|
+
I had trouble maintaining BIG sql queries.
|
14
16
|
|
15
|
-
|
17
|
+
I tried many things.
|
16
18
|
|
19
|
+
The best way (within my preferences)
|
20
|
+
was to use sub-queries, CTEs, avoid joins as much as possible,
|
21
|
+
and this gem to manage SQL fragments and CTEs.
|
17
22
|
|
18
|
-
|
23
|
+
Naturally, you would want to use prepared statements, compiled wat-cha-me-call-its,
|
24
|
+
functions, views, thing-ma-jig-ers, and other tools available in your RDBMS.
|
19
25
|
|
20
|
-
|
26
|
+
So this gem is for lazy, stupid people like me.
|
27
|
+
|
28
|
+
# Usage
|
29
|
+
|
30
|
+
Please note that none of this is ready yet.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
|
34
|
+
require 'i_dig_sql'
|
35
|
+
|
36
|
+
sql = I_Dig_Sql.new
|
37
|
+
sql[:HEROES] = "SELECT id FROM hero WHERE id = :PERSON_ID"
|
38
|
+
sql[:VILLIANS] = "SELECT id FROM villian WHERE id = :PERSON_ID"
|
39
|
+
sql << %^
|
40
|
+
SELECT *
|
41
|
+
FROM people
|
42
|
+
WHERE
|
43
|
+
id IN ( {{{ HEROES }}} AND status = :ALIVE)
|
44
|
+
OR
|
45
|
+
id IN (SELECT ID FROM {{ HEROES }} AND status = :ALIVE)
|
46
|
+
OR
|
47
|
+
id IN ( {{ * HEROES }} )
|
48
|
+
OR
|
49
|
+
id IN ( {{ ! VILLIANS }} )
|
50
|
+
^
|
21
51
|
|
52
|
+
sql.to_sql
|
53
|
+
```
|
22
54
|
|
23
55
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
1.0.0
|
data/bin/test
CHANGED
data/englishy_sql.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
|
2
|
+
require 'pry'
|
3
|
+
require 'sequel'
|
4
|
+
DB = Sequel.mock
|
5
|
+
|
6
|
+
class Englishy_Sql
|
7
|
+
|
8
|
+
Duplicate = Class.new RuntimeError
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@sql = {}
|
12
|
+
@vals = {}
|
13
|
+
instance_eval(&(Proc.new))
|
14
|
+
end # === def initialize
|
15
|
+
|
16
|
+
def val name, v
|
17
|
+
if @vals.has_key?(name) && @vals[name] != v
|
18
|
+
fail Duplicate, "Already set w/different value: #{name.inspect}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def screen_name name
|
23
|
+
val :SCREEN_NAME, name.to_s.strip.upcase
|
24
|
+
sql(:SCREEN_NAME) {
|
25
|
+
|
26
|
+
any! {
|
27
|
+
owner_id.==(:AUDIENCE_ID)
|
28
|
+
all {
|
29
|
+
no_block
|
30
|
+
any! {
|
31
|
+
WORLD!
|
32
|
+
allowed
|
33
|
+
}
|
34
|
+
}
|
35
|
+
} # === any
|
36
|
+
|
37
|
+
one!
|
38
|
+
} # === sql
|
39
|
+
end
|
40
|
+
|
41
|
+
def post
|
42
|
+
@sql = @sql.where(:id=> DB[:post].select(:id) )
|
43
|
+
end # === def post
|
44
|
+
|
45
|
+
def comments
|
46
|
+
@sql = @sql.or(:parent_id=>DB[:comment].select(:id))
|
47
|
+
end
|
48
|
+
|
49
|
+
def sql *args
|
50
|
+
case
|
51
|
+
when args.empty?
|
52
|
+
@sql.sql
|
53
|
+
|
54
|
+
when args.size == 2
|
55
|
+
name, v = args
|
56
|
+
is_dup = @sql.has_key?(name) && @sql[name] != v
|
57
|
+
fail Duplicate, "Already set w/different value: #{name.inspect}" if is_dup
|
58
|
+
|
59
|
+
else
|
60
|
+
fail ArgumentError, "Unknown args: #{args.inspect}"
|
61
|
+
|
62
|
+
end # === case
|
63
|
+
end
|
64
|
+
|
65
|
+
end # === Englishy_Sql
|
66
|
+
|
67
|
+
|
68
|
+
puts(Englishy_Sql.new {
|
69
|
+
table(:computer) {
|
70
|
+
as(:comments)
|
71
|
+
any! {
|
72
|
+
is_owner
|
73
|
+
}
|
74
|
+
}
|
75
|
+
}.sql)
|
76
|
+
|
77
|
+
SELECT *
|
78
|
+
FROM screen_name
|
79
|
+
WHERE
|
80
|
+
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
|
92
|
+
)
|
93
|
+
)
|
94
|
+
)
|
95
|
+
)
|
data/i_dig_sql.gemspec
CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.license = "MIT"
|
18
18
|
|
19
19
|
spec.files = `git ls-files -z`.split("\x0")
|
20
|
-
spec.executables = spec.files.grep("bin/#{spec.name}")
|
20
|
+
spec.executables = spec.files.grep("bin/#{spec.name}") { |f| File.basename(f) }
|
21
21
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
22
22
|
spec.require_paths = ["lib"]
|
23
23
|
|
data/lib/i_dig_sql.rb
CHANGED
@@ -1,136 +1,113 @@
|
|
1
1
|
|
2
2
|
class I_Dig_Sql
|
3
3
|
|
4
|
-
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
Duplicate = Class.new RuntimeError
|
5
7
|
|
6
8
|
class << self
|
7
9
|
end # === class self ===
|
8
10
|
|
9
|
-
|
10
|
-
@withs = []
|
11
|
-
@tags_for_withs = {}
|
12
|
-
@select = nil
|
13
|
-
@as = nil
|
14
|
-
@unions = []
|
15
|
-
@sql = sql
|
16
|
-
@args = args
|
17
|
-
yield self if block_given?
|
18
|
-
end
|
19
|
-
|
20
|
-
def args
|
21
|
-
@args
|
22
|
-
end
|
23
|
-
protected :args
|
11
|
+
class H < Hash
|
24
12
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
raise "@as not set"
|
13
|
+
def [] name
|
14
|
+
fail ArgumentError, "Unknown key: #{k.inspect}" unless has_key?(name)
|
15
|
+
super
|
29
16
|
end
|
30
17
|
|
31
|
-
|
32
|
-
|
33
|
-
|
18
|
+
def []= name, val
|
19
|
+
if has_key?(name) && self[name] != val
|
20
|
+
fail ArgumentError, "Key already set: #{name.inspect}"
|
21
|
+
end
|
34
22
|
|
35
|
-
|
36
|
-
|
37
|
-
@tags_for_withs[o] = []
|
38
|
-
@args.concat(o.args) if o.is_a?(I_Dig_Sql)
|
39
|
-
@args.concat args
|
40
|
-
self
|
41
|
-
end
|
23
|
+
super
|
24
|
+
end
|
42
25
|
|
43
|
-
|
26
|
+
def merge_these *args
|
27
|
+
args.each { |h|
|
28
|
+
h.each { |k,v|
|
29
|
+
self[k] = v
|
30
|
+
}
|
31
|
+
}
|
32
|
+
self
|
33
|
+
end
|
44
34
|
|
45
|
-
|
46
|
-
list = @tags_for_withs[@withs.last]
|
47
|
-
raise "Last query was not a WITH/cte query" unless list
|
48
|
-
list.push name
|
49
|
-
self
|
50
|
-
end
|
35
|
+
end # === class H
|
51
36
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
57
|
-
memo
|
37
|
+
attr_reader :sqls, :vars
|
38
|
+
def initialize *args
|
39
|
+
@digs = args.select { |a|
|
40
|
+
a.is_a? I_Dig_Sql
|
58
41
|
}
|
59
|
-
end
|
60
42
|
|
61
|
-
|
62
|
-
@
|
43
|
+
@sqls = H.new
|
44
|
+
@vars = H.new
|
63
45
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
def FROM o
|
68
|
-
@select[:from] = o
|
46
|
+
@sqls.merge_these *(@digs.map(&:sqls))
|
47
|
+
@vars.merge_these *(@digs.map(&:vars))
|
69
48
|
|
70
|
-
|
49
|
+
@string = args.select { |s| s.is_a? String }.join("\n")
|
71
50
|
end
|
72
51
|
|
73
|
-
def
|
52
|
+
def [] name
|
53
|
+
@sqls[name]
|
54
|
+
end
|
74
55
|
|
75
|
-
|
76
|
-
|
77
|
-
|
56
|
+
def []= name, val
|
57
|
+
@sqls[name] = val
|
58
|
+
end
|
78
59
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
@args.concat sql[:args]
|
60
|
+
def each
|
61
|
+
if block_given?
|
62
|
+
@sqls.each { |k, v| yield k, v }
|
83
63
|
else
|
84
|
-
@
|
64
|
+
@sqls.each
|
85
65
|
end
|
86
|
-
|
87
|
-
@select[:where] = o
|
88
|
-
self
|
89
66
|
end
|
90
67
|
|
91
|
-
def
|
92
|
-
@
|
68
|
+
def << str
|
69
|
+
@string << (
|
70
|
+
if @string.empty?
|
71
|
+
str
|
72
|
+
else
|
73
|
+
"\n" << str
|
74
|
+
end
|
75
|
+
)
|
76
|
+
end
|
93
77
|
|
94
|
-
|
78
|
+
def to_pair
|
79
|
+
[to_sql, vars]
|
95
80
|
end
|
96
81
|
|
97
82
|
def to_sql
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
" #{w.AS} AS (#{w.to_sql[:sql]}) "
|
113
|
-
end
|
114
|
-
}.join("\n,\n")
|
115
|
-
end
|
116
|
-
|
117
|
-
s << "\n"
|
118
|
-
|
119
|
-
if @select
|
120
|
-
s << "\n SELECT #{@select[:select]}"
|
121
|
-
s << "\n FROM #{@select[:from]}" if @select[:from]
|
122
|
-
s << "\n WHERE #{@select[:where]}" if @select[:where]
|
83
|
+
s = @string.dup
|
84
|
+
ctes = []
|
85
|
+
|
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
|
95
|
+
ctes << key
|
96
|
+
key
|
123
97
|
end
|
124
|
-
|
125
|
-
end # === if @sql
|
126
|
-
|
127
|
-
if not @unions.empty?
|
128
|
-
s << "\n UNION #{@unions.map { |sql| sql.to_sql[:sql] }.join "\nUNION\n" }"
|
129
98
|
end
|
130
99
|
|
131
|
-
s
|
132
|
-
|
133
|
-
|
100
|
+
return s if ctes.empty?
|
101
|
+
|
102
|
+
%^
|
103
|
+
WITH
|
104
|
+
#{ctes.map { |k| "#{k} AS (
|
105
|
+
#{self[k]}
|
106
|
+
)" }.join "
|
107
|
+
,
|
108
|
+
"}
|
109
|
+
#{s}
|
110
|
+
^
|
134
111
|
end
|
135
112
|
|
136
113
|
end # === class I_Dig_Sql ===
|
data/specs/i_dig_sql.rb
CHANGED
@@ -1,14 +1,11 @@
|
|
1
1
|
|
2
|
-
require "pry"
|
3
2
|
require "Bacon_Colored"
|
4
|
-
require "i_dig_sql"
|
5
|
-
require "i_dig_sql/String"
|
6
3
|
|
7
4
|
def sql o
|
8
5
|
if o.is_a? String
|
9
6
|
return o.split.join(" ")
|
10
7
|
else
|
11
|
-
sql(o.to_sql
|
8
|
+
sql(o.to_sql)
|
12
9
|
end
|
13
10
|
end
|
14
11
|
|
@@ -16,218 +13,143 @@ def args o
|
|
16
13
|
o.to_sql[:args]
|
17
14
|
end
|
18
15
|
|
19
|
-
describe
|
20
|
-
|
21
|
-
it "
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
it "
|
31
|
-
|
32
|
-
|
33
|
-
sql
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
.
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
16
|
+
describe :I_Dig_Sql do
|
17
|
+
|
18
|
+
it "runs the code from README.md" do
|
19
|
+
readme = File.read(File.expand_path(File.dirname(__FILE__) + '/../README.md'))
|
20
|
+
code = readme[/```ruby([^`]+)```/] && $1
|
21
|
+
line = 0
|
22
|
+
readme.split("\n").detect { |l| line = line + 1; l['```ruby'] }
|
23
|
+
result = eval(code, nil, 'README.md', line)
|
24
|
+
sql(result)['WITH HEROES'].should == 'WITH HEROES'
|
25
|
+
end # === it
|
26
|
+
|
27
|
+
it "adds WITH: {{MY_NAME}}" do
|
28
|
+
sql = I_Dig_Sql.new
|
29
|
+
sql[:MY_HERO] = "SELECT * FROM hero"
|
30
|
+
sql[:MY_NAME] = "SELECT * FROM name"
|
31
|
+
sql << %^
|
32
|
+
{{MY_HERO}}
|
33
|
+
{{MY_NAME}}
|
34
|
+
^
|
35
|
+
sql(sql).should == sql(%^
|
36
|
+
WITH
|
37
|
+
MY_HERO AS (
|
38
|
+
SELECT * FROM hero
|
39
|
+
)
|
40
|
+
,
|
41
|
+
MY_NAME AS (
|
42
|
+
SELECT * FROM name
|
43
|
+
)
|
44
|
+
MY_HERO
|
45
|
+
MY_NAME
|
44
46
|
^)
|
45
47
|
end
|
46
48
|
|
47
|
-
it "
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
describe "#comma" do
|
63
|
-
|
64
|
-
it "acts like a WITH statement" do
|
65
|
-
o = I_Dig_Sql.new
|
66
|
-
.WITH('cte1 AS ( SELECT * FROM table_1 ) ')
|
67
|
-
.comma('cte2 AS ( SELECT * FROM table_2 )')
|
68
|
-
|
69
|
-
sql(o).should == sql(%^
|
70
|
-
WITH
|
71
|
-
cte1 AS ( SELECT * FROM table_1 )
|
72
|
-
, cte2 AS ( SELECT * FROM table_2 )
|
73
|
-
^)
|
74
|
-
end
|
75
|
-
|
76
|
-
it "saves args" do
|
77
|
-
o = I_Dig_Sql.new
|
78
|
-
.WITH('cte1 AS ( SELECT * FROM table_1 ) ')
|
79
|
-
.comma('cte2 AS ( SELECT * FROM table_2 WHERE id = ?)', 2)
|
80
|
-
|
81
|
-
args(o).should == [2]
|
82
|
-
end
|
83
|
-
|
84
|
-
end # === describe #comma ===
|
85
|
-
|
86
|
-
describe "#to_sql" do
|
87
|
-
|
88
|
-
describe ":sql" do
|
89
|
-
|
90
|
-
it "includes both WITH and SELECT statements" do
|
91
|
-
o = I_Dig_Sql.new
|
92
|
-
o.WITH("cte AS (SELECT * FROM other_table)")
|
93
|
-
o.SELECT(" parent_id ")
|
94
|
-
.FROM("main_table")
|
95
|
-
sql(o).should == sql(%^
|
96
|
-
WITH cte AS (SELECT * FROM other_table)
|
97
|
-
SELECT parent_id
|
98
|
-
FROM main_table
|
99
|
-
^)
|
100
|
-
end
|
101
|
-
|
102
|
-
end # === describe :sql ===
|
103
|
-
|
104
|
-
describe ":args" do
|
105
|
-
|
106
|
-
it "returns an array of arguments" do
|
107
|
-
o = I_Dig_Sql.new
|
108
|
-
.SELECT(" parent_id ")
|
109
|
-
.FROM("main_table")
|
110
|
-
.WHERE(" ? = ? AND b = ? ", 1, 2, 3)
|
111
|
-
args(o).should == [1,2,3]
|
112
|
-
end
|
113
|
-
|
114
|
-
end # === describe :args ===
|
115
|
-
|
116
|
-
|
117
|
-
end # === describe #to_sql ===
|
118
|
-
|
119
|
-
describe "#WHERE" do
|
120
|
-
|
121
|
-
it "merges sql into string if arg is a I_Dig_Sql" do
|
122
|
-
other = I_Dig_Sql.new.SELECT("*").FROM("other")
|
123
|
-
|
124
|
-
o = I_Dig_Sql.new.SELECT("parent_id")
|
125
|
-
.FROM("main")
|
126
|
-
.WHERE("id IN", other)
|
127
|
-
|
128
|
-
sql(o).should == sql(%~
|
129
|
-
SELECT parent_id
|
130
|
-
FROM main
|
131
|
-
WHERE id IN (
|
132
|
-
SELECT * FROM other
|
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
|
133
64
|
)
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
end
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
sql(o.to_sql[:sql]).should == sql(%^SELECT id FROM my_table^)
|
167
|
-
end
|
168
|
-
|
169
|
-
end # === describe String ===
|
170
|
-
|
171
|
-
|
172
|
-
describe ".tag_as" do
|
173
|
-
|
174
|
-
it "adds tag to tag list of last query" do
|
175
|
-
o = "SELECT *".i_dig_sql
|
176
|
-
o.WITH("cte1 AS (SELECT * FROM table_1)")
|
177
|
-
.tag_as('group 1')
|
178
|
-
o.WITH("cte2 AS (SELECT * FROM table_2)")
|
179
|
-
.tag_as('group 1')
|
180
|
-
|
181
|
-
list = o.find_tagged('group 1')
|
182
|
-
list.size.should == 2
|
183
|
-
sql(list.first).should == sql("cte1 AS (SELECT * FROM table_1)")
|
184
|
-
sql(list.last).should == sql("cte2 AS (SELECT * FROM table_2)")
|
185
|
-
end
|
186
|
-
|
187
|
-
it "raises error if last query was not a WITH/cte" do
|
188
|
-
o = "SELECT *".i_dig_sql
|
189
|
-
should.raise(RuntimeError) {
|
190
|
-
o.tag_as('group 1')
|
191
|
-
}.message.should.match /Last query was not a WITH\/cte/i
|
192
|
-
end
|
193
|
-
|
194
|
-
end # === describe .tag_as ===
|
65
|
+
SELECT * FROM MY_NAME
|
66
|
+
SELECT * FROM MY_HERO
|
67
|
+
^)
|
68
|
+
end # === it
|
69
|
+
|
70
|
+
it "replaces text with content: {{ ! MY_NAME }}" do
|
71
|
+
sql = I_Dig_Sql.new
|
72
|
+
sql[:MY_HERO] = "SELECT * FROM hero"
|
73
|
+
sql << %^
|
74
|
+
{{ ! MY_HERO }}
|
75
|
+
^
|
76
|
+
sql(sql).should == "SELECT * FROM hero"
|
77
|
+
end # === it
|
78
|
+
|
79
|
+
end # === describe :I_Dig_Sql
|
80
|
+
|
81
|
+
describe '.new' do
|
82
|
+
|
83
|
+
it "merges sql values: .new(i_dig_sql)" do
|
84
|
+
first = I_Dig_Sql.new
|
85
|
+
first[:hero] = "SELECT * FROM hero"
|
86
|
+
second = I_Dig_Sql.new(first)
|
87
|
+
second[:name] = "SELECT * FROM name"
|
88
|
+
second << %^
|
89
|
+
{{ ! hero }}
|
90
|
+
{{ ! name }}
|
91
|
+
^
|
92
|
+
sql(second).should == sql(%^
|
93
|
+
SELECT * FROM hero
|
94
|
+
SELECT * FROM name
|
95
|
+
^)
|
96
|
+
end # === it
|
195
97
|
|
196
|
-
|
98
|
+
it "combines all Strings" do
|
99
|
+
i = I_Dig_Sql.new "SELECT ", " * ", " FROM ", " NAME "
|
100
|
+
sql(i).should == sql("SELECT * FROM NAME")
|
101
|
+
end # === it
|
197
102
|
|
198
|
-
|
199
|
-
o = "SELECT *".i_dig_sql
|
200
|
-
o.WITH("cte1 AS (SELECT * FROM table_1)")
|
201
|
-
.tag_as('group 1')
|
202
|
-
o.WITH("cte2 AS (SELECT * FROM table_2)")
|
203
|
-
.tag_as('group 1')
|
103
|
+
end # === describe '.new'
|
204
104
|
|
205
|
-
list = o.find_tagged('group 1')
|
206
|
-
list.size.should == 2
|
207
|
-
sql(list.first).should == sql("cte1 AS (SELECT * FROM table_1)")
|
208
|
-
sql(list.last).should == sql("cte2 AS (SELECT * FROM table_2)")
|
209
|
-
end
|
210
105
|
|
211
|
-
|
212
|
-
o = "SELECT *".i_dig_sql
|
213
|
-
o.WITH("cte1 AS (SELECT * FROM table_1)").tag_as('group 1')
|
214
|
-
o.WITH("cte2 AS (SELECT * FROM table_2)").tag_as('group 1')
|
215
|
-
o.WITH("cte3 AS (SELECT * FROM table_3)").tag_as('group 2')
|
106
|
+
describe :vars do
|
216
107
|
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
sql(list.last).should == sql("cte2 AS (SELECT * FROM table_2)")
|
221
|
-
end
|
108
|
+
it "combines vars from other digs" do
|
109
|
+
one = I_Dig_Sql.new
|
110
|
+
one.vars[:one] = 1
|
222
111
|
|
223
|
-
|
112
|
+
two = I_Dig_Sql.new
|
113
|
+
one.vars[:two] = 2
|
224
114
|
|
115
|
+
three = I_Dig_Sql.new
|
116
|
+
one.vars[:three] = 3
|
225
117
|
|
118
|
+
dig = I_Dig_Sql.new one, two, three
|
119
|
+
dig.vars[:four] = 4
|
226
120
|
|
121
|
+
dig.vars.should == {
|
122
|
+
four: 4,
|
123
|
+
three: 3,
|
124
|
+
two: 2,
|
125
|
+
one: 1
|
126
|
+
}
|
127
|
+
end # === it
|
227
128
|
|
129
|
+
it "fails w/Duplicate if other digs have the same var name" do
|
130
|
+
should.raise(ArgumentError) {
|
131
|
+
one = I_Dig_Sql.new
|
132
|
+
one.vars[:two] = 2
|
228
133
|
|
134
|
+
i = I_Dig_Sql.new one
|
135
|
+
i.vars[:two] = 3
|
136
|
+
i.vars
|
137
|
+
}.message.should.match /Key already set: :two/
|
138
|
+
end # === it
|
229
139
|
|
140
|
+
end # === describe :vars
|
230
141
|
|
231
142
|
|
143
|
+
describe :to_pair do
|
232
144
|
|
145
|
+
it "returns an Array: [String, Hash]" do
|
146
|
+
i = I_Dig_Sql.new <<-EOF
|
147
|
+
SELECT * FROM new
|
148
|
+
EOF
|
149
|
+
i.vars[:one] = 2
|
150
|
+
sql, vars = i.to_pair
|
151
|
+
sql(sql).should == sql("SELECT * FROM new")
|
152
|
+
vars.should == {one: 2}
|
153
|
+
end # === it
|
233
154
|
|
155
|
+
end # === describe :to_pair
|
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: 0.
|
4
|
+
version: 1.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-
|
11
|
+
date: 2015-04-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -82,6 +82,7 @@ files:
|
|
82
82
|
- Rakefile
|
83
83
|
- VERSION
|
84
84
|
- bin/test
|
85
|
+
- englishy_sql.rb
|
85
86
|
- i_dig_sql.gemspec
|
86
87
|
- lib/i_dig_sql.rb
|
87
88
|
- lib/i_dig_sql/String.rb
|