From 8885b2d6c1855742600d0afdb9dfc002acb62e5e Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Wed, 6 May 2009 14:16:03 -0300 Subject: [PATCH] Refactor to calculations. Migration's versions are string not integer. ARel submodule updated. --- .../lib/active_record/calculations.rb | 17 ++++++++-------- .../connection_adapters/postgresql_adapter.rb | 20 +++++++++---------- activerecord/lib/active_record/migration.rb | 10 +++++----- arel | 2 +- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index c941be2837..9d04686a5f 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -170,12 +170,12 @@ module ActiveRecord elsif !options[:select].blank? column_name = options[:select] end - construct_calculation_arel(options.merge(:select => Arel::Attribute.new(Arel(table), column_name).count(options[:distinct]))).select_value + construct_calculation_arel(options.merge(:select => Arel::Attribute.new(Arel(table), column_name).count(options[:distinct]))) else - construct_calculation_arel(options.merge(:select => Arel::Attribute.new(Arel(table), column_name).send(operation))).select_value + construct_calculation_arel(options.merge(:select => Arel::Attribute.new(Arel(table), column_name).send(operation))) end - type_cast_calculated_value(value, column_for(column_name), operation) + type_cast_calculated_value(connection.select_value(value.to_sql), column_for(column_name), operation) end def execute_grouped_calculation(operation, column_name, options) #:nodoc: @@ -190,12 +190,11 @@ module ActiveRecord aggregate_alias = column_alias_for(operation, column_name) - if operation == 'count' && column_name == :all - options[:select] = "COUNT(*) AS count_all, #{group_field} AS #{group_alias}" - else - arel_column = Arel::Attribute.new(arel_table, column_name).send(operation) - options[:select] = "#{arel_column.as(aggregate_alias).to_sql}, #{group_field} AS #{group_alias}" - end + options[:select] = (operation == 'count' && column_name == :all) ? + "COUNT(*) AS count_all" : + Arel::Attribute.new(arel_table, column_name).send(operation).as(aggregate_alias).to_sql + + options[:select] << ", #{group_field} AS #{group_alias}" calculated_data = connection.select_all(construct_calculation_arel(options).to_sql) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 4961793866..12b2c4a4d1 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -67,7 +67,7 @@ module ActiveRecord # depending on the server specifics super end - + # Maps PostgreSQL-specific data types to logical Rails types. def simplified_type(field_type) case field_type @@ -102,7 +102,7 @@ module ActiveRecord :string # Arrays when /^\D+\[\]$/ - :string + :string # Object identifier types when /^oid$/ :integer @@ -111,7 +111,7 @@ module ActiveRecord super end end - + # Extracts the value from a PostgreSQL column default definition. def self.extract_value_from_default(default) case default @@ -272,7 +272,7 @@ module ActiveRecord def supports_ddl_transactions? true end - + def supports_savepoints? true end @@ -551,7 +551,7 @@ module ActiveRecord def rollback_db_transaction execute "ROLLBACK" end - + if defined?(PGconn::PQTRANS_IDLE) # The ruby-pg driver supports inspecting the transaction status, # while the ruby-postgres driver does not. @@ -896,18 +896,18 @@ module ActiveRecord sql = "DISTINCT ON (#{columns}) #{columns}, " sql << order_columns * ', ' end - + # Returns an ORDER BY clause for the passed order option. - # + # # PostgreSQL does not allow arbitrary ordering when using DISTINCT ON, so we work around this # by wrapping the +sql+ string as a sub-select and ordering in that query. def add_order_by_for_association_limiting!(sql, options) #:nodoc: return sql if options[:order].blank? - + order = options[:order].split(',').collect { |s| s.strip }.reject(&:blank?) order.map! { |s| 'DESC' if s =~ /\bdesc$/i } order = order.zip((0...order.size).to_a).map { |s,i| "id_list.alias_#{i} #{s}" }.join(', ') - + sql.replace "SELECT * FROM (#{sql}) AS id_list ORDER BY #{order}" end @@ -1020,7 +1020,7 @@ module ActiveRecord if res.ftype(cell_index) == MONEY_COLUMN_TYPE_OID # Because money output is formatted according to the locale, there are two # cases to consider (note the decimal separators): - # (1) $12,345,678.12 + # (1) $12,345,678.12 # (2) $12.345.678,12 case column = row[cell_index] when /^-?\D+[\d,]+\.\d{2}$/ # (1) diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index aa9e9e8fdd..ce02fe019f 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -410,7 +410,7 @@ module ActiveRecord def get_all_versions table = Arel(schema_migrations_table_name) - table.project(table['version']).select_values.map(&:to_i).sort + Base.connection.select_values(table.project(table['version']).to_sql).map(&:to_i).sort end def current_version @@ -535,11 +535,11 @@ module ActiveRecord @migrated_versions ||= [] if down? - @migrated_versions.delete(version.to_i) - table.where(table["version"].eq(version)).delete + @migrated_versions.delete(version) + table.where(table["version"].eq(version.to_s)).delete else - @migrated_versions.push(version.to_i).sort! - table.insert table["version"] => version + @migrated_versions.push(version).sort! + table.insert table["version"] => version.to_s end end diff --git a/arel b/arel index de843c8651..a7dea38204 160000 --- a/arel +++ b/arel @@ -1 +1 @@ -Subproject commit de843c86518e4ac871d4bb5b0873bb6c184ac304 +Subproject commit a7dea38204f3c40e4d0c3f29ebe17af818659697