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

Instance Attribute Summary

Attributes inherited from OracleEnhancedConnection

#raw_connection

Instance Method Summary collapse

Methods inherited from OracleEnhancedConnection

create, #oracle_downcase

Constructor Details

#initialize(config) ⇒ OracleEnhancedOCIConnection

Returns a new instance of OracleEnhancedOCIConnection.



29
30
31
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 29

def initialize(config)
  @raw_connection = OCI8EnhancedAutoRecover.new(config, OracleEnhancedOCIFactory)
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

def active?
  @raw_connection.active?
end

#auto_retryObject



33
34
35
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 33

def auto_retry
  @raw_connection.auto_retry if @raw_connection
end

#auto_retry=(value) ⇒ Object



37
38
39
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 37

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

#autocommit=(value) ⇒ Object



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

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

#autocommit?Boolean

Returns:

  • (Boolean)


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

def autocommit?
  @raw_connection.autocommit?
end

#commitObject



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

def commit
  @raw_connection.commit
end

#describe(name) ⇒ Object



165
166
167
168
169
170
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 165

def describe(name)
  quoted_name = OracleEnhancedAdapter.valid_table_name?(name) ? name : "\"#{name}\""
  @raw_connection.describe(quoted_name)
rescue OCIException => e
  raise OracleEnhancedConnectionException, e.message
end

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



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

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

#logoffObject



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

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.



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

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

#reset!Object



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

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

#rollbackObject



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

def rollback
  @raw_connection.rollback
end

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 85

def select(sql, name = nil, return_column_names = false)
  cursor = @raw_connection.exec(sql)
  cols = cursor.get_col_names.map { |x| oracle_downcase(x) }
  rows = []

  while row = cursor.fetch
    hash = Hash.new

    cols.each_with_index do |col, i|
      hash[col] =
        case row[i]
        when OCI8::LOB
          if name == 'Writable Large Object'
            row[i]
          else
            data =  row[i].read
            # In Ruby 1.9.1 always change encoding to ASCII-8BIT for binaries
            data.force_encoding('ASCII-8BIT') if data.respond_to?(:force_encoding) && row[i].is_a?(OCI8::BLOB)
            data
          end
        # ruby-oci8 1.0 returns OraDate
        when OraDate
          d = row[i]
          # RSI: added emulate_dates_by_column_name functionality
          # if emulate_dates_by_column_name && self.class.is_date_column?(col)
          #   d.to_date
          # elsif
          if OracleEnhancedAdapter.emulate_dates && (d.hour == 0 && d.minute == 0 && d.second == 0)
            d.to_date
          else
            # code from Time.time_with_datetime_fallback
            begin
              Time.send(Base.default_timezone, d.year, d.month, d.day, d.hour, d.minute, d.second)
            rescue
              offset = Base.default_timezone.to_sym == :local ? ::DateTime.local_offset : 0
              ::DateTime.civil(d.year, d.month, d.day, d.hour, d.minute, d.second, offset)
            end
          end
        # ruby-oci8 2.0 returns Time or DateTime
        when Time, DateTime
          d = row[i]
          if OracleEnhancedAdapter.emulate_dates && (d.hour == 0 && d.min == 0 && d.sec == 0)
            d.to_date
          else
            # recreate Time or DateTime using Base.default_timezone
            begin
              Time.send(Base.default_timezone, d.year, d.month, d.day, d.hour, d.min, d.sec)
            rescue
              offset = Base.default_timezone.to_sym == :local ? ::DateTime.local_offset : 0
              ::DateTime.civil(d.year, d.month, d.day, d.hour, d.min, d.sec, offset)
            end
          end
        # RSI: added emulate_integers_by_column_name functionality
        when Float
          n = row[i]
          if OracleEnhancedAdapter.emulate_integers_by_column_name && OracleEnhancedAdapter.is_integer_column?(col)
            n.to_i
          else
            n
          end
        # ruby-oci8 2.0 returns OraNumber - convert it to Integer or BigDecimal
        when OraNumber
          n = row[i]
          n == (n_to_i = n.to_i) ? n_to_i : BigDecimal.new(n.to_s)
        else row[i]
        end unless col == 'raw_rnum_'
    end

    rows << hash
  end

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

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



161
162
163
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 161

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