Class: Samidare::EmbulkUtility::MySQLClient

Inherits:
Object
  • Object
show all
Defined in:
lib/samidare/embulk_utility.rb

Constant Summary collapse

COLUMN_INFO_SQL =
<<-SQL
  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(db_info) ⇒ MySQLClient

Returns a new instance of MySQLClient.



55
56
57
# File 'lib/samidare/embulk_utility.rb', line 55

def initialize(db_info)
  @db_info = db_info
end

Instance Method Details

#clientObject



59
60
61
62
63
64
65
# File 'lib/samidare/embulk_utility.rb', line 59

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

#column_infos(table_name) ⇒ Object



72
73
74
75
# File 'lib/samidare/embulk_utility.rb', line 72

def column_infos(table_name)
  rows = client.xquery(COLUMN_INFO_SQL, @db_info['database'], table_name)
  rows.map { |row| ColumnInfo.new(row['column_name'], row['data_type']) }
end

#generate_bq_schema(table_name) ⇒ Object



67
68
69
70
# File 'lib/samidare/embulk_utility.rb', line 67

def generate_bq_schema(table_name)
  infos = column_infos(table_name)
  BigQueryUtility.generate_schema(infos)
end