Class: Naginegi::PostgreSQL::PgClient

Inherits:
Object
  • Object
show all
Defined in:
lib/naginegi/postgresql.rb

Constant Summary collapse

COLUMN_SQL =
<<-SQL.freeze
  SELECT column_name, data_type
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE table_name = $1
  ORDER BY ordinal_position
SQL

Instance Method Summary collapse

Constructor Details

#initialize(db_config) ⇒ PgClient

Returns a new instance of PgClient.



17
18
19
# File 'lib/naginegi/postgresql.rb', line 17

def initialize(db_config)
  @db_config = db_config
end

Instance Method Details

#clientObject



21
22
23
24
25
26
27
28
# File 'lib/naginegi/postgresql.rb', line 21

def client
  @client ||= PG::Connection.new(
    host: @db_config['host'],
    user: @db_config['username'],
    password: @db_config['password'],
    dbname: @db_config['database']
  )
end

#columns(table_name) ⇒ Object



35
36
37
38
# File 'lib/naginegi/postgresql.rb', line 35

def columns(table_name)
  rows = client.exec_params(COLUMN_SQL, [table_name])
  rows.map { |row| Column.new(row['column_name'], row['data_type']) }
end

#generate_bq_schema(table_name) ⇒ Object



30
31
32
33
# File 'lib/naginegi/postgresql.rb', line 30

def generate_bq_schema(table_name)
  infos = columns(table_name)
  BigQuery.generate_schema(infos)
end