Giter Club home page Giter Club logo

Comments (7)

csummers avatar csummers commented on July 24, 2024

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.

yatesco avatar yatesco commented on July 24, 2024

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.

henryw374 avatar henryw374 commented on July 24, 2024

+1 for snippet

from hugsql.

csummers avatar csummers commented on July 24, 2024

As of 0.4.0, we have Clojure Expressions and Snippets to compose your SQL. Check'em out!

from hugsql.

xurelo avatar xurelo commented on July 24, 2024

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.

csummers avatar csummers commented on July 24, 2024

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.

xurelo avatar xurelo commented on July 24, 2024

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)

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.