forked from metabase/metabase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unprepare args for BigQuery [ci drivers]
- Loading branch information
Showing
4 changed files
with
45 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
(ns metabase.driver.generic-sql.util.unprepare | ||
"Utility functions for converting a prepared statement with `?` into a plain SQL query." | ||
(:require [clojure.string :as str] | ||
[honeysql.core :as hsql] | ||
[metabase.util :as u] | ||
[metabase.util.honeysql-extensions :as hx]) | ||
(:import java.util.Date)) | ||
|
||
(defprotocol ^:private IUnprepare | ||
(^:private unprepare-arg ^String [this])) | ||
|
||
(extend-protocol IUnprepare | ||
nil (unprepare-arg [this] "NULL") | ||
String (unprepare-arg [this] (str \' (str/replace this "'" "\\\\'") \')) ; escape single-quotes | ||
Boolean (unprepare-arg [this] (if this "TRUE" "FALSE")) | ||
Number (unprepare-arg [this] (str this)) | ||
Date (unprepare-arg [this] (first (hsql/format (hsql/call :timestamp (hx/literal (u/date->iso-8601 this))))))) ; TODO - this probably doesn't work for every DB! | ||
|
||
(defn unprepare | ||
"Convert a normal SQL `[statement & prepared-statement-args]` vector into a flat, non-prepared statement." | ||
^String [[sql & args]] | ||
(loop [sql sql, [arg & more-args, :as args] args] | ||
(if-not (seq args) | ||
sql | ||
(recur (str/replace-first sql #"(?<!\?)\?(?!\?)" (unprepare-arg arg)) | ||
more-args)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
(ns metabase.driver.generic-sql.util.unprepare-test | ||
(:require [expectations :refer :all] | ||
[metabase.driver.generic-sql.util.unprepare :as unprepare])) | ||
|
||
(expect | ||
"SELECT 'Cam\\'s Cool Toucan' FROM TRUE WHERE x ?? y AND z = timestamp('2017-01-01T00:00:00.000Z')" | ||
(unprepare/unprepare ["SELECT ? FROM ? WHERE x ?? y AND z = ?" | ||
"Cam's Cool Toucan" | ||
true | ||
#inst "2017-01-01T00:00:00.000Z"])) |