Class: MysqlConn
- Inherits:
-
Object
- Object
- MysqlConn
- Defined in:
- lib/mysqlconn.rb
Overview
Copyright Neville Kadwa (2014)
Constant Summary collapse
- MODE_MYSQL =
:MYSQL
- MODE_MYSQLDUMP =
:MYSQLDUMP
- CONFIG_LOCATION =
File.("~/.db_connection_alias.yml")
Instance Attribute Summary collapse
-
#db_key ⇒ Object
readonly
Returns the value of attribute db_key.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
-
#initialize(mode) ⇒ MysqlConn
constructor
A new instance of MysqlConn.
- #mode_command ⇒ Object
- #mode_default_args(args) ⇒ Object
- #run(args) ⇒ Object
Constructor Details
#initialize(mode) ⇒ MysqlConn
Returns a new instance of MysqlConn.
17 18 19 20 |
# File 'lib/mysqlconn.rb', line 17 def initialize(mode) @mode = mode @verbose = false end |
Instance Attribute Details
#db_key ⇒ Object (readonly)
Returns the value of attribute db_key.
15 16 17 |
# File 'lib/mysqlconn.rb', line 15 def db_key @db_key end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
13 14 15 |
# File 'lib/mysqlconn.rb', line 13 def mode @mode end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
14 15 16 |
# File 'lib/mysqlconn.rb', line 14 def verbose @verbose end |
Instance Method Details
#mode_command ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/mysqlconn.rb', line 22 def mode_command case mode when :MYSQL "mysql" when :MYSQLDUMP "mysqldump" else raise "Unknown mode: #{mode}" end end |
#mode_default_args(args) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mysqlconn.rb', line 33 def mode_default_args(args) case mode when :MYSQL "--prompt=\"\\u@#{db_key} [\\d]> \"" when :MYSQLDUMP "" else raise "Unknown mode: #{mode}" end end |
#run(args) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/mysqlconn.rb', line 44 def run(args) @db_key = args.shift || raise('no db key provided. Usage mysqlconn db_key [mysql opts]*') config = begin File.open(CONFIG_LOCATION, 'r') do |f| YAML.load(f) end rescue Errno::ENOENT => e raise("No config found. Please follow instructions for creating #{CONFIG_LOCATION}") end raise("Config file is world readable. Exiting...") if File.stat(CONFIG_LOCATION).world_readable? if db_key == '-v' @verbose = true @db_key = args.shift end if db_key == '-l' filter = /.*#{args.shift||'.*'}.*/ config.keys.each do |k| puts k if filter =~ k end exit(0) end db = config[db_key] || raise("No #{db_key} found") db['host'] || raise("No #{db_key}.host found") command = "#{mode_command} -h #{db['host']} #{mode_default_args(args)}" command << " -P #{db['port']}" if db['port'] command << " -u #{db['user']}" if db['user'] command << " -p#{db['password']}" if db['password'] if db['database'] && mode == :MYSQL case mode when :MYSQL command << " -D #{db['database']}" when :MYSQLDUMP command << " #{db['database']}" end end unless args.empty? command << " " command << args.map{|s| "'#{s}'"}.join(' ') end STDERR.puts command if verbose exec(command) end |