Class: Mysqlknife::Mysql::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/mysqlknife/mysql/command.rb

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



6
7
8
# File 'lib/mysqlknife/mysql/command.rb', line 6

def initialize
  @cnf = Configs.instance
end

Instance Method Details

#checkObject



23
24
25
26
# File 'lib/mysqlknife/mysql/command.rb', line 23

def check
  command?('mysql')
  command?('mysqlshow')
end

#command?(name) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/mysqlknife/mysql/command.rb', line 18

def command?(name)
  `which #{name}`
  $?.success?
end

#connection(name) ⇒ Object



10
11
12
# File 'lib/mysqlknife/mysql/command.rb', line 10

def connection(name)
  @cnf.connection(name)
end

#console(sentence = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mysqlknife/mysql/command.rb', line 57

def console(sentence = nil)
  command = %W[mysql
               -h #{@cnf.mysql_host}
               -P #{@cnf.mysql_port}
               -u #{@cnf.mysql_username}
               #{password(@cnf.mysql_password)}
               --prompt='#{prompt}[\\d]> '
               #{@cnf.mysql_database}
               #{execute(sentence)}
               ].join(' ')
  purge(command)
end

#describe(*args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/mysqlknife/mysql/command.rb', line 70

def describe(*args)
  command = %W[mysqlshow
               -h #{@cnf.mysql_host}
               -P #{@cnf.mysql_port}
               -u #{@cnf.mysql_username}
               #{password(@cnf.mysql_password)}
               --keys
               #{args.join(' ')}].join(' ')
  purge(command)
end

#execute(sentence) ⇒ Object



49
50
51
# File 'lib/mysqlknife/mysql/command.rb', line 49

def execute(sentence)
  "--execute='#{sentence}'" unless sentence.nil? || sentence.empty?
end

#host(host) ⇒ Object



14
15
16
# File 'lib/mysqlknife/mysql/command.rb', line 14

def host(host)
  @cnf.mysql_host = host
end

#parse(command) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/mysqlknife/mysql/command.rb', line 32

def parse(command)
  host     = @cnf.mysql_host
  port     = @cnf.mysql_port
  username = @cnf.mysql_username
  password = password(@cnf.mysql_password)
  command  = eval("\"#{command}\"")
  purge(command)
end

#password(password) ⇒ Object



53
54
55
# File 'lib/mysqlknife/mysql/command.rb', line 53

def password(password)
  "-p#{password}" unless password.nil? || password.empty?
end

#promptObject



41
42
43
44
45
46
47
# File 'lib/mysqlknife/mysql/command.rb', line 41

def prompt
  if !!(@cnf.mysql_host =~ Resolv::IPv4::Regex)
    @cnf.name
  else
    @cnf.mysql_host.partition('.').first
  end
end

#purge(string) ⇒ Object



28
29
30
# File 'lib/mysqlknife/mysql/command.rb', line 28

def purge(string)
  string.squeeze(' ').strip
end