sequel 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. data/CHANGELOG +1551 -4
  2. data/README +306 -19
  3. data/Rakefile +84 -56
  4. data/bin/sequel +106 -0
  5. data/doc/cheat_sheet.rdoc +225 -0
  6. data/doc/dataset_filtering.rdoc +182 -0
  7. data/lib/sequel_core.rb +136 -0
  8. data/lib/sequel_core/adapters/adapter_skeleton.rb +54 -0
  9. data/lib/sequel_core/adapters/ado.rb +80 -0
  10. data/lib/sequel_core/adapters/db2.rb +148 -0
  11. data/lib/sequel_core/adapters/dbi.rb +117 -0
  12. data/lib/sequel_core/adapters/informix.rb +78 -0
  13. data/lib/sequel_core/adapters/jdbc.rb +186 -0
  14. data/lib/sequel_core/adapters/jdbc/mysql.rb +55 -0
  15. data/lib/sequel_core/adapters/jdbc/postgresql.rb +66 -0
  16. data/lib/sequel_core/adapters/jdbc/sqlite.rb +47 -0
  17. data/lib/sequel_core/adapters/mysql.rb +231 -0
  18. data/lib/sequel_core/adapters/odbc.rb +155 -0
  19. data/lib/sequel_core/adapters/odbc_mssql.rb +106 -0
  20. data/lib/sequel_core/adapters/openbase.rb +64 -0
  21. data/lib/sequel_core/adapters/oracle.rb +170 -0
  22. data/lib/sequel_core/adapters/postgres.rb +199 -0
  23. data/lib/sequel_core/adapters/shared/mysql.rb +275 -0
  24. data/lib/sequel_core/adapters/shared/postgres.rb +351 -0
  25. data/lib/sequel_core/adapters/shared/sqlite.rb +146 -0
  26. data/lib/sequel_core/adapters/sqlite.rb +138 -0
  27. data/lib/sequel_core/connection_pool.rb +194 -0
  28. data/lib/sequel_core/core_ext.rb +203 -0
  29. data/lib/sequel_core/core_sql.rb +184 -0
  30. data/lib/sequel_core/database.rb +471 -0
  31. data/lib/sequel_core/database/schema.rb +156 -0
  32. data/lib/sequel_core/dataset.rb +457 -0
  33. data/lib/sequel_core/dataset/callback.rb +13 -0
  34. data/lib/sequel_core/dataset/convenience.rb +245 -0
  35. data/lib/sequel_core/dataset/pagination.rb +96 -0
  36. data/lib/sequel_core/dataset/query.rb +41 -0
  37. data/lib/sequel_core/dataset/schema.rb +15 -0
  38. data/lib/sequel_core/dataset/sql.rb +889 -0
  39. data/lib/sequel_core/deprecated.rb +26 -0
  40. data/lib/sequel_core/exceptions.rb +42 -0
  41. data/lib/sequel_core/migration.rb +187 -0
  42. data/lib/sequel_core/object_graph.rb +216 -0
  43. data/lib/sequel_core/pretty_table.rb +71 -0
  44. data/lib/sequel_core/schema.rb +2 -0
  45. data/lib/sequel_core/schema/generator.rb +239 -0
  46. data/lib/sequel_core/schema/sql.rb +325 -0
  47. data/lib/sequel_core/sql.rb +812 -0
  48. data/lib/sequel_model.rb +5 -1
  49. data/lib/sequel_model/association_reflection.rb +3 -8
  50. data/lib/sequel_model/base.rb +15 -10
  51. data/lib/sequel_model/inflector.rb +3 -5
  52. data/lib/sequel_model/plugins.rb +1 -1
  53. data/lib/sequel_model/record.rb +11 -3
  54. data/lib/sequel_model/schema.rb +4 -4
  55. data/lib/sequel_model/validations.rb +6 -1
  56. data/spec/adapters/ado_spec.rb +17 -0
  57. data/spec/adapters/informix_spec.rb +96 -0
  58. data/spec/adapters/mysql_spec.rb +764 -0
  59. data/spec/adapters/oracle_spec.rb +222 -0
  60. data/spec/adapters/postgres_spec.rb +441 -0
  61. data/spec/adapters/spec_helper.rb +7 -0
  62. data/spec/adapters/sqlite_spec.rb +400 -0
  63. data/spec/integration/dataset_test.rb +51 -0
  64. data/spec/integration/eager_loader_test.rb +702 -0
  65. data/spec/integration/schema_test.rb +102 -0
  66. data/spec/integration/spec_helper.rb +44 -0
  67. data/spec/integration/type_test.rb +43 -0
  68. data/spec/rcov.opts +2 -0
  69. data/spec/sequel_core/connection_pool_spec.rb +363 -0
  70. data/spec/sequel_core/core_ext_spec.rb +156 -0
  71. data/spec/sequel_core/core_sql_spec.rb +427 -0
  72. data/spec/sequel_core/database_spec.rb +964 -0
  73. data/spec/sequel_core/dataset_spec.rb +2977 -0
  74. data/spec/sequel_core/expression_filters_spec.rb +346 -0
  75. data/spec/sequel_core/migration_spec.rb +261 -0
  76. data/spec/sequel_core/object_graph_spec.rb +234 -0
  77. data/spec/sequel_core/pretty_table_spec.rb +58 -0
  78. data/spec/sequel_core/schema_generator_spec.rb +122 -0
  79. data/spec/sequel_core/schema_spec.rb +497 -0
  80. data/spec/sequel_core/spec_helper.rb +51 -0
  81. data/spec/{association_reflection_spec.rb → sequel_model/association_reflection_spec.rb} +6 -6
  82. data/spec/{associations_spec.rb → sequel_model/associations_spec.rb} +47 -18
  83. data/spec/{base_spec.rb → sequel_model/base_spec.rb} +2 -1
  84. data/spec/{caching_spec.rb → sequel_model/caching_spec.rb} +0 -0
  85. data/spec/{dataset_methods_spec.rb → sequel_model/dataset_methods_spec.rb} +13 -1
  86. data/spec/{eager_loading_spec.rb → sequel_model/eager_loading_spec.rb} +75 -14
  87. data/spec/{hooks_spec.rb → sequel_model/hooks_spec.rb} +4 -4
  88. data/spec/sequel_model/inflector_spec.rb +119 -0
  89. data/spec/{model_spec.rb → sequel_model/model_spec.rb} +30 -11
  90. data/spec/{plugins_spec.rb → sequel_model/plugins_spec.rb} +0 -0
  91. data/spec/{record_spec.rb → sequel_model/record_spec.rb} +47 -6
  92. data/spec/{schema_spec.rb → sequel_model/schema_spec.rb} +18 -4
  93. data/spec/{spec_helper.rb → sequel_model/spec_helper.rb} +3 -2
  94. data/spec/{validations_spec.rb → sequel_model/validations_spec.rb} +37 -17
  95. data/spec/spec_config.rb +9 -0
  96. data/spec/spec_config.rb.example +10 -0
  97. metadata +110 -37
  98. data/spec/inflector_spec.rb +0 -34
