Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/ruby
- require 'mysql2'
- require 'base64'
- begin
- connection = {
- host: 'localhost',
- username: 'username',
- password: 'password',
- database: 'database'
- }
- client = Mysql2::Client.new(connection)
- client.query('show tables').each do |result|
- table = result['tabel_name']
- puts "Inspecting #{connection[:database]}.#{table}'\n\n"
- client.query("select * from #{table}", symbolize_keys: true).each do |row|
- key_length = row.keys.max_by(&:length).length
- default_line_format = "%-#{key_length}s = %s\n"
- binary_line_format = "%-#{key_length}s = %s\n"
- puts "#{connection[:database]}.#{table}.#{row.first[1]}\n"
- row.each_pair do |key, value|
- value_encoding = value.encoding rescue 'NUMBER'
- if value_encoding.to_s == "ASCII-8BIT"
- file = "#{Base64.decode64(value)}"
- printf "#{default_line_format}" % [key, "<<binary#{"#{file}".length > 0 ? '_file' : '_null'}>>"]
- else
- printf "#{default_line_format}" % [key, value.nil? || value.to_s.empty? ? 'NULL' : "#{value}"]
- end
- end
- puts "\n"
- end
- end
- rescue Mysql2::Error => e
- puts e.errno
- puts e.error
- ensure
- client.close if client
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement