Fork me on GitHub
#honeysql
<
2023-02-21
>
nigel22:02:52

Is there are way to quote a field or column only in special edge cases? Say I have a quote-for-safety? fn that returns a bool if a word contains certain special characters. I suppose I could register a new dialect and create a new :quote fn that only quotes conditionally. Alternatively, is there any way to map onto the just the keywords and columns of a honeysql expression?

seancorfield22:02:42

@U0ZCYC77T Can you explain what problem you are trying to solve? Or is this just a preference in terms of the generated SQL?

nigel23:02:34

@U04V70XH6 Here's the specific problem I'm running into:

; I want this quoting behavior, but with :sqlserver quotes
(sql/format {:select [:some##field :another-field]})
;; => ["SELECT \"some##field\", another_field"]
;; 
;; passing a dialect quotes everything
(sql/format {:select [:some##field :another-field]} {:dialect :sqlserver})
;; => ["SELECT [some##field], [another-field]"]
;; 
;; turning quoting off passes the first field unquoted
(sql/format {:select [:some##field :another-field]} {:dialect :sqlserver :quoted false})
;; => ["SELECT some##field, another_field"]
using com.github.seancorfield/honeysql {:mvn/version "2.4.969"}

seancorfield23:02:25

Right, but what problem are you trying to solve? Quoting all the fields is not a problem in itself.

seancorfield23:02:40

That's why I asked if this is just preference on your part?

seancorfield23:02:46

You can set the (default) dialect globally -- is that what you're asking about?

seancorfield23:02:26

Was that the answer? You wanted the default dialect to be SQL Server, rather than ANSI?

nigel23:02:34

Oh, sorry. I'm migrating a codebase from HoneySQL 1 -> 2, was just trying to match the existing behavior. I'm actually not sure if quoting everything will cause a problem in our case, I'll try that.

nigel23:02:47

And, I just figured out what I needed:

(sql/format {:select [:some##field :another-field]} {:dialect :sqlserver})
;; => ["SELECT [some##field], [another-field]"]
;;
(sql/format {:select [:some##field :another-field]} {:dialect :sqlserver :quoted nil})
;; => ["SELECT [some##field], another_field"]

nigel23:02:05

:quoted nil restores the default quoting strategy (only quoting unusual entity names) Many thanks @U04V70XH6

seancorfield23:02:41

You have multiple DB dialects in play? Else you could just set one as the global default:

user=> (require '[honey.sql :as h])
nil
user=> (h/set-dialect! :sqlserver)
nil
user=> (h/format {:select [:some##field :another-field]})
["SELECT [some##field], another_field"]
user=>

nigel23:02:24

We do, we're using honey.sql in a data warehouse coordinator system

2
nigel23:02:34

ETL from a bunch of source DBs

nigel23:02:25

Thanks again!

seancorfield23:02:04

I'll create an issue to review the docs around the :dialect option and make sure it's clear that you can explicitly use the "default quoting" via :quoted nil

nigel23:02:21

I'm happy to work on that

seancorfield23:02:39

If you want to send a PR for it, that would be great since I may not get to it for a few weeks.

nigel23:02:38

Will be happy to.

2