The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
sql-jdbc: bind boolean insert values as parameters (#78864)
Timothy S. Dean959c2fca8260
diff --git a/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj b/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.cljindex b788deb3889..e1cfd45730d 100644--- a/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj+++ b/modules/drivers/sqlserver/test/metabase/driver/sqlserver_test.clj@@ -955,6 +955,14 @@(is (re-find #"(?i)nvarchar\(max\)" ddl))(is (not (re-find #"(?i)\bn?text\b" ddl))))))+(deftest ^:parallel insert-boolean-values-test+ (testing "SQL Server has no boolean literal -- a bare TRUE/FALSE token parses as an unquoted identifier+ (\"Invalid column name 'TRUE'\") -- so boolean row values must bind as parameters"+ (let [[sql & params] (first (#'driver.sql-jdbc/insert-into!-sqls :sqlserver :dbo/t ["id" "flag"]+ [[1 true] [2 false]] false))]+ (is (not (re-find #"(?i)\bTRUE\b|\bFALSE\b" sql)))+ (is (= [1 true 2 false] params)))))+(deftest ^:parallel compile-transform-test(mt/test-driver :sqlserver(testing "compile-transform wraps each base table in a self-UNION subquery so the target doesn't inheritdiff --git a/src/metabase/driver/sql_jdbc.clj b/src/metabase/driver/sql_jdbc.cljindex c5c16128ea8..b6dc171a7e5 100644--- a/src/metabase/driver/sql_jdbc.clj+++ b/src/metabase/driver/sql_jdbc.clj@@ -225,25 +225,36 @@(jdbc/with-db-transaction [conn (sql-jdbc.conn/db->pooled-connection-spec db-id)](jdbc/execute! conn sql))))+(defn- lift-boolean [v] (if (boolean? v) [:lift v] v))+(defn- lift-booleans+ "Wraps all boolean in `[:lift v]` for HoneySQL to bind it as a parameter."+ [row]+ (mapv lift-boolean row))+(defn- insert-into!-sqls [driver table-name column-names values inline?]- (let [;; We need to partition the insert into multiple statements for both performance and correctness.- ;;- ;; On Postgres with a large file, 100 (3.76m) was significantly faster than 50 (4.03m) and 25 (4.27m). 1,000 was a- ;; little faster but not by much (3.63m), and 10,000 threw an error:- ;; PreparedStatement can have at most 65,535 parameters- ;; One imagines that `(long (/ 65535 (count columns)))` might be best, but I don't trust the 65K limit to apply- ;; across all drivers. With that in mind, 100 seems like a safe compromise.- ;; There's nothing magic about 100, but it felt good in testing. There could well be a better number.- chunks (partition-all (or driver/*insert-chunk-rows* 100) values)- dialect (sql.qp/quote-style driver)- sqls (map #(sql/format {:insert-into (keyword table-name)- :columns (quote-columns driver column-names)- :values %}- :inline inline?- :quoted true- :dialect dialect)- chunks)]- sqls))+ ;; We need to partition the insert into multiple statements for both performance and correctness.+ ;;+ ;; Tim Macdonald (April 2023) -- On Postgres with a large file:+ ;; tested 1000 (3.63m), 100 (3.76m), 50 (4.03m), and 25 (4.27m).+ ;; 10,000 threw an error: `PreparedStatement can have at most 65,535 parameters`+ ;; `(long (/ 65535 (count columns)))` might be best, but the 65K limit may not apply on all drivers.+ ;; There's nothing magic about 100, but it felt good in testing. There could well be a better number.+ (let [dialect (sql.qp/quote-style driver)+ columns-quoted (quote-columns driver column-names)+ table-name-k (keyword table-name)+ chunk-size (or driver/*insert-chunk-rows* 100)]+ (->> values+ (sequence (comp+ (partition-all chunk-size)+ (map (fn chunk-sql [row-chunk]+ (sql/format {:insert-into table-name-k+ :columns columns-quoted+ ;; HoneySQL inlines true/false unless we do this, even when+ ;; the rest of the query is parameterized.+ :values (mapv lift-booleans row-chunk)}+ :inline inline?+ :quoted true+ :dialect dialect))))))))(defmethod driver/insert-into! :sql-jdbc[driver db-id table-name column-names values]diff --git a/test/metabase/driver/sql_jdbc_test.clj b/test/metabase/driver/sql_jdbc_test.cljindex 705713f80d9..066deefbbfd 100644--- a/test/metabase/driver/sql_jdbc_test.clj+++ b/test/metabase/driver/sql_jdbc_test.clj@@ -5,6 +5,7 @@[clojure.test :refer :all][metabase.driver :as driver][metabase.driver.common.table-rows-sample :as table-rows-sample]+ [metabase.driver.sql-jdbc :as driver.sql-jdbc][metabase.driver.sql-jdbc.sync.describe-database :as sql-jdbc.describe-database][metabase.driver.sql.query-processor :as sql.qp][metabase.driver.util :as driver.u]@@ -391,3 +392,12 @@(driver/drop-table! driver db-id qualified-renamed))(when (driver/table-exists? driver (mt/db) {:name test-table :schema schema})(driver/drop-table! driver db-id qualified-table))))))))++(deftest ^:parallel insert-into-sqls-boolean-literal-test+ (testing "boolean row values bind as parameters, never as inlined literals -- not every+ dialect has a boolean literal keyword"+ (doseq [driver [:h2]]+ (let [[sql & params] (first (#'driver.sql-jdbc/insert-into!-sqls driver :dbo/t ["id" "flag"]+ [[1 true] [2 false]] false))]+ (is (not (re-find #"(?i)\bTRUE\b|\bFALSE\b" sql)) (str driver))+ (is (= [1 true 2 false] params) (str driver))))))