                       (name table-name)
                       (if (seq where-cl)
                         (str "where " where-cl) ""))]
-    (with-open [cursor (.rawQuery (.db tagged-db) query nil)]
+    (with-open [^Cursor cursor (.rawQuery (.db tagged-db) query nil)]
       (try (.moveToFirst cursor)
            (get-value-from-cursor cursor 0 type)
            (catch CursorIndexOutOfBoundsException e nil)))))

  ...]`, in which case it will be transformed into `key = value1 OR
  key = value2 ...`. Nested vectors is supported."
  [k v]
  (let [k (name k)]
    (condp #(= % (type %2)) v
      PersistentVector (let [[op & values] v]
                         (->> values
                              (map (partial keyval-to-sql k))
                              (interpose (str " " (name op) " "))
                              string/join))
      String (format "(%s = '%s')" k v)
      Boolean (format "(%s = %s)" k (if v 1 0))
      nil (format "(%s is NULL)" k)
      (format "(%s = %s)" k v))))

;; ### SQL operations


         (interpose " AND ")
         string/join)))

(defn construct-sql-query [select from where]
  (str "select " (string/join (interpose ", " (map qualified-name select)))
       " from " (cond (string? from) from

                      (sequential? from)
                      (string/join (interpose ", " (map name from)))

                      :else (name from))
       (let [wc (where-clause where)]
         (if-not (empty? wc)
           (str " where " wc) ""))))

(defn query
  "Executes SELECT statement against the database and returns a Cursor
  object with the results. `where` argument should be a map of column
  keywords to values."
  ([^TaggedDatabase tagged-db, from where]
   {:pre [(keyword? from)]}
   (let [columns (->> (get-in (.schema tagged-db) [:tables from :columns])
                      keys)]
     (query tagged-db columns from where)))
  ([^TaggedDatabase tagged-db columns from where]
   (.rawQuery (.db tagged-db) (construct-sql-query columns from where) nil)))

(def db-query query)

(defn seq-cursor
  "Turns data from Cursor object into a lazy sequence. Takes database
  argument in order to get schema from it."
  ([^TaggedDatabase tagged-db, table-name cursor]
   (seq-cursor tagged-db (-> (.schema tagged-db) :tables
                             (get table-name) :columns keys)
               table-name cursor))
  ([^TaggedDatabase tagged-db, column-names, from, ^Cursor cursor]
   (.moveToFirst cursor)
   (let [tables (:tables (.schema tagged-db))
         columns (if (keyword? from)
                   (let [table-cls (:columns (get tables from))]
                     (mapv (fn [cl-name]
                             [cl-name (:type (get table-cls cl-name))])
                           column-names))
                   (mapv (fn [^Keyword kw]
                           (let [cl-name (keyword (.getName kw))
                                 cl (-> (get tables (keyword (.getNamespace kw)))
                                        :columns
                                        (get cl-name))]
                             [kw (:type cl)]))
                         column-names))
         seq-fn (fn seq-fn []
                  (lazy-seq
                   (if (.isAfterLast cursor)
                     (.close cursor)
                     (let [v (reduce-kv
                              (fn [data i [column-name type]]
                                (assoc data column-name
                                       (get-value-from-cursor cursor i type)))
                              {} columns)]
                       (.moveToNext cursor)
                       (cons v (seq-fn))))))]
     (seq-fn))))

(defn query-seq
  "Executes a SELECT statement against the database and returns the
  result in a sequence. Same as calling `seq-cursor` on `query` output."
  ([^TaggedDatabase tagged-db table-name where]
   (seq-cursor tagged-db table-name (query tagged-db table-name where)))
  ([^TaggedDatabase tagged-db columns from where]
   (seq-cursor tagged-db columns from
               (query tagged-db columns from where))))
(def db-query-seq query-seq)

(defn query-scalar
