Class: Mysqlknife::Mysql

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

Instance Method Summary collapse

Constructor Details

#initializeMysql

Returns a new instance of Mysql.



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

def initialize
  @config = Configs.instance
end

Instance Method Details

#checkObject



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

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

#command?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#console(sentence = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# 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) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Object



40
41
42
# File 'lib/mysqlknife/mysql.rb', line 40

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

#parse(command) ⇒ Object



23
24
25
26
27
28
29
30
# 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) ⇒ Object



44
45
46
# File 'lib/mysqlknife/mysql.rb', line 44

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

#promptObject



32
33
34
35
36
37
38
# 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) ⇒ Object



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

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