Packages
exqlite
0.8.0
0.39.0
0.38.0
0.37.0
0.36.0
0.35.0
0.34.0
0.33.1
0.33.0
0.32.1
0.32.0
0.31.0
0.30.1
0.30.0
0.29.0
0.28.0
0.27.1
0.27.0
0.26.0
0.25.0
0.24.2
0.24.1
0.24.0
0.23.0
0.22.0
0.21.0
0.20.0
0.19.0
0.18.0
0.17.0
0.16.2
0.16.1
0.16.0
0.15.0
0.14.0
0.13.15
0.13.14
0.13.13
0.13.12
0.13.11
0.13.10
0.13.9
0.13.8
0.13.7
0.13.6
0.13.5
0.13.4
0.13.3
0.13.2
0.13.1
0.13.0
0.12.0
0.11.9
0.11.8
0.11.7
0.11.6
0.11.5
0.11.4
0.11.3
0.11.2
0.11.1
0.11.0
0.10.3
0.10.2
0.10.1
0.10.0
0.9.3
0.9.2
0.9.1
0.9.0
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.9
0.7.8
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.11
0.5.10
0.5.9
0.5.8
0.5.7
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.9
0.4.8
0.4.7
0.4.6
0.4.5
0.4.4
0.4.2
0.4.1
0.4.0
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.1
0.1.0
An Elixir SQLite3 library
Current section
Files
Jump to
Current section
Files
lib/exqlite/query.ex
defmodule Exqlite.Query do
@moduledoc """
Query struct returned from a successfully prepared query.
"""
@type t :: %__MODULE__{
statement: iodata(),
name: atom() | String.t(),
ref: reference() | nil,
command: :insert | :delete | :update | nil
}
defstruct statement: nil,
name: nil,
ref: nil,
command: nil
def build(options) do
statement = Keyword.get(options, :statement)
name = Keyword.get(options, :name)
ref = Keyword.get(options, :ref)
command =
case Keyword.get(options, :command) do
nil -> extract_command(statement)
value -> value
end
%__MODULE__{
statement: statement,
name: name,
ref: ref,
command: command
}
end
defp extract_command(nil), do: nil
defp extract_command(statement) do
cond do
String.contains?(statement, "INSERT") -> :insert
String.contains?(statement, "DELETE") -> :delete
String.contains?(statement, "UPDATE") -> :update
true -> nil
end
end
defimpl DBConnection.Query do
def parse(query, _opts) do
query
end
def describe(query, _opts) do
query
end
# TODO: See also Connection.bind/3
#
# def encode(%{ref: nil} = query, _params, _opts) do
# raise ArgumentError, "query #{inspect(query)} has not been prepared"
# end
#
# def encode(%{num_params: nil} = query, _params, _opts) do
# raise ArgumentError, "query #{inspect(query)} has not been prepared"
# end
#
# def encode(%{num_params: num_params} = query, params, _opts)
# when num_params != length(params) do
# message =
# "expected params count: #{inspect(num_params)}, got values: #{inspect(params)}" <>
# " for query: #{inspect(query)}"
#
# raise ArgumentError, message
# end
def encode(_query, params, _opts) do
params
end
def decode(_query, result, _opts) do
result
end
end
defimpl String.Chars do
def to_string(%{statement: statement}) do
IO.iodata_to_binary(statement)
end
end
end