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, 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



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

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



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

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

#autocommit?Boolean

Returns:

  • (Boolean)


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

def autocommit?
  raw_connection.autocommit?
end

#commitObject



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

def commit
  raw_connection.commit
end

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



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

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

#database_versionObject



292
293
294
295
# File 'lib/plsql/oci_connection.rb', line 292

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

#describe_synonym(schema_name, synonym_name) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
# File 'lib/plsql/oci_connection.rb', line 280

def describe_synonym(schema_name, synonym_name)
  if schema_name == 'PUBLIC'
    full_name = synonym_name.to_s
  else
    full_name = "#{schema_name}.#{synonym_name}"
  end
   = raw_connection.describe_synonym(full_name)
  [.schema_name, .name]
rescue OCIError
  nil
end

#exec(sql, *bindvars) ⇒ Object



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

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

#logoffObject



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

def logoff
  super
  raw_connection.logoff
end

#ora_value_to_ruby_value(value) ⇒ Object



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
278
# File 'lib/plsql/oci_connection.rb', line 250

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



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

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

#plsql_to_ruby_data_type(metadata) ⇒ Object



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

def plsql_to_ruby_data_type()
  data_type, data_length = [:data_type], [:data_length]
  case data_type
  when "VARCHAR2", "CHAR", "NVARCHAR2", "NCHAR"
    [String, data_length || 32767]
  when "CLOB", "NCLOB"
    [OCI8::CLOB, nil]
  when "BLOB"
    [OCI8::BLOB, nil]
  when "NUMBER", "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"
    # 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



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

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

#rollbackObject



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

def rollback
  raw_connection.rollback
end

#ruby_value_to_ora_value(value, type = nil) ⇒ Object



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

def ruby_value_to_ora_value(value, type=nil)
  type ||= value.class
  case type.to_s.to_sym
  when :Fixnum, :BigDecimal, :String
    value
  when :OraNumber
    # pass parameters as OraNumber to avoid rounding errors
    case value
    when Bignum
      OraNumber.new(value.to_s)
    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, attr_length = 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, attr_length = 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