colincasey-sequel 2.10.0 → 2.10.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -1
- data/doc/advanced_associations.rdoc +614 -0
- data/doc/cheat_sheet.rdoc +223 -0
- data/doc/dataset_filtering.rdoc +158 -0
- data/doc/prepared_statements.rdoc +104 -0
- data/doc/release_notes/1.0.txt +38 -0
- data/doc/release_notes/1.1.txt +143 -0
- data/doc/release_notes/1.3.txt +101 -0
- data/doc/release_notes/1.4.0.txt +53 -0
- data/doc/release_notes/1.5.0.txt +155 -0
- data/doc/release_notes/2.0.0.txt +298 -0
- data/doc/release_notes/2.1.0.txt +271 -0
- data/doc/release_notes/2.10.0.txt +328 -0
- data/doc/release_notes/2.2.0.txt +253 -0
- data/doc/release_notes/2.3.0.txt +88 -0
- data/doc/release_notes/2.4.0.txt +106 -0
- data/doc/release_notes/2.5.0.txt +137 -0
- data/doc/release_notes/2.6.0.txt +157 -0
- data/doc/release_notes/2.7.0.txt +166 -0
- data/doc/release_notes/2.8.0.txt +171 -0
- data/doc/release_notes/2.9.0.txt +97 -0
- data/doc/schema.rdoc +29 -0
- data/doc/sharding.rdoc +113 -0
- data/lib/sequel.rb +1 -0
- data/lib/sequel_core/adapters/ado.rb +89 -0
- data/lib/sequel_core/adapters/db2.rb +143 -0
- data/lib/sequel_core/adapters/dbi.rb +112 -0
- data/lib/sequel_core/adapters/do/mysql.rb +38 -0
- data/lib/sequel_core/adapters/do/postgres.rb +92 -0
- data/lib/sequel_core/adapters/do/sqlite.rb +31 -0
- data/lib/sequel_core/adapters/do.rb +205 -0
- data/lib/sequel_core/adapters/firebird.rb +298 -0
- data/lib/sequel_core/adapters/informix.rb +85 -0
- data/lib/sequel_core/adapters/jdbc/h2.rb +69 -0
- data/lib/sequel_core/adapters/jdbc/mysql.rb +66 -0
- data/lib/sequel_core/adapters/jdbc/oracle.rb +23 -0
- data/lib/sequel_core/adapters/jdbc/postgresql.rb +113 -0
- data/lib/sequel_core/adapters/jdbc/sqlite.rb +43 -0
- data/lib/sequel_core/adapters/jdbc.rb +491 -0
- data/lib/sequel_core/adapters/mysql.rb +369 -0
- data/lib/sequel_core/adapters/odbc.rb +174 -0
- data/lib/sequel_core/adapters/openbase.rb +68 -0
- data/lib/sequel_core/adapters/oracle.rb +107 -0
- data/lib/sequel_core/adapters/postgres.rb +456 -0
- data/lib/sequel_core/adapters/shared/ms_access.rb +110 -0
- data/lib/sequel_core/adapters/shared/mssql.rb +102 -0
- data/lib/sequel_core/adapters/shared/mysql.rb +325 -0
- data/lib/sequel_core/adapters/shared/oracle.rb +61 -0
- data/lib/sequel_core/adapters/shared/postgres.rb +715 -0
- data/lib/sequel_core/adapters/shared/progress.rb +31 -0
- data/lib/sequel_core/adapters/shared/sqlite.rb +265 -0
- data/lib/sequel_core/adapters/sqlite.rb +248 -0
- data/lib/sequel_core/connection_pool.rb +258 -0
- data/lib/sequel_core/core_ext.rb +217 -0
- data/lib/sequel_core/core_sql.rb +202 -0
- data/lib/sequel_core/database/schema.rb +164 -0
- data/lib/sequel_core/database.rb +691 -0
- data/lib/sequel_core/dataset/callback.rb +13 -0
- data/lib/sequel_core/dataset/convenience.rb +237 -0
- data/lib/sequel_core/dataset/pagination.rb +96 -0
- data/lib/sequel_core/dataset/prepared_statements.rb +220 -0
- data/lib/sequel_core/dataset/query.rb +41 -0
- data/lib/sequel_core/dataset/schema.rb +15 -0
- data/lib/sequel_core/dataset/sql.rb +1010 -0
- data/lib/sequel_core/dataset/stored_procedures.rb +75 -0
- data/lib/sequel_core/dataset/unsupported.rb +43 -0
- data/lib/sequel_core/dataset.rb +511 -0
- data/lib/sequel_core/deprecated.rb +26 -0
- data/lib/sequel_core/exceptions.rb +44 -0
- data/lib/sequel_core/migration.rb +212 -0
- data/lib/sequel_core/object_graph.rb +230 -0
- data/lib/sequel_core/pretty_table.rb +71 -0
- data/lib/sequel_core/schema/generator.rb +320 -0
- data/lib/sequel_core/schema/sql.rb +325 -0
- data/lib/sequel_core/schema.rb +2 -0
- data/lib/sequel_core/sql.rb +887 -0
- data/lib/sequel_core/version.rb +11 -0
- data/lib/sequel_core.rb +172 -0
- data/lib/sequel_model/association_reflection.rb +267 -0
- data/lib/sequel_model/associations.rb +499 -0
- data/lib/sequel_model/base.rb +523 -0
- data/lib/sequel_model/caching.rb +82 -0
- data/lib/sequel_model/dataset_methods.rb +26 -0
- data/lib/sequel_model/eager_loading.rb +370 -0
- data/lib/sequel_model/exceptions.rb +7 -0
- data/lib/sequel_model/hooks.rb +101 -0
- data/lib/sequel_model/inflector.rb +281 -0
- data/lib/sequel_model/plugins.rb +62 -0
- data/lib/sequel_model/record.rb +568 -0
- data/lib/sequel_model/schema.rb +49 -0
- data/lib/sequel_model/validations.rb +429 -0
- data/lib/sequel_model.rb +91 -0
- data/spec/adapters/ado_spec.rb +46 -0
- data/spec/adapters/firebird_spec.rb +376 -0
- data/spec/adapters/informix_spec.rb +96 -0
- data/spec/adapters/mysql_spec.rb +881 -0
- data/spec/adapters/oracle_spec.rb +244 -0
- data/spec/adapters/postgres_spec.rb +687 -0
- data/spec/adapters/spec_helper.rb +10 -0
- data/spec/adapters/sqlite_spec.rb +555 -0
- data/spec/integration/dataset_test.rb +134 -0
- data/spec/integration/eager_loader_test.rb +696 -0
- data/spec/integration/prepared_statement_test.rb +130 -0
- data/spec/integration/schema_test.rb +180 -0
- data/spec/integration/spec_helper.rb +58 -0
- data/spec/integration/type_test.rb +96 -0
- data/spec/rcov.opts +6 -0
- data/spec/sequel_core/connection_pool_spec.rb +526 -0
- data/spec/sequel_core/core_ext_spec.rb +156 -0
- data/spec/sequel_core/core_sql_spec.rb +522 -0
- data/spec/sequel_core/database_spec.rb +1188 -0
- data/spec/sequel_core/dataset_spec.rb +3481 -0
- data/spec/sequel_core/expression_filters_spec.rb +363 -0
- data/spec/sequel_core/migration_spec.rb +261 -0
- data/spec/sequel_core/object_graph_spec.rb +272 -0
- data/spec/sequel_core/pretty_table_spec.rb +58 -0
- data/spec/sequel_core/schema_generator_spec.rb +167 -0
- data/spec/sequel_core/schema_spec.rb +780 -0
- data/spec/sequel_core/spec_helper.rb +55 -0
- data/spec/sequel_core/version_spec.rb +7 -0
- data/spec/sequel_model/association_reflection_spec.rb +93 -0
- data/spec/sequel_model/associations_spec.rb +1767 -0
- data/spec/sequel_model/base_spec.rb +419 -0
- data/spec/sequel_model/caching_spec.rb +215 -0
- data/spec/sequel_model/dataset_methods_spec.rb +78 -0
- data/spec/sequel_model/eager_loading_spec.rb +1165 -0
- data/spec/sequel_model/hooks_spec.rb +485 -0
- data/spec/sequel_model/inflector_spec.rb +119 -0
- data/spec/sequel_model/model_spec.rb +588 -0
- data/spec/sequel_model/plugins_spec.rb +80 -0
- data/spec/sequel_model/record_spec.rb +1184 -0
- data/spec/sequel_model/schema_spec.rb +90 -0
- data/spec/sequel_model/spec_helper.rb +78 -0
- data/spec/sequel_model/validations_spec.rb +1067 -0
- data/spec/spec.opts +0 -0
- data/spec/spec_config.rb.example +10 -0
- metadata +177 -3
@@ -0,0 +1,522 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
context "Array#all_two_pairs?" do
|
4
|
+
specify "should return false if empty" do
|
5
|
+
[].all_two_pairs?.should == false
|
6
|
+
end
|
7
|
+
|
8
|
+
specify "should return false if any of the elements is not an array" do
|
9
|
+
[1].all_two_pairs?.should == false
|
10
|
+
[[1,2],1].all_two_pairs?.should == false
|
11
|
+
end
|
12
|
+
|
13
|
+
specify "should return false if any of the elements has a length other than two" do
|
14
|
+
[[1,2],[]].all_two_pairs?.should == false
|
15
|
+
[[1,2],[1]].all_two_pairs?.should == false
|
16
|
+
[[1,2],[1,2,3]].all_two_pairs?.should == false
|
17
|
+
end
|
18
|
+
|
19
|
+
specify "should return true if all of the elements are arrays with a length of two" do
|
20
|
+
[[1,2]].all_two_pairs?.should == true
|
21
|
+
[[1,2],[1,2]].all_two_pairs?.should == true
|
22
|
+
[[1,2],[1,2],[1,2]].all_two_pairs?.should == true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "Array#case and Hash#case" do
|
27
|
+
setup do
|
28
|
+
@d = Sequel::Dataset.new(nil)
|
29
|
+
end
|
30
|
+
|
31
|
+
specify "should return SQL CASE expression" do
|
32
|
+
@d.literal({:x=>:y}.case(:z)).should == '(CASE WHEN x THEN y ELSE z END)'
|
33
|
+
@d.literal({:x=>:y}.case(:z, :exp)).should == '(CASE exp WHEN x THEN y ELSE z END)'
|
34
|
+
['(CASE WHEN x THEN y WHEN a THEN b ELSE z END)',
|
35
|
+
'(CASE WHEN a THEN b WHEN x THEN y ELSE z END)'].should(include(@d.literal({:x=>:y, :a=>:b}.case(:z))))
|
36
|
+
@d.literal([[:x, :y]].case(:z)).should == '(CASE WHEN x THEN y ELSE z END)'
|
37
|
+
@d.literal([[:x, :y], [:a, :b]].case(:z)).should == '(CASE WHEN x THEN y WHEN a THEN b ELSE z END)'
|
38
|
+
@d.literal([[:x, :y], [:a, :b]].case(:z, :exp)).should == '(CASE exp WHEN x THEN y WHEN a THEN b ELSE z END)'
|
39
|
+
@d.literal([[:x, :y], [:a, :b]].case(:z, :exp__w)).should == '(CASE exp.w WHEN x THEN y WHEN a THEN b ELSE z END)'
|
40
|
+
end
|
41
|
+
|
42
|
+
specify "should raise an error if an array that isn't all two pairs is used" do
|
43
|
+
proc{[:b].case(:a)}.should raise_error(Sequel::Error)
|
44
|
+
proc{[:b, :c].case(:a)}.should raise_error(Sequel::Error)
|
45
|
+
proc{[[:b, :c], :d].case(:a)}.should raise_error(Sequel::Error)
|
46
|
+
end
|
47
|
+
|
48
|
+
specify "should raise an error if an empty array/hash is used" do
|
49
|
+
proc{[].case(:a)}.should raise_error(Sequel::Error)
|
50
|
+
proc{{}.case(:a)}.should raise_error(Sequel::Error)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "Array#sql_array" do
|
55
|
+
setup do
|
56
|
+
@d = Sequel::Dataset.new(nil)
|
57
|
+
end
|
58
|
+
|
59
|
+
specify "should treat the array as an SQL array instead of conditions" do
|
60
|
+
@d.literal([[:x, 1], [:y, 2]]).should == '((x = 1) AND (y = 2))'
|
61
|
+
@d.literal([[:x, 1], [:y, 2]].sql_array).should == '((x, 1), (y, 2))'
|
62
|
+
@d.filter([:a, :b]=>[[:x, 1], [:y, 2]].sql_array).sql.should == 'SELECT * WHERE ((a, b) IN ((x, 1), (y, 2)))'
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "Array#to_sql" do
|
67
|
+
specify "should concatenate multiple lines into a single string" do
|
68
|
+
"SELECT * \r\nFROM items\r\n WHERE a = 1".split.to_sql. \
|
69
|
+
should == 'SELECT * FROM items WHERE a = 1'
|
70
|
+
end
|
71
|
+
|
72
|
+
specify "should remove superfluous white space and line breaks" do
|
73
|
+
"\tSELECT * \n FROM items ".split.to_sql. \
|
74
|
+
should == 'SELECT * FROM items'
|
75
|
+
end
|
76
|
+
|
77
|
+
specify "should remove ANSI SQL comments" do
|
78
|
+
"SELECT * --comment\r\n FROM items\r\n --comment".split.to_sql. \
|
79
|
+
should == 'SELECT * FROM items'
|
80
|
+
end
|
81
|
+
|
82
|
+
specify "should remove C-style comments" do
|
83
|
+
"SELECT * \r\n /* comment comment\r\n comment\r\n FROM items */\r\n FROM items\r\n--comment".split.to_sql. \
|
84
|
+
should == 'SELECT * FROM items'
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context "String#to_sql" do
|
89
|
+
specify "should concatenate multiple lines into a single string" do
|
90
|
+
"SELECT * \r\nFROM items\r\nWHERE a = 1".to_sql. \
|
91
|
+
should == 'SELECT * FROM items WHERE a = 1'
|
92
|
+
end
|
93
|
+
|
94
|
+
specify "should remove superfluous white space and line breaks" do
|
95
|
+
"\tSELECT * \r\n FROM items ".to_sql. \
|
96
|
+
should == 'SELECT * FROM items'
|
97
|
+
end
|
98
|
+
|
99
|
+
specify "should remove ANSI SQL comments" do
|
100
|
+
"SELECT * --comment \r\n FROM items\r\n --comment".to_sql. \
|
101
|
+
should == 'SELECT * FROM items'
|
102
|
+
end
|
103
|
+
|
104
|
+
specify "should remove C-style comments" do
|
105
|
+
"SELECT * \r\n/* comment comment\r\ncomment\r\nFROM items */\r\nFROM items\r\n--comment".to_sql. \
|
106
|
+
should == 'SELECT * FROM items'
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context "String#lit" do
|
111
|
+
specify "should return an LiteralString object" do
|
112
|
+
'xyz'.lit.should be_a_kind_of(Sequel::LiteralString)
|
113
|
+
'xyz'.lit.to_s.should == 'xyz'
|
114
|
+
end
|
115
|
+
|
116
|
+
specify "should inhibit string literalization" do
|
117
|
+
Sequel::Database.new[:t].update_sql(:stamp => "NOW()".expr).should == \
|
118
|
+
"UPDATE t SET stamp = NOW()"
|
119
|
+
end
|
120
|
+
|
121
|
+
specify "should be aliased as expr" do
|
122
|
+
'xyz'.expr.should be_a_kind_of(Sequel::LiteralString)
|
123
|
+
'xyz'.expr.to_s.should == 'xyz'
|
124
|
+
Sequel::Database.new[:t].update_sql(:stamp => "NOW()".expr).should == \
|
125
|
+
"UPDATE t SET stamp = NOW()"
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context "String#to_blob and #to_sequel_blob" do
|
130
|
+
specify "should return a Blob object" do
|
131
|
+
'xyz'.to_blob.should be_a_kind_of(::Sequel::SQL::Blob)
|
132
|
+
'xyz'.to_blob.should == 'xyz'
|
133
|
+
'xyz'.to_sequel_blob.should be_a_kind_of(::Sequel::SQL::Blob)
|
134
|
+
'xyz'.to_sequel_blob.should == 'xyz'
|
135
|
+
end
|
136
|
+
|
137
|
+
specify "should retain binary data" do
|
138
|
+
"\1\2\3\4".to_blob.should == "\1\2\3\4"
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
context "String#split_sql" do
|
143
|
+
specify "should split a string containing multiple statements" do
|
144
|
+
"DROP TABLE a; DROP TABLE c".split_sql.should == \
|
145
|
+
['DROP TABLE a', 'DROP TABLE c']
|
146
|
+
end
|
147
|
+
|
148
|
+
specify "should remove comments from the string" do
|
149
|
+
"DROP TABLE a;/* DROP TABLE b; DROP TABLE c;*/DROP TABLE d".split_sql.should == \
|
150
|
+
['DROP TABLE a', 'DROP TABLE d']
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
context "#desc" do
|
155
|
+
setup do
|
156
|
+
@ds = Sequel::Dataset.new(nil)
|
157
|
+
end
|
158
|
+
|
159
|
+
specify "should format a DESC clause for a column ref" do
|
160
|
+
:test.desc.to_s(@ds).should == 'test DESC'
|
161
|
+
|
162
|
+
:items__price.desc.to_s(@ds).should == 'items.price DESC'
|
163
|
+
end
|
164
|
+
|
165
|
+
specify "should format a DESC clause for a function" do
|
166
|
+
:avg.sql_function(:test).desc.to_s(@ds).should == 'avg(test) DESC'
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
context "#asc" do
|
171
|
+
setup do
|
172
|
+
@ds = Sequel::Dataset.new(nil)
|
173
|
+
end
|
174
|
+
|
175
|
+
specify "should format a ASC clause for a column ref" do
|
176
|
+
:test.asc.to_s(@ds).should == 'test ASC'
|
177
|
+
|
178
|
+
:items__price.asc.to_s(@ds).should == 'items.price ASC'
|
179
|
+
end
|
180
|
+
|
181
|
+
specify "should format a ASC clause for a function" do
|
182
|
+
:avg.sql_function(:test).asc.to_s(@ds).should == 'avg(test) ASC'
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
context "#as" do
|
187
|
+
setup do
|
188
|
+
@ds = Sequel::Dataset.new(nil)
|
189
|
+
end
|
190
|
+
|
191
|
+
specify "should format a AS clause for a column ref" do
|
192
|
+
:test.as(:t).to_s(@ds).should == 'test AS t'
|
193
|
+
|
194
|
+
:items__price.as(:p).to_s(@ds).should == 'items.price AS p'
|
195
|
+
end
|
196
|
+
|
197
|
+
specify "should format a AS clause for a function" do
|
198
|
+
:avg.sql_function(:test).as(:avg).to_s(@ds).should == 'avg(test) AS avg'
|
199
|
+
end
|
200
|
+
|
201
|
+
specify "should format a AS clause for a literal value" do
|
202
|
+
'abc'.as(:abc).to_s(@ds).should == "'abc' AS abc"
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
context "Column references" do
|
207
|
+
setup do
|
208
|
+
@c = Class.new(Sequel::Dataset) do
|
209
|
+
def quoted_identifier(c); "`#{c}`"; end
|
210
|
+
end
|
211
|
+
@ds = @c.new(MockDatabase.new)
|
212
|
+
@ds.quote_identifiers = true
|
213
|
+
end
|
214
|
+
|
215
|
+
specify "should be quoted properly" do
|
216
|
+
@ds.literal(:xyz).should == "`xyz`"
|
217
|
+
@ds.literal(:xyz__abc).should == "`xyz`.`abc`"
|
218
|
+
|
219
|
+
@ds.literal(:xyz.as(:x)).should == "`xyz` AS `x`"
|
220
|
+
@ds.literal(:xyz__abc.as(:x)).should == "`xyz`.`abc` AS `x`"
|
221
|
+
|
222
|
+
@ds.literal(:xyz___x).should == "`xyz` AS `x`"
|
223
|
+
@ds.literal(:xyz__abc___x).should == "`xyz`.`abc` AS `x`"
|
224
|
+
end
|
225
|
+
|
226
|
+
specify "should be quoted properly in SQL functions" do
|
227
|
+
@ds.literal(:avg.sql_function(:xyz)).should == "avg(`xyz`)"
|
228
|
+
@ds.literal(:avg.sql_function(:xyz, 1)).should == "avg(`xyz`, 1)"
|
229
|
+
@ds.literal(:avg.sql_function(:xyz).as(:a)).should == "avg(`xyz`) AS `a`"
|
230
|
+
end
|
231
|
+
|
232
|
+
specify "should be quoted properly in ASC/DESC clauses" do
|
233
|
+
@ds.literal(:xyz.asc).should == "`xyz` ASC"
|
234
|
+
@ds.literal(:avg.sql_function(:xyz, 1).desc).should == "avg(`xyz`, 1) DESC"
|
235
|
+
end
|
236
|
+
|
237
|
+
specify "should be quoted properly in a cast function" do
|
238
|
+
@ds.literal(:x.cast_as(:integer)).should == "CAST(`x` AS integer)"
|
239
|
+
@ds.literal(:x__y.cast_as('varchar(20)')).should == "CAST(`x`.`y` AS varchar(20))"
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
context "Blob" do
|
244
|
+
specify "#to_blob and #to_sequel_blob should return self" do
|
245
|
+
blob = "x".to_blob
|
246
|
+
blob.to_blob.object_id.should == blob.object_id
|
247
|
+
blob = "x".to_sequel_blob
|
248
|
+
blob.to_sequel_blob.object_id.should == blob.object_id
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
if RUBY_VERSION < '1.9.0'
|
253
|
+
context "Symbol#[]" do
|
254
|
+
specify "should format an SQL Function" do
|
255
|
+
ds = Sequel::Dataset.new(nil)
|
256
|
+
ds.literal(:xyz[]).should == 'xyz()'
|
257
|
+
ds.literal(:xyz[1]).should == 'xyz(1)'
|
258
|
+
ds.literal(:xyz[1, 2, :abc[3]]).should == 'xyz(1, 2, abc(3))'
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
context "Symbol#*" do
|
264
|
+
setup do
|
265
|
+
@ds = Sequel::Dataset.new(nil)
|
266
|
+
end
|
267
|
+
|
268
|
+
specify "should format a qualified wildcard if no argument" do
|
269
|
+
:xyz.*.to_s(@ds).should == 'xyz.*'
|
270
|
+
:abc.*.to_s(@ds).should == 'abc.*'
|
271
|
+
end
|
272
|
+
|
273
|
+
specify "should format a filter expression if an argument" do
|
274
|
+
:xyz.*(3).to_s(@ds).should == '(xyz * 3)'
|
275
|
+
:abc.*(5).to_s(@ds).should == '(abc * 5)'
|
276
|
+
end
|
277
|
+
|
278
|
+
specify "should support qualified symbols if no argument" do
|
279
|
+
:xyz__abc.*.to_s(@ds).should == 'xyz.abc.*'
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
context "Symbol" do
|
284
|
+
before do
|
285
|
+
@ds = Sequel::Dataset.new(nil)
|
286
|
+
@ds.quote_identifiers = true
|
287
|
+
@ds.upcase_identifiers = true
|
288
|
+
end
|
289
|
+
|
290
|
+
specify "#identifier should format an identifier" do
|
291
|
+
@ds.literal(:xyz__abc.identifier).should == '"XYZ__ABC"'
|
292
|
+
end
|
293
|
+
|
294
|
+
specify "#qualify should format a qualified column" do
|
295
|
+
@ds.literal(:xyz.qualify(:abc)).should == '"ABC"."XYZ"'
|
296
|
+
end
|
297
|
+
|
298
|
+
specify "should be able to qualify an identifier" do
|
299
|
+
@ds.literal(:xyz.identifier.qualify(:xyz__abc)).should == '"XYZ"."ABC"."XYZ"'
|
300
|
+
end
|
301
|
+
|
302
|
+
specify "should be able to specify a schema.table.column" do
|
303
|
+
@ds.literal(:column.qualify(:table.qualify(:schema))).should == '"SCHEMA"."TABLE"."COLUMN"'
|
304
|
+
@ds.literal(:column.qualify(:table__name.identifier.qualify(:schema))).should == '"SCHEMA"."TABLE__NAME"."COLUMN"'
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
context "Symbol#to_column_ref" do
|
309
|
+
setup do
|
310
|
+
@ds = MockDataset.new(nil)
|
311
|
+
end
|
312
|
+
|
313
|
+
specify "should convert qualified symbol notation into dot notation" do
|
314
|
+
:abc__def.to_column_ref(@ds).should == 'abc.def'
|
315
|
+
end
|
316
|
+
|
317
|
+
specify "should convert AS symbol notation into SQL AS notation" do
|
318
|
+
:xyz___x.to_column_ref(@ds).should == 'xyz AS x'
|
319
|
+
:abc__def___x.to_column_ref(@ds).should == 'abc.def AS x'
|
320
|
+
end
|
321
|
+
|
322
|
+
specify "should support names with digits" do
|
323
|
+
:abc2.to_column_ref(@ds).should == 'abc2'
|
324
|
+
:xx__yy3.to_column_ref(@ds).should == 'xx.yy3'
|
325
|
+
:ab34__temp3_4ax.to_column_ref(@ds).should == 'ab34.temp3_4ax'
|
326
|
+
:x1___y2.to_column_ref(@ds).should == 'x1 AS y2'
|
327
|
+
:abc2__def3___ggg4.to_column_ref(@ds).should == 'abc2.def3 AS ggg4'
|
328
|
+
end
|
329
|
+
|
330
|
+
specify "should support upper case and lower case" do
|
331
|
+
:ABC.to_column_ref(@ds).should == 'ABC'
|
332
|
+
:Zvashtoy__aBcD.to_column_ref(@ds).should == 'Zvashtoy.aBcD'
|
333
|
+
end
|
334
|
+
|
335
|
+
specify "should support spaces inside column names" do
|
336
|
+
@ds.quote_identifiers = true
|
337
|
+
:"AB C".to_column_ref(@ds).should == '"AB C"'
|
338
|
+
:"Zvas htoy__aB cD".to_column_ref(@ds).should == '"Zvas htoy"."aB cD"'
|
339
|
+
:"aB cD___XX XX".to_column_ref(@ds).should == '"aB cD" AS "XX XX"'
|
340
|
+
:"Zva shtoy__aB cD___XX XX".to_column_ref(@ds).should == '"Zva shtoy"."aB cD" AS "XX XX"'
|
341
|
+
end
|
342
|
+
end
|
343
|
+
|
344
|
+
context "Symbol" do
|
345
|
+
setup do
|
346
|
+
@ds = Sequel::Dataset.new(MockDatabase.new)
|
347
|
+
end
|
348
|
+
|
349
|
+
specify "should support upper case outer functions" do
|
350
|
+
:COUNT.sql_function('1').to_s(@ds).should == "COUNT('1')"
|
351
|
+
end
|
352
|
+
|
353
|
+
specify "should inhibit string literalization" do
|
354
|
+
db = Sequel::Database.new
|
355
|
+
ds = db[:t]
|
356
|
+
ds.select(:COUNT.sql_function('1')).sql.should == "SELECT COUNT('1') FROM t"
|
357
|
+
end
|
358
|
+
|
359
|
+
specify "should support cast method and its cast_as alias" do
|
360
|
+
:abc.cast_as(:integer).to_s(@ds).should == "CAST(abc AS integer)"
|
361
|
+
:abc.cast(:integer).to_s(@ds).should == "CAST(abc AS integer)"
|
362
|
+
end
|
363
|
+
|
364
|
+
specify "should support cast_numeric and cast_string" do
|
365
|
+
x = :abc.cast_numeric
|
366
|
+
x.should be_a_kind_of(Sequel::SQL::NumericExpression)
|
367
|
+
x.to_s(@ds).should == "CAST(abc AS integer)"
|
368
|
+
|
369
|
+
x = :abc.cast_numeric(:real)
|
370
|
+
x.should be_a_kind_of(Sequel::SQL::NumericExpression)
|
371
|
+
x.to_s(@ds).should == "CAST(abc AS real)"
|
372
|
+
|
373
|
+
x = :abc.cast_string
|
374
|
+
x.should be_a_kind_of(Sequel::SQL::StringExpression)
|
375
|
+
x.to_s(@ds).should == "CAST(abc AS varchar(255))"
|
376
|
+
|
377
|
+
x = :abc.cast_string(:varchar)
|
378
|
+
x.should be_a_kind_of(Sequel::SQL::StringExpression)
|
379
|
+
x.to_s(@ds).should == "CAST(abc AS varchar)"
|
380
|
+
end
|
381
|
+
|
382
|
+
specify "should allow database independent types when casting" do
|
383
|
+
m = MockDatabase.new
|
384
|
+
m.instance_eval do
|
385
|
+
def type_literal_base(column)
|
386
|
+
return :foo if column[:type] == Integer
|
387
|
+
return :bar if column[:type] == String
|
388
|
+
column
|
389
|
+
end
|
390
|
+
end
|
391
|
+
@ds2 = Sequel::Dataset.new(m)
|
392
|
+
:abc.cast_as(Integer).to_s(@ds).should == "CAST(abc AS integer)"
|
393
|
+
:abc.cast_as(Integer).to_s(@ds2).should == "CAST(abc AS foo)"
|
394
|
+
:abc.cast(String).to_s(@ds).should == "CAST(abc AS varchar(255))"
|
395
|
+
:abc.cast(String).to_s(@ds2).should == "CAST(abc AS bar)"
|
396
|
+
:abc.cast(String).to_s(@ds2).should == "CAST(abc AS bar)"
|
397
|
+
:abc.cast_string.to_s(@ds2).should == "CAST(abc AS bar)"
|
398
|
+
:abc.cast_string(Integer).to_s(@ds2).should == "CAST(abc AS foo)"
|
399
|
+
:abc.cast_numeric.to_s(@ds2).should == "CAST(abc AS foo)"
|
400
|
+
:abc.cast_numeric(String).to_s(@ds2).should == "CAST(abc AS bar)"
|
401
|
+
end
|
402
|
+
|
403
|
+
specify "should support subscript access using | operator" do
|
404
|
+
(:abc|1).to_s(@ds).should == 'abc[1]'
|
405
|
+
(:abc|[1]).to_s(@ds).should == 'abc[1]'
|
406
|
+
(:abc|[1, 2]).to_s(@ds).should == 'abc[1, 2]'
|
407
|
+
(:abc|1|2).to_s(@ds).should == 'abc[1, 2]'
|
408
|
+
end
|
409
|
+
|
410
|
+
specify "should support SQL EXTRACT function via #extract " do
|
411
|
+
:abc.extract(:year).to_s(@ds).should == "extract(year FROM abc)"
|
412
|
+
end
|
413
|
+
end
|
414
|
+
|
415
|
+
context "String#to_time" do
|
416
|
+
specify "should convert the string into a Time object" do
|
417
|
+
"2007-07-11".to_time.should == Time.parse("2007-07-11")
|
418
|
+
"06:30".to_time.should == Time.parse("06:30")
|
419
|
+
end
|
420
|
+
|
421
|
+
specify "should raise Error::InvalidValue for an invalid time" do
|
422
|
+
proc {'0000-00-00'.to_time}.should raise_error(Sequel::Error::InvalidValue)
|
423
|
+
end
|
424
|
+
end
|
425
|
+
|
426
|
+
context "String#to_date" do
|
427
|
+
after do
|
428
|
+
Sequel.convert_two_digit_years = true
|
429
|
+
end
|
430
|
+
|
431
|
+
specify "should convert the string into a Date object" do
|
432
|
+
"2007-07-11".to_date.should == Date.parse("2007-07-11")
|
433
|
+
end
|
434
|
+
|
435
|
+
specify "should convert 2 digit years by default" do
|
436
|
+
"July 11, 07".to_date.should == Date.parse("2007-07-11")
|
437
|
+
end
|
438
|
+
|
439
|
+
specify "should not convert 2 digit years if set not to" do
|
440
|
+
Sequel.convert_two_digit_years = false
|
441
|
+
"July 11, 07".to_date.should == Date.parse("0007-07-11")
|
442
|
+
end
|
443
|
+
|
444
|
+
specify "should raise Error::InvalidValue for an invalid date" do
|
445
|
+
proc {'0000-00-00'.to_date}.should raise_error(Sequel::Error::InvalidValue)
|
446
|
+
end
|
447
|
+
end
|
448
|
+
|
449
|
+
context "String#to_datetime" do
|
450
|
+
after do
|
451
|
+
Sequel.convert_two_digit_years = true
|
452
|
+
end
|
453
|
+
|
454
|
+
specify "should convert the string into a DateTime object" do
|
455
|
+
"2007-07-11 10:11:12a".to_datetime.should == DateTime.parse("2007-07-11 10:11:12a")
|
456
|
+
end
|
457
|
+
|
458
|
+
specify "should convert 2 digit years by default" do
|
459
|
+
"July 11, 07 10:11:12a".to_datetime.should == DateTime.parse("2007-07-11 10:11:12a")
|
460
|
+
end
|
461
|
+
|
462
|
+
specify "should not convert 2 digit years if set not to" do
|
463
|
+
Sequel.convert_two_digit_years = false
|
464
|
+
"July 11, 07 10:11:12a".to_datetime.should == DateTime.parse("0007-07-11 10:11:12a")
|
465
|
+
end
|
466
|
+
|
467
|
+
specify "should raise Error::InvalidValue for an invalid date" do
|
468
|
+
proc {'0000-00-00'.to_datetime}.should raise_error(Sequel::Error::InvalidValue)
|
469
|
+
end
|
470
|
+
end
|
471
|
+
|
472
|
+
context "String#to_sequel_time" do
|
473
|
+
after do
|
474
|
+
Sequel.datetime_class = Time
|
475
|
+
Sequel.convert_two_digit_years = true
|
476
|
+
end
|
477
|
+
|
478
|
+
specify "should convert the string into a Time object by default" do
|
479
|
+
"2007-07-11 10:11:12a".to_sequel_time.class.should == Time
|
480
|
+
"2007-07-11 10:11:12a".to_sequel_time.should == Time.parse("2007-07-11 10:11:12a")
|
481
|
+
end
|
482
|
+
|
483
|
+
specify "should convert the string into a DateTime object if that is set" do
|
484
|
+
Sequel.datetime_class = DateTime
|
485
|
+
"2007-07-11 10:11:12a".to_sequel_time.class.should == DateTime
|
486
|
+
"2007-07-11 10:11:12a".to_sequel_time.should == DateTime.parse("2007-07-11 10:11:12a")
|
487
|
+
end
|
488
|
+
|
489
|
+
specify "should convert 2 digit years by default if using DateTime class" do
|
490
|
+
Sequel.datetime_class = DateTime
|
491
|
+
"July 11, 07 10:11:12a".to_sequel_time.should == DateTime.parse("2007-07-11 10:11:12a")
|
492
|
+
end
|
493
|
+
|
494
|
+
specify "should not convert 2 digit years if set not to when using DateTime class" do
|
495
|
+
Sequel.datetime_class = DateTime
|
496
|
+
Sequel.convert_two_digit_years = false
|
497
|
+
"July 11, 07 10:11:12a".to_sequel_time.should == DateTime.parse("0007-07-11 10:11:12a")
|
498
|
+
end
|
499
|
+
|
500
|
+
specify "should raise Error::InvalidValue for an invalid time" do
|
501
|
+
proc {'0000-00-00'.to_sequel_time}.should raise_error(Sequel::Error::InvalidValue)
|
502
|
+
Sequel.datetime_class = DateTime
|
503
|
+
proc {'0000-00-00'.to_sequel_time}.should raise_error(Sequel::Error::InvalidValue)
|
504
|
+
end
|
505
|
+
end
|
506
|
+
|
507
|
+
context "Sequel::SQL::Function#==" do
|
508
|
+
specify "should be true for functions with the same name and arguments, false otherwise" do
|
509
|
+
a = :date.sql_function(:t)
|
510
|
+
b = :date.sql_function(:t)
|
511
|
+
a.should == b
|
512
|
+
(a == b).should == true
|
513
|
+
c = :date.sql_function(:c)
|
514
|
+
a.should_not == c
|
515
|
+
(a == c).should == false
|
516
|
+
d = :time.sql_function(:c)
|
517
|
+
a.should_not == d
|
518
|
+
c.should_not == d
|
519
|
+
(a == d).should == false
|
520
|
+
(c == d).should == false
|
521
|
+
end
|
522
|
+
end
|