Skip to content

Commit 591a9de

Browse files
committed
Fixed datetime quoting for ActiveSupport::TimeWithZone objects. Fixes #187 and #189.
1 parent e681d6b commit 591a9de

File tree

3 files changed

+28
-164
lines changed

3 files changed

+28
-164
lines changed

CHANGELOG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11

2+
* 3.2.3 *
3+
4+
* Fixed datetime quoting for ActiveSupport::TimeWithZone objects. Fixes #187 and #189.
5+
6+
27
* 3.2.2 *
38

49
* Fixes all known issues with cross database schema reflection. Fixes #185 [Chris Altman]

lib/active_record/connection_adapters/sqlserver/quoting.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ def quoted_datetime(value)
7171
if value.is_a?(Date)
7272
time_zone_qualified_value.to_time.xmlschema.to(18)
7373
else
74-
if value.is_a?(ActiveSupport::TimeWithZone)
75-
time_zone_qualified_value = time_zone_qualified_value.to_time
76-
end
74+
time_zone_qualified_value = time_zone_qualified_value.to_time if value.is_a?(ActiveSupport::TimeWithZone)
7775
time_zone_qualified_value.iso8601(3).to(22)
7876
end
7977
else

test/cases/adapter_test_sqlserver.rb

Lines changed: 22 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def setup
374374
context "on an activerecord :integer column" do
375375

376376
setup do
377-
@column = stub("column", :type => :integer)
377+
@column = Post.columns_hash['id']
378378
end
379379

380380
should "return null for empty string" do
@@ -383,22 +383,7 @@ def setup
383383

384384
end
385385

386-
context "on an activerecord :binary column" do
387-
388-
setup do
389-
@column = stub("column", :type => :binary)
390-
end
391-
392-
should "ask the column class to convert the value" do
393-
value = "value"
394-
result = stub("result")
395-
@column.class.stubs(:string_to_binary).with(value).returns(result)
396-
assert_equal result, @connection.quote(value, @column)
397-
end
398-
399-
end
400-
401-
context "on an activerecord :string column or with any value that is_utf8?" do
386+
context "on an activerecord :string column or with any value" do
402387

403388
should "surround it when N'...'" do
404389
assert_equal "N'foo'", @connection.quote("foo")
@@ -410,131 +395,40 @@ def setup
410395

411396
end
412397

413-
context "with a value that's not is_utf8?" do
414-
415-
setup do
416-
@value = "value"
417-
@value.stubs(:is_utf8? => false)
418-
end
419-
420-
should_eventually "call super"
421-
422-
end
423-
424398
end
425399

426400
context "date and time values" do
427401

428402
setup do
429-
@value = Date.today
403+
@date = Date.parse '2000-01-01'
404+
@column = SqlServerChronic.columns_hash['datetime']
430405
end
431406

432407
context "on a sql datetime column" do
433408

434-
setup do
435-
@column = stub("column", :sql_type => 'datetime')
436-
end
437-
438409
should "call quoted_datetime and surrounds its result with single quotes" do
439-
@connection.stubs(:quoted_datetime).with(@value).returns("2000-01-01")
440-
assert_equal "'2000-01-01'", @connection.quote(@value, @column)
410+
assert_equal "'01-01-2000'", @connection.quote(@date, @column)
441411
end
442412

443413
end
444414

445-
context "on a sql datetimeoffset column" do
446-
447-
setup do
448-
@column = stub("column", :sql_type => 'datetimeoffset')
449-
end
450-
451-
should "call quoted_full_iso8601 and surrounds its result with single quotes" do
452-
@connection.stubs(:quoted_full_iso8601).with(@value).returns("2000-01-01")
453-
assert_equal "'2000-01-01'", @connection.quote(@value, @column)
454-
end
455-
456-
end
457-
458-
context "on a sql time column" do
459-
460-
setup do
461-
@column = stub("column", :sql_type => 'time')
462-
end
463-
464-
should "call quoted_full_iso8601 and surrounds its result with single quotes" do
465-
@connection.stubs(:quoted_full_iso8601).with(@value).returns("2000-01-01")
466-
assert_equal "'2000-01-01'", @connection.quote(@value, @column)
467-
end
468-
469-
end
470-
471-
context "with another sql column" do
472-
473-
setup do
474-
@column = stub("column", :sql_type => 'foo')
475-
end
476-
477-
should_eventually "call super"
478-
479-
end
480-
481-
context "with no column" do
482-
483-
should_eventually "call super"
484-
485-
end
486-
487-
end
488-
489-
context "nil values" do
490-
491-
context "on a sql timestamp column" do
492-
493-
setup do
494-
@column = stub("column", :sql_type => 'timestamp')
495-
end
496-
497-
should "return DEFAULT" do
498-
assert_equal "DEFAULT", @connection.quote(nil, @column)
499-
end
500-
501-
end
502-
503-
context "with another sql column" do
504-
505-
setup do
506-
@column = stub("column", :sql_type => 'foo')
507-
end
508-
509-
should_eventually "call super"
510-
511-
end
512-
513-
end
514-
515-
context "other values" do
516-
517-
setup do
518-
@value = stub("value")
519-
end
520-
521-
should_eventually "call super"
522-
523415
end
524416

