Class: Flydata::SourceMysql::TableMeta

Inherits:
TableMeta
  • Object
show all
Defined in:
lib/flydata/source_mysql/table_meta.rb

Constant Summary collapse

MANDATORY_OPTS =
[
  :host, :port, :username, :password,
  :database, :tables,
]
OPTIONAL_OPTS =
[
  :ssl_ca
]
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

Instance Method Summary collapse

Methods inherited from TableMeta

#[]

Constructor Details

#initialize(opts) ⇒ TableMeta

Returns a new instance of TableMeta.


31
32
33
34
35
36
37
38
39
# File 'lib/flydata/source_mysql/table_meta.rb', line 31

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

  @db_opts = FlydataCore::Mysql::Config.build_mysql_db_opts(opts)
  @database = opts[:database]
  @tables = opts[:tables]
  @table_meta = Hash.new{|h, k| h[k] = {}}
end

Instance Method Details

#reloadObject


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/flydata/source_mysql/table_meta.rb', line 41

def reload
  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|
    mysql_charset = col['character_set_name']
    @table_meta[col['table_name'].to_sym][:encoding] = FlydataCore::TableDef::MysqlTableDef.ruby_encoding(mysql_charset)
    @table_meta[col['table_name'].to_sym][:mysql_charset] = mysql_charset
  end
ensure
  conn.close rescue nil if conn
end