Skip to content

Working version of tvp to stored proc #125

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

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions lib/tds.ex
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ defmodule Tds do
end
end

def proc(pid, statement, params, opts \\ []) do
opts = Keyword.put_new(opts, :proc, statement)

query(pid, statement, params, opts)
end

@spec execute(conn, Tds.Query.t(), list, [execute_option]) ::
{:ok, Tds.Query.t(), Tds.Result.t()}
| {:error, Tds.Error.t()}
Expand Down
9 changes: 9 additions & 0 deletions lib/tds/column.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Tds.Column do
@type t :: %__MODULE__{
name: String.t | nil,
type: Atom | nil,
opts: Keyword.t
}

defstruct [name: "", type: nil, opts: []]
end
6 changes: 6 additions & 0 deletions lib/tds/messages.ex
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,12 @@ defmodule Tds.Messages do
encode_rpc_params(params, "")
end

defp encode_rpc(proc, params) when is_binary(proc) do
rpc_size = byte_size(proc)
rpc_name = to_little_ucs2(proc)
<<rpc_size::little-size(16)>> <> rpc_name <> <<0x00, 0x00>> <> encode_rpc_params(params, "")
end

# Finished processing params
defp encode_rpc_params([], ret), do: ret

Expand Down
25 changes: 21 additions & 4 deletions lib/tds/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,15 @@ defmodule Tds.Protocol do
%{sock: _sock} = s
) do
params = opts[:parameters] || params
proc = opts[:proc] || nil
Process.put(:resultset, Keyword.get(opts, :resultset, false))

if params != [] do
send_param_query(query, params, s)
else
send_query(statement, s)
cond do
params != [] and is_nil(proc) -> send_param_query(query, params, s)
not is_nil(proc) -> send_proc(proc, params, s)
true -> send_query(statement, s)
end

rescue
exception ->
{:error, exception, s}
Expand Down Expand Up @@ -629,6 +631,21 @@ defmodule Tds.Protocol do
end
end

def send_proc(proc, params, s) do
params = Tds.Parameter.prepare_params(params)
msg = msg_rpc(proc: proc, params: params)

case msg_send(msg, s) do
{:ok, %{result: result} = s} ->
{:ok, result, %{s | state: :ready}}
{:error, err, %{transaction: :started} = s} ->
{:error, err, %{s | transaction: :failed}}
err ->
err
end
end


@spec send_param_query(Tds.Query.t(), list(), t) ::
{:error, any()}
| {:ok, %{optional(:result) => none()}}
Expand Down
114 changes: 104 additions & 10 deletions lib/tds/types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Tds.Types do
use Bitwise

alias Tds.Parameter
alias Tds.Column

@year_1900_days :calendar.date_to_gregorian_days({1900, 1, 1})
@secs_in_min 60
Expand Down Expand Up @@ -66,6 +67,7 @@ defmodule Tds.Types do
@tds_data_type_nchar 0xEF
@tds_data_type_xml 0xF1
@tds_data_type_udt 0xF0
@tds_data_type_tvp 0xF3
@tds_data_type_text 0x23
@tds_data_type_image 0x22
@tds_data_type_ntext 0x63
Expand Down Expand Up @@ -714,9 +716,9 @@ defmodule Tds.Types do
size = byte_size(value)
<<value::little-size(size)-unit(8)>> = value

Decimal.get_context()
Decimal.Context.get()
|> Map.put(:precision, precision)
|> Decimal.set_context()
|> Decimal.Context.set()

case sign do
0 -> Decimal.new(-1, value, -scale)
Expand Down Expand Up @@ -763,6 +765,7 @@ defmodule Tds.Types do
:date -> encode_date_type(param)
:time -> encode_time_type(param)
:uuid -> encode_uuid_type(param)
:tvp -> encode_tvp_type(param)
_ -> encode_string_type(param)
end
end
Expand Down Expand Up @@ -803,6 +806,12 @@ defmodule Tds.Types do
{type, data, []}
end

def encode_tvp_type(%Parameter{}) do
type = @tds_data_type_tvp
data = <<type>> <> <<0, 0, 0>>
{type, data, []}
end

def encode_uuid_type(%Parameter{value: value}) do
length =
if value == nil do
Expand Down Expand Up @@ -887,9 +896,9 @@ defmodule Tds.Types do
end

def encode_decimal_type(%Parameter{value: value}) do
d_ctx = Decimal.get_context()
d_ctx = Decimal.Context.get()
d_ctx = %{d_ctx | precision: 38}
Decimal.set_context(d_ctx)
Decimal.Context.set(d_ctx)

value_list =
value
Expand Down Expand Up @@ -942,9 +951,9 @@ defmodule Tds.Types do
end

def encode_float_type(%Parameter{value: %Decimal{} = value}) do
d_ctx = Decimal.get_context()
d_ctx = Decimal.Context.get()
d_ctx = %{d_ctx | precision: 38}
Decimal.set_context(d_ctx)
Decimal.Context.set(d_ctx)

value_list =
value
Expand Down Expand Up @@ -1034,6 +1043,8 @@ defmodule Tds.Types do
:smalldatetime ->
"smalldatetime"

