 
 (defmacro with-metadata
   "Execute BODY with `java.sql.DatabaseMetaData` for DATABASE."
-  [[binding driver database] & body]
-  `(with-open [^java.sql.Connection conn# (get-connection-for-sync ~driver (:details ~database))]
+  [[binding _ database] & body]
+  `(with-open [^java.sql.Connection conn# (jdbc/get-connection (db->jdbc-connection-spec ~database))]
      (let [~binding (.getMetaData conn#)]
        ~@body)))
 

     SECONDS-OR-MILLISECONDS refers to the resolution of the int in question and with be either `:seconds` or `:milliseconds`."))


(def ^{:arglists '([connection-spec])}
  connection-spec->pooled-connection-spec
  "Return a JDBC connection spec that includes a cp30 `ComboPooledDataSource`.
   Theses connection pools are cached so we don't create multiple ones to the same DB.
   Pools are destroyed after they aren't used for more than 3 hours."
  (memoize/ttl
   (fn [spec]
     (log/debug (u/format-color 'magenta "Creating new connection pool..."))
     (kdb/connection-pool (assoc spec :minimum-pool-size           1
                                      ;; prevent broken connections closed by dbs by testing them every 3 mins
                                      :idle-connection-test-period (* 3 60)
                                      ;; prevent overly large pools by condensing them when connections are idle for 15m+
                                      :excess-timeout              (* 15 60))))
   :ttl/threshold (* 6 60 60 1000)))

(defn db->jdbc-connection-spec
  "Return a JDBC connection spec for DATABASE. Normally this will have a C3P0 pool as its datasource, unless the database is `short-lived`."
  {:arglists '([database] [driver details])}
  ;; TODO - I don't think short-lived? key is really needed anymore. It's only used by unit tests, and its original purpose was for creating temporary DBs;
  ;; since we don't destroy databases at the end of each test anymore, it's probably time to remove this
  ([{:keys [engine details]}]
   (db->jdbc-connection-spec (driver/engine->driver engine) details))
  ([driver {:keys [short-lived?], :as details}]
   (let [connection-spec (connection-details->spec driver details)]
     (if short-lived?
       connection-spec
       (connection-spec->pooled-connection-spec connection-spec)))))

(def ^{:arglists '([database] [driver details])}
  db->connection
  "Return a [possibly pooled] connection to DATABASE. Make sure to close this when you're done! (e.g. by using `with-open`)"
  (comp jdbc/get-connection db->jdbc-connection-spec))


(defn escape-field-name


(defn- db->korma-db
  "Return a Korma DB spec for Metabase DATABASE."
  [database]
  (let [driver  (driver/engine->driver (:engine database))
        details (:details database)]
    (assoc (kx/create-db (connection-details->spec driver details))
      :pool (db->jdbc-connection-spec database))))

(defn korma-entity
  "Return a Korma entity for [DB and] TABLE.
