Comments (8)
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.
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.
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.
Thanks for all your help. Really enjoying hugsql so far!
from hugsql.
This is released in 0.4.6. Give it a try!
from hugsql.
Just curious, is there a reason why :table-name
uses map notation whereas :columns
uses array notation?
from hugsql.
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.
That reminds me...the test suite should not assume column order when using a map.
from hugsql.
Related Issues (20)
- Taking array as an input
- Not able to update multiple column values
- Optional null parameters HOT 1
- Way to get generated SQL without executing? HOT 1
- Add support for next.jdbc.sql/insert-multi! HOT 1
- Avoid calling apply in 'hugsql.adapter.clojure-java-jdbc' HOT 5
- Incorrect Arities Shown in Docs for def-fb-fns HOT 1
- HugSql fails during AOT compilation with depstar HOT 4
- Switch to newer groupId of next.jdbc dependency in hugsql-adapter-next-jdbc HOT 1
- How to use a function from the database? HOT 2
- Migration fails if a comment is present on line with semicolon HOT 2
- Babashka support HOT 2
- No comments in doc on mobile? HOT 1
- Using classpath resource sql functions breaks cider HOT 3
- Hugsql 0.5.3 still requires SQL files on uberjar compilation (with leiningen) HOT 1
- strange key with jsonb HOT 3
- Should hugsql switch to next.jdbc by default ? HOT 2
- Cannot look up by UUID HOT 2
- Bulk update? HOT 1
- Inline parameters
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hugsql.