Class: ActiveRecord::ConnectionAdapters::OracleEnhanced::OCIConnection

Inherits:
Connection
  • Object
show all
Defined in:
lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Cursor

Instance Attribute Summary

Attributes inherited from Connection

#raw_connection

Instance Method Summary collapse

Methods inherited from Connection

create

Constructor Details

#initialize(config) ⇒ OCIConnection

Returns a new instance of OCIConnection.

[View source]

30
31
32
33
34
35
36
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 30

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)
[View source]

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

def active?
  @raw_connection.active?
end

#auto_retryObject

[View source]

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

def auto_retry
  @raw_connection.auto_retry if @raw_connection
end

#auto_retry=(value) ⇒ Object

[View source]

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

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

#autocommit=(value) ⇒ Object

[View source]

73
74
75
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 73

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

#autocommit?Boolean

Returns:

  • (Boolean)
[View source]

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

def autocommit?
  @raw_connection.autocommit?
end

#commitObject

[View source]

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

def commit
  @raw_connection.commit
end

#database_versionObject

[View source]

271
272
273
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 271

def database_version
  @database_version ||= (version = raw_connection.oracle_server_version) && [version.major, version.minor]
end

#describe(name) ⇒ Object

[View source]

232
233
234
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 232

def describe(name)
  super
end

#error_code(exception) ⇒ Object

Return OCIError error code

[View source]

237
238
239
240
241
242
243
244
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 237

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

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

[View source]

100
101
102
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 100

def exec(sql, *bindvars, allow_retry: false, &block)
  with_retry(allow_retry: allow_retry) { @raw_connection.exec(sql, *bindvars, &block) }
end

#logoffObject

[View source]

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

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.

[View source]

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

def ping
  @raw_connection.ping
rescue OCIException => e
  raise OracleEnhanced::ConnectionException, e.message
end

#prepare(sql) ⇒ Object

[View source]

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

[View source]

38
39
40
41
42
43
44
45
46
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 38

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(:@raw_connection)
  end
end

#resetObject

[View source]

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

def reset
  @raw_connection.reset
end

#reset!Object

[View source]

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

def reset!
  @raw_connection.reset!
rescue OCIException => e
  raise OracleEnhanced::ConnectionException, e.message
end

#rollbackObject

[View source]

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

def rollback
  @raw_connection.rollback
end

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

[View source]

190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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 190

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|
      col_value = typecast_result_value(row[i], get_lob_value)
       = cursor..fetch(i)
      if !.nil?
        key = .data_type
        case key.to_s.downcase
        when "char"
          col_value = col_value.to_s.rstrip
        end
      end
      hash[col] = col_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

[View source]

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 246

def typecast_result_value(value, get_lob_value)
  case value
  when Integer
    value
  when String
    value
  when Float, BigDecimal
    # return Integer 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 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
    create_time_with_default_timezone(value)
  else
    value
  end
end

#with_retry(allow_retry: false, &block) ⇒ Object

[View source]

104
105
106
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 104

def with_retry(allow_retry: false, &block)
  @raw_connection.with_retry(allow_retry: allow_retry, &block)
end

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

[View source]

228
229
230
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 228

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