Module: Flounder::PostgresUtils

Included in:
Connection, Result::Descriptor
Defined in:
lib/flounder/postgres_utils.rb

Constant Summary collapse

OID_BOOLEAN =
16
OID_SMALLINT =
21
OID_INTEGER =
23
OID_TEXT =
25
OID_FLOAT =
701
OID_VARCHAR =
1043
OID_TIMESTAMP =
1114
OID_DATE =
1082
OID_TIME =
1083
OID_TIMESTAMPTZ =
1184

Instance Method Summary collapse

Instance Method Details

#access_value(connection, result, type_oid, row_idx, col_idx) ⇒ Object



116
117
118
119
# File 'lib/flounder/postgres_utils.rb', line 116

def access_value connection, result, type_oid, row_idx, col_idx
  value = result.getvalue(row_idx, col_idx)
  typecast(connection, type_oid, value)
end

#each_field(entity, result) {|idx, entity, field_name, type, binary| ... } ⇒ Object

Yields header information for each column in the given result in turn.

Yield Parameters:

  • idx (Integer)

    column index

  • entity (Flounder::Entity)

    entity as resolved by table OID

  • field_name (String)

    field name as present in the SQL result

  • type (Integer)

    type OID

  • binary (Boolean)

    is this a binary field?



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/flounder/postgres_utils.rb', line 100

def each_field entity, result
  domain = entity.domain

  result.nfields.times do |field_idx|
    table_oid = result.ftable(field_idx)
    entity = domain.by_oid(table_oid)

    yield(
      field_idx, 
      entity,
      result.fname(field_idx),
      result.ftype(field_idx), 
      result.fformat(field_idx) == 1)
  end
end

#oid_hstore(connection) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/flounder/postgres_utils.rb', line 52

def oid_hstore connection
  unless @oid_hstore_initialized
    @oid_hstore_initialized = true

    @oid_hstore = begin 
      result = connection.exec("SELECT oid FROM pg_type WHERE typname='hstore'")
      row = result.first

      row && row.values.first.to_i
    end
  end

  @oid_hstore
end

#type_name(ftype, fmod) ⇒ Object

Helper function for debugging



122
123
124
125
126
127
# File 'lib/flounder/postgres_utils.rb', line 122

def type_name ftype, fmod
  pg.
    exec( "SELECT format_type($1,$2)", 
      [ftype, fmod] ).
    getvalue( 0, 0 )
end

#type_oid_to_sym(connection, oid) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/flounder/postgres_utils.rb', line 67

def type_oid_to_sym connection, oid
  raise ArgumentError, "AF: oid must not be nil" unless oid

  return :hash if oid==oid_hstore(connection)

  case oid
    when OID_TIMESTAMP, OID_TIMESTAMPTZ
      :datetime
    when OID_DATE
      :date
    when OID_TIME
      :time
    when OID_INTEGER, OID_SMALLINT
      :integer
    when OID_FLOAT
      :float
    when OID_VARCHAR, OID_TEXT
      :string
    when OID_BOOLEAN
      :boolean
  else
    nil
  end
end

#typecast(connection, type_oid, value) ⇒ Object

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/flounder/postgres_utils.rb', line 19

def typecast connection, type_oid, value
  raise ArgumentError, "AF: type_oid must not be nil" unless type_oid

  return nil unless value

  # hstore extension
  if type_oid == oid_hstore(connection)
    return PgHstore.load(value) 
  end

  # assert: value is not nil
  case type_oid
    when OID_TIMESTAMPTZ
      DateTime.parse(value)
    when OID_TIMESTAMP
      DateTime.parse(value)
    when OID_DATE
      Date.parse(value)
    when OID_TIME
      Time.parse(value)
    when OID_INTEGER, OID_SMALLINT
      value.to_i
    when OID_FLOAT
      value.to_f
    when OID_BOOLEAN
      value == 't'
    when OID_TEXT
      value.to_s      
  else
    value
  end
end