honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
seancorfield 2021-02-11T00:36:41.084Z

Progress report! WITH is back to supporting multiple CTEs (and there's even a test for it); I have "finished" the clause reference mentioned above; I have incorporated all of the SQL portions from the nilenso/honeysql-postgres library (although some of the syntax has ended up a little different (HoneySQL v2's :insert-into supports an alias out of the box so insert-into-as is not needed, and do-update-set! is not needed because HoneySQL v2's :do-update-set clause supports both the simple excluded case and the full SET-like version). See https://github.com/seancorfield/honeysql/blob/v2/test/honey/sql/helpers_test.cljc#L281-L332 for the HoneySQL v2 tests of helpers like the nilenso functions.

1
seancorfield 2021-02-11T00:37:53.085200Z

Next up: add the DDL from nilenso/honeysql-postgres (and document those clauses) and comb through the source and tests in that library to see if there's anything that isn't documented in the README.

seancorfield 2021-02-11T00:39:34.086500Z

Then I'll continue on through the open issues for v2 and documentation and figure out at what point it will be worth cutting an alpha release to clojars. The latest SHA for anyone playing with the :git/url version is e157aec976769e4cfad0e63f0bad665ba5068e01.

dharrigan 2021-02-11T21:02:27.087400Z

So, just getting round to "port" my existing application to honeysql v2. Am I right in thinking that this style is no longer in use (or rather no longer encouraged)...

dharrigan 2021-02-11T21:02:54.087700Z

(-> (select :device-id)
        (helpers/from :trip)
        (where [:= :vrn vrn]
               [:= :tenant-id tenant-id]
               [:>= :start-datetime (db/datetime->sql from)]
               [:<= :end-datetime (db/datetime->sql to)])
        (limit 1)
        sql/format)))

dharrigan 2021-02-11T21:03:01.088Z

and should be replaced with {} maps?

dharrigan 2021-02-11T21:12:25.088200Z

i.e.,

dharrigan 2021-02-11T21:12:28.088500Z

(sql/format
  {:select [:device-id]
   :from [:trip]
   :where [:and [:= :vrn "asd"]
                [:= :tenant-id "tenant-id"]
                [:>= :start-datetime "from"]
                [:<= :end-datetime "to"]]
   :limit 1})
;; ["SELECT device_id FROM trip WHERE (vrn = ?) AND (tenant_id = ?) AND (start_datetime >= ?) AND (end_datetime <= ?) LIMIT ?"
;;  "asd"
;;  "tenant-id"
;;  "from"
;;  "to"
;;  1]

seancorfield 2021-02-11T21:40:08.089Z

honey.sql.helpers has all the helpers you are used to.

seancorfield 2021-02-11T21:44:04.089500Z

@dharrigan

user=> (-> (select :device-id)
  #_=>         (helpers/from :trip)
  #_=>         (where [:= :vrn vrn]
  #_=>                [:= :tenant-id tenant-id]
  #_=>                [:>= :start-datetime (db/datetime->sql from)]
  #_=>                [:<= :end-datetime (db/datetime->sql to)])
  #_=>         (limit 1)
  #_=>         sql/format)
["SELECT device_id FROM trip WHERE (vrn = ?) AND (tenant_id = ?) AND (start_datetime >= ?) AND (end_datetime <= ?) LIMIT ?" "vrn" 42 #inst "2021-02-11T21:43:31.289-00:00" #inst "2021-02-11T21:43:31.289-00:00" 1]
🎉