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



28
29
30
31
32
33
34
35
# File 'lib/plsql/oci_connection.rb', line 28

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



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

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

#autocommit?Boolean

Returns:

  • (Boolean)


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

def autocommit?
  raw_connection.autocommit?
end

#commitObject



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

def commit
  raw_connection.commit
end

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



141
142
143
# File 'lib/plsql/oci_connection.rb', line 141

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

#database_versionObject



279
280
281
282
# File 'lib/plsql/oci_connection.rb', line 279

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

#exec(sql, *bindvars) ⇒ Object



62
63
64
65
# File 'lib/plsql/oci_connection.rb', line 62

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

#logoffObject



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

def logoff
  super
  raw_connection.logoff
end

#ora_value_to_ruby_value(value) ⇒ Object



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
274
275
276
277
# File 'lib/plsql/oci_connection.rb', line 249

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



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

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

#plsql_to_ruby_data_type(metadata) ⇒ Object



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
170
171
172
173
# File 'lib/plsql/oci_connection.rb', line 145

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"
    # 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



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

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

#rollbackObject



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

def rollback
  raw_connection.rollback
end

#ruby_value_to_ora_value(value, type = nil) ⇒ Object



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
244
245
246
247
# File 'lib/plsql/oci_connection.rb', line 175

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