 
 (defn delete
   [t]
-  (DSL/delete (table* t)))
+  (defer
+    (DSL/delete (table* t))))
 
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; DDL


(defn on
  [q & clauses]
  (->> (map condition* clauses)
       (into-array org.jooq.Condition)
       (.on q)))

(defn where
  "Create where clause with variable number
  of conditions (that are implicitly combined
  with `and` logical operator)."
  [q & clauses]
  (->> (map condition* clauses)
       (into-array org.jooq.Condition)
       (.where q)))

(defn exists
  "Create an exists condition."
  [select']
  (DSL/exists select'))

(defn group-by
  [q & fields]
  (->> (map field* fields)
       (into-array org.jooq.GroupField)
       (.groupBy q)))

(defn having
  "Create having clause with variable number
  of conditions (that are implicitly combined
  with `and` logical operator)."
  [q & clauses]
  (->> (map condition* clauses)
       (into-array org.jooq.Condition)
       (.having q)))

(defn order-by
  [q & clauses]
  (->> (map sort-field* clauses)
       (into-array org.jooq.SortField)
       (.orderBy q)))

(defn for-update
  [q & fields]
  (let [q (.forUpdate q)]
    (if (seq fields)
      (->> (map field* fields)
           (into-array org.jooq.Field)
           (.of q))
      q)))

(defn limit
  "Creates limit clause."
  [q num]
  (.limit q num))

(defn offset
  "Creates offset clause."
  [q num]
  (.offset q num))

(defn union
  [& clauses]
  (reduce (fn [acc v] (.union acc v))
          (first clauses)
          (rest clauses)))

(defn union-all
  [& clauses]
  (reduce (fn [acc v] (.unionAll acc v))
          (first clauses)
          (rest clauses)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Logical operators (for conditions)


  org.jooq.impl.Val
  (field* ^org.jooq.Field [v] v))

  ;; Deferred
  ;; (field* ^org.jooq.Field [v] @v))

(extend-protocol ISortField
  java.lang.String
