   deferred queries."
   (-unwrap [_] "Unwrap the object"))
 
+(defprotocol ITableCoerce
+  (-as-table [_ params] "Table coersion."))
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Protocol Implementations
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn val
  [v]
  (-val v))

(defn table
  "Create a table instance."
  [data & [{:keys [alias] :as opts}]]
  (defer
    (let [f (-table data)]
      (if alias
        (.as f (clojure.core/name alias))
        f))))

(defn select
  "Start select statement."

  (-unwrap [self]
    (-unwrap @self)))

(extend-protocol ITableCoerce
  org.jooq.Name
  (-as-table [v selectexp]
    (assert (instance? Select selectexp))
    (.as v selectexp))

  org.jooq.DerivedColumnList
  (-as-table [v selectexp]
    (assert (instance? Select selectexp))
    (.as v selectexp)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Common DSL functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defn field
  "Create a field instance."
  ([v]
   (-field v))
  ([v alias]
   (-> (-field v)
       (.as (clojure.core/name alias)))))

(defn table
  "Create a table instance."
  ([v]
   (-table v))
  ([v alias]
   (-> (-table v)
       (.as (clojure.core/name alias)))))

(defn to-table
  "Coerce querypart to table expression."
  [texp talias & params]
  (defer
    (let [texp (-unwrap texp)
          talias (-unwrap talias)]
      (cond
        (clojure.core/and (satisfies? ITableCoerce texp)
                          (instance? Select talias))
        (-as-table texp talias)

        (instance? TableLike texp)
        (let [talias (clojure.core/name (-unwrap talias))
              params (into-array String (map clojure.core/name params))]
          (.asTable texp talias params))))))

(defn f
  "Create a field instance, specialized on
  create function like field instance."
  [v]
  (-field v))

(defn typed-field
  [data type]
