class ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter

Oracle enhanced adapter will work with both CRuby ruby-oci8 gem (which provides interface to Oracle OCI client) or with JRuby and Oracle JDBC driver.

It should work with Oracle 10g, 11g and 12c databases.

Usage notes:

Required parameters:

Optional parameters:

Optionals NLS parameters:

Fixed NLS values (not overridable):

Constants

ADAPTER_NAME
AUTOGENERATED_SEQUENCE_NAME

use in set_sequence_name to avoid fetching primary key value from sequence

TYPE_MAP
VERSION

Public Class Methods

database_exists?(config) click to toggle source

Oracle enhanced adapter has no implementation because Oracle Database cannot detect ‘NoDatabaseError`. Please refer to the following discussion for details. github.com/rsim/oracle-enhanced/pull/1900

# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 260
def self.database_exists?(config)
  raise NotImplementedError
end
default_sequence_start_value() click to toggle source

Specify default sequence start with value (by default 1 if not explicitly set), e.g.:

ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_sequence_start_value = 10000
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 206
cattr_accessor :default_sequence_start_value
default_tablespaces() click to toggle source

OracleEnhancedAdapter will use the default tablespace, but if you want specific types of objects to go into specific tablespaces, specify them like this in an initializer:

 ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_tablespaces =
{:clob => 'TS_LOB', :blob => 'TS_LOB', :index => 'TS_INDEX', :table => 'TS_DATA'}

Using the :tablespace option where available (e.g create_table) will take precedence over these settings.

# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 178
cattr_accessor :default_tablespaces
emulate_booleans() click to toggle source

By default, the OracleEnhancedAdapter will consider all columns of type NUMBER(1) as boolean. If you wish to disable this emulation you can add the following line to your initializer file:

ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_booleans = false
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 165
cattr_accessor :emulate_booleans
emulate_booleans_from_strings() click to toggle source

If you wish that CHAR(1), VARCHAR2(1) columns are typecasted to booleans then you can add the following line to your initializer file:

ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.emulate_booleans_from_strings = true
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 187
cattr_accessor :emulate_booleans_from_strings
permissions() click to toggle source

By default, OracleEnhanced adapter will grant unlimited tablespace, create session, create table, create view, and create sequence when running the rake task db:create.

If you wish to change these permissions you can add the following line to your initializer file:

ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.permissions =
["create session", "create table", "create view", "create sequence", "create trigger", "ctxapp"]
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 229
cattr_accessor :permissions
use_old_oracle_visitor() click to toggle source

By default, OracleEnhanced adapter will use Oracle12 visitor if database version is Oracle 12.1. If you wish to use Oracle visitor which is intended to work with Oracle 11.2 or lower for Oracle 12.1 database you can add the following line to your initializer file:

ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.use_old_oracle_visitor = true
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 198
cattr_accessor :use_old_oracle_visitor
use_shorter_identifier() click to toggle source

By default, OracleEnhanced adapter will use longer 128 bytes identifier if database version is Oracle 12.2 or higher. If you wish to use shorter 30 byte identifier with Oracle Database supporting longer identifier you can add the following line to your initializer file:

ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.use_shorter_identifier = true
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 217
cattr_accessor :use_shorter_identifier

Private Class Methods

initialize_type_map(m) click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 695
def initialize_type_map(m)
  super
  # oracle
  register_class_with_precision m, %r(WITH TIME ZONE)i,       Type::OracleEnhanced::TimestampTz
  register_class_with_precision m, %r(WITH LOCAL TIME ZONE)i, Type::OracleEnhanced::TimestampLtz
  register_class_with_limit m, %r(raw)i,            Type::OracleEnhanced::Raw
  register_class_with_limit m, %r{^(char)}i,        Type::OracleEnhanced::CharacterString
  register_class_with_limit m, %r{^(nchar)}i,       Type::OracleEnhanced::String
  register_class_with_limit m, %r(varchar)i,        Type::OracleEnhanced::String
  register_class_with_limit m, %r(clob)i,           Type::OracleEnhanced::Text
  register_class_with_limit m, %r(nclob)i,           Type::OracleEnhanced::NationalCharacterText

  m.register_type "NCHAR", Type::OracleEnhanced::NationalCharacterString.new
  m.alias_type %r(NVARCHAR2)i,    "NCHAR"

  m.register_type(%r(NUMBER)i) do |sql_type|
    scale = extract_scale(sql_type)
    precision = extract_precision(sql_type)
    limit = extract_limit(sql_type)
    if scale == 0
      Type::OracleEnhanced::Integer.new(precision: precision, limit: limit)
    else
      Type::Decimal.new(precision: precision, scale: scale)
    end
  end

  if OracleEnhancedAdapter.emulate_booleans
    m.register_type %r(^NUMBER\(1\))i, Type::Boolean.new
  end
end

Public Instance Methods

bind_string(name, value) click to toggle source

create bind object for type String

# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 771
def bind_string(name, value)
  ActiveRecord::Relation::QueryAttribute.new(name, value, Type::OracleEnhanced::String.new)
end
build_statement_pool() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 272
def build_statement_pool
  StatementPool.new(self.class.type_cast_config_to_integer(@config[:statement_limit]))
end
check_version() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 685
def check_version
  version = get_database_version.join(".").to_f

  if version < 10
    raise "Your version of Oracle (#{version}) is too old. Active Record Oracle enhanced adapter supports Oracle >= 10g."
  end
end
clear_table_columns_cache(table_name) click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 588
def clear_table_columns_cache(table_name)
  @columns_cache[table_name.to_s] = nil
end
column_definitions(table_name) click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 562
      def column_definitions(table_name)
        (owner, desc_table_name) = @connection.describe(table_name)

        select_all(<<~SQL.squish, "SCHEMA", [bind_string("owner", owner), bind_string("table_name", desc_table_name)])
          SELECT /*+ OPTIMIZER_FEATURES_ENABLE('11.2.0.2') */ 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 cols, all_col_comments comments
           WHERE cols.owner      = :owner
             AND cols.table_name = :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
      end
current_database() click to toggle source

Current database name

# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 530
      def current_database
        select_value(<<~SQL.squish, "SCHEMA")
          SELECT SYS_CONTEXT('userenv', 'con_name') FROM dual
        SQL
      rescue ActiveRecord::StatementInvalid
        select_value(<<~SQL.squish, "SCHEMA")
          SELECT SYS_CONTEXT('userenv', 'db_name') FROM dual
        SQL
      end
current_schema() click to toggle source

Current database session schema

# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 548
      def current_schema
        select_value(<<~SQL.squish, "SCHEMA")
          SELECT SYS_CONTEXT('userenv', 'current_schema') FROM dual
        SQL
      end
current_user() click to toggle source

Current database session user

# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 541
      def current_user
        select_value(<<~SQL.squish, "SCHEMA")
          SELECT SYS_CONTEXT('userenv', 'session_user') FROM dual
        SQL
      end
default_tablespace() click to toggle source

Default tablespace name of current user

# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 555
      def default_tablespace
        select_value(<<~SQL.squish, "SCHEMA")
          SELECT /*+ OPTIMIZER_FEATURES_ENABLE('11.2.0.2') */ LOWER(default_tablespace) FROM user_users
          WHERE username = SYS_CONTEXT('userenv', 'current_schema')
        SQL
      end
discard!() click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 467
def discard!
  super
  @connection = nil
end
extract_value_from_default(default) click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 733
def extract_value_from_default(default)
  case default
  when String
    default.gsub("''", "'")
  else
    default
  end
end
get_database_version() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 681
def get_database_version
  @connection.database_version
end
index_name_length()
max_identifier_length() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 675
def max_identifier_length
  supports_longer_identifier? ? 128 : 30
end
next_sequence_value(sequence_name) click to toggle source

Returns the next sequence value from a sequence generator. Not generally called directly; used by ActiveRecord to get the next primary key value when inserting a new database record (see prefetch_primary_key?).

# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 478
      def next_sequence_value(sequence_name)
        # if sequence_name is set to :autogenerated then it means that primary key will be populated by trigger
        raise ArgumentError.new "Trigger based primary key is not supported" if sequence_name == AUTOGENERATED_SEQUENCE_NAME
        # call directly connection method to avoid prepared statement which causes fetching of next sequence value twice
        select_value(<<~SQL.squish, "SCHEMA")
          SELECT #{quote_table_name(sequence_name)}.NEXTVAL FROM dual
        SQL
      end
prefetch_primary_key?(table_name = nil) click to toggle source

Returns true for Oracle adapter (since Oracle requires primary key values to be pre-fetched before insert). See also next_sequence_value.

# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 489
def prefetch_primary_key?(table_name = nil)
  return true if table_name.nil?
  table_name = table_name.to_s
  do_not_prefetch = @do_not_prefetch_primary_key[table_name]
  if do_not_prefetch.nil?
    owner, desc_table_name = @connection.describe(table_name)
    @do_not_prefetch_primary_key[table_name] = do_not_prefetch = !has_primary_key?(table_name, owner, desc_table_name)
  end
  !do_not_prefetch
end
primary_key(table_name) click to toggle source

Returns just a table’s primary key

# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 627
def primary_key(table_name)
  pk_and_sequence = pk_and_sequence_for(table_name)
  pk_and_sequence && pk_and_sequence.first
end
raw_connection() click to toggle source

return raw OCI8 or JDBC connection

# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 433
def raw_connection
  @connection.raw_connection
end
reset!() click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 456
def reset!
  clear_cache!
  super
end
select_value_forcing_binds(arel, name, binds) click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 787
def select_value_forcing_binds(arel, name, binds)
  single_value_from_rows(select_values_forcing_binds(arel, name, binds))
end
select_values_forcing_binds(arel, name, binds) click to toggle source

call select_values using binds even if surrounding SQL preparation/execution is done + # with conn.unprepared_statement (like AR.to_sql)

# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 776
def select_values_forcing_binds(arel, name, binds)
  # remove possible force of unprepared SQL during dictionary access
  unprepared_statement_forced = prepared_statements_disabled_cache.include?(object_id)
  prepared_statements_disabled_cache.delete(object_id) if unprepared_statement_forced

  select_values(arel, name, binds)
ensure
  # Restore unprepared_statement setting for surrounding SQL
  prepared_statements_disabled_cache.add(object_id) if unprepared_statement_forced
end
supports_comments?() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 312
def supports_comments?
  true
end
supports_common_table_expressions?() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 292
def supports_common_table_expressions?
  true
end
supports_datetime_with_precision?() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 308
def supports_datetime_with_precision?
  true
end
supports_fetch_first_n_rows_and_offset?() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 300
def supports_fetch_first_n_rows_and_offset?
  if !use_old_oracle_visitor && database_version.first >= 12
    true
  else
    false
  end
end
supports_foreign_keys?() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 284
def supports_foreign_keys?
  true
end
supports_json?() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 324
def supports_json?
  # Oracle Database 12.1 or higher version supports JSON.
  # However, Oracle enhanced adapter has limited support for JSON data type.
  # which does not pass many of ActiveRecord JSON tests.
  #
  # No migration supported for :json type due to there is no `JSON` data type
  # in Oracle Database itself.
  #
  # If you want to use JSON data type, here are steps
  # 1.Define :string or :text in migration
  #
  # create_table :test_posts, force: true do |t|
  #   t.string  :title
  #   t.text    :article
  # end
  #
  # 2. Set :json attributes
  #
  # class TestPost < ActiveRecord::Base
  #  attribute :title, :json
  #  attribute :article, :json
  # end
  #
  # 3. Add `is json` database constraints by running sql statements
  #
  # alter table test_posts add constraint test_posts_title_is_json check (title is json)
  # alter table test_posts add constraint test_posts_article_is_json check (article is json)
  #
  false
end
supports_longer_identifier?() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 355
def supports_longer_identifier?
  if !use_shorter_identifier && database_version.to_s >= [12, 2].to_s
    true
  else
    false
  end
end
supports_multi_insert?() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 316
def supports_multi_insert?
  database_version.to_s >= [11, 2].to_s
end
supports_optimizer_hints?() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 288
def supports_optimizer_hints?
  true
end
supports_views?() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 296
def supports_views?
  true
end
supports_virtual_columns?() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 320
def supports_virtual_columns?
  database_version.first >= 11
end
table_alias_length()
type_map() click to toggle source
# File lib/active_record/connection_adapters/oracle_enhanced_adapter.rb, line 729
def type_map
  TYPE_MAP
end