Comments (7)
We might could have SQL snippets identified in the SQL file with a -- :snippet
special comment (as opposed to -- :name
), or using some inline snippet notation within a regular query. A Snippet Parameter Type would integrate snippet function results passed as parameter data into the regular query functions.
from hugsql.
Hey, that is great. Ideally I would have one snippet
for 'restrict by X' another for 'restrict by Y', 'restrict by Z' etc. and a fully formed query. I could then 'attach' the relevant 'snippets' to the query based on the runtime data. If they only restrict by 'Y' then I 'attach' the 'restrict by X' snippet to the query?
For clarity - in my current Honey SQL implementation I have something like the following:
(defn restrict-by-x [filter m] (if (:x filter) (add-where m [:= :x (:x filter)]) m)
(defn restrict-by-y [filter m] (if (:y filter) (add-where m [:= :y (:y filter)]) m)
(defn restrict-by-z [filter m] (if (:z filter) (add-where m [:= :z (:z filter)]) m)
(defn filter->sql [filter]
(-> (base-query filter)
(restrict-by-x filter)
(restrict-by-y filter)
(restrict-by-z filter))
No, of course I don't really do that, but I hope it clarifies the intent :-)
from hugsql.
+1 for snippet
from hugsql.
As of 0.4.0, we have Clojure Expressions and Snippets to compose your SQL. Check'em out!
from hugsql.
Hello guys,
I'm not sure how the snippets work:
-- :snip test-snip
select * from store.contents
-- :name test-contents :? :*
:snip:test-snip limit 10
The following error is shown: Parameter Mismatch: :test-snip parameter data not found
The sql queries are parsed by means of: hugsql/map-of-db-fns-from-string.
I am pretty sure I am missing something, but I do not know what it is.
from hugsql.
Snippets create functions in your namespace that you must provide in your larger query function's parameters:
(test-contents db {:test-snip (test-snip)}
Read through https://www.hugsql.org/#using-snippets for more, and check out https://www.hugsql.org/#using-expressions for runtime composability.
from hugsql.
Thanks for the quick answer, but I can't get it working.
Should I do any ns-resolve with the name of the snippet in order to resolve it? I have tried (ns-resolve 'mynamespace (symbol "test-snip")) but I get a nil.
from hugsql.
Related Issues (20)
- 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
- Doesn't work with AOT compilation
- Migrations DSL HOT 2
- Non positional insert-multi HOT 6
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.