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