Skip to content

Commit

Permalink
Formatting improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Jae Bach Hardie committed Mar 3, 2017
1 parent b26a067 commit 6598abc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
1 change: 0 additions & 1 deletion lib/mssqlex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ defmodule Mssqlex do
"""
@spec start_link(Keyword.t) :: {:ok, pid}
def start_link(opts) do

opts = opts
|> Keyword.put_new(:odbc_driver, "{ODBC Driver 13 for SQL Server}")
|> Keyword.put_new(:hostname, System.get_env("MSSQL_HST") || "localhost")
Expand Down
34 changes: 28 additions & 6 deletions lib/mssqlex/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,25 @@ defmodule Mssqlex.Type do
"""
@spec encode(value :: param(), opts :: Keyword.t) ::
{:odbc.odbc_data_type(), [:odbc.value()]}
def encode(value, _) when is_boolean(value), do: {:sql_bit, [value]}
def encode(value, _) when is_boolean(value) do
{:sql_bit, [value]}
end

def encode({_year, _month, _day} = date, _) do
encoded = Date.from_erl!(date)
|> to_string
|> to_charlist
{{:sql_varchar, length(encoded)}, [encoded]}
end

def encode({hour, minute, sec, usec}, _) do
precision = if usec == 0, do: 0, else: 6
encoded = Time.from_erl!({hour, minute, sec}, {usec, precision})
|> to_string
|> to_charlist
{{:sql_varchar, length(encoded)}, [encoded]}
end

def encode({{year, month, day}, {hour, minute, sec, usec}}, _) do
precision = if usec == 0, do: 0, else: 6
encoded = NaiveDateTime.from_erl!(
Expand All @@ -54,17 +59,21 @@ defmodule Mssqlex.Type do
|> to_charlist
{{:sql_varchar, length(encoded)}, [encoded]}
end

def encode(value, _) when is_integer(value) do
{:sql_integer, [value]}
end

def encode(value, _) when is_float(value) do
encoded = value |> to_string |> to_charlist
{{:sql_varchar, length(encoded)}, [encoded]}
end

def encode(%Decimal{} = value, _) do
encoded = value |> to_string |> to_charlist
{{:sql_varchar, length(encoded)}, [encoded]}
end

def encode(value, _) when is_binary(value) do
case :unicode.characters_to_binary(value, :unicode, :latin1) do
{_, _, _} ->
Expand All @@ -81,8 +90,11 @@ defmodule Mssqlex.Type do
{{:sql_varchar, length(latin1)}, [latin1]}
end
end
def encode(value, _), do: raise %Mssqlex.Error{
message: "could not parse param #{inspect value} of unrecognised type."}

def encode(value, _) do
raise %Mssqlex.Error{
message: "could not parse param #{inspect value} of unrecognised type."}
end

@doc """
Transforms `:odbc` return values to Elixir representations.
Expand All @@ -91,14 +103,24 @@ defmodule Mssqlex.Type do
def decode({{_year, _month, _day} = date, {hour, minute, sec}}, _) do
{date, {hour, minute, sec, 0}}
end
def decode(value, _) when is_float(value), do: Decimal.new(value)

def decode(value, _) when is_float(value) do
Decimal.new(value)
end

def decode(value, opts) when is_binary(value) do
if opts[:preserve_encoding] do
value
else
:unicode.characters_to_binary(value, {:utf16, :little})
end
end
def decode(value, _) when is_list(value), do: to_string(value)
def decode(value, _), do: value

def decode(value, _) when is_list(value) do
to_string(value)
end

def decode(value, _) do
value
end
end

0 comments on commit 6598abc

Please sign in to comment.