Class: ActiveRecord::ConnectionAdapters::OracleEnhancedOCIConnection

Inherits:
OracleEnhancedConnection show all
Defined in:
lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb

Overview

OCI database interface for MRI

Defined Under Namespace

Classes: Cursor

Instance Attribute Summary

Attributes inherited from OracleEnhancedConnection

#raw_connection

Instance Method Summary collapse

Methods inherited from OracleEnhancedConnection

create, #oracle_downcase, #select_one, #select_value, #select_values

Constructor Details

#initialize(config) ⇒ OracleEnhancedOCIConnection

:nodoc:



23
24
25
26
27
28
29
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 23

def initialize(config)
  @raw_connection = OCI8EnhancedAutoRecover.new(config, OracleEnhancedOCIFactory)
  # default schema owner
  @owner = config[:schema]
  @owner ||= config[:username]
  @owner = @owner.to_s.upcase
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 79

def active?
  @raw_connection.active?
end

#auto_retryObject



41
42
43
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 41

def auto_retry
  @raw_connection.auto_retry if @raw_connection
end

#auto_retry=(value) ⇒ Object



45
46
47
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 45

def auto_retry=(value)
  @raw_connection.auto_retry = value if @raw_connection
end

#autocommit=(value) ⇒ Object



66
67
68
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 66

def autocommit=(value)
  @raw_connection.autocommit = value
end

#autocommit?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 62

def autocommit?
  @raw_connection.autocommit?
end

#commitObject



54
55
56
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 54

def commit
  @raw_connection.commit
end

#describe(name) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 214

def describe(name)
  # fall back to SELECT based describe if using database link
  return super if name.to_s.include?('@')
  quoted_name = OracleEnhancedAdapter.valid_table_name?(name) ? name : "\"#{name}\""
  @raw_connection.describe(quoted_name)
rescue OCIException => e
  if e.code == 4043
    raise OracleEnhancedConnectionException, %Q{"DESC #{name}" failed; does it exist?}
  else
    # fall back to SELECT which can handle synonyms to database links
    super
  end
end

#error_code(exception) ⇒ Object

Return OCIError error code



229
230
231
232
233
234
235
236
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 229

def error_code(exception)
  case exception
  when OCIError
    exception.code
  else
    nil
  end
end

#exec(sql, *bindvars, &block) ⇒ Object



89
90
91
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 89

def exec(sql, *bindvars, &block)
  @raw_connection.exec(sql, *bindvars, &block)
end

#exec_with_returning(sql) ⇒ Object

execute sql with RETURNING … INTO :insert_id and return :insert_id value



99
100
101
102
103
104
105
106
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 99

def exec_with_returning(sql)
  cursor = @raw_connection.parse(sql)
  cursor.bind_param(':insert_id', nil, Integer)
  cursor.exec
  cursor[':insert_id']
ensure
  cursor.close rescue nil
end

#logoffObject



49
50
51
52
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 49

def logoff
  @raw_connection.logoff
  @raw_connection.active = false
end

#pingObject

Checks connection, returns true if active. Note that ping actively checks the connection, while #active? simply returns the last known state.



73
74
75
76
77
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 73

def ping
  @raw_connection.ping
rescue OCIException => e
  raise OracleEnhancedConnectionException, e.message
end

#prepare(sql) ⇒ Object



108
109
110
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 108

def prepare(sql)
  Cursor.new(self, @raw_connection.parse(sql))
end

#raw_oci_connectionObject



31
32
33
34
35
36
37
38
39
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 31

def raw_oci_connection
  if @raw_connection.is_a? OCI8
    @raw_connection
  # ActiveRecord Oracle enhanced adapter puts OCI8EnhancedAutoRecover wrapper around OCI8
  # in this case we need to pass original OCI8 connection
  else
    @raw_connection.instance_variable_get(:@connection)
  end
end

#reset!Object



83
84
85
86
87
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 83

def reset!
  @raw_connection.reset!
rescue OCIException => e
  raise OracleEnhancedConnectionException, e.message
end

#returning_clause(quoted_pk) ⇒ Object



93
94
95
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 93

def returning_clause(quoted_pk)
  " RETURNING #{quoted_pk} INTO :insert_id"
end

#rollbackObject



58
59
60
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 58

def rollback
  @raw_connection.rollback
end

#select(sql, name = nil, return_column_names = false) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 181

def select(sql, name = nil, return_column_names = false)
  cursor = @raw_connection.exec(sql)
  cols = []
  # Ignore raw_rnum_ which is used to simulate LIMIT and OFFSET
  cursor.get_col_names.each do |col_name|
    col_name = oracle_downcase(col_name)
    cols << col_name unless col_name == 'raw_rnum_'
  end
  # Reuse the same hash for all rows
  column_hash = {}
  cols.each {|c| column_hash[c] = nil}
  rows = []
  get_lob_value = !(name == 'Writable Large Object')

  while row = cursor.fetch
    hash = column_hash.dup

    cols.each_with_index do |col, i|
      hash[col] = typecast_result_value(row[i], get_lob_value)
    end

    rows << hash
  end

  return_column_names ? [rows, cols] : rows
ensure
  cursor.close if cursor
end

#typecast_result_value(value, get_lob_value) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 238

def typecast_result_value(value, get_lob_value)
  case value
  when Fixnum, Bignum
    value
  when String
    value
  when Float, BigDecimal
    # return Fixnum or Bignum if value is integer (to avoid issues with _before_type_cast values for id attributes)
    value == (v_to_i = value.to_i) ? v_to_i : value
  when OraNumber
    # change OraNumber value (returned in early versions of ruby-oci8 2.0.x) to BigDecimal
    value == (v_to_i = value.to_i) ? v_to_i : BigDecimal.new(value.to_s)
  when OCI8::LOB
    if get_lob_value
      data = value.read || ""     # if value.read returns nil, then we have an empty_clob() i.e. an empty string
      # In Ruby 1.9.1 always change encoding to ASCII-8BIT for binaries
      data.force_encoding('ASCII-8BIT') if data.respond_to?(:force_encoding) && value.is_a?(OCI8::BLOB)
      data
    else
      value
    end
  when Time, DateTime
    if OracleEnhancedAdapter.emulate_dates && date_without_time?(value)
      value.to_date
    else
      create_time_with_default_timezone(value)
    end
  else
    value
  end
end

#write_lob(lob, value, is_binary = false) ⇒ Object



210
211
212
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 210

def write_lob(lob, value, is_binary = false)
  lob.write value
end