From 6df8e8bb8932fe63aacc15cc53d06301f4d9252a Mon Sep 17 00:00:00 2001 From: Konrad Zemek Date: Thu, 24 Aug 2017 15:12:58 +0200 Subject: [PATCH] Add tagged parameters to encode. --- lib/mariaex/query.ex | 4 ++++ lib/mariaex/structs.ex | 5 +++++ test/query_test.exs | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+) 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