Class: ViewData::PG::PrimaryKey::GetColumns

Inherits:
Object
  • Object
show all
Defined in:
lib/view_data/pg/primary_key/get_columns.rb

Defined Under Namespace

Modules: Telemetry

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(session: nil) ⇒ Object



10
11
12
13
14
15
# File 'lib/view_data/pg/primary_key/get_columns.rb', line 10

def self.build(session: nil)
  instance = new
  Session.configure(instance, session: session)
  ::Telemetry.configure(instance)
  instance
end

.call(table) ⇒ Object



17
18
19
20
# File 'lib/view_data/pg/primary_key/get_columns.rb', line 17

def self.call(table)
  instance = build()
  instance.(table)
end

.register_telemetry_sink(instance) ⇒ Object



53
54
55
56
57
# File 'lib/view_data/pg/primary_key/get_columns.rb', line 53

def self.register_telemetry_sink(instance)
  sink = Telemetry::Sink.new
  instance.telemetry.register(sink)
  sink
end

Instance Method Details

#cacheObject



47
48
49
50
51
# File 'lib/view_data/pg/primary_key/get_columns.rb', line 47

def cache
  @cache ||= Hash.new do |hash, table|
    hash[table] = query(table)
  end
end

#call(table) ⇒ Object



22
23
24
# File 'lib/view_data/pg/primary_key/get_columns.rb', line 22

def call(table)
  cache[table]
end

#query(table) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/view_data/pg/primary_key/get_columns.rb', line 26

def query(table)
  result = session.execute("    SELECT\n      TEXT(attr.attname) AS column_name\n    FROM pg_index AS index\n    JOIN pg_attribute AS attr\n      ON attr.attrelid = index.indrelid\n      AND attr.attnum = ANY(index.indkey)\n    WHERE index.indrelid = $1::regclass\n    AND index.indisprimary\n  SQL\n\n  columns = result.to_a.map do |hash|\n    hash['column_name']\n  end\n\n  telemetry.record(:columns_queried, Telemetry::Data.new(table, columns))\n\n  columns\nend\n", [table])