class Mysqlknife::Mysql

Public Class Methods

new() click to toggle source
# File lib/mysqlknife/mysql.rb, line 5
def initialize
  @config = Configs.instance
end

Public Instance Methods

check() click to toggle source
# File lib/mysqlknife/mysql.rb, line 14
def check
  command?('mysql')
  command?('mysqlshow')
end
command?(name) click to toggle source
# File lib/mysqlknife/mysql.rb, line 9
def command?(name)
  `which #{name}`
  $?.success?
end
console(sentence = nil) click to toggle source
# File lib/mysqlknife/mysql.rb, line 48
def console(sentence = nil)
  command = %W[mysql
               -h #{@config.host}
               -P #{@config.mysql[:port]}
               -u #{@config.mysql[:username]}
               #{password(@config.mysql[:password])}
               --prompt='#{prompt}[\\d]> '
               #{@config.mysql[:database]}
               #{execute(sentence)}
               2>/dev/null].join(' ')
  purge(command)
end
describe(*args) click to toggle source
# File lib/mysqlknife/mysql.rb, line 61
def describe(*args)
  command = %W[mysqlshow
               -h #{@config.host}
               -P #{@config.mysql[:port]}
               -u #{@config.mysql[:username]}
               #{password(@config.mysql[:password])}
               --keys
               #{args.join(' ')}
               2>/dev/null].join(' ')
  purge(command)
end
execute(sentence) click to toggle source
# File lib/mysqlknife/mysql.rb, line 40
def execute(sentence)
  "--execute='#{sentence}'" unless sentence.nil? || sentence.empty?
end
parse(command) click to toggle source
# File lib/mysqlknife/mysql.rb, line 23
def parse(command)
  host     = @config.host
  port     = @config.mysql[:port]
  username = @config.mysql[:username]
  password = password(@config.mysql[:password])
  command  = eval("\"#{command}\"")
  purge(command)
end
password(password) click to toggle source
# File lib/mysqlknife/mysql.rb, line 44
def password(password)
  "-p#{password}" unless password.nil? || password.empty?
end
prompt() click to toggle source
# File lib/mysqlknife/mysql.rb, line 32
def prompt
  if !!(@config.host =~ Resolv::IPv4::Regex)
    @config.name
  else
    @config.host.partition('.').first
  end
end
purge(string) click to toggle source
# File lib/mysqlknife/mysql.rb, line 19
def purge(string)
  string.squeeze(' ').strip
end