Skip to content

Commit

Permalink
fixing StaleEntity issue when insert () output inserted.id is used in…
Browse files Browse the repository at this point in the history
… ecto query
  • Loading branch information
mjaric committed Oct 6, 2017
1 parent 0bb00ef commit cdc6460
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v1.0.3
* BugFix
* When insert is performed with output incorect row count is calucated. Causing tds_ecto and ecto to think it is StaleEntity

# v0.5.4
* Enhancements
* Cleaned up code style for Elixir 1.2.0 warnings
Expand Down
50 changes: 28 additions & 22 deletions lib/tds/tokens.ex
Original file line number Diff line number Diff line change
Expand Up @@ -176,40 +176,46 @@ defmodule Tds.Tokens do
end
## DONE
defp decode_token(<<@tds_token_done, status::int16, cur_cmd::binary(2), row_count::little-size(8)-unit(8), _tail::binary>>, tokens) do
case tokens do
[done: done] ->

cond do
row_count > done.rows -> {[done: %{status: status, cmd: cur_cmd, rows: row_count}] ++ tokens, nil}
true -> {tokens, nil}
end
case Keyword.get(tokens, :done) do
nil ->
{Keyword.put(tokens, :done, %{status: status, cmd: cur_cmd, rows: row_count}), nil}
%{rows: rows} when row_count > rows ->
{Keyword.put(tokens, :done, %{status: status, cmd: cur_cmd, rows: row_count}), nil}
_ ->
{tokens, nil}
_ -> {[done: %{status: status, cmd: cur_cmd, rows: row_count}] ++ tokens, nil}
end
end
## DONEPROC
defp decode_token(<<@tds_token_doneproc, status::int16, cur_cmd::binary(2), row_count::little-size(8)-unit(8), _tail::binary>>, tokens) do
case tokens do
[done: done] ->
cond do
row_count > done.rows -> {[done: %{status: status, cmd: cur_cmd, rows: row_count}] ++ tokens, nil}
true -> {tokens, nil}
end
case Keyword.get(tokens, :done) do
nil ->
{Keyword.put(tokens, :done, %{status: status, cmd: cur_cmd, rows: row_count}), nil}
%{rows: rows} when row_count > rows ->
{Keyword.put(tokens, :done, %{status: status, cmd: cur_cmd, rows: row_count}), nil}
_ ->
{tokens, nil}
_ -> {[done: %{status: status, cmd: cur_cmd, rows: row_count}] ++ tokens, nil}
end
end
## DONEINPROC
defp decode_token(<<@tds_token_doneinproc, status::int16, cur_cmd::binary(2), row_count::little-size(8)-unit(8), _something::binary-size(5), tail::binary>>, tokens) do
case tokens do
[done: done] ->
cond do
row_count > done.rows -> {[done: %{status: status, cmd: cur_cmd, rows: row_count}] ++ tokens, nil}
true -> {tokens, tail}
end
case Keyword.get(tokens, :done) do
nil ->
{Keyword.put(tokens, :done, %{status: status, cmd: cur_cmd, rows: row_count}), tail}
%{rows: rows} when row_count > rows ->
{Keyword.put(tokens, :done, %{status: status, cmd: cur_cmd, rows: row_count}), nil}
_ ->
{tokens, nil}
_ -> {[done: %{status: status, cmd: cur_cmd, rows: row_count}] ++ tokens, tail}
end

# case tokens do
# [done: done] ->
# cond do
# row_count > done.rows -> {[done: %{status: status, cmd: cur_cmd, rows: row_count}] ++ tokens, nil}
# true -> {tokens, tail}
# end
# {tokens, nil}
# _ -> {[done: %{status: status, cmd: cur_cmd, rows: row_count}] ++ tokens, tail}
# end
end

defp decode_column_order(<<tail::binary>>, n, columns) when n == 0 do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Tds.Mixfile do

def project do
[ app: :tds,
version: "1.0.2",
version: "1.0.3",
elixir: "~> 1.0",
deps: deps(),
test_coverage: [tool: ExCoveralls],
Expand Down
2 changes: 1 addition & 1 deletion test/rpc_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ defmodule RPCTest do
query("DROP TABLE dbo.TestTable2", [])
assert :ok = query("CREATE TABLE TestTable2 (text varbinary(max) NULL)", [])
query("INSERT INTO TestTable2 VALUES (@1)",[%Parameter{name: "@1", value: "hello", type: :binary}])
assert [["hello"]] = query("SELECT * FROM TestTable2 WHERE text IN ('x', 'y', @1)", [%Parameter{name: "@1", value: "hello"}])
assert [["hello"]] = query("SELECT * FROM TestTable2 WHERE text IN ('x', 'y', @1)", [%Parameter{name: "@1", value: "hello", type: :binary}])
end

test "Common Types Null", context do
Expand Down

0 comments on commit cdc6460

Please sign in to comment.