sqldsl 1.4.0 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +12 -6
- data/lib/and_where_builder.rb +13 -0
- data/lib/blank_slate.rb +1 -1
- data/lib/inner_join_builder.rb +1 -1
- data/lib/receive_any.rb +5 -0
- data/lib/select.rb +15 -1
- data/lib/sql_statement.rb +14 -1
- data/lib/sqldsl.rb +1 -0
- data/lib/where_builder.rb +9 -0
- data/rakefile.rb +1 -1
- data/test/receive_any_test.rb +16 -0
- data/test/select_acceptance_test.rb +9 -4
- data/test/where_builder_test.rb +7 -0
- metadata +3 -2
data/README
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
SQL DSL is a library for creating SQL statements using ruby code.
|
4
4
|
|
5
|
-
by Jay[http://jayfields.
|
5
|
+
by Jay[http://blog.jayfields.com] Fields[http://blog.jayfields.com]
|
6
6
|
|
7
7
|
== Download and Installation
|
8
8
|
|
@@ -86,6 +86,7 @@ See the tests for more examples
|
|
86
86
|
less_than_or_equal :column3, :column4
|
87
87
|
greater_than :column1, 0
|
88
88
|
greater_than_or_equal :column2, 'bar'
|
89
|
+
like :column1, 'any'
|
89
90
|
is_not_null :column1
|
90
91
|
is_in :column1, [1,2]
|
91
92
|
is_not_in :column2, [3, 4]
|
@@ -94,7 +95,7 @@ See the tests for more examples
|
|
94
95
|
end
|
95
96
|
expected = "select column1, 'book', 10 from table1, table2
|
96
97
|
where column1 = 99 and column1 <> 100 and column2 < 'foo'
|
97
|
-
and column3 <= column4 and column1 > 0 and column2 >= 'bar'
|
98
|
+
and column3 <= column4 and column1 > 0 and column2 >= 'bar' and column1 like 'any'
|
98
99
|
and column1 is not null and column1 in (1, 2) and column2 not in (3, 4)
|
99
100
|
and exists (0) and not exists (0)"
|
100
101
|
assert_equal expected.delete("\n").squeeze(" "), statement.to_sql
|
@@ -108,13 +109,14 @@ See the tests for more examples
|
|
108
109
|
column3 <= column4
|
109
110
|
column1 > 0
|
110
111
|
column2 >= 'bar'
|
112
|
+
column1 =~ 'any'
|
111
113
|
column1 ^ nil
|
112
114
|
column1 >> [1,2]
|
113
115
|
column2 << [3, 4]
|
114
116
|
end
|
115
117
|
expected = "select column1, 'book', 10 from table1, table2
|
116
118
|
where column1 = 99 and column1 <> 100 and column2 < 'foo'
|
117
|
-
and column3 <= column4 and column1 > 0 and column2 >= 'bar'
|
119
|
+
and column3 <= column4 and column1 > 0 and column2 >= 'bar' and column1 like 'any'
|
118
120
|
and column1 is not null and column1 in (1, 2) and column2 not in (3, 4)"
|
119
121
|
assert_equal expected.delete("\n").squeeze(" "), statement.to_sql
|
120
122
|
end
|
@@ -127,13 +129,14 @@ See the tests for more examples
|
|
127
129
|
column3.less_than_or_equal column4
|
128
130
|
column1.greater_than 0
|
129
131
|
column2.greater_than_or_equal 'bar'
|
132
|
+
column1.like 'any'
|
130
133
|
column1.is_not_null
|
131
134
|
column1.is_in [1,2]
|
132
135
|
column2.is_not_in [3, 4]
|
133
136
|
end
|
134
137
|
expected = "select column1, 'book', 10 from table1, table2
|
135
138
|
where column1 = 99 and column1 <> 100 and column2 < 'foo'
|
136
|
-
and column3 <= column4 and column1 > 0 and column2 >= 'bar'
|
139
|
+
and column3 <= column4 and column1 > 0 and column2 >= 'bar' and column1 like 'any'
|
137
140
|
and column1 is not null and column1 in (1, 2) and column2 not in (3, 4)"
|
138
141
|
assert_equal expected.delete("\n").squeeze(" "), statement.to_sql
|
139
142
|
end
|
@@ -143,8 +146,10 @@ See the tests for more examples
|
|
143
146
|
column1.equal 0
|
144
147
|
end.or do
|
145
148
|
column1 > 100
|
149
|
+
end.and do
|
150
|
+
column2 == 15
|
146
151
|
end
|
147
|
-
expected = "select column1, 'book', 10 from table1, table2 where column1 = 0 or (column1 > 100)"
|
152
|
+
expected = "select column1, 'book', 10 from table1, table2 where column1 = 0 or (column1 > 100) and (column2 = 15)"
|
148
153
|
assert_equal expected, statement.to_sql
|
149
154
|
end
|
150
155
|
|
@@ -190,4 +195,5 @@ See the tests for more examples
|
|
190
195
|
end
|
191
196
|
|
192
197
|
=== Contributors
|
193
|
-
Matt Deiters
|
198
|
+
Matt Deiters[http://www.theagiledeveloper.com]
|
199
|
+
Sergio Espeja[http://spejman-on-rails.blogspot.com/]
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class AndWhereBuilder < WhereBuilder
|
2
|
+
# call-seq: and_where.to_sql -> a_string
|
3
|
+
#
|
4
|
+
# Returns a string by collecting all the conditions and joins them with ' and '.
|
5
|
+
#
|
6
|
+
# AndWhereBuilder.new [] do
|
7
|
+
# equal :column1, 10
|
8
|
+
# equal :column2, 'book'
|
9
|
+
# end.to_sql #=> " and (column1 = 10 and column2 = 'book')"
|
10
|
+
def to_sql
|
11
|
+
" and (#{sql_parts.join(' and ')})"
|
12
|
+
end
|
13
|
+
end
|
data/lib/blank_slate.rb
CHANGED
data/lib/inner_join_builder.rb
CHANGED
data/lib/receive_any.rb
CHANGED
data/lib/select.rb
CHANGED
@@ -68,11 +68,17 @@ class Select < SqlStatement
|
|
68
68
|
self
|
69
69
|
end
|
70
70
|
|
71
|
+
# call-seq: select.inner_join -> an_inner_join_builder
|
72
|
+
#
|
73
|
+
# Returns an InnerJoinBuilder instance.
|
74
|
+
#
|
75
|
+
# Select.all.from[:table1].inner_join
|
76
|
+
# #=> #<InnerJoinBuilder:0x654f4 @select_builder=#<Select:0x65968 @tables=[:table1], @to_sql="select * from table1">>
|
71
77
|
def inner_join
|
72
78
|
InnerJoinBuilder.new(self)
|
73
79
|
end
|
74
80
|
|
75
|
-
def inner_join_table(table_name)
|
81
|
+
def inner_join_table(table_name) #:nodoc:
|
76
82
|
@to_sql << " inner join "
|
77
83
|
if table_name.to_s =~ / as /
|
78
84
|
@tables << table_name.to_s.split(/ as /).last.to_sym
|
@@ -84,6 +90,14 @@ class Select < SqlStatement
|
|
84
90
|
self
|
85
91
|
end
|
86
92
|
|
93
|
+
# call-seq: sql_statement.on { block } -> a_sql_statement
|
94
|
+
#
|
95
|
+
# Creates a new OnWhereBuilder instance, passing the block as a parameter, then executes to_sql on the OnWhereBuilder instance.
|
96
|
+
# The resulting string from the OnWhereBuilder instance is appended to the SQL statement.
|
97
|
+
# Returns self.
|
98
|
+
#
|
99
|
+
# Select.all.from[:table1].inner_join[:table2].on { equal :table1.column1, :table2.column1 }.to_sql
|
100
|
+
# #=> "select * from table1 inner join table2 on table1.column1 = table2.column2"
|
87
101
|
def on(&block)
|
88
102
|
@to_sql += OnWhereBuilder.new(self.tables, &block).to_sql
|
89
103
|
self
|
data/lib/sql_statement.rb
CHANGED
@@ -24,9 +24,22 @@ class SqlStatement
|
|
24
24
|
# The resulting string from the OrWhereBuilder instance is appended to the SQL statement.
|
25
25
|
# Returns self.
|
26
26
|
#
|
27
|
-
# Select[1].where { equal :column1, 1 }.or { equal :column1, 100 }.to_sql #=> "select 1 where column1 = 1 or column1 = 100"
|
27
|
+
# Select[1].where { equal :column1, 1 }.or { equal :column1, 100 }.to_sql #=> "select 1 where column1 = 1 or (column1 = 100)"
|
28
28
|
def or(&block)
|
29
29
|
@to_sql += OrWhereBuilder.new(self.tables, &block).to_sql
|
30
30
|
self
|
31
31
|
end
|
32
|
+
|
33
|
+
# call-seq: sql_statement.and { block } -> a_sql_statement
|
34
|
+
#
|
35
|
+
# Creates a new AndWhereBuilder instance, passing the block as a parameter, then executes to_sql on the AndWhereBuilder instance.
|
36
|
+
# The resulting string from the AndWhereBuilder instance is appended to the SQL statement.
|
37
|
+
# Returns self.
|
38
|
+
#
|
39
|
+
# Select[1].where { equal :column1, 1 }.and { equal :column2, 100 }.to_sql #=> "select 1 where column1 = 1 and (column2 = 100)"
|
40
|
+
def and(&block)
|
41
|
+
@to_sql += AndWhereBuilder.new(self.tables, &block).to_sql
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
32
45
|
end
|
data/lib/sqldsl.rb
CHANGED
@@ -8,6 +8,7 @@ require File.dirname(__FILE__) + '/time.rb'
|
|
8
8
|
require File.dirname(__FILE__) + '/sql_statement.rb'
|
9
9
|
require File.dirname(__FILE__) + '/inner_join_builder.rb'
|
10
10
|
require File.dirname(__FILE__) + '/where_builder.rb'
|
11
|
+
require File.dirname(__FILE__) + '/and_where_builder.rb'
|
11
12
|
require File.dirname(__FILE__) + '/or_where_builder.rb'
|
12
13
|
require File.dirname(__FILE__) + '/on_where_builder.rb'
|
13
14
|
require File.dirname(__FILE__) + '/select.rb'
|
data/lib/where_builder.rb
CHANGED
@@ -93,6 +93,15 @@ class WhereBuilder
|
|
93
93
|
def is_not_null(column)
|
94
94
|
sql_parts << "#{column.to_sql} is not null"
|
95
95
|
end
|
96
|
+
|
97
|
+
# call-seq: where.like(arg1, arg2)
|
98
|
+
#
|
99
|
+
# Appends a like condition to the where SQL clause.
|
100
|
+
#
|
101
|
+
# where { like :column1, 'any' }.to_sql #=> " where column1 like 'any'"
|
102
|
+
def like(lval, rval)
|
103
|
+
add_condition(lval, "like", rval)
|
104
|
+
end
|
96
105
|
|
97
106
|
# call-seq: where.exists(clause)
|
98
107
|
#
|
data/rakefile.rb
CHANGED
@@ -27,7 +27,7 @@ Gem::manage_gems
|
|
27
27
|
specification = Gem::Specification.new do |s|
|
28
28
|
s.name = "sqldsl"
|
29
29
|
s.summary = "A DSL for creating SQL Statements"
|
30
|
-
s.version = "1.4.
|
30
|
+
s.version = "1.4.1"
|
31
31
|
s.author = 'Jay Fields'
|
32
32
|
s.description = "A DSL for creating SQL Statements"
|
33
33
|
s.email = 'sqldsl-developer@rubyforge.org'
|
data/test/receive_any_test.rb
CHANGED
@@ -157,4 +157,20 @@ class ReceiveAnyTest < Test::Unit::TestCase
|
|
157
157
|
end
|
158
158
|
ReceiveAny.new(:foo, builder) ^ nil
|
159
159
|
end
|
160
|
+
|
161
|
+
def test_like
|
162
|
+
builder = mock
|
163
|
+
builder.expects(:like).with do |lval, rval|
|
164
|
+
lval.class == ReceiveAny && rval == "any"
|
165
|
+
end
|
166
|
+
ReceiveAny.new(:foo, builder).like "any"
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_like
|
170
|
+
builder = mock
|
171
|
+
builder.expects(:like).with do |lval, rval|
|
172
|
+
lval.class == ReceiveAny && rval == "any"
|
173
|
+
end
|
174
|
+
ReceiveAny.new(:foo, builder) =~ "any"
|
175
|
+
end
|
160
176
|
end
|
@@ -9,6 +9,7 @@ class SelectAcceptanceTest < Test::Unit::TestCase
|
|
9
9
|
less_than_or_equal :column3, :column4
|
10
10
|
greater_than :column1, 0
|
11
11
|
greater_than_or_equal :column2, 'bar'
|
12
|
+
like :column1, 'any'
|
12
13
|
is_not_null :column1
|
13
14
|
is_in :column1, [1,2]
|
14
15
|
is_not_in :column2, [3, 4]
|
@@ -17,7 +18,7 @@ class SelectAcceptanceTest < Test::Unit::TestCase
|
|
17
18
|
end
|
18
19
|
expected = "select column1, 'book', 10 from table1, table2
|
19
20
|
where column1 = 99 and column1 <> 100 and column2 < 'foo'
|
20
|
-
and column3 <= column4 and column1 > 0 and column2 >= 'bar'
|
21
|
+
and column3 <= column4 and column1 > 0 and column2 >= 'bar' and column1 like 'any'
|
21
22
|
and column1 is not null and column1 in (1, 2) and column2 not in (3, 4)
|
22
23
|
and exists (0) and not exists (0)"
|
23
24
|
assert_equal expected.delete("\n").squeeze(" "), statement.to_sql
|
@@ -31,13 +32,14 @@ class SelectAcceptanceTest < Test::Unit::TestCase
|
|
31
32
|
column3 <= column4
|
32
33
|
column1 > 0
|
33
34
|
column2 >= 'bar'
|
35
|
+
column1 =~ 'any'
|
34
36
|
column1 ^ nil
|
35
37
|
column1 >> [1,2]
|
36
38
|
column2 << [3, 4]
|
37
39
|
end
|
38
40
|
expected = "select column1, 'book', 10 from table1, table2
|
39
41
|
where column1 = 99 and column1 <> 100 and column2 < 'foo'
|
40
|
-
and column3 <= column4 and column1 > 0 and column2 >= 'bar'
|
42
|
+
and column3 <= column4 and column1 > 0 and column2 >= 'bar' and column1 like 'any'
|
41
43
|
and column1 is not null and column1 in (1, 2) and column2 not in (3, 4)"
|
42
44
|
assert_equal expected.delete("\n").squeeze(" "), statement.to_sql
|
43
45
|
end
|
@@ -50,13 +52,14 @@ class SelectAcceptanceTest < Test::Unit::TestCase
|
|
50
52
|
column3.less_than_or_equal column4
|
51
53
|
column1.greater_than 0
|
52
54
|
column2.greater_than_or_equal 'bar'
|
55
|
+
column1.like 'any'
|
53
56
|
column1.is_not_null
|
54
57
|
column1.is_in [1,2]
|
55
58
|
column2.is_not_in [3, 4]
|
56
59
|
end
|
57
60
|
expected = "select column1, 'book', 10 from table1, table2
|
58
61
|
where column1 = 99 and column1 <> 100 and column2 < 'foo'
|
59
|
-
and column3 <= column4 and column1 > 0 and column2 >= 'bar'
|
62
|
+
and column3 <= column4 and column1 > 0 and column2 >= 'bar' and column1 like 'any'
|
60
63
|
and column1 is not null and column1 in (1, 2) and column2 not in (3, 4)"
|
61
64
|
assert_equal expected.delete("\n").squeeze(" "), statement.to_sql
|
62
65
|
end
|
@@ -66,8 +69,10 @@ class SelectAcceptanceTest < Test::Unit::TestCase
|
|
66
69
|
column1.equal 0
|
67
70
|
end.or do
|
68
71
|
column1 > 100
|
72
|
+
end.and do
|
73
|
+
column2 == 15
|
69
74
|
end
|
70
|
-
expected = "select column1, 'book', 10 from table1, table2 where column1 = 0 or (column1 > 100)"
|
75
|
+
expected = "select column1, 'book', 10 from table1, table2 where column1 = 0 or (column1 > 100) and (column2 = 15)"
|
71
76
|
assert_equal expected, statement.to_sql
|
72
77
|
end
|
73
78
|
|
data/test/where_builder_test.rb
CHANGED
@@ -51,6 +51,13 @@ class WhereBuilderTest < Test::Unit::TestCase
|
|
51
51
|
assert_equal " where column1 in (1, 2)", statement.to_sql
|
52
52
|
end
|
53
53
|
|
54
|
+
def test_like
|
55
|
+
statement = WhereBuilder.new [] do
|
56
|
+
like :column1, "any"
|
57
|
+
end
|
58
|
+
assert_equal " where column1 like 'any'", statement.to_sql
|
59
|
+
end
|
60
|
+
|
54
61
|
def test_is_not_in_where_criteria
|
55
62
|
statement = WhereBuilder.new [] do
|
56
63
|
is_not_in :column1, [1,2]
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: sqldsl
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 1.4.
|
7
|
-
date: 2007-03-
|
6
|
+
version: 1.4.1
|
7
|
+
date: 2007-03-25 00:00:00 -04:00
|
8
8
|
summary: A DSL for creating SQL Statements
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -28,6 +28,7 @@ cert_chain:
|
|
28
28
|
authors:
|
29
29
|
- Jay Fields
|
30
30
|
files:
|
31
|
+
- lib/and_where_builder.rb
|
31
32
|
- lib/array.rb
|
32
33
|
- lib/blank_slate.rb
|
33
34
|
- lib/delete.rb
|