Class: ClickHouse::Connection
- Inherits:
-
Object
- Object
- ClickHouse::Connection
- Includes:
- Extend::ConnectionAltering, Extend::ConnectionDatabase, Extend::ConnectionExplaining, Extend::ConnectionHealthy, Extend::ConnectionInserting, Extend::ConnectionSelective, Extend::ConnectionTable
- Defined in:
- lib/click_house/connection.rb
Constant Summary
Constants included from Extend::ConnectionExplaining
Extend::ConnectionExplaining::EXPLAIN, Extend::ConnectionExplaining::EXPLAIN_RE
Constants included from Extend::ConnectionInserting
Extend::ConnectionInserting::DEFAULT_JSON_COMPACT_EACH_ROW_FORMAT, Extend::ConnectionInserting::DEFAULT_JSON_EACH_ROW_FORMAT
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#compose(path, query = {}) ⇒ Object
rubocop:enable Metrics/AbcSize.
- #execute(query, body = nil, database: config.database, params: {}) ⇒ Object
- #faraday_v1? ⇒ Boolean
- #get(path = '/', body: '', query: {}, database: config.database) ⇒ Faraday::Response
-
#initialize(config) ⇒ Connection
constructor
A new instance of Connection.
- #post(body = nil, query: {}, database: config.database, params: {}) ⇒ Object
-
#transport ⇒ Object
transport should work the same both with Faraday v1 and Faraday v2 rubocop:disable Metrics/AbcSize.
Methods included from Extend::ConnectionExplaining
Methods included from Extend::ConnectionAltering
#add_column, #add_index, #alter_table, #clear_column, #drop_column, #drop_index, #modify_column
Methods included from Extend::ConnectionInserting
#insert, #insert_compact, #insert_rows
Methods included from Extend::ConnectionSelective
#select_all, #select_one, #select_value
Methods included from Extend::ConnectionTable
#create_table, #describe_table, #drop_table, #rename_table, #table_exists?, #table_schema, #tables, #truncate_table, #truncate_tables
Methods included from Extend::ConnectionDatabase
#create_database, #databases, #drop_database
Methods included from Extend::ConnectionHealthy
Constructor Details
#initialize(config) ⇒ Connection
Returns a new instance of Connection.
16 17 18 |
# File 'lib/click_house/connection.rb', line 16 def initialize(config) @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
13 14 15 |
# File 'lib/click_house/connection.rb', line 13 def config @config end |
Instance Method Details
#compose(path, query = {}) ⇒ Object
rubocop:enable Metrics/AbcSize
76 77 78 79 |
# File 'lib/click_house/connection.rb', line 76 def compose(path, query = {}) # without <query.compact> "DB::Exception: Empty query" error will occur "#{path}?#{URI.encode_www_form({ send_progress_in_http_headers: 1 }.merge(query).compact)}" end |
#execute(query, body = nil, database: config.database, params: {}) ⇒ Object
20 21 22 |
# File 'lib/click_house/connection.rb', line 20 def execute(query, body = nil, database: config.database, params: {}) post(body, query: { query: query }, database: database, params: config.global_params.merge(params)) end |
#faraday_v1? ⇒ Boolean
82 83 84 |
# File 'lib/click_house/connection.rb', line 82 def faraday_v1? Faraday::VERSION.start_with?('1') end |
#get(path = '/', body: '', query: {}, database: config.database) ⇒ Faraday::Response
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/click_house/connection.rb', line 30 def get(path = '/', body: '', query: {}, database: config.database) # backward compatibility since # https://github.com/shlima/click_house/pull/12/files#diff-9c6f3f06d3b575731eae4b6b95ddbcdcc20452c432b8f6e87a3a8e8645818107R24 if query.is_a?(String) query = { query: query } config.logger!.warn('since v1.4.0 use connection.get(body: "SELECT 1") instead of connection.get(query: "SELECT 1")') end transport.get(path) do |conn| conn.params = query.merge(database: database).compact conn.params[:send_progress_in_http_headers] = 1 unless body.empty? conn.body = body end end |
#post(body = nil, query: {}, database: config.database, params: {}) ⇒ Object
45 46 47 |
# File 'lib/click_house/connection.rb', line 45 def post(body = nil, query: {}, database: config.database, params: {}) transport.post(compose('/', query.merge(database: database, **params)), body) end |
#transport ⇒ Object
transport should work the same both with Faraday v1 and Faraday v2 rubocop:disable Metrics/AbcSize
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/click_house/connection.rb', line 51 def transport @transport ||= Faraday.new(config.url!) do |conn| conn..timeout = config.timeout conn..open_timeout = config.open_timeout conn.headers = config.headers conn.ssl.verify = config.ssl_verify if config.auth? if faraday_v1? conn.request :basic_auth, config.username, config.password else conn.request :authorization, :basic, config.username, config.password end end conn.response Middleware::RaiseError conn.response Middleware::Logging, logger: config.logger! conn.response Middleware::SummaryMiddleware, options: { config: config } # should be after logger conn.response config.json_parser, content_type: %r{application/json}, options: { config: config } conn.response Middleware::ParseCsv, content_type: %r{text/csv}, options: { config: config } conn.adapter config.adapter end end |