Module: Clickhouse::CLI::Client
Constant Summary collapse
- HISTORY_FILE =
"#{ENV["HOME"]}/.clickhouse_history"
Class Method Summary collapse
- .debug(message = nil) ⇒ Object
- .extended(base) ⇒ Object
- .included(base) ⇒ Object
- .load_history ⇒ Object
- .log ⇒ Object
Instance Method Summary collapse
- #alter_history(sql, current = true) ⇒ Object
- #dump_history ⇒ Object
- #execute(sql, &block) ⇒ Object
- #interpolate_patterns(sql, replaced) ⇒ Object
- #numerize_patterns(sql, replaced = []) ⇒ Object
- #prettify(sql) ⇒ Object
Class Method Details
.debug(message = nil) ⇒ Object
16 17 18 |
# File 'lib/clickhouse/cli/client.rb', line 16 def self.debug( = nil) @log = .split("\n").detect{|line| line.include?("36m")} end |
.extended(base) ⇒ Object
11 12 13 14 |
# File 'lib/clickhouse/cli/client.rb', line 11 def self.extended(base) Clickhouse.logger = self load_history end |
.included(base) ⇒ Object
7 8 9 |
# File 'lib/clickhouse/cli/client.rb', line 7 def self.included(base) extended(base) end |
.load_history ⇒ Object
24 25 26 27 28 |
# File 'lib/clickhouse/cli/client.rb', line 24 def self.load_history File.readlines(HISTORY_FILE).each do |line| Readline::HISTORY.push line.gsub(";;", "\n").strip end if File.exists?(HISTORY_FILE) end |
.log ⇒ Object
20 21 22 |
# File 'lib/clickhouse/cli/client.rb', line 20 def self.log @log end |
Instance Method Details
#alter_history(sql, current = true) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/clickhouse/cli/client.rb', line 30 def alter_history(sql, current = true) (Readline::HISTORY.to_a.count{|line| line[-1] != ";"} + (current ? 1 : 0)).times do Readline::HISTORY.pop end unless Readline::HISTORY.to_a[-1] == sql Readline::HISTORY.push(sql) end end |
#dump_history ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/clickhouse/cli/client.rb', line 39 def dump_history File.open(HISTORY_FILE, "w+") do |file| Readline::HISTORY.each do |line| file.puts line.strip.gsub("\n", ";;") end end end |
#execute(sql, &block) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/clickhouse/cli/client.rb', line 131 def execute(sql, &block) if sql[-1] == ";" dump_history method = sql.match(/^(SELECT|SHOW|DESCRIBE)/i) ? :query : :execute result = Clickhouse.connection.send(method, sql[0..-2]) if block_given? block.call(result, Client.log) else process_result(result, Client.log) end else sql end end |
#interpolate_patterns(sql, replaced) ⇒ Object
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/clickhouse/cli/client.rb', line 120 def interpolate_patterns(sql, replaced) matched = false sql = sql.gsub(/(\$|%)\{(\d+)\}/) do |match| matched = true replaced[$2.to_i] end matched ? interpolate_patterns(sql, replaced) : sql end |
#numerize_patterns(sql, replaced = []) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/clickhouse/cli/client.rb', line 103 def numerize_patterns(sql, replaced = []) sql = sql.gsub(/(["'])(?:(?=(\\?))\2.)*?\1/) do |match| replaced << match "${#{replaced.size - 1}}" end parenthesized = false sql = sql.gsub(/\([^\(\)]*?\)/) do |match| parenthesized = true replaced << match "%{#{replaced.size - 1}}" end parenthesized ? numerize_patterns(sql, replaced) : [sql, replaced] end |
#prettify(sql) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 |
# File 'lib/clickhouse/cli/client.rb', line 47 def prettify(sql) sql, replaced = numerize_patterns(sql) preserved_words = %w( USE SHOW DATABASES TABLES PROCESSLIST INSERT INTO FORMAT SELECT COUNT DISTINCT SAMPLE AS FROM JOIN UNION ALL PREWHERE WHERE AND OR NOT IN GROUP BY HAVING ORDER LIMIT CREATE DESCRIBE ALTER RENAME DROP DETACH ATTACH TABLE VIEW PARTITION EXISTS SET OPTIMIZE WITH TOTALS ).sort{|a, b| [b.size, a] <=> [a.size, b]} sql.gsub!(/(\b)(#{preserved_words.join("|")})(\b)/i) do "#{$1}#{$2.upcase}#{$3}" end interpolate_patterns(sql, replaced) end |