mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Ensure we always use instances of the adapter specific column class
- Create a consistent API across adapters for building new columns - Use it for custom properties so we don't get `UndefinedMethodError`s in stuff I'm implementing elsewhere.
This commit is contained in:
parent
37c7774504
commit
98a7dde064
5 changed files with 15 additions and 6 deletions
|
@ -363,6 +363,10 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def new_column(name, default, cast_type, sql_type = nil, null = true)
|
||||||
|
Column.new(name, default, cast_type, sql_type, null)
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def lookup_cast_type(sql_type) # :nodoc:
|
def lookup_cast_type(sql_type) # :nodoc:
|
||||||
|
|
|
@ -206,8 +206,7 @@ module ActiveRecord
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
def new_column(field, default, sql_type, null, collation, extra = "") # :nodoc:
|
def new_column(field, default, cast_type, sql_type = nil, null = true, collation = "", extra = "") # :nodoc:
|
||||||
cast_type = lookup_cast_type(sql_type)
|
|
||||||
Column.new(field, default, cast_type, sql_type, null, collation, strict_mode?, extra)
|
Column.new(field, default, cast_type, sql_type, null, collation, strict_mode?, extra)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -425,7 +424,9 @@ module ActiveRecord
|
||||||
execute_and_free(sql, 'SCHEMA') do |result|
|
execute_and_free(sql, 'SCHEMA') do |result|
|
||||||
each_hash(result).map do |field|
|
each_hash(result).map do |field|
|
||||||
field_name = set_field_encoding(field[:Field])
|
field_name = set_field_encoding(field[:Field])
|
||||||
new_column(field_name, field[:Default], field[:Type], field[:Null] == "YES", field[:Collation], field[:Extra])
|
sql_type = field[:Type]
|
||||||
|
cast_type = lookup_cast_type(sql_type)
|
||||||
|
new_column(field_name, field[:Default], cast_type, sql_type, field[:Null] == "YES", field[:Collation], field[:Extra])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -181,10 +181,14 @@ module ActiveRecord
|
||||||
oid = get_oid_type(oid.to_i, fmod.to_i, column_name, type)
|
oid = get_oid_type(oid.to_i, fmod.to_i, column_name, type)
|
||||||
default_value = extract_value_from_default(default)
|
default_value = extract_value_from_default(default)
|
||||||
default_function = extract_default_function(default_value, default)
|
default_function = extract_default_function(default_value, default)
|
||||||
PostgreSQLColumn.new(column_name, default_value, oid, type, notnull == 'f', default_function)
|
new_column(column_name, default_value, oid, type, notnull == 'f', default_function)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def new_column(name, default, cast_type, sql_type = nil, null = true, default_function = nil) # :nodoc:
|
||||||
|
PostgreSQLColumn.new(name, default, cast_type, sql_type, null, default_function)
|
||||||
|
end
|
||||||
|
|
||||||
# Returns the current database name.
|
# Returns the current database name.
|
||||||
def current_database
|
def current_database
|
||||||
query('select current_database()', 'SCHEMA')[0][0]
|
query('select current_database()', 'SCHEMA')[0][0]
|
||||||
|
|
|
@ -393,7 +393,7 @@ module ActiveRecord
|
||||||
|
|
||||||
sql_type = field['type']
|
sql_type = field['type']
|
||||||
cast_type = lookup_cast_type(sql_type)
|
cast_type = lookup_cast_type(sql_type)
|
||||||
Column.new(field['name'], field['dflt_value'], cast_type, sql_type, field['notnull'].to_i == 0)
|
new_column(field['name'], field['dflt_value'], cast_type, sql_type, field['notnull'].to_i == 0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -65,7 +65,7 @@ module ActiveRecord
|
||||||
def property(name, cast_type)
|
def property(name, cast_type)
|
||||||
name = name.to_s
|
name = name.to_s
|
||||||
# Assign a new hash to ensure that subclasses do not share a hash
|
# Assign a new hash to ensure that subclasses do not share a hash
|
||||||
self.user_provided_columns = user_provided_columns.merge(name => ConnectionAdapters::Column.new(name, nil, cast_type))
|
self.user_provided_columns = user_provided_columns.merge(name => connection.new_column(name, nil, cast_type))
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns an array of column objects for the table associated with this class.
|
# Returns an array of column objects for the table associated with this class.
|
||||||
|
|
Loading…
Reference in a new issue