sequel_core 1.0.2 → 1.0.3

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,13 @@
1
+ === 1.0.3 (2008-01-17)
2
+
3
+ * Added support for UNSIGNED constraint, used in MySQL? (#127).
4
+
5
+ * Implemented constraint definitions inside Database#create_table.
6
+
7
+ * Fixed postgres adapter to define PGconn#async_exec as alias to #exec if not defined (for pure-ruby postgres driver).
8
+
9
+ * Added String#to_date. Updated mysql adapter to use String#to_date for mysql date types (thanks drfreeze).
10
+
1
11
  === 1.0.2 (2008-01-14)
2
12
 
3
13
  * Removed ConnectionPool, NumericExtensions. Added dependency on assistance.
data/Rakefile CHANGED
@@ -9,7 +9,7 @@ include FileUtils
9
9
  # Configuration
10
10
  ##############################################################################
11
11
  NAME = "sequel_core"
12
- VERS = "1.0.2"
12
+ VERS = "1.0.3"
13
13
  CLEAN.include ["**/.*.sw?", "pkg/*", ".config", "doc/*", "coverage/*"]
14
14
  RDOC_OPTS = [
15
15
  "--quiet",
@@ -13,11 +13,11 @@ class Mysql::Result
13
13
  7 => :to_time, # MYSQL_TYPE_TIMESTAMP
14
14
  8 => :to_i, # MYSQL_TYPE_LONGLONG
15
15
  9 => :to_i, # MYSQL_TYPE_INT24
16
- 10 => :to_time, # MYSQL_TYPE_DATE
16
+ 10 => :to_date, # MYSQL_TYPE_DATE
17
17
  11 => :to_time, # MYSQL_TYPE_TIME
18
18
  12 => :to_time, # MYSQL_TYPE_DATETIME
19
19
  13 => :to_i, # MYSQL_TYPE_YEAR
20
- 14 => :to_time, # MYSQL_TYPE_NEWDATE
20
+ 14 => :to_date, # MYSQL_TYPE_NEWDATE
21
21
  # 15 => :to_s # MYSQL_TYPE_VARCHAR
22
22
  # 16 => :to_s, # MYSQL_TYPE_BIT
23
23
  246 => :to_d, # MYSQL_TYPE_NEWDECIMAL
@@ -43,6 +43,10 @@ class PGconn
43
43
  status == PGconn::CONNECTION_OK
44
44
  end
45
45
 
46
+ unless instance_methods.include?('async_exec')
47
+ alias_method :async_exec, :exec
48
+ end
49
+
46
50
  def execute(sql)
47
51
  begin
48
52
  async_exec(sql)
@@ -52,7 +52,15 @@ class String
52
52
  rescue Exception => e
53
53
  raise Sequel::Error::InvalidValue, "Invalid time value '#{self}' (#{e.message})"
54
54
  end
55
- # Why does Time.parse('0000-00-00') bork and not return nil or some such?
55
+ end
56
+
57
+ # Converts a string into a Date object.
58
+ def to_date
59
+ begin
60
+ Date.parse(self)
61
+ rescue Exception => e
62
+ raise Sequel::Error::InvalidValue, "Invalid date value '#{self}' (#{e.message})"
63
+ end
56
64
  end
57
65
  end
58
66
 
@@ -40,6 +40,14 @@ module Sequel
40
40
  index(name) if opts[:index]
41
41
  end
42
42
 
43
+ def check(*args, &block)
44
+ @columns << {:name => nil, :type => :check, :check => block || args}
45
+ end
46
+
47
+ def constraint(name, *args, &block)
48
+ @columns << {:name => name, :type => :check, :check => block || args}
49
+ end
50
+
43
51
  def has_column?(name)
44
52
  @columns.each {|c| return true if c[:name] == name}
45
53
  false
@@ -31,6 +31,7 @@ module Sequel
31
31
  COMMA_SEPARATOR = ', '.freeze
32
32
  UNIQUE = ' UNIQUE'.freeze
33
33
  NOT_NULL = ' NOT NULL'.freeze
34
+ UNSIGNED = ' UNSIGNED'.freeze
34
35
  PRIMARY_KEY = ' PRIMARY KEY'.freeze
35
36
 
36
37
  TYPES = Hash.new {|h, k| k}
@@ -43,14 +44,22 @@ module Sequel
43
44
  def literal(v)
44
45
  schema_utility_dataset.literal(v)
45
46
  end
47
+
48
+ def expression_list(*args, &block)
49
+ schema_utility_dataset.expression_list(*args, &block)
50
+ end
46
51
 
47
52
  def column_definition_sql(column)
53
+ if column[:type] == :check
54
+ return constraint_definition_sql(column)
55
+ end
48
56
  sql = "#{literal(column[:name].to_sym)} #{TYPES[column[:type]]}"
49
57
  column[:size] ||= 255 if column[:type] == :varchar
50
58
  elements = column[:size] || column[:elements]
51
59
  sql << "(#{literal(elements)})" if elements
52
60
  sql << UNIQUE if column[:unique]
53
61
  sql << NOT_NULL if column[:null] == false
62
+ sql << UNSIGNED if column[:unsigned]
54
63
  sql << " DEFAULT #{literal(column[:default])}" if column.include?(:default)
55
64
  sql << PRIMARY_KEY if column[:primary_key]
56
65
  if column[:table]
@@ -61,6 +70,13 @@ module Sequel
61
70
  sql << " #{auto_increment_sql}" if column[:auto_increment]
62
71
  sql
63
72
  end
73
+
74
+ def constraint_definition_sql(column)
75
+ sql = column[:name] ? "CONSTRAINT #{literal(column[:name].to_sym)} " : ""
76
+
77
+ sql << "CHECK #{expression_list(column[:check], true)}"
78
+ sql
79
+ end
64
80
 
65
81
  def column_list_sql(columns)
66
82
  columns.map {|c| column_definition_sql(c)}.join(COMMA_SEPARATOR)
@@ -299,3 +299,13 @@ context "String#to_time" do
299
299
  end
300
300
  end
301
301
 
302
+ context "String#to_date" do
303
+ specify "should convert the string into a Date object" do
304
+ "2007-07-11".to_date.should == Date.parse("2007-07-11")
305
+ end
306
+
307
+ specify "should raise Error::InvalidValue for an invalid date" do
308
+ proc {'0000-00-00'.to_time}.should raise_error(Sequel::Error::InvalidValue)
309
+ end
310
+ end
311
+
@@ -7,6 +7,8 @@ describe Sequel::Schema::Generator do
7
7
  column :body, :text
8
8
  foreign_key :parent_id
9
9
  primary_key :id
10
+ check 'price > 100'
11
+ constraint(:xxx) {:yyy == :zzz}
10
12
  index :title
11
13
  index [:title, :body]
12
14
  end
@@ -19,8 +21,8 @@ describe Sequel::Schema::Generator do
19
21
  end
20
22
  end
21
23
 
22
- it "counts primary key as column" do
23
- @columns.size.should == 4
24
+ it "counts primary key, column and constraint definitions as columns" do
25
+ @columns.size.should == 6
24
26
  end
25
27
 
26
28
  it "places primary key first" do
@@ -52,6 +54,16 @@ describe Sequel::Schema::Generator do
52
54
  @generator.has_column?(:foo).should_not be_true
53
55
  end
54
56
 
57
+ it "creates constraints" do
58
+ @columns[4][:name].should == nil
59
+ @columns[4][:type].should == :check
60
+ @columns[4][:check].should == ['price > 100']
61
+
62
+ @columns[5][:name].should == :xxx
63
+ @columns[5][:type].should == :check
64
+ @columns[5][:check].should be_a_kind_of(Proc)
65
+ end
66
+
55
67
  it "creates indexes" do
56
68
  @indexes[0][:columns].should include(:title)
57
69
  @indexes[1][:columns].should include(:title)
data/spec/schema_spec.rb CHANGED
@@ -69,6 +69,13 @@ context "DB#create_table" do
69
69
  @db.sqls.should == ["CREATE TABLE cats (id integer, name text UNIQUE)"]
70
70
  end
71
71
 
72
+ specify "should accept unsigned definition" do
73
+ @db.create_table(:cats) do
74
+ integer :value, :unsigned => true
75
+ end
76
+ @db.sqls.should == ["CREATE TABLE cats (value integer UNSIGNED)"]
77
+ end
78
+
72
79
  specify "should accept [SET|ENUM](...) types" do
73
80
  @db.create_table(:cats) do
74
81
  set :color, :elements => ['black', 'tricolor', 'grey']
@@ -205,6 +212,34 @@ context "DB#create_table" do
205
212
  end
206
213
  @db.sqls.should == ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_name_index ON cats (id, name)"]
207
214
  end
215
+
216
+ specify "should accept unnamed constraint definitions" do
217
+ @db.create_table(:cats) do
218
+ integer :score
219
+ check {:x > 0 && :y < 1}
220
+ end
221
+ @db.sqls.should == ["CREATE TABLE cats (score integer, CHECK (((x > 0) AND (y < 1))))"]
222
+ @db.sqls.clear
223
+
224
+ @db.create_table(:cats) do
225
+ check 'price < ?', 100
226
+ end
227
+ @db.sqls.should == ["CREATE TABLE cats (CHECK (price < 100))"]
228
+ end
229
+
230
+ specify "should accept named constraint definitions" do
231
+ @db.create_table(:cats) do
232
+ integer :score
233
+ constraint :valid_score, 'score <= 100'
234
+ end
235
+ @db.sqls.should == ["CREATE TABLE cats (score integer, CONSTRAINT valid_score CHECK (score <= 100))"]
236
+ @db.sqls.clear
237
+
238
+ @db.create_table(:cats) do
239
+ constraint(:blah_blah) {:x > 0 && :y < 1}
240
+ end
241
+ @db.sqls.should == ["CREATE TABLE cats (CONSTRAINT blah_blah CHECK (((x > 0) AND (y < 1))))"]
242
+ end
208
243
  end
209
244
 
210
245
  context "DB#create_table!" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
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: 2008-01-14 00:00:00 +02:00
12
+ date: 2008-01-17 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency