Class ActiveRecord::ConnectionAdapters::OracleAdapter
In: vendor/rails/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb
Parent: AbstractAdapter

This is an Oracle/OCI adapter for the ActiveRecord persistence framework. It relies upon the OCI8 driver, which works with Oracle 8i and above. Most recent development has been on Debian Linux against a 10g database, ActiveRecord 1.12.1 and OCI8 0.1.13. See: rubyforge.org/projects/ruby-oci8/

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.
  • 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. If the column_name ends in _time it‘s created as a Ruby Time. Else if the hours/minutes/seconds are 0, I make it a Ruby Date. Else it‘s a Ruby Time. This is a bit nasty - but if you use Duck Typing you‘ll probably not care very much. In 9i and up it‘s tempting to map DATE to Date and TIMESTAMP to Time, but too many databases use DATE for both. 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.
  • Support for Oracle8 is limited by Rails’ use of ANSI join syntax, which is supported in Oracle9i and later. You will need to use finder_sql for has_and_belongs_to_many associations to run against Oracle8.

Required parameters:

  • :username
  • :password
  • :database

Methods

Public Instance methods

Returns true if the connection is active.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb, line 193
193:         def active?
194:           # Pings the connection to check if it's still good. Note that an
195:           # #active? method is also available, but that simply returns the
196:           # last known state, which isn't good enough if the connection has
197:           # gone stale since the last use.
198:           @connection.ping
199:         rescue OCIException
200:           false
201:         end

ORDER BY clause for the passed order option.

Uses column aliases as defined by distinct.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb, line 480
480:         def add_order_by_for_association_limiting!(sql, options)
481:           return sql if options[:order].blank?
482: 
483:           order = options[:order].split(',').collect { |s| s.strip }.reject(&:blank?)
484:           order.map! {|s| $1 if s =~ / (.*)/}
485:           order = order.zip((0...order.size).to_a).map { |s,i| "alias_#{i}__ #{s}" }.join(', ')
486: 
487:           sql << "ORDER BY #{order}"
488:         end

Disconnects from the database.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb, line 211
211:         def disconnect!
212:           @connection.logoff rescue nil
213:           @connection.active = false
214:         end

SELECT DISTINCT clause for a given set of columns and a given ORDER BY clause.

Oracle requires the ORDER BY columns to be in the SELECT list for DISTINCT queries. However, with those columns included in the SELECT DISTINCT list, you won‘t actually get a distinct list of the column you want (presuming the column has duplicates with multiple values for the ordered-by columns. So we use the FIRST_VALUE function to get a single (first) value for each column, effectively making every row the same.

  distinct("posts.id", "posts.created_at desc")

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb, line 464
464:         def distinct(columns, order_by)
465:           return "DISTINCT #{columns}" if order_by.blank?
466: 
467:           # construct a valid DISTINCT clause, ie. one that includes the ORDER BY columns, using
468:           # FIRST_VALUE such that the inclusion of these columns doesn't invalidate the DISTINCT
469:           order_columns = order_by.split(',').map { |s| s.strip }.reject(&:blank?)
470:           order_columns = order_columns.zip((0...order_columns.size).to_a).map do |c, i|
471:             "FIRST_VALUE(#{c.split.first}) OVER (PARTITION BY #{columns} ORDER BY #{c}) AS alias_#{i}__"
472:           end
473:           sql = "DISTINCT #{columns}, "
474:           sql << order_columns * ", "
475:         end

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?).

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb, line 228
228:         def next_sequence_value(sequence_name)
229:           id = 0
230:           @connection.exec("select #{sequence_name}.nextval id from dual") { |r| id = r[0].to_i }
231:           id
232:         end

Find a table‘s primary key and sequence. Note: Only primary key is implemented - sequence will be nil.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb, line 395
395:         def pk_and_sequence_for(table_name)
396:           (owner, table_name) = @connection.describe(table_name)
397: 
398:           pks = select_values("select cc.column_name\nfrom all_constraints c, all_cons_columns cc\nwhere c.owner = '\#{owner}'\nand c.table_name = '\#{table_name}'\nand c.constraint_type = 'P'\nand cc.owner = c.owner\nand cc.constraint_name = c.constraint_name\n", 'Primary Key')
399: 
400:           # only support single column keys
401:           pks.size == 1 ? [oracle_downcase(pks.first), nil] : nil
402:         end

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

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb, line 267
267:         def prefetch_primary_key?(table_name = nil)
268:           true
269:         end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb, line 184
184:         def quoted_false
185:           "0"
186:         end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb, line 180
180:         def quoted_true
181:           "1"
182:         end

Reconnects to the database.

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb, line 204
204:         def reconnect!
205:           @connection.reset!
206:         rescue OCIException => e
207:           @logger.warn "#{adapter_name} automatic reconnection failed: #{e.message}"
208:         end

[Source]

     # File vendor/rails/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb, line 154
154:         def table_alias_length
155:           30
156:         end

[Validate]