@@ -0,0 +1,346 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ context "Blockless Ruby Filters" do
4
+ before do
5
+ db = Sequel::Database.new
6
+ db.quote_identifiers = false
7
+ @d = db[:items]
8
+ def @d.l(*args, &block)
9
+ literal(filter_expr(*args, &block))
10
+ end
11
+ def @d.lit(*args)
12
+ literal(*args)
13
+ end
14
+ end
15
+
16
+ it "should support boolean columns directly" do
17
+ @d.l(:x).should == 'x'
18
+ end
19
+
20
+ it "should support NOT via Symbol#~" do
21
+ @d.l(~:x).should == 'NOT x'
22
+ end
23
+
24
+ it "should support qualified columns" do
25
+ @d.l(:x__y).should == 'x.y'
26
+ @d.l(~:x__y).should == 'NOT x.y'
27
+ end
28
+
29
+ it "should support NOT with SQL functions" do
30
+ @d.l(~:is_blah[]).should == 'NOT is_blah()'
31
+ @d.l(~:is_blah[:x]).should == 'NOT is_blah(x)'
32
+ @d.l(~:is_blah[:x__y]).should == 'NOT is_blah(x.y)'
33
+ @d.l(~:is_blah[:x, :x__y]).should == 'NOT is_blah(x, x.y)'
34
+ end
35
+
36
+ it "should handle multiple ~" do
37
+ @d.l(~~:x).should == 'x'
38
+ @d.l(~~~:x).should == 'NOT x'
39
+ @d.l(~~(:x > 100)).should == '(x > 100)'
40
+ @d.l(~~(:x & :y)).should == '(x AND y)'
41
+ @d.l(~~(:x | :y)).should == '(x OR y)'
42
+ end
43
+
44
+ it "should support >, <, >=, and <= via Symbol#>,<,>=,<=" do
45
+ @d.l(:x > 100).should == '(x > 100)'
46
+ @d.l(:x < 100.01).should == '(x < 100.01)'
47
+ @d.l(:x >= 100000000000000000000000000000000000).should == '(x >= 100000000000000000000000000000000000)'
48
+ @d.l(:x <= 100).should == '(x <= 100)'
49
+ end
50
+
51
+ it "should support negation of >, <, >=, and <= via Symbol#~" do
52
+ @d.l(~(:x > 100)).should == '(x <= 100)'
53
+ @d.l(~(:x < 100.01)).should == '(x >= 100.01)'
54
+ @d.l(~(:x >= 100000000000000000000000000000000000)).should == '(x < 100000000000000000000000000000000000)'
55
+ @d.l(~(:x <= 100)).should == '(x > 100)'
56
+ end
57
+
58
+ it "should support = via Hash" do
59
+ @d.l(:x => 100).should == '(x = 100)'
60
+ @d.l(:x => 'a').should == '(x = \'a\')'
61
+ @d.l(:x => true).should == '(x = \'t\')'
62
+ @d.l(:x => false).should == '(x = \'f\')'
63
+ @d.l(:x => nil).should == '(x IS NULL)'
64
+ @d.l(:x => [1,2,3]).should == '(x IN (1, 2, 3))'
65
+ end
66
+
67
+ it "should support != via Hash#~" do
68
+ @d.l(~{:x => 100}).should == '(x != 100)'
69
+ @d.l(~{:x => 'a'}).should == '(x != \'a\')'
70
+ @d.l(~{:x => true}).should == '(x != \'t\')'
71
+ @d.l(~{:x => false}).should == '(x != \'f\')'
72
+ @d.l(~{:x => nil}).should == '(x IS NOT NULL)'
73
+ end
74
+
75
+ it "should support ~ via Hash and Regexp (if supported by database)" do
76
+ @d.l(:x => /blah/).should == '(x ~ \'blah\')'
77
+ end
78
+
79
+ it "should support !~ via Hash#~ and Regexp" do
80
+ @d.l(~{:x => /blah/}).should == '(x !~ \'blah\')'
81
+ end
82
+
83
+ it "should support LIKE via Symbol#like" do
84
+ @d.l(:x.like('a')).should == '(x LIKE \'a\')'
85
+ @d.l(:x.like(/a/)).should == '(x ~ \'a\')'
86
+ @d.l(:x.like('a', 'b')).should == '((x LIKE \'a\') OR (x LIKE \'b\'))'
87
+ @d.l(:x.like(/a/, /b/i)).should == '((x ~ \'a\') OR (x ~* \'b\'))'
88
+ @d.l(:x.like('a', /b/)).should == '((x LIKE \'a\') OR (x ~ \'b\'))'
89
+ end
90
+
91
+ it "should support NOT LIKE via Symbol#like and Symbol#~" do
92
+ @d.l(~:x.like('a')).should == '(x NOT LIKE \'a\')'
93
+ @d.l(~:x.like(/a/)).should == '(x !~ \'a\')'
94
+ @d.l(~:x.like('a', 'b')).should == '((x NOT LIKE \'a\') AND (x NOT LIKE \'b\'))'
95
+ @d.l(~:x.like(/a/, /b/i)).should == '((x !~ \'a\') AND (x !~* \'b\'))'
96
+ @d.l(~:x.like('a', /b/)).should == '((x NOT LIKE \'a\') AND (x !~ \'b\'))'
97
+ end
98
+
99
+ it "should support ILIKE via Symbol#ilike" do
100
+ @d.l(:x.ilike('a')).should == '(x ILIKE \'a\')'
101
+ @d.l(:x.ilike(/a/)).should == '(x ~* \'a\')'
102
+ @d.l(:x.ilike('a', 'b')).should == '((x ILIKE \'a\') OR (x ILIKE \'b\'))'
103
+ @d.l(:x.ilike(/a/, /b/i)).should == '((x ~* \'a\') OR (x ~* \'b\'))'
104
+ @d.l(:x.ilike('a', /b/)).should == '((x ILIKE \'a\') OR (x ~* \'b\'))'
105
+ end
106
+
107
+ it "should support NOT ILIKE via Symbol#ilike and Symbol#~" do
108
+ @d.l(~:x.ilike('a')).should == '(x NOT ILIKE \'a\')'
109
+ @d.l(~:x.ilike(/a/)).should == '(x !~* \'a\')'
110
+ @d.l(~:x.ilike('a', 'b')).should == '((x NOT ILIKE \'a\') AND (x NOT ILIKE \'b\'))'
111
+ @d.l(~:x.ilike(/a/, /b/i)).should == '((x !~* \'a\') AND (x !~* \'b\'))'
112
+ @d.l(~:x.ilike('a', /b/)).should == '((x NOT ILIKE \'a\') AND (x !~* \'b\'))'
113
+ end
114
+
115
+ it "should support negating ranges via Hash#~ and Range" do
116
+ @d.l(~{:x => 1..5}).should == '((x < 1) OR (x > 5))'
117
+ @d.l(~{:x => 1...5}).should == '((x < 1) OR (x >= 5))'
118
+ end
119
+
120
+ it "should support negating NOT IN via Hash#~ and Dataset or Array" do
121
+ @d.l(~{:x => @d.select(:i)}).should == '(x NOT IN (SELECT i FROM items))'
122
+ @d.l(~{:x => [1,2,3]}).should == '(x NOT IN (1, 2, 3))'
123
+ end
124
+
125
+ it "should support + - * / via Symbol#+,-,*,/" do
126
+ @d.l(:x + 1 > 100).should == '((x + 1) > 100)'
127
+ @d.l((:x * :y) < 100.01).should == '((x * y) < 100.01)'
128
+ @d.l((:x - :y/2) >= 100000000000000000000000000000000000).should == '((x - (y / 2)) >= 100000000000000000000000000000000000)'
129
+ @d.l((((:x - :y)/(:x + :y))*:z) <= 100).should == '((((x - y) / (x + y)) * z) <= 100)'
130
+ @d.l(~((((:x - :y)/(:x + :y))*:z) <= 100)).should == '((((x - y) / (x + y)) * z) > 100)'
131
+ end
132
+
133
+ it "should not allow negation of string expressions" do
134
+ proc{~:x.sql_string}.should raise_error
135
+ proc{~([:x, :y].sql_string_join)}.should raise_error
136
+ end
137
+
138
+ it "should not allow mathematical, inequality, or string operations on true, false, or nil" do
139
+ proc{:x + 1}.should_not raise_error
140
+ proc{:x - true}.should raise_error(Sequel::Error)
141
+ proc{:x / false}.should raise_error(Sequel::Error)
142
+ proc{:x * nil}.should raise_error(Sequel::Error)
143
+ proc{:x > 1}.should_not raise_error
144
+ proc{:x < true}.should raise_error(Sequel::Error)
145
+ proc{:x >= false}.should raise_error(Sequel::Error)
146
+ proc{:x <= nil}.should raise_error(Sequel::Error)
147
+ proc{[:x, nil].sql_string_join}.should raise_error(Sequel::Error)
148
+ end
149
+
150
+ it "should not allow mathematical, inequality, or string operations on boolean complex expressions" do
151
+ proc{:x + (:y + 1)}.should_not raise_error
152
+ proc{:x - (~:y)}.should raise_error(Sequel::Error)
153
+ proc{:x / (:y & :z)}.should raise_error(Sequel::Error)
154
+ proc{:x * (:y | :z)}.should raise_error(Sequel::Error)
155
+ proc{:x > (:y > 5)}.should raise_error(Sequel::Error)
156
+ proc{:x < (:y < 5)}.should raise_error(Sequel::Error)
157
+ proc{:x >= (:y >= 5)}.should raise_error(Sequel::Error)
158
+ proc{:x <= (:y <= 5)}.should raise_error(Sequel::Error)
159
+ proc{:x > {:y => nil}}.should raise_error(Sequel::Error)
160
+ proc{:x < ~{:y => nil}}.should raise_error(Sequel::Error)
161
+ proc{:x >= {:y => 5}}.should raise_error(Sequel::Error)
162
+ proc{:x <= ~{:y => 5}}.should raise_error(Sequel::Error)
163
+ proc{:x >= {:y => [1,2,3]}}.should raise_error(Sequel::Error)
164
+ proc{:x <= ~{:y => [1,2,3]}}.should raise_error(Sequel::Error)
165
+ proc{:x + :y.like('a')}.should raise_error(Sequel::Error)
166
+ proc{:x - :y.like(/a/)}.should raise_error(Sequel::Error)
167
+ proc{:x * :y.like(/a/i)}.should raise_error(Sequel::Error)
168
+ proc{:x + ~:y.like('a')}.should raise_error(Sequel::Error)
169
+ proc{:x - ~:y.like(/a/)}.should raise_error(Sequel::Error)
170
+ proc{:x * ~:y.like(/a/i)}.should raise_error(Sequel::Error)
171
+ proc{[:x, ~:y.like(/a/i)].sql_string_join}.should raise_error(Sequel::Error)
172
+ end
173
+
174
+ it "should support AND conditions via &" do
175
+ @d.l(:x & :y).should == '(x AND y)'
176
+ @d.l(:x.sql_boolean & :y).should == '(x AND y)'
177
+ @d.l(:x & :y & :z).should == '((x AND y) AND z)'
178
+ @d.l(:x & {:y => :z}).should == '(x AND (y = z))'
179
+ @d.l({:y => :z} & :x).should == '((y = z) AND x)'
180
+ @d.l({:x => :a} & {:y => :z}).should == '((x = a) AND (y = z))'
181
+ @d.l((:x > 200) & (:y < 200)).should == '((x > 200) AND (y < 200))'
182
+ @d.l(:x & ~:y).should == '(x AND NOT y)'
183
+ @d.l(~:x & :y).should == '(NOT x AND y)'
184
+ @d.l(~:x & ~:y).should == '(NOT x AND NOT y)'
185
+ end
186
+
187
+ it "should support OR conditions via |" do
188
+ @d.l(:x | :y).should == '(x OR y)'
189
+ @d.l(:x.sql_boolean | :y).should == '(x OR y)'
190
+ @d.l(:x | :y | :z).should == '((x OR y) OR z)'
191
+ @d.l(:x | {:y => :z}).should == '(x OR (y = z))'
192
+ @d.l({:y => :z} | :x).should == '((y = z) OR x)'
193
+ @d.l({:x => :a} | {:y => :z}).should == '((x = a) OR (y = z))'
194
+ @d.l((:x > 200) | (:y < 200)).should == '((x > 200) OR (y < 200))'
195
+ end
196
+
197
+ it "should support & | combinations" do
198
+ @d.l((:x | :y) & :z).should == '((x OR y) AND z)'
199
+ @d.l(:x | (:y & :z)).should == '(x OR (y AND z))'
200
+ @d.l((:x & :w) | (:y & :z)).should == '((x AND w) OR (y AND z))'
201
+ end
202
+
203
+ it "should support & | with ~" do
204
+ @d.l(~((:x | :y) & :z)).should == '((NOT x AND NOT y) OR NOT z)'
205
+ @d.l(~(:x | (:y & :z))).should == '(NOT x AND (NOT y OR NOT z))'
206
+ @d.l(~((:x & :w) | (:y & :z))).should == '((NOT x OR NOT w) AND (NOT y OR NOT z))'
207
+ @d.l(~((:x > 200) | (:y & :z))).should == '((x <= 200) AND (NOT y OR NOT z))'
208
+ end
209
+
210
+ it "should support LiteralString" do
211
+ @d.l('x'.lit).should == '(x)'
212
+ @d.l(~'x'.lit).should == 'NOT x'
213
+ @d.l(~~'x'.lit).should == 'x'
214
+ @d.l(~(('x'.lit | :y) & :z)).should == '((NOT x AND NOT y) OR NOT z)'
215
+ @d.l(~(:x | 'y'.lit)).should == '(NOT x AND NOT y)'
216
+ @d.l(~('x'.lit & 'y'.lit)).should == '(NOT x OR NOT y)'
217
+ @d.l({'y'.lit => 'z'.lit} & 'x'.lit).should == '((y = z) AND x)'
218
+ @d.l(('x'.lit > 200) & ('y'.lit < 200)).should == '((x > 200) AND (y < 200))'
219
+ @d.l(~('x'.lit + 1 > 100)).should == '((x + 1) <= 100)'
220
+ @d.l('x'.lit.like(/a/)).should == '(x ~ \'a\')'
221
+ @d.l('x'.lit + 1 > 100).should == '((x + 1) > 100)'
222
+ @d.l(('x'.lit * :y) < 100.01).should == '((x * y) < 100.01)'
223
+ @d.l(('x'.lit - :y/2) >= 100000000000000000000000000000000000).should == '((x - (y / 2)) >= 100000000000000000000000000000000000)'
224
+ @d.l(('z'.lit * (('x'.lit / :y)/(:x + :y))) <= 100).should == '((z * ((x / y) / (x + y))) <= 100)'
225
+ @d.l(~(((('x'.lit - :y)/(:x + :y))*:z) <= 100)).should == '((((x - y) / (x + y)) * z) > 100)'
226
+ end
227
+
228
+ it "should support hashes by ANDing the conditions" do
229
+ @d.l(:x => 100, :y => 'a')[1...-1].split(' AND ').sort.should == ['(x = 100)', '(y = \'a\')']
230
+ @d.l(:x => true, :y => false)[1...-1].split(' AND ').sort.should == ['(x = \'t\')', '(y = \'f\')']
231
+ @d.l(:x => nil, :y => [1,2,3])[1...-1].split(' AND ').sort.should == ['(x IS NULL)', '(y IN (1, 2, 3))']
232
+ end
233
+
234
+ it "should support sql_negate on hashes" do
235
+ @d.l({:x => 100, :y => 'a'}.sql_negate)[1...-1].split(' AND ').sort.should == ['(x != 100)', '(y != \'a\')']
236
+ @d.l({:x => true, :y => false}.sql_negate)[1...-1].split(' AND ').sort.should == ['(x != \'t\')', '(y != \'f\')']
237
+ @d.l({:x => nil, :y => [1,2,3]}.sql_negate)[1...-1].split(' AND ').sort.should == ['(x IS NOT NULL)', '(y NOT IN (1, 2, 3))']
238
+ end
239
+
240
+ it "should support ~ on hashes" do
241
+ @d.l(~{:x => 100, :y => 'a'})[1...-1].split(' OR ').sort.should == ['(x != 100)', '(y != \'a\')']
242
+ @d.l(~{:x => true, :y => false})[1...-1].split(' OR ').sort.should == ['(x != \'t\')', '(y != \'f\')']
243
+ @d.l(~{:x => nil, :y => [1,2,3]})[1...-1].split(' OR ').sort.should == ['(x IS NOT NULL)', '(y NOT IN (1, 2, 3))']
244
+ end
245
+
246
+ it "should support sql_or on hashes" do
247
+ @d.l({:x => 100, :y => 'a'}.sql_or)[1...-1].split(' OR ').sort.should == ['(x = 100)', '(y = \'a\')']
248
+ @d.l({:x => true, :y => false}.sql_or)[1...-1].split(' OR ').sort.should == ['(x = \'t\')', '(y = \'f\')']
249
+ @d.l({:x => nil, :y => [1,2,3]}.sql_or)[1...-1].split(' OR ').sort.should == ['(x IS NULL)', '(y IN (1, 2, 3))']
250
+ end
251
+
252
+ it "should support arrays with all two pairs the same as hashes" do
253
+ @d.l([[:x, 100],[:y, 'a']]).should == '((x = 100) AND (y = \'a\'))'
254
+ @d.l([[:x, true], [:y, false]]).should == '((x = \'t\') AND (y = \'f\'))'
255
+ @d.l([[:x, nil], [:y, [1,2,3]]]).should == '((x IS NULL) AND (y IN (1, 2, 3)))'
256
+ end
257
+
258
+ it "should support sql_negate on arrays with all two pairs" do
259
+ @d.l([[:x, 100],[:y, 'a']].sql_negate).should == '((x != 100) AND (y != \'a\'))'
260
+ @d.l([[:x, true], [:y, false]].sql_negate).should == '((x != \'t\') AND (y != \'f\'))'
261
+ @d.l([[:x, nil], [:y, [1,2,3]]].sql_negate).should == '((x IS NOT NULL) AND (y NOT IN (1, 2, 3)))'
262
+ end
263
+
264
+ it "should support ~ on arrays with all two pairs" do
265
+ @d.l(~[[:x, 100],[:y, 'a']]).should == '((x != 100) OR (y != \'a\'))'
266
+ @d.l(~[[:x, true], [:y, false]]).should == '((x != \'t\') OR (y != \'f\'))'
267
+ @d.l(~[[:x, nil], [:y, [1,2,3]]]).should == '((x IS NOT NULL) OR (y NOT IN (1, 2, 3)))'
268
+ end
269
+
270
+ it "should support sql_or on arrays with all two pairs" do
271
+ @d.l([[:x, 100],[:y, 'a']].sql_or).should == '((x = 100) OR (y = \'a\'))'
272
+ @d.l([[:x, true], [:y, false]].sql_or).should == '((x = \'t\') OR (y = \'f\'))'
273
+ @d.l([[:x, nil], [:y, [1,2,3]]].sql_or).should == '((x IS NULL) OR (y IN (1, 2, 3)))'
274
+ end
275
+
276
+ it "should support Array#sql_string_join for concatenation of SQL strings" do
277
+ @d.lit([:x].sql_string_join).should == '(x)'
278
+ @d.lit([:x].sql_string_join(', ')).should == '(x)'
279
+ @d.lit([:x, :y].sql_string_join).should == '(x || y)'
280
+ @d.lit([:x, :y].sql_string_join(', ')).should == "(x || ', ' || y)"
281
+ @d.lit([:x[1], :y|1].sql_string_join).should == '(x(1) || y[1])'
282
+ @d.lit([:x[1], 'y.z'.lit].sql_string_join(', ')).should == "(x(1) || ', ' || y.z)"
283
+ @d.lit([:x, 1, :y].sql_string_join).should == "(x || '1' || y)"
284
+ @d.lit([:x, 1, :y].sql_string_join(', ')).should == "(x || ', ' || '1' || ', ' || y)"
285
+ @d.lit([:x, 1, :y].sql_string_join(:y__z)).should == "(x || y.z || '1' || y.z || y)"
286
+ @d.lit([:x, 1, :y].sql_string_join(1)).should == "(x || '1' || '1' || '1' || y)"
287
+ @d.lit([:x, :y].sql_string_join('y.x || x.y'.lit)).should == "(x || y.x || x.y || y)"
288
+ @d.lit([[:x, :y].sql_string_join, [:a, :b].sql_string_join].sql_string_join).should == "((x || y) || (a || b))"
289
+ end
290
+
291
+ it "should support StringExpression#+ for concatenation of SQL strings" do
292
+ @d.lit(:x.sql_string + :y).should == '(x || y)'
293
+ @d.lit([:x].sql_string_join + :y).should == '((x) || y)'
294
+ @d.lit([:x, :z].sql_string_join(' ') + :y).should == "((x || ' ' || z) || y)"
295
+ end
296
+
297
+ it "should be supported inside blocks" do
298
+ @d.l{[[:x, nil], [:y, [1,2,3]]].sql_or}.should == '((x IS NULL) OR (y IN (1, 2, 3)))'
299
+ @d.l{~[[:x, nil], [:y, [1,2,3]]]}.should == '((x IS NOT NULL) OR (y NOT IN (1, 2, 3)))'
300
+ @d.l{~(((('x'.lit - :y)/(:x + :y))*:z) <= 100)}.should == '((((x - y) / (x + y)) * z) > 100)'
301
+ @d.l{{:x => :a} & {:y => :z}}.should == '((x = a) AND (y = z))'
302
+ end
303
+
304
+ it "should support &, |, ^, ~, <<, and >> for NumericExpressions" do
305
+ @d.l(:x.sql_number & 1 > 100).should == '((x & 1) > 100)'
306
+ @d.l(:x.sql_number | 1 > 100).should == '((x | 1) > 100)'
307
+ @d.l(:x.sql_number ^ 1 > 100).should == '((x ^ 1) > 100)'
308
+ @d.l(~:x.sql_number > 100).should == '(~x > 100)'
309
+ @d.l(:x.sql_number << 1 > 100).should == '((x << 1) > 100)'
310
+ @d.l(:x.sql_number >> 1 > 100).should == '((x >> 1) > 100)'
311
+ @d.l((:x + 1) & 1 > 100).should == '(((x + 1) & 1) > 100)'
312
+ @d.l((:x + 1) | 1 > 100).should == '(((x + 1) | 1) > 100)'
313
+ @d.l((:x + 1) ^ 1 > 100).should == '(((x + 1) ^ 1) > 100)'
314
+ @d.l(~(:x + 1) > 100).should == '(~(x + 1) > 100)'
315
+ @d.l((:x + 1) << 1 > 100).should == '(((x + 1) << 1) > 100)'
316
+ @d.l((:x + 1) >> 1 > 100).should == '(((x + 1) >> 1) > 100)'
317
+ @d.l((:x + 1) & (:x + 2) > 100).should == '(((x + 1) & (x + 2)) > 100)'
318
+ end
319
+
320
+ it "should raise an error if use a Bitwise method on a ComplexExpression that isn't a NumericExpression" do
321
+ proc{(:x + 1) & (:x & 2)}.should raise_error(Sequel::Error)
322
+ end
323
+
324
+ it "should raise an error if use a Boolean method on a ComplexExpression that isn't a BooleanExpression" do
325
+ proc{:x & (:x + 2)}.should raise_error(Sequel::Error)
326
+ end
327
+
328
+ it "should raise an error if attempting to invert a ComplexExpression that isn't a BooleanExpression" do
329
+ proc{Sequel::SQL::BooleanExpression.invert(:x + 2)}.should raise_error(Sequel::Error)
330
+ end
331
+
332
+ it "should return self on .lit" do
333
+ y = :x + 1
334
+ y.lit.should == y
335
+ end
336
+
337
+ it "should raise an error if trying to create an invalid complex expression" do
338
+ proc{Sequel::SQL::ComplexExpression.new(:BANG, 1, 2)}.should raise_error(Sequel::Error)
339
+ end
340
+
341
+ it "should raise an error if trying to literalize an invalid complex expression" do
342
+ ce = :x + 1
343
+ ce.instance_variable_set(:@op, :BANG)
344
+ proc{@d.lit(ce)}.should raise_error(Sequel::Error)
345
+ end
346
+ end
@@ -0,0 +1,261 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ context "Migration classes" do
4
+ setup do
5
+ Sequel::Migration.descendants.clear
6
+ end
7
+
8
+ specify "should be registred in Migration.descendants" do
9
+ @class = Class.new(Sequel::Migration)
10
+
11
+ Sequel::Migration.descendants.should == [@class]
12
+ end
13
+
14
+ specify "should be registered in the right order" do
15
+ @c1 = Class.new(Sequel::Migration)
16
+ @c2 = Class.new(Sequel::Migration)
17
+ @c3 = Class.new(Sequel::Migration)
18
+
19
+ Sequel::Migration.descendants.should == [@c1, @c2, @c3]
20
+ end
21
+ end
22
+
23
+ context "Migration#apply" do
24
+ setup do
25
+ @c = Class.new do
26
+ define_method(:one) {|x| [1111, x]}
27
+ define_method(:two) {|x| [2222, x]}
28
+ end
29
+ @db = @c.new
30
+
31
+ @migration = Class.new(Sequel::Migration) do
32
+ define_method(:up) {one(3333)}
33
+ define_method(:down) {two(4444)}
34
+ end
35
+ end
36
+
37
+ specify "should raise for an invalid direction" do
38
+ proc {@migration.apply(@db, :hahaha)}.should raise_error(ArgumentError)
39
+ end
40
+
41
+ specify "should apply the up direction correctly" do
42
+ @migration.apply(@db, :up).should == [1111, 3333]
43
+ end
44
+
45
+ specify "should apply the down direction correctly" do
46
+ @migration.apply(@db, :down).should == [2222, 4444]
47
+ end
48
+ end
49
+
50
+ class DummyMigrationDataset
51
+ attr_reader :from
52
+
53
+ def initialize(x); @from = x; end
54
+
55
+ @@version = nil
56
+
57
+ def version; @@version; end
58
+ def version=(x); @@version = x; end
59
+ def first; @@version ? {:version => @@version} : nil; end
60
+ def update(h); @@version = h[:version]; end
61
+ def <<(h); @@version = h[:version]; end
62
+ end
63
+
64
+ class DummyMigrationDB
65
+ attr_reader :creates, :drops, :table_created
66
+
67
+ def initialize
68
+ @creates = []
69
+ @drops = []
70
+ end
71
+
72
+ def create(x); @creates << x; end
73
+ def drop(x); @drops << x; end
74
+
75
+ def [](x); DummyMigrationDataset.new(x); end
76
+
77
+ def create_table(x); raise if @table_created == x; @table_created = x; end
78
+ def table_exists?(x); @table_created == x; end
79
+
80
+ def transaction; yield; end
81
+ end
82
+
83
+ MIGRATION_001 = %[
84
+ class CreateSessions < Sequel::Migration
85
+ def up
86
+ create(1111)
87
+ end
88
+
89
+ def down
90
+ drop(1111)
91
+ end
92
+ end
93
+ ]
94
+
95
+ MIGRATION_002 = %[
96
+ class CreateNodes < Sequel::Migration
97
+ def up
98
+ create(2222)
99
+ end
100
+
101
+ def down
102
+ drop(2222)
103
+ end
104
+ end
105
+ ]
106
+
107
+ MIGRATION_003 = %[
108
+ class CreateUsers < Sequel::Migration
109
+ def up
110
+ create(3333)
111
+ end
112
+
113
+ def down
114
+ drop(3333)
115
+ end
116
+ end
117
+ ]
118
+
119
+ MIGRATION_005 = %[
120
+ class CreateAttributes < Sequel::Migration
121
+ def up
122
+ create(5555)
123
+ end
124
+
125
+ def down
126
+ drop(5555)
127
+ end
128
+ end
129
+ ]
130
+
131
+ context "Sequel::Migrator" do
132
+ setup do
133
+ @db = DummyMigrationDB.new
134
+
135
+ File.open('001_create_sessions.rb', 'w') {|f| f << MIGRATION_001}
136
+ File.open('002_create_nodes.rb', 'w') {|f| f << MIGRATION_002}
137
+ File.open('003_create_users.rb', 'w') {|f| f << MIGRATION_003}
138
+ File.open('005_create_attributes.rb', 'w') {|f| f << MIGRATION_005}
139
+
140
+ @db[:schema_info].version = nil
141
+ end
142
+
143
+ teardown do
144
+ Object.send(:remove_const, "CreateSessions") if Object.const_defined?("CreateSessions")
145
+ Object.send(:remove_const, "CreateNodes") if Object.const_defined?("CreateNodes")
146
+ Object.send(:remove_const, "CreateUsers") if Object.const_defined?("CreateUsers")
147
+ Object.send(:remove_const, "CreateAttributes") if Object.const_defined?("CreateAttributes")
148
+
149
+ File.delete('001_create_sessions.rb')
150
+ File.delete('002_create_nodes.rb')
151
+ File.delete('003_create_users.rb')
152
+ File.delete('005_create_attributes.rb')
153
+ end
154
+
155
+ specify "should return the list of files for a specified version range" do
156
+ Sequel::Migrator.migration_files('.', 1..1).should == \
157
+ ['./001_create_sessions.rb']
158
+
159
+ Sequel::Migrator.migration_files('.', 1..3).should == \
160
+ ['./001_create_sessions.rb', './002_create_nodes.rb', './003_create_users.rb']
161
+
162
+ Sequel::Migrator.migration_files('.', 3..5).should == \
163
+ ['./003_create_users.rb', './005_create_attributes.rb']
164
+
165
+ Sequel::Migrator.migration_files('.', 7..8).should == []
166
+ end
167
+
168
+ specify "should return the latest version available" do
169
+ Sequel::Migrator.latest_migration_version('.').should == 5
170
+ end
171
+
172
+ specify "should load the migration classes for the specified range" do
173
+ Sequel::Migrator.migration_classes('.', 3, 0, :up).should == \
174
+ [CreateSessions, CreateNodes, CreateUsers]
175
+ end
176
+
177
+ specify "should load the migration classes for the specified range" do
178
+ Sequel::Migrator.migration_classes('.', 0, 5, :down).should == \
179
+ [CreateAttributes, CreateUsers, CreateNodes, CreateSessions]
180
+ end
181
+
182
+ specify "should start from current + 1 for the up direction" do
183
+ Sequel::Migrator.migration_classes('.', 3, 1, :up).should == \
184
+ [CreateNodes, CreateUsers]
185
+ end
186
+
187
+ specify "should end on current + 1 for the down direction" do
188
+ Sequel::Migrator.migration_classes('.', 2, 5, :down).should == \
189
+ [CreateAttributes, CreateUsers]
190
+ end
191
+
192
+ specify "should automatically create the schema_info table" do
193
+ @db.table_exists?(:schema_info).should be_false
194
+ Sequel::Migrator.schema_info_dataset(@db)
195
+ @db.table_exists?(:schema_info).should be_true
196
+
197
+ # should not raise if table already exists
198
+ proc {Sequel::Migrator.schema_info_dataset(@db)}.should_not raise_error
199
+ end
200
+
201
+ specify "should return a dataset for the schema_info table" do
202
+ d = Sequel::Migrator.schema_info_dataset(@db)
203
+ d.from.should == :schema_info
204
+ end
205
+
206
+ specify "should get the migration version stored in the database" do
207
+ # default is 0
208
+ Sequel::Migrator.get_current_migration_version(@db).should == 0
209
+
210
+ Sequel::Migrator.schema_info_dataset(@db) << {:version => 4321}
211
+
212
+ Sequel::Migrator.get_current_migration_version(@db).should == 4321
213
+ end
214
+
215
+ specify "should set the migration version stored in the database" do
216
+ Sequel::Migrator.get_current_migration_version(@db).should == 0
217
+ Sequel::Migrator.set_current_migration_version(@db, 6666)
218
+ Sequel::Migrator.get_current_migration_version(@db).should == 6666
219
+ end
220
+
221
+ specify "should apply migrations correctly in the up direction" do
222
+ Sequel::Migrator.apply(@db, '.', 3, 2)
223
+ @db.creates.should == [3333]
224
+
225
+ Sequel::Migrator.get_current_migration_version(@db).should == 3
226
+
227
+ Sequel::Migrator.apply(@db, '.', 5)
228
+ @db.creates.should == [3333, 5555]
229
+
230
+ Sequel::Migrator.get_current_migration_version(@db).should == 5
231
+ end
232
+
233
+ specify "should apply migrations correctly in the down direction" do
234
+ Sequel::Migrator.apply(@db, '.', 1, 5)
235
+ @db.drops.should == [5555, 3333, 2222]
236
+
237
+ Sequel::Migrator.get_current_migration_version(@db).should == 1
238
+ end
239
+
240
+ specify "should apply migrations up to the latest version if no target is given" do
241
+ Sequel::Migrator.apply(@db, '.')
242
+ @db.creates.should == [1111, 2222, 3333, 5555]
243
+
244
+ Sequel::Migrator.get_current_migration_version(@db).should == 5
245
+ end
246
+
247
+ specify "should apply migrations down to 0 version correctly" do
248
+ Sequel::Migrator.apply(@db, '.', 0, 5)
249
+ @db.drops.should == [5555, 3333, 2222, 1111]
250
+
251
+ Sequel::Migrator.get_current_migration_version(@db).should == 0
252
+ end
253
+
254
+ specify "should return the target version" do
255
+ Sequel::Migrator.apply(@db, '.', 3, 2).should == 3
256
+
257
+ Sequel::Migrator.apply(@db, '.', 0).should == 0
258
+
259
+ Sequel::Migrator.apply(@db, '.').should == 5
260
+ end
261
+ end