Last active
May 4, 2023 07:27
-
-
Save Seayon/217428083110bbced5d411a37df695bf to your computer and use it in GitHub Desktop.
Custom My-IN SQL Export Script for DataGrip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Available context bindings: | |
* COLUMNS List<DataColumn> | |
* ROWS Iterable<DataRow> | |
* OUT { append() } | |
* FORMATTER { format(row, col); formatValue(Object, col); getTypeName(Object, col); isStringLiteral(Object, col); } | |
* TRANSPOSED Boolean | |
* plus ALL_COLUMNS, TABLE, DIALECT | |
* | |
* where: | |
* DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object } | |
* DataColumn { columnNumber(), name() } | |
*/ | |
SEP = ", " | |
NEWLINE = System.getProperty("line.separator") | |
OUT.append("(").append(NEWLINE) | |
def rowIndex = 0 | |
def rowList = ROWS.collect { it } | |
for (row in rowList) { | |
def rowValues = COLUMNS.collect { col -> row.value(col) } | |
def rowSize = rowValues.size() | |
if (rowSize > 1) { | |
OUT.append(" (") | |
} else { | |
OUT.append(" ") | |
} | |
for (int colIndex = 0; colIndex < rowSize; colIndex++) { | |
def value = rowValues[colIndex] | |
def column = COLUMNS[colIndex] | |
def isStringLiteral = value != null && FORMATTER.isStringLiteral(value, column) | |
if (isStringLiteral) { | |
OUT.append("'").append(FORMATTER.formatValue(value, column)).append("'") | |
} else { | |
OUT.append(FORMATTER.formatValue(value, column)) | |
} | |
if (colIndex < rowSize - 1) { | |
OUT.append(SEP) | |
} | |
} | |
if (rowSize > 1) { | |
OUT.append(")") | |
} | |
if (rowIndex < rowList.size() - 1) { | |
OUT.append(",") | |
} | |
OUT.append(NEWLINE) | |
rowIndex++ | |
} | |
OUT.append(")") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script helps you export selected data in a format suitable for SQL IN clauses. Here are some examples illustrating the effects of the script:
By exporting the "id" column using this script, you will obtain the following output format:
This way, you can directly insert this output into the IN clause of your SQL query, such as:
By exporting the "order_id" and "product_id" columns using this script, you will obtain the following output format:
This way, you can directly insert this output into the IN clause of your SQL query, such as:
This script can flexibly handle both single-column and multi-column data exports, saving you time in manually formatting the data and improving the efficiency of writing queries.