- Ken
If I may help you working on the Rails3 adapter, please tell me so.
As you may have seen, I have set up a test environment for Rails3 on
my windows system.
And there I am still struggling with the datetime problem. Found it on MS MSDN:
"The setting for SET DATEFORMAT determines how date values are
interpreted. If the order does not match the setting, the values are
not interpreted as dates because they are out of range or the values
are misinterpreted. For example, 12/10/08 can be interpreted as !!!
one of six dates !!!, depending on the DATEFORMAT setting."
-------------------
PS:
I have found in database_statements :
def handle_to_fields_and_rows_odbc(handle)
...
if value.is_a? ODBC::TimeStamp
row[i] = value.to_sqlserver_string
end
and in odbc.rb you are defining:
module ActiveRecord
module ConnectionAdapters
module SQLServerCoreExtensions
module ODBC
module TimeStamp
def to_sqlserver_string
date, time, nanoseconds = to_s.split(' ')
"#{date} #{time}.#{sprintf("%03d",nanoseconds.to_i/
1000000)}"
end
end
So here you are converting the ODBC::TimeStamp in a string, splitting
up the string, converting to integer, building a string again ...
Wouldn't it be nicer if we implement the function to_sqlserver_string
( and may to_iso8601 ) directly in the ruby-odbc driver ?
BTW, for longer code fragments we may want to use gists or some other online paste tool. Helps keep email lean and formatting intact.
>> {"isolation_level"=>"read committed", "concat_null_yields_null"=>"SET", "quoted_identifier"=>"SET", "ansi_padding"=>"SET", "ansi_null_dflt_on"=>"SET", "datefirst"=>"7", "language"=>"us_english", "textsize"=>"64512", "lock_timeout"=>"-1", "ansi_nulls"=>"SET", "dateformat"=>"mdy", "ansi_warnings"=>"SET"}
>>
>> Perhaps we can easily case off that "dateformat" to help the adapter cast correctly? Seems to me we could easily do that in the #quoted_date for the inbound data and then the coercion of the ODBC::TimeStamp on the way out.
>
> In my test system I have changed #quoted_date , so that it will
> convert the data to ISO8601 format which is independent of the
> language settings. BUT
>
> Problem 1:
> - it won't catch situations where someone is using connection.execute
> See your test 'For chronic data types' :
> @connection.execute("INSERT INTO [sql_server_chronics] ([datetime])
> VALUES('#{datetime}')")
>
> Possible solution ??:
> @connection.execute("INSERT INTO [sql_server_chronics]
> ([datetime]) VALUES('#{@connection.quoted_date(datetime)}')")
That is not a problem, we just change the test to insert correctly. We never want to get into examining other peoples SQL on raw execute, etc. So problem 1 is moot and easily solvable.
At first glance this looks heavy handed with to many conditions and the specific case for 2008 mixed all over. I am also not sure about that forced cast to UTC. That would assume that people have that config in active record for that zone, which we should not assume. Lemme run some tests... yea, more than half of my suite is failing now. Over 1,400 errors. Most saying "Conversion failed when converting date and/or time from character string"... '2005-07-15T10:28:00.009900-04:00'
I would suggest taking a different approach. Did you try my recommendation of examining the dateformat from the user options or some other reflection means and casing off of that? I would also suggest memorizing that method too so we are not doing a user options query for every quote value.
That is true and not a problem that the adapter or we should try to monkey patch. Many people are used to doing #to_s(:db). Seems to me if your DB is set to not accept that format, the end user would be better redefining the :db formatter for Time/Date conversions, maybe even just delegating to iso8601. The adapter would never make an assumption to do something so bold, but that is perfectly reasonable for an end user setup/trump. We should only be in the business of quoting of Date/Time objects.
I am sure there is a clean simple solution and I think your getting close. I have a topic branch dedicated just for testing your patches. I'll help too. Can we move comments to the ticket and start swapping diffs for me to test, comment and make recommendations on? FYI, I am also in the #rails-sqlserver IRC room more often lately and can be found on skype/aim too.
- Ken