Fork me on GitHub
#honeysql
<
2022-08-03
>
genRaiy15:08:59

I would like to use honeysql to connect to Postgres using nbb, so via ClojureScript - has anyone done this yet?

borkdude15:08:35

honeysql is only for producing SQL queries, it does not make connections

borkdude15:08:19

It should be similar for postgres

genRaiy15:08:21

I know but come on man, I just want to cut n paste :rolling_on_the_floor_laughing:

genRaiy15:08:43

I'll work it out

borkdude15:08:50

I notice the syntax expected by this pg lib is $1 instead of ? what honeysql uses.

genRaiy15:08:58

searching around GH I see that this lib has done it before

genRaiy15:08:16

so I'll take it from there

👍 1
seancorfield16:08:12

Yeah, the Node PG lib doesn't support regular ? placeholders -- and HoneySQL does not produce the numbered parameters. There's a ticket in the backlog for HoneySQL but it turns out to be extremely tricky with the current machinery -- although I have some ideas about making it work. HoneySQL v1 used to support the numbered parameters but almost no one was using it and since it was really handled "under the hood" it didn't matter much.

borkdude16:08:57

I wonder how that async_db.cljs example works then

seancorfield16:08:01

It uses HoneySQL v1 not v2. And it uses the PG extensions library for HoneySQL v1.

borkdude16:08:59

Perhaps it's not difficult to hack around this: replace each ? with a $ + an incremental number

seancorfield16:08:18

In SQL ?? is the escaped version of a ? when you don't want a placeholder and PG has (at least) three four operators that include a ?

seancorfield16:08:27

So you can "hack around" it for simple cases if you know what your SQL is going to contain 🙂

borkdude16:08:35

hehe yeah...

seancorfield16:08:09

Have I said lately just how much I dislike SQL? And how much I particularly like PostgreSQL's extensions to SQL? 😆

borkdude16:08:23

I heard your podcast :)

seancorfield16:08:27

I should go listen to myself rant... maybe...

genRaiy19:08:09

are you suggesting that it's not recommended @U04V70XH6?

genRaiy19:08:29

my uses cases are pretty simple tbh

genRaiy19:08:46

so strings are likely good enough

borkdude20:08:14

@U04V5V0V4 Perhaps this also works:

{:select :$1 :from :$2}

borkdude20:08:57

{:select :$1 :from :$2 :where [[:= :x :$3]]}

borkdude20:08:55

and then you're own your own for passing those arguments in the right order

seancorfield20:08:45

Maybe I'll try to set aside a day or two to add support for it 😞 I sort of know how to go about it (the complication is that you can't guarantee formatting actually happens strictly left-to-right -- so the actual parameters potentially need to be reordered at the end to match the order the $n were actually embedded in the final string).

borkdude20:08:35

So is this $n syntax coming from the library or is it actually a postgres syntax?

seancorfield20:08:38

PostgreSQL supports both ? in SQL strings and $n -- presumably at the JDBC driver level.

seancorfield20:08:06

"all" JDBC drivers support ?. But not all PG drivers across all platforms support ?.

borkdude20:08:25

so JDBC does the ? => $n translation?

seancorfield20:08:22

I'm not sure what you mean by "translation" there.

seancorfield20:08:54

For JDBC, generally you provide a SQL string, with a ? placeholder in the string for each parameter for the statement, in the same matching order.

borkdude20:08:15

if postgres expects $n syntax, but JDBC supports writing ?, does JDBC driver translate this before passing it to PG? Or do drivers convert the string + arguments into something else at the driver level?

seancorfield20:08:49

PG's driver supports ? for JDBC.

seancorfield20:08:44

I don't know that drivers parse the SQL string (I find that extremely unlikely but, hey, databases and drivers are weird). The generic JDBC docs talk about using ?.

seancorfield20:08:34

Under the hood, how each JDBC driver talks to its specific database tends to be proprietary. So "anything" could be happening there.

borkdude20:08:56

After skimming a little bit, my assumption now is that PG only supports the $n syntax natively and JDBC drivers do the ? to $n translation to offer a uniform interface

seancorfield20:08:38

So it's entirely possible that PG's underlying ... yeah, I was just about to say that and that's probably why the PG node driver doesn't bother and just forces users to put $n in their SQL strings.

seancorfield20:08:17

HoneySQL currently "assumes" JDBC even tho' it is technically a clj/s library.

genRaiy05:08:42

Does JavaScript have JDBC? 🥴

alpox21:08:52

Looks like the go-to querybuilder of the node-universe (KnexJS) does in its postgres adapter a simple regex replace: https://github.com/knex/knex/blob/master/lib/dialects/postgres/index.js#L140

seancorfield22:08:16

And what of SQL like this? "SELECT ' ? ' AS answer" To be fair, Apache Derby and HSQLDB both throw exceptions trying to parse this but all the other DBs/drivers I test next.jdbc against are happy with this SQL.

borkdude22:08:22

also the sqlite go library seemed to accept the ? output from honeysql