Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
### Improvements
* Decode `sql_variant` values, e.g. `SELECT SERVERPROPERTY('EngineEdition')`, which raised a `FunctionClauseError` in `Tds.Types.decode_data/2`

## v2.4.0 (2026-08-19)
### Fixes
* Fix `unique_constraint` support in Ecto for en-US error messages: MSSQL error metadata is now preserved when re-raising from `query!/4`, `prepare!/3`, `execute!/4` and `close!/2` (#173)
Expand Down
76 changes: 75 additions & 1 deletion lib/tds/types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,26 @@ defmodule Tds.Types do
{value, tail}
end

# TODO Variant Types
# Variant Types
# See: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-tds/2435ed85-583b-4d14-9ffe-a2c7d6b4b4b8
def decode_data(%{data_reader: :variant}, <<0::little-unsigned-32, tail::binary>>),
do: {nil, tail}

def decode_data(
%{data_reader: :variant},
<<
size::little-unsigned-32,
base_type::unsigned-8,
prop_bytes::unsigned-8,
props::binary-size(prop_bytes),
tail::binary
>>
) do
# size counts the base type and prop bytes count bytes too
data_size = size - 2 - prop_bytes
<<data::binary-size(data_size), tail::binary>> = tail
{decode_variant(base_type, props, data), tail}
end

def decode_data(%{data_reader: :plp}, <<
@tds_plp_null::little-unsigned-64,
Expand Down Expand Up @@ -644,6 +663,61 @@ defmodule Tds.Types do
{value, tail}
end

# Unlike fixed length columns, tinyint is unsigned
defp decode_variant(@tds_data_type_tinyint, <<>>, <<val::unsigned-8>>), do: val

defp decode_variant(base_type, <<>>, data)
when is_map_key(@fixed_data_types, base_type) do
info = %{
data_type: :fixed,
data_type_code: base_type,
length: @fixed_data_types[base_type]
}

{value, _tail} = decode_data(info, data)
value
end

defp decode_variant(@tds_data_type_uniqueidentifier, <<>>, data),
do: decode_uuid(:binary.copy(data))

defp decode_variant(@tds_data_type_daten, <<>>, data), do: decode_date(data)

defp decode_variant(@tds_data_type_timen, <<scale>>, data),
do: decode_time(scale, data)

defp decode_variant(@tds_data_type_datetime2n, <<scale>>, data),
do: decode_datetime2(scale, data)

defp decode_variant(@tds_data_type_datetimeoffsetn, <<scale>>, data),
do: decode_datetimeoffset(scale, data)

defp decode_variant(base_type, <<precision, scale>>, data)
when base_type in [@tds_data_type_decimaln, @tds_data_type_numericn],
do: decode_decimal(precision, scale, data)

defp decode_variant(base_type, <<_max_length::little-unsigned-16>>, data)
when base_type in [@tds_data_type_bigvarbinary, @tds_data_type_bigbinary],
do: :binary.copy(data)

defp decode_variant(
base_type,
<<collation::binary-5, _max_length::little-unsigned-16>>,
data
)
when base_type in [@tds_data_type_bigvarchar, @tds_data_type_bigchar] do
{:ok, collation} = decode_collation(collation)
decode_char(%{collation: collation}, data)
end

defp decode_variant(
base_type,
<<_collation::binary-5, _max_length::little-unsigned-16>>,
data
)
when base_type in [@tds_data_type_nvarchar, @tds_data_type_nchar],
do: decode_nchar(%{}, data)

def decode_plp_chunk(<<chunksize::little-unsigned-32, tail::binary>>, buf)
when chunksize == 0,
do: {buf, tail}
Expand Down
94 changes: 94 additions & 0 deletions test/variant_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
defmodule VariantTest do
import Tds.TestHelper
use ExUnit.Case, async: true

setup do
{:ok, pid} = Tds.start_link(opts())

{:ok, [pid: pid]}
end

test "sql_variant NULL", context do
assert [[nil]] = query("SELECT CAST(NULL AS sql_variant)", [])
end

test "sql_variant SERVERPROPERTY", context do
assert [[edition]] = query("SELECT SERVERPROPERTY('EngineEdition')", [])
assert is_integer(edition)
end

test "sql_variant integer and bit base types", context do
assert [[255, -7, 42, -9_000_000_000, true]] =
query(
"""
SELECT CAST(CAST(255 AS tinyint) AS sql_variant),
CAST(CAST(-7 AS smallint) AS sql_variant),
CAST(42 AS sql_variant),
CAST(CAST(-9000000000 AS bigint) AS sql_variant),
CAST(CAST(1 AS bit) AS sql_variant)
""",
[]
)
end

test "sql_variant numeric base types", context do
assert [[decimal, numeric, 12.3456, -1.5, 1.5, 2.25]] =
query(
"""
SELECT CAST(CAST(-12.345 AS decimal(10, 3)) AS sql_variant),
CAST(CAST(1.25 AS numeric(5, 2)) AS sql_variant),
CAST(CAST(12.3456 AS money) AS sql_variant),
CAST(CAST(-1.5 AS smallmoney) AS sql_variant),
CAST(CAST(1.5 AS real) AS sql_variant),
CAST(CAST(2.25 AS float) AS sql_variant)
""",
[]
)

assert Decimal.equal?(decimal, Decimal.new("-12.345"))
assert Decimal.equal?(numeric, Decimal.new("1.25"))
end

test "sql_variant date and time base types", context do
assert [
[
{2026, 11, 2},
{13, 45, 30, 1_234_567},
{{2026, 11, 2}, {13, 45, 30, 12}},
{{2026, 11, 2}, {11, 45, 30, 123}, 120},
{{2026, 11, 2}, {13, 45, 0, 0}},
{{2026, 11, 2}, {13, 45, 30, 0}}
]
] =
query(
"""
SELECT CAST(CAST('2026-11-02' AS date) AS sql_variant),
CAST(CAST('13:45:30.1234567' AS time(7)) AS sql_variant),
CAST(CAST('2026-11-02T13:45:30.12' AS datetime2(2)) AS sql_variant),
CAST(CAST('2026-11-02T13:45:30.123+02:00' AS datetimeoffset(3)) AS sql_variant),
CAST(CAST('2026-11-02T13:45:00' AS smalldatetime) AS sql_variant),
CAST(CAST('2026-11-02T13:45:30' AS datetime) AS sql_variant)
""",
[]
)
end

test "sql_variant string, binary and uniqueidentifier base types", context do
uuid = Tds.Types.UUID.bingenerate()
{:ok, uuid_string} = Tds.Types.UUID.load(uuid)

assert [["ab ", "plain", "résumé", "ñ ", <<0xDE, 0xAD, 0xBE, 0xEF>>, <<0xAB, 0, 0>>, ^uuid]] =
query(
"""
SELECT CAST(CAST('ab' AS char(4)) AS sql_variant),
CAST('plain' AS sql_variant),
CAST(N'résumé' AS sql_variant),
CAST(CAST(N'ñ' AS nchar(2)) AS sql_variant),
CAST(0xDEADBEEF AS sql_variant),
CAST(CAST(0xAB AS binary(3)) AS sql_variant),
CAST(CAST('#{uuid_string}' AS uniqueidentifier) AS sql_variant)
""",
[]
)
end
end