Class: ClickhouseNative::Client

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/clickhouse_native/client.rb,
lib/clickhouse_native/logging.rb,
ext/clickhouse_native/client.cpp

Constant Summary

Constants included from Logging

Logging::LEVEL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#execute, #insert_block, #query, #query_each, #query_value

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



5
6
7
# File 'lib/clickhouse_native/client.rb', line 5

def database
  @database
end

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/clickhouse_native/client.rb', line 5

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



60
61
62
# File 'lib/clickhouse_native/logging.rb', line 60

def logger
  @logger
end

#ping_before_queryObject (readonly)

Returns the value of attribute ping_before_query.



5
6
7
# File 'lib/clickhouse_native/client.rb', line 5

def ping_before_query
  @ping_before_query
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/clickhouse_native/client.rb', line 5

def port
  @port
end

#retry_timeoutObject (readonly)

Returns the value of attribute retry_timeout.



5
6
7
# File 'lib/clickhouse_native/client.rb', line 5

def retry_timeout
  @retry_timeout
end

#tcp_keepaliveObject (readonly)

Returns the value of attribute tcp_keepalive.



5
6
7
# File 'lib/clickhouse_native/client.rb', line 5

def tcp_keepalive
  @tcp_keepalive
end

Instance Method Details

#clear_schema_cache(table = nil, db_name: nil) ⇒ Object

Drop the memoized schema for a table (or all tables). Needed after DDL that changes column set or types, since insert-time schema lookups are cached per (table, db_name).



43
44
45
46
# File 'lib/clickhouse_native/client.rb', line 43

def clear_schema_cache(table = nil, db_name: nil)
  return unless defined?(@schema_cache) && @schema_cache
  table.nil? ? @schema_cache.clear : @schema_cache.delete([db_name, table.to_s])
end

#describe_table(table, db_name: nil) ⇒ Object



7
8
9
10
# File 'lib/clickhouse_native/client.rb', line 7

def describe_table(table, db_name: nil)
  fq = db_name ? "#{db_name}.#{table}" : table
  query("DESCRIBE TABLE #{fq}")
end

#insert(table, rows, columns: nil, db_name: nil, types: nil) ⇒ Object

insert(table, rows, columns: nil, db_name: nil, types: nil)

rows may be Array<Hash=> Object> or Array. columns defaults to the first hash's keys (for Array) or all table columns in DDL order (for Array). types may be supplied to skip the DESCRIBE lookup.

The schema lookup is memoized per (table, db_name) on the client, so repeated inserts into the same table don't re-run DESCRIBE. Call clear_schema_cache after ALTER TABLE to invalidate.

Hash keys not present in the schema raise ArgumentError — if you need to insert a subset, pass columns: explicitly.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/clickhouse_native/client.rb', line 25

def insert(table, rows, columns: nil, db_name: nil, types: nil)
  return 0 if rows.empty?

  fq = db_name ? "#{db_name}.#{table}" : table
  col_pairs =
    if types && columns
      zip_columns_and_types(columns, types)
    else
      columns_from_schema(table, rows, columns, db_name, fq)
    end
  row_arrays = rows.first.is_a?(Hash) ? hash_rows_to_arrays(rows, col_pairs) : rows

  insert_block(fq, col_pairs, row_arrays)
end

#inspectObject



48
49
50
# File 'lib/clickhouse_native/client.rb', line 48

def inspect
  "#<#{self.class} #{host}:#{port}/#{database}>"
end