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.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/oci8/object.rb', line 150

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



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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
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
# File 'lib/oci8/object.rb', line 229

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.


146
147
148
# File 'lib/oci8/object.rb', line 146

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

.set_typename(name) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/oci8/object.rb', line 136

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



132
133
134
# File 'lib/oci8/object.rb', line 132

def self.typename
  @@class_to_name[self]
end