Class: PLSQL::OCIConnection

Inherits:
Connection show all
Defined in:
lib/plsql/oci_connection.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Cursor

Constant Summary

Constants inherited from Connection

Connection::RUBY_TEMP_TABLE_PREFIX

Instance Attribute Summary

Attributes inherited from Connection

#activerecord_class, #raw_driver

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Connection

create, create_new, #describe_synonym, driver_type, #drop_all_ruby_temporary_tables, #drop_session_ruby_temporary_tables, #initialize, #jdbc?, #oci?, #raw_connection, #select_all, #select_first, #select_hash_all, #select_hash_first, #session_id, #set_time_zone, #time_zone

Constructor Details

This class inherits a constructor from PLSQL::Connection

Class Method Details

.create_raw(params) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/plsql/oci_connection.rb', line 24

def self.create_raw(params)
  connection_string = if params[:host]
    "//#{params[:host]}:#{params[:port] || 1521}/#{params[:database]}"
  else
    params[:database]
  end
  new(OCI8.new(params[:username], params[:password], connection_string))
end

Instance Method Details

#autocommit=(value) ⇒ Object



50
51
52
# File 'lib/plsql/oci_connection.rb', line 50

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

#autocommit?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/plsql/oci_connection.rb', line 46

def autocommit?
  raw_connection.autocommit?
end

#commitObject



38
39
40
# File 'lib/plsql/oci_connection.rb', line 38

def commit
  raw_connection.commit
end

#cursor_from_query(sql, bindvars = [], options = {}) ⇒ Object



137
138
139
# File 'lib/plsql/oci_connection.rb', line 137

def cursor_from_query(sql, bindvars = [], options = {})
  Cursor.new_from_query(self, sql, bindvars, options)
end

#database_versionObject



275
276
277
278
# File 'lib/plsql/oci_connection.rb', line 275

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

#exec(sql, *bindvars) ⇒ Object



58
59
60
61
# File 'lib/plsql/oci_connection.rb', line 58

def exec(sql, *bindvars)
  raw_connection.exec(sql, *bindvars)
  true
end

#logoffObject



33
34
35
36
# File 'lib/plsql/oci_connection.rb', line 33

def logoff
  super
  raw_connection.logoff
end

#ora_value_to_ruby_value(value) ⇒ Object



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
271
272
273
# File 'lib/plsql/oci_connection.rb', line 245

def ora_value_to_ruby_value(value)
  case value
  when Float, OraNumber, BigDecimal
    ora_number_to_ruby_number(value)
  when DateTime, OraDate
    ora_date_to_ruby_date(value)
  when OCI8::LOB
    if value.available?
      value.rewind
      value.read
    else
      nil
    end
  when OCI8::Object::Base
    tdo = raw_oci_connection.get_tdo_by_class(value.class)
    if tdo.is_collection?
      value.to_ary.map { |e| ora_value_to_ruby_value(e) }
    else # object type
      tdo.attributes.inject({}) do |hash, attr|
        hash[attr.name] = ora_value_to_ruby_value(value.instance_variable_get(:@attributes)[attr.name])
        hash
      end
    end
  when OCI8::Cursor
    Cursor.new(self, value)
  else
    value
  end
end

#parse(sql) ⇒ Object



133
134
135
# File 'lib/plsql/oci_connection.rb', line 133

def parse(sql)
  Cursor.new_from_parse(self, sql)
end

#plsql_to_ruby_data_type(metadata) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/plsql/oci_connection.rb', line 141

