Class: Mysqlknife::Mysql::Kill

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKill

Returns a new instance of Kill.



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

def initialize
  @mysql     = MySQL.new
  @mysql_sql = Mysql::SQL.new
end

Instance Attribute Details

#where=(value) ⇒ Object (writeonly)

Sets the attribute where

Parameters:

  • value

    the value to set the attribute where to.



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

def where=(value)
  @where = value
end

Instance Method Details

#check_grantsObject



28
29
30
31
32
33
# File 'lib/mysqlknife/mysql/kill.rb', line 28

def check_grants
  sql    = @mysql_sql.current_user_grants
  result = @mysql.execute(sql).first.values.to_s =~ /PROCESS/

  return true if result
end

#check_privilegesObject



35
36
37
# File 'lib/mysqlknife/mysql/kill.rb', line 35

def check_privileges
  check_user || check_grants
end

#check_userObject



21
22
23
24
25
26
# File 'lib/mysqlknife/mysql/kill.rb', line 21

def check_user
  sql    = @mysql_sql.current_user
  result = @mysql.execute(sql).first['CURRENT_USER'] =~ /^root\@/

  return true if result
end

#clearObject



13
14
15
16
17
18
19
# File 'lib/mysqlknife/mysql/kill.rb', line 13

def clear
  list.each do |process|
    kill(process['id'])

    Mysqlknife::Log.new.kill(process)
  end
end

#kill(id) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/mysqlknife/mysql/kill.rb', line 62

def kill(id)
  if check_privileges
    if rds?
      @mysql.execute(@mysql_sql.mysql_kill(id))
    else
      @mysql.execute(@mysql_sql.rds_kill(id))
    end
  end
end

#listObject



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

def list
  @mysql.execute(@mysql_sql.show_processlist(@where))
end

#rds?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/mysqlknife/mysql/kill.rb', line 58

def rds?
  ! @mysql.execute(@mysql_sql.show_procedure('rds_kill')).first
end

#showObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mysqlknife/mysql/kill.rb', line 43

def show
  process = list

  unless process.first.nil?
    table = Terminal::Table.new do |t|
      t.add_row(process.first.keys)
      t.add_separator
      process.each do |row|
        t.add_row(row.values)
      end
    end
    puts table
  end
end