1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Refactor to calculations. Migration's versions are string not integer. ARel submodule updated.

This commit is contained in:
Emilio Tagua 2009-05-06 14:16:03 -03:00
parent 118b19a9fa
commit 8885b2d6c1
4 changed files with 24 additions and 25 deletions

View file

@ -170,12 +170,12 @@ module ActiveRecord
elsif !options[:select].blank? elsif !options[:select].blank?
column_name = options[:select] column_name = options[:select]
end 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 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 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 end
def execute_grouped_calculation(operation, column_name, options) #:nodoc: def execute_grouped_calculation(operation, column_name, options) #:nodoc:
@ -190,12 +190,11 @@ module ActiveRecord
aggregate_alias = column_alias_for(operation, column_name) aggregate_alias = column_alias_for(operation, column_name)
if operation == 'count' && column_name == :all options[:select] = (operation == 'count' && column_name == :all) ?
options[:select] = "COUNT(*) AS count_all, #{group_field} AS #{group_alias}" "COUNT(*) AS count_all" :
else Arel::Attribute.new(arel_table, column_name).send(operation).as(aggregate_alias).to_sql
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}" options[:select] << ", #{group_field} AS #{group_alias}"
end
calculated_data = connection.select_all(construct_calculation_arel(options).to_sql) calculated_data = connection.select_all(construct_calculation_arel(options).to_sql)

View file

@ -67,7 +67,7 @@ module ActiveRecord
# depending on the server specifics # depending on the server specifics
super super
end end
# Maps PostgreSQL-specific data types to logical Rails types. # Maps PostgreSQL-specific data types to logical Rails types.
def simplified_type(field_type) def simplified_type(field_type)
case field_type case field_type
@ -102,7 +102,7 @@ module ActiveRecord
:string :string
# Arrays # Arrays
when /^\D+\[\]$/ when /^\D+\[\]$/
:string :string
# Object identifier types # Object identifier types
when /^oid$/ when /^oid$/
:integer :integer
@ -111,7 +111,7 @@ module ActiveRecord
super super
end end
end end
# Extracts the value from a PostgreSQL column default definition. # Extracts the value from a PostgreSQL column default definition.
def self.extract_value_from_default(default) def self.extract_value_from_default(default)
case default case default
@ -272,7 +272,7 @@ module ActiveRecord
def supports_ddl_transactions? def supports_ddl_transactions?
true true
end end
def supports_savepoints? def supports_savepoints?
true true
end end
@ -551,7 +551,7 @@ module ActiveRecord
def rollback_db_transaction def rollback_db_transaction
execute "ROLLBACK" execute "ROLLBACK"
end end
if defined?(PGconn::PQTRANS_IDLE) if defined?(PGconn::PQTRANS_IDLE)
# The ruby-pg driver supports inspecting the transaction status, # The ruby-pg driver supports inspecting the transaction status,
# while the ruby-postgres driver does not. # while the ruby-postgres driver does not.
@ -896,18 +896,18 @@ module ActiveRecord
sql = "DISTINCT ON (#{columns}) #{columns}, " sql = "DISTINCT ON (#{columns}) #{columns}, "
sql << order_columns * ', ' sql << order_columns * ', '
end end
# Returns an ORDER BY clause for the passed order option. # 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 # 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. # by wrapping the +sql+ string as a sub-select and ordering in that query.
def add_order_by_for_association_limiting!(sql, options) #:nodoc: def add_order_by_for_association_limiting!(sql, options) #:nodoc:
return sql if options[:order].blank? return sql if options[:order].blank?
order = options[:order].split(',').collect { |s| s.strip }.reject(&:blank?) order = options[:order].split(',').collect { |s| s.strip }.reject(&:blank?)
order.map! { |s| 'DESC' if s =~ /\bdesc$/i } 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(', ') 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}" sql.replace "SELECT * FROM (#{sql}) AS id_list ORDER BY #{order}"
end end
@ -1020,7 +1020,7 @@ module ActiveRecord
if res.ftype(cell_index) == MONEY_COLUMN_TYPE_OID if res.ftype(cell_index) == MONEY_COLUMN_TYPE_OID
# Because money output is formatted according to the locale, there are two # Because money output is formatted according to the locale, there are two
# cases to consider (note the decimal separators): # cases to consider (note the decimal separators):
# (1) $12,345,678.12 # (1) $12,345,678.12
# (2) $12.345.678,12 # (2) $12.345.678,12
case column = row[cell_index] case column = row[cell_index]
when /^-?\D+[\d,]+\.\d{2}$/ # (1) when /^-?\D+[\d,]+\.\d{2}$/ # (1)

View file

@ -410,7 +410,7 @@ module ActiveRecord
def get_all_versions def get_all_versions
table = Arel(schema_migrations_table_name) 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 end
def current_version def current_version
@ -535,11 +535,11 @@ module ActiveRecord
@migrated_versions ||= [] @migrated_versions ||= []
if down? if down?
@migrated_versions.delete(version.to_i) @migrated_versions.delete(version)
table.where(table["version"].eq(version)).delete table.where(table["version"].eq(version.to_s)).delete
else else
@migrated_versions.push(version.to_i).sort! @migrated_versions.push(version).sort!
table.insert table["version"] => version table.insert table["version"] => version.to_s
end end
end end

2
arel

@ -1 +1 @@
Subproject commit de843c86518e4ac871d4bb5b0873bb6c184ac304 Subproject commit a7dea38204f3c40e4d0c3f29ebe17af818659697