Methods
F
N
S
Class Public methods
new(app)
    # File railties/lib/rails/commands/dbconsole.rb, line 18
18:     def initialize(app)
19:       @app = app
20:     end
start(app)
    # File railties/lib/rails/commands/dbconsole.rb, line 14
14:     def self.start(app)
15:       new(app).start
16:     end
Instance Public methods
find_cmd(*commands)
    # File railties/lib/rails/commands/dbconsole.rb, line 49
49:       def find_cmd(*commands)
50:         dirs_on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR)
51:         commands += commands.map{|cmd| "#{cmd}.exe"} if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
52: 
53:         full_path_command = nil
54:         found = commands.detect do |cmd|
55:           dir = dirs_on_path.detect do |path|
56:             full_path_command = File.join(path, cmd)
57:             File.executable? full_path_command
58:           end
59:         end
60:         found ? full_path_command : abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.")
61:       end
start()
     # File railties/lib/rails/commands/dbconsole.rb, line 22
 22:     def start
 23:       include_password = false
 24:       options = {}
 25:       OptionParser.new do |opt|
 26:         opt.banner = "Usage: dbconsole [environment] [options]"
 27:         opt.on("-p", "--include-password", "Automatically provide the password from database.yml") do |v|
 28:           include_password = true
 29:         end
 30: 
 31:         opt.on("--mode [MODE]", ['html', 'list', 'line', 'column'],
 32:           "Automatically put the sqlite3 database in the specified mode (html, list, line, column).") do |mode|
 33:             options['mode'] = mode
 34:         end
 35: 
 36:         opt.on("--header") do |h|
 37:           options['header'] = h
 38:         end
 39: 
 40:         opt.parse!(ARGV)
 41:         abort opt.to_s unless (0..1).include?(ARGV.size)
 42:       end
 43: 
 44:       unless config = @app.config.database_configuration[Rails.env]
 45:         abort "No database is configured for the environment '#{Rails.env}'"
 46:       end
 47: 
 48: 
 49:       def find_cmd(*commands)
 50:         dirs_on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR)
 51:         commands += commands.map{|cmd| "#{cmd}.exe"} if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
 52: 
 53:         full_path_command = nil
 54:         found = commands.detect do |cmd|
 55:           dir = dirs_on_path.detect do |path|
 56:             full_path_command = File.join(path, cmd)
 57:             File.executable? full_path_command
 58:           end
 59:         end
 60:         found ? full_path_command : abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.")
 61:       end
 62: 
 63:       case config["adapter"]
 64:       when /^mysql/
 65:         args = {
 66:           'host'      => '--host',
 67:           'port'      => '--port',
 68:           'socket'    => '--socket',
 69:           'username'  => '--user',
 70:           'encoding'  => '--default-character-set'
 71:         }.map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }.compact
 72: 
 73:         if config['password'] && include_password
 74:           args << "--password=#{config['password']}"
 75:         elsif config['password'] && !config['password'].to_s.empty?
 76:           args << "-p"
 77:         end
 78: 
 79:         args << config['database']
 80: 
 81:         exec(find_cmd('mysql', 'mysql5'), *args)
 82: 
 83:       when "postgresql", "postgres"
 84:         ENV['PGUSER']     = config["username"] if config["username"]
 85:         ENV['PGHOST']     = config["host"] if config["host"]
 86:         ENV['PGPORT']     = config["port"].to_s if config["port"]
 87:         ENV['PGPASSWORD'] = config["password"].to_s if config["password"] && include_password
 88:         exec(find_cmd('psql'), config["database"])
 89: 
 90:       when "sqlite"
 91:         exec(find_cmd('sqlite'), config["database"])
 92: 
 93:       when "sqlite3"
 94:         args = []
 95: 
 96:         args << "-#{options['mode']}" if options['mode']
 97:         args << "-header" if options['header']
 98:         args << config['database']
 99: 
100:         exec(find_cmd('sqlite3'), *args)
101: 
102:       when "oracle", "oracle_enhanced"
103:         logon = ""
104: 
105:         if config['username']
106:           logon = config['username']
107:           logon << "/#{config['password']}" if config['password'] && include_password
108:           logon << "@#{config['database']}" if config['database']
109:         end
110: 
111:         exec(find_cmd('sqlplus'), logon)
112: 
113:       else
114:         abort "Unknown command-line client for #{config['database']}. Submit a Rails patch to add support!"
115:       end
116:     end