Sha256: 5470e2470ffa4c6eaf37644c9d80625dde3cb1aff39605e89ae25ed140fae38b
Contents?: true
Size: 1.65 KB
Versions: 26
Compression:
Stored size: 1.65 KB
Contents
require 'mysql2' require 'flydata-core/mysql/config' require 'flydata-core/table_def/mysql_table_def' module Flydata module Mysql class TableMeta 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 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 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| 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 # Return table meta # :character_set_name def [](table_name) @table_meta[table_name.to_sym] end end end end
Version data entries
26 entries across 26 versions & 1 rubygems