Skip to content

Instantly share code, notes, and snippets.

@jimschubert
Last active September 24, 2023 13:31
Show Gist options
  • Star 49 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save jimschubert/a9dc46dafc891227f25d82a934a7df72 to your computer and use it in GitHub Desktop.
Save jimschubert/a9dc46dafc891227f25d82a934a7df72 to your computer and use it in GitHub Desktop.
DataGrip (IntelliJ) output SQL results to Markdown

Installation

Open the scripts directory. There are a couple ways to do this...

  1. Hit SHIFTSHIFT and select "Data Extractor", then choose "Go to Scripts Directory"
  2. In an existing query, choose the output format (CSV, HTML, TSV selector), then choose "Go to Scripts Directory"

Create a new file (Right Click -> New -> File) and name it Markdown-JavaScript.markdown.js.

Paste the contents of Markdown-JavaScript.markdown.js into this file.

Usage

Execute a query, select Markdown-JavaScript.markdown.js from the output format dropdown, click the download icon and choose To File or To Clipboard.

Credits

This is based on the HTML-JavaScript.html.js extractor which comes with DataGrip. Markdown escape logic taken from https://github.com/kemitchell/markdown-escape.js.

if (!String.prototype.repeat) {
// polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
String.prototype.repeat = function(count) {
'use strict';
if (this == null) {
throw new TypeError('can\'t convert ' + this + ' to object');
}
var str = '' + this;
count = +count;
if (count != count) {
count = 0;
}
if (count < 0) {
throw new RangeError('repeat count must be non-negative');
}
if (count == Infinity) {
throw new RangeError('repeat count must be less than infinity');
}
count = Math.floor(count);
if (str.length == 0 || count == 0) {
return '';
}
// Ensuring count is a 31-bit integer allows us to heavily optimize the
// main part. But anyway, most current (August 2014) browsers can't handle
// strings 1 << 28 chars or longer, so:
if (str.length * count >= 1 << 28) {
throw new RangeError('repeat count must not overflow maximum string size');
}
var rpt = '';
for (;;) {
if ((count & 1) == 1) {
rpt += str;
}
count >>>= 1;
if (count == 0) {
break;
}
str += str;
}
// Could we try:
// return Array(count + 1).join(this);
return rpt;
}
}
// credit: https://github.com/kemitchell/markdown-escape.js
var replacements = [
[ /\*/g, '\\*' ],
[ /#/g, '\\#' ],
[ /\//g, '\\/' ],
[ /\(/g, '\\(' ],
[ /\)/g, '\\)' ],
[ /\[/g, '\\[' ],
[ /\]/g, '\\]' ],
[ /\</g, '&lt;' ],
[ /\>/g, '&gt;' ],
[ /\|/g, '\\|' ],
[ /_/g, '\\_' ] ];
function escapeMarkdown(string) {
return replacements.reduce(
function(string, replacement) {
return string.replace(replacement[0], replacement[1])
},
string)
}
function eachWithIdx(iterable, f) { var i = iterable.iterator(); var idx = 0; while (i.hasNext()) f(i.next(), idx++); }
function mapEach(iterable, f) { var vs = []; eachWithIdx(iterable, function (i) { vs.push(f(i));}); return vs; }
function escape(str) {
str = str.replaceAll("\t|\b|\\f", "");
str = com.intellij.openapi.util.text.StringUtil.escapeXml(str);
str = str.replaceAll("\\r|\\n|\\r\\n", "<br/>");
return escapeMarkdown(str);
}
var NEWLINE = "\n";
function output() { for (var i = 0; i < arguments.length; i++) { OUT.append(arguments[i]); } }
function outputRow(items) {
for (var i = 0; i < items.length; i++) {
output("|");
output(escape(items[i]));
if(i == items.length - 1) {
output("|"); // trailing pipe
}
}
output(NEWLINE);
}
output("Queried: " + (new Date()).toLocaleDateString(), NEWLINE, NEWLINE);
if (TRANSPOSED) {
var values = mapEach(COLUMNS, function(col) { return [col.name()]; });
eachWithIdx(ROWS, function (row) {
eachWithIdx(COLUMNS, function (col, i) {
values[i].push(FORMATTER.format(row, col));
});
});
eachWithIdx(COLUMNS, function (_, i) { outputRow(values[i]); });
}
else {
outputRow(mapEach(COLUMNS, function (col) { return col.name(); })); // header names
outputRow(mapEach(COLUMNS, function (col) { return '-'.repeat(Math.max(4, col.name().length)); })); // header separator
eachWithIdx(ROWS, function (row) {
outputRow(mapEach(COLUMNS, function (col) { return FORMATTER.format(row, col); }))
});
}
output(NEWLINE);
@MarkBorcherding
Copy link

You also need to replace

  [ /\|/g, '\\|' ]

Incase your table contains |.

@datamaskin
Copy link

Executing in the DataGrip JavaScript console
ECMAException: ReferenceError: "OUT" is not defined

@jimschubert
Copy link
Author

@datamaskin what version of DataGrip are you using?

@datamaskin
Copy link

datamaskin commented Nov 20, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment