Back

row-number (clj)

(source)

protocol

(row-number this)
Return the current 1-based row number, if available. Should not cause any row realization.

Examples

next-jdbc
  What's left to be tested:
  * ReadableColumn protocol extension point"
  (:require [clojure.core.protocols :as core-p]
            [clojure.datafy :as d]
            [clojure.string :as str]
            [clojure.test :refer [deftest is testing use-fixtures]]
            [next.jdbc.protocols :as p]
            [next.jdbc.result-set :as rs]
            [next.jdbc.specs :as specs]
            [next.jdbc.test-fixtures :refer [with-test-db ds column
                                              default-options
                                              derby? mssql? mysql? postgres?]])
  (:import (java.sql ResultSet ResultSetMetaData)))

(deftest test-row-number
  ;; two notes here: we use as-arrays as a nod to issue #110 to make
  ;; sure that actually works; also Apache Derby is the only database
  ;; (that we test against) to restrict .getRow() calls to scroll cursors
  (testing "row-numbers on bare abstraction"
    (is (= [1 2 3]
           (into [] (map rs/row-number)
                 (p/-execute (ds) ["select * from fruit where id < ?" 4]
                             ;; we do not need a real builder here...
                             (cond-> {:builder-fn (constantly nil)}
                                     (derby?)
                                     (assoc :concurrency :read-only
                                            :cursors     :close
                                            :result-type :scroll-insensitive)))))))
  (testing "row-numbers on realized row"
    (is (= [1 2 3]
           (into [] (comp (map #(rs/datafiable-row % (ds) {}))
                          (map rs/row-number))
                 (p/-execute (ds) ["select * from fruit where id < ?" 4]
                             ;; ...but datafiable-row requires a real builder
                             (cond-> {:builder-fn rs/as-arrays}
                                     (derby?)
                                     (assoc :concurrency :read-only
                                            :cursors     :close
                                            :result-type :scroll-insensitive))))))))

(deftest test-over-partition-all
  ;; this verifies that InspectableMapifiedResultSet survives partition-all
  (testing "row-numbers on partitioned rows"
    (is (= [[1 2] [3 4]]
           (into [] (comp (map #(rs/datafiable-row % (ds) %))
                          (partition-all 2)
                          (map #(map rs/row-number %)))
                 (p/-execute (ds) ["select * from fruit"]
                             (cond-> {:builder-fn rs/as-arrays}
                                     (derby?)
                                     (assoc :concurrency :read-only
                                            :cursors     :close
                                            :result-type :scroll-insensitive))))))))