Class: PgFuncall::TypeInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_funcall/type_info.rb

Constant Summary collapse

CATEGORY_MAP =
{'A' => 'array',
 'B' => 'boolean',
 'C' => 'composite',
 'D' => 'datetime',
 'E' => 'enum',
 'G' => 'geometric',
 'I' => 'network_address',
 'N' => 'numeric',
 'P' => 'pseudotype',
 'S' => 'string',
 'T' => 'timespan',
 'U' => 'user_defined',
 'V' => 'bit_string',
 'X' => 'unknown'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row, ar_type = nil) ⇒ TypeInfo

Returns a new instance of TypeInfo.



7
8
9
10
11
12
13
14
15
16
# File 'lib/pg_funcall/type_info.rb', line 7

def initialize(row, ar_type = nil)
  @row = {}
  # copy and convert int-looking things to int along the way
  row.each do |key, val|
    @row[key] =
        (val && val.respond_to?(:match) && val.match(/^-?\d+$/)) ? val.to_i : val
  end
  @row.freeze
  @ar_type = ar_type
end

Instance Attribute Details

#ar_typeObject

Returns the value of attribute ar_type.



3
4
5
# File 'lib/pg_funcall/type_info.rb', line 3

def ar_type
  @ar_type
end

#array_typeObject

Returns the value of attribute array_type.



4
5
6
# File 'lib/pg_funcall/type_info.rb', line 4

def array_type
  @array_type
end

#element_typeObject

Returns the value of attribute element_type.



5
6
7
# File 'lib/pg_funcall/type_info.rb', line 5

def element_type
  @element_type
end

Instance Method Details

#[](element) ⇒ Object



147
148
149
# File 'lib/pg_funcall/type_info.rb', line 147

def [](element)
  @row[element.to_s]
end

#_format_param_for_descriptor(param, type = nil) ⇒ Object

Represent a Ruby object in a string form to be passed as a parameter within a descriptor hash, rather than substituted into a string-form query.



30
31
32
33
34
35
36
37
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
# File 'lib/pg_funcall/type_info.rb', line 30

def _format_param_for_descriptor(param, type=nil)
  return param.value if param.is_a?(PgFuncall::Literal)

  case param
    when PgFuncall::TypedArray
      _format_param_for_descriptor(param.value, param.type + "[]")
    when PgFuncall::Typed
      _format_param_for_descriptor(param.value, param.type)
    when PgFuncall::PGTyped
      param.respond_to?(:__pg_value) ?
          param.__pg_value :
          _format_param_for_descriptor(param, type)
    when TrueClass
      'true'
    when FalseClass
      'false'
    when String
      if type == 'bytea' || param.encoding == Encoding::BINARY
        '\x' + param.unpack('C*').map {|x| sprintf("%02X", x)}.join("")
      else
        param
      end
    when Array
      "{" + param.map {|p| _format_param_for_descriptor(p)}.join(",") + "}"
    when IPAddr
      param.to_cidr_string
    when Range
      last_char = param.exclude_end? ? ')' : ']'
      case type
        when 'tsrange', 'tstzrange'
          "[#{param.first.utc},#{param.last.utc}#{last_char}"
        else
          "[#{param.first},#{param.last}#{last_char}"
      end
    when Set
      _format_param_for_descriptor(param.to_a)
    when Hash
      param.map do |k,v|
        "#{k} => #{v}"
      end.join(',')
    else
      ActiveRecord::Base.connection.quote(param)
  end
end

#array_type_oidObject



143
144
145
# File 'lib/pg_funcall/type_info.rb', line 143

def array_type_oid
  @row['typarray']
end

#cast_from_database(value) ⇒ Object

TODO: replace this to not use ar_type



19
20
21
22
23
# File 'lib/pg_funcall/type_info.rb', line 19

def cast_from_database(value)
  @ar_type.respond_to?(:type_cast_from_database) ?
      @ar_type.type_cast_from_database(value) :
      @ar_type.type_cast(value)
end

#cast_to_database(value) ⇒ Object

TODO: replace this to not use ar_type



76
77
78
79
80
# File 'lib/pg_funcall/type_info.rb', line 76

def cast_to_database(value)
  @ar_type.respond_to?(:type_cast_for_database) ?
      @ar_type.type_cast_for_database(value).to_s :
      _format_param_for_descriptor(value, name)
end

#categoryObject



103
104
105
# File 'lib/pg_funcall/type_info.rb', line 103

def category
  @row['typcategory']
end

#category_nameObject



134
135
136
# File 'lib/pg_funcall/type_info.rb', line 134

def category_name
  CATEGORY_MAP[category]
end

#element_type_oidObject



138
139
140
141
# File 'lib/pg_funcall/type_info.rb', line 138

def element_type_oid
  raise "Can only call on array" unless array?
  @row['typelem']
end

#fqnameObject

Don’t fully qualify base types – this is pretty, but is it wise?



93
94
95
96
97
# File 'lib/pg_funcall/type_info.rb', line 93

def fqname
  namespace == 'pg_catalog' ?
      name :
      namespace + '.' + name
end

#nameObject



82
83
84
# File 'lib/pg_funcall/type_info.rb', line 82

def name
  @row['typname']
end

#namespaceObject



86
87
88
# File 'lib/pg_funcall/type_info.rb', line 86

def namespace
  @row['nspname']
end

#oidObject



99
100
101
# File 'lib/pg_funcall/type_info.rb', line 99

def oid
  @row['oid']
end

#temporal?Boolean

Returns:



107
108
109
# File 'lib/pg_funcall/type_info.rb', line 107

def temporal?
  datetime? || timespan?
end