Re: DateTime and date/time fields

2008-06-04 Thread Farrel

On Jun 4, 1:21 am, Jeremy Evans [EMAIL PROTECTED] wrote:
 Yes, and that's the current expected behavior.  As I mentioned, I'll
 accept patches to fix it so that DateTime is returned by the adapter.
 You should change the before_* hooks to use a DateTime if you want to
 use that, though it really won't matter much for your case.
 Alternatively, you can override things to always provide Time instead
 of DateTime.  The only reason that DateTime is the default is that it
 has a larger range than Time, so it works when Time does not (dates
 before 1900, for example).

I'm a bit confused here. In your previous email you said that DateTime
is used in models with typecasting, which I have enabled, but I'm not
seeing that. Using DateTime in my before_create/update blocks on
Organisation.create the updated_at field becomes Time, then DateTime
on a save and then Time again after reload. If I use Time (as I did in
my previous email) I get the same result.

This kind of sucks because Time and DateTime are not compatiable at
all and it breaks the POLS in a big way because given an object I do
not know if the updated_at field is Time or DateTime because even if I
use DateTime or Time exclusively in my own code calling save or reload
anywhere previously has the potential to change the class without my
knowing. The only option is to call reload whenever a function is
passed a sequel object it didn't create which is definitely not an
ideal solution.

If you point me to the file that needs patching I'll have a look at it
and see what I can do but I am not familiar with the Sequel code base.

Farrel
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sequel-talk group.
To post to this group, send email to sequel-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~--~~~~--~~--~--~---



Re: DateTime and date/time fields

2008-06-04 Thread Jeremy Evans

On Jun 4, 12:40 am, Farrel [EMAIL PROTECTED] wrote:
 On Jun 4, 1:21 am, Jeremy Evans [EMAIL PROTECTED] wrote:

  Yes, and that's the current expected behavior.  As I mentioned, I'll
  accept patches to fix it so that DateTime is returned by the adapter.
  You should change the before_* hooks to use a DateTime if you want to
  use that, though it really won't matter much for your case.
  Alternatively, you can override things to always provide Time instead
  of DateTime.  The only reason that DateTime is the default is that it
  has a larger range than Time, so it works when Time does not (dates
  before 1900, for example).

 I'm a bit confused here. In your previous email you said that DateTime
 is used in models with typecasting, which I have enabled, but I'm not
 seeing that. Using DateTime in my before_create/update blocks on
 Organisation.create the updated_at field becomes Time, then DateTime
 on a save and then Time again after reload. If I use Time (as I did in
 my previous email) I get the same result.

DateTime is used in models with typecasting.  What that means is if
you assign
a value to the model object, it gets casted to a DateTime.  For
example:

  model.datetime_field = '1800-01-01 10:20:30'
  model.datetime_field # DateTime ...

What may not have been clear is that when you load data from the
database, the model does not typecast it, so if the database adapter
gives you a Time, it doesn't get converted to a DateTime.

 This kind of sucks because Time and DateTime are not compatiable at
 all and it breaks the POLS in a big way because given an object I do
 not know if the updated_at field is Time or DateTime because even if I
 use DateTime or Time exclusively in my own code calling save or reload
 anywhere previously has the potential to change the class without my
 knowing. The only option is to call reload whenever a function is
 passed a sequel object it didn't create which is definitely not an
 ideal solution.

The easiest workaround currently would be to override
Datebase#typecast_value for your adapter and have it return a Time for
a :datetime type instead of a DateTime.

As I mentioned, the best long term solution is to have the Database
adapters return DateTimes intead of Times.  I suppose we could make
the use of Time or DateTime an option, I'm amenable to that if it
doesn't complicate things too much.

 If you point me to the file that needs patching I'll have a look at it
 and see what I can do but I am not familiar with the Sequel code base.

