Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #!/usr/bin/ruby
  2.  
  3. require 'mysql2'
  4. require 'base64'
  5.  
  6. begin
  7.  
  8. connection = {
  9. host: 'localhost',
  10. username: 'username',
  11. password: 'password',
  12. database: 'database'
  13. }
  14.  
  15. client = Mysql2::Client.new(connection)
  16.  
  17. client.query('show tables').each do |result|
  18. table = result['tabel_name']
  19.  
  20. puts "Inspecting #{connection[:database]}.#{table}'\n\n"
  21.  
  22. client.query("select * from #{table}", symbolize_keys: true).each do |row|
  23.  
  24. key_length = row.keys.max_by(&:length).length
  25. default_line_format = "%-#{key_length}s = %s\n"
  26. binary_line_format = "%-#{key_length}s = %s\n"
  27.  
  28. puts "#{connection[:database]}.#{table}.#{row.first[1]}\n"
  29.  
  30. row.each_pair do |key, value|
  31.  
  32. value_encoding = value.encoding rescue 'NUMBER'
  33.  
  34. if value_encoding.to_s == "ASCII-8BIT"
  35. file = "#{Base64.decode64(value)}"
  36. printf "#{default_line_format}" % [key, "<<binary#{"#{file}".length > 0 ? '_file' : '_null'}>>"]
  37. else
  38. printf "#{default_line_format}" % [key, value.nil? || value.to_s.empty? ? 'NULL' : "#{value}"]
  39. end
  40. end
  41. puts "\n"
  42. end
  43. end
  44.  
  45. rescue Mysql2::Error => e
  46. puts e.errno
  47. puts e.error
  48.  
  49. ensure
  50. client.close if client
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement