Class: Mysql::TableMeta

Inherits:
Object
  • Object
show all
Defined in:
lib/flydata/fluent-plugins/mysql/table_meta.rb

Constant Summary collapse

MANDATORY_OPTS =
[
  :host, :port, :username, :password,
  :database, :tables,
]
OPTIONAL_OPTS =
[
  :ssl_ca
]
GET_TABLE_META_SQL =
"SELECT\n  T.table_name as table_name,\n  CCSA.character_set_name as character_set_name\nFROM\n  information_schema.`TABLES` T,\n  information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA\nWHERE\n  CCSA.collation_name = T.table_collation\n  AND T.table_schema = '%{database}'\n  AND T.table_name in (%{tables});\n"
CHARSET_ENCODE_RULE =

Charset naming conversion rule. mysql => ruby

mysql http://dev.mysql.com/doc/refman/5.6/en/charset-charsets.html mysql(supported CJK character sets) http://dev.mysql.com/doc/refman/5.6/en/faqs-cjk.html#qandaitem-A-11-1-1 For ruby, you can see encoding list with "Encoding.list"

{
  'ascii' => nil, # 'ASCII-8BIT', (not need to be encoded)
  'utf8' => nil, # 'UTF-8', (not need to be encoded)
  'utf8mb4' => nil, # 'UTF-8', (not need to be encoded)
  'utf16' => 'UTF-16',
  'utf32' => 'UTF-32',
  'latin1' => 'ISO-8859-1',
  'latin2' => 'ISO-8859-2',
  'big5' => 'Big5',
  'cp932' => 'CP932',
  'eucjpms' => 'eucJP-ms',
  'euckr' => 'EUC-KR',
  'gb18030' => 'GB18030',
  'gb2312' => 'GB2312',
  'gbk' => 'GBK',
  'sjis' => 'Shift_JIS',
  'ujis' => 'EUC-JP',
}

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ TableMeta

Returns a new instance of TableMeta.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/flydata/fluent-plugins/mysql/table_meta.rb', line 26

def initialize(opts)
  missing_opts = MANDATORY_OPTS - opts.keys
  raise "Mandatory option(s) are missing: #{missing_opts.join(', ')}" unless (missing_opts.empty?)

  @db_opts = [:host, :port, :username, :password, :database, :ssl_ca].inject({}) {|h, sym| h[sym] = opts[sym]; h}
  @db_opts[:sslca] = @db_opts[:ssl_ca] # for mysql2 gem

  @database = opts[:database]
  @tables = opts[:tables]
  @table_meta = Hash.new{|h, k| h[k] = {}}
end

Instance Method Details

#[](table_name) ⇒ Object

Return table meta :character_set_name



85
86
87
# File 'lib/flydata/fluent-plugins/mysql/table_meta.rb', line 85

def [](table_name)
  @table_meta[table_name.to_sym]
end

#get_ruby_charset_name(mysql_charset) ⇒ Object



76
77
78
79
80
81
# File 'lib/flydata/fluent-plugins/mysql/table_meta.rb', line 76

def get_ruby_charset_name(mysql_charset)
  return nil if mysql_charset.to_s.empty?
  raise "Unsupported charset:#{mysql_charset}." unless CHARSET_ENCODE_RULE.has_key?(mysql_charset)
  charset = CHARSET_ENCODE_RULE[mysql_charset]
  return charset
end

#updateObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/flydata/fluent-plugins/mysql/table_meta.rb', line 38

def update
  conn = Mysql2::Client.new(@db_opts)
  sql = GET_TABLE_META_SQL % {
    database: @database, tables: @tables.collect{|t| "'#{t}'"}.join(',') }
  columns = conn.query(sql)
  columns.collect do |col|
    @table_meta[col['table_name'].to_sym][:encoding] = get_ruby_charset_name(col['character_set_name'])
  end
ensure
  conn.close rescue nil if conn
end