QueryFlag: Create query templates
A template, which the QueryFlag settings call an entry, is a named query for one data source. The query contains the token <QUERY_FLAG_TOKEN>. When you run the template, QueryFlag replaces the token with the text you selected in the editor.
Before you begin
Add the data source that the query runs against in the Database tool window. The template dialog lists the data sources that the current project can use.
Add a template in Settings
Add a template
Press Ctrl+Alt+S (macOS: ⌘ ,) to open the Settings dialog, and select . In DataGrip, select .
To find the page faster, type
QueryFlagin the search field of the Settings dialog.Click Add (+) on the toolbar above the list, or press Alt+Insert (macOS: ⌘ N).
When the list is empty, you can also click the Add a QueryFlag Entry link.
In the Add a QueryFlag Entry dialog, fill in the fields:
- Name
The label of the template in the Query Flag in DB menu. The hint under the field recommends a combination of database, table, and column name. Name templates after the database, table, and column they search, for example
demo.customers.email, so that you can find them in the Query Flag in DB menu.- DataSource
The data source the query runs against. The list shows the data sources from the Database tool window.
- QueryFlag with Token
The query, with at least one
<QUERY_FLAG_TOKEN>token. The field offers syntax highlighting and code completion for the query language of the data source. See Write the template query. If the field highlights the wrong language, see The query field highlights the wrong language.
Click OK. If a field is empty or the query contains no token, the dialog shows an error and stays open. See The template dialog does not accept the query.
A token inside a line that starts with
//also passes this check, but QueryFlag removes that line when it saves the template.Click Apply or OK in the Settings dialog to save the template.
The template now appears in the Query Flag in DB context menu of the editor.
Write the template query
When you run a template, QueryFlag removes every double quote (") from the selected text and puts the rest in place of each <QUERY_FLAG_TOKEN> token, exactly as it is. It does not add quotes and does not escape the value. Write the query so that it is valid after this replacement:
Put quotes around the token wherever the query needs a string literal: single quotes in SQL (
'<QUERY_FLAG_TOKEN>') and double quotes in MongoDB ("<QUERY_FLAG_TOKEN>").Leave the token unquoted for numbers.
Type the token exactly as shown, in upper case and with the angle brackets.
Use the token as often as you need: QueryFlag replaces every occurrence.
Lines that start with
//are notes. QueryFlag removes them when it saves the template, so they never reach the database.Single quotes in the selected text are not escaped, so a selected value with a single quote makes the SQL statement invalid.
SQL examples:
MongoDB example:
Create a template from a query console
If you already have a working query in a query console, turn it into a template. Add Query Flag appears only in the context menu of an editor that is connected to a data source, such as a query console, and only when you have selected some text.
Create a template from a query console
In a query console of the data source, select the query.
To open a new query console, right-click the data source in the Database tool window and select .
Right-click the selection and select Add Query Flag.
The Add a QueryFlag Entry dialog opens with these values: Name and DataSource are set to the console's data source, and QueryFlag with Token contains the selected text without surrounding spaces and surrounding double quotes.
Replace the value that should come from the editor selection with
<QUERY_FLAG_TOKEN>.If the selection had no token, QueryFlag adds the line
// Need to add at least one <QUERY_FLAG_TOKEN> replacement token. Add the token to the query yourself. This line contains the token, so the dialog accepts the query even if you add none. QueryFlag removes the line when it saves the template. A template saved without a token always runs the same query, whatever you select.Optional: change Name to a name you can recognize in the menu.
QueryFlag does not check that names are unique. If you keep the suggested name, all templates that you create from the same data source have the same name in the menu.
Click OK.
QueryFlag saves the template immediately. You do not need to click Apply in Settings.
Edit or remove a template
Edit a template
In , select the template in the list.
Click Edit on the toolbar. The same dialog opens, with the title Add a QueryFlag Entry.
If the template's data source is not available in the current project, the DataSource field is empty. To save the template, you must select a data source, and it replaces the original one. To keep the original data source, edit the template in a project where it is available.
Change the fields and click OK.
Click Apply.
Remove a template
In , select the template in the list.
Click Remove (-) on the toolbar.
Click Apply.
Where templates are available
QueryFlag stores templates for the whole IDE, not per project. Every project shows the same list in Settings.
The Query Flag in DB menu lists a template only in projects where its data source is available. In other projects, the settings list shows the template with the red text "DataSource not found, please check your Databases settings". See A template shows DataSource not found.
Templates refer to data sources by their ID, not by name or connection settings. A data source that you create again, even with the same settings, gets a new ID and does not match existing templates. To use the same templates in every project, create them for a global data source, or make their data source global:
Make a data source available in every project
In the Database tool window, right-click the data source and select Properties (Shift+Enter, macOS: ⌘ I).
In the Data Sources and Drivers dialog, right-click the data source and select Make Global.
Click OK.
To reuse templates on another computer, see Where QueryFlag stores settings.