Skip to content
Snippets Groups Projects
Unverified Commit 109915f8 authored by Cam Saul's avatar Cam Saul Committed by GitHub
Browse files

Add MongoDB params examples to FE (#11518)

parent 2628902f
Branches
Tags
No related merge requests found
......@@ -4,7 +4,7 @@ import Code from "metabase/components/Code";
import MetabaseSettings from "metabase/lib/settings";
import Utils from "metabase/lib/utils";
const EXAMPLES = {
const SQL_EXAMPLES = {
variable: {
database: null,
type: "native",
......@@ -100,6 +100,100 @@ const EXAMPLES = {
},
};
const MONGO_EXAMPLES = {
variable: {
database: null,
type: "native",
native: {
query: "[{ $match: { price: {{price}} } }]",
"template-tags": {
category: {
id: Utils.uuid(),
name: "price",
display_name: "Price",
type: "number",
required: true,
default: "2",
},
},
},
},
dimension: {
database: null,
type: "native",
native: {
query: "[{ $match: {{created_at}} }]",
"template-tags": {
created_at: {
id: Utils.uuid(),
name: "created_at",
display_name: "Created At",
type: "dimension",
dimension: null,
required: false,
},
},
},
},
optional: {
database: null,
type: "native",
native: {
query: "[{ $match: { [[ _id: {{id}} ]] } }]",
"template-tags": {
category: {
id: Utils.uuid(),
name: "id",
display_name: "ID",
type: "text",
required: false,
},
},
},
},
multipleOptional: {
database: null,
type: "native",
native: {
query:
"[{ $match: { [[ _id: {{id}} [[, category: {{category}} ]] ]] } }]",
"template-tags": {
id: {
id: Utils.uuid(),
name: "id",
display_name: "ID",
type: "number",
required: false,
},
category: {
id: Utils.uuid(),
name: "category",
display_name: "Category",
type: "text",
required: false,
},
},
},
},
optionalDimension: {
database: null,
type: "native",
native: {
query: "[{ $match: { $and: [ { _id: 1 } [[, {{category}} ]] ] } }]",
"template-tags": {
category: {
id: Utils.uuid(),
name: "category",
display_name: "Category",
type: "dimension",
dimension: null,
required: false,
},
},
},
},
};
const TagExample = ({ datasetQuery, setDatasetQuery }) => (
<div>
<h5>Example:</h5>
......@@ -119,23 +213,29 @@ const TagExample = ({ datasetQuery, setDatasetQuery }) => (
);
const TagEditorHelp = ({
database,
setDatasetQuery,
sampleDatasetId,
switchToSettings,
}) => {
let setQueryWithSampleDatasetId = null;
if (sampleDatasetId != null) {
setQueryWithSampleDatasetId = (dataset_query, run) => {
const driver = database && database.engine;
const examples = driver === "mongo" ? MONGO_EXAMPLES : SQL_EXAMPLES;
const datasetId = driver === "mongo" ? database.id : sampleDatasetId;
let setQueryWithDatasetId = null;
if (datasetId != null) {
setQueryWithDatasetId = (dataset_query, run) => {
setDatasetQuery(
{
...dataset_query,
database: sampleDatasetId,
database: datasetId,
},
run,
);
switchToSettings();
};
}
return (
<div>
<h4>{t`What's this for?`}</h4>
......@@ -147,23 +247,23 @@ const TagEditorHelp = ({
<p>
{jt`${(
<Code>{"{{variable_name}}"}</Code>
)} creates a variable in this SQL template called "variable_name". Variables can be given types in the side panel, which changes their behavior. All variable types other than "Field Filter" will automatically cause a filter widget to be placed on this question; with Field Filters, this is optional. When this filter widget is filled in, that value replaces the variable in the SQL template.`}
)} creates a variable in this query template called "variable_name". Variables can be given types in the side panel, which changes their behavior. All variable types other than "Field Filter" will automatically cause a filter widget to be placed on this question; with Field Filters, this is optional. When this filter widget is filled in, that value replaces the variable in the query template.`}
</p>
<TagExample
datasetQuery={EXAMPLES.variable}
setDatasetQuery={setQueryWithSampleDatasetId}
datasetQuery={examples.variable}
setDatasetQuery={setQueryWithDatasetId}
/>
<h4 className="pt2">{t`Field Filters`}</h4>
<p>
{t`Giving a variable the "Field Filter" type allows you to link SQL cards to dashboard filter widgets or use more types of filter widgets on your SQL question. A Field Filter variable inserts SQL similar to that generated by the GUI query builder when adding filters on existing columns.`}
{t`Giving a variable the "Field Filter" type allows you to link questions to dashboard filter widgets or use more types of filter widgets on your SQL question. A Field Filter variable inserts SQL similar to that generated by the GUI query builder when adding filters on existing columns.`}
</p>
<p>
{t`When adding a Field Filter variable, you'll need to map it to a specific field. You can then choose to display a filter widget on your question, but even if you don't, you can now map your Field Filter variable to a dashboard filter when adding this question to a dashboard. Field Filters should be used inside of a "WHERE" clause.`}
</p>
<TagExample
datasetQuery={EXAMPLES.dimension}
setDatasetQuery={setQueryWithSampleDatasetId}
datasetQuery={examples.dimension}
setDatasetQuery={setQueryWithDatasetId}
/>
<h4 className="pt2">{t`Optional Clauses`}</h4>
......@@ -173,22 +273,22 @@ const TagEditorHelp = ({
)} create an optional clause in the template. If "variable" is set, then the entire clause is placed into the template. If not, then the entire clause is ignored.`}
</p>
<TagExample
datasetQuery={EXAMPLES.optional}
setDatasetQuery={setQueryWithSampleDatasetId}
datasetQuery={examples.optional}
setDatasetQuery={setQueryWithDatasetId}
/>
<p>
{t`To use multiple optional clauses you can include at least one non-optional WHERE clause followed by optional clauses starting with "AND".`}
</p>
<TagExample
datasetQuery={EXAMPLES.multipleOptional}
setDatasetQuery={setQueryWithSampleDatasetId}
datasetQuery={examples.multipleOptional}
setDatasetQuery={setQueryWithDatasetId}
/>
<p>{t`When using a Field Filter, the column name should not be included in the SQL. Instead, the variable should be mapped to a field in the side panel.`}</p>
<TagExample
datasetQuery={EXAMPLES.optionalDimension}
setDatasetQuery={setQueryWithSampleDatasetId}
datasetQuery={examples.optionalDimension}
setDatasetQuery={setQueryWithDatasetId}
/>
<p className="pt2 link">
......
......@@ -108,6 +108,7 @@ export default class TagEditorSidebar extends React.Component {
/>
) : (
<TagEditorHelp
database={database}
sampleDatasetId={sampleDatasetId}
setDatasetQuery={setDatasetQuery}
switchToSettings={() => this.setSection("settings")}
......
......@@ -22,7 +22,6 @@ Object {
"expressions",
"case-sensitivity-string-filter-options",
"binning",
"native-parameters-field-filters",
"inner-join",
],
"id": 1,
......@@ -1123,7 +1122,6 @@ Object {
"expressions",
"case-sensitivity-string-filter-options",
"binning",
"native-parameters-field-filters",
"inner-join",
],
"id": 1,
......@@ -2329,7 +2327,6 @@ Object {
"expressions",
"case-sensitivity-string-filter-options",
"binning",
"native-parameters-field-filters",
"inner-join",
],
"id": 1,
......@@ -3178,7 +3175,6 @@ Object {
"expressions",
"case-sensitivity-string-filter-options",
"binning",
"native-parameters-field-filters",
"inner-join",
],
"id": 1,
......
......@@ -363,14 +363,6 @@
;; SELECT * FROM table WHERE field = 1
:native-parameters
;; Does this driver support parameter substitution in native queries, where parameter expressions are replaced
;; with entire conditional expressions? e.g.
;;
;; SELECT * FROM table WHERE {{param}}
;; ->
;; SELECT * FROM table WHERE field = 1
:native-parameters-field-filters
;; Does the driver support using expressions inside aggregations? e.g. something like \"sum(x) + count(y)\" or
;; \"avg(x + y)\"
:expression-aggregations
......
......@@ -19,7 +19,6 @@
:expressions
:expression-aggregations
:native-parameters
:native-parameters-field-filters
:nested-queries
:binning]]
(defmethod driver/supports? [:sql feature] [_ _] true))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment