Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generating query filter dynamically #51

Open
roti opened this issue Oct 2, 2011 · 2 comments
Open

generating query filter dynamically #51

roti opened this issue Oct 2, 2011 · 2 comments

Comments

@roti
Copy link

roti commented Oct 2, 2011

Hi,

I'm trying to generate the filter expression dynamically. It does not seem possible with the current implementation of query. Take following function

(defn filterexpand [params]
(map #(list '= (key %) (val %)) params))

Using it like this is not possible:
(defn search-orders [params]
(let [o (ds/query :kind Order (filterexpand params))]
(map-xml-list o)))

I also tried with a macro version of filterexpand:
(defmacro filterexpand [body]
(let [p (eval body)]
(map #(list '= (key %) (val%)) p)))

Can such usage of query be made possible?

Razvan

@gcv
Copy link
Owner

gcv commented Oct 2, 2011

I can probably support this use case, although it will not use the :filter keyword to query. It would need to use a different keyword, perhaps :eval-filter, because otherwise it'll be impossible to distinguish between the following two uses:

(query :kind Order :filter (= :type "book"))
(query :kind Order :filter (filterexpand params))

I understand the appeal, though, and I'll keep this ticket open as a feature request for a future version. In the meanwhile, if you really need this, you can hack something together: you would need to construct the entire (query ...) expression as a list and eval it.

@roti
Copy link
Author

roti commented Oct 3, 2011

I have tried but ended up in a "Can't eval locals" error. It's very likely you stumble upon the same error.

Anyway, why not make it simple: if no operator recognized after :filter (no =, <, >, etc.), then just assume that there's an expression there that needs evaluation. In other words, just make :eval-filter an else-branch of :filter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants