Class: Dumbo::CompositeType

Inherits:
Type show all
Defined in:
lib/dumbo/composite_type.rb

Instance Attribute Summary collapse

Attributes inherited from Type

#name, #type, #typrelid

Attributes inherited from PgObject

#oid

Instance Method Summary collapse

Methods inherited from Type

#drop, #get

Methods inherited from PgObject

#downgrade, #execute, #get, identfied_by, #identify, #initialize, #upgrade

Constructor Details

This class inherits a constructor from Dumbo::PgObject

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'lib/dumbo/composite_type.rb', line 3

def attributes
  @attributes
end

Instance Method Details

#load_attributesObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dumbo/composite_type.rb', line 5

def load_attributes
  super
  res = execute <<-SQL
  SELECT
    attname,
    format_type(t.oid,NULL) AS typname
  FROM pg_attribute att
  JOIN pg_type t ON t.oid=atttypid

  WHERE att.attrelid=#{typrelid}
  ORDER by attnum
  SQL

  attribute = Struct.new(:name, :type)
  @attributes = res.map { |r| attribute.new(r['attname'], r['typname']) }
end

#to_sqlObject



22
23
24
25
26
27
28
29
# File 'lib/dumbo/composite_type.rb', line 22

def to_sql
  attr_str = attributes.map { |a| "#{a.name} #{a.type}" }.join(",\n  ")
  <<-SQL.gsub(/^ {6}/, '')
  CREATE TYPE #{name} AS (
    #{attr_str}
  );
  SQL
end