Skip to content
Snippets Groups Projects
Unverified Commit 925454f1 authored by Tim Macdonald's avatar Tim Macdonald Committed by GitHub
Browse files

[ParseSQL] Find-and-replace for snippets (#43011)

* Support F&R for snippets

[Fixes #42648]
parent dc4d78f1
Branches
Tags
No related merge requests found
......@@ -29,6 +29,13 @@
[raw-query]
(first-unique raw-query #(str (gensym "metabase_sentinel_table_"))))
(defn- gen-snippet-sentinel
[raw-query {:keys [content]}]
(let [delimited-snippet (fn [sentinel snippet-contents]
(format "/* snippet_start_%s */ %s /* snippet_end_%s */"
sentinel snippet-contents sentinel))]
(first-unique raw-query #(delimited-snippet (gensym "mb_") content))))
(defn- braceify
[s]
(format "{{%s}}" s))
......@@ -53,7 +60,8 @@
:else
(let [v (param->value (:k token))
card-ref? (params/ReferencedCardQuery? v)]
card-ref? (params/ReferencedCardQuery? v)
snippet? (params/ReferencedQuerySnippet? v)]
(cond
card-ref?
(let [sub (gen-table-sentinel raw-query)]
......@@ -61,9 +69,16 @@
(str query-so-far sub)
(add-tag substitutions sub token)))
snippet?
(let [sub (gen-snippet-sentinel raw-query v)]
(recur rest
(str query-so-far sub)
(add-tag substitutions sub token)))
;; Plain variable
(and (params/Param? token)
(not card-ref?))
(not card-ref?)
(not snippet?))
(let [sub (gen-variable-sentinel raw-query)]
(recur rest
(str query-so-far sub)
......
......@@ -195,6 +195,18 @@
(->sql (mt/native-query {:template-tags (tags "str_starts")
:query "SELECT * FROM people WHERE {{str_starts}}"})))))
(deftest snippet-test
(testing "With a snippet"
(t2.with-temp/with-temp
[:model/NativeQuerySnippet {snippet-id :id} {:name "a lovely snippet"
:content "where total > 10"}]
(let [og-query "SELECT total FROM orders {{snippet: a lovely snippet}}"]
(is (= "SELECT total FROM orders where total > 10"
(->sql (mt/native-query {:query og-query
:template-tags (assoc-in (lib-native/extract-template-tags og-query)
["snippet: a lovely snippet" :snippet-id]
snippet-id)}))))))))
(deftest card-ref-test
(t2.with-temp/with-temp
[:model/Card {card-id :id} {:type :model
......
......@@ -56,12 +56,25 @@
"city" "town"} ; make sure FFs aren't replaced
:tables {"people" "folk"}})))))
(deftest ^:parallel referenced-card-test
(deftest referenced-card-test
(testing "With a reference to a card"
(t2.with-temp/with-temp
[:model/Card {card-id :id} {:type :model
:dataset_query (mt/native-query {:query "SELECT TOTAL, TAX FROM ORDERS"})}]
(is (= (format "SELECT SUBTOTAL FROM {{#%s}} LIMIT 3" card-id)
(replace-names (q (format "SELECT TOTAL FROM {{#%s}} LIMIT 3" card-id))
{:columns {"TOTAL" "SUBTOTAL"}
:tables {"ORDERS" "PURCHASES"}}))))))
:dataset_query (mt/native-query {:query "SELECT total, tax FROM orders"})}]
(is (= (format "SELECT subtotal FROM {{#%s}} LIMIT 3" card-id)
(replace-names (q (format "SELECT total FROM {{#%s}} LIMIT 3" card-id))
{:columns {"total" "subtotal"}
:tables {"orders" "purchases"}}))))))
(deftest snippet-test
(testing "With a snippet"
(t2.with-temp/with-temp
[:model/NativeQuerySnippet {snippet-id :id} {:name "a lovely snippet"
:content "where subtotal > 10"}]
(is (= "SELECT amount FROM purchases {{snippet: a lovely snippet}}"
(replace-names (assoc-in
(q "SELECT total FROM orders {{snippet: a lovely snippet}}")
[:native :template-tags "snippet: a lovely snippet" :snippet-id]
snippet-id)
{:columns {"total" "amount"}
:tables {"orders" "purchases"}}))))))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment