Class: Samidare::EmbulkUtility::MySQLClient

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

Constant Summary collapse

COLUMN_INFO_SQL =
"  SELECT column_name, data_type\n  FROM INFORMATION_SCHEMA.COLUMNS\n  WHERE table_schema = ?\n  AND table_name = ?\n  ORDER BY ordinal_position\n"

Instance Method Summary collapse

Constructor Details

#initialize(db_info) ⇒ MySQLClient

Returns a new instance of MySQLClient.



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

def initialize(db_info)
  @db_info = db_info
end

Instance Method Details

#clientObject



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

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



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

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



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

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