honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
kwrooijen 2020-07-21T13:14:49.273400Z

Is there any built-in way to double quote table names? With Postgres if you want to create a table named user, you have to wrap it in double quotes. This is because the user table is used internally by Postgres. Currently I'm quoting tables myself before running the execution command

2020-07-21T13:44:38.274100Z

@kevin.van.rooijen did you try? (sql/format :quoting :ansi)

👍 1
dharrigan 2020-07-21T13:45:05.274300Z

don't create a table called user 😉

dharrigan 2020-07-21T13:45:31.274700Z

in a similiar vein, don't create a table called group

dharrigan 2020-07-21T13:46:06.275400Z

Those two are about the only exceptions I make when keeping table names singular. I have a users table and a groups table. All my other tables are singluar.

dharrigan 2020-07-21T13:46:31.275900Z

It will help in the long term, for you won't have to remember to quote your queries every single time.

kwrooijen 2020-07-21T13:46:44.276100Z

Sadly, that doesn't work

(-> (q/select :*)
    (q/from :user)
    (sql/format {:quoting :ansi}))
;; => ["SELECT * FROM user"]

😞 1
kwrooijen 2020-07-21T13:47:39.277Z

I don't have to remember, since all my queries go through a single pipeline. My setup currently works fine, but if there's a built-in way to manage it in honeysql then I'd rather have that

dharrigan 2020-07-21T13:48:17.277800Z

sure, whatever works for you 🙂 But, you're forgetting you may not only be the only user of that database in the future....

kwrooijen 2020-07-21T13:49:00.278300Z

That's true, guess I'm just selfish like that 😛

kwrooijen 2020-07-21T13:49:09.278500Z

But you make a valid point

kwrooijen 2020-07-21T13:51:52.279100Z

Maybe I can solve this in a more practical way, thanks

dharrigan 2020-07-21T13:53:15.279300Z

🙂

kwrooijen 2020-07-21T14:05:22.279400Z

oh wow, format doesn't take a map 🙂

kwrooijen 2020-07-21T14:06:23.279600Z

(-> (q/select :*)
    (q/from :user)
    (sql/format :quoting :ansi))
;; => ["SELECT * FROM \"user\""]
I've been doing something seriously wrong haha

kwrooijen 2020-07-21T14:07:36.280500Z

Apparently :quoting ansi works fine if you don't mess it up like I did haha

kwrooijen 2020-07-21T14:14:30.280700Z

Thanks

seancorfield 2020-07-21T16:05:00.280900Z

In some databases, those plural names will collides with system variables etc. You're better off sticking to singular everywhere and using quoting as appropriate.

👍 1
dharrigan 2020-07-21T18:01:00.281200Z

With respect, I disagree - user and group are reserved words and thus should be avoided (in fact, I came across a table today that had a column named desc). Why make lives difficult? Perhaps finding another name would be appropriate, that may be related to the problem domain, like shopper or customer or buyer - trying to avoid reserved words is a good thing, imho 🙂

seancorfield 2020-07-21T18:03:35.281400Z

The inconsistency of two possible plural tables when everything else is singular is bad -- especially since users will bite you on some databases, so your advice isn't even universal.

dharrigan 2020-07-21T18:09:02.281600Z

Instead, with the approach you advocate, you are pushing the problem further down the line, for every single integrator to the database will have to know to quote the tables - and where does it stop? Why not call columns desc and quote them too?

seancorfield 2020-07-21T18:17:12.281800Z

Why not consistently quoted everywhere? Which is essentially what happens for next.jdbc.sql functions (and their equivalents in c.j.j) and if you're using HoneySQL, you just add :quoting :ansi to the format calls (or whatever DB you're using).

seancorfield 2020-07-21T18:18:11.282Z

It's far better to be consistent in your data model than arbitrarily bend your data model to fit whatever weird rules your particular database has. Especially since different DBs have different rules 😐