Class: Mysql::TableMeta

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

Constant Summary collapse

MANDATORY_OPTS =
[
  :mysql_url, :database, :tables
]
OPTIONAL_OPTS =
[
]
GET_TABLE_META_SQL =
<<EOT
SELECT
  T.table_name as table_name,
  CCSA.character_set_name as character_set_name
FROM
  information_schema.`TABLES` T,
  information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
WHERE
  CCSA.collation_name = T.table_collation
  AND T.table_schema = '%{database}'
  AND T.table_name in (%{tables});
EOT
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)
  '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.



24
25
26
27
28
29
30
31
# File 'lib/flydata/fluent-plugins/mysql/table_meta.rb', line 24

def initialize(opts)
  missing_opts = MANDATORY_OPTS - opts.keys
  raise "Mandatory option(s) are missing: #{missing_opts.join(', ')}" unless (missing_opts.empty?)
  @mysql_url = opts[:mysql_url]
  @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


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

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

#get_ruby_charset_name(mysql_charset) ⇒ Object



70
71
72
73
74
75
# File 'lib/flydata/fluent-plugins/mysql/table_meta.rb', line 70

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



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/flydata/fluent-plugins/mysql/table_meta.rb', line 33

def update
  conn = Mysql2::Client.new(@mysql_url)
  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