Skip to content

Commit

Permalink
Add map_nulls_to_nil configuration value added for converting null at…
Browse files Browse the repository at this point in the history
…om in query responses to Elixir nil atom
  • Loading branch information
JustinG721 committed Jun 2, 2021
1 parent 711ec5d commit b617ee8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ config :my_app, MyApp.SnowflakeConnection,
]
```

The odbc driver will, by default, return `:null` for empty values returned from snowflake queries.
This will be converted to `nil` by default by Snowflex. A configuration value `map_nulls_to_nil?`
can be set to `false` if you do not desire this behavior.

Then, in your application module, you would start your connection:

```elixir
Expand Down
6 changes: 6 additions & 0 deletions lib/snowflex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ defmodule Snowflex do
end

defp process_results({:selected, headers, rows}) do
null_to_nil? = Application.get_env(:snowflex, :map_nulls_to_nil?, true)

bin_headers =
headers
|> Enum.map(fn header -> header |> to_string() |> String.downcase() end)
Expand All @@ -86,6 +88,7 @@ defmodule Snowflex do
row
|> elem(index)
|> to_string_if_charlist()
|> map_null_to_nil(null_to_nil?)

Map.put(map, col, data)
end)
Expand All @@ -95,6 +98,9 @@ defmodule Snowflex do
defp to_string_if_charlist(data) when is_list(data), do: to_string(data)
defp to_string_if_charlist(data), do: data

defp map_null_to_nil(:null, true), do: nil
defp map_null_to_nil(data, _), do: data

defp cast_row(row, schema) do
schema
|> struct()
Expand Down

0 comments on commit b617ee8

Please sign in to comment.