Class: ClickHouse::HTTPClient::Query

Inherits:
Data
  • Object
show all
Defined in:
lib/clickhouse/http_client/query.rb

Overview

Immutable query value object: SQL text with ClickHouse {name:Type} placeholders, plus the placeholder values to bind. Also the normal form every accepted query is converted into at the client boundary.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sql:, placeholders: {}) ⇒ Query

Returns a new instance of Query.

Raises:



37
38
39
40
41
42
43
44
45
46
# File 'lib/clickhouse/http_client/query.rb', line 37

def initialize(sql:, placeholders: {})
  raise QueryError, "Empty query string given" if sql.to_s.strip.empty?

  super(
    sql: -sql.to_s,
    placeholders: ValueNormalization.immutable_copy(
      ValueNormalization.symbolize_keys(placeholders || {}),
    ),
  )
end

Instance Attribute Details

#placeholdersObject (readonly)

Returns the value of attribute placeholders

Returns:

  • (Object)

    the current value of placeholders



8
9
10
# File 'lib/clickhouse/http_client/query.rb', line 8

def placeholders
  @placeholders
end

#sqlObject (readonly)

Returns the value of attribute sql

Returns:

  • (Object)

    the current value of sql



8
9
10
# File 'lib/clickhouse/http_client/query.rb', line 8

def sql
  @sql
end

Class Method Details

.wrap(query) ⇒ ClickHouse::HTTPClient::Query

Accepts a String of trusted SQL or any object honoring the query protocol (#to_sql and #placeholders).

Parameters:

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/clickhouse/http_client/query.rb', line 14

def self.wrap(query)
  case query
  when String
    new(sql: query)
  else
    # A builder that can compile itself (e.g. ClickHouse::SQL's
    # SelectQuery#to_query) does so here, so its compile-time
    # validations — like the nil-placeholder net — apply even when a
    # caller passes the builder directly instead of calling #to_query.
    # The arity check excludes ActiveSupport's unrelated
    # Object#to_query(namespace), which every object responds to.
    if query.respond_to?(:to_query) && query.method(:to_query).arity.zero?
      query = query.to_query
    end

    unless query.respond_to?(:to_sql) && query.respond_to?(:placeholders)
      raise QueryError, "Expected a SQL string or an object responding to #to_sql and #placeholders, got #{query.class}"
    end

    new(sql: query.to_sql, placeholders: query.placeholders)
  end
end

Instance Method Details

#to_sqlString

Returns SQL query text.

Returns:

  • (String)

    SQL query text.



49
50
51
# File 'lib/clickhouse/http_client/query.rb', line 49

def to_sql
  sql
end