alexdesousa / ayesql

Library for using raw SQL in Elixir
MIT License
138 stars 14 forks source link

Passing NULL not supported? #9

Closed meoyawn closed 4 years ago

meoyawn commented 4 years ago

Problem: I want to pass NULL (nil) in some queries, and I'm getting :not_found

And I have to duplicate every query where null might be passed:

-- name: check_result
INSERT INTO checks (pid, roundtrip, ts)
VALUES             (:pid, :roundtrip, :ts)

-- name: check_null
INSERT INTO checks (pid, roundtrip, ts)
VALUES             (:pid, NULL, :ts)

There's no mention of nil in README, is this intentional?

BTW postgrex supports nil just fine https://github.com/elixir-ecto/postgrex/blob/master/README.md#data-representation

alexdesousa commented 4 years ago

Hey, thanks for reporting this :)

The problem is AyeSQL tries to find values for every mandatory parameter in the query and treats nil as a missing parameter. Though this shouldn't be the case, as NULL is a valid value in SQL.

The required change seems easy, but I need to check the possible consequences. I will try to patch this as soon as possible.

In the meantime, I have a possible workaround for you:

-- name: null
NULL

-- name: check_result
-- docs: Checks result.
INSERT INTO checks (pid, roundtrip, ts)
VALUES             (:pid, :roundtrip, :ts)

And then you can call this as:

iex> params = [pid: pid, roundtrip: &Queries.null/2, ts: ts]
iex> Queries.check_result(params)

Explanation:

Hope this hepls.

alexdesousa commented 4 years ago

@adelnizamutdinov The fix worked perfectly, so you can ignore my proposed workaround and actually pass nil :) It's available in hex as v0.5.4

Again, thanks for reporting the issue :)

meoyawn commented 4 years ago

wow, that was fast :) already using it, thank you so much!