The database adapters are in would be in sequel_core/lib/sequel_core/
adapters.  Find your adapter and change the type conversion to use
DateTime instead of Time.  If you want to support choosing either,
modify Datebase#typecast_value in database.rb to return whichever
option is selected, and do the same in the adapter.

I agree that this is a problem that needs to be fixed.  I have added
it to my todo list for Sequel 2.1.  Obviously, patches will speed the
process up, but if you don't get to it, I will before 2.1.

Jeremy
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sequel-talk group.
To post to this group, send email to sequel-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~--~~~~--~~--~--~---



Re: DateTime and date/time fields

2008-06-04 Thread Farrel

On Jun 4, 4:46 pm, Jeremy Evans [EMAIL PROTECTED] wrote:
 What may not have been clear is that when you load data from the
 database, the model does not typecast it, so if the database adapter
 gives you a Time, it doesn't get converted to a DateTime.

Ah I see so this is why the problem occured.

 The database adapters are in would be in sequel_core/lib/sequel_core/
 adapters.  Find your adapter and change the type conversion to use
 DateTime instead of Time.  If you want to support choosing either,
 modify Datebase#typecast_value in database.rb to return whichever
 option is selected, and do the same in the adapter.

If I get some free time I'll have a go at a patch. Thanks for the help

Farrel
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sequel-talk group.
To post to this group, send email to sequel-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~--~~~~--~~--~--~---



Re: DateTime and date/time fields

2008-06-04 Thread Jeremy Evans

On Jun 4, 7:57 am, Farrel [EMAIL PROTECTED] wrote:
 On Jun 4, 4:46 pm, Jeremy Evans [EMAIL PROTECTED] wrote:

  What may not have been clear is that when you load data from the
  database, the model does not typecast it, so if the database adapter
  gives you a Time, it doesn't get converted to a DateTime.

 Ah I see so this is why the problem occured.

  The database adapters are in would be in sequel_core/lib/sequel_core/
  adapters.  Find your adapter and change the type conversion to use
  DateTime instead of Time.  If you want to support choosing either,
  modify Datebase#typecast_value in database.rb to return whichever
  option is selected, and do the same in the adapter.

 If I get some free time I'll have a go at a patch. Thanks for the help

I decided to fix this right away, see the following commits:

http://github.com/jeremyevans/sequel/commit/58106a92d7d516b41262946eb3c489adc021c0b7
http://github.com/jeremyevans/sequel/commit/fd5bbf055d667c68a0bbae29aabe9a946dd24ece

We are going to be releasing 2.0.1 soon for this fix and other
post-2.0.0 fixes (the Model.create_table fix, and the Oracle quoting
fix, and the refactoring of ComplexExpression).

Jeremy
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sequel-talk group.
To post to this group, send email to sequel-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~--~~~~--~~--~--~---



Re: DateTime and date/time fields

2008-06-03 Thread Farrel

On Jun 2, 6:59 pm, Jeremy Evans [EMAIL PROTECTED] wrote:
 The value you get from the database is adapter specific (I think Time
 is used in most cases).  When using models with typecasting, it will
 always cast to a DateTime and not a Time.  I'll accept patches for
 adapters to use a DateTime field instead of a Time field.

I've found some inconsistent code with Sequel's handling of Time and
DateTime. I have an Organisation class that sets the created_at and
updated_at fields when the object is created and subsequently updated.
When an object is created the class of updated_at is Time. On a
subsequent save it becomes DateTime and after the model is reloaded it
switches back to Time. Below is a capture of an IRb session which
shows this.