:tvp -> "#{value.name} readonly"

:binary ->
encode_binary_descriptor(value)

Expand Down Expand Up @@ -1129,9 +1140,9 @@ defmodule Tds.Types do
end

def encode_decimal_descriptor(%Parameter{value: %Decimal{} = dec}) do
d_ctx = Decimal.get_context()
d_ctx = Decimal.Context.get()
d_ctx = %{d_ctx | precision: 38}
Decimal.set_context(d_ctx)
Decimal.Context.set(d_ctx)

value_list =
dec
Expand Down Expand Up @@ -1194,6 +1205,42 @@ defmodule Tds.Types do
@doc """
Data encoding
"""
def encode_column_type(%Column{type: type} = col) when type != nil do
case type do
:varchar -> encode_bigvarchar_col_type(col)
:boolean -> encode_binary_type(%Parameter{type: :boolean, value: nil})
:varbinary -> encode_varbinary_col_type(col)
:int -> encode_integer_type(%Parameter{type: :integer, value: nil})
:decimal -> encode_decimal_type(%Parameter{type: :decimal, value: nil})
:float -> encode_float_type(%Parameter{type: :float, value: nil})
:datetime -> encode_datetime_type(%Parameter{type: :datetime, value: nil})
:smalldatetime -> encode_smalldatetime_type(%Parameter{type: :smalldatetime, value: nil})
:datetime2 -> encode_datetime2_type(%Parameter{type: :datetime2, value: nil})
:datetimeoffset -> encode_datetimeoffset_type(%Parameter{type: :datetimeoffset, value: nil})
:date -> encode_date_type(%Parameter{type: :datetimeoffset, value: nil})
:time -> encode_time_type(%Parameter{type: :time, value: nil})
:uuid -> encode_uuid_type(%Parameter{type: :uuid, value: nil})
end
end

def encode_bigvarchar_col_type(%Column{opts: opts} = _col) do
type = @tds_data_type_bigvarchar
length = Keyword.get(opts, :length, 0)
collation = <<0x00, 0x00, 0x00, 0x00, 0x00>>
bin_length = if length <= 8000, do: <<8000::little-unsigned-16>>, else: <<0xFF, 0xFF>>
data = <<type>> <> bin_length <> collation

{type, data, length: length}
end

def encode_varbinary_col_type(%Column{opts: opts} = _col) do
type = @tds_data_type_bigvarbinary
length = Keyword.get(opts, :length, 0)
bin_length = if length <= 8000, do: <<8000::little-unsigned-16>>, else: <<0xFF, 0xFF>>
data = <<type>> <> bin_length

{type, data, length: length}
end

# binary
def encode_data(@tds_data_type_bigvarbinary, value, attr)
Expand All @@ -1211,7 +1258,54 @@ defmodule Tds.Types do
end
end

#tvp
def encode_data(@tds_data_type_tvp, %{columns: nil}, _attrs),
do: <<0xFF :: little-unsigned-16, 0x00, 0x00 >>

def encode_data(@tds_data_type_tvp, %{columns: columns, rows: rows}, _attrs) do
column_length = <<length(columns) :: little-unsigned-16>>
{column_attrs, column_meta} = Enum.reduce(columns, {[], <<>>}, fn (%Column{} = param, {attrs, acc_bin}) ->
{bin_type, data, attr} = encode_column_type(param)
bin = acc_bin <> <<0x00 :: little-unsigned-32, 0x00 :: little-unsigned-16 >> <> data <> <<0x00>>

{[{bin_type, attr} | attrs], bin}
end)

row_data = Enum.reduce(rows, <<>>, fn (params, row_acc) ->
row_bin = column_attrs
|> Enum.reverse
|> Enum.zip(params)
|> Enum.reduce(<<>>, fn ({{type, attr}, param}, acc) ->
acc <> encode_data(type, param, attr)
end)

row_acc <> << 0x01 >> <> row_bin
end)

column_length <> column_meta <> <<0x00>> <> row_data <> <<0x00>>
end

# string
def encode_data(@tds_data_type_bigvarchar, nil, opts) do
length = Keyword.get(opts, :length, 0)
if length <= 8000,
do: <<65535::little-unsigned-16>>,
else: <<@tds_plp_null::little-unsigned-64>>
end

def encode_data(@tds_data_type_bigvarchar, value, _opts) do
value_size = byte_size(value)
cond do
value_size <= 0 ->
<<0x00::unsigned-64, 0x00::unsigned-32>>
value_size > 8000 ->
encode_plp(value)
true ->
<<value_size::little-size(2)-unit(8)>> <> value
end
end


def encode_data(@tds_data_type_nvarchar, nil, _),
do: <<@tds_plp_null::little-unsigned-64>>

Expand Down Expand Up @@ -1282,9 +1376,9 @@ defmodule Tds.Types do

# decimal
def encode_data(@tds_data_type_decimaln, %Decimal{} = value, attr) do
d_ctx = Decimal.get_context()
d_ctx = Decimal.Context.get()
d_ctx = %{d_ctx | precision: 38}
Decimal.set_context(d_ctx)
Decimal.Context.set(d_ctx)
precision = attr[:precision]

d =
Expand Down
Loading