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.
Files changed (137) hide show
  1. data/CHANGELOG +7 -1
  2. data/doc/advanced_associations.rdoc +614 -0
  3. data/doc/cheat_sheet.rdoc +223 -0
  4. data/doc/dataset_filtering.rdoc +158 -0
  5. data/doc/prepared_statements.rdoc +104 -0
  6. data/doc/release_notes/1.0.txt +38 -0
  7. data/doc/release_notes/1.1.txt +143 -0
  8. data/doc/release_notes/1.3.txt +101 -0
  9. data/doc/release_notes/1.4.0.txt +53 -0
  10. data/doc/release_notes/1.5.0.txt +155 -0
  11. data/doc/release_notes/2.0.0.txt +298 -0
  12. data/doc/release_notes/2.1.0.txt +271 -0
  13. data/doc/release_notes/2.10.0.txt +328 -0
  14. data/doc/release_notes/2.2.0.txt +253 -0
  15. data/doc/release_notes/2.3.0.txt +88 -0
  16. data/doc/release_notes/2.4.0.txt +106 -0
  17. data/doc/release_notes/2.5.0.txt +137 -0
  18. data/doc/release_notes/2.6.0.txt +157 -0
  19. data/doc/release_notes/2.7.0.txt +166 -0
  20. data/doc/release_notes/2.8.0.txt +171 -0
  21. data/doc/release_notes/2.9.0.txt +97 -0
  22. data/doc/schema.rdoc +29 -0
  23. data/doc/sharding.rdoc +113 -0
  24. data/lib/sequel.rb +1 -0
  25. data/lib/sequel_core/adapters/ado.rb +89 -0
  26. data/lib/sequel_core/adapters/db2.rb +143 -0
  27. data/lib/sequel_core/adapters/dbi.rb +112 -0
  28. data/lib/sequel_core/adapters/do/mysql.rb +38 -0
  29. data/lib/sequel_core/adapters/do/postgres.rb +92 -0
  30. data/lib/sequel_core/adapters/do/sqlite.rb +31 -0
  31. data/lib/sequel_core/adapters/do.rb +205 -0
  32. data/lib/sequel_core/adapters/firebird.rb +298 -0
  33. data/lib/sequel_core/adapters/informix.rb +85 -0
  34. data/lib/sequel_core/adapters/jdbc/h2.rb +69 -0
  35. data/lib/sequel_core/adapters/jdbc/mysql.rb +66 -0
  36. data/lib/sequel_core/adapters/jdbc/oracle.rb +23 -0
  37. data/lib/sequel_core/adapters/jdbc/postgresql.rb +113 -0
  38. data/lib/sequel_core/adapters/jdbc/sqlite.rb +43 -0
  39. data/lib/sequel_core/adapters/jdbc.rb +491 -0
  40. data/lib/sequel_core/adapters/mysql.rb +369 -0
  41. data/lib/sequel_core/adapters/odbc.rb +174 -0
  42. data/lib/sequel_core/adapters/openbase.rb +68 -0
  43. data/lib/sequel_core/adapters/oracle.rb +107 -0
  44. data/lib/sequel_core/adapters/postgres.rb +456 -0
  45. data/lib/sequel_core/adapters/shared/ms_access.rb +110 -0
  46. data/lib/sequel_core/adapters/shared/mssql.rb +102 -0
  47. data/lib/sequel_core/adapters/shared/mysql.rb +325 -0
  48. data/lib/sequel_core/adapters/shared/oracle.rb +61 -0
  49. data/lib/sequel_core/adapters/shared/postgres.rb +715 -0
  50. data/lib/sequel_core/adapters/shared/progress.rb +31 -0
  51. data/lib/sequel_core/adapters/shared/sqlite.rb +265 -0
  52. data/lib/sequel_core/adapters/sqlite.rb +248 -0
  53. data/lib/sequel_core/connection_pool.rb +258 -0
  54. data/lib/sequel_core/core_ext.rb +217 -0
  55. data/lib/sequel_core/core_sql.rb +202 -0
  56. data/lib/sequel_core/database/schema.rb +164 -0
  57. data/lib/sequel_core/database.rb +691 -0
  58. data/lib/sequel_core/dataset/callback.rb +13 -0
  59. data/lib/sequel_core/dataset/convenience.rb +237 -0
  60. data/lib/sequel_core/dataset/pagination.rb +96 -0
  61. data/lib/sequel_core/dataset/prepared_statements.rb +220 -0
  62. data/lib/sequel_core/dataset/query.rb +41 -0
  63. data/lib/sequel_core/dataset/schema.rb +15 -0
  64. data/lib/sequel_core/dataset/sql.rb +1010 -0
  65. data/lib/sequel_core/dataset/stored_procedures.rb +75 -0
  66. data/lib/sequel_core/dataset/unsupported.rb +43 -0
  67. data/lib/sequel_core/dataset.rb +511 -0
  68. data/lib/sequel_core/deprecated.rb +26 -0
  69. data/lib/sequel_core/exceptions.rb +44 -0
  70. data/lib/sequel_core/migration.rb +212 -0
  71. data/lib/sequel_core/object_graph.rb +230 -0
  72. data/lib/sequel_core/pretty_table.rb +71 -0
  73. data/lib/sequel_core/schema/generator.rb +320 -0
  74. data/lib/sequel_core/schema/sql.rb +325 -0
  75. data/lib/sequel_core/schema.rb +2 -0
  76. data/lib/sequel_core/sql.rb +887 -0
  77. data/lib/sequel_core/version.rb +11 -0
  78. data/lib/sequel_core.rb +172 -0
  79. data/lib/sequel_model/association_reflection.rb +267 -0
  80. data/lib/sequel_model/associations.rb +499 -0
  81. data/lib/sequel_model/base.rb +523 -0
  82. data/lib/sequel_model/caching.rb +82 -0
  83. data/lib/sequel_model/dataset_methods.rb +26 -0
  84. data/lib/sequel_model/eager_loading.rb +370 -0
  85. data/lib/sequel_model/exceptions.rb +7 -0
  86. data/lib/sequel_model/hooks.rb +101 -0
  87. data/lib/sequel_model/inflector.rb +281 -0
  88. data/lib/sequel_model/plugins.rb +62 -0
  89. data/lib/sequel_model/record.rb +568 -0
  90. data/lib/sequel_model/schema.rb +49 -0
  91. data/lib/sequel_model/validations.rb +429 -0
  92. data/lib/sequel_model.rb +91 -0
  93. data/spec/adapters/ado_spec.rb +46 -0
  94. data/spec/adapters/firebird_spec.rb +376 -0
  95. data/spec/adapters/informix_spec.rb +96 -0
  96. data/spec/adapters/mysql_spec.rb +881 -0
  97. data/spec/adapters/oracle_spec.rb +244 -0
  98. data/spec/adapters/postgres_spec.rb +687 -0
  99. data/spec/adapters/spec_helper.rb +10 -0
  100. data/spec/adapters/sqlite_spec.rb +555 -0
  101. data/spec/integration/dataset_test.rb +134 -0
  102. data/spec/integration/eager_loader_test.rb +696 -0
  103. data/spec/integration/prepared_statement_test.rb +130 -0
  104. data/spec/integration/schema_test.rb +180 -0
  105. data/spec/integration/spec_helper.rb +58 -0
  106. data/spec/integration/type_test.rb +96 -0
  107. data/spec/rcov.opts +6 -0
  108. data/spec/sequel_core/connection_pool_spec.rb +526 -0
  109. data/spec/sequel_core/core_ext_spec.rb +156 -0
  110. data/spec/sequel_core/core_sql_spec.rb +522 -0
  111. data/spec/sequel_core/database_spec.rb +1188 -0
  112. data/spec/sequel_core/dataset_spec.rb +3481 -0
  113. data/spec/sequel_core/expression_filters_spec.rb +363 -0
  114. data/spec/sequel_core/migration_spec.rb +261 -0
  115. data/spec/sequel_core/object_graph_spec.rb +272 -0
  116. data/spec/sequel_core/pretty_table_spec.rb +58 -0
  117. data/spec/sequel_core/schema_generator_spec.rb +167 -0
  118. data/spec/sequel_core/schema_spec.rb +780 -0
  119. data/spec/sequel_core/spec_helper.rb +55 -0
  120. data/spec/sequel_core/version_spec.rb +7 -0
  121. data/spec/sequel_model/association_reflection_spec.rb +93 -0
  122. data/spec/sequel_model/associations_spec.rb +1767 -0
  123. data/spec/sequel_model/base_spec.rb +419 -0
  124. data/spec/sequel_model/caching_spec.rb +215 -0
  125. data/spec/sequel_model/dataset_methods_spec.rb +78 -0
  126. data/spec/sequel_model/eager_loading_spec.rb +1165 -0
  127. data/spec/sequel_model/hooks_spec.rb +485 -0
  128. data/spec/sequel_model/inflector_spec.rb +119 -0
  129. data/spec/sequel_model/model_spec.rb +588 -0
  130. data/spec/sequel_model/plugins_spec.rb +80 -0
  131. data/spec/sequel_model/record_spec.rb +1184 -0
  132. data/spec/sequel_model/schema_spec.rb +90 -0
  133. data/spec/sequel_model/spec_helper.rb +78 -0
  134. data/spec/sequel_model/validations_spec.rb +1067 -0
  135. data/spec/spec.opts +0 -0
  136. data/spec/spec_config.rb.example +10 -0
  137. metadata +177 -3
@@ -0,0 +1,687 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper.rb')
2
+
3
+ unless defined?(POSTGRES_DB)
4
+ POSTGRES_URL = 'postgres://postgres:postgres@localhost:5432/reality_spec' unless defined? POSTGRES_URL
5
+ POSTGRES_DB = Sequel.connect(ENV['SEQUEL_PG_SPEC_DB']||POSTGRES_URL)
6
+ end
7
+ #POSTGRES_DB.instance_variable_set(:@server_version, 80100)
8
+ POSTGRES_DB.create_table! :test do
9
+ text :name
10
+ integer :value, :index => true
11
+ end
12
+ POSTGRES_DB.create_table! :test2 do
13
+ text :name
14
+ integer :value
15
+ end
16
+ POSTGRES_DB.create_table! :test3 do
17
+ integer :value
18
+ timestamp :time
19
+ end
20
+ POSTGRES_DB.create_table! :test4 do
21
+ varchar :name, :size => 20
22
+ bytea :value
23
+ end
24
+ POSTGRES_DB.create_table! :test5 do
25
+ primary_key :xid
26
+ integer :value
27
+ end
28
+
29
+ context "A PostgreSQL database" do
30
+ setup do
31
+ @db = POSTGRES_DB
32
+ end
33
+
34
+ specify "should provide disconnect functionality" do
35
+ @db.tables
36
+ @db.pool.size.should == 1
37
+ @db.disconnect
38
+ @db.pool.size.should == 0
39
+ end
40
+
41
+ specify "should provide the server version" do
42
+ @db.server_version.should > 70000
43
+ end
44
+
45
+ specify "should raise Sequel::Error on error" do
46
+ proc{@db << "SELECT 1 + 'a'"}.should raise_error(Sequel::Error)
47
+ end
48
+
49
+ specify "should correctly parse the schema" do
50
+ require 'logger'
51
+ @db.schema(:test3, :reload=>true).should == [
52
+ [:value, {:type=>:integer, :allow_null=>true, :default=>nil, :db_type=>"integer", :primary_key=>false}],
53
+ [:time, {:type=>:datetime, :allow_null=>true, :default=>nil, :db_type=>"timestamp without time zone", :primary_key=>false}]
54
+ ]
55
+ @db.schema(:test4, :reload=>true).should == [
56
+ [:name, {:type=>:string, :allow_null=>true, :default=>nil, :db_type=>"character varying(20)", :primary_key=>false}],
57
+ [:value, {:type=>:blob, :allow_null=>true, :default=>nil, :db_type=>"bytea", :primary_key=>false}]
58
+ ]
59
+ end
60
+
61
+ specify "should get the schema all database tables if no table name is used" do
62
+ @db.schema(:test3, :reload=>true).should == @db.schema(nil, :reload=>true)[:test3]
63
+ end
64
+ end
65
+
66
+ context "A PostgreSQL dataset" do
67
+ setup do
68
+ @d = POSTGRES_DB[:test]
69
+ @d.delete # remove all records
70
+ end
71
+
72
+ specify "should return the correct record count" do
73
+ @d.count.should == 0
74
+ @d << {:name => 'abc', :value => 123}
75
+ @d << {:name => 'abc', :value => 456}
76
+ @d << {:name => 'def', :value => 789}
77
+ @d.count.should == 3
78
+ end
79
+
80
+ specify "should return the correct records" do
81
+ @d.to_a.should == []
82
+ @d << {:name => 'abc', :value => 123}
83
+ @d << {:name => 'abc', :value => 456}
84
+ @d << {:name => 'def', :value => 789}
85
+
86
+ @d.order(:value).to_a.should == [
87
+ {:name => 'abc', :value => 123},
88
+ {:name => 'abc', :value => 456},
89
+ {:name => 'def', :value => 789}
90
+ ]
91
+ end
92
+
93
+ specify "should update records correctly" do
94
+ @d << {:name => 'abc', :value => 123}
95
+ @d << {:name => 'abc', :value => 456}
96
+ @d << {:name => 'def', :value => 789}
97
+ @d.filter(:name => 'abc').update(:value => 530)
98
+
99
+ # the third record should stay the same
100
+ # floating-point precision bullshit
101
+ @d[:name => 'def'][:value].should == 789
102
+ @d.filter(:value => 530).count.should == 2
103
+ end
104
+
105
+ specify "should delete records correctly" do
106
+ @d << {:name => 'abc', :value => 123}
107
+ @d << {:name => 'abc', :value => 456}
108
+ @d << {:name => 'def', :value => 789}
109
+ @d.filter(:name => 'abc').delete
110
+
111
+ @d.count.should == 1
112
+ @d.first[:name].should == 'def'
113
+ end
114
+
115
+ specify "should be able to literalize booleans" do
116
+ proc {@d.literal(true)}.should_not raise_error
117
+ proc {@d.literal(false)}.should_not raise_error
118
+ end
119
+
120
+ specify "should quote columns and tables using double quotes if quoting identifiers" do
121
+ @d.quote_identifiers = true
122
+ @d.select(:name).sql.should == \
123
+ 'SELECT "name" FROM "test"'
124
+
125
+ @d.select('COUNT(*)'.lit).sql.should == \
126
+ 'SELECT COUNT(*) FROM "test"'
127
+
128
+ @d.select(:max.sql_function(:value)).sql.should == \
129
+ 'SELECT max("value") FROM "test"'
130
+
131
+ @d.select(:NOW.sql_function).sql.should == \
132
+ 'SELECT NOW() FROM "test"'
133
+
134
+ @d.select(:max.sql_function(:items__value)).sql.should == \
135
+ 'SELECT max("items"."value") FROM "test"'
136
+
137
+ @d.order(:name.desc).sql.should == \
138
+ 'SELECT * FROM "test" ORDER BY "name" DESC'
139
+
140
+ @d.select('test.name AS item_name'.lit).sql.should == \
141
+ 'SELECT test.name AS item_name FROM "test"'
142
+
143
+ @d.select('"name"'.lit).sql.should == \
144
+ 'SELECT "name" FROM "test"'
145
+
146
+ @d.select('max(test."name") AS "max_name"'.lit).sql.should == \
147
+ 'SELECT max(test."name") AS "max_name" FROM "test"'
148
+
149
+ @d.select(:test.sql_function(:abc, 'hello')).sql.should == \
150
+ "SELECT test(\"abc\", 'hello') FROM \"test\""
151
+
152
+ @d.select(:test.sql_function(:abc__def, 'hello')).sql.should == \
153
+ "SELECT test(\"abc\".\"def\", 'hello') FROM \"test\""
154
+
155
+ @d.select(:test.sql_function(:abc__def, 'hello').as(:x2)).sql.should == \
156
+ "SELECT test(\"abc\".\"def\", 'hello') AS \"x2\" FROM \"test\""
157
+
158
+ @d.insert_sql(:value => 333).should =~ \
159
+ /\AINSERT INTO "test" \("value"\) VALUES \(333\)( RETURNING NULL)?\z/
160
+
161
+ @d.insert_sql(:x => :y).should =~ \
162
+ /\AINSERT INTO "test" \("x"\) VALUES \("y"\)( RETURNING NULL)?\z/
163
+ end
164
+
165
+ specify "should quote fields correctly when reversing the order if quoting identifiers" do
166
+ @d.quote_identifiers = true
167
+ @d.reverse_order(:name).sql.should == \
168
+ 'SELECT * FROM "test" ORDER BY "name" DESC'
169
+
170
+ @d.reverse_order(:name.desc).sql.should == \
171
+ 'SELECT * FROM "test" ORDER BY "name" ASC'
172
+
173
+ @d.reverse_order(:name, :test.desc).sql.should == \
174
+ 'SELECT * FROM "test" ORDER BY "name" DESC, "test" ASC'
175
+
176
+ @d.reverse_order(:name.desc, :test).sql.should == \
177
+ 'SELECT * FROM "test" ORDER BY "name" ASC, "test" DESC'
178
+ end
179
+
180
+ specify "should support transactions" do
181
+ POSTGRES_DB.transaction do
182
+ @d << {:name => 'abc', :value => 1}
183
+ end
184
+
185
+ @d.count.should == 1
186
+ end
187
+
188
+ specify "should have #transaction yield the connection" do
189
+ POSTGRES_DB.transaction do |conn|
190
+ conn.should_not == nil
191
+ end
192
+ end
193
+
194
+ specify "should correctly rollback transactions" do
195
+ proc do
196
+ POSTGRES_DB.transaction do
197
+ @d << {:name => 'abc', :value => 1}
198
+ raise Interrupt, 'asdf'
199
+ end
200
+ end.should raise_error(Interrupt)
201
+
202
+ @d.count.should == 0
203
+ end
204
+
205
+ specify "should handle returning inside of the block by committing" do
206
+ def POSTGRES_DB.ret_commit
207
+ transaction do
208
+ self[:test] << {:name => 'abc'}
209
+ return
210
+ self[:test] << {:name => 'd'}
211
+ end
212
+ end
213
+ @d.count.should == 0
214
+ POSTGRES_DB.ret_commit
215
+ @d.count.should == 1
216
+ POSTGRES_DB.ret_commit
217
+ @d.count.should == 2
218
+ proc do
219
+ POSTGRES_DB.transaction do
220
+ raise Interrupt, 'asdf'
221
+ end
222
+ end.should raise_error(Interrupt)
223
+
224
+ @d.count.should == 2
225
+ end
226
+
227
+ specify "should support nested transactions through savepoints" do
228
+ POSTGRES_DB.transaction do
229
+ @d << {:name => '1'}
230
+ POSTGRES_DB.transaction do
231
+ @d << {:name => '2'}
232
+ POSTGRES_DB.transaction do
233
+ @d << {:name => '3'}
234
+ raise Sequel::Error::Rollback
235
+ end
236
+ @d << {:name => '4'}
237
+ POSTGRES_DB.transaction do
238
+ @d << {:name => '6'}
239
+ POSTGRES_DB.transaction do
240
+ @d << {:name => '7'}
241
+ end
242
+ raise Sequel::Error::Rollback
243
+ end
244
+ @d << {:name => '5'}
245
+ end
246
+ end
247
+
248
+ @d.count.should == 4
249
+ @d.order(:name).map(:name).should == %w{1 2 4 5}
250
+ end
251
+
252
+ specify "should support regexps" do
253
+ @d << {:name => 'abc', :value => 1}
254
+ @d << {:name => 'bcd', :value => 2}
255
+ @d.filter(:name => /bc/).count.should == 2
256
+ @d.filter(:name => /^bc/).count.should == 1
257
+ end
258
+
259
+ specify "should correctly escape strings" do
260
+ POSTGRES_DB['SELECT ? AS a', "\\dingo"].get(:a) == "\\dingo"
261
+ end
262
+
263
+ specify "should correctly escape strings with quotes" do
264
+ POSTGRES_DB['SELECT ? AS a', "\\'dingo"].get(:a) == "\\'dingo"
265
+ end
266
+
267
+ specify "should properly escape binary data" do
268
+ POSTGRES_DB['SELECT ? AS a', "\1\2\3".to_blob].get(:a) == "\1\2\3"
269
+ end
270
+
271
+ specify "should retrieve binary data as Blob object" do
272
+ d = POSTGRES_DB[:test4]
273
+ d << {:name => '123', :value => "\1\2\3".to_blob}
274
+ retrieved_binary_value = d[:name => '123'][:value]
275
+ retrieved_binary_value.should be_a_kind_of(::Sequel::SQL::Blob)
276
+ retrieved_binary_value.should == "\1\2\3"
277
+ retrieved_binary_value = d[:value => "\1\2\3".to_blob][:value]
278
+ retrieved_binary_value.should be_a_kind_of(::Sequel::SQL::Blob)
279
+ retrieved_binary_value.should == "\1\2\3"
280
+ end
281
+
282
+ specify "should properly receive binary data" do
283
+ POSTGRES_DB['SELECT ?::bytea AS a', "a"].get(:a) == "a"
284
+ end
285
+ end
286
+
287
+ context "A PostgreSQL dataset with a timestamp field" do
288
+ setup do
289
+ @d = POSTGRES_DB[:test3]
290
+ @d.delete
291
+ end
292
+
293
+ specify "should store milliseconds in time fields" do
294
+ t = Time.now
295
+ @d << {:value=>1, :time=>t}
296
+ @d.literal(@d[:value =>'1'][:time]).should == @d.literal(t)
297
+ @d[:value=>'1'][:time].usec.should == t.usec
298
+ end
299
+ end
300
+
301
+ context "A PostgreSQL database" do
302
+ setup do
303
+ @db = POSTGRES_DB
304
+ end
305
+
306
+ specify "should support column operations" do
307
+ @db.create_table!(:test2){text :name; integer :value}
308
+ @db[:test2] << {}
309
+ @db[:test2].columns.should == [:name, :value]
310
+
311
+ @db.add_column :test2, :xyz, :text, :default => '000'
312
+ @db[:test2].columns.should == [:name, :value, :xyz]
313
+ @db[:test2] << {:name => 'mmm', :value => 111}
314
+ @db[:test2].first[:xyz].should == '000'
315
+
316
+ @db[:test2].columns.should == [:name, :value, :xyz]
317
+ @db.drop_column :test2, :xyz
318
+
319
+ @db[:test2].columns.should == [:name, :value]
320
+
321
+ @db[:test2].delete
322
+ @db.add_column :test2, :xyz, :text, :default => '000'
323
+ @db[:test2] << {:name => 'mmm', :value => 111, :xyz => 'qqqq'}
324
+
325
+ @db[:test2].columns.should == [:name, :value, :xyz]
326
+ @db.rename_column :test2, :xyz, :zyx
327
+ @db[:test2].columns.should == [:name, :value, :zyx]
328
+ @db[:test2].first[:zyx].should == 'qqqq'
329
+
330
+ @db.add_column :test2, :xyz, :float
331
+ @db[:test2].delete
332
+ @db[:test2] << {:name => 'mmm', :value => 111, :xyz => 56.78}
333
+ @db.set_column_type :test2, :xyz, :integer
334
+
335
+ @db[:test2].first[:xyz].should == 57
336
+ end
337
+ end
338
+
339
+ context "A PostgreSQL database" do
340
+ setup do
341
+ end
342
+
343
+ specify "should support fulltext indexes" do
344
+ g = Sequel::Schema::Generator.new(POSTGRES_DB) do
345
+ text :title
346
+ text :body
347
+ full_text_index [:title, :body]
348
+ end
349
+ POSTGRES_DB.create_table_sql_list(:posts, *g.create_info).should == [
350
+ "CREATE TABLE posts (title text, body text)",
351
+ "CREATE INDEX posts_title_body_index ON posts USING gin (to_tsvector('simple', (COALESCE(title, '') || ' ' || COALESCE(body, ''))))"
352
+ ]
353
+ end
354
+
355
+ specify "should support fulltext indexes with a specific language" do
356
+ g = Sequel::Schema::Generator.new(POSTGRES_DB) do
357
+ text :title
358
+ text :body
359
+ full_text_index [:title, :body], :language => 'french'
360
+ end
361
+ POSTGRES_DB.create_table_sql_list(:posts, *g.create_info).should == [
362
+ "CREATE TABLE posts (title text, body text)",
363
+ "CREATE INDEX posts_title_body_index ON posts USING gin (to_tsvector('french', (COALESCE(title, '') || ' ' || COALESCE(body, ''))))"
364
+ ]
365
+ end
366
+
367
+ specify "should support full_text_search" do
368
+ POSTGRES_DB[:posts].full_text_search(:title, 'ruby').sql.should ==
369
+ "SELECT * FROM posts WHERE (to_tsvector('simple', (COALESCE(title, ''))) @@ to_tsquery('simple', 'ruby'))"
370
+
371
+ POSTGRES_DB[:posts].full_text_search([:title, :body], ['ruby', 'sequel']).sql.should ==
372
+ "SELECT * FROM posts WHERE (to_tsvector('simple', (COALESCE(title, '') || ' ' || COALESCE(body, ''))) @@ to_tsquery('simple', 'ruby | sequel'))"
373
+
374
+ POSTGRES_DB[:posts].full_text_search(:title, 'ruby', :language => 'french').sql.should ==
375
+ "SELECT * FROM posts WHERE (to_tsvector('french', (COALESCE(title, ''))) @@ to_tsquery('french', 'ruby'))"
376
+ end
377
+
378
+ specify "should support spatial indexes" do
379
+ g = Sequel::Schema::Generator.new(POSTGRES_DB) do
380
+ geometry :geom
381
+ spatial_index [:geom]
382
+ end
383
+ POSTGRES_DB.create_table_sql_list(:posts, *g.create_info).should == [
384
+ "CREATE TABLE posts (geom geometry)",
385
+ "CREATE INDEX posts_geom_index ON posts USING gist (geom)"
386
+ ]
387
+ end
388
+
389
+ specify "should support indexes with index type" do
390
+ g = Sequel::Schema::Generator.new(POSTGRES_DB) do
391
+ varchar :title, :size => 5
392
+ index :title, :type => 'hash'
393
+ end
394
+ POSTGRES_DB.create_table_sql_list(:posts, *g.create_info).should == [
395
+ "CREATE TABLE posts (title varchar(5))",
396
+ "CREATE INDEX posts_title_index ON posts USING hash (title)"
397
+ ]
398
+ end
399
+
400
+ specify "should support unique indexes with index type" do
401
+ g = Sequel::Schema::Generator.new(POSTGRES_DB) do
402
+ varchar :title, :size => 5
403
+ index :title, :type => 'hash', :unique => true
404
+ end
405
+ POSTGRES_DB.create_table_sql_list(:posts, *g.create_info).should == [
406
+ "CREATE TABLE posts (title varchar(5))",
407
+ "CREATE UNIQUE INDEX posts_title_index ON posts USING hash (title)"
408
+ ]
409
+ end
410
+
411
+ specify "should support partial indexes" do
412
+ g = Sequel::Schema::Generator.new(POSTGRES_DB) do
413
+ varchar :title, :size => 5
414
+ index :title, :where => {:something => 5}
415
+ end
416
+ POSTGRES_DB.create_table_sql_list(:posts, *g.create_info).should == [
417
+ "CREATE TABLE posts (title varchar(5))",
418
+ "CREATE INDEX posts_title_index ON posts (title) WHERE (something = 5)"
419
+ ]
420
+ end
421
+
422
+ specify "should support identifiers for table names in indicies" do
423
+ g = Sequel::Schema::Generator.new(POSTGRES_DB) do
424
+ varchar :title, :size => 5
425
+ index :title, :where => {:something => 5}
426
+ end
427
+ POSTGRES_DB.create_table_sql_list(Sequel::SQL::Identifier.new(:posts__test), *g.create_info).should == [
428
+ "CREATE TABLE posts__test (title varchar(5))",
429
+ "CREATE INDEX posts__test_title_index ON posts__test (title) WHERE (something = 5)"
430
+ ]
431
+ end
432
+ end
433
+
434
+ context "Postgres::Dataset#multi_insert_sql / #import" do
435
+ setup do
436
+ @ds = POSTGRES_DB[:test]
437
+ end
438
+
439
+ specify "should return separate insert statements if server_version < 80200" do
440
+ @ds.meta_def(:server_version){80199}
441
+
442
+ @ds.multi_insert_sql([:x, :y], [[1, 2], [3, 4]]).should == [
443
+ 'INSERT INTO test (x, y) VALUES (1, 2)',
444
+ 'INSERT INTO test (x, y) VALUES (3, 4)'
445
+ ]
446
+ end
447
+
448
+ specify "should a single insert statement if server_version >= 80200" do
449
+ @ds.meta_def(:server_version){80200}
450
+
451
+ @ds.multi_insert_sql([:x, :y], [[1, 2], [3, 4]]).should == [
452
+ 'INSERT INTO test (x, y) VALUES (1, 2), (3, 4)'
453
+ ]
454
+
455
+ @ds.meta_def(:server_version){80201}
456
+
457
+ @ds.multi_insert_sql([:x, :y], [[1, 2], [3, 4]]).should == [
458
+ 'INSERT INTO test (x, y) VALUES (1, 2), (3, 4)'
459
+ ]
460
+ end
461
+ end
462
+
463
+ context "Postgres::Dataset#insert" do
464
+ setup do
465
+ @ds = POSTGRES_DB[:test5]
466
+ @ds.delete
467
+ end
468
+
469
+ specify "should call insert_sql if server_version < 80200" do
470
+ @ds.meta_def(:server_version){80100}
471
+ @ds.should_receive(:execute_insert).once.with('INSERT INTO test5 (value) VALUES (10)', :table=>:test5, :values=>{:value=>10})
472
+ @ds.insert(:value=>10)
473
+ end
474
+
475
+ specify "should use INSERT RETURNING if server_version >= 80200" do
476
+ @ds.meta_def(:server_version){80201}
477
+ @ds.should_receive(:single_value).once.with(:server=>:default, :sql=>'INSERT INTO test5 (value) VALUES (10) RETURNING xid')
478
+ @ds.insert(:value=>10)
479
+ end
480
+
481
+ specify "should have insert_returning_sql use the RETURNING keyword" do
482
+ @ds.insert_returning_sql(:xid, :value=>10).should == "INSERT INTO test5 (value) VALUES (10) RETURNING xid"
483
+ @ds.insert_returning_sql('*'.lit, :value=>10).should == "INSERT INTO test5 (value) VALUES (10) RETURNING *"
484
+ end
485
+
486
+ specify "should have insert_select return nil if server_version < 80200" do
487
+ @ds.meta_def(:server_version){80100}
488
+ @ds.insert_select(:value=>10).should == nil
489
+ end
490
+
491
+ specify "should have insert_select insert the record and return the inserted record if server_version < 80200" do
492
+ @ds.meta_def(:server_version){80201}
493
+ h = @ds.insert_select(:value=>10)
494
+ h[:value].should == 10
495
+ @ds.first(:xid=>h[:xid])[:value].should == 10
496
+ end
497
+
498
+ specify "should correctly return the inserted record's primary key value" do
499
+ value1 = 10
500
+ id1 = @ds.insert(:value=>value1)
501
+ @ds.first(:xid=>id1)[:value].should == value1
502
+ value2 = 20
503
+ id2 = @ds.insert(:value=>value2)
504
+ @ds.first(:xid=>id2)[:value].should == value2
505
+ end
506
+
507
+ specify "should return nil if the table has no primary key" do
508
+ ds = POSTGRES_DB[:test4]
509
+ ds.delete
510
+ ds.insert(:name=>'a').should == nil
511
+ end
512
+ end
513
+
514
+ context "Postgres::Database schema qualified tables" do
515
+ setup do
516
+ POSTGRES_DB << "CREATE SCHEMA schema_test"
517
+ POSTGRES_DB.instance_variable_set(:@primary_keys, {})
518
+ POSTGRES_DB.instance_variable_set(:@primary_key_sequences, {})
519
+ end
520
+ teardown do
521
+ POSTGRES_DB << "DROP SCHEMA schema_test CASCADE"
522
+ POSTGRES_DB.default_schema = :public
523
+ end
524
+
525
+ specify "should be able to create, drop, select and insert into tables in a given schema" do
526
+ POSTGRES_DB.create_table(:schema_test__schema_test){primary_key :i}
527
+ POSTGRES_DB[:schema_test__schema_test].first.should == nil
528
+ POSTGRES_DB[:schema_test__schema_test].insert(:i=>1).should == 1
529
+ POSTGRES_DB[:schema_test__schema_test].first.should == {:i=>1}
530
+ POSTGRES_DB.from('schema_test.schema_test'.lit).first.should == {:i=>1}
531
+ POSTGRES_DB.drop_table(:schema_test__schema_test)
532
+ POSTGRES_DB.create_table(:schema_test.qualify(:schema_test)){integer :i}
533
+ POSTGRES_DB[:schema_test__schema_test].first.should == nil
534
+ POSTGRES_DB.from('schema_test.schema_test'.lit).first.should == nil
535
+ POSTGRES_DB.drop_table(:schema_test.qualify(:schema_test))
536
+ end
537
+
538
+ specify "#tables should include only tables in the public schema if no schema is given" do
539
+ POSTGRES_DB.create_table(:schema_test__schema_test){integer :i}
540
+ POSTGRES_DB.tables.should_not include(:schema_test)
541
+ end
542
+
543
+ specify "#tables should return tables in the schema provided by the :schema argument" do
544
+ POSTGRES_DB.create_table(:schema_test__schema_test){integer :i}
545
+ POSTGRES_DB.tables(:schema=>:schema_test).should == [:schema_test]
546
+ end
547
+
548
+ specify "#table_exists? should assume the public schema if no schema is provided" do
549
+ POSTGRES_DB.create_table(:schema_test__schema_test){integer :i}
550
+ POSTGRES_DB.table_exists?(:schema_test).should == false
551
+ end
552
+
553
+ specify "#table_exists? should see if the table is in a given schema" do
554
+ POSTGRES_DB.create_table(:schema_test__schema_test){integer :i}
555
+ POSTGRES_DB.table_exists?(:schema_test__schema_test).should == true
556
+ end
557
+
558
+ specify "should be able to get primary keys for tables in a given schema" do
559
+ POSTGRES_DB.create_table(:schema_test__schema_test){primary_key :i}
560
+ POSTGRES_DB.primary_key(:schema_test__schema_test).should == 'i'
561
+ end
562
+
563
+ specify "should be able to get serial sequences for tables in a given schema" do
564
+ POSTGRES_DB.create_table(:schema_test__schema_test){primary_key :i}
565
+ POSTGRES_DB.primary_key_sequence(:schema_test__schema_test).should == '"schema_test"."schema_test_i_seq"'
566
+ end
567
+
568
+ specify "should be able to get custom sequences for tables in a given schema" do
569
+ POSTGRES_DB << "CREATE SEQUENCE schema_test.kseq"
570
+ POSTGRES_DB.create_table(:schema_test__schema_test){integer :j; primary_key :k, :type=>:integer, :default=>"nextval('schema_test.kseq'::regclass)".lit}
571
+ POSTGRES_DB.primary_key_sequence(:schema_test__schema_test).should == '"schema_test"."kseq"'
572
+ end
573
+
574
+ specify "#default_schema= should change the default schema used from public" do
575
+ POSTGRES_DB.create_table(:schema_test__schema_test){primary_key :i}
576
+ POSTGRES_DB.default_schema = :schema_test
577
+ POSTGRES_DB.table_exists?(:schema_test).should == true
578
+ POSTGRES_DB.tables.should == [:schema_test]
579
+ POSTGRES_DB.primary_key(:schema_test__schema_test).should == 'i'
580
+ POSTGRES_DB.primary_key_sequence(:schema_test__schema_test).should == '"schema_test"."schema_test_i_seq"'
581
+ end
582
+ end
583
+
584
+ if POSTGRES_DB.server_version >= 80300
585
+
586
+ POSTGRES_DB.create_table! :test6 do
587
+ text :title
588
+ text :body
589
+ full_text_index [:title, :body]
590
+ end
591
+
592
+ context "PostgreSQL tsearch2" do
593
+ before do
594
+ @ds = POSTGRES_DB[:test6]
595
+ end
596
+ after do
597
+ POSTGRES_DB[:test6].delete
598
+ end
599
+
600
+ specify "should search by indexed column" do
601
+ record = {:title => "oopsla conference", :body => "test"}
602
+ @ds << record
603
+ @ds.full_text_search(:title, "oopsla").all.should include(record)
604
+ end
605
+
606
+ specify "should join multiple coumns with spaces to search by last words in row" do
607
+ record = {:title => "multiple words", :body => "are easy to search"}
608
+ @ds << record
609
+ @ds.full_text_search([:title, :body], "words").all.should include(record)
610
+ end
611
+
612
+ specify "should return rows with a NULL in one column if a match in another column" do
613
+ record = {:title => "multiple words", :body =>nil}
614
+ @ds << record
615
+ @ds.full_text_search([:title, :body], "words").all.should include(record)
616
+ end
617
+ end
618
+ end
619
+
620
+ context "Postgres::Database functions, languages, and triggers" do
621
+ setup do
622
+ @d = POSTGRES_DB
623
+ end
624
+ teardown do
625
+ @d.drop_function('tf', :if_exists=>true, :cascade=>true)
626
+ @d.drop_function('tf', :if_exists=>true, :cascade=>true, :args=>%w'integer integer')
627
+ @d.drop_language(:plpgsql, :if_exists=>true, :cascade=>true)
628
+ @d.drop_trigger(:test5, :identity, :if_exists=>true, :cascade=>true)
629
+ end
630
+
631
+ specify "#create_function and #drop_function should create and drop functions" do
632
+ proc{@d['SELECT tf()'].all}.should raise_error(Sequel::DatabaseError)
633
+ args = ['tf', 'SELECT 1', {:returns=>:integer}]
634
+ @d.create_function_sql(*args).should =~ /\A\s*CREATE FUNCTION tf\(\)\s+RETURNS integer\s+LANGUAGE SQL\s+AS 'SELECT 1'\s*\z/
635
+ @d.create_function(*args)
636
+ rows = @d['SELECT tf()'].all.should == [{:tf=>1}]
637
+ @d.drop_function_sql('tf').should == 'DROP FUNCTION tf()'
638
+ @d.drop_function('tf')
639
+ proc{@d['SELECT tf()'].all}.should raise_error(Sequel::DatabaseError)
640
+ end
641
+
642
+ specify "#create_function and #drop_function should support options" do
643
+ args = ['tf', 'SELECT $1 + $2', {:args=>[[:integer, :a], :integer], :replace=>true, :returns=>:integer, :language=>'SQL', :behavior=>:immutable, :strict=>true, :security_definer=>true, :cost=>2, :set=>{:search_path => 'public'}}]
644
+ @d.create_function_sql(*args).should =~ /\A\s*CREATE OR REPLACE FUNCTION tf\(a integer, integer\)\s+RETURNS integer\s+LANGUAGE SQL\s+IMMUTABLE\s+STRICT\s+SECURITY DEFINER\s+COST 2\s+SET search_path = public\s+AS 'SELECT \$1 \+ \$2'\s*\z/
645
+ @d.create_function(*args)
646
+ # Make sure replace works
647
+ @d.create_function(*args)
648
+ rows = @d['SELECT tf(1, 2)'].all.should == [{:tf=>3}]
649
+ args = ['tf', {:if_exists=>true, :cascade=>true, :args=>[[:integer, :a], :integer]}]
650
+ @d.drop_function_sql(*args).should == 'DROP FUNCTION IF EXISTS tf(a integer, integer) CASCADE'
651
+ @d.drop_function(*args)
652
+ # Make sure if exists works
653
+ @d.drop_function(*args)
654
+ end
655
+
656
+ specify "#create_language and #drop_language should create and drop languages" do
657
+ @d.create_language_sql(:plpgsql).should == 'CREATE LANGUAGE plpgsql'
658
+ @d.create_language(:plpgsql)
659
+ proc{@d.create_language(:plpgsql)}.should raise_error(Sequel::DatabaseError)
660
+ @d.drop_language_sql(:plpgsql).should == 'DROP LANGUAGE plpgsql'
661
+ @d.drop_language(:plpgsql)
662
+ proc{@d.drop_language(:plpgsql)}.should raise_error(Sequel::DatabaseError)
663
+ @d.create_language_sql(:plpgsql, :trusted=>true, :handler=>:a, :validator=>:b).should == 'CREATE TRUSTED LANGUAGE plpgsql HANDLER a VALIDATOR b'
664
+ @d.drop_language_sql(:plpgsql, :if_exists=>true, :cascade=>true).should == 'DROP LANGUAGE IF EXISTS plpgsql CASCADE'
665
+ # Make sure if exists works
666
+ @d.drop_language(:plpgsql, :if_exists=>true, :cascade=>true)
667
+ end
668
+
669
+ specify "#create_trigger and #drop_trigger should create and drop triggers" do
670
+ @d.create_language(:plpgsql)
671
+ @d.create_function(:tf, 'BEGIN IF NEW.value IS NULL THEN RAISE EXCEPTION \'Blah\'; END IF; RETURN NEW; END;', :language=>:plpgsql, :returns=>:trigger)
672
+ @d.create_trigger_sql(:test, :identity, :tf, :each_row=>true).should == 'CREATE TRIGGER identity BEFORE INSERT OR UPDATE OR DELETE ON public.test FOR EACH ROW EXECUTE PROCEDURE tf()'
673
+ @d.create_trigger(:test, :identity, :tf, :each_row=>true)
674
+ @d[:test].insert(:name=>'a', :value=>1)
675
+ @d[:test].filter(:name=>'a').all.should == [{:name=>'a', :value=>1}]
676
+ proc{@d[:test].filter(:name=>'a').update(:value=>nil)}.should raise_error(Sequel::DatabaseError)
677
+ @d[:test].filter(:name=>'a').all.should == [{:name=>'a', :value=>1}]
678
+ @d[:test].filter(:name=>'a').update(:value=>3)
679
+ @d[:test].filter(:name=>'a').all.should == [{:name=>'a', :value=>3}]
680
+ @d.drop_trigger_sql(:test, :identity).should == 'DROP TRIGGER identity ON public.test'
681
+ @d.drop_trigger(:test, :identity)
682
+ @d.create_trigger_sql(:test, :identity, :tf, :after=>true, :events=>:insert, :args=>[1, 'a']).should == 'CREATE TRIGGER identity AFTER INSERT ON public.test EXECUTE PROCEDURE tf(1, \'a\')'
683
+ @d.drop_trigger_sql(:test, :identity, :if_exists=>true, :cascade=>true).should == 'DROP TRIGGER IF EXISTS identity ON public.test CASCADE'
684
+ # Make sure if exists works
685
+ @d.drop_trigger(:test, :identity, :if_exists=>true, :cascade=>true)
686
+ end
687
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ unless Object.const_defined?('Sequel')
3
+ $:.unshift(File.join(File.dirname(__FILE__), "../../lib/"))
4
+ require 'sequel_core'
5
+ Sequel.quote_identifiers = false
6
+ end
7
+ begin
8
+ require File.join(File.dirname(__FILE__), '../spec_config.rb')
9
+ rescue LoadError
10
+ end