honeysql

2024-10-22T18:31:43.309119Z

phronmophobic 2024-10-22T19:08:26.772679Z

I'm working with a sqlite db where the table and column names have "-" in the name (which is my own fault). For the most part, I can use :quoted and it works, but referencing columns with :a-table/a-column doesn't seem to work as expected:

(sql/format
 {:select [:a-table/a-column]
  :from :a-table}
 {:quoted true})
;; ["SELECT \"a_table\".\"a-column\" FROM \"a-table\""]
:a-table is quoted the way I want in the :from clause, but is converted to snake case in the :select clause. This behavior seems to be consistent for other clauses like :left-join and :where. I'm not sure if this is intended or if I'm just doing something goofy. If this isn't intended, I can create an issue on github if that's helpful. Thanks!

seancorfield 2024-10-22T19:16:59.205239Z

:foo/bar and :foo.bar have slightly different semantics (deliberately) but you can do:

user=> (sql/format
 {:select [:a-table.a-column]
  :from :a-table}
 {:quoted true})
["SELECT \"a-table\".\"a-column\" FROM \"a-table\""]

🙌 1
phronmophobic 2024-10-22T19:17:48.685059Z

Awesome! That's great.

phronmophobic 2024-10-22T19:18:00.688839Z

Out of curiousity, what is the difference?

seancorfield 2024-10-22T19:18:10.093129Z

Note that there is also this option:

user=> (sql/format
 {:select [:a-table/a-column]
  :from :a-table}
 {:quoted true :quoted-snake true})
["SELECT \"a_table\".\"a_column\" FROM \"a_table\""]

phronmophobic 2024-10-22T19:18:43.130869Z

In my particular case, I'm trying to avoid snakecase.

seancorfield 2024-10-22T19:22:38.327899Z

It's somewhat historical. Hyphens in column names seemed common enough to make that work for quoting "by default" with :quoted-snake to reverse that, but hyphens in table names were uncommon so the dotted form has one behavior and the / form has another.

phronmophobic 2024-10-22T19:24:10.987799Z

ah ok. I was under the impression that they were interchangeable. I see that is mentioned in the General Reference.

seancorfield 2024-10-22T19:25:27.057659Z

The joys of having so many users with different "preferences" in how they set up their databases... unfortunately 😞

phronmophobic 2024-10-22T19:26:07.015209Z

and there are also a dozen sql dialects with random variations and requirements

phronmophobic 2024-10-22T19:26:11.637139Z

🎉

phronmophobic 2024-10-22T19:27:53.399529Z

I was recently trying h2 for the first time and their version of SQL has several interesting changes that seem arbitrary.

seancorfield 2024-10-22T19:32:03.505029Z

Yeah, and then the behavior of each DB's JDBC driver is different in various ways too.

seancorfield 2024-10-22T19:33:32.228339Z

The jTDS and MS SQL Server drivers behave differently, even when connected to the same SQL Server instance. MariaDB's driver is different to MySQL's -- again, even connected to the same DB. Oracle's driver doesn't implement some things, MS's doesn't implement some other things, PG's issues additional SQL queries behind the scenes (that other drivers do not).

phronmophobic 2024-10-22T21:16:15.128709Z

I'm doing some generic query writing. Is there a way to programmatically construct a fully qualified column name given a table and a column? Obviously, I can use string manipulation to construct a keyword in the form :table.col, but I was wondering if there was already a utility or idiom for this.

seancorfield 2024-10-22T21:25:35.169959Z

https://cljdoc.org/d/com.github.seancorfield/honeysql/2.6.1203/doc/getting-started/sql-special-syntax-#dot- sounds like what you want?

🙏 1
phronmophobic 2024-10-22T21:27:21.609829Z

Yep, thanks!