Class: OCI8::Object::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/oci8/object.rb

Constant Summary collapse

@@class_to_name =
{}
@@name_to_class =
{}
@@default_connection =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Base

Returns a new instance of Base.


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
# File 'lib/oci8/object.rb', line 174

def initialize(*args)
  @attributes = {}
  if args[0].is_a? OCI8
    @con = args.shift
  else
    @con = @@default_connection
  end
  return if args.empty?
  raise "no connection is specified." if @con.nil?
  # setup a TDO object.
  tdo = @con.get_tdo_by_class(self.class)
  # call constructor.
  bind_arg_helper = BindArgumentHelper.new(*args)
  sql = <<EOS
BEGIN
  :self := #{tdo.typename}(#{bind_arg_helper.arg_str});
END;
EOS
  csr = @con.parse_internal(sql)
  begin
    csr.bind_param(:self, nil, :named_type_internal, tdo)
    bind_arg_helper.exec(@con, csr)
    @attributes = csr[:self].attributes
  ensure
    csr.close
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object


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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/oci8/object.rb', line 253

def method_missing(method_id, *args)
  if @attributes.is_a? Array
    return @attributes if method_id == :to_ary
    super
  end
  # getter func
  if @attributes.has_key?(method_id)
    if args.length != 0
      raise ArgumentError, "wrong number of arguments (#{args.length} for 0)"
    end
    return @attributes[method_id]
  end
  # setter func
  method_name = method_id.to_s
  if method_name[-1] == ?=
    attr = method_name[0...-1].intern
    if @attributes.has_key?(attr)
      if args.length != 1
        raise ArgumentError, "wrong number of arguments (#{args.length} for 1)"
      end
      return @attributes[attr] = args[0]
    end
  end

  super if @con.nil?

  tdo = @con.get_tdo_by_class(self.class)
  return_type = tdo.instance_methods[method_id]
  if return_type == :none
    # procedure
    bind_arg_helper = BindArgumentHelper.new(*args)
    sql = <<EOS
DECLARE
  val #{tdo.typename} := :self;
BEGIN
  val.#{method_id}(#{bind_arg_helper.arg_str});
  :self := val;
END;
EOS
    csr = @con.parse_internal(sql)
    begin
      csr.bind_param(:self, nil, :named_type_internal, tdo)
      csr[:self].attributes = self
      bind_arg_helper.exec(@con, csr)
      @attributes = csr[:self].attributes
    ensure
      csr.close
    end
    return nil
  elsif return_type
    # function
    bind_arg_helper = BindArgumentHelper.new(*args)
    sql = <<EOS
DECLARE
  val #{tdo.typename} := :self;
BEGIN
  :rv := val.#{method_id}(#{bind_arg_helper.arg_str});
  :self := val;
END;
EOS
    csr = @con.parse_internal(sql)
    begin
      csr.bind_param(:self, nil, :named_type_internal, tdo)
      csr.bind_param(:rv, nil, return_type)
      csr[:self].attributes = self
      bind_arg_helper.exec(@con, csr)
      @attributes = csr[:self].attributes
      rv = csr[:rv]
    ensure
      csr.close
    end
    return rv
  end
  # The method is not found.
  super
end

Class Method Details

.default_connection=(con) ⇒ Object

Deprecated.

170
171
172
# File 'lib/oci8/object.rb', line 170

def self.default_connection=(con)
  @@default_connection = con
end

.set_typename(name) ⇒ Object


160
161
162
163
164
165
166
167
# File 'lib/oci8/object.rb', line 160

def self.set_typename(name)
  # delete an old name-to-class mapping.
  @@name_to_class[@@class_to_name[self]] = nil
  # set a new name-to-class mapping.
  name = name.upcase
  @@class_to_name[self] = name
  @@name_to_class[name] = self
end

.typenameObject


156
157
158
# File 'lib/oci8/object.rb', line 156

def self.typename
  @@class_to_name[self]
end