Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make the sqlite adapter respect the Sequel.datetime_class setting, fo…

…r timestamp and datetime types
  • Loading branch information...
commit 22a57f2a9e43c000f1d145d64e22527423fba58c 1 parent a1ffd73
@jeremyevans authored
View
2  CHANGELOG
@@ -1,5 +1,7 @@
=== HEAD
+* Make the sqlite adapter respect the Sequel.datetime_class setting, for timestamp and datetime types (jeremyevans)
+
* Enhance the CASE statement support to include an optional expression (jarredholman)
* Default to using the simple language if no language is specified for a full text index on PostgreSQL (michalbugno)
View
12 lib/sequel_core/adapters/sqlite.rb
@@ -8,6 +8,7 @@ module SQLite
# Database class for PostgreSQL databases used with Sequel and the
# ruby-sqlite3 driver.
class Database < Sequel::Database
+ UNIX_EPOCH_TIME_FORMAT = /\A\d+\z/.freeze
include ::Sequel::SQLite::DatabaseMethods
set_adapter_scheme :sqlite
@@ -30,10 +31,13 @@ def connect(server)
db = ::SQLite3::Database.new(opts[:database])
db.busy_timeout(opts.fetch(:timeout, 5000))
db.type_translation = true
- # fix for timestamp translation
- db.translator.add_translator("timestamp") do |t, v|
- v =~ /^\d+$/ ? Time.at(v.to_i) : Time.parse(v)
- end
+ # Handle datetime's with Sequel.datetime_class
+ prok = proc do |t,v|
+ v = Time.at(v.to_i).iso8601 if UNIX_EPOCH_TIME_FORMAT.match(v)
+ v.to_sequel_time
+ end
+ db.translator.add_translator("timestamp", &prok)
+ db.translator.add_translator("datetime", &prok)
db
end
View
22 spec/adapters/sqlite_spec.rb
@@ -9,6 +9,9 @@
before do
@db = SQLITE_DB
end
+ after do
+ Sequel.datetime_class = Time
+ end
if SQLITE_DB.auto_vacuum == :none
specify "should support getting pragma values" do
@@ -128,12 +131,17 @@ def @db.ret_commit
@db[:items2].count.should == 2
end
- specify "should support timestamps" do
- t1 = Time.at(Time.now.to_i) #normalize time
- @db.create_table!(:time) {timestamp :t}
- @db[:time] << {:t => t1}
- x = @db[:time].first[:t]
- [t1.iso8601, t1.to_s].should include(x.respond_to?(:iso8601) ? x.iso8601 : x.to_s)
+ specify "should support timestamps and datetimes and respect datetime_class" do
+ @db.create_table!(:time){timestamp :t; datetime :d}
+ t1 = Time.at(1)
+ @db[:time] << {:t => t1, :d => t1.to_i}
+ @db[:time] << {:t => t1.to_i, :d => t1}
+ @db[:time].map(:t).should == [t1, t1]
+ @db[:time].map(:d).should == [t1, t1]
+ t2 = t1.iso8601.to_datetime
+ Sequel.datetime_class = DateTime
+ @db[:time].map(:t).should == [t2, t2]
+ @db[:time].map(:d).should == [t2, t2]
end
specify "should support sequential primary keys" do
@@ -164,7 +172,7 @@ def @db.ret_commit
specify "should get the schema all database tables if no table name is used" do
@db.create_table!(:time2) {timestamp :t}
- @db.schema(:time2, :reload=>true).should == @db.schema(nil, :reload=>true)[:time]
+ @db.schema(:time2, :reload=>true).should == @db.schema(nil, :reload=>true)[:time2]
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.