Class: MysqlData

Inherits:
BaseUri show all
Defined in:
lib/libisi/uri/mysql.rb

Instance Attribute Summary

Attributes inherited from BaseUri

#options, #uri

Instance Method Summary collapse

Methods inherited from BaseUri

#base_uri, #credential_hash, #execute_command, #find, #password, #user

Constructor Details

#initialize(uri, options = {}) ⇒ MysqlData

Returns a new instance of MysqlData.



21
22
23
24
# File 'lib/libisi/uri/mysql.rb', line 21

def initialize(uri, options = {})
  super
  self.database
end

Instance Method Details

#copy_command(target = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/libisi/uri/mysql.rb', line 59

def copy_command(target = nil)    
  raise "Convert mysql to #{target.class} not implemented." unless target.class == self.class or target.class == NilClass
  raise "You must specify a source database if you give a target database." if database.nil? and database
  
  ret = execute_command(source_command)
  if target
    ret += "|" + target.execute_command(target.target_command)
  end
  ret
end

#create_commandObject



86
87
88
# File 'lib/libisi/uri/mysql.rb', line 86

def create_command
  execute_mysql("CREATE DATABASE #{database}")
end

#databaseObject



26
27
28
29
30
31
# File 'lib/libisi/uri/mysql.rb', line 26

def database
  db = uri.path
  db = db[1..-1] if db =~ /^\//
  raise "Unexpected database name #{db}" if db =~ /\//
  db
end

#execute_mysql(sql) ⇒ Object



39
40
41
# File 'lib/libisi/uri/mysql.rb', line 39

def execute_mysql(sql)
  execute_command("echo '#{sql}' | " + mysql_command)
end

#make_readable_commandObject



78
79
80
81
82
83
84
# File 'lib/libisi/uri/mysql.rb', line 78

def make_readable_command
  if database
    execute_mysql("GRANT SELECT, LOCK TABLES ON #{database}.* TO #{user}@localhost IDENTIFIED BY \"#{password}\"")
  else
    execute_mysql("GRANT SELECT, LOCK TABLES ON *.* TO #{user}@localhost IDENTIFIED BY \"#{password}\"")
  end
end

#make_writable_commandObject



90
91
92
93
94
95
96
# File 'lib/libisi/uri/mysql.rb', line 90

def make_writable_command
  if database
    execute_mysql("GRANT ALL ON #{database}.* TO #{user}@localhost IDENTIFIED BY \"#{password}\"")
  else
    execute_mysql("GRANT ALL ON *.* TO #{user}@localhost IDENTIFIED BY \"#{password}\"")
  end
end

#mysql_command(command = "mysql") ⇒ Object



33
34
35
36
37
38
# File 'lib/libisi/uri/mysql.rb', line 33

def mysql_command(command = "mysql")
  cmd = command
  cmd += " -u #{user}" if user
  cmd += " -p#{password}" if password
  cmd
end

#readable_commandObject



70
71
72
73
74
75
76
# File 'lib/libisi/uri/mysql.rb', line 70

def readable_command
  if database
    execute_command("echo 'show tables' | " + mysql_command + " #{database} 2>&1 > /dev/null")
  else
    execute_command("echo 'show databases' | " + mysql_command + " 2>&1 > /dev/null")
  end
end

#source_commandObject



43
44
45
46
47
48
49
50
51
# File 'lib/libisi/uri/mysql.rb', line 43

def source_command
  cmd = mysql_command("mysqldump")
  if uri.path
    cmd += " #{database}"
  else
    cmd += " --all-databases"
  end
  cmd
end

#target_commandObject



53
54
55
56
57
# File 'lib/libisi/uri/mysql.rb', line 53

def target_command
  cmd = mysql_command
  cmd += " #{database}" if uri.path
  cmd
end