honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
nachos 2020-06-18T22:27:33.090100Z

I am getting this weird error where when I try to format an insert query it adds an extra ( before the insert statement.

[(INSERT INTO users (first_name, last_name, email, status) VALUES (?, ?, ?, CAST(? AS user_status)) ON CONFLICT (email) DO UPDATE SET first_name = EXCLUDED.first_name, last_name = EXCLUDED.last_name, status = EXCLUDED.status) abcd@gmail.com_ abcd@gmail.com_ abcd@gmail.com_ unregistered]
This only happens when I am running this in a transaction. When I run it normally this seems to be working fine.
["INSERT INTO users (first_name, last_name, email, status) VALUES (?, ?, ?, CAST(? AS user_status)) ON CONFLICT (email) DO UPDATE SET first_name = EXCLUDED.first_name, last_name = EXCLUDED.last_name, status = EXCLUDED.status"
 "abcd@gmail.com_"
 "abcd@gmail.com_"
 "abcd@gmail.com_"
 "unregistered"]
This is my function
(-> (insert-into :users)
      (columns :first_name :last_name :email :status)
      (values [[first-name
                last-name
                email
                (sql/call :cast (or status "active")
                          :user_status)]])
      (psqlh/upsert (-> (psqlh/on-conflict :email)
                        (psqlh/do-update-set :first_name :last_name :status)))
      sql/format)

seancorfield 2020-06-18T22:38:51.091100Z

@danisam17 HoneySQL does not run queries -- it just builds the SQL -- so how you run the resulting SQL cannot possibly have any impact whatsoever on the syntax of the generated SQL.

seancorfield 2020-06-18T22:40:25.091900Z

There must be something else at play here. What is psqlh in your code above?

nachos 2020-06-18T22:41:55.092600Z

@seancorfield psqlh is the postgres helper for honeysql https://github.com/nilenso/honeysql-postgres

seancorfield 2020-06-18T22:42:17.092900Z

I suspected as much...

seancorfield 2020-06-18T22:46:02.093500Z

I'm trying your code locally and confirmed it produced the right SQL (as expected).

nachos 2020-06-18T22:48:26.094800Z

Yeah, when I directly call the function with the same data it returns the correct SQL. I tried it without the postgres helper and it still returns the query with the extra (

[(INSERT INTO users (first_name, last_name, email, status) VALUES (?, ?, ?, CAST(? AS user_status))) abcd@gmail.com_ abcd@gmail.com_ abcd@gmail.com_ unregistered]

seancorfield 2020-06-18T22:48:53.095100Z

Note what is missing in that.

seancorfield 2020-06-18T22:49:34.095600Z

You are doing something differently between the code that works and the code that does not.

seancorfield 2020-06-18T22:50:56.096600Z

You must require honeysql-postgres.format in order to enable the PG extensions. Without that, you won't get any of the PG clauses expanded -- HoneySQL won't know about them, so it'll ignore them.

seancorfield 2020-06-18T22:51:11.096800Z

user=> sql-data
{:insert-into :users, :columns (:first_name :last_name :email :status), :values [["Sean" "Corfield" "<mailto:sean@corfield.org|sean@corfield.org>" #sql/call [:cast "test" :user_status]]], :upsert {:on-conflict (:email), :do-update-set (:first_name :last_name :status)}}
user=&gt; (sql/format sql-data)
["INSERT INTO users (first_name, last_name, email, status) VALUES (?, ?, ?, CAST(? AS user_status)) " "Sean" "Corfield" "<mailto:sean@corfield.org|sean@corfield.org>" "test"]
user=&gt; (require '[honeysql-postgres.format :as ff])
nil
user=&gt; (sql/format sql-data)
["INSERT INTO users (first_name, last_name, email, status) VALUES (?, ?, ?, CAST(? AS user_status)) ON CONFLICT (email) DO UPDATE SET first_name = EXCLUDED.first_name, last_name = EXCLUDED.last_name, status = EXCLUDED.status" "Sean" "Corfield" "<mailto:sean@corfield.org|sean@corfield.org>" "test"]

seancorfield 2020-06-18T22:51:53.097500Z

But you have something else being done to your data structure in the case that produces the (incorrect) ( .. ) wrapping the SQL.

nachos 2020-06-18T22:54:38.098Z

Hmm.. Not sure what I am missing. Will take a look again. Thanks for the help @seancorfield

seancorfield 2020-06-18T22:56:40.098400Z

@danisam17 Please let us know what you figure out.