Thanks Sean, in general it’s easy to rewrite the SQL such so as to avoid names with spaces. I must be missing something though, because I can’t think of a scenario where unreadable Clojure as a query result would be useful at all! I find myself pulling fields from maps in query results quite frequently, so if it’s unreadable as Clojure what good is it? As I said, I may be missing something obvious. In any case it’s good to know there’s “as-modified-maps” however to fix things. Thanks again!
You are the first person to raise the issue of spaces in aliases (and therefore spaces in keywords) in the 8-9 years I've been maintaining clojure.java.jdbc
(and now next.jdbc
) so I can only conclude that "almost no one" does that.
If you really want keywords with spaces, you can always do this:
(def broken-key (keyword "Broken Key"))
(def my-query (jdbc/execute! ds ["select foo as 'Broken Key' from table"]))
(map broken-key my-query)
But, like I say, I've never heard of anyone doing this.
@mchampine Does that help?
Yes! I didn’t realize keywords could have spaces, so I wrongly assumed the result was ‘broken’. I imagine the scenario is pretty rare, since putting spaces in keys/names/identifiers seems to be inviting trouble. E.g. (pr-str (keyword “foo bar”)) => “:foo bar” but (read-string “:foo bar”) => :foo Thanks for your patience with my confusion over this odd corner case.
Personally I would never put spaces in an alias.. I found it in the examples in https://www.sohamkamani.com/blog/2016/07/07/a-beginners-guide-to-sql/ which, for learning purposes, I have translated into examples for Honey, Hug, Korma, java.jdbc, and next.jdbc https://github.com/mchampine/beginners-sql
👀 I think that is the only time I've ever seen someone aliasing stuff to names with spaces!
But, it's true, you can alias expressions or columns to pretty much any string -- so any auto-conversion of those strings to symbols/keywords is going to produce something "weird" in any language.
I'd put it in the category of "Doctor, it's hurts when I do <this>!" and the doctor replies "Well, stop doing <that>!" 🙂
What do you do with the aliases in HoneySQL? It usually has [<expr> :the-alias]
for that so you can't use unreadable keywords there...
Looks like you just use "sensible" aliases there... I'm curious: did you initially try to mirror the aliases with spaces from the beginners guide, or did you just pick valid keywords for the aliases straight away?
I went straight to ‘munged’ keywords (:returndate) not knowing how to make it work otherwise!
1😄