Class: Naginegi::MySQL::MySQLClient

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

Constant Summary collapse

COLUMN_SQL =
<<-SQL.freeze
  SELECT column_name, data_type
  FROM INFORMATION_SCHEMA.COLUMNS
  WHERE table_schema = ?
  AND table_name = ?
  ORDER BY ordinal_position
SQL

Instance Method Summary collapse

Constructor Details

#initialize(database_config) ⇒ MySQLClient

Returns a new instance of MySQLClient.



18
19
20
# File 'lib/naginegi/mysql.rb', line 18

def initialize(database_config)
  @database_config = database_config
end

Instance Method Details

#clientObject



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

def client
  @client ||= Mysql2::Client.new(
    host: @database_config['host'],
    username: @database_config['username'],
    password: @database_config['password'],
    database: @database_config['database']
  )
end

#columns(table_name) ⇒ Object



36
37
38
39
# File 'lib/naginegi/mysql.rb', line 36

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

#generate_bq_schema(table_name) ⇒ Object



31
32
33
34
# File 'lib/naginegi/mysql.rb', line 31

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