class PgFuncall::TypeInfo

Constants

CATEGORY_MAP

Attributes

ar_type[RW]
array_type[RW]
element_type[RW]

Public Class Methods

new(row, ar_type = nil) click to toggle source
# 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

Public Instance Methods

[](element) click to toggle source
# File lib/pg_funcall/type_info.rb, line 147
def [](element)
  @row[element.to_s]
end
_format_param_for_descriptor(param, type=nil) click to toggle source

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.

# 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_oid() click to toggle source
# File lib/pg_funcall/type_info.rb, line 143
def array_type_oid
  @row['typarray']
end
cast_from_database(value) click to toggle source

TODO: replace this to not use ar_type

# 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) click to toggle source

TODO: replace this to not use ar_type

# 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
category() click to toggle source
# File lib/pg_funcall/type_info.rb, line 103
def category
  @row['typcategory']
end
category_name() click to toggle source
# File lib/pg_funcall/type_info.rb, line 134
def category_name
  CATEGORY_MAP[category]
end
element_type_oid() click to toggle source
# File lib/pg_funcall/type_info.rb, line 138
def element_type_oid
  raise "Can only call on array" unless array?
  @row['typelem']
end
fqname() click to toggle source

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

# File lib/pg_funcall/type_info.rb, line 93
def fqname
  namespace == 'pg_catalog' ?
      name :
      namespace + '.' + name
end
name() click to toggle source
# File lib/pg_funcall/type_info.rb, line 82
def name
  @row['typname']
end
namespace() click to toggle source
# File lib/pg_funcall/type_info.rb, line 86
def namespace
  @row['nspname']
end
oid() click to toggle source
# File lib/pg_funcall/type_info.rb, line 99
def oid
  @row['oid']
end
temporal?() click to toggle source
# File lib/pg_funcall/type_info.rb, line 107
def temporal?
  datetime? || timespan?
end