layerware/hugsql

Parameter with false value not found

Closed this issue · 5 comments

For some reason, Clojure Expressions doesn't work when I pass a false keyword to the params map. To explain it better, I am executing the next function:

(s/defn fetch-invoices-filterable :- [schemas/Invoice]
  [db :- ConnectionMap
   parameters :- schemas/SearchParameters]
  (println parameters)
  (execute! db :select-invoices-filterable parameters))

And that function calls this SQL query:

-- :name select-invoices-filterable :? :*
-- :doc Retrieve invoices according to a group of filters.
SELECT
  i.id,
  i.type,
  i.legal_date,
  i.invoice_year,
  i.invoice_month,
  i.code,
  i.comments,
  i.start_date,
  i.end_date,
  i.is_paid,
  i.created_at,
  i.person_id,
  p.first_name AS person_first_name,
  p.legal_uid AS person_legal_uid,
  p.org_representative AS person_org_representative
FROM
  invoices i,
  people p
WHERE
  i.person_id = p.id
--~ (when (:person-identifier params) "AND upper(p.first_name) LIKE :person-identifier")
--~ (when-not (nil? (:org-representative params)) "AND p.org_representative = :org-representative")
--~ (when (:type params) "AND upper(i.type::varchar) LIKE :type")
--~ (when (:invoice-year params) "AND i.invoice_year = :invoice-year")
--~ (when (:invoice-month params) "AND i.invoice_month = :invoice-month")
--~ (when (:code params) "AND upper(i.code) LIKE :code")
--~ (when (:comments params) "AND upper(i.comments) LIKE :comments")
GROUP BY
  i.id,
  p.id
ORDER BY
  :i*:order-by-fields
OFFSET :offset
LIMIT :page-size;

And I am getting this error:

user=> {:org-representative false, :page 1, :page-size 20, :offset 0, :order-by-fields (i.created_at ASC)}
ERROR Parameter Mismatch: :org-representative parameter data not found.
clojure.lang.ExceptionInfo: Parameter Mismatch: :org-representative parameter data not found. {}
    at clojure.core$ex_info.invokeStatic(core.clj:4617)
    at clojure.core$ex_info.invoke(core.clj:4617)
    at hugsql.core$validate_parameters_BANG_.invokeStatic(core.clj:61)
    at hugsql.core$validate_parameters_BANG_.invoke(core.clj:55)
    at hugsql.core$prepare_sql.invokeStatic(core.clj:172)
    at hugsql.core$prepare_sql.invoke(core.clj:160)
    at hugsql.core$db_fn_STAR_$y__10617.doInvoke(core.clj:437)
    at clojure.lang.RestFn.invoke(RestFn.java:445)

In the first line you can see that I am passing the missing keyword org-representative. Also, If I execute the same function with :org-representative true everything works fine.

Any idea on what could be happening?

I forgot to say that I am using version 0.4.1 and Clojure 1.8.0.

That's definitely a bug and one that I introduced with the deep-get parameter feature in 0.4.x. (commit e7d45f1).

I've fixed this in master and will be rolling a 0.4.2 release a bit later today.

Thanks for the report!

Excellent! I will be waiting for that.

I just released 0.4.2. Please confirm this fixes the issue. Thanks!

Curtis, everything works fine now. Thanks!