Parsing code:

(ns core
  (:require [ :as zip]
            [clojure.xml :as xml]
            [clojure.string :as string])

(defn clean-up
  (-> s
      (string/replace #"/\*.*\*/" "")
      (string/replace "\\ r\\ n" "\\ n")
      (string/replace #"--+.*\\ n" "")
      (string/replace #"[\\ n\\ t]" " ")))

(defn into-tokens [s]
  (string/split s #"[ ]+"))

(defn declaring-table? [tok]
  (or (= (string/lower-case tok) "from")
      (= (string/lower-case tok) "join")))

(defn tidy-tablename
  (-> tbl
      (string/replace #"^dbo." "")
      (string/replace ")" "")))

(defn find-tables [t]
  "given a token stream, returns table names"
  (map #(tidy-tablename (second %))
       (filter #(declaring-table? (first %))
               (partition 2 (interleave t (rest t))))))

(defn parse-sql-for-tables
  "returns list of tables that are used by the query"
  (-> sql clean-up into-tokens find-tables))

(defn zip-file [filename]
  (zip/xml-zip (xml/parse ( filename))))

(defn parse-xml-table
  (let [table-name (first (xml-> table (attr :name)))]
    (map (fn [provider sql]
           (list table-name
                 (parse-sql-for-tables sql)))
         (xml-> table :SOURCESET :DATAPROVIDER (attr :SharedProviderID))
         (xml-> table :SOURCESET :DATAPROVIDER :COMMANDTEXT text))))

(defn parse-tables-from-xml
  (mapcat parse-xml-table (xml-> zipper :TABLE)))

(defn add-tables
  [s tables]
  (let [sett (if (nil? s) (sorted-set) s)]
    (reduce #(conj %1 %2) sett tables)))

(defn merge-tables
  "possibly multiple entries for a table to coalesce them"
  (reduce (fn [h [master _ tables]]
            (assoc h master (add-tables (h master) tables)))

(defn view-tables
  (map (fn [t]
         (println (first t))
         (map (fn [e] (println "   " e)) (second t)))

(defn main
    (filter #(= "re7connection" (string/lower-case (second %)))
            (parse-tables-from-xml (zip-file filename)))))