1. Shantanu Kumar
  2. SQLRat

Commits

shantanuk  committed acea09e

move criteria validation to entity/internal.clj

  • Participants
  • Parent commits 38a8324
  • Branches default

Comments (0)

Files changed (2)

File src/main/clj/org/bituf/sqlrat/entity.clj

View file
     (if *assert-args* (do
                         (assert (fn? f))
                         (assert-as entity-meta EntityMetadata)
-                        (assert (vector? cols))
-                        (assert (clause? where))
-                        (assert (vector? groupby))
-                        (assert (clause? other))))
+                        (assert-criteria {:cols cols       :where where
+                                          :groupby groupby :other other})))
     (let [where-clause (as-clause where)
           other-clause (as-clause other)
           sql-vector   (SELECT (csnames cols)
                          (GROUP-BY (csnames groupby))
                          other-clause)]
       (find-by-sql (fn [entities] (f entities)) entity-meta sql-vector)))
-  ([^EntityMetadata entity-meta
-    ^IPersistentMap {:keys [^IPersistentVector cols    ^IPersistentVector where
-                            ^IPersistentVector groupby ^IPersistentVector other]
-                     :or   {cols    [star-col]     ;; vector of col names
-                            where   (empty-clause) ;; clause
-                            groupby []             ;; vector of expressions
-                            other   (empty-clause) ;; clause
-                            }}]
-    (find-by-criteria as-vector entity-meta
-      {:cols cols :where where :groupby groupby :other other}))
+  ([^EntityMetadata entity-meta ^IPersistentMap criteria]
+    (find-by-criteria as-vector entity-meta criteria))
   ([^EntityMetadata entity-meta]
     (find-by-criteria entity-meta {})))
 
                            groupby []             ;; vector of expressions
                            other   (empty-clause) ;; clause
                            }}]
-  ;; assert only args that are read before find-by-criteria is called
   (if *assert-args* (do
                       (assert-as entity-meta EntityMetadata)
                       (assert (not (nil? id)))
-                      (assert (clause? where))))
+                      (assert-criteria {:cols    cols    :where where
+                                        :groupby groupby :other other})))
   (let [id-clause    (=? (:id entity-meta) id)
         where-clause (if (empty-clause? where) id-clause
                        (AND id-clause where))
                         (assert (fn? f))
                         (assert (vector? entities))
                         (assert-as that-meta EntityMetadata)
-                        (assert (vector? cols))
-                        (assert (clause? where))
-                        (assert (vector? groupby))
-                        (assert (clause? other))
+                        (assert-criteria {:cols    cols    :where where
+                                          :groupby groupby :other other})
                         (assert-same-type-entities entities)))
     ;; actual processing
     (let [entity         (first entities)
       (with-find-by-criteria-results ents that-meta criteria
         (f ents))))
   ([^IPersistentVector entities ^EntityMetadata that-meta
-    ^IPersistentMap {:keys [^IPersistentVector cols    ^IPersistentVector where
-                            ^IPersistentVector groupby ^IPersistentVector other]
-                     :or   {cols    [star-col]     ;; vector of col names
-                            where   (empty-clause) ;; clause
-                            groupby []             ;; vector of expressions
-                            other   (empty-clause) ;; clause
-                            }}]
-    (let [criteria {:cols cols :where where :groupby groupby :other other}
-          rels     (find-rels #(into [] %) entities that-meta criteria)]
-      rels))
+    ^IPersistentMap criteria]
+    (find-rels as-vector entities that-meta criteria))
   ([^IPersistentVector entities ^EntityMetadata that-meta]
     (find-rels entities that-meta {})))
 
   "Find related entities for the given set of entities and return a map of
    entity versus related-entities (see entity-rel-map function for details)."
   ([^IPersistentVector entities ^EntityMetadata that-meta
-    ^IPersistentMap {:keys [^IPersistentVector cols    ^IPersistentVector where
-                            ^IPersistentVector groupby ^IPersistentVector other]
-                     :or   {cols    [star-col]     ;; vector of col names
-                            where   (empty-clause) ;; clause
-                            groupby []             ;; vector of expressions
-                            other   (empty-clause) ;; clause
-                            }}]
-    (entity-rels-map entities (find-rels entities that-meta
-                                {:cols    cols    :where where
-                                 :groupby groupby :other other})))
+    ^IPersistentMap criteria]
+    (entity-rels-map entities (find-rels entities that-meta criteria)))
   ([^IPersistentVector entities ^EntityMetadata that-meta]
     (find-entity-rels-map entities that-meta {})))
 
                             groupby []             ;; vector of expressions
                             other   (empty-clause) ;; clause
                             }}]
+    (if *assert-args* (do
+                        (assert (fn? f))
+                        (assert (map? entity))
+                        (assert (map? rel-entity))
+                        (assert-criteria {:cols    cols    :where where
+                                          :groupby groupby :other other})))
     (let [this-meta       (get-meta entity)
           that-table-map  (dbrel-lookup-by-that-entity (rel-meta entity))
           rel-data        (that-table-map (:name (get-meta rel-entity)))
 (defmacro with-find-siblings-results
   [sibling-entities ^Entity entity ^Entity rel-entity ^IPersistentMap criteria
    & body]
-  `(find-siblings (fn [~sibling-entities] ~@body) ~entity ~rel-entity ~criteria))
+  `(find-siblings (fn [~sibling-entities] ~@body) ~entity ~rel-entity
+     ~criteria))
 
 
 (defn delete-cascade [entity]
   [entity]
   "Delete (cascaded) a given entity"
   (if *assert-args* (do
-                      (assert (not (nil? entity)))
                       (assert (map? entity))))
   (let [rels (rel-meta entity)]
     (doseq [each rels]

File src/main/clj/org/bituf/sqlrat/entity/internal.clj

View file
              entities))))
 
 
+(defn assert-criteria
+  "Assert query criteria"
+  [{:keys [cols where groupby other]}]
+  (assert (vector? cols))
+  (assert (clause? where))
+  (assert (vector? groupby))
+  (assert (clause? other)))