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
# 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[:username].to_s.upcase
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 77

def active?
  @raw_connection.active?
end

#auto_retryObject



39
40
41
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 39

def auto_retry
  @raw_connection.auto_retry if @raw_connection
end

#auto_retry=(value) ⇒ Object



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

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

#autocommit=(value) ⇒ Object



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

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

#autocommit?Boolean

Returns:

  • (Boolean)


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

def autocommit?
  @raw_connection.autocommit?
end

#commitObject



52
53
54
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 52

def commit
  @raw_connection.commit
end

#describe(name) ⇒ Object



206
207
208
209
210
211
212
213
214
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 206

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
  # fall back to SELECT which can handle synonyms to database links
  super
end

#error_code(exception) ⇒ Object

Return OCIError error code



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

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

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



87
88
89
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 87

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



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

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



47
48
49
50
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 47

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.



71
72
73
74
75
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 71

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

#prepare(sql) ⇒ Object



106
107
108
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 106

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

#raw_oci_connectionObject



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

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



81
82
83
84
85
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 81

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

#returning_clause(quoted_pk) ⇒ Object



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

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

#rollbackObject



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

def rollback
  @raw_connection.rollback
end

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 173

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



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 226

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
  # ruby-oci8 1.0 returns OraDate
  # ruby-oci8 2.0 returns Time or DateTime
  when OraDate, 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



202
203
204
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 202

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