Class: ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter
- Inherits:
-
AbstractAdapter
- Object
- AbstractAdapter
- ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter
- Includes:
- ActiveRecord::ConnectionAdapters::OracleEnhanced::ContextIndex, ActiveRecord::ConnectionAdapters::OracleEnhanced::DatabaseLimits, ActiveRecord::ConnectionAdapters::OracleEnhanced::DatabaseStatements, ActiveRecord::ConnectionAdapters::OracleEnhanced::DbmsOutput, ActiveRecord::ConnectionAdapters::OracleEnhanced::Quoting, ActiveRecord::ConnectionAdapters::OracleEnhanced::SchemaStatements, ActiveRecord::ConnectionAdapters::OracleEnhanced::StructureDump
- Defined in:
- lib/active_record/connection_adapters/oracle_enhanced_adapter.rb,
lib/active_record/connection_adapters/oracle_enhanced/database_tasks.rb
Overview
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:
-
Key generation assumes a “$table_name_seq” sequence is available for all tables; the sequence name can be changed using ActiveRecord::Base.set_sequence_name. When using Migrations, these sequences are created automatically. Use set_sequence_name :autogenerated with legacy tables that have triggers that populate primary keys automatically.
-
Oracle uses DATE or TIMESTAMP datatypes for both dates and times. Consequently some hacks are employed to map data back to Date or Time in Ruby. Timezones and sub-second precision on timestamps are not supported.
-
Default values that are functions (such as “SYSDATE”) are not supported. This is a restriction of the way ActiveRecord supports default values.
Required parameters:
-
:username
-
:password
-
:database
- either TNS alias or connection string for OCI client or database name in JDBC connection string
Optional parameters:
-
:host
- host name for JDBC connection, defaults to “localhost” -
:port
- port number for JDBC connection, defaults to 1521 -
:privilege
- set “SYSDBA” if you want to connect with this privilege -
:allow_concurrency
- set to “true” if non-blocking mode should be enabled (just for OCI client) -
:prefetch_rows
- how many rows should be fetched at one time to increase performance, defaults to 100 -
:cursor_sharing
- cursor sharing mode to minimize amount of unique statements, no default value -
:time_zone
- database session time zone (it is recommended to set it using ENV which will be then also used for database session time zone) -
:schema
- database schema which holds schema objects. -
:tcp_keepalive
- TCP keepalive is enabled for OCI client, defaults to true -
:tcp_keepalive_time
- TCP keepalive time for OCI client, defaults to 600
Optionals NLS parameters:
-
:nls_calendar
-
:nls_comp
-
:nls_currency
-
:nls_date_language
-
:nls_dual_currency
-
:nls_iso_currency
-
:nls_language
-
:nls_length_semantics
- semantics of size of VARCHAR2 and CHAR columns, defaults toCHAR
(meaning that size specifies number of characters and not bytes) -
:nls_nchar_conv_excp
-
:nls_numeric_characters
-
:nls_sort
-
:nls_territory
-
:nls_timestamp_tz_format
-
:nls_time_format
-
:nls_time_tz_format
Fixed NLS values (not overridable):
-
:nls_date_format
- format for :date columns isYYYY-MM-DD HH24:MI:SS
-
:nls_timestamp_format
- format for :timestamp columns isYYYY-MM-DD HH24:MI:SS:FF6
Direct Known Subclasses
Defined Under Namespace
Classes: DatabaseTasks, StatementPool
Constant Summary collapse
- ADAPTER_NAME =
"OracleEnhanced"
- DEFAULT_NLS_PARAMETERS =
:stopdoc:
{ nls_calendar: nil, nls_comp: nil, nls_currency: nil, nls_date_language: nil, nls_dual_currency: nil, nls_iso_currency: nil, nls_language: nil, nls_length_semantics: "CHAR", nls_nchar_conv_excp: nil, nls_numeric_characters: nil, nls_sort: nil, nls_territory: nil, nls_timestamp_tz_format: nil, nls_time_format: nil, nls_time_tz_format: nil }
- FIXED_NLS_PARAMETERS =
:stopdoc:
{ nls_date_format: "YYYY-MM-DD HH24:MI:SS", nls_timestamp_format: "YYYY-MM-DD HH24:MI:SS:FF6" }
- NATIVE_DATABASE_TYPES =
:stopdoc:
{ primary_key: "NUMBER(38) NOT NULL PRIMARY KEY", string: { name: "VARCHAR2", limit: 255 }, text: { name: "CLOB" }, ntext: { name: "NCLOB" }, integer: { name: "NUMBER", limit: 38 }, float: { name: "BINARY_FLOAT" }, decimal: { name: "NUMBER" }, datetime: { name: "TIMESTAMP" }, timestamp: { name: "TIMESTAMP" }, timestamptz: { name: "TIMESTAMP WITH TIME ZONE" }, timestampltz: { name: "TIMESTAMP WITH LOCAL TIME ZONE" }, time: { name: "TIMESTAMP" }, date: { name: "DATE" }, binary: { name: "BLOB" }, boolean: { name: "NUMBER", limit: 1 }, raw: { name: "RAW", limit: 2000 }, bigint: { name: "NUMBER", limit: 19 } }
- NATIVE_DATABASE_TYPES_BOOLEAN_STRINGS =
if emulate_booleans_from_strings then store booleans in VARCHAR2
NATIVE_DATABASE_TYPES.dup.merge( boolean: { name: "VARCHAR2", limit: 1 } )
- AUTOGENERATED_SEQUENCE_NAME =
use in set_sequence_name to avoid fetching primary key value from sequence
"autogenerated"
- VERSION =
File.read(File.("../../../../../VERSION", __FILE__)).chomp
Constants included from ActiveRecord::ConnectionAdapters::OracleEnhanced::StructureDump
ActiveRecord::ConnectionAdapters::OracleEnhanced::StructureDump::STATEMENT_TOKEN
Constants included from ActiveRecord::ConnectionAdapters::OracleEnhanced::DbmsOutput
ActiveRecord::ConnectionAdapters::OracleEnhanced::DbmsOutput::DBMS_OUTPUT_BUFFER_SIZE
Constants included from ActiveRecord::ConnectionAdapters::OracleEnhanced::DatabaseLimits
ActiveRecord::ConnectionAdapters::OracleEnhanced::DatabaseLimits::IDENTIFIER_MAX_LENGTH
Constants included from ActiveRecord::ConnectionAdapters::OracleEnhanced::Quoting
ActiveRecord::ConnectionAdapters::OracleEnhanced::Quoting::NONQUOTED_OBJECT_NAME, ActiveRecord::ConnectionAdapters::OracleEnhanced::Quoting::VALID_TABLE_NAME
Instance Attribute Summary collapse
-
#auto_retry ⇒ Object
If SQL statement fails due to lost connection then reconnect and retry SQL statement if autocommit mode is enabled.
Class Method Summary collapse
-
.database_exists?(config) ⇒ Boolean
Oracle enhanced adapter has no implementation because Oracle Database cannot detect `NoDatabaseError`.
Instance Method Summary collapse
-
#active? ⇒ Boolean
Returns true if the connection is active.
-
#adapter_name ⇒ Object
:nodoc:.
-
#arel_visitor ⇒ Object
:nodoc:.
- #build_statement_pool ⇒ Object
- #check_version ⇒ Object
- #clear_table_columns_cache(table_name) ⇒ Object
- #column_definitions(table_name) ⇒ Object
-
#columns_for_distinct(columns, orders) ⇒ Object
:nodoc:.
-
#current_database ⇒ Object
Current database name.
-
#current_schema ⇒ Object
Current database session schema.
-
#current_user ⇒ Object
Current database session user.
-
#default_sequence_start_value ⇒ Object
:singleton-method: Specify default sequence start with value (by default 1 if not explicitly set), e.g.:.
-
#default_tablespace ⇒ Object
Default tablespace name of current user.
-
#default_tablespaces ⇒ Object
:singleton-method: 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:.
- #discard! ⇒ Object
-
#disconnect! ⇒ Object
Disconnects from the database.
-
#emulate_booleans ⇒ Object
:singleton-method: By default, the OracleEnhancedAdapter will consider all columns of type
NUMBER(1)
as boolean. -
#emulate_booleans_from_strings ⇒ Object
:singleton-method: If you wish that CHAR(1), VARCHAR2(1) columns are typecasted to booleans then you can add the following line to your initializer file:.
- #get_database_version ⇒ Object
-
#has_primary_key?(table_name, owner = nil, desc_table_name = nil) ⇒ Boolean
:nodoc:.
-
#initialize(connection, logger = nil, config = {}) ⇒ OracleEnhancedAdapter
constructor
:nodoc:.
- #max_identifier_length ⇒ Object (also: #table_alias_length, #index_name_length)
-
#native_database_types ⇒ Object
:startdoc:.
-
#next_sequence_value(sequence_name) ⇒ Object
Returns the next sequence value from a sequence generator.
-
#permissions ⇒ Object
:singleton-method: By default, OracleEnhanced adapter will grant unlimited tablespace, create session, create table, create view, and create sequence when running the rake task db:create.
-
#pk_and_sequence_for(table_name, owner = nil, desc_table_name = nil) ⇒ Object
Find a table's primary key and sequence.
-
#prefetch_primary_key?(table_name = nil) ⇒ Boolean
Returns true for Oracle adapter (since Oracle requires primary key values to be pre-fetched before insert).
-
#primary_key(table_name) ⇒ Object
Returns just a table's primary key.
-
#primary_keys(table_name) ⇒ Object
:nodoc:.
-
#raw_connection ⇒ Object
return raw OCI8 or JDBC connection.
-
#reconnect! ⇒ Object
Reconnects to the database.
- #reset! ⇒ Object
-
#reset_pk_sequence!(table_name, primary_key = nil, sequence_name = nil) ⇒ Object
:nodoc:.
- #supports_comments? ⇒ Boolean
- #supports_common_table_expressions? ⇒ Boolean
- #supports_datetime_with_precision? ⇒ Boolean
- #supports_fetch_first_n_rows_and_offset? ⇒ Boolean
- #supports_foreign_keys? ⇒ Boolean
- #supports_json? ⇒ Boolean
- #supports_longer_identifier? ⇒ Boolean
- #supports_multi_insert? ⇒ Boolean
- #supports_optimizer_hints? ⇒ Boolean
-
#supports_savepoints? ⇒ Boolean
:nodoc:.
-
#supports_transaction_isolation? ⇒ Boolean
:nodoc:.
- #supports_views? ⇒ Boolean
- #supports_virtual_columns? ⇒ Boolean
-
#temporary_table?(table_name) ⇒ Boolean
:nodoc:.
-
#use_old_oracle_visitor ⇒ Object
:singleton-method: By default, OracleEnhanced adapter will use Oracle12 visitor if database version is Oracle 12.1.
-
#use_shorter_identifier ⇒ Object
:singleton-method: By default, OracleEnhanced adapter will use longer 128 bytes identifier if database version is Oracle 12.2 or higher.
Methods included from ActiveRecord::ConnectionAdapters::OracleEnhanced::StructureDump
#execute_structure_dump, #foreign_key_definition, #full_drop, #structure_drop, #structure_dump, #structure_dump_column, #structure_dump_column_comments, #structure_dump_db_stored_code, #structure_dump_fk_constraints, #structure_dump_indexes, #structure_dump_primary_key, #structure_dump_synonyms, #structure_dump_table_comments, #structure_dump_unique_keys, #structure_dump_views, #structure_dump_virtual_column, #temp_table_drop
Methods included from ActiveRecord::ConnectionAdapters::OracleEnhanced::DbmsOutput
#dbms_output_enabled?, #disable_dbms_output, #enable_dbms_output
Methods included from ActiveRecord::ConnectionAdapters::OracleEnhanced::DatabaseLimits
#column_name_length, #in_clause_length, #sequence_name_length, #table_name_length
Methods included from ActiveRecord::ConnectionAdapters::OracleEnhanced::Quoting
#_quote, #_type_cast, #column_name_matcher, #column_name_with_order_matcher, mixed_case?, #quote_column_name, #quote_column_name_or_expression, #quote_string, #quote_table_name, #quoted_false, #quoted_true, #unquoted_false, #unquoted_true, valid_table_name?
Methods included from ActiveRecord::ConnectionAdapters::OracleEnhanced::OCIQuoting
Methods included from ActiveRecord::ConnectionAdapters::OracleEnhanced::JDBCQuoting
Methods included from ActiveRecord::ConnectionAdapters::OracleEnhanced::ContextIndex
#add_context_index, #remove_context_index
Methods included from ActiveRecord::ConnectionAdapters::OracleEnhanced::SchemaStatements
#add_column, #add_index, #add_index_options, #add_reference, #add_synonym, #aliased_types, #change_column, #change_column_comment, #change_column_default, #change_column_null, #change_table_comment, #column_comment, #columns, #create_alter_table, #create_schema_dumper, #create_table, #data_source_exists?, #data_sources, #disable_referential_integrity, #drop_table, #extract_foreign_key_action, #foreign_keys, #index_name, #index_name_exists?, #indexes, #insert_versions_sql, #materialized_views, #remove_column, #remove_index, #remove_synonym, #rename_column, #rename_index, #rename_table, #synonyms, #table_comment, #table_exists?, #table_options, #tables, #tablespace, #type_to_sql, #update_table_definition, #views
Methods included from ActiveRecord::ConnectionAdapters::OracleEnhanced::DatabaseStatements
#begin_db_transaction, #begin_isolated_db_transaction, #clear_cache!, #commit_db_transaction, #create_savepoint, #default_sequence_name, #empty_insert_statement_value, #exec_insert, #exec_query, #exec_rollback_db_transaction, #exec_rollback_to_savepoint, #exec_update, #execute, #explain, #insert, #insert_fixture, #insert_fixtures_set, #release_savepoint, #sql_for_insert, #supports_explain?, #transaction_isolation_levels, #write_lobs
Constructor Details
#initialize(connection, logger = nil, config = {}) ⇒ OracleEnhancedAdapter
:nodoc:
242 243 244 245 246 247 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 242 def initialize(connection, logger = nil, config = {}) # :nodoc: super(connection, logger, config) @enable_dbms_output = false @do_not_prefetch_primary_key = {} @columns_cache = {} end |
Instance Attribute Details
#auto_retry ⇒ Object
If SQL statement fails due to lost connection then reconnect and retry SQL statement if autocommit mode is enabled. By default this functionality is disabled.
423 424 425 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 423 def auto_retry @auto_retry end |
Class Method Details
.database_exists?(config) ⇒ Boolean
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
259 260 261 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 259 def self.database_exists?(config) raise NotImplementedError end |
Instance Method Details
#active? ⇒ Boolean
Returns true if the connection is active.
437 438 439 440 441 442 443 444 445 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 437 def active? #:nodoc: # Pings the connection to check if it's still good. Note that an # #active? method is also available, but that simply returns the # last known state, which isn't good enough if the connection has # gone stale since the last use. @connection.ping rescue OracleEnhanced::ConnectionException false end |
#adapter_name ⇒ Object
:nodoc:
251 252 253 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 251 def adapter_name #:nodoc: ADAPTER_NAME end |
#arel_visitor ⇒ Object
:nodoc:
263 264 265 266 267 268 269 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 263 def arel_visitor # :nodoc: if supports_fetch_first_n_rows_and_offset? Arel::Visitors::Oracle12.new(self) else Arel::Visitors::Oracle.new(self) end end |
#build_statement_pool ⇒ Object
271 272 273 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 271 def build_statement_pool StatementPool.new(self.class.type_cast_config_to_integer(@config[:statement_limit])) end |
#check_version ⇒ Object
684 685 686 687 688 689 690 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 684 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) ⇒ Object
587 588 589 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 587 def clear_table_columns_cache(table_name) @columns_cache[table_name.to_s] = nil end |
#column_definitions(table_name) ⇒ Object
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 561 def column_definitions(table_name) (owner, desc_table_name) = @connection.describe(table_name) select_all(<<~SQL.squish, "SCHEMA") 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 = #{quote(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 end |
#columns_for_distinct(columns, orders) ⇒ Object
:nodoc:
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 651 def columns_for_distinct(columns, orders) #:nodoc: # construct a valid columns name for DISTINCT clause, # ie. one that includes the ORDER BY columns, using FIRST_VALUE such that # the inclusion of these columns doesn't invalidate the DISTINCT # # It does not construct DISTINCT clause. Just return column names for distinct. order_columns = orders.reject(&:blank?).map { |s| s = visitor.compile(s) unless s.is_a?(String) # remove any ASC/DESC modifiers s.gsub(/\s+(ASC|DESC)\s*?/i, "") }.reject(&:blank?).map.with_index { |column, i| "FIRST_VALUE(#{column}) OVER (PARTITION BY #{columns} ORDER BY #{column}) AS alias_#{i}__" } (order_columns << super).join(", ") end |
#current_database ⇒ Object
Current database name
529 530 531 532 533 534 535 536 537 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 529 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 ⇒ Object
Current database session schema
547 548 549 550 551 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 547 def current_schema select_value(<<~SQL.squish, "SCHEMA") SELECT SYS_CONTEXT('userenv', 'current_schema') FROM dual SQL end |
#current_user ⇒ Object
Current database session user
540 541 542 543 544 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 540 def current_user select_value(<<~SQL.squish, "SCHEMA") SELECT SYS_CONTEXT('userenv', 'session_user') FROM dual SQL end |
#default_sequence_start_value ⇒ Object
:singleton-method: Specify default sequence start with value (by default 1 if not explicitly set), e.g.:
ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter.default_sequence_start_value = 10000
205 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 205 cattr_accessor :default_sequence_start_value |
#default_tablespace ⇒ Object
Default tablespace name of current user
554 555 556 557 558 559 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 554 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 |
#default_tablespaces ⇒ Object
:singleton-method: 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.
177 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 177 cattr_accessor :default_tablespaces |
#discard! ⇒ Object
466 467 468 469 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 466 def discard! super @connection = nil end |
#disconnect! ⇒ Object
Disconnects from the database.
461 462 463 464 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 461 def disconnect! #:nodoc: super @connection.logoff rescue nil end |
#emulate_booleans ⇒ Object
:singleton-method: 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
164 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 164 cattr_accessor :emulate_booleans |
#emulate_booleans_from_strings ⇒ Object
:singleton-method: 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
186 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 186 cattr_accessor :emulate_booleans_from_strings |
#get_database_version ⇒ Object
680 681 682 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 680 def get_database_version @connection.database_version end |
#has_primary_key?(table_name, owner = nil, desc_table_name = nil) ⇒ Boolean
:nodoc:
631 632 633 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 631 def has_primary_key?(table_name, owner = nil, desc_table_name = nil) #:nodoc: !pk_and_sequence_for(table_name, owner, desc_table_name).nil? end |
#max_identifier_length ⇒ Object Also known as: table_alias_length, index_name_length
674 675 676 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 674 def max_identifier_length supports_longer_identifier? ? 128 : 30 end |
#native_database_types ⇒ Object
:startdoc:
413 414 415 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 413 def native_database_types #:nodoc: emulate_booleans_from_strings ? NATIVE_DATABASE_TYPES_BOOLEAN_STRINGS : NATIVE_DATABASE_TYPES end |
#next_sequence_value(sequence_name) ⇒ Object
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?).
477 478 479 480 481 482 483 484 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 477 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 |
#permissions ⇒ Object
:singleton-method: 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. =
["create session", "create table", "create view", "create sequence", "create trigger", "ctxapp"]
228 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 228 cattr_accessor :permissions |
#pk_and_sequence_for(table_name, owner = nil, desc_table_name = nil) ⇒ Object
Find a table's primary key and sequence. Note: Only primary key is implemented - sequence will be nil.
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 593 def pk_and_sequence_for(table_name, owner = nil, desc_table_name = nil) #:nodoc: (owner, desc_table_name) = @connection.describe(table_name) seqs = select_values(<<~SQL.squish, "SCHEMA") select /*+ OPTIMIZER_FEATURES_ENABLE('11.2.0.2') */ us.sequence_name from all_sequences us where us.sequence_owner = '#{owner}' and us.sequence_name = upper(#{quote(default_sequence_name(desc_table_name))}) SQL # changed back from user_constraints to all_constraints for consistency pks = select_values(<<~SQL.squish, "SCHEMA") SELECT /*+ OPTIMIZER_FEATURES_ENABLE('11.2.0.2') */ cc.column_name FROM all_constraints c, all_cons_columns cc WHERE c.owner = '#{owner}' AND c.table_name = #{quote(desc_table_name)} AND c.constraint_type = 'P' AND cc.owner = c.owner AND cc.constraint_name = c.constraint_name SQL warn <<~WARNING if pks.count > 1 WARNING: Active Record does not support composite primary key. #{table_name} has composite primary key. Composite primary key is ignored. WARNING # only support single column keys pks.size == 1 ? [oracle_downcase(pks.first), oracle_downcase(seqs.first)] : nil end |
#prefetch_primary_key?(table_name = nil) ⇒ Boolean
Returns true for Oracle adapter (since Oracle requires primary key values to be pre-fetched before insert). See also #next_sequence_value.
488 489 490 491 492 493 494 495 496 497 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 488 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) ⇒ Object
Returns just a table's primary key
626 627 628 629 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 626 def primary_key(table_name) pk_and_sequence = pk_and_sequence_for(table_name) pk_and_sequence && pk_and_sequence.first end |
#primary_keys(table_name) ⇒ Object
:nodoc:
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 635 def primary_keys(table_name) # :nodoc: (_owner, desc_table_name) = @connection.describe(table_name) pks = select_values(<<~SQL.squish, "SCHEMA", [bind_string("table_name", desc_table_name)]) SELECT /*+ OPTIMIZER_FEATURES_ENABLE('11.2.0.2') */ cc.column_name FROM all_constraints c, all_cons_columns cc WHERE c.owner = SYS_CONTEXT('userenv', 'current_schema') AND c.table_name = :table_name AND c.constraint_type = 'P' AND cc.owner = c.owner AND cc.constraint_name = c.constraint_name order by cc.position SQL pks.map { |pk| oracle_downcase(pk) } end |
#raw_connection ⇒ Object
return raw OCI8 or JDBC connection
432 433 434 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 432 def raw_connection @connection.raw_connection end |
#reconnect! ⇒ Object
Reconnects to the database.
448 449 450 451 452 453 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 448 def reconnect! #:nodoc: super @connection.reset! rescue OracleEnhanced::ConnectionException => e @logger.warn "#{adapter_name} automatic reconnection failed: #{e.}" if @logger end |
#reset! ⇒ Object
455 456 457 458 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 455 def reset! clear_cache! super end |
#reset_pk_sequence!(table_name, primary_key = nil, sequence_name = nil) ⇒ Object
:nodoc:
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 499 def reset_pk_sequence!(table_name, primary_key = nil, sequence_name = nil) #:nodoc: return nil unless data_source_exists?(table_name) unless primary_key && sequence_name # *Note*: Only primary key is implemented - sequence will be nil. primary_key, sequence_name = pk_and_sequence_for(table_name) # TODO This sequence_name implemantation is just enough # to satisty fixures. To get correct sequence_name always # pk_and_sequence_for method needs some work. begin sequence_name = table_name.classify.constantize.sequence_name rescue sequence_name = default_sequence_name(table_name) end end if @logger && primary_key && !sequence_name @logger.warn "#{table_name} has primary key #{primary_key} with no default sequence" end if primary_key && sequence_name new_start_value = select_value(<<~SQL.squish, "SCHEMA") select NVL(max(#{quote_column_name(primary_key)}),0) + 1 from #{quote_table_name(table_name)} SQL execute "DROP SEQUENCE #{quote_table_name(sequence_name)}" execute "CREATE SEQUENCE #{quote_table_name(sequence_name)} START WITH #{new_start_value}" end end |
#supports_comments? ⇒ Boolean
311 312 313 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 311 def supports_comments? true end |
#supports_common_table_expressions? ⇒ Boolean
291 292 293 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 291 def supports_common_table_expressions? true end |
#supports_datetime_with_precision? ⇒ Boolean
307 308 309 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 307 def supports_datetime_with_precision? true end |
#supports_fetch_first_n_rows_and_offset? ⇒ Boolean
299 300 301 302 303 304 305 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 299 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? ⇒ Boolean
283 284 285 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 283 def supports_foreign_keys? true end |
#supports_json? ⇒ Boolean
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 323 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? ⇒ Boolean
354 355 356 357 358 359 360 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 354 def supports_longer_identifier? if !use_shorter_identifier && database_version.to_s >= [12, 2].to_s true else false end end |
#supports_multi_insert? ⇒ Boolean
315 316 317 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 315 def supports_multi_insert? database_version.to_s >= [11, 2].to_s end |
#supports_optimizer_hints? ⇒ Boolean
287 288 289 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 287 def supports_optimizer_hints? true end |
#supports_savepoints? ⇒ Boolean
:nodoc:
275 276 277 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 275 def supports_savepoints? #:nodoc: true end |
#supports_transaction_isolation? ⇒ Boolean
:nodoc:
279 280 281 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 279 def supports_transaction_isolation? #:nodoc: true end |
#supports_views? ⇒ Boolean
295 296 297 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 295 def supports_views? true end |
#supports_virtual_columns? ⇒ Boolean
319 320 321 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 319 def supports_virtual_columns? database_version.first >= 11 end |
#temporary_table?(table_name) ⇒ Boolean
:nodoc:
667 668 669 670 671 672 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 667 def temporary_table?(table_name) #:nodoc: select_value(<<~SQL.squish, "SCHEMA", [bind_string("table_name", table_name.upcase)]) == "Y" SELECT /*+ OPTIMIZER_FEATURES_ENABLE('11.2.0.2') */ temporary FROM all_tables WHERE table_name = :table_name and owner = SYS_CONTEXT('userenv', 'current_schema') SQL end |
#use_old_oracle_visitor ⇒ Object
:singleton-method: 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
197 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 197 cattr_accessor :use_old_oracle_visitor |
#use_shorter_identifier ⇒ Object
:singleton-method: 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
216 |
# File 'lib/active_record/connection_adapters/oracle_enhanced_adapter.rb', line 216 cattr_accessor :use_shorter_identifier |