Fork me on GitHub
#beginners
<
2016-05-14
>
hoopes11:05:18

hi - i have a honeysql question (there doesn’t appear to be a room for it - if there’s a better room for this question, please let me know). I’m trying to slowly build a query, and using merge-where, but i’m not sure i’m using it right. it recursively nests the where clauses, where i’d rather see them all ANDed together at the “root” of the query, so to speak - is there something I’m missing?

user=> (require '[honeysql.core :as sql])
user=> (require '[honeysql.helpers :as h])
user=> (def query {:select [:*] :from [:mytable]})
user=> (def w1 (h/merge-where query [:= :a 1]))
user=> (def w2 (h/merge-where w1 [:= :b 2]))
user=> (def w3 (h/merge-where w2 [:= :c 3]))
user=> w3
{:select [:*], :from [:mytable], :where [:and [:and [:= :a 1] [:= :b 2]] [:= :c 3]]}
user=> (sql/format w3)
["SELECT * FROM mytable WHERE ((a = 1 AND b = 2) AND c = 3)”]

hoopes11:05:24

I know that logically, it doesn’t make any difference, but as I build more complex thing, I’m a little nervous of some little subtle thing I miss, and a simpler query would make more sense to me - thanks in advance for any advice!

govind11:05:31

where will combine multiple clauses together using and:

(-> (select :*)
    (from :foo)
    (where [:= :a 1] [:< :b 100])
    sql/format)
=> ["SELECT * FROM foo WHERE (a = 1 AND b < 100)”]

govind11:05:50

Hope this is what you are looking for.

hoopes11:05:25

hey thanks - yeah, i got that far, i’m trying to pass the query map around among a bunch of functions, adding on clauses as i go (“is there a :b param? add that to the query - is there a :c param? add that to the query” - etc

hoopes11:05:53

so i thought in each function, i could add another clauses to the :where in the map, and they would all be ANDed together at the end

hoopes11:05:37

so maybe i should just build up a list of clauses as i go, and add it to the query map at the end?

agi_underground18:05:43

hi everyone! what kind of path i need to set up for hiccup image? relative or absolute or else? [:img {:style "width:1366px;height:180px;", :alt "myImg", :src "/resources/public/img/image.png"}]

byron-woodfork18:05:54

I'm trying to find documentation on this bit of code ['in [1 2 3 4]] .. The 'in. I'm not sure what this is called nor what it does. Any links/suggestions?

arrdem18:05:20

byronwoodfork: ' is quote.

arrdem18:05:05

(quote e) is actually a compiler special form which yields e unevaluated.

arrdem18:05:43

So normally if you write a symbol, such as in, it's evaluated which means that you don't get in back, you get the value bound to in.

arrdem18:05:21

But if you quote in, you get the symbol in back. (eval (quote e)) <=> e

arrdem18:05:41

does that make sense?

byron-woodfork19:05:09

It does. Reading through the documentation and playing with it in the repl now. Thanks! 😃

agi_underground19:05:23

please show someone how to define hiccup image element

nha22:05:55

@agi_underground: I would start with an absolute path to be sure it works and experiment from there, other than that relative or absolute is your preference (exactly the same as if you were writing html). Also hiccup tests are always a nice reference when in doubt (or your REPL also) https://github.com/weavejester/hiccup/blob/53057de9799b27d5d0ddfe865fdac6e7a141d896/test/hiccup/core_test.clj#L69 .