 (ns metabase.driver.generic-sql.util
   "Shared functions for our generic-sql query processor."
-  (:require [clojure.core.memoize :as memo]
-            [clojure.java.jdbc :as jdbc]
+  (:require [clojure.java.jdbc :as jdbc]
             [clojure.tools.logging :as log]
             [colorize.core :as color]
             [korma.core :as korma]

(def ^{:arglists '([database])}
  db->korma-db
  "Return a Korma database definition for DATABASE.
   This does a little bit of smart caching (for 60 seconds) to avoid creating new connections when unneeded."
  (let [-db->korma-db (memo/ttl (fn [database]
                                  (log/debug (color/red "Creating a new DB connection..."))
                                  (kdb/create-db (db->connection-spec database)))
                                :ttl/threshold (* 60 1000))]
    ;; only :engine and :details are needed for driver/connection so just pass those so memoization works as expected
    (fn [database]
      (-db->korma-db (select-keys database [:engine :details])))))

(def ^:dynamic ^java.sql.DatabaseMetaData *jdbc-metadata*
  "JDBC metadata object for a database. This is set by `with-jdbc-metadata`."

            [metabase.driver :as driver]
            [metabase.driver.query-processor :as qp]))

(defn- db->connection-spec [{{:keys [short-lived?]} :details, :as database}]
  (let [driver                              (driver/engine->driver (:engine database))
        database->connection-details        (:database->connection-details driver)
