Merge pull request #15397 from sgrif/sg-column-class

Ensure we always use instances of the adapter specific column class
This commit is contained in:
Yves Senn 2014-05-29 13:13:18 +02:00
commit 159c812b35
5 changed files with 15 additions and 6 deletions

@ -363,6 +363,10 @@ def type_map # :nodoc:
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
def lookup_cast_type(sql_type) # :nodoc:

@ -206,8 +206,7 @@ def each_hash(result) # :nodoc:
raise NotImplementedError
end
def new_column(field, default, sql_type, null, collation, extra = "") # :nodoc:
cast_type = lookup_cast_type(sql_type)
def new_column(field, default, cast_type, sql_type = nil, null = true, collation = "", extra = "") # :nodoc:
Column.new(field, default, cast_type, sql_type, null, collation, strict_mode?, extra)
end
@ -425,7 +424,9 @@ def columns(table_name)#:nodoc:
execute_and_free(sql, 'SCHEMA') do |result|
each_hash(result).map do |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

@ -181,10 +181,14 @@ def columns(table_name)
oid = get_oid_type(oid.to_i, fmod.to_i, column_name, type)
default_value = extract_value_from_default(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
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.
def current_database
query('select current_database()', 'SCHEMA')[0][0]

@ -393,7 +393,7 @@ def columns(table_name) #:nodoc:
sql_type = field['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

@ -65,7 +65,7 @@ module ClassMethods
def property(name, cast_type)
name = name.to_s
# 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
# Returns an array of column objects for the table associated with this class.