2 module ConnectionAdapters #:nodoc:
3 class OracleEnhancedColumn < Column
5 attr_reader :table_name, :forced_column_type, :nchar, :virtual_column_data_default #:nodoc:
7 def initialize(name, default, sql_type = nil, null = true, table_name = nil, forced_column_type = nil, virtual=false) #:nodoc:
8 @table_name = table_name
9 @forced_column_type = forced_column_type
11 super(name, default, sql_type, null)
12 @virtual_column_data_default = default.inspect if virtual
13 # Is column NCHAR or NVARCHAR2 (will need to use N'...' value quoting for these data types)?
14 # Define only when needed as adapter "quote" method will check at first if instance variable is defined.
15 @nchar = true if @type == :string && sql_type[0,1] == 'N'
18 def type_cast(value) #:nodoc:
19 return OracleEnhancedColumn::string_to_raw(value) if type == :raw
20 return guess_date_or_time(value) if type == :datetime && OracleEnhancedAdapter.emulate_dates
28 # convert something to a boolean
29 # added y as boolean value
30 def self.value_to_boolean(value) #:nodoc:
31 if value == true || value == false
33 elsif value.is_a?(String) && value.blank?
36 %w(true t 1 y +).include?(value.to_s.downcase)
40 # convert Time or DateTime value to Date for :date columns
41 def self.string_to_date(string) #:nodoc:
42 return string.to_date if string.is_a?(Time) || string.is_a?(DateTime)
46 # convert Date value to Time for :datetime columns
47 def self.string_to_time(string) #:nodoc:
48 return string.to_time if string.is_a?(Date) && !OracleEnhancedAdapter.emulate_dates
52 # convert RAW column values back to byte strings.
53 def self.string_to_raw(string) #:nodoc:
57 # Get column comment from schema definition.
58 # Will work only if using default ActiveRecord connection.
60 ActiveRecord::Base.connection.column_comment(@table_name, name)
65 def simplified_type(field_type)
68 when /decimal|numeric|number/i
69 if OracleEnhancedAdapter.emulate_booleans && field_type == 'NUMBER(1)'
71 elsif extract_scale(field_type) == 0 ||
72 # if column name is ID or ends with _ID
73 OracleEnhancedAdapter.emulate_integers_by_column_name && OracleEnhancedAdapter.is_integer_column?(name, table_name)
81 if OracleEnhancedAdapter.emulate_booleans_from_strings &&
82 OracleEnhancedAdapter.is_boolean_column?(name, field_type, table_name)
88 if OracleEnhancedAdapter.emulate_dates_by_column_name && OracleEnhancedAdapter.is_date_column?(name, table_name)
102 def guess_date_or_time(value)
103 value.respond_to?(:hour) && (value.hour == 0 and value.min == 0 and value.sec == 0) ?
104 Date.new(value.year, value.month, value.day) : value
110 def fallback_string_to_date(string) #:nodoc:
111 if OracleEnhancedAdapter.string_to_date_format || OracleEnhancedAdapter.string_to_time_format
112 return (string_to_date_or_time_using_format(string).to_date rescue super)
117 def fallback_string_to_time(string) #:nodoc:
118 if OracleEnhancedAdapter.string_to_time_format || OracleEnhancedAdapter.string_to_date_format
119 return (string_to_date_or_time_using_format(string).to_time rescue super)
124 def string_to_date_or_time_using_format(string) #:nodoc:
125 if OracleEnhancedAdapter.string_to_time_format && dt=Date._strptime(string, OracleEnhancedAdapter.string_to_time_format)
126 return Time.parse("#{dt[:year]}-#{dt[:mon]}-#{dt[:mday]} #{dt[:hour]}:#{dt[:min]}:#{dt[:sec]}#{dt[:zone]}")
128 DateTime.strptime(string, OracleEnhancedAdapter.string_to_date_format).to_date