LauJensen / clojureql

ClojureQL is superior SQL integration for Clojure
https://clojureql.sabrecms.com
Eclipse Public License 1.0
285 stars 39 forks source link

Contents of where clause overridden in join #138

Closed hotwoofy closed 6 years ago

hotwoofy commented 11 years ago

We're running the following code, and noticing that in both cases of join ordering, the contents of the where expression is overridden.

user> (def c (select (table nil {:company :c}) (where (= :c.name "MongoDB"))))
#'user/c
user> (def k (select (table nil {:investment :k}) (where (= :k.type "angel"))))
#'user/k
user> (join c k (where (= :k.permalink :c.company_permalink)))
SELECT c.*,k.* FROM company c JOIN (SELECT k.* FROM investment k WHERE (k.type = angel)) AS k ON (k.permalink = c.company_permalink) WHERE (c.name = angel)
user> (join k c (where (= :c.company_permalink :k.permalink)))
SELECT k.*,c.* FROM investment k JOIN (SELECT c.* FROM company c WHERE (c.name = MongoDB)) AS c ON (k.permalink = c.company_permalink) WHERE (k.type = MongoDB)

We would expect:

user> (join c k (where (= :k.permalink :c.company_permalink)))
SELECT c.*,k.* FROM company c JOIN (SELECT k.* FROM investment k WHERE (k.type = angel)) AS k ON (k.permalink = c.company_permalink) WHERE (c.name = MongoDB)
rm-hull commented 11 years ago

The compiled SQL has two placeholders but three trailing parameters:

user=> (compile (join c k (where (= :k.permalink :c.company_permalink))) nil)
["SELECT c.*,k.* FROM company c JOIN (SELECT k.* FROM investment k WHERE (k.type = ?)) AS k ON (k.permalink = c.company_permalink) WHERE (c.name = ?)" "angel" "angel" "MongoDB"]

The problem is somewhere inside the clojureql.core/compile method where the environment is being assembled. I expect it should be producing only one parameter of "angel" rather than two.

https://github.com/LauJensen/clojureql/blob/master/src/clojureql/sql92compiler.clj#L116 and https://github.com/LauJensen/clojureql/blob/master/src/clojureql/sql92compiler.clj#L118 are both contributing (nil "angel" nil) which gets flattened down and nils removed

LauJensen commented 6 years ago

Thanks for posting. Pushed a fix.