irb(main):001:0 o = Organisation.create
 ~ INSERT INTO organisations (created_at, updated_at, active,
uuid) VALUES (TIMESTAMP '2008-06-03 22:07:21.00', TIMESTAMP
'2008-06-03 22:07:21.00', true, 'c214427f-a82e-4f14-a7e2-
cf2d1d1d2815')
 ~ SELECT * FROM organisations WHERE (id = 2) LIMIT 1
= #Organisation
@values={:admin_user_id=nil, :phone_number=nil, :fax_number=nil, 
:created_at=Tue
Jun 03 22:07:21 +0200 2008, :name=nil, :updated_at=Tue Jun 03
22:07:21 +0200
2008, :postal_address=nil, :active=true, :street_address=nil, 
:uuid=c214427f-
a82e-4f14-a7e2-cf2d1d1d2815, :email_address=nil, :id=2}
irb(main):002:0 o.created_at.class
= Time
irb(main):003:0 o.updated_at.class
= Time
irb(main):004:0 o.name = test
= test
irb(main):005:0 o.save
 ~ UPDATE organisations SET admin_user_id = NULL, phone_number =
NULL, fax_number = NULL, created_at = TIMESTAMP '2008-06-03
22:07:21.00', name = 'test', updated_at = TIMESTAMP
'2008-06-03 22:07:52.00', postal_address = NULL, active =
true, street_address = NULL, uuid = 'c214427f-a82e-4f14-a7e2-
cf2d1d1d2815', email_address = NULL, id = 2 WHERE (id = 2)
= #Organisation
@values={:admin_user_id=nil, :phone_number=nil, :fax_number=nil, 
:created_at=Tue
Jun 03 22:07:21 +0200 2008, :name=test, :updated_at=#DateTime:
26509911359/10800,0,2299161, :postal_address=nil, :active=true, 
:street_address=nil, :uuid=c214427f-
a82e-4f14-a7e2-cf2d1d1d2815, :email_address=nil, :id=2}
irb(main):006:0 o.created_at.class
= Time
irb(main):007:0 o.updated_at.class
= DateTime
irb(main):008:0 o.reload
 ~ SELECT * FROM organisations WHERE (id = 2) LIMIT 1
= #Organisation
@values={:admin_user_id=nil, :phone_number=nil, :fax_number=nil, 
:created_at=Tue
Jun 03 22:07:21 +0200 2008, :name=test, :updated_at=Tue Jun 03
22:07:52 +0200
2008, :postal_address=nil, :active=true, :street_address=nil, 
:uuid=c214427f-
a82e-4f14-a7e2-cf2d1d1d2815, :email_address=nil, :id=2}
irb(main):009:0 o.updated_at.class
= Time

On line 3 and line 9 a A Time class is returned as the class of
updated_at, while on line 7 it's a DateTime.

Farrel
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sequel-talk group.
To post to this group, send email to sequel-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~--~~~~--~~--~--~---



Re: DateTime and date/time fields

2008-06-03 Thread Farrel

I should also add this is on a Postgres databases, Sequel 2.0 stable
gem, Ruby 1.8.6
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sequel-talk group.
To post to this group, send email to sequel-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~--~~~~--~~--~--~---



Re: DateTime and date/time fields

2008-06-03 Thread Farrel

Here's the code for the Organisation class

class Organisation  Sequel::Model

  before_validation do
if self.new?
  self.uuid = UUID.random_create.to_s
end
  end

  before_create do
creation_time = Time.now
self.created_at = creation_time
self.updated_at = creation_time
self.active = true
  end

  before_update do
update_time = Time.now
self.updated_at = update_time
  end
end
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sequel-talk group.
To post to this group, send email to sequel-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~--~~~~--~~--~--~---



Re: DateTime and date/time fields

2008-06-02 Thread Jeremy Evans

On Jun 2, 7:56 am, Farrel [EMAIL PROTECTED] wrote:
 I noticed with Sequel 2.0 that the Ruby object for a Postgres
 timestamp field is now a DateTime instead of a Time object.  Is
 DateTime the default for all date and time related fields or is Time
 and Date objects used in other cases?

The value you get from the database is adapter specific (I think Time
is used in most cases).  When using models with typecasting, it will
always cast to a DateTime and not a Time.  I'll accept patches for
adapters to use a DateTime field instead of a Time field.

Jeremy
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sequel-talk group.
To post to this group, send email to sequel-talk@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~--~~~~--~~--~--~---