Skip to content

Commit dbbcc83

Browse files
committed
Merge pull request rails#15151 from sgrif/sg-add-type-to-column
Add a type object to Column constructor
2 parents 7359f81 + 4bd5dff commit dbbcc83

File tree

14 files changed

+86
-57
lines changed

14 files changed

+86
-57
lines changed

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'bigdecimal/util'
44
require 'active_support/core_ext/benchmark'
55
require 'active_record/connection_adapters/schema_cache'
6+
require 'active_record/connection_adapters/type'
67
require 'active_record/connection_adapters/abstract/schema_dumper'
78
require 'active_record/connection_adapters/abstract/schema_creation'
89
require 'monitor'
@@ -362,6 +363,10 @@ def close
362363

363364
protected
364365

366+
def lookup_cast_type(sql_type) # :nodoc:
367+
Type::Value.new
368+
end
369+
365370
def translate_exception_class(e, sql)
366371
message = "#{e.class.name}: #{e.message}: #{sql}"
367372
@logger.error message if @logger

activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ def schema_creation
5656
class Column < ConnectionAdapters::Column # :nodoc:
5757
attr_reader :collation, :strict, :extra
5858

59-
def initialize(name, default, sql_type = nil, null = true, collation = nil, strict = false, extra = "")
59+
def initialize(name, default, cast_type, sql_type = nil, null = true, collation = nil, strict = false, extra = "")
6060
@strict = strict
6161
@collation = collation
6262
@extra = extra
63-
super(name, default, sql_type, null)
63+
super(name, default, cast_type, sql_type, null)
6464
end
6565

6666
def extract_default(default)
@@ -263,8 +263,9 @@ def each_hash(result) # :nodoc:
263263
end
264264

265265
# Overridden by the adapters to instantiate their specific Column type.
266-
def new_column(field, default, type, null, collation, extra = "") # :nodoc:
267-
Column.new(field, default, type, null, collation, extra)
266+
def new_column(field, default, sql_type, null, collation, extra = "") # :nodoc:
267+
cast_type = lookup_cast_type(sql_type)
268+
Column.new(field, default, cast_type, sql_type, null, collation, extra)
268269
end
269270

270271
# Must return the Mysql error number from the exception, if the exception has an

activerecord/lib/active_record/connection_adapters/column.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ module Format
2222
#
2323
# +name+ is the column's name, such as <tt>supplier_id</tt> in <tt>supplier_id int(11)</tt>.
2424
# +default+ is the type-casted default value, such as +new+ in <tt>sales_stage varchar(20) default 'new'</tt>.
25+
# +cast_type+ is the object used for type casting and type information.
2526
# +sql_type+ is used to extract the column's length, if necessary. For example +60+ in
2627
# <tt>company_name varchar(60)</tt>.
2728
# It will be mapped to one of the standard Rails SQL types in the <tt>type</tt> attribute.
2829
# +null+ determines if this column allows +NULL+ values.
29-
def initialize(name, default, sql_type = nil, null = true)
30+
def initialize(name, default, cast_type, sql_type = nil, null = true)
3031
@name = name
32+
@cast_type = cast_type
3133
@sql_type = sql_type
3234
@null = null
3335
@limit = extract_limit(sql_type)

activerecord/lib/active_record/connection_adapters/mysql2_adapter.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ def each_hash(result) # :nodoc:
6969
end
7070
end
7171

72-
def new_column(field, default, type, null, collation, extra = "") # :nodoc:
73-
Column.new(field, default, type, null, collation, strict_mode?, extra)
72+
def new_column(field, default, sql_type, null, collation, extra = "") # :nodoc:
73+
cast_type = lookup_cast_type(sql_type)
74+
Column.new(field, default, cast_type, sql_type, null, collation, strict_mode?, extra)
7475
end
7576

7677
def error_number(exception)

activerecord/lib/active_record/connection_adapters/mysql_adapter.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@ def each_hash(result) # :nodoc:
156156
end
157157
end
158158

159-
def new_column(field, default, type, null, collation, extra = "") # :nodoc:
160-
Column.new(field, default, type, null, collation, strict_mode?, extra)
159+
def new_column(field, default, sql_type, null, collation, extra = "") # :nodoc:
160+
cast_type = lookup_cast_type(sql_type)
161+
Column.new(field, default, cast_type, sql_type, null, collation, strict_mode?, extra)
161162
end
162163

163164
def error_number(exception) # :nodoc:

activerecord/lib/active_record/connection_adapters/postgresql/column.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ def initialize(name, default, oid_type, sql_type = nil, null = true)
1212

1313
if sql_type =~ /\[\]$/
1414
@array = true
15-
super(name, default_value, sql_type[0..sql_type.length - 3], null)
15+
super(name, default_value, oid_type, sql_type[0..sql_type.length - 3], null)
1616
else
1717
@array = false
18-
super(name, default_value, sql_type, null)
18+
super(name, default_value, oid_type, sql_type, null)
1919
end
2020

2121
@default_function = default if has_default_function?(default_value, default)

activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,9 @@ def columns(table_name) #:nodoc:
394394
field["dflt_value"] = $1.gsub('""', '"')
395395
end
396396

397-
SQLite3Column.new(field['name'], field['dflt_value'], field['type'], field['notnull'].to_i == 0)
397+
sql_type = field['type']
398+
cast_type = lookup_cast_type(sql_type)
399+
SQLite3Column.new(field['name'], field['dflt_value'], cast_type, sql_type, field['notnull'].to_i == 0)
398400
end
399401
end
400402

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'active_record/connection_adapters/type/value'
2+
3+
module ActiveRecord
4+
module ConnectionAdapters
5+
module Type # :nodoc:
6+
end
7+
end
8+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module ActiveRecord
2+
module ConnectionAdapters
3+
module Type
4+
class Value # :nodoc:
5+
end
6+
end
7+
end
8+
end

activerecord/test/active_record/connection_adapters/fake_adapter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def merge_column(table_name, name, sql_type = nil, options = {})
2929
@columns[table_name] << ActiveRecord::ConnectionAdapters::Column.new(
3030
name.to_s,
3131
options[:default],
32+
lookup_cast_type(sql_type.to_s),
3233
sql_type.to_s,
3334
options[:null])
3435
end

0 commit comments

Comments
 (0)