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:



25
26
27
28
29
30
31
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 25

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)


81
82
83
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 81

def active?
  @raw_connection.active?
end

#auto_retryObject



43
44
45
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 43

def auto_retry
  @raw_connection.auto_retry if @raw_connection
end

#auto_retry=(value) ⇒ Object



47
48
49
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 47

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

#autocommit=(value) ⇒ Object



68
69
70
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 68

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

#autocommit?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 64

def autocommit?
  @raw_connection.autocommit?
end

#commitObject



56
57
58
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 56

def commit
  @raw_connection.commit
end

#describe(name) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 216

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



231
232
233
234
235
236
237
238
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 231

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

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



91
92
93
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 91

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



101
102
103
104
105
106
107
108
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 101

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



51
52
53
54
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 51

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.



75
76
77
78
79
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 75

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

#prepare(sql) ⇒ Object



110
111
112
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 110

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

#raw_oci_connectionObject



33
34
35
36
37
38
39
40
41
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 33

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



85
86
87
88
89
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 85

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

#returning_clause(quoted_pk) ⇒ Object



95
96
97
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 95

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

#rollbackObject



60
61
62
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 60

def rollback
  @raw_connection.rollback
end

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



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
209
210
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 183

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



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
269
270
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 240

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



212
213
214
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 212

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