diff --git a/lib/mariaex/query.ex b/lib/mariaex/query.ex index f7ab6da..d9f701b 100644 --- a/lib/mariaex/query.ex +++ b/lib/mariaex/query.ex @@ -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), diff --git a/lib/mariaex/structs.ex b/lib/mariaex/structs.ex index ddb89e7..2e1e31b 100644 --- a/lib/mariaex/structs.ex +++ b/lib/mariaex/structs.ex @@ -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 diff --git a/test/query_test.exs b/test/query_test.exs index 5bafe86..a5a837e 100644 --- a/test/query_test.exs +++ b/test/query_test.exs @@ -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