Class: Flounder::Result::Descriptor

Inherits:
Object
  • Object
show all
Includes:
PostgresUtils
Defined in:
lib/flounder/result/descriptor.rb

Constant Summary

Constants included from PostgresUtils

PostgresUtils::OID_BOOLEAN, PostgresUtils::OID_DATE, PostgresUtils::OID_FLOAT, PostgresUtils::OID_INTEGER, PostgresUtils::OID_SMALLINT, PostgresUtils::OID_TEXT, PostgresUtils::OID_TIME, PostgresUtils::OID_TIMESTAMP, PostgresUtils::OID_TIMESTAMPTZ, PostgresUtils::OID_VARCHAR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PostgresUtils

#access_value, #each_field, #oid_hstore, #type_name, #type_oid_to_sym, #typecast

Constructor Details

#initialize(connection, entity, pg_result, &name_resolver) ⇒ Descriptor

Returns a new instance of Descriptor.



21
22
23
24
25
26
27
28
# File 'lib/flounder/result/descriptor.rb', line 21

def initialize connection, entity, pg_result, &name_resolver
  @entity = entity
  @pg_result = pg_result
  @connection = connection
  @name_resolver = name_resolver || -> (name) {}

  build_accessors
end

Instance Attribute Details

#accessor_rootObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Root of the accessor tree.



16
17
18
# File 'lib/flounder/result/descriptor.rb', line 16

def accessor_root
  @accessor_root
end

#connectionObject (readonly)

Returns the value of attribute connection.



9
10
11
# File 'lib/flounder/result/descriptor.rb', line 9

def connection
  @connection
end

#entityObject (readonly)

Entity to use for field resolution



12
13
14
# File 'lib/flounder/result/descriptor.rb', line 12

def entity
  @entity
end

#name_resolverObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



19
20
21
# File 'lib/flounder/result/descriptor.rb', line 19

def name_resolver
  @name_resolver
end

#pg_resultObject (readonly)

Result obtained from the connection



7
8
9
# File 'lib/flounder/result/descriptor.rb', line 7

def pg_result
  @pg_result
end

Instance Method Details

#build_accessorsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses and builds accessor structure for the result stored here.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/flounder/result/descriptor.rb', line 42

def build_accessors
  @accessor_root = Accessor::Node.new
  each_field(entity, pg_result) do |idx, entity, field, type, binary|
    processed_entity, processed_name = name_resolver.call(field)
    entity = processed_entity if processed_entity
    field  = processed_name if processed_name

    # JOIN tables are available from the result using their singular names.
    if entity
      accessor_root[entity.singular].add_field(field, self, idx, type)
    end

    # The main entity and custom fields (AS something) are available on the
    # top-level of the result. 
    if field && (!entity || entity == self.entity)
      raise ::Flounder::DuplicateField, 
        "#{field.inspect} already defined in result set, aliasing occurs." \
        if accessor_root.has_obj? field

      accessor_root.add_field(field, self, idx, type)
    end

  end
end

#row(idx) ⇒ Object



30
31
32
# File 'lib/flounder/result/descriptor.rb', line 30

def row idx
  Row.new(accessor_root, idx)
end

#value(type, row_idx, col_idx) ⇒ Object



34
35
36
# File 'lib/flounder/result/descriptor.rb', line 34

def value type, row_idx, col_idx
  access_value(connection, pg_result, type, row_idx, col_idx)
end