Packages
xandra
0.8.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.
"""
defstruct [:statement, :values, :id, :bound_columns, :result_columns]
@opaque t :: %__MODULE__{
statement: Xandra.statement,
values: Xandra.values | nil,
id: binary | nil,
bound_columns: list | nil,
result_columns: list | 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, Protocol}
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)
|> Protocol.encode_request(%{prepared | values: values}, options)
|> Frame.encode(options[:compressor])
end
def decode(prepared, %Frame{} = frame, _options) do
Protocol.decode_response(frame, prepared)
end
def describe(prepared, _options) do
prepared
end
end
defimpl Inspect do
import Inspect.Algebra
def inspect(prepared, options) do
concat(["#Xandra.Prepared<", to_doc(prepared.statement, options), ">"])
end
end
end