From b617ee87c9b48cac24116988b5eeb5883d52b757 Mon Sep 17 00:00:00 2001 From: JustinG721 <=> Date: Wed, 2 Jun 2021 14:19:27 -0400 Subject: [PATCH] Add map_nulls_to_nil configuration value added for converting null atom in query responses to Elixir nil atom --- README.md | 4 ++++ lib/snowflex.ex | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/README.md b/README.md index 58621c2..6683ec0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/snowflex.ex b/lib/snowflex.ex index ec9f552..7526aa9 100644 --- a/lib/snowflex.ex +++ b/lib/snowflex.ex @@ -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) @@ -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) @@ -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()