Class: Snapback::MySQL::ClientControl

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/snapback/mysql/client_control.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#hostnameObject

Returns the value of attribute hostname.



9
10
11
# File 'lib/snapback/mysql/client_control.rb', line 9

def hostname
  @hostname
end

#passwordObject

Returns the value of attribute password.



9
10
11
# File 'lib/snapback/mysql/client_control.rb', line 9

def password
  @password
end

#usernameObject

Returns the value of attribute username.



9
10
11
# File 'lib/snapback/mysql/client_control.rb', line 9

def username
  @username
end

Instance Method Details

#connectObject



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

def connect
  @connection = Mysql.new @hostname, @username, @password
end

#database_create(database) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/snapback/mysql/client_control.rb', line 62

def database_create(database)
  begin
    @connection.query "CREATE DATABASE `#{Mysql.escape_string database}`"
    true
  rescue
    false
  end
end

#database_exists?(database) ⇒ Boolean

Database

Returns:

  • (Boolean)


58
59
60
# File 'lib/snapback/mysql/client_control.rb', line 58

def database_exists?(database)
  @connection.list_dbs(database).size == 1
end

#database_select(database) ⇒ Object



71
72
73
# File 'lib/snapback/mysql/client_control.rb', line 71

def database_select(database)
  @connection.select_db(database)
end

#database_tablesObject



75
76
77
# File 'lib/snapback/mysql/client_control.rb', line 75

def database_tables
  @connection.query "SHOW TABLES"
end

#flush_tablesObject

Maintenance



25
26
27
28
# File 'lib/snapback/mysql/client_control.rb', line 25

def flush_tables
  @connection.query "FLUSH TABLES WITH READ LOCK"
  true
end

#get_data_directoryObject

Configuration



37
38
39
40
41
42
# File 'lib/snapback/mysql/client_control.rb', line 37

def get_data_directory
  rs = @connection.query "SHOW VARIABLES LIKE 'datadir'"
  rs.each_hash do |row|
    return row['Value'].gsub(/\/$/, '')
  end
end

#has_innodb_file_per_table?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
# File 'lib/snapback/mysql/client_control.rb', line 44

def has_innodb_file_per_table?
  rs = @connection.query "SHOW VARIABLES LIKE 'innodb_file_per_table'"
  rs.each_hash do |row|
    return row['Value'] == "ON"
  end
end

#set_innodb_file_per_table(on) ⇒ Object



51
52
53
54
# File 'lib/snapback/mysql/client_control.rb', line 51

def set_innodb_file_per_table(on)
  rs = @connection.prepare "SET GLOBAL innodb_file_per_table = ?"
  rs.execute(on ? 'ON' : 'OFF')
end

#table_drop(table) ⇒ Object

Tables



81
82
83
84
85
86
87
88
# File 'lib/snapback/mysql/client_control.rb', line 81

def table_drop(table)
  begin
    @connection.query "DROP TABLE `#{Mysql.escape_string table}`"
    true
  rescue
    false
  end
end

#table_optimize(table) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/snapback/mysql/client_control.rb', line 90

def table_optimize(table)
  begin
    @connection.query "ALTER TABLE `#{Mysql.escape_string table}` IMPORT TABLESPACE"
  rescue
    # If this fails, it's usually just because the table is not InnoDB, so ignore errors
  end

  begin
    @connection.query "OPTIMIZE TABLE `#{Mysql.escape_string table}`"
    true
  rescue
    false
  end
end

#table_set_engine(table, engine) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/snapback/mysql/client_control.rb', line 105

def table_set_engine(table, engine)
  begin
    @connection.query "ALTER TABLE `#{Mysql.escape_string table}` ENGINE = #{Mysql.escape_string engine};"
    true
  rescue
    false
  end
end

#tablespace_discard(table) ⇒ Object

Tablespace



115
116
117
118
119
120
121
122
# File 'lib/snapback/mysql/client_control.rb', line 115

def tablespace_discard(table)
  begin
    @connection.query "ALTER TABLE `#{Mysql.escape_string table}` DISCARD TABLESPACE"
    true
  rescue
    false
  end
end

#unlock_tablesObject



30
31
32
33
# File 'lib/snapback/mysql/client_control.rb', line 30

def unlock_tables
  @connection.query "UNLOCK TABLES"
  true
end