Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tagged parameters to encode. #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/mariaex/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ defimpl DBConnection.Query, for: Mariaex.Query do

defp encode_param(nil, _binary_as),
do: {1, :field_type_null, ""}
defp encode_param(%Mariaex.TypedValue{type: :binary, value: bin}, _binary_as) when is_binary(bin),
do: encode_param(bin, :field_type_blob)
defp encode_param(%Mariaex.TypedValue{type: :string, value: bin}, _binary_as) when is_binary(bin),
do: encode_param(bin, :field_type_var_string)
defp encode_param(bin, binary_as) when is_binary(bin),
do: {0, binary_as, << to_length_encoded_integer(byte_size(bin)) :: binary, bin :: binary >>}
defp encode_param(int, _binary_as) when is_integer(int),
Expand Down
5 changes: 5 additions & 0 deletions lib/mariaex/structs.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ defmodule Mariaex.Cursor do
@moduledoc false
defstruct [:ref, :statement_id, :params, max_rows: 0]
end

defmodule Mariaex.TypedValue do
@moduledoc false
defstruct [:type, :value]
end
18 changes: 18 additions & 0 deletions test/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,24 @@ defmodule QueryTest do
assert query("SELECT active, tiny from #{table} WHERE id = ?", [2]) == [[1, 0]]
end

test "tagged binary and string tests", context do
table = "tagged_test"
binary = 15..0 |> Enum.into([]) |> IO.iodata_to_binary()
string = "☀☁☂☃☄★☆☇☈☉☊☋☌☍☎☏"

:ok = query("CREATE TABLE #{table} (id serial, bin binary(16), str varchar(16))", [])

:ok = query(~s{INSERT INTO #{table} (id, bin, str) VALUES (?, ?, ?)},
[1, %Mariaex.TypedValue{type: :binary, value: binary},
%Mariaex.TypedValue{type: :string, value: string}])

assert query("SELECT bin, str from #{table} WHERE id = ?", [1]) == [[binary, string]]

assert query("SELECT bin, str from #{table} WHERE id = ? AND bin = ? AND str = ?",
[1, %Mariaex.TypedValue{type: :binary, value: binary},
%Mariaex.TypedValue{type: :string, value: string}]) == [[binary, string]]
end

test "support numeric data types using prepared statements", context do
integer = 16
float = 0.1
Expand Down