Class: Dumbo::Cast

Inherits:
PgObject show all
Defined in:
lib/dumbo/cast.rb

Instance Attribute Summary collapse

Attributes inherited from PgObject

#oid

Instance Method Summary collapse

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

#argument_typeObject

Returns the value of attribute argument_type.



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

def argument_type
  @argument_type
end

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#function_nameObject

Returns the value of attribute function_name.



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

def function_name
  @function_name
end

#source_typeObject

Returns the value of attribute source_type.



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

def source_type
  @source_type
end

#target_typeObject

Returns the value of attribute target_type.



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

def target_type
  @target_type
end

Instance Method Details

#dropObject



33
34
35
# File 'lib/dumbo/cast.rb', line 33

def drop
  "DROP CAST (#{source_type} AS #{target_type});"
end

#load_attributesObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dumbo/cast.rb', line 6

def load_attributes
  result = execute <<-SQL
  SELECT
    format_type(st.oid,NULL) AS source_type,
    format_type(st.oid,NULL) AS argument_type,
    format_type(tt.oid,tt.typtypmod) AS target_type,
    proname AS function_name,
  CASE WHEN ca.castcontext = 'e' THEN NULL
        WHEN ca.castcontext = 'a' THEN 'ASSIGNMENT'
        ELSE 'IMPLICIT'
   END AS context

   FROM pg_cast ca
   JOIN pg_type st ON st.oid=castsource
   JOIN pg_type tt ON tt.oid=casttarget
   LEFT JOIN pg_proc pr ON pr.oid=castfunc
   LEFT OUTER JOIN pg_description des ON (des.objoid=ca.oid AND des.objsubid=0 AND des.classoid='pg_cast'::regclass)
  WHERE ca.oid = #{oid}
  SQL

  result.first.each do |k, v|
    send("#{k}=", v) rescue nil
  end

  result.first
end

#to_sqlObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/dumbo/cast.rb', line 37

def to_sql
  attributes = []
  attributes << "WITH FUNCTION #{function_name}(#{source_type})" if function_name
  attributes << 'WITHOUT FUNCTION' unless function_name
  attributes << context if context

  <<-SQL.gsub(/^ {6}/, '')
  CREATE CAST (#{source_type} AS #{target_type})
  #{attributes.join("\nAS ")};
  SQL
end