| 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:
Required parameters:
Returns true if the connection is active.
# 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.
# 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.
# 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")
# 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?).
# 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.
# 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.
# 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
# File vendor/rails/activerecord/lib/active_record/connection_adapters/oracle_adapter.rb, line 184
184: def quoted_false
185: "0"
186: end
# 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.
# 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