Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,46 @@ def close
pool.checkin self
end

def type_map # :nodoc:
@type_map ||= Type::TypeMap.new.tap do |mapping|
initialize_type_map(mapping)
end
end

protected

def lookup_cast_type(sql_type) # :nodoc:
Type::Value.new
type_map.lookup(sql_type)
end

def initialize_type_map(m) # :nodoc:
m.register_type %r(boolean)i, Type::Boolean.new
m.register_type %r(char)i, Type::String.new
m.register_type %r(binary)i, Type::Binary.new
m.alias_type %r(blob)i, 'binary'
m.register_type %r(text)i, Type::Text.new
m.alias_type %r(clob)i, 'text'
m.register_type %r(date)i, Type::Date.new
m.register_type %r(time)i, Type::Time.new
m.register_type %r(timestamp)i, Type::Timestamp.new
m.register_type %r(datetime)i, Type::DateTime.new
m.alias_type %r(numeric)i, 'decimal'
m.alias_type %r(number)i, 'decimal'
m.register_type %r(float)i, Type::Float.new
m.alias_type %r(double)i, 'float'
m.register_type %r(int)i, Type::Integer.new
m.register_type(%r(decimal)i) do |sql_type|
if Type.extract_scale(sql_type) == 0
Type::Integer.new
else
Type::Decimal.new
end
end
end

def reload_type_map # :nodoc:
type_map.clear
initialize_type_map(type_map)
end

def translate_exception_class(e, sql)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,6 @@ def case_sensitive?

private

def simplified_type(field_type)
return :boolean if adapter.emulate_booleans && field_type.downcase.index("tinyint(1)")

case field_type
when /enum/i, /set/i then :string
when /year/i then :integer
when /bit/i then :binary
else
super
end
end

def extract_limit(sql_type)
case sql_type
when /^enum\((.+)\)/i
Expand Down Expand Up @@ -318,6 +306,11 @@ def disable_referential_integrity #:nodoc:

# DATABASE STATEMENTS ======================================

def clear_cache!
super
reload_type_map
end

# Executes the SQL statement in the context of this connection.
def execute(sql, name = nil)
log(sql, name) { @connection.query(sql) }
Expand Down Expand Up @@ -645,6 +638,15 @@ def valid_type?(type)

protected

def initialize_type_map(m)
super
m.alias_type %r(tinyint\(1\))i, 'boolean' if emulate_booleans
m.alias_type %r(enum)i, 'varchar'
m.alias_type %r(set)i, 'varchar'
m.alias_type %r(year)i, 'integer'
m.alias_type %r(bit)i, 'binary'
end

# MySQL is too stupid to create a temporary table for use subquery, so we have
# to give it some prompting in the form of a subsubquery. Ugh!
def subquery_for(key, select)
Expand Down
41 changes: 5 additions & 36 deletions activerecord/lib/active_record/connection_adapters/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ module Format
ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/
end

attr_reader :name, :default, :type, :limit, :null, :sql_type, :precision, :scale, :default_function
attr_reader :name, :default, :cast_type, :limit, :null, :sql_type, :precision, :scale, :default_function
attr_accessor :primary, :coder

alias :encoded? :coder

delegate :type, to: :cast_type

# Instantiates a new column in the table.
#
# +name+ is the column's name, such as <tt>supplier_id</tt> in <tt>supplier_id int(11)</tt>.
Expand All @@ -35,7 +37,6 @@ def initialize(name, default, cast_type, sql_type = nil, null = true)
@limit = extract_limit(sql_type)
@precision = extract_precision(sql_type)
@scale = extract_scale(sql_type)
@type = simplified_type(sql_type)
@default = extract_default(default)
@default_function = nil
@primary = nil
Expand Down Expand Up @@ -256,47 +257,15 @@ def fallback_string_to_time(string)
end

private
delegate :extract_scale, to: Type

def extract_limit(sql_type)
$1.to_i if sql_type =~ /\((.*)\)/
end

def extract_precision(sql_type)
$2.to_i if sql_type =~ /^(numeric|decimal|number)\((\d+)(,\d+)?\)/i
end

def extract_scale(sql_type)
case sql_type
when /^(numeric|decimal|number)\((\d+)\)/i then 0
when /^(numeric|decimal|number)\((\d+)(,(\d+))\)/i then $4.to_i
end
end

def simplified_type(field_type)
case field_type
when /int/i
:integer
when /float|double/i
:float
when /decimal|numeric|number/i
extract_scale(field_type) == 0 ? :integer : :decimal
when /datetime/i
:datetime
when /timestamp/i
:timestamp
when /time/i
:time
when /date/i
:date
when /clob/i, /text/i
:text
when /blob/i, /binary/i
:binary
when /char/i
:string
when /boolean/i
:boolean
end
end
end
end
# :startdoc:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ def select_rows(sql, name = nil, binds = [])

# Clears the prepared statements cache.
def clear_cache!
super
@statements.clear
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,6 @@ def extract_precision(sql_type)
super
end
end

# Maps PostgreSQL-specific data types to logical Rails types.
def simplified_type(field_type)
@oid_type.simplified_type(field_type) || super
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ module ActiveRecord
module ConnectionAdapters
module PostgreSQL
module OID # :nodoc:
class Type
def type; end
def simplified_type(sql_type); type end

class Type < Type::Value
def infinity(options = {})
::Float::INFINITY * (options[:negative] ? -1 : 1)
end
Expand Down Expand Up @@ -136,11 +133,11 @@ def type_cast(value)
end

class Range < Type
attr_reader :subtype
def simplified_type(sql_type); sql_type.to_sym end
attr_reader :subtype, :type

def initialize(subtype)
def initialize(subtype, type)
@subtype = subtype
@type = type
end

def extract_bounds(value)
Expand Down Expand Up @@ -412,7 +409,7 @@ def register_array_type(row)

def register_range_type(row)
if subtype = @store[row['rngsubtype'].to_i]
register row['oid'], OID::Range.new(subtype)
register row['oid'], OID::Range.new(subtype, row['typname'].to_sym)
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,6 @@ def translate_exception(exception, message)

private

def type_map
@type_map
end

def get_oid_type(oid, fmod, column_name)
if !type_map.key?(oid)
initialize_type_map(type_map, [oid])
Expand All @@ -554,11 +550,6 @@ def get_oid_type(oid, fmod, column_name)
}
end

def reload_type_map
type_map.clear
initialize_type_map(type_map)
end

def initialize_type_map(type_map, oids = nil)
if supports_ranges?
query = <<-SQL
Expand Down
20 changes: 20 additions & 0 deletions activerecord/lib/active_record/connection_adapters/type.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
require 'active_record/connection_adapters/type/value'
require 'active_record/connection_adapters/type/binary'
require 'active_record/connection_adapters/type/boolean'
require 'active_record/connection_adapters/type/date'
require 'active_record/connection_adapters/type/date_time'
require 'active_record/connection_adapters/type/decimal'
require 'active_record/connection_adapters/type/float'
require 'active_record/connection_adapters/type/integer'
require 'active_record/connection_adapters/type/string'
require 'active_record/connection_adapters/type/text'
require 'active_record/connection_adapters/type/time'
require 'active_record/connection_adapters/type/timestamp'
require 'active_record/connection_adapters/type/type_map'

module ActiveRecord
module ConnectionAdapters
module Type # :nodoc:
class << self
def extract_scale(sql_type)
case sql_type
when /^(numeric|decimal|number)\((\d+)\)/i then 0
when /^(numeric|decimal|number)\((\d+)(,(\d+))\)/i then $4.to_i
end
end
end
end
end
end
11 changes: 11 additions & 0 deletions activerecord/lib/active_record/connection_adapters/type/binary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ActiveRecord
module ConnectionAdapters
module Type
class Binary < Value # :nodoc:
def type
:binary
end
end
end
end
end
11 changes: 11 additions & 0 deletions activerecord/lib/active_record/connection_adapters/type/boolean.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ActiveRecord
module ConnectionAdapters
module Type
class Boolean < Value # :nodoc:
def type
:boolean
end
end
end
end
end
11 changes: 11 additions & 0 deletions activerecord/lib/active_record/connection_adapters/type/date.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ActiveRecord
module ConnectionAdapters
module Type
class Date < Value # :nodoc:
def type
:date
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'active_record/connection_adapters/type/timestamp'

module ActiveRecord
module ConnectionAdapters
module Type
class DateTime < Timestamp # :nodoc:
def type
:datetime
end
end
end
end
end
11 changes: 11 additions & 0 deletions activerecord/lib/active_record/connection_adapters/type/decimal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ActiveRecord
module ConnectionAdapters
module Type
class Decimal < Value # :nodoc:
def type
:decimal
end
end
end
end
end
11 changes: 11 additions & 0 deletions activerecord/lib/active_record/connection_adapters/type/float.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ActiveRecord
module ConnectionAdapters
module Type
class Float < Value # :nodoc:
def type
:float
end
end
end
end
end
11 changes: 11 additions & 0 deletions activerecord/lib/active_record/connection_adapters/type/integer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ActiveRecord
module ConnectionAdapters
module Type
class Integer < Value # :nodoc:
def type
:integer
end
end
end
end
end
11 changes: 11 additions & 0 deletions activerecord/lib/active_record/connection_adapters/type/string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ActiveRecord
module ConnectionAdapters
module Type
class String < Value # :nodoc:
def type
:string
end
end
end
end
end
13 changes: 13 additions & 0 deletions activerecord/lib/active_record/connection_adapters/type/text.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'active_record/connection_adapters/type/string'

module ActiveRecord
module ConnectionAdapters
module Type
class Text < String # :nodoc:
def type
:text
end
end
end
end
end
11 changes: 11 additions & 0 deletions activerecord/lib/active_record/connection_adapters/type/time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ActiveRecord
module ConnectionAdapters
module Type
class Time < Value # :nodoc:
def type
:time
end
end
end
end
end
Loading