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?
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)

View file

@ -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)

View file

@ -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

2
arel

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