diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-10-27 08:18:41 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-10-27 08:18:41 +0000 |
commit | beff664f2a036c91e266e2ad867f3d1ed8d716c8 (patch) | |
tree | a175c7bc0ed6affea55d1595577a2c37e38c74e0 /activerecord | |
parent | f15819e8f0cece48ccd5117a37cf6d37374d1bb1 (diff) | |
download | rails-beff664f2a036c91e266e2ad867f3d1ed8d716c8.tar.gz rails-beff664f2a036c91e266e2ad867f3d1ed8d716c8.tar.bz2 rails-beff664f2a036c91e266e2ad867f3d1ed8d716c8.zip |
Refactor DB exceptions and deal more with DB2 (closes #2624)
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2761 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/db2_adapter.rb | 10 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb | 2 | ||||
-rwxr-xr-x | activerecord/test/abstract_unit.rb | 5 | ||||
-rwxr-xr-x | activerecord/test/associations_test.rb | 6 | ||||
-rwxr-xr-x | activerecord/test/base_test.rb | 71 | ||||
-rwxr-xr-x | activerecord/test/deprecated_finder_test.rb | 6 | ||||
-rwxr-xr-x | activerecord/test/fixtures_test.rb | 4 | ||||
-rwxr-xr-x | activerecord/test/inheritance_test.rb | 5 | ||||
-rw-r--r-- | activerecord/test/schema_dumper_test.rb | 5 |
12 files changed, 58 insertions, 62 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 8f205a0de3..b0686e9e53 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -4,7 +4,7 @@ * Fixed handling of nil number columns on Oracle and cleaned up tests for Oracle in general #2555 [schoenm@earthlink.net] -* Added quoted_true and quoted_false methods to db2_adapter and cleaned up tests for DB2 #2493 [maik schmidt] +* Added quoted_true and quoted_false methods and tables to db2_adapter and cleaned up tests for DB2 #2493, #2624 [maik schmidt] *1.12.2* (October 26th, 2005) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index dd0b71b65f..9b3296b3e4 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -893,7 +893,7 @@ module ActiveRecord connection.select_values( construct_finder_sql_for_association_limiting(options), "#{name} Load IDs For Limited Eager Loading" - ).collect { |id| "'#{id}'" }.join(", ") + ).collect { |id| connection.quote(id) }.join(", ") end def construct_finder_sql_for_association_limiting(options) diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index c04b6b96ff..97f4bcde7f 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -33,7 +33,7 @@ module ActiveRecord 'Abstract' end - # Does this adapter support migrations ? Backend specific, as the + # Does this adapter support migrations? Backend specific, as the # abstract adapter always returns +false+. def supports_migrations? false diff --git a/activerecord/lib/active_record/connection_adapters/db2_adapter.rb b/activerecord/lib/active_record/connection_adapters/db2_adapter.rb index cc0e535123..232971e4ec 100644 --- a/activerecord/lib/active_record/connection_adapters/db2_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/db2_adapter.rb @@ -1,4 +1,4 @@ -# Author: Maik Schmidt <contact@maik-schmidt.de> +# Author/Maintainer: Maik Schmidt <contact@maik-schmidt.de> require 'active_record/connection_adapters/abstract_adapter' @@ -113,6 +113,14 @@ begin end end + def tables(name = nil) + stmt = DB2::Statement.new(@connection) + result = [] + stmt.tables.each { |t| result << t[2].downcase } + stmt.free + result + end + def columns(table_name, name = nil) stmt = DB2::Statement.new(@connection) result = [] diff --git a/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb index 56be126c05..c6e82605ee 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb @@ -11,6 +11,8 @@ require 'active_record/connection_adapters/abstract_adapter' # Modifications (ODBC): Mark Imbriaco <mark.imbriaco@pobox.com> # Date: 6/26/2005 # +# Current maintainer: Ryan Tomayko <rtomayko@gmail.com> +# module ActiveRecord class Base def self.sqlserver_connection(config) #:nodoc: diff --git a/activerecord/test/abstract_unit.rb b/activerecord/test/abstract_unit.rb index 9373422403..5c585283f4 100755 --- a/activerecord/test/abstract_unit.rb +++ b/activerecord/test/abstract_unit.rb @@ -17,3 +17,8 @@ class Test::Unit::TestCase #:nodoc: Fixtures.create_fixtures(File.dirname(__FILE__) + "/fixtures/", table_names, &block) end end + +def current_adapter?(type) + ActiveRecord::ConnectionAdapters.const_defined?(type) && + ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type)) +end diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index 70f0eac7ee..6bdafd7868 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -1151,7 +1151,7 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase kenReloaded = Developer.find_by_name 'Ken' # SQL Server doesn't have a separate column type just for dates, # so the time is in the string and incorrectly formatted - if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter and ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) + if current_adapter?(:SQLServerAdapter) kenReloaded.projects.each { |prj| assert_equal(sqlnow, prj.joined_on.strftime("%Y/%m/%d 00:00:00")) } else kenReloaded.projects.each { |prj| assert_equal(now.to_s, prj.joined_on.to_s) } @@ -1245,7 +1245,7 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase def test_additional_columns_from_join_table # SQL Server doesn't have a separate column type just for dates, # so the time is in the string and incorrectly formatted - if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter and ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) + if current_adapter?(:SQLServerAdapter) assert_equal Time.mktime(2004, 10, 10).strftime("%Y/%m/%d 00:00:00"), Time.parse(Developer.find(1).projects.first.joined_on).strftime("%Y/%m/%d 00:00:00") else assert_equal Date.new(2004, 10, 10).to_s, Developer.find(1).projects.first.joined_on.to_s @@ -1266,7 +1266,7 @@ class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase jamis.projects.push_with_attributes(projects(:action_controller), :joined_on => Date.today) # SQL Server doesn't have a separate column type just for dates, # so the time is in the string and incorrectly formatted - if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter and ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) + if current_adapter?(:SQLServerAdapter) assert_equal Time.now.strftime("%Y/%m/%d 00:00:00"), Time.parse(jamis.projects.select { |p| p.name == projects(:action_controller).name }.first.joined_on).strftime("%Y/%m/%d 00:00:00") assert_equal Time.now.strftime("%Y/%m/%d 00:00:00"), Time.parse(developers(:jamis).projects.select { |p| p.name == projects(:action_controller).name }.first.joined_on).strftime("%Y/%m/%d 00:00:00") else diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index acfd327556..5d924f540a 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -115,7 +115,10 @@ class BasicsTest < Test::Unit::TestCase assert_equal(%w( one two three four five ), topic.content) end - def test_attributes_hash + def test_case_sensitive_attributes_hash + # DB2 is not case-sensitive + return true if current_adapter?(:DB2Adapter) + assert_equal @loaded_fixtures['computers']['workstation'].to_hash, Computer.find(:first).attributes end @@ -220,26 +223,23 @@ class BasicsTest < Test::Unit::TestCase def test_preserving_date_objects # SQL Server doesn't have a separate column type just for dates, so all are returned as time - if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter - return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) - end + return true if current_adapter?(:SQLServerAdapter) assert_kind_of( Date, Topic.find(1).last_read, "The last_read attribute should be of the Date class" ) + end + def test_preserving_time_objects # Oracle does not have a TIME datatype. - if ActiveRecord::ConnectionAdapters.const_defined? :OracleAdapter - return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::OracleAdapter) - end + return true if current_adapter?(:OCIAdapter) + assert_kind_of( Time, Topic.find(1).bonus_time, "The bonus_time attribute should be of the Time class" ) - end - def test_preserving_time_objects assert_kind_of( Time, Topic.find(1).written_on, "The written_on attribute should be of the Time class" @@ -384,9 +384,8 @@ class BasicsTest < Test::Unit::TestCase def test_update_all # The ADO library doesn't support the number of affected rows - if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter - return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) - end + return true if current_adapter?(:SQLServerAdapter) + assert_equal 2, Topic.update_all("content = 'bulk updated!'") assert_equal "bulk updated!", Topic.find(1).content assert_equal "bulk updated!", Topic.find(2).content @@ -406,9 +405,8 @@ class BasicsTest < Test::Unit::TestCase def test_delete_all # The ADO library doesn't support the number of affected rows - if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter - return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) - end + return true if current_adapter?(:SQLServerAdapter) + assert_equal 2, Topic.delete_all end @@ -471,16 +469,16 @@ class BasicsTest < Test::Unit::TestCase assert_nil topic.last_read end - # Oracle and SQLServer do not have a TIME datatype. - unless 'OCI' == ActiveRecord::Base.connection.adapter_name or ActiveRecord::ConnectionAdapters.const_defined?(:SQLServerAdapter) - def test_utc_as_time_zone - Topic.default_timezone = :utc - attributes = { "bonus_time" => "5:42:00AM" } - topic = Topic.find(1) - topic.attributes = attributes - assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time - Topic.default_timezone = :local - end + def test_utc_as_time_zone + # Oracle and SQLServer do not have a TIME datatype. + return true if current_adapter?(:SQLServerAdapter) || current_adapter?(:OCIAdapter) + + Topic.default_timezone = :utc + attributes = { "bonus_time" => "5:42:00AM" } + topic = Topic.find(1) + topic.attributes = attributes + assert_equal Time.utc(2000, 1, 1, 5, 42, 0), topic.bonus_time + Topic.default_timezone = :local end def test_default_values_on_empty_strings @@ -587,9 +585,7 @@ class BasicsTest < Test::Unit::TestCase def test_multiparameter_attributes_on_date # SQL Server doesn't have a separate column type just for dates, so all are returned as time - if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter - return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) - end + return true if current_adapter?(:SQLServerAdapter) attributes = { "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "24" } topic = Topic.find(1) @@ -601,9 +597,7 @@ class BasicsTest < Test::Unit::TestCase def test_multiparameter_attributes_on_date_with_empty_date # SQL Server doesn't have a separate column type just for dates, so all are returned as time - if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter - return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) - end + return true if current_adapter?(:SQLServerAdapter) attributes = { "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "" } topic = Topic.find(1) @@ -650,14 +644,8 @@ class BasicsTest < Test::Unit::TestCase end def test_attributes_on_dummy_time - # Oracle does not have a TIME datatype. - if ActiveRecord::ConnectionAdapters.const_defined? :OCIAdapter - return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::OCIAdapter) - end - # Sqlserver doesn't either . - if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter - return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) - end + # Oracle and SQL Server does not have a TIME datatype. + return true if current_adapter?(:SQLServerAdapter) || current_adapter?(:OCIAdapter) attributes = { "bonus_time" => "5:42:00AM" @@ -738,7 +726,7 @@ class BasicsTest < Test::Unit::TestCase end # TODO: extend defaults tests to other databases! - if 'PostgreSQL' == ActiveRecord::Base.connection.adapter_name + if current_adapter?(:PostgreSQLAdapter) def test_default default = Default.new @@ -1086,9 +1074,8 @@ class BasicsTest < Test::Unit::TestCase #end private - def assert_readers(model, exceptions) expected_readers = model.column_names - (model.serialized_attributes.keys + exceptions + ['id']) assert_equal expected_readers.sort, model.read_methods.keys.sort end -end +end
\ No newline at end of file diff --git a/activerecord/test/deprecated_finder_test.rb b/activerecord/test/deprecated_finder_test.rb index 7a725efb2f..b9257c0a2c 100755 --- a/activerecord/test/deprecated_finder_test.rb +++ b/activerecord/test/deprecated_finder_test.rb @@ -15,10 +15,8 @@ class DeprecatedFinderTest < Test::Unit::TestCase end def test_find_all_with_prepared_limit_and_offset - if ActiveRecord::ConnectionAdapters.const_defined? :OracleAdapter - if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::OracleAdapter) - assert_raises(ArgumentError) { Entrant.find_all nil, "id ASC", [2, 1] } - end + if current_adapter?(:OCIAdapter) + assert_raises(ArgumentError) { Entrant.find_all nil, "id ASC", [2, 1] } else entrants = Entrant.find_all nil, "id ASC", [2, 1] diff --git a/activerecord/test/fixtures_test.rb b/activerecord/test/fixtures_test.rb index 69ffcc6019..7666777184 100755 --- a/activerecord/test/fixtures_test.rb +++ b/activerecord/test/fixtures_test.rb @@ -53,9 +53,7 @@ class FixturesTest < Test::Unit::TestCase def test_inserts_with_pre_and_suffix # not supported yet in OCI adapter - if ActiveRecord::ConnectionAdapters.const_defined? :OCIAdapter - return true if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::OCIAdapter) - end + return true if current_adapter?(:OCIAdapter) ActiveRecord::Base.connection.create_table :prefix_topics_suffix do |t| t.column :title, :string diff --git a/activerecord/test/inheritance_test.rb b/activerecord/test/inheritance_test.rb index a3b9ce25ad..432aee7e0e 100755 --- a/activerecord/test/inheritance_test.rb +++ b/activerecord/test/inheritance_test.rb @@ -8,12 +8,13 @@ class InheritanceTest < Test::Unit::TestCase def test_a_bad_type_column #SQLServer need to turn Identity Insert On before manually inserting into the Identity column - if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter and ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) + if current_adapter?(:SQLServerAdapter) Company.connection.execute "SET IDENTITY_INSERT companies ON" end Company.connection.insert "INSERT INTO companies (id, type, name) VALUES(100, 'bad_class!', 'Not happening')" + #We then need to turn it back Off before continuing. - if ActiveRecord::ConnectionAdapters.const_defined? :SQLServerAdapter and ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::SQLServerAdapter) + if current_adapter?(:SQLServerAdapter) Company.connection.execute "SET IDENTITY_INSERT companies OFF" end assert_raises(ActiveRecord::SubclassNotFound) { Company.find(100) } diff --git a/activerecord/test/schema_dumper_test.rb b/activerecord/test/schema_dumper_test.rb index e57429a4c8..72ce1bce42 100644 --- a/activerecord/test/schema_dumper_test.rb +++ b/activerecord/test/schema_dumper_test.rb @@ -3,9 +3,7 @@ require "#{File.dirname(__FILE__)}/../lib/active_record/schema_dumper" require 'stringio' if ActiveRecord::Base.connection.respond_to?(:tables) - - unless ActiveRecord::ConnectionAdapters.const_defined?(:OCIAdapter) && \ - ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::OCIAdapter) + unless current_adapter?(:OCIAdapter) class SchemaDumperTest < Test::Unit::TestCase def test_schema_dump @@ -20,5 +18,4 @@ if ActiveRecord::Base.connection.respond_to?(:tables) end end - end |