Class: JRubySQL::Controller

Inherits:
Object
  • Object
show all
Includes:
Messages
Defined in:
lib/jrubysql/controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Messages

#m

Constructor Details

#initialize(options, argv_str) ⇒ Controller

Returns a new instance of Controller.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jrubysql/controller.rb', line 9

def initialize options, argv_str
  @config = JRubySQL::Config.new
  upgrade_jrubysqlrc!

  histories = @config['connections']

  if options.nil?
    if histories.nil? || histories.empty?
      JRubySQL::OptionParser.parse []
    else
      # FIXME: Output
      puts m(:choose_parameter)
      histories.each_with_index do |entry, idx|
        puts "[#{idx + 1}] #{entry.keys.first}"
      end
      print '> '
      select = JRubySQL::Controller.get_console_input
      select = select && select.chomp
      if (1..(histories.length)).include?(select.to_i)
        entry     = histories[select.to_i - 1]
        @options  = entry.values.first[:options]
        @argv_str = entry.keys.first
      else
        puts
        JRubySQL::OptionParser.parse []
      end
    end
  else
    @options = options
    @argv_str = argv_str
  end

  @db_type = JRubySQL::RDBMS.get_type(@options[:type] || @options[:driver])

  # Setting up input: file or console (and more?)
  if @options[:filename]
    @input = JRubySQL::Input::Script.new(self, ::File.read(@options[:filename]))
  elsif @options[:script]
    @input = JRubySQL::Input::Script.new(self, @options[:script])
  else
    @input = JRubySQL::Input::Console.new(self)
  end

  # Setting up output: Colored terminal 
  case @options[:output]
  when 'cterm'
    @output = JRubySQL::Output::CTerm.new @config['colors']
    @config['colors'] = @output.colors
  when 'term'
    @output = JRubySQL::Output::Term.new
  when 'csv'
    @output = JRubySQL::Output::CSV.new
  end
end

Instance Attribute Details

#db_typeObject (readonly)

Returns the value of attribute db_type.



7
8
9
# File 'lib/jrubysql/controller.rb', line 7

def db_type
  @db_type
end

Instance Method Details

#cursor(empty = true) ⇒ Object



112
113
114
# File 'lib/jrubysql/controller.rb', line 112

def cursor empty = true
  @output.cursor empty
end


116
117
118
# File 'lib/jrubysql/controller.rb', line 116

def print_cursor empty = true
  @output.print_cursor empty
end

#startObject



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/jrubysql/controller.rb', line 64

def start
  @output.welcome!
  @output.info m(:connecting)
  begin
    @rdbms = JRubySQL::RDBMS.new @options
  rescue Exception => e
    @output.error e.to_s
    exit 1
  end
  @output.info m(:connected)

  history   = @config['connections'] || []
  entry_idx = history.index { |h| h.keys.first == @argv_str }
  entry     = entry_idx ? history.delete_at(entry_idx) : { @argv_str => {} }
  entry[@argv_str][:options] = @options
  commands = entry[@argv_str][:commands] ||= []
  @input.prepare commands

  history.unshift entry
  history.pop while history.length > JRubySQL::Constants::MAX_CONNECTION_HISTORY
  @config['connections'] = history

  add_cmd = lambda do |c|
    commands.push c unless commands.last == c
    commands.shift while commands.length > JRubySQL::Constants::MAX_COMMAND_HISTORY
    @config['connections'] = history
  end

  loop do
    ret = @input.get

    ret[:sqls].each do |sql|
      begin
        add_cmd.call sql + ret[:delimiter] if ret[:history]
        output @rdbms.execute(sql)
      rescue Exception => e
        @output.error e.to_s
      end
    end if ret.has_key?(:sqls)

    if ret.has_key?(:commands) && ret[:commands].first
      command, line = ret[:commands]
      add_cmd.call line unless command.keys.first == :quit
      process_command command.keys.first, command.values.first
    end
  end
end