r0man/sqlingvo

respect string in "as" function

Closed this issue · 3 comments

If I use the as function, I want to be able to supply a string instead of a keyword: (as :first-name "first___name") and have it completely respect whatever I put in and not deal with the keyword underscore/hyphenation rules for a special case.

Right now the SQL gets rendered as a blank space like: column AS ,.

r0man commented

@AlJohri You can now use a string with the as expression [1]. This doesn't solve your problem directly, but, if you provide a custom :sql-name function and do something with identifiers (like underscore) you can handle this now yourself, like this:

(defn underscore [s]
  (cond
    (keyword? s)
    (str/replace (name s) "-" "_")
    (string? s)
    s))

(def my-db (sql/db :postgresql {:sql-name underscore}))

(sql/select my-db [(sql/as 1 :alias-1)
                   (sql/as 2 "alias-2")])
;=> ["SELECT 1 AS \"alias_1\", 2 AS \"alias-2\""]

So, the knobs are there to customize it. I'm not sure yet if this should be included in this library by default.

[1] https://github.com/r0man/sqlingvo/pull/81/files#diff-1ca990d05b86c975df32f50e5edb0738

@r0man thanks! this worked perfectly!

[SELECT "original"->? AS "_id", "original"->? AS "date", "original"->? AS "analyzed-date", "original"->? AS "meta", "original"->? AS "topics" FROM "articles" "a" LIMIT 1 OFFSET 0 _id date analyzed-date meta topics]

AlJohri/clojure-postgres-test@09864e1

what hesitation do you have to adding this? seems backward compatible and a useful feature

r0man commented

@AlJohri Fixed in 0.9.5.