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.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/oci8/object.rb', line 132

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

instance method



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

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



128
129
130
# File 'lib/oci8/object.rb', line 128

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

.get_class_by_typename(name) ⇒ Object



111
112
113
# File 'lib/oci8/object.rb', line 111

def self.get_class_by_typename(name)
  @@name_to_class[name]
end

.inherited(klass) ⇒ Object



105
106
107
108
109
# File 'lib/oci8/object.rb', line 105

def self.inherited(klass)
  name = klass.to_s.gsub(/^.*::/, '').gsub(/([a-z\d])([A-Z])/,'\1_\2').upcase
  @@class_to_name[klass] = name
  @@name_to_class[name] = klass
end

.method_missing(method_id, *args) ⇒ Object

class method



161
162
163
164
165
166
167
168
169
170
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
# File 'lib/oci8/object.rb', line 161

def self.method_missing(method_id, *args)
  if args[0].is_a? OCI8
    con = args.shift
  else
    con = @@default_connection
  end
  tdo = con.get_tdo_by_class(self)
  return_type = tdo.class_methods[method_id]
  if return_type == :none
    # procedure
    bind_arg_helper = BindArgumentHelper.new(*args)
    sql = <<EOS
BEGIN
  #{tdo.typename}.#{method_id}(#{bind_arg_helper.arg_str});
END;
EOS
    csr = con.parse(sql)
    begin
      bind_arg_helper.exec(con, csr)
    ensure
      csr.close
    end
    return nil
  elsif return_type
    # function
    return_type = tdo.class_methods[method_id]
    bind_arg_helper = BindArgumentHelper.new(*args)
    sql = <<EOS
BEGIN
  :rv := #{tdo.typename}.#{method_id}(#{bind_arg_helper.arg_str});
END;
EOS
    csr = con.parse(sql)
    begin
      csr.bind_param(:rv, nil, return_type)
      bind_arg_helper.exec(con, csr)
      rv = csr[:rv]
    ensure
      csr.close
    end
    return rv
  end
  super # The method is not found.
end

.set_typename(name) ⇒ Object



119
120
121
122
123
124
125
126
# File 'lib/oci8/object.rb', line 119

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



115
116
117
# File 'lib/oci8/object.rb', line 115

def self.typename
  @@class_to_name[self]
end