Advertisement
Guest User

Need to recur to outer loop?

a guest
Jul 17th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;; What we're after is pretty-printing a DB result set.
  2. ;; We need to go through all results before printing to note the maximum width
  3. ;; of each column, so we can print something like:
  4. ;;
  5. ;; +-------+--------------------------------+-------+
  6. ;; | COL_A | COL_B                          | COL_C |
  7. ;; +-------+--------------------------------+-------+
  8. ;; | foo   | Large data so col size expands | bar   |
  9. ;; +-------+--------------------------------+-------+
  10. ;;
  11. (defn print-result-set [rs]
  12.   (let [meta-data (.getMetaData rs)]
  13.     (loop [col-widths  {}
  14.            current-col 1] ; indexed from 1
  15.       (if (<= current-col (.getColumnCount meta-data))
  16.         (recur (assoc col-widths
  17.                       current-col
  18.                       (.length (.getColumnName meta-data current-col)))
  19.                (+ 1 current-col))
  20.         (loop [col-widths col-widths
  21.                col-data   {}
  22.                next-row   (.next rs)]
  23.           (if next-row
  24.             (loop [; go through cols, getting each piece and noting size.
  25.         ; now done with column headers, so process data.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement