Packages
ecto_ch
0.2.2
0.10.0
0.9.4
0.9.3
0.9.2
0.9.1
0.9.0
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
retired
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.6
0.6.5
0.6.4
0.6.3
0.6.2
retired
0.6.1
retired
0.6.0
retired
0.5.1
0.5.0
retired
0.4.1
0.4.0
retired
0.3.10
0.3.9
0.3.8
0.3.7
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.2.2
0.2.1
0.2.0
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
ClickHouse adapter for Ecto
Current section
Files
Jump to
Current section
Files
lib/ecto/adapters/clickhouse/api.ex
defmodule Ecto.Adapters.ClickHouse.API do
@moduledoc """
Helpers for building Ecto queries for ClickHouse.
"""
import Ecto.Query
@doc """
Builds an [`input(structure)`](https://clickhouse.com/docs/en/sql-reference/table-functions/input) that can be used in `Ecto.Query.from`
from i in input(a: "Int16", b: :string)
from i in input(Schema)
"""
def input(schema) when is_list(schema) do
structure =
schema
|> Enum.map(fn {name, type} ->
type =
case type do
_ when is_binary(type) -> type
_ -> Ch.Types.encode(type)
end
IO.iodata_to_binary([to_string(name), ?\s, type])
end)
|> Enum.join(", ")
%Ecto.Query{from: %Ecto.Query.FromExpr{source: {:fragment, _ctx = [], fragment}} = from} =
query = from(fragment("input(?)", literal(^structure)))
# TODO or maybe build query struct manually with a custom :fragment (like a custom op)
# which would be handled in connection.ex
%{query | from: %{from | source: {:fragment, [input: schema], fragment}}}
end
def input(schema) when is_atom(schema) do
query =
schema.__schema__(:fields)
|> Enum.map(fn field ->
type = schema.__schema__(:type, field)
type || raise "missing type for field " <> inspect(field)
{field, Ecto.Adapters.ClickHouse.Schema.remap_type(type, schema, field)}
end)
|> input()
# TODO
# %{query | sources: {nil, schema}}
query
end
end