lib/clickhouse/connection/client.rb in clickhouse-0.1.1 vs lib/clickhouse/connection/client.rb in clickhouse-0.1.2

- old
+ new

@@ -1,13 +1,22 @@ module Clickhouse class Connection module Client def connect! - return if connected? + ping! unless connected? + end + + def ping! ensure_authentication - ping! + status = client.get("/").status + if status != 200 + raise ConnectionError, "Unexpected response status: #{status}" + end + true + rescue Faraday::Error => e + raise ConnectionError, e.message end def connected? instance_variables.include?(:@client) && !!@client end @@ -18,16 +27,16 @@ def post(query, body = nil) request(:post, query, body) end - private - def url "#{@config[:scheme]}://#{@config[:host]}:#{@config[:port]}" end + private + def path(query) database = "database=#{@config[:database]}&" if @config[:database] "/?#{database}query=#{CGI.escape(query)}" end @@ -38,27 +47,19 @@ def ensure_authentication username, password = @config.values_at(:username, :password) client.basic_auth(username || "default", password) if username || password end - def ping! - status = client.get("/").status - if status != 200 - raise ConnectionError, "Unexpected response status: #{status}" - end - true - rescue Faraday::ConnectionFailed => e - raise ConnectionError, e.message - end - def request(method, query, body = nil) connect! query = query.to_s.strip start = Time.now client.send(method, path(query), body).tap do |response| log :info, "\n SQL (#{((Time.now - start) * 1000).round(1)}ms) #{query.gsub(/( FORMAT \w+|;$)/, "")};" raise QueryError, response.body unless response.status == 200 end + rescue Faraday::Error => e + raise ConnectionError, e.message end end end end