i_dig_sql 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/VERSION +1 -0
- data/i_dig_sql.gemspec +4 -3
- data/lib/i_dig_sql/version.rb +1 -1
- data/lib/i_dig_sql.rb +28 -10
- data/specs/i_dig_sql.rb +97 -12
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7935c90bb6a2a1905d660337a7e4b5875c5a137
|
4
|
+
data.tar.gz: 9a33d2b2dc1021e457055d4afb1d898365b368df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7686a037ab4e9291ea9184f731b29c1e76226376f192a964929f7d91e321c14128e42562607b534430a94b343ec7d9d5aa733bce6d9468b73b1435a6c9c2b94e
|
7
|
+
data.tar.gz: c3df8799043ef5619baa0059fb732e9cd470073a6fcd9c1fc8df7a06036a5fdfccfe71384f0c5b6caa37d463ab88d4121d64cfd7a5b7143221cfa349bc5dd219
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.4
|
data/i_dig_sql.gemspec
CHANGED
@@ -22,7 +22,8 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
23
23
|
spec.require_paths = ["lib"]
|
24
24
|
|
25
|
-
spec.add_development_dependency "bundler", "~> 1.5"
|
26
|
-
spec.add_development_dependency "rake", '~> 0'
|
27
|
-
spec.add_development_dependency "bacon", '~> 0'
|
25
|
+
spec.add_development_dependency "bundler" , "~> 1.5"
|
26
|
+
spec.add_development_dependency "rake" , '~> 0'
|
27
|
+
spec.add_development_dependency "bacon" , '~> 0'
|
28
|
+
spec.add_development_dependency "pry" , '~> 0'
|
28
29
|
end
|
data/lib/i_dig_sql/version.rb
CHANGED
data/lib/i_dig_sql.rb
CHANGED
@@ -1,19 +1,26 @@
|
|
1
1
|
|
2
2
|
class I_Dig_Sql
|
3
3
|
|
4
|
+
Only_One_Where = Class.new(RuntimeError)
|
5
|
+
|
4
6
|
class << self
|
5
7
|
end # === class self ===
|
6
8
|
|
7
|
-
def initialize sql = nil, args
|
9
|
+
def initialize sql = nil, *args
|
8
10
|
@withs = []
|
9
11
|
@select = nil
|
10
12
|
@as = nil
|
11
13
|
@unions = []
|
12
14
|
@sql = sql
|
13
|
-
@args = args
|
15
|
+
@args = args
|
14
16
|
yield self if block_given?
|
15
17
|
end
|
16
18
|
|
19
|
+
def args
|
20
|
+
@args
|
21
|
+
end
|
22
|
+
protected :args
|
23
|
+
|
17
24
|
def AS o = :return
|
18
25
|
if o == :return
|
19
26
|
return @as if @as
|
@@ -24,15 +31,14 @@ class I_Dig_Sql
|
|
24
31
|
self
|
25
32
|
end
|
26
33
|
|
27
|
-
def WITH o
|
34
|
+
def WITH o, *args
|
28
35
|
@withs << o
|
29
|
-
|
36
|
+
@args.concat(o.args) if o.is_a?(I_Dig_Sql)
|
37
|
+
@args.concat args
|
30
38
|
self
|
31
39
|
end
|
32
40
|
|
33
|
-
|
34
|
-
WITH o
|
35
|
-
end
|
41
|
+
alias_method :comma, :WITH
|
36
42
|
|
37
43
|
def SELECT str, *args
|
38
44
|
@select = {:select=>str, :args=>args, :from=>nil, :where=>nil}
|
@@ -46,9 +52,21 @@ class I_Dig_Sql
|
|
46
52
|
self
|
47
53
|
end
|
48
54
|
|
49
|
-
def WHERE o
|
50
|
-
@select[:where] = o
|
55
|
+
def WHERE o, *args
|
51
56
|
|
57
|
+
if @select[:where]
|
58
|
+
raise Only_One_Where.new("Multiple use of WHERE: #{@select[:where]} |--| #{o}")
|
59
|
+
end
|
60
|
+
|
61
|
+
if args.size == 1 && args.first.is_a?(I_Dig_Sql)
|
62
|
+
sql = args.first.to_sql
|
63
|
+
o = "#{o} ( #{sql[:sql]} )"
|
64
|
+
@args.concat sql[:args]
|
65
|
+
else
|
66
|
+
@args.concat args
|
67
|
+
end
|
68
|
+
|
69
|
+
@select[:where] = o
|
52
70
|
self
|
53
71
|
end
|
54
72
|
|
@@ -94,7 +112,7 @@ class I_Dig_Sql
|
|
94
112
|
|
95
113
|
s << "\n"
|
96
114
|
|
97
|
-
{:sql=>s, :args
|
115
|
+
{:sql=>s, :args=>@args}
|
98
116
|
end
|
99
117
|
|
100
118
|
end # === class I_Dig_Sql ===
|
data/specs/i_dig_sql.rb
CHANGED
@@ -10,6 +10,10 @@ def sql o
|
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
|
+
def args o
|
14
|
+
o.to_sql[:args]
|
15
|
+
end
|
16
|
+
|
13
17
|
describe ".new" do
|
14
18
|
|
15
19
|
it "returns a I_Dig_Sql if passed a String" do
|
@@ -38,11 +42,24 @@ describe "#WITH()" do
|
|
38
42
|
^)
|
39
43
|
end
|
40
44
|
|
45
|
+
it "accepts args" do
|
46
|
+
o = I_Dig_Sql.new
|
47
|
+
.WITH(" some_table AS (SELECT * FROM other_table WHERE id = ?) ", 1)
|
48
|
+
args(o).should == [1]
|
49
|
+
end
|
50
|
+
|
51
|
+
it "merges args from other I_Dig_Sql objects" do
|
52
|
+
other = I_Dig_Sql.new("SELECT * FROM main_table WHERE i = ?", 1).AS('cte1')
|
53
|
+
|
54
|
+
o = I_Dig_Sql.new
|
55
|
+
.WITH(other)
|
56
|
+
args(o).should == [1] end
|
57
|
+
|
41
58
|
end # === describe #WITH() ===
|
42
59
|
|
43
60
|
describe "#comma" do
|
44
61
|
|
45
|
-
it "
|
62
|
+
it "acts like a WITH statement" do
|
46
63
|
o = I_Dig_Sql.new
|
47
64
|
.WITH('cte1 AS ( SELECT * FROM table_1 ) ')
|
48
65
|
.comma('cte2 AS ( SELECT * FROM table_2 )')
|
@@ -54,24 +71,92 @@ describe "#comma" do
|
|
54
71
|
^)
|
55
72
|
end
|
56
73
|
|
74
|
+
it "saves args" do
|
75
|
+
o = I_Dig_Sql.new
|
76
|
+
.WITH('cte1 AS ( SELECT * FROM table_1 ) ')
|
77
|
+
.comma('cte2 AS ( SELECT * FROM table_2 WHERE id = ?)', 2)
|
78
|
+
|
79
|
+
args(o).should == [2]
|
80
|
+
end
|
81
|
+
|
57
82
|
end # === describe #comma ===
|
58
83
|
|
59
84
|
describe "#to_sql" do
|
60
85
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
86
|
+
describe ":sql" do
|
87
|
+
|
88
|
+
it "includes both WITH and SELECT statements" do
|
89
|
+
o = I_Dig_Sql.new
|
90
|
+
o.WITH("cte AS (SELECT * FROM other_table)")
|
91
|
+
o.SELECT(" parent_id ")
|
92
|
+
.FROM("main_table")
|
93
|
+
sql(o).should == sql(%^
|
94
|
+
WITH cte AS (SELECT * FROM other_table)
|
95
|
+
SELECT parent_id
|
96
|
+
FROM main_table
|
97
|
+
^)
|
98
|
+
end
|
99
|
+
|
100
|
+
end # === describe :sql ===
|
101
|
+
|
102
|
+
describe ":args" do
|
103
|
+
|
104
|
+
it "returns an array of arguments" do
|
105
|
+
o = I_Dig_Sql.new
|
106
|
+
.SELECT(" parent_id ")
|
107
|
+
.FROM("main_table")
|
108
|
+
.WHERE(" ? = ? AND b = ? ", 1, 2, 3)
|
109
|
+
args(o).should == [1,2,3]
|
110
|
+
end
|
111
|
+
|
112
|
+
end # === describe :args ===
|
113
|
+
|
72
114
|
|
73
115
|
end # === describe #to_sql ===
|
74
116
|
|
117
|
+
describe "#WHERE" do
|
118
|
+
|
119
|
+
it "merges sql into string if arg is a I_Dig_Sql" do
|
120
|
+
other = I_Dig_Sql.new.SELECT("*").FROM("other")
|
121
|
+
|
122
|
+
o = I_Dig_Sql.new.SELECT("parent_id")
|
123
|
+
.FROM("main")
|
124
|
+
.WHERE("id IN", other)
|
125
|
+
|
126
|
+
sql(o).should == sql(%~
|
127
|
+
SELECT parent_id
|
128
|
+
FROM main
|
129
|
+
WHERE id IN (
|
130
|
+
SELECT * FROM other
|
131
|
+
)
|
132
|
+
~)
|
133
|
+
end
|
134
|
+
|
135
|
+
it "merges args if arg is a I_Dig_Sql" do
|
136
|
+
other = I_Dig_Sql.new.SELECT("*").FROM("other")
|
137
|
+
.WHERE("id = ", 1)
|
138
|
+
|
139
|
+
o = I_Dig_Sql.new.SELECT("parent_id")
|
140
|
+
.FROM("main")
|
141
|
+
.WHERE("id IN", other)
|
142
|
+
|
143
|
+
args(o).should == [1]
|
144
|
+
end
|
145
|
+
|
146
|
+
it "raises exception if used more than once" do
|
147
|
+
lambda {
|
148
|
+
I_Dig_Sql.new
|
149
|
+
.SELECT('*')
|
150
|
+
.FROM('main')
|
151
|
+
.WHERE("id = 1")
|
152
|
+
.WHERE("id = 2")
|
153
|
+
}.should.raise(I_Dig_Sql::Only_One_Where)
|
154
|
+
.message.should.match(/Multiple use of WHERE:/)
|
155
|
+
end
|
156
|
+
|
157
|
+
end # === describe #WHERE ===
|
158
|
+
|
159
|
+
|
75
160
|
describe "String#i_dig_sql" do
|
76
161
|
|
77
162
|
it "returns an I_Dig_Sql instance set to String" do
|
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.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- da99
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: "\n You probably want another gem: arel. Use that\n to generate SQL
|
56
70
|
using Ruby.\n This gem only generates SELECT and WITH (ie CTE) statements/expressions.\n
|
57
71
|
\ "
|
@@ -68,6 +82,7 @@ files:
|
|
68
82
|
- LICENSE.txt
|
69
83
|
- README.md
|
70
84
|
- Rakefile
|
85
|
+
- VERSION
|
71
86
|
- bin/test
|
72
87
|
- i_dig_sql.gemspec
|
73
88
|
- lib/i_dig_sql.rb
|