Skip to content

Commit

Permalink
Datetime columns now return NaiveDateTime. 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jae Bach Hardie committed Mar 6, 2017
1 parent 0186a93 commit e96b38d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ by adding `mssqlex` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[{:mssqlex, "~> 0.2"}]
[{:mssqlex, "~> 0.3"}]
end
```

Expand Down
7 changes: 3 additions & 4 deletions lib/mssqlex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ defmodule Mssqlex do
10 and 15 and/or scale between 1 and 15: `Decimal` structs.
* bigint, money, decimal and numeric when precision > 15: strings.
* date: `{year, month, day}`
* smalldatetime, datetime, dateime2:
`{{year, month, day}, {hour, minute, sec, 0}}` (fractional second data is
lost due to limitations of the ODBC adapter. To preserve it you can
convert these columns to varchar during selection.)
* smalldatetime, datetime, dateime2: `NaiveDateTime` (note that fractional
second data is lost due to limitations of the ODBC adapter. To preserve it
you can convert these columns to varchar during selection.)
* uniqueidentifier, time, binary, varbinary, rowversion: not currently
supported due to adapter limitations. Select statements for columns
of these types must convert them to supported types (e.g. varchar).
Expand Down
4 changes: 2 additions & 2 deletions lib/mssqlex/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ defmodule Mssqlex.Type do
Transforms `:odbc` return values to Elixir representations.
"""
@spec decode(:odbc.value(), opts :: Keyword.t) :: return_value()
def decode({{_year, _month, _day} = date, {hour, minute, sec}}, _) do
{date, {hour, minute, sec, 0}}
def decode({{_year, _month, _day}, {_hour, _minute, _sec}} = datetime, _) do
NaiveDateTime.from_erl!(datetime)
end

def decode(value, _) when is_float(value) 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 Mssqlex.Mixfile do

def project do
[app: :mssqlex,
version: "0.2.0",
version: "0.3.0",
description: "Adapter to Microsoft SQL Server. Using DBConnection and ODBC.",
elixir: "~> 1.4",
build_embedded: Mix.env == :prod,
Expand Down
2 changes: 1 addition & 1 deletion test/mssqlex/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Mssqlex.QueryTest do

assert {:ok, _, %Result{
num_rows: 1,
rows: [[1, "Jae", {{2017, 1, 1}, {12, 1, 1, 0}}]]}} =
rows: [[1, "Jae", _]]}} =
Mssqlex.query(pid, "SELECT * FROM query_test.dbo.parametrized_query;", [])
end
end
11 changes: 7 additions & 4 deletions test/mssqlex/types_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,21 @@ defmodule Mssqlex.TypesTest do
end

test "smalldatetime as tuple", %{pid: pid} do
assert {_query, %Result{rows: [[{{2017, 1, 1}, {12, 10, 0, 0}}]]}} =
{:ok, value} = NaiveDateTime.new(2017, 1, 1, 12, 10, 0)
assert {_query, %Result{rows: [[^value]]}} =
act(pid, "smalldatetime", [{{2017, 1, 1}, {12, 10, 0, 0}}])
end

test "datetime as tuple", %{pid: pid} do
assert {_query, %Result{rows: [[{{2017, 1, 1}, {12, 10, 0, 0}}]]}} =
{:ok, value} = NaiveDateTime.new(2017, 1, 1, 12, 10, 0)
assert {_query, %Result{rows: [[^value]]}} =
act(pid, "datetime", [{{2017, 1, 1}, {12, 10, 0, 0}}])
end

test "datetime2 as tuple", %{pid: pid} do
assert {_query, %Result{rows: [[{{2017, 1, 1}, {12, 10, 0, 0}}]]}} =
act(pid, "datetime2", [{{2017, 1, 1}, {12, 10, 0, 54}}])
{:ok, value} = NaiveDateTime.new(2017, 1, 1, 12, 10, 0)
assert {_query, %Result{rows: [[^value]]}} =
act(pid, "datetime2", [{{2017, 1, 1}, {12, 10, 0, 0}}])
end

test "date as tuple", %{pid: pid} do
Expand Down

0 comments on commit e96b38d

Please sign in to comment.