Class: Itds::Repl

Inherits:
Object
  • Object
show all
Defined in:
lib/itds/repl.rb

Class Method Summary collapse

Class Method Details

.cancel_requestObject


45
46
47
48
49
50
# File 'lib/itds/repl.rb', line 45

def cancel_request
  if @res
    @res.cancel
    @res = nil
  end
end

.config(options) ⇒ Object


8
9
10
11
# File 'lib/itds/repl.rb', line 8

def config(options)
  @options = options
  @client = Client.new(options)
end

.execute_cmd(cmd) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/itds/repl.rb', line 33

def execute_cmd(cmd)
  begin
    stop if cmd == "exit"
    @res = @client.execute(cmd)
    print_result(@res)
  rescue => e
    print_err(e)
  ensure
    cancel_request
  end
end

70
71
72
73
74
75
76
77
78
79
# File 'lib/itds/repl.rb', line 70

def print_affected_row(res)
  # tiny_tds seems not only return affected_rows for update/delete
  # but also select.
  affected = res.affected_rows

  if affected > 0
    puts ""
    puts "Affected Rows: #{affected}"
  end
end

81
82
83
# File 'lib/itds/repl.rb', line 81

def print_err(err)
  puts "Error: #{err.message}"
end

21
22
23
24
25
26
27
# File 'lib/itds/repl.rb', line 21

def print_help
  msg = <<-EOT
itds SQL server interactive console.
Press Ctrl-C to cancel current query, press again to quit.
EOT
  puts msg
end

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/itds/repl.rb', line 52

def print_result(res)
  fields = res.fields
  rows = []
  res.each do |rowset|
    rows << rowset.values
  end

  output = {}
  output[:headings] = fields unless fields.empty?
  output[:rows] = rows unless rows.empty?

  unless output.empty?
    puts Terminal::Table.new output
  end

  print_affected_row(res)
end

.promptObject


29
30
31
# File 'lib/itds/repl.rb', line 29

def prompt
  @prompt ||= "#{@options[:database]}> "
end

.runObject


13
14
15
16
17
18
19
# File 'lib/itds/repl.rb', line 13

def run
  print_help

  while cmd = Readline.readline(prompt, true)
    execute_cmd(cmd)
  end
end

.singalObject


85
86
87
88
89
90
91
# File 'lib/itds/repl.rb', line 85

def singal
  if @res
    cancel_request
  else
    stop
  end
end

.stopObject


93
94
95
96
# File 'lib/itds/repl.rb', line 93

def stop
  @client.close
  exit
end