Sha256: e4ed06ba0939a9d95cbbd98dbc1331573365aaeb5f7f241ea2f026ff5e0d01cc

Contents?: true

Size: 1.72 KB

Versions: 18

Compression:

Stored size: 1.72 KB

Contents

require 'mysql2'
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 = [: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

    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

18 entries across 18 versions & 1 rubygems

Version Path
flydata-0.5.5 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.5.4 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.5.3 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.5.2 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.5.1 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.5.0 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.4.3 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.4.2 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.4.1 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.4.0 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.3.24 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.3.23 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.3.22 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.3.21 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.3.20 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.3.19 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.3.18 lib/flydata/fluent-plugins/mysql/table_meta.rb
flydata-0.3.17 lib/flydata/fluent-plugins/mysql/table_meta.rb