Module: Clickhouse::CLI::Console

Extended by:
Client, Console
Included in:
Console
Defined in:
lib/clickhouse/cli/console.rb

Constant Summary collapse

CLR =
"\r\e[A\e[K"

Constants included from Client

Clickhouse::CLI::Client::HISTORY_FILE

Instance Method Summary collapse

Methods included from Client

alter_history, debug, dump_history, execute, extended, included, interpolate_patterns, load_history, log, numerize_patterns, prettify

Instance Method Details

#process_result(result, log) ⇒ Object



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
63
64
65
66
67
68
69
# File 'lib/clickhouse/cli/console.rb', line 38

def process_result(result, log)
  if result.is_a?(Clickhouse::Connection::Query::ResultSet)
    if result.size > 0
      array = [result.names].concat(result.to_a)
      lengths = array.inject([]) do |lengths, row|
        row.each_with_index do |value, index|
          length = value.to_s.strip.length
          lengths[index] = [lengths[index].to_i, length].max
        end
        lengths
      end
      puts
      array.each_with_index do |row, i|
        values = [nil]
        lengths.each_with_index do |length, index|
          values << row[index].to_s.ljust(length, " ")
        end
        values << nil
        separator = (i == 0) ? "+" : "|"
        puts values.join(" #{separator} ")
      end
    end
  else
    puts result == true ? "Ok." : (result || "Fail.")
  end

  if log
    puts
    puts log.strip
  end
  puts
end

#readline(buffer = nil) ⇒ Object

private



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/clickhouse/cli/console.rb', line 17

def readline(buffer = nil)
  prompt = buffer ? ":-] " : ":) "
  line = Readline.readline(prompt, true)

  exit! unless line && !%w(exit quit).include?(line = line.strip)

  line = prettify(line)
  sql = [buffer, line].compact.join("\n").gsub(/\s+;$/, ";")

  puts "#{CLR}#{prompt}#{line}"
  alter_history(sql)

  buffer = begin
    execute(sql)
  rescue Clickhouse::Error => e
    puts "ERROR: #{e.message}"
  end

  readline buffer
end

#run!(options = {}) ⇒ Object



11
12
13
# File 'lib/clickhouse/cli/console.rb', line 11

def run!(options = {})
  readline
end