AshClickhouse.Connection (AshClickhouse v0.1.0)

Copy Markdown View Source

Process wrapper around the clickhouse client connection.

A connection is a named process that holds the configuration for a ClickHouse database. Queries are dispatched through this module so that the rest of the data layer does not need to know the details of the underlying client.

Options

  • :name — register the connection under this name (atom)
  • :url — the ClickHouse HTTP URL, e.g. "http://localhost:8123"
  • :username / :password — credentials
  • :database — the default database
  • :otp_app — application to read config from (used by AshClickhouse.Repo)

The clickhouse client is started as a supervised GenServer. We keep a thin wrapper so that the data layer can resolve a connection by name and run queries with consistent error handling.

Summary

Functions

Returns the child spec for a connection (for supervision trees).

Returns the connection struct registered under name, if any.

Inserts rows into a table using the client's bulk insert helper.

Runs a SQL query against ClickHouse.

Runs a SQL query, raising on error.

Starts a ClickHouse connection as part of a supervision tree.

Stops the connection.

Types

t()

@type t() :: %AshClickhouse.Connection{
  conn: pid() | atom(),
  database: String.t() | nil,
  name: atom() | nil
}

Functions

child_spec(opts)

@spec child_spec(keyword()) :: Supervisor.child_spec()

Returns the child spec for a connection (for supervision trees).

get_conn(name \\ __MODULE__)

@spec get_conn(atom()) :: t() | nil

Returns the connection struct registered under name, if any.

insert_rows(conn_or_name, table, rows, opts \\ [])

@spec insert_rows(t() | atom(), String.t(), [map()], keyword()) ::
  {:ok, term()} | {:error, term()}

Inserts rows into a table using the client's bulk insert helper.

query(conn_or_name, sql, params \\ [], opts \\ [])

@spec query(t() | atom(), String.t(), list(), keyword()) ::
  {:ok, term()} | {:error, term()}

Runs a SQL query against ClickHouse.

Returns {:ok, %ClickHouse.Result{}} or {:error, %ClickHouse.Error{}}.

query!(conn_or_name, sql, params \\ [], opts \\ [])

@spec query!(t() | atom(), String.t(), list(), keyword()) :: term()

Runs a SQL query, raising on error.

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Starts a ClickHouse connection as part of a supervision tree.

stop(name)

@spec stop(t() | atom()) :: :ok

Stops the connection.