honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
seancorfield 2021-02-17T03:59:45.176400Z

seancorfield/honeysql {:mvn/version "2.0.0-alpha2"} is available for testing. It's mostly an overhaul and expansion of the documentation around v2 but also includes a number of small bug fixes and improvements, mostly around the insert/upsert syntax for PostgreSQL. Everything in nilenso/honeysql-postgres is built-in for this release and mostly documented (there are some small differences) -- see: https://cljdoc.org/d/seancorfield/honeysql/2.0.0-alpha2/doc/getting-started/postgresql-support and nearly all of the helper functions have docstrings now -- see: https://cljdoc.org/d/seancorfield/honeysql/2.0.0-alpha2/api/honey.sql.helpers

seancorfield 2021-02-17T04:03:05.178300Z

At this point, the core API feels pretty stable to me and the overall shape and scope of the library also feels fairly well "baked" so I think it's mostly a matter of documentation to reach a "gold" 2.0 release, although I'm still ploughing though the open issues from the 2.0 milestone: https://github.com/seancorfield/honeysql/milestone/7

👍 1
😍 2
zackteo 2021-02-17T06:23:16.182100Z

Whoops. I think I might have glanced past that - and I think I might have been trying out how other forms of PostGIS queries might work. Looks like I can use sql/call for the most part then 🙂 and tap into sql/raw and sql/format for finer manipulation

seancorfield 2021-02-17T17:42:02.183400Z

@zackteo Yup, in 2.0 it's easier because you never need sql/call and sql/raw has just become a :raw "function" (and :inline is more powerful now too).

borkdude 2021-02-17T21:42:54.184600Z

Is there an option in honeysql to convert a sqlvec string (with question marks) and values into one string with the values embedded? This might be an anti-pattern but for shelling out I want to use this (just for a demo)

borkdude 2021-02-17T22:01:37.184900Z

:parameterizer
does what I want, I think :)

seancorfield 2021-02-17T22:08:08.185700Z

In 1.x, yes, :parameterizer but in 2.x, you can just pass :inline true to sql/format and it attempts to inline all parameters.

borkdude 2021-02-17T22:10:54.185900Z

wow... I'll try!

borkdude 2021-02-17T22:16:16.188300Z

@seancorfield That worked. With babashka! I'm writing a small demo for a presention. 1) First I show how shitty it is to write manual sql using string functions and send that through java.shell to sqlite. 2) Then I show that you can use honeysql to take care of the sql.

(defn insert-sql [animals]
  (sql/format (-> (h/insert-into :animals)
                  (h/columns :name :scientific_name)
                  (h/values (map (juxt :name :scientific-name)
                                 animals)))
              {:inline true}))

(defn insert-rows! [animals]
  (let [sql (insert-sql animals)
        {:keys [:exit :err]}
        (query (first sql))]
    (when (not (zero? exit))
      (println "Error inserting var!" err)
      (System/exit exit))))

(insert-rows! [{:name "Bison" :scientific-name "Bos gaurus"}
               {:name "Duck"  :scientific-name "Anas Platyrhynchos"}])
3) And then I show how to use the pod-babashka-sqlite3 pod so you don't need the :inline true option but can interact with sqlite as if it's a local library.

1
seancorfield 2021-02-17T22:17:23.189Z

LMK if you run into anything in 2.0.0 that causes problems or you'd like to see enhanced.

borkdude 2021-02-17T22:17:31.189200Z

will do, so far so good

borkdude 2021-02-17T22:19:46.189800Z

I was looking for something around create, I guess most people just do that as a raw string and not using data structures?

borkdude 2021-02-17T22:20:21.190100Z

I don't really need it, I just wondered if there was something

seancorfield 2021-02-17T22:24:19.190300Z

As in create table?

seancorfield 2021-02-17T22:25:32.191100Z

https://cljdoc.org/d/seancorfield/honeysql/2.0.0-alpha2/doc/getting-started#ddl-statements which leads you to https://cljdoc.org/d/seancorfield/honeysql/2.0.0-alpha2/doc/getting-started/postgresql-support (because the DDL stuff all used to be in the nilenso library, but now it is supported in core).

seancorfield 2021-02-17T22:26:23.191800Z

DDL documentation is the first section here: https://cljdoc.org/d/seancorfield/honeysql/2.0.0-alpha2/doc/getting-started/sql-clause-reference and the helpers are all listed here https://cljdoc.org/d/seancorfield/honeysql/2.0.0-alpha2/api/honey.sql.helpers

borkdude 2021-02-17T22:26:46.192400Z

ooh, thanks

seancorfield 2021-02-17T22:27:30.193300Z

You can't -- currently -- add clauses to a create table statement after the column definitions, but that is coming maybe in the next alpha (since I need it for MySQL).

borkdude 2021-02-17T22:30:28.193500Z

Worked.

(defn create-db! []
  (when (not (.exists (io/file db)))
    (query (first
            (sql/format
             (-> (h/create-table :animals)
                 (h/with-columns [[:name :text]
                                  [:scientific-name :text]])))))))

borkdude 2021-02-17T22:31:34.193900Z

So for values in v1 I needed to do:

(h/values (map (juxt :name :scientific-name)
                                 animals))
Does v2 maybe accept maps?

borkdude 2021-02-17T22:32:37.194200Z

eh, I guess it does, reading this in the README now

borkdude 2021-02-17T22:34:31.194700Z

oh yes, it only works when you leave out the h/columns clause, is this expected?

seancorfield 2021-02-17T22:34:52.195200Z

Yes -- because the column names come from the (union of) the hash maps instea.

borkdude 2021-02-17T22:35:10.195700Z

I guess h/columns could act as select-keys maybe?

seancorfield 2021-02-17T22:35:44.196300Z

Maybe? Or maybe throw an exception if the h/columns are not in the hash map keys?

seancorfield 2021-02-17T22:35:50.196600Z

I can see arguments for both.

borkdude 2021-02-17T22:36:46.197Z

yeah, maybe the user should just map a select-keys over the maps first

borkdude 2021-02-17T22:36:56.197300Z

so the current behavior could stay as it is

borkdude 2021-02-17T22:36:59.197500Z

not sure

borkdude 2021-02-17T22:38:55.198Z

but right now it yields invalid sql when you combine columns + maps

borkdude 2021-02-17T22:39:50.198900Z

maybe throw in that case with a message like: you should explicitly format the maps yourself with the desired columns

borkdude 2021-02-17T22:43:00.199500Z

Afk now. Thanks for the improvements so far!

➕ 1