def plsql_to_ruby_data_type()
  data_type, data_length = [:data_type], [:data_length]
  case data_type
  when "VARCHAR", "VARCHAR2", "CHAR", "NVARCHAR2", "NCHAR"
    [String, data_length || 32767]
  when "CLOB", "NCLOB"
    [OCI8::CLOB, nil]
  when "BLOB"
    [OCI8::BLOB, nil]
  when "NUMBER", "NATURAL", "NATURALN", "POSITIVE", "POSITIVEN", "SIGNTYPE", "SIMPLE_INTEGER", "PLS_INTEGER", "BINARY_INTEGER"
    [OraNumber, nil]
  when "DATE"
    [DateTime, nil]
  when "TIMESTAMP", "TIMESTAMP WITH TIME ZONE", "TIMESTAMP WITH LOCAL TIME ZONE"
    [Time, nil]
  when "TABLE", "VARRAY", "OBJECT", "XMLTYPE", "OPAQUE/XMLTYPE"
    # create Ruby class for collection
    klass = OCI8::Object::Base.get_class_by_typename([:sql_type_name])
    unless klass
      klass = Class.new(OCI8::Object::Base)
      klass.set_typename [:sql_type_name]
    end
    [klass, nil]
  when "REF CURSOR"
    [OCI8::Cursor]
  else
    [String, 32767]
  end
end

#prefetch_rows=(value) ⇒ Object



54
55
56
# File 'lib/plsql/oci_connection.rb', line 54

def prefetch_rows=(value)
  raw_connection.prefetch_rows = value
end

#rollbackObject



42
43
44
# File 'lib/plsql/oci_connection.rb', line 42

def rollback
  raw_connection.rollback
end

#ruby_value_to_ora_value(value, type = nil) ⇒ Object



171
172
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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/plsql/oci_connection.rb', line 171

def ruby_value_to_ora_value(value, type = nil)
  type ||= value.class
  case type.to_s.to_sym
  when :Integer, :BigDecimal, :String
    value
  when :OraNumber
    # pass parameters as OraNumber to avoid rounding errors
    case value
    when BigDecimal
      OraNumber.new(value.to_s("F"))
    when TrueClass
      OraNumber.new(1)
    when FalseClass
      OraNumber.new(0)
    else
      value
    end
  when :DateTime
    case value
    when Time
      ::DateTime.civil(value.year, value.month, value.day, value.hour, value.min, value.sec, Rational(value.utc_offset, 86400))
    when DateTime
      value
    when Date
      ::DateTime.civil(value.year, value.month, value.day, 0, 0, 0, 0)
    else
      value
    end
  when :"OCI8::CLOB", :"OCI8::BLOB"
    # ruby-oci8 cannot create CLOB/BLOB from ''
    value.to_s.length > 0 ? type.new(raw_oci_connection, value) : nil
  when :"OCI8::Cursor"
    value && value.raw_cursor
  else
    # collections and object types
    if type.superclass == OCI8::Object::Base
      return nil if value.nil?
      tdo = raw_oci_connection.get_tdo_by_class(type)
      if tdo.is_collection?
        raise ArgumentError, "You should pass Array value for collection type parameter" unless value.is_a?(Array)
        elem_list = value.map do |elem|
          if (attr_tdo = tdo.coll_attr.typeinfo)
            attr_type, _ = plsql_to_ruby_data_type(data_type: "OBJECT", sql_type_name: attr_tdo.typename)
          else
            attr_type = elem.class
          end
          ruby_value_to_ora_value(elem, attr_type)
        end
        # construct collection value
        # TODO: change setting instance variable to appropriate ruby-oci8 method call when available
        collection = type.new(raw_oci_connection)
        collection.instance_variable_set("@attributes", elem_list)
        collection
      else # object type
        raise ArgumentError, "You should pass Hash value for object type parameter" unless value.is_a?(Hash)
        object_attrs = value.dup
        object_attrs.keys.each do |key|
          raise ArgumentError, "Wrong object type field passed to PL/SQL procedure" unless (attr = tdo.attr_getters[key])
          case attr.datatype
          when OCI8::TDO::ATTR_NAMED_TYPE, OCI8::TDO::ATTR_NAMED_COLLECTION
            # nested object type or collection
            attr_type, _ = plsql_to_ruby_data_type(data_type: "OBJECT", sql_type_name: attr.typeinfo.typename)
            object_attrs[key] = ruby_value_to_ora_value(object_attrs[key], attr_type)
          end
        end
        type.new(raw_oci_connection, object_attrs)
      end
    # all other cases
    else
      value
    end
  end
end