Export data in any way with IntelliJ-based IDEs

In DataGrip, or any other JetBrains IDE featuring database support, there is a powerful engine to export data in many formats. You can even create your own export format.

ExportSq1

Let’s look closer at this engine.

Export directions

Any table/view or result-set can be exported to a file or copied to the clipboard.

To export to file:
      — Context menu on a table/view and → Dump data to file.
      — Context menu on a query and → Execute to file.
      — In the toolbar of the result-set or data editor, select Dump data button → To File…

DumpToFile

To export to clipboard:

      — Select the data you need within a result-set or data editor and press Copy or Ctrl/Cmd+C.
      — In the toolbar of a result-set or data editor, select Dump data button → To Clipboard

Default extractors

We’ll talk about copying data to the clipboard from the data editor, but the same applies to other described ways to export data.

Look to the left of the Dump data button for a drop-down where you can choose the extractor — the format to export data in.

ChooseSQLInserts

There are several built-in formats as you can see. Some of them let you export data as a set of INSERT/UPDATE statements, while others export data as text such as CSV, JSON, HTML, etc. For more details on how they work, please visit this page.

Still, users may need to extract data in many different ways.

Twitter

Creating a DSV-based extractor

Let’s see how we can extend the default functionality.

To create your own format based on CSV (or any DSV format), select Configure CSV formats…

ConfigureDSV

In this dialog, in addition to changing the existing CSV and TSV extractors, you can create your own. For example, Confluence Wiki Markup.

ConfluenceWikiMarkup

Once created, it appears among the other extractors.

ConfluenceWikiMarkuipInMenu

Creating any text extractor with scripting

For the more complicated cases, consider using scripting extractors. Several of them are already there such as CSV-Groovy.csv.groovy, HTML-Groove.html.groovy, and others. These scripts are written in Groovy, but they can also be written in JavaScript. Our examples use Groovy.

Looking closely at the file name, CSV-Groovy.csv.groovy:

      CSV-Groovy is just the name of the script;
      csv is the extension of the result file if you extract to file;
      groovy is the extension of the script itself. It helps IntelliJ IDEA highlight your code if that’s where you create/edit your scripts.

Scripts are usually located in Scratches and Consoles/Extensions/Database Tools and SQL/data/extractors. Or you can select Go to scripts directory in the extractor menu to navigate there.

ChooseGoToSCriptsDirewctory

You should edit an existing extractor or just add your own to this folder.

Let’s create an extractor which will dump your data to CSV format, but to just one row. It can be useful if you are going to paste these values into an IN operator in a WHERE clause.

Here is the diff view of two scripts: the existing CSV-Groovy.csv.groovy and our new one which we’ll name CSV-ToOneRow-Groovy.csv.groovy.

ScriptsDif

Note the available context bindings:

These objects are equal when dumping the whole page to file.

Where:

 

DasTable has two important methods:

Before v2017.3

Since v2017.3

 

Additional information about the API can be found here.

When you create or edit Groovy scripts in IntelliJ IDEA, and have Groovy installed, coding assistance is available.

CodingAssistance

Once the new script file is in the folder, you can use the extractor!

ChooseCSVGToOneRow

Copy these values and paste them into the query.

ToOneRowSQL

Here’s another example based on SQL-Insert-Statements.sql.groovy. MySQL and PostgreSQL allow using multi-row syntax. To use this type of extractor, create a new SQL-Inserts-MultirowSynthax.sql.groovy file in the scripting folder.

SQLInsertsMultiRowExtractor

Again, simply select it in the menu.

ChooseSQLMultiRow
The result:

InserMultiRowSQL

There are already some user-generated extractors on GitHub which everybody can use:

Extractor to copy tab separated query results to paste into Excel with comma decimal separator.
DBunit XML.
Txt-file with fixed length columns. (Here’s a very similar one).
Php array.
Markdown.

We hope this tutorial will help you tweak DataGrip to your specific needs and encourage you to share extractors for all to use.

The DataGrip Team

This entry was posted in Features, Tutorial, Uncategorized. Bookmark the permalink.

4 Responses to Export data in any way with IntelliJ-based IDEs

  1. Mike says:

    At the head of the article is written «or any other JetBrains IDE featuring database support».
    I tried to find the same menus in IDEA 2017.3 EAP Ultimate Edition, but didn’t find it.

    The question is does IntelliJ IDEA 2017.3 EAP (Ultimate Edition) contains the features, described here? If yes, how can I convert XML to SQL with IDEA?

    Thanks.

    • Maksim Sobolevskiy says:

      Can you please send us a screenshot of the absence of this menu? It should be there. Now you cannot convert XML to SQL, because this is import, not export. We considering extending import functionality as well.

  2. Peter says:

    I would like to format dates to dd-mm-yyyy (the standard here). Is there a way to obtain the columns ‘raw’ value and apply a format or do I have to resort to cut+paste the string value from the FORMATTER.format ?
    I think it is a pity that FORMATTER.format does not take any localisation into consideration.

Leave a Reply

Your email address will not be published. Required fields are marked *