Sha256: 0a68c979c684a5d18dfe26851bd4857ec66b248b2bf74dbb99ccf103543b3f10

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

module Sip
  class DBBase
    attr_reader :args

    def self.make_interface(type, args, sipper)
      if type == 'mysql'
        require 'sip/databases/mysql'
        MySQLSipper.new args, sipper
      else
        raise UnsupportedDatabaseType, "DB type #{type.to_s} not supported."
      end
    end

    def initialize(args, sipper)
      @args = args
      @sipper = sipper
    end
    
    def tables
      query('SHOW tables')
    end

    def rowcount(table)
      query('SELECT count(1) FROM #{table}').first.first.to_i
    end

    def get_column_max(tablename, field)
      query("SELECT max(#{field}) FROM #{tablename}").first.first.to_i
    end

    def hive_columns(table)
      columns(table).map { |name, type|
        [name, convert_to_hive_type(type)]
      }
    end

    def columns(table)
      query("DESCRIBE #{table}").map { |col|
        col.slice(0,2)
      }
    end

    def order_column_list(table, cols)
      columns(table).map { |k,v| k }.select { |c| cols.include? c }
    end

    def generate_command(tableconf, first=nil, last=nil)
      select = "SELECT #{columns(tableconf['tablename']).map { |k,v| k }.join(',')} FROM #{tableconf['tablename']}"
      wheres = []
      wheres << "#{tableconf['incremental_index']} >= #{first}" if not first.nil?
      wheres << "#{tableconf['incremental_index']} <= #{last}" if not last.nil?
      select += " WHERE #{wheres.join(" AND ")}" if wheres.length > 0
      cmd_line_execute_string select
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sip-0.0.0 lib/sip/databases/dbbase.rb