This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-02-17
Channels
- # adventofcode (3)
- # announcements (1)
- # babashka (25)
- # beginners (55)
- # calva (12)
- # cider (40)
- # clj-kondo (13)
- # clojure-australia (2)
- # clojure-dev (11)
- # clojure-europe (67)
- # clojure-france (6)
- # clojure-nl (16)
- # clojure-uk (9)
- # clojuredesign-podcast (9)
- # clojurescript (17)
- # conjure (7)
- # cursive (3)
- # datomic (3)
- # emacs (8)
- # figwheel-main (7)
- # fulcro (21)
- # google-cloud (21)
- # graphql (8)
- # helix (1)
- # honeysql (32)
- # instaparse (2)
- # jobs (2)
- # jobs-discuss (2)
- # meander (80)
- # mount (1)
- # off-topic (25)
- # pathom (31)
- # polylith (1)
- # rdf (24)
- # re-frame (21)
- # reagent (29)
- # releases (1)
- # remote-jobs (1)
- # shadow-cljs (16)
- # slack-help (6)
- # sql (5)
- # tools-deps (23)
- # uncomplicate (2)
- # wasm (2)
- # xtdb (4)
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
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
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
@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).
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)
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.
@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.LMK if you run into anything in 2.0.0 that causes problems or you'd like to see enhanced.
I was looking for something around create
, I guess most people just do that as a raw string and not using data structures?
As in create table
?
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).
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
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).
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]])))))))
So for values in v1 I needed to do:
(h/values (map (juxt :name :scientific-name)
animals))
Does v2 maybe accept maps?Yes -- because the column names come from the (union of) the hash maps instea.
Maybe? Or maybe throw an exception if the h/columns
are not in the hash map keys?
I can see arguments for both.