Class: Itds::Repl
- Inherits:
-
Object
- Object
- Itds::Repl
- Defined in:
- lib/itds/repl.rb
Class Method Summary collapse
- .cancel_request ⇒ Object
- .config(options) ⇒ Object
- .execute_cmd(cmd) ⇒ Object
- .print_affected_row(res) ⇒ Object
- .print_err(err) ⇒ Object
- .print_help ⇒ Object
- .print_result(res) ⇒ Object
- .prompt ⇒ Object
- .run ⇒ Object
- .singal ⇒ Object
- .stop ⇒ Object
Class Method Details
.cancel_request ⇒ Object
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 = @client = Client.new() 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 |
.print_affected_row(res) ⇒ Object
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 |
.print_err(err) ⇒ Object
81 82 83 |
# File 'lib/itds/repl.rb', line 81 def print_err(err) puts "Error: #{err.}" end |
.print_help ⇒ Object
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 |
.print_result(res) ⇒ Object
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 |
.prompt ⇒ Object
29 30 31 |
# File 'lib/itds/repl.rb', line 29 def prompt @prompt ||= "#{@options[:database]}> " end |
.run ⇒ Object
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 |
.singal ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/itds/repl.rb', line 85 def singal if @res cancel_request else stop end end |
.stop ⇒ Object
93 94 95 96 |
# File 'lib/itds/repl.rb', line 93 def stop @client.close exit end |