diff --git a/modules/drivers/sqlite/src/metabase/driver/sqlite.clj b/modules/drivers/sqlite/src/metabase/driver/sqlite.clj index ec76491fb7083c3f6f37e28f95f1bc7b46d1d4f0..88d23de96edafb2fe99a83af7561599b63c2fcf2 100644 --- a/modules/drivers/sqlite/src/metabase/driver/sqlite.clj +++ b/modules/drivers/sqlite/src/metabase/driver/sqlite.clj @@ -1,5 +1,6 @@ (ns metabase.driver.sqlite - (:require [clojure.string :as str] + (:require [clojure.java.io :as io] + [clojure.string :as str] [honeysql.core :as hsql] [honeysql.format :as hformat] [java-time :as t] @@ -38,6 +39,23 @@ ;; for now we'll just skip the foreign key stuff in the tests. (defmethod driver/supports? [:sqlite :foreign-keys] [_ _] (not config/is-test?)) +;; Every SQLite3 file starts with "SQLite Format 3" +;; or "** This file contains an SQLite +;; There is also SQLite2 but last 2 version was 2005 +(defn- confirm-file-is-sqlite [filename] + (with-open [reader (io/input-stream filename)] + (let [outarr (byte-array 50)] + (.read reader outarr) + (let [line (String. outarr)] + (or (str/includes? line "SQLite format 3") + (str/includes? line "This file contains an SQLite")))))) + +(defmethod driver/can-connect? :sqlite + [driver details] + (if (confirm-file-is-sqlite (:db details)) + (sql-jdbc.conn/can-connect? driver details) + false )) + (defmethod driver/db-start-of-week :sqlite [_] :sunday)