Packages
xandra
0.14.0
0.19.4
0.19.3
0.19.2
0.19.1
0.19.0
0.18.1
0.18.0
0.18.0-rc.9
0.18.0-rc.8
0.18.0-rc.7
0.18.0-rc.6
0.18.0-rc.5
0.18.0-rc.4
0.18.0-rc.3
0.18.0-rc.2
0.18.0-rc.1
0.17.0
0.16.0
0.15.0
0.15.0-rc.1
0.14.0
0.13.1
0.13.0
0.12.0
0.11.0
0.10.1
0.10.0
0.9.2
0.9.1
0.9.0
0.8.0
0.7.2
0.7.1
0.7.0
0.6.1
0.6.0
0.5.1
0.5.0
0.4.3
0.4.2
0.4.1
0.4.0
0.3.2
0.3.1
0.3.0
0.2.0
0.1.0
Fast, simple, and robust Cassandra driver for Elixir.
Current section
Files
Jump to
Current section
Files
lib/xandra/prepared.ex
defmodule Xandra.Prepared do
@moduledoc """
A data structure used to internally represent prepared queries.
These are the publicly accessible fields of this struct:
* `:tracing_id` - the tracing ID (as a UUID binary) if tracing was enabled,
or `nil` if no tracing was enabled. See the "Tracing" section in `Xandra.execute/4`.
All other fields are documented in `t:t/0` to avoid Dialyzer warnings,
but are not meant to be used by users.
"""
defstruct [
:statement,
:values,
:id,
:bound_columns,
:result_columns,
:default_consistency,
:protocol_module,
:tracing_id,
:keyspace
]
@type t :: %__MODULE__{
statement: Xandra.statement(),
values: Xandra.values() | nil,
id: binary | nil,
bound_columns: list | nil,
result_columns: list | nil,
default_consistency: atom | nil,
protocol_module: module | nil,
tracing_id: binary | nil,
keyspace: binary | nil
}
@doc false
def rewrite_named_params_to_positional(%__MODULE__{} = prepared, params)
when is_map(params) do
Enum.map(prepared.bound_columns, fn {_keyspace, _table, name, _type} ->
case Map.fetch(params, name) do
{:ok, value} ->
value
:error ->
raise ArgumentError,
"missing named parameter #{inspect(name)} for prepared query, " <>
"got: #{inspect(params)}"
end
end)
end
defimpl DBConnection.Query do
alias Xandra.Frame
def parse(prepared, _options) do
prepared
end
def encode(prepared, values, options) when is_map(values) do
encode(prepared, @for.rewrite_named_params_to_positional(prepared, values), options)
end
def encode(prepared, values, options) when is_list(values) do
Frame.new(:execute, Keyword.take(options, [:compressor, :tracing]))
|> prepared.protocol_module.encode_request(%{prepared | values: values}, options)
|> Frame.encode(prepared.protocol_module)
end
def decode(_prepared, response, _options) do
response
end
def describe(prepared, _options) do
prepared
end
end
defimpl Inspect do
import Inspect.Algebra
def inspect(prepared, options) do
properties = [
statement: prepared.statement,
tracing_id: prepared.tracing_id
]
concat(["#Xandra.Prepared<", to_doc(properties, options), ">"])
end
end
end