   [^ResultSet res]
   (if (.next res) res :err))
 
-(defn exec-sql 
-  []
-  (try 
-    {:ok 
-      (get-fst-result 
+(defn get-db-connection
+  [db]
+  (connect-to-db (get-jdbc-url db)))
+
+(defn exec-sql
+  [db-connection statement]
+  (try
+    {:ok
+      (get-fst-result
         (execute-query
-          (.createStatement (connect-to-db (get-jdbc-url db-prod-eu)))
-          "SELECT NOW();")) }
+          (.createStatement db-connection statement))) }
   (catch Exception e
     {:error "Exception" :fn "exec-sql" :exception (.getMessage e) })))
 
-(defn start-time 
+(defn start-time
   ^Date []
   (Date. (.getStartTime (ManagementFactory/getRuntimeMXBean))))
 


;; nil if enything goes sideways

(def db-prod-eu 
  (:eu 
    (:ok 
      (parse-edn-string 
        (:ok 
          (read-file (File. "conf/prod.edn")))))))

(defn get-jdbc-url 
  ^String [^PersistentArrayMap hm] 
  (str "jdbc:" (:dbtype hm) "://" (:host hm) "/" 
       (:dbname hm) "?" "user=" (:user hm) 
       "&" "password=" (:password hm) 
       "&" "ssl=" (get-in hm [:ssl] "false")
       "&" "sslfactory=" (get-in hm [:sslfactory] "org.postgresql.ssl.NonValidatingFactory")))

(defn connect-to-db 
  ^Connection [^String dburl]
  (DriverManager/getConnection dburl))

(defn create-statement 
  ^Statement [^Connection dbconn]
  (.createStatement dbconn))


  []
  (.getInputArguments (ManagementFactory/getRuntimeMXBean)))

(def select-now "SELECT NOW();")

(defn get-kms
  [key-name]
  (println key-name)
  (let [
        ^bytes          encryptedKey  (Base64/decode
                                        (System/getenv key-name))
        ^AWSKMS         client        (AWSKMSClientBuilder/defaultClient)
        ^DecryptRequest request       (.withCiphertextBlob
                                        (DecryptRequest.)
                                        (ByteBuffer/wrap encryptedKey))
        ^ByteBuffer     plainTextKey  (.getPlaintext
                                        (.decrypt client request))
        ^String         ret           (String.
                                        (.array plainTextKey) (Charset/forName "UTF-8"))
        ]
    ret))

; (fv [param] body)

(defn init
  []
  (println (str "Start time :: " (start-time)))
  (println (str "Input args :: " (get-input-args))))

(defn config-selector
  [config]
  (cond
    (= config :local)
      { :ok (parse-edn-string
              (:ok
                (read-file (File. "conf/prod.edn")))) }
    (= config :kms)
      { :ok (get-kms "prod") }
    :else
      { :err :err }))


(defn sql-exec
  [config] ; :local or :kms
  (let [ databases (config-selector config) ]
    (println databases)))

(defn -handler
  [s]
  (init)
  (sql-exec :kms)
  s)

(defn -main
  [& args]
  (init)
  (sql-exec :local))