525417
end
526418

527419
context "#quoted_datetime" do
528-
420+
421+
setup do
422+
@iso_string = '2001-02-03T04:05:06-0700'
423+
@date = Date.parse @iso_string
424+
@time = Time.parse @iso_string
425+
@datetime = DateTime.parse @iso_string
426+
end
427+
529428
context "with a Date" do
530429

531-
setup do
532-
@value = Date.parse('2001-02-03T04:05:06-0700')
533-
assert @value.is_a?(Date)
534-
end
535-
536430
should "return a dd-mm-yyyy date string" do
537-
assert_equal '02-03-2001', @connection.quoted_datetime(@value)
431+
assert_equal '02-03-2001', @connection.quoted_datetime(@date)
538432
end
539433

540434
end
@@ -553,77 +447,44 @@ def setup
553447

554448
context "with a Time" do
555449

556-
setup do
557-
@value = Time.parse('2001-02-03T04:05:06-0700')
558-
assert @value.is_a?(Time)
559-
end
560-
561450
should "return an ISO 8601 datetime string" do
562-
assert_equal '2001-02-03T11:05:06.000', @connection.quoted_datetime(@value)
451+
assert_equal '2001-02-03T11:05:06.000', @connection.quoted_datetime(@time)
563452
end
564453

565454
end
566455

567456
context "with a DateTime" do
568457

569-
setup do
570-
@value = DateTime.parse('2001-02-03T04:05:06-0700')
571-
assert @value.is_a?(DateTime)
572-
end
573-
574458
should "return an ISO 8601 datetime string" do
575-
assert_equal '2001-02-03T11:05:06', @connection.quoted_datetime(@value)
459+
assert_equal '2001-02-03T11:05:06', @connection.quoted_datetime(@datetime)
576460
end
577461

578462
end
579463

580464
context "with an ActiveSupport::TimeWithZone" do
581465

582-
setup do
583-
require 'tzinfo'
584-
@old_zone = Time.zone
585-
Time.zone = 'Eastern Time (US & Canada)'
586-
end
587-
588-
teardown do
589-
Time.zone = @old_zone
590-
@old_zone = nil
591-
end
592-
593466
context "wrapping a datetime" do
594467

595-
setup do
596-
@value = DateTime.parse('2001-02-03T04:05:06-0700').in_time_zone
597-
assert @value.is_a?(ActiveSupport::TimeWithZone)
598-
end
599-
600468
should "return an ISO 8601 datetime string with milliseconds" do
601-
assert_equal '2001-02-03T11:05:06.000', @connection.quoted_datetime(@value)
469+
Time.use_zone('Eastern Time (US & Canada)') do
470+
assert_equal '2001-02-03T11:05:06.000', @connection.quoted_datetime(@datetime.in_time_zone)
471+
end
602472
end
603473

604474
end
605475

606476
context "wrapping a time" do
607477

608-
setup do
609-
@value = Time.parse('2001-02-03T04:05:06-0700').in_time_zone
610-
assert @value.is_a?(ActiveSupport::TimeWithZone)
611-
end
612-
613478
should "return an ISO 8601 datetime string with milliseconds" do
614-
assert_equal '2001-02-03T11:05:06.000', @connection.quoted_datetime(@value)
479+
Time.use_zone('Eastern Time (US & Canada)') do
480+
assert_equal '2001-02-03T11:05:06.000', @connection.quoted_datetime(@time.in_time_zone)
481+
end
615482
end
616483

617484
end
618485

619486
end
620487

621-
context "with a String" do
622-
623-
should_eventually "call quoted_date on super"
624-
625-
end
626-
627488
end
628489

629490
end

0 commit comments

Comments
 (0)