Class: PLSQL::Type

Inherits:
Object
  • Object
show all
Extended by:
TypeClassMethods
Defined in:
lib/plsql/type.rb

Defined Under Namespace

Classes: TypeProcedure

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TypeClassMethods

find

Constructor Details

#initialize(schema, type, override_schema_name = nil) ⇒ Type

:nodoc:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/plsql/type.rb', line 38

def initialize(schema, type, override_schema_name = nil) #:nodoc:
  @schema = schema
  @schema_name = override_schema_name || schema.schema_name
  @type_name = type.to_s.upcase
  @attributes = {}
  @type_procedures = {}

  @typecode, @type_object_id = @schema.select_first(
    "SELECT t.typecode, o.object_id FROM all_types t, all_objects o
    WHERE t.owner = :owner
    AND t.type_name = :type_name
    AND o.owner = t.owner
    AND o.object_name = t.type_name
    AND o.object_type = 'TYPE'",
    @schema_name, @type_name)

  @schema.select_all(
    "SELECT attr_name, attr_no,
          attr_type_name, length, precision, scale,
          attr_type_owner, attr_type_mod,
          (SELECT t.typecode FROM all_types t
          WHERE t.owner = attr_type_owner
          AND t.type_name = attr_type_name) typecode
    FROM all_type_attrs
    WHERE owner = :owner
    AND type_name = :type_name
    ORDER BY attr_no",
    @schema_name, @type_name
  ) do |r|
    attr_name, position,
          data_type, data_length, data_precision, data_scale,
          data_type_owner, _, typecode = r
    @attributes[attr_name.downcase.to_sym] = {
      position: position && position.to_i,
      data_type: data_type_owner && (typecode == "COLLECTION" ? "TABLE" : "OBJECT") || data_type,
      data_length: data_type_owner ? nil : data_length && data_length.to_i,
      data_precision: data_precision && data_precision.to_i,
      data_scale: data_scale && data_scale.to_i,
      type_owner: data_type_owner,
      type_name: data_type_owner && data_type,
      sql_type_name: data_type_owner && "#{data_type_owner}.#{data_type}"
    }
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

:nodoc:



110
111
112
113
114
115
116
# File 'lib/plsql/type.rb', line 110

def method_missing(method, *args, &block) #:nodoc:
  if procedure = find_procedure(method)
    procedure.exec_with_options(args, {}, &block)
  else
    raise ArgumentError, "No PL/SQL procedure '#{method.to_s.upcase}' found for type '#{@type_name}'"
  end
end

Instance Attribute Details

#attributesObject (readonly)

:nodoc:



36
37
38
# File 'lib/plsql/type.rb', line 36

def attributes
  @attributes
end

#schema_nameObject (readonly)

:nodoc:



36
37
38
# File 'lib/plsql/type.rb', line 36

def schema_name
  @schema_name
end

#type_nameObject (readonly)

:nodoc:



36
37
38
# File 'lib/plsql/type.rb', line 36

def type_name
  @type_name
end

#type_object_idObject (readonly)

:nodoc:



36
37
38
# File 'lib/plsql/type.rb', line 36

def type_object_id
  @type_object_id
end

#typecodeObject (readonly)

:nodoc:



36
37
38
# File 'lib/plsql/type.rb', line 36

def typecode
  @typecode
end

Instance Method Details

#attribute_namesObject

list of object type attribute names



89
90
91
# File 'lib/plsql/type.rb', line 89

def attribute_names
  @attribute_names ||= @attributes.keys.sort_by { |k| @attributes[k][:position] }
end

#collection?Boolean

is type collection?

Returns:

  • (Boolean)


84
85
86
# File 'lib/plsql/type.rb', line 84

def collection?
  @is_collection ||= @typecode == "COLLECTION"
end

#find_procedure(new_or_procedure) ⇒ Object

:nodoc:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/plsql/type.rb', line 118

def find_procedure(new_or_procedure) #:nodoc:
  @type_procedures[new_or_procedure] ||= begin
    procedure_name = new_or_procedure == :new ? @type_name : new_or_procedure
    # find defined procedure for type
    if @schema.select_first(
      "SELECT procedure_name FROM all_procedures
      WHERE owner = :owner
        AND object_name = :object_name
        AND procedure_name = :procedure_name",
          @schema_name, @type_name, procedure_name.to_s.upcase)
      TypeProcedure.new(@schema, self, procedure_name)
    # call default constructor
    elsif new_or_procedure == :new
      TypeProcedure.new(@schema, self, :new)
    end
  end
end

#new(*args, &block) ⇒ Object

create new PL/SQL object instance



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/plsql/type.rb', line 94

def new(*args, &block)
  procedure = find_procedure(:new)
  # in case of collections pass array of elements as one argument for constructor
  if collection? && !(args.size == 1 && args[0].is_a?(Array))
    args = [args]
  end
  result = procedure.exec_with_options(args, { skip_self: true }, &block)
  # TODO: collection constructor should return Array of ObhjectInstance objects
  if collection?
    result
  else
    # TODO: what to do if block is passed to constructor?
    ObjectInstance.create(self, result)
  end
end