Permalink
Browse files

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
  • Loading branch information...
1 parent f15819e commit beff664f2a036c91e266e2ad867f3d1ed8d716c8 @dhh dhh committed Oct 27, 2005
View
@@ -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)
@@ -893,7 +893,7 @@ def select_limited_ids_list(options)
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)
@@ -33,7 +33,7 @@ def adapter_name
'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
@@ -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 @@ def add_limit_offset!(sql, options)
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 = []
@@ -11,6 +11,8 @@
# 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:
@@ -17,3 +17,8 @@ def create_fixtures(*table_names, &block)
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
@@ -1151,7 +1151,7 @@ def test_habtm_adding_before_save_with_join_attributes
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 @@ def test_removing_associations_on_destroy
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 @@ def test_rich_association
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
@@ -115,7 +115,10 @@ def test_update_array_content
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 @@ def test_non_attribute_access_and_assignment
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 @@ def test_decrement_counter
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 @@ def test_update_many
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 @@ def test_default_values
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 @@ def test_mass_assignment_protection_inheritance
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 @@ def test_multiparameter_attributes_on_date
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 @@ def test_multiparameter_mass_assignment_protector
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 @@ def test_bignum
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 @@ def test_constrain_limit_offset
#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
@@ -15,10 +15,8 @@ def test_find_all_with_limit
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]
@@ -53,9 +53,7 @@ def test_inserts
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
@@ -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) }
@@ -3,9 +3,7 @@
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 @@ def test_schema_dump
end
end
-
end

0 comments on commit beff664

Please sign in to comment.