diff --git a/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb b/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb index e5d100b51ba1e9e672a76b036dffb4c9acf8e880..3c560903f79ba1ae119197d9f5f8cd282bd48ddf 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb @@ -32,7 +32,6 @@ def #{method_name}_with_query_dirty(*args) # def update_with_query_dirty( # Enable the query cache within the block. def cache old, @query_cache_enabled = @query_cache_enabled, true - @query_cache ||= {} yield ensure clear_query_cache @@ -54,7 +53,7 @@ def uncached # the same SQL query and repeatedly return the same result each time, silently # undermining the randomness you were expecting. def clear_query_cache - @query_cache.clear if @query_cache + @query_cache.clear end def select_all_with_query_cache(*args) diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 6ffffc8654658bd82bd5a3f0aa358a744196707a..578297029b8b4e3744c62c0a680c3a659d98e664 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -41,6 +41,7 @@ def initialize(connection, logger = nil) #:nodoc: @connection, @logger = connection, logger @runtime = 0 @query_cache_enabled = false + @query_cache = {} end # Returns the human-readable name of the adapter. Use mixed case - one diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 68ee88bba48e2a12b5739f530e67d54e695cae7f..7169c8f3f0d0a842741e2e09c38dfdfd05d0cc1d 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -54,6 +54,12 @@ def initialize(name, default, sql_type = nil, null = true) super(name, self.class.extract_value_from_default(default), sql_type, null) end + # :stopdoc: + class << self + attr_accessor :money_precision + end + # :startdoc: + private def extract_limit(sql_type) case sql_type @@ -71,9 +77,11 @@ def extract_scale(sql_type) # Extracts the precision from PostgreSQL-specific data types. def extract_precision(sql_type) - # Actual code is defined dynamically in PostgreSQLAdapter.connect - # depending on the server specifics - super + if sql_type == 'money' + self.class.money_precision + else + super + end end # Maps PostgreSQL-specific data types to logical Rails types. @@ -83,18 +91,18 @@ def simplified_type(field_type) when /^(?:real|double precision)$/ :float # Monetary types - when /^money$/ + when 'money' :decimal # Character types when /^(?:character varying|bpchar)(?:\(\d+\))?$/ :string # Binary data types - when /^bytea$/ + when 'bytea' :binary # Date/time types when /^timestamp with(?:out)? time zone$/ :datetime - when /^interval$/ + when 'interval' :string # Geometric types when /^(?:point|line|lseg|box|"?path"?|polygon|circle)$/ @@ -106,16 +114,16 @@ def simplified_type(field_type) when /^bit(?: varying)?(?:\(\d+\))?$/ :string # XML type - when /^xml$/ + when 'xml' :xml # Arrays when /^\D+\[\]$/ :string # Object identifier types - when /^oid$/ + when 'oid' :integer # UUID type - when /^uuid$/ + when 'uuid' :string # Small and big integer types when /^(?:small|big)int$/ @@ -383,9 +391,9 @@ def unescape_bytea(original_value) def quote(value, column = nil) #:nodoc: if value.kind_of?(String) && column && column.type == :binary "#{quoted_string_prefix}'#{escape_bytea(value)}'" - elsif value.kind_of?(String) && column && column.sql_type =~ /^xml$/ + elsif value.kind_of?(String) && column && column.sql_type == 'xml' "xml E'#{quote_string(value)}'" - elsif value.kind_of?(Numeric) && column && column.sql_type =~ /^money$/ + elsif value.kind_of?(Numeric) && column && column.sql_type == 'money' # Not truly string input, so doesn't require (or allow) escape string syntax. "'#{value.to_s}'" elsif value.kind_of?(String) && column && column.sql_type =~ /^bit/ @@ -925,7 +933,7 @@ def distinct(columns, order_by) #:nodoc: # Construct a clean list of column names from the ORDER BY clause, removing # any ASC/DESC modifiers order_columns = order_by.split(',').collect { |s| s.split.first } - order_columns.delete_if &:blank? + order_columns.delete_if(&:blank?) order_columns = order_columns.zip((0...order_columns.size).to_a).map { |s,i| "#{s} AS alias_#{i}" } # Return a DISTINCT ON() clause that's distinct on the columns we want but includes @@ -989,17 +997,8 @@ def connect # Money type has a fixed precision of 10 in PostgreSQL 8.2 and below, and as of # PostgreSQL 8.3 it has a fixed precision of 19. PostgreSQLColumn.extract_precision # should know about this but can't detect it there, so deal with it here. - money_precision = (postgresql_version >= 80300) ? 19 : 10 - PostgreSQLColumn.module_eval(<<-end_eval) - def extract_precision(sql_type) # def extract_precision(sql_type) - if sql_type =~ /^money$/ # if sql_type =~ /^money$/ - #{money_precision} # 19 - else # else - super # super - end # end - end # end - end_eval - + PostgreSQLColumn.money_precision = + (postgresql_version >= 80300) ? 19 : 10 configure_connection end diff --git a/activerecord/test/cases/datatype_test_postgresql.rb b/activerecord/test/cases/datatype_test_postgresql.rb index 9454b6e0593316b5f8e7f381a6fc5884480ba8a5..3c2d9fb7bdf54035bdd04e2eee92cbf3fb6a9470 100644 --- a/activerecord/test/cases/datatype_test_postgresql.rb +++ b/activerecord/test/cases/datatype_test_postgresql.rb @@ -97,7 +97,7 @@ def test_array_values def test_money_values assert_equal 567.89, @first_money.wealth - assert_equal -567.89, @second_money.wealth + assert_equal(-567.89, @second_money.wealth) end def test_number_values