Giter Club home page Giter Club logo

Comments (8)

csummers avatar csummers commented on July 24, 2024

This can be achieved a couple of ways with either Clojure Expressions or implementing your own HugSQL parameter type.

Here's the general idea with a Clojure Expression:
SQL

-- :name select-labeled-identifiers :? :*
/* :require [clojure.string :as string]
            [hugsql.parameters :refer [identifier-param-quote]] */
select
/*~
(string/join ", "
  (for [[field label] (:cols params)]
    (str (identifier-param-quote field options)
      (when label (str " as " label)))))
~*/
from test

Clojure:

(testing "labeled identifiers: col_name as label_name"
    (is (= ["select  a as lbl_a, b as lbl_b, c from test"]
           (select-labeled-identifiers-sqlvec
            {:cols [["a" "lbl_a"]
                    ["b" "lbl_b"]
                    ["c"]]}))))

If you're going to use this kind of thing very often, then implementing a custom HugSQL parameter type is probably worth it.

I'm might consider adding a couple of built-in parameter types to support this pattern (:aliased-identifier and :aliased-identifier-list). Let me think on that.

from hugsql.

csummers avatar csummers commented on July 24, 2024

Here's a different way to do it. This expression does not require the use of identifier-param-quote, but instead rewrites the parameters to use identifier param types referenced by the index in the passed-in vector using HugSQL's Deep Get Param Name feature. And, I put it in a snippet named cols for reuse:

-- :snip cols
/* :require [clojure.string :as string] */
/*~
(string/join ", "
  (map-indexed
    (fn [i [field label]]
      (str ":i:cols." i ".0" 
        (when label (str " as :i:cols." i ".1"))))
    (:cols params)))
~*/

-- :name select-labeled-identifiers :? :*
select :snip:cols from test

Only issue here is that calling the snippet is a little verbose with the cols

(testing "labeled identifiers: col_name as label_name"
    (is (= ["select a as lbl_a, b as lbl_b, c from test"]
           (select-labeled-identifiers-sqlvec
            {:cols (cols {:cols [["a" "lbl_a"]
                                 ["b" "lbl_b"]
                                 ["c"]]})}))))

from hugsql.

csummers avatar csummers commented on July 24, 2024

So, I decided the best way to tackle this was to just extend the existing identifier parameter types to support aliases. This will be in the next release allowing you to pass along vector pairs:

 (is (= ["select id as my_id, name as my_name from test"]
           (identifier-param-list-sqlvec {:columns [["id" "my_id"], ["name" "my_name"]]})))
    (is (= ["select * from test as my_test"]
           (identifier-param-sqlvec {:table-name {"test" "my_test"}})))

from hugsql.

sthomp avatar sthomp commented on July 24, 2024

Thanks for all your help. Really enjoying hugsql so far!

from hugsql.

csummers avatar csummers commented on July 24, 2024

This is released in 0.4.6. Give it a try!

from hugsql.

sthomp avatar sthomp commented on July 24, 2024

Just curious, is there a reason why :table-name uses map notation whereas :columns uses array notation?

from hugsql.

csummers avatar csummers commented on July 24, 2024

Well, you can actually use either, but I copied examples from the test suite here that didn't match. The docs use vectors in both cases and that is the preferred usage: http://www.hugsql.org/#param-identifier

from hugsql.

csummers avatar csummers commented on July 24, 2024

That reminds me...the test suite should not assume column order when using a map.

from hugsql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.