How do I read from an input text file that is 1 column of ids and produce a MySQL query of the format:
SELECT col1,col2,col3 FROM Orders WHERE Id IN ('inputId1','inputId3','inputId3');
The ids in the input file are separated by /n and should be converted into the comma separated list of Ids enclosed in quotes for the MySQL query.
(ns export.core
(:require [clojure.java.jdbc :as j])
(:gen-class))
(defn -main [& args]
;; Get home directory
(def out-file
(str (System/getProperty "user.home") "/Desktop/export.txt"))
(def in-file
(str (System/getProperty "user.home") "/Desktop/orders.txt"))
;; Get string of order-ids
(def order-ids-string (slurp in-file))
???????????
???????????
;; Connect to database
(def db {:subprotocol "mysql"
:subname "XXXXXXXX"
:user "XXXXXXX"
:password "XXXXXXX"})
;; Get headers
(def header-seq
(j/query db ["DESCRIBE Orders"] :row-fn :field))
(def header-str
(str (clojure.string/join "\t" header-seq) "\n"))
;; Get product results and spit data to file
(def header-keys
(into []
(map keyword
(map clojure.string/lower-case header-seq))))
(def data-seq
(j/query db [<needed sql query>]))
(defn select-values [map]
(reduce #(conj %1 (map %2)) [] header-keys))
(spit out-file header-str)
(doseq [row data-seq]
(spit out-file
(str (clojure.string/join "\t" (select-values row)) "\n")
:append true)))