sequel 0.4.2.1 → 0.4.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ === 0.4.2.2 (2007-12-10)
2
+
3
+ * Improved code coverage.
4
+
5
+ * Fixed Dataset#count to work properly with datasets with fixed SQL (when using #fetch).
6
+
7
+ * Added Model.create_with_params method that filters the given parameters accordring to the model's columns (thanks Aman Gupta).
8
+
1
9
  === 0.4.2.1 (2007-12-09)
2
10
 
3
11
  * Refactored and fixed Dataset#reverse_order to work with field quoting (thanks Christian).
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'fileutils'
6
6
  include FileUtils
7
7
 
8
8
  NAME = "sequel"
9
- VERS = "0.4.2.1"
9
+ VERS = "0.4.2.2"
10
10
  CLEAN.include ['**/.*.sw?', 'pkg/*', '.config', 'doc/*', 'coverage/*']
11
11
  RDOC_OPTS = ['--quiet', '--title', "Sequel: Concise ORM for Ruby",
12
12
  "--opname", "index.html",
@@ -98,8 +98,7 @@ module Sequel
98
98
  if block
99
99
  ds.fetch_rows(sql, &block)
100
100
  else
101
- ds.meta_def(:select_sql) {|*args| sql}
102
- ds.meta_def(:sql) {|*args| sql}
101
+ ds.opts[:sql] = sql
103
102
  ds
104
103
  end
105
104
  end
@@ -35,9 +35,10 @@ module Sequel
35
35
  if columns.empty?
36
36
  WILDCARD
37
37
  else
38
- columns.map do |i|
38
+ m = columns.map do |i|
39
39
  i.is_a?(Hash) ? i.map {|kv| "#{literal(kv[0])} AS #{kv[1]}"} : literal(i)
40
- end.join(COMMA_SEPARATOR)
40
+ end
41
+ m.join(COMMA_SEPARATOR)
41
42
  end
42
43
  end
43
44
 
@@ -47,7 +48,7 @@ module Sequel
47
48
  raise SequelError, 'No source specified for query'
48
49
  end
49
50
  auto_alias_count = 0
50
- source.map do |i|
51
+ m = source.map do |i|
51
52
  case i
52
53
  when Dataset:
53
54
  auto_alias_count += 1
@@ -58,7 +59,8 @@ module Sequel
58
59
  else
59
60
  i
60
61
  end
61
- end.join(COMMA_SEPARATOR)
62
+ end
63
+ m.join(COMMA_SEPARATOR)
62
64
  end
63
65
 
64
66
  NULL = "NULL".freeze
@@ -540,11 +542,15 @@ module Sequel
540
542
  end
541
543
  end
542
544
 
543
- SELECT_COUNT = {:select => ["COUNT(*)".lit], :order => nil}.freeze
545
+ STOCK_COUNT_OPTS = {:select => ["COUNT(*)".lit], :order => nil}.freeze
544
546
 
545
547
  # Returns the number of records in the dataset.
546
548
  def count
547
- single_value(SELECT_COUNT).to_i
549
+ opts = @opts[:sql] ? \
550
+ {:sql => "SELECT COUNT(*) FROM (#{@opts[:sql]}) AS c", :order => nil} : \
551
+ STOCK_COUNT_OPTS
552
+
553
+ single_value(opts).to_i
548
554
  end
549
555
  end
550
556
  end
data/lib/sequel/model.rb CHANGED
@@ -250,8 +250,7 @@ module Sequel
250
250
  # Ticket.find {:price == 17} # => Dataset
251
251
  #
252
252
  def self.find(*args, &block)
253
- dataset.filter(*args, &block).limit(1).first
254
- # dataset[cond.is_a?(Hash) ? cond : primary_key_hash(cond)]
253
+ dataset.filter(*args, &block).first
255
254
  end
256
255
 
257
256
  def self.[](*args)
@@ -273,8 +272,11 @@ module Sequel
273
272
 
274
273
  # Like delete_all, but invokes before_destroy and after_destroy hooks if used.
275
274
  def self.destroy_all
276
- has_hooks?(:before_destroy) || has_hooks?(:after_destroy) ? \
277
- dataset.destroy : dataset.delete
275
+ if has_hooks?(:before_destroy) || has_hooks?(:after_destroy)
276
+ dataset.destroy
277
+ else
278
+ dataset.delete
279
+ end
278
280
  end
279
281
  # Deletes all records.
280
282
  def self.delete_all
@@ -135,6 +135,13 @@ module Sequel
135
135
  end
136
136
  end
137
137
 
138
+ class << self
139
+ def create_with_params(params)
140
+ create(params.reject {|k, v| !columns.include?(k.to_sym)})
141
+ end
142
+ alias_method :create_with, :create_with_params
143
+ end
144
+
138
145
  # Returns (naked) dataset bound to current instance.
139
146
  def this
140
147
  @this ||= self.class.dataset.filter(:id => @values[:id]).limit(1).naked
@@ -275,5 +275,9 @@ context "Symbol" do
275
275
  specify "should support cast function" do
276
276
  :abc.cast_as(:integer).to_s(@ds).should == "cast(abc AS integer)"
277
277
  end
278
+
279
+ specify "should raise NoMethodError for non-uppercase invalid methods" do
280
+ proc {:abc.dfaxs}.should raise_error(NoMethodError)
281
+ end
278
282
  end
279
283
 
data/spec/dataset_spec.rb CHANGED
@@ -902,6 +902,12 @@ context "Dataset#count" do
902
902
  @dataset.filter {:abc < 30}.count.should == 1
903
903
  @c.sql.should == 'SELECT COUNT(*) FROM test WHERE (abc < 30)'
904
904
  end
905
+
906
+ specify "should count properly for datasets with fixed sql" do
907
+ @dataset.opts[:sql] = "select abc from xyz"
908
+ @dataset.count.should == 1
909
+ @c.sql.should == "SELECT COUNT(*) FROM (select abc from xyz) AS c"
910
+ end
905
911
  end
906
912
 
907
913
 
@@ -2018,6 +2024,8 @@ context "Dataset#transform" do
2018
2024
  specify "should change the dataset to transform values loaded from the database" do
2019
2025
  @ds.raw = {:x => Marshal.dump([1, 2, 3]), :y => 'hello'}
2020
2026
  @ds.first.should == {:x => [1, 2, 3], :y => 'hello'}
2027
+ @ds.raw = {:x => Marshal.dump([1, 2, 3]), :y => 'hello'}
2028
+ @ds.all.should == [{:x => [1, 2, 3], :y => 'hello'}]
2021
2029
  end
2022
2030
 
2023
2031
  specify "should change the dataset to transform values saved to the database" do
data/spec/model_spec.rb CHANGED
@@ -312,7 +312,6 @@ context "Model#serialize" do
312
312
  ]
