Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix compound-key boundary condition generation

  • Loading branch information...
commit 8506b5c4f2730808a441080ab4a6128c941bda6b 1 parent ddf59c0
@mccraigmccraig authored
View
1  .gitignore
@@ -9,3 +9,4 @@ pom.xml
.lein-deps-sum
.lein-failures
.lein-plugins
+pom.xml.asc
View
2  project.clj
@@ -1,4 +1,4 @@
-(defproject qseq "0.2.2"
+(defproject qseq "0.3.0"
:description "qseq: a lazy sequence for simply and efficiently consuming Korma and ClojureQL queries"
:url "http://github.com/mccraigmccraig/qseq"
:license {:name "Eclipse Public License"
View
23 src/qseq/key.clj
@@ -9,12 +9,24 @@
">" :asc
">=" :asc})
+(def boundary-excluding-operators
+ "if a query is boundary-inclusive with a compound key, all of the
+ clauses but the final clause must use the corresponding boundary-excluding operator"
+ {"<" '<
+ "<=" '<
+ ">" '>
+ ">=" '>})
+
(defn boundary-query-sort-direction
"return the sort direction to use such that the first row of a bounded query will
return the boundary value of the key under the operator"
[operator]
(boundary-query-sort-directions (name operator)))
+(defn boundary-excluding-operator
+ [operator]
+ (boundary-excluding-operators (name operator)))
+
(def inclusion-operators-for-traversal-directions
"inclusion operators for different sort directions"
{"asc" '<=
@@ -30,10 +42,13 @@
"expand conditions restricting (operator key boundary). eq-conds accumulates equal conditions
for compound keys"
[operator eq-conds [key & next-keys] [boundary & next-boundaries]]
- (cons
- `(~'and ~@eq-conds (~(-> operator name symbol) ~key ~boundary))
- (if (not-empty next-keys)
- (compound-key-conditions operator (conj eq-conds `(~'= ~key ~boundary)) next-keys next-boundaries))))
+ (let [bxo (boundary-excluding-operator operator)]
+ (cons
+ (if (not-empty next-keys)
+ `(~'and ~@eq-conds (~(-> bxo name symbol) ~key ~boundary))
+ `(~'and ~@eq-conds (~(-> operator name symbol) ~key ~boundary)))
+ (if (not-empty next-keys)
+ (compound-key-conditions operator (conj eq-conds `(~'= ~key ~boundary)) next-keys next-boundaries)))))
(defn key-condition
"given an operator and a simple or compound key and corresponding boundary, return query conditions for records
View
4 test/qseq/core_test.clj
@@ -59,10 +59,10 @@
"SELECT foo.* FROM foo WHERE (((foo.id <= 10)))"
(cql-qstr (q-bounded (q/table :foo) :key [:bar :baz] :boundary [10 20])) =>
- "SELECT foo.* FROM foo WHERE (((foo.bar <= 10)) OR ((foo.bar = 10) AND (foo.baz <= 20)))"
+ "SELECT foo.* FROM foo WHERE (((foo.bar < 10)) OR ((foo.bar = 10) AND (foo.baz <= 20)))"
(k-qstr (q-bounded (k/select* :foo) :key [:bar :baz] :boundary [10 20])) =>
- "SELECT \"foo\".* FROM \"foo\" WHERE ((\"foo\".\"bar\" <= ?) OR (\"foo\".\"bar\" = ? AND \"foo\".\"baz\" <= ?))")
+ "SELECT \"foo\".* FROM \"foo\" WHERE ((\"foo\".\"bar\" < ?) OR (\"foo\".\"bar\" = ? AND \"foo\".\"baz\" <= ?))")
(with-default-transactor (fn [f] (f))
View
11 test/qseq/key_test.clj
@@ -14,16 +14,15 @@
(inclusion-operator-for-traversal-direction "asc") => '<=
(inclusion-operator-for-traversal-direction 'desc) => '>=)
-
(fact
(compound-key-conditions :<= nil [:foo] [10]) => '((and (<= :foo 10)))
- (compound-key-conditions :<= nil [:foo :bar] [10 20]) => '((and (<= :foo 10))
- (and (= :foo 10) (<= :bar 20))))
+ (compound-key-conditions :<= nil [:foo :bar] [10 20]) => '((and (< :foo 10))
+ (and (= :foo 10) (<= :bar 20))))
(fact
(key-condition :<= :foo 10) => '(or (and (<= :foo 10)))
- (key-condition :<= [:foo :bar] [10 20]) => '(or (and (<= :foo 10))
+ (key-condition :<= [:foo :bar] [10 20]) => '(or (and (< :foo 10))
(and (= :foo 10) (<= :bar 20)))
- (key-condition :<= [:foo :bar :baz] [10 20 30]) => '(or (and (<= :foo 10))
- (and (= :foo 10) (<= :bar 20))
+ (key-condition :<= [:foo :bar :baz] [10 20 30]) => '(or (and (< :foo 10))
+ (and (= :foo 10) (< :bar 20))
(and (= :bar 20) (= :foo 10) (<= :baz 30))))
Please sign in to comment.
Something went wrong with that request. Please try again.