Fork me on GitHub
#sql
<
2019-03-05
>
kenny00:03:22

Why does this result in an error:

(require '[honeysql.core :as sql])

(jdbc/query
  db-spec-user
  (sql/format
    {:select [:*]
     :from   ["fruit"]}))
Execution error (PSQLException) at org.postgresql.core.v3.QueryExecutorImpl/receiveErrorResponse (QueryExecutorImpl.java:2440).
ERROR: syntax error at or near "$1"
  Position: 15
And this doesn't:
(jdbc/query
  db-spec-user
  (sql/format
    {:select [:*]
     :from   [:fruit]}))
I assume it's because passing in a string implies a different meaning sometimes. Shouldn't passing a string in the context of the :from work though?

kenny00:03:26

It's really the difference between (jdbc/query db-spec ["SELECT * FROM ?" "fruit"]) and (jdbc/query db-spec ["SELECT * FROM fruit"]).

hiredman00:03:27

I don't know, but that won't stop me from speculating wildly

hiredman00:03:18

honeysql transforms the datastructure you give it in to the standard query + params vector that clojure.java.jdbc uses

hiredman00:03:45

oh, right, you already skipped past all that part

seancorfield00:03:55

It's not legal SQL to parameterize on the name of the table.

☝️ 5
seancorfield00:03:30

HoneySQL assumes keywords correspond to literal SQL names (tables, columns) but strings are "values" that should be parameterized.

kenny01:03:45

Oh, ok. I assume the design decision was that that definition applies to all parts of the query, even though it doesn't make sense for particular parts of the query.

seancorfield01:03:23

Yes, rather than have special rules for different parts of the data structure, it's a lot simpler to have consistent rules -- since users can extend the DSL.