313
313
  end
314
314
 
315
-
316
315
  specify "should support calling after the class is defined" do
317
316
  @c = Class.new(Sequel::Model(:items)) do
318
317
  no_primary_key
@@ -498,7 +497,8 @@ context "Model#save" do
498
497
  o = @c.new(:id => 3, :x => 1)
499
498
  o.save
500
499
 
501
- MODEL_DB.sqls.first.should == "UPDATE items SET x = 1, id = 3 WHERE (id = 3)"
500
+ MODEL_DB.sqls.first.should =~
501
+ /UPDATE items SET (id = 3, x = 1|x = 1, id = 3) WHERE \(id = 3\)/
502
502
  end
503
503
 
504
504
  specify "should update only the given columns if given" do
@@ -725,6 +725,7 @@ context "Model.[]" do
725
725
  end
726
726
 
727
727
  context "Model.fetch" do
728
+
728
729
  setup do
729
730
  MODEL_DB.reset
730
731
  @c = Class.new(Sequel::Model(:items))
@@ -733,6 +734,12 @@ context "Model.fetch" do
733
734
  specify "should return instances of Model" do
734
735
  @c.fetch("SELECT * FROM items").first.should be_a_kind_of(@c)
735
736
  end
737
+
738
+ specify "should return true for .empty? and not raise an error on empty selection" do
739
+ rows = @c.fetch("SELECT * FROM items WHERE FALSE")
740
+ @c.class_def(:fetch_rows) {|sql| yield({:count => 0})}
741
+ proc {rows.empty?}.should_not raise_error
742
+ end
736
743
  end
737
744
 
738
745
  context "A cached model" do
@@ -1213,4 +1220,122 @@ context "A model using a plugin" do
1213
1220
  c.should respond_to(:deff)
1214
1221
  c.deff.should == {:a => 1, :b => 2}
1215
1222
  end
1223
+ end
1224
+
1225
+ context "Model#create_with_params" do
1226
+ setup do
1227
+ MODEL_DB.reset
1228
+
1229
+ @c = Class.new(Sequel::Model(:items)) do
1230
+ def self.columns; [:x, :y]; end
1231
+ end
1232
+ end
1233
+
1234
+ specify "should filter the given params using the model columns" do
1235
+ @c.create_with_params(:x => 1, :z => 2)
1236
+ MODEL_DB.sqls.first.should == "INSERT INTO items (x) VALUES (1)"
1237
+
1238
+ MODEL_DB.reset
1239
+ @c.create_with_params(:y => 1, :abc => 2)
1240
+ MODEL_DB.sqls.first.should == "INSERT INTO items (y) VALUES (1)"
1241
+ end
1242
+
1243
+ specify "should be aliased by create_with" do
1244
+ @c.create_with(:x => 1, :z => 2)
1245
+ MODEL_DB.sqls.first.should == "INSERT INTO items (x) VALUES (1)"
1246
+
1247
+ MODEL_DB.reset
1248
+ @c.create_with(:y => 1, :abc => 2)
1249
+ MODEL_DB.sqls.first.should == "INSERT INTO items (y) VALUES (1)"
1250
+ end
1251
+ end
1252
+
1253
+ context "Model.find_or_create" do
1254
+ setup do
1255
+ MODEL_DB.reset
1256
+ @c = Class.new(Sequel::Model(:items)) do
1257
+ no_primary_key
1258
+ end
1259
+ end
1260
+
1261
+ specify "should find the record" do
1262
+ @c.find_or_create(:x => 1)
1263
+ MODEL_DB.sqls.should == ["SELECT * FROM items WHERE (x = 1) LIMIT 1"]
1264
+
1265
+ MODEL_DB.reset
1266
+ end
1267
+
1268
+ specify "should create the record if not found" do
1269
+ @c.meta_def(:find) do |*args|
1270
+ dataset.filter(*args).first
1271
+ nil
1272
+ end
1273
+
1274
+ @c.find_or_create(:x => 1)
1275
+ MODEL_DB.sqls.should == [
1276
+ "SELECT * FROM items WHERE (x = 1) LIMIT 1",
1277
+ "INSERT INTO items (x) VALUES (1)"
1278
+ ]
1279
+ end
1280
+ end
1281
+
1282
+ context "Model.delete_all" do
1283
+ setup do
1284
+ MODEL_DB.reset
1285
+ @c = Class.new(Sequel::Model(:items)) do
1286
+ no_primary_key
1287
+ end
1288
+
1289
+ @c.dataset.meta_def(:delete) {MODEL_DB << delete_sql}
1290
+ end
1291
+
1292
+ specify "should delete all records in the dataset" do
1293
+ @c.delete_all
1294
+ MODEL_DB.sqls.should == ["DELETE FROM items"]
1295
+ end
1296
+ end
1297
+
1298
+ context "Model.destroy_all" do
1299
+ setup do
1300
+ MODEL_DB.reset
1301
+ @c = Class.new(Sequel::Model(:items)) do
1302
+ no_primary_key
1303
+ end
1304
+
1305
+ @c.dataset.meta_def(:delete) {MODEL_DB << delete_sql}
1306
+ end
1307
+
1308
+ specify "should delete all records in the dataset" do
1309
+ @c.destroy_all
1310
+ MODEL_DB.sqls.should == ["DELETE FROM items"]
1311
+ end
1312
+ end
1313
+
1314
+ context "Model.join" do
1315
+ setup do
1316
+ MODEL_DB.reset
1317
+ @c = Class.new(Sequel::Model(:items)) do
1318
+ no_primary_key
1319
+ end
1320
+ end
1321
+
1322
+ specify "should format proper SQL" do
1323
+ @c.join(:atts, :item_id => :id).sql.should == \
1324
+ "SELECT items.* FROM items INNER JOIN atts ON (atts.item_id = items.id)"
1325
+ end
1326
+ end
1327
+
1328
+ context "Model.all" do
1329
+ setup do
1330
+ MODEL_DB.reset
1331
+ @c = Class.new(Sequel::Model(:items)) do
1332
+ no_primary_key
1333
+ end
1334
+
1335
+ @c.dataset.meta_def(:all) {1234}
1336
+ end
1337
+
1338
+ specify "should return all records in the dataset" do
1339
+ @c.all.should == 1234
1340
+ end
1216
1341
  end
@@ -326,3 +326,14 @@ context "Proc#to_sql" do
326
326
  proc {:x == ("%d" % prod).lit}.to_sql.should == "(x = 1)"
327
327
  end
328
328
  end
329
+
330
+ context "Proc#to_sql stock" do
331
+ specify "should not support regexps" do
332
+ db = Sequel::Database.new
333
+ ds = db[:items]
334
+
335
+ p = proc {:x =~ /abc/}
336
+ proc {ds.proc_to_sql(p)}.should raise_error(SequelError)
337
+ end
338
+ end
339
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2.1
4
+ version: 0.4.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2007-12-09 00:00:00 +02:00
12
+ date: 2007-12-11 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -54,78 +54,77 @@ files:
54
54
  - README
55
55
  - Rakefile
56
56
  - bin/sequel
57
- - doc/rdoc
57
+ - spec/dataset_spec.rb
58
+ - spec/connection_pool_spec.rb
59
+ - spec/database_spec.rb
60
+ - spec/core_ext_spec.rb
58
61
  - spec/adapters
59
- - spec/adapters/informix_spec.rb
62
+ - spec/adapters/sqlite_spec.rb
60
63
  - spec/adapters/mysql_spec.rb
61
- - spec/adapters/oracle_spec.rb
62
64
  - spec/adapters/postgres_spec.rb
63
- - spec/adapters/sqlite_spec.rb
64
- - spec/array_keys_spec.rb
65
- - spec/connection_pool_spec.rb
66
- - spec/core_ext_spec.rb
67
- - spec/core_sql_spec.rb
68
- - spec/database_spec.rb
69
- - spec/dataset_spec.rb
65
+ - spec/adapters/oracle_spec.rb
66
+ - spec/adapters/informix_spec.rb
70
67
  - spec/migration_spec.rb
71
- - spec/model_spec.rb
72
- - spec/pretty_table_spec.rb
73
- - spec/schema_generator_spec.rb
74
68
  - spec/schema_spec.rb
75
- - spec/sequelizer_spec.rb
69
+ - spec/schema_generator_spec.rb
70
+ - spec/pretty_table_spec.rb
76
71
  - spec/spec_helper.rb
72
+ - spec/model_spec.rb
73
+ - spec/sequelizer_spec.rb
74
+ - spec/array_keys_spec.rb
77
75
  - spec/worker_spec.rb
76
+ - spec/core_sql_spec.rb
78
77
  - lib/sequel
79
- - lib/sequel/adapters
80
- - lib/sequel/adapters/adapter_skeleton.rb
81
- - lib/sequel/adapters/ado.rb
82
- - lib/sequel/adapters/db2.rb
83
- - lib/sequel/adapters/dbi.rb
84
- - lib/sequel/adapters/informix.rb
85
- - lib/sequel/adapters/jdbc.rb
86
- - lib/sequel/adapters/mysql.rb
87
- - lib/sequel/adapters/odbc.rb
88
- - lib/sequel/adapters/odbc_mssql.rb
89
- - lib/sequel/adapters/openbase.rb
90
- - lib/sequel/adapters/oracle.rb
91
- - lib/sequel/adapters/postgres.rb
92
- - lib/sequel/adapters/sqlite.rb
93
- - lib/sequel/ado.rb
94
- - lib/sequel/array_keys.rb
78
+ - lib/sequel/pretty_table.rb
79
+ - lib/sequel/model.rb
80
+ - lib/sequel/schema.rb
81
+ - lib/sequel/database.rb
82
+ - lib/sequel/dataset.rb
95
83
  - lib/sequel/connection_pool.rb
96
84
  - lib/sequel/core_ext.rb
97
- - lib/sequel/core_sql.rb
98
- - lib/sequel/database.rb
85
+ - lib/sequel/error.rb
99
86
  - lib/sequel/dataset
100
- - lib/sequel/dataset/convenience.rb
101
- - lib/sequel/dataset/sequelizer.rb
102
87
  - lib/sequel/dataset/sql.rb
103
- - lib/sequel/dataset.rb
104
- - lib/sequel/db2.rb
105
- - lib/sequel/dbi.rb
106
- - lib/sequel/error.rb
107
- - lib/sequel/informix.rb
88
+ - lib/sequel/dataset/sequelizer.rb
89
+ - lib/sequel/dataset/convenience.rb
108
90
  - lib/sequel/migration.rb
91
+ - lib/sequel/schema
92
+ - lib/sequel/schema/schema_sql.rb
93
+ - lib/sequel/schema/schema_generator.rb
109
94
  - lib/sequel/model
110
- - lib/sequel/model/base.rb
111
- - lib/sequel/model/caching.rb
112
- - lib/sequel/model/hooks.rb
113
- - lib/sequel/model/plugins.rb
114
95
  - lib/sequel/model/record.rb
115
96
  - lib/sequel/model/relations.rb
116
97
  - lib/sequel/model/schema.rb
117
- - lib/sequel/model.rb
118
- - lib/sequel/mysql.rb
119
- - lib/sequel/odbc.rb
98
+ - lib/sequel/model/hooks.rb
99
+ - lib/sequel/model/base.rb
100
+ - lib/sequel/model/caching.rb
101
+ - lib/sequel/model/plugins.rb
102
+ - lib/sequel/array_keys.rb
103
+ - lib/sequel/worker.rb
104
+ - lib/sequel/core_sql.rb
105
+ - lib/sequel/informix.rb
106
+ - lib/sequel/adapters
107
+ - lib/sequel/adapters/dbi.rb
108
+ - lib/sequel/adapters/sqlite.rb
109
+ - lib/sequel/adapters/ado.rb
110
+ - lib/sequel/adapters/mysql.rb
111
+ - lib/sequel/adapters/oracle.rb
112
+ - lib/sequel/adapters/postgres.rb
113
+ - lib/sequel/adapters/db2.rb
114
+ - lib/sequel/adapters/odbc.rb
115
+ - lib/sequel/adapters/informix.rb
116
+ - lib/sequel/adapters/odbc_mssql.rb
117
+ - lib/sequel/adapters/openbase.rb
118
+ - lib/sequel/adapters/jdbc.rb
119
+ - lib/sequel/adapters/adapter_skeleton.rb
120
+ - lib/sequel/dbi.rb
121
+ - lib/sequel/sqlite.rb
122
+ - lib/sequel/ado.rb
120
123
  - lib/sequel/oracle.rb
124
+ - lib/sequel/mysql.rb
121
125
  - lib/sequel/postgres.rb
122
- - lib/sequel/pretty_table.rb
123
- - lib/sequel/schema
124
- - lib/sequel/schema/schema_generator.rb
125
- - lib/sequel/schema/schema_sql.rb
126
- - lib/sequel/schema.rb
127
- - lib/sequel/sqlite.rb
128
- - lib/sequel/worker.rb
126
+ - lib/sequel/odbc.rb
127
+ - lib/sequel/db2.rb
129
128
  - lib/sequel.rb
130
129
  - CHANGELOG
131
130
  has_rdoc: true