Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vadviktor/b98139d9be37c3b34a7d5aed0512f502 to your computer and use it in GitHub Desktop.
Save vadviktor/b98139d9be37c3b34a7d5aed0512f502 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, '\\_' ] ];
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment