Class: Arc::DataStores::ObjectDefinitions::PostgresTable

Inherits:
Table
  • Object
show all
Defined in:
lib/arc/data_stores/postgres/object_definitions.rb

Instance Attribute Summary

Attributes inherited from Table

#name

Instance Method Summary collapse

Methods inherited from Table

#initialize

Constructor Details

This class inherits a constructor from Arc::DataStores::ObjectDefinitions::Table

Instance Method Details

#fetch_item(name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/arc/data_stores/postgres/object_definitions.rb', line 47

def fetch_item name
  return {} unless keys.include? name.to_sym
  c = raw_column_data.find{|c| c[:name] == name.to_s}
  PostgresColumn.new @data_store,{
    name: c[:name],
    allows_null: c[:not_null] == "f",
    default: nil,
    primary_key: c[:primary_key] == "t",
    type: c[:type]
  }
end

#fetch_keysObject



44
45
46
# File 'lib/arc/data_stores/postgres/object_definitions.rb', line 44

def fetch_keys
  raw_column_data.map {|c| c[:name].to_sym }
end

#raw_column_dataObject



18
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
# File 'lib/arc/data_stores/postgres/object_definitions.rb', line 18

def raw_column_data
  column_info_query = <<-SQL
    SELECT  col.attrelid::regclass as table,
            col.attname as name,
            format_type(col.atttypid, col.atttypmod) as type,
            col.attnotnull as not_null,
            def.adsrc as default,
            ( SELECT d.refobjid
              FROM pg_depend d
              WHERE col.attrelid = d.refobjid
              AND col.attnum = d.refobjsubid
              LIMIT 1
            ) IS NOT NULL as primary_key
    FROM pg_attribute as col
    LEFT JOIN pg_attrdef def
      ON col.attrelid = def.adrelid
      AND col.attnum = def.adnum
    JOIN pg_tables tbl
      ON col.attrelid::regclass = tbl.tablename::regclass
    WHERE tbl.schemaname = ANY (current_schemas(false))
      AND col.attnum > 0
      AND tbl.tablename = '#{name}'
    ORDER BY primary_key DESC
  SQL
  @data_store.read(column_info_query).to_a.symbolize_keys!
end