Class: ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord/monkey_patch/rails5_0.rb,
lib/activerecord/monkey_patch/rails5_1.rb

Instance Method Summary collapse

Instance Method Details

#columns_without_cache(table_name, name = nil) ⇒ Object

Original code is the following URL.

https://github.com/rsim/oracle-enhanced/blob/v1.8.0.beta1/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb#L735-L801


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/activerecord/monkey_patch/rails5_0.rb', line 57

def columns_without_cache(table_name, name = nil) #:nodoc:
  table_name = table_name.to_s
  # get ignored_columns by original table name
  ignored_columns = ignored_table_columns(table_name)

  (owner, desc_table_name, db_link) = @connection.describe(table_name)

  # reset do_not_prefetch_primary_key cache for this table
  @@do_not_prefetch_primary_key[table_name] = nil

  table_cols = <<-SQL.strip.gsub(/\s+/, ' ')
    SELECT cols.column_name AS name, cols.data_type AS sql_type,
           cols.data_default, cols.nullable, cols.virtual_column, cols.hidden_column,
           cols.data_type_owner AS sql_type_owner,
           DECODE(cols.data_type, 'NUMBER', data_precision,
                             'FLOAT', data_precision,
                             'VARCHAR2', DECODE(char_used, 'C', char_length, data_length),
                             'RAW', DECODE(char_used, 'C', char_length, data_length),
                             'CHAR', DECODE(char_used, 'C', char_length, data_length),
                              NULL) AS limit,
           DECODE(data_type, 'NUMBER', data_scale, NULL) AS scale,
           comments.comments as column_comment
      FROM all_tab_cols#{db_link} cols, all_col_comments#{db_link} comments
     WHERE cols.owner      = '#{owner}'
       AND cols.table_name = '#{desc_table_name}'
       AND cols.hidden_column = 'NO'
       AND cols.owner = comments.owner
       AND cols.table_name = comments.table_name
       AND cols.column_name = comments.column_name
     ORDER BY cols.column_id
  SQL

  # added deletion of ignored columns
  select_all(table_cols, name).to_a.delete_if do |row|
    ignored_columns && ignored_columns.include?(row['name'].downcase)
  end.map do |row|
    limit, scale = row['limit'], row['scale']
    if limit || scale
      row['sql_type'] += "(#{(limit || 38).to_i}" + ((scale = scale.to_i) > 0 ? ",#{scale})" : ")")
    end

    if row['sql_type_owner']
      row['sql_type'] = row['sql_type_owner'] + '.' + row['sql_type']
    end

    is_virtual = row['virtual_column']=='YES'

    # clean up odd default spacing from Oracle
    if row['data_default'] && !is_virtual
      row['data_default'].sub!(/^(.*?)\s*$/, '\1')

      # If a default contains a newline these cleanup regexes need to
      # match newlines.
      row['data_default'].sub!(/^'(.*)'$/m, '\1')
      row['data_default'] = nil if row['data_default'] =~ /^(null|empty_[bc]lob\(\))$/i
      # TODO: Needs better fix to fallback "N" to false
      row['data_default'] = false if (row['data_default'] == "N" && OracleEnhancedAdapter.emulate_booleans_from_strings)
    end

     = (row['sql_type'])

    # *** Its a monkey patch paragraph. ***
    if /date/i === .sql_type
      if .type == :date
        .instance_eval('@sql_type = "DATETIME"')
      end
    end

    new_column(oracle_downcase(row['name']),
                     row['data_default'],
                     ,
                     row['nullable'] == 'Y',
                     table_name,
                     is_virtual,
                     false,
                     row['column_comment']
              )
  end
end

#native_database_typesObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/activerecord/monkey_patch/rails5_0.rb', line 39

def native_database_types
  native_database_types_patch = {
    datetime: { name: "DATE" },
    time:     { name: "DATE" }
  }

  if emulate_booleans_from_strings
    ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter::NATIVE_DATABASE_TYPES_BOOLEAN_STRINGS.dup.merge(native_database_types_patch)
  else
    ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter::NATIVE_DATABASE_TYPES.dup.merge(native_database_types_patch)
  end
end

#supports_datetime_with_precision?Boolean

‘supports_datetime_with_precision?` method and `native_database_types` method are overwriting the mapping between Rails and Oracle type to match Rails 4.2 behavior.

The following is the SQL type when generating datetime column.

  • Rails 5 … ‘TIMESTAMP(6)’ SQL type.

  • Rails 4 … ‘DATE’ SQL type. ***This is the behavior of the monkey patch***.

Original code is the following URL.

github.com/rsim/oracle-enhanced/blob/v1.8.0.rc1/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb#L336

Returns:

  • (Boolean)


35
36
37
# File 'lib/activerecord/monkey_patch/rails5_0.rb', line 35

def supports_datetime_with_precision?
  false
end