Created
November 1, 2021 10:52
-
-
Save gorbunov/c4ea70183f87b995a2c4b67bbd73165f to your computer and use it in GitHub Desktop.
DataGrip exporter for query results without ID key, uses first column as matcher
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 = ", " | |
QUOTE = "\'" | |
STRING_PREFIX = DIALECT.getDbms().isMicrosoft() ? "N" : "" | |
NEWLINE = System.getProperty("line.separator") | |
KEYWORDS_LOWERCASE = com.intellij.database.util.DbSqlUtil.areKeywordsLowerCase(PROJECT) | |
KW_UPDATE_TEXT = KEYWORDS_LOWERCASE ? "update " : "UPDATE " | |
KW_SET = KEYWORDS_LOWERCASE ? " SET " : " SET " | |
KW_MATCHER = KEYWORDS_LOWERCASE ? " WHERE " : " WHERE " | |
KW_NULL = KEYWORDS_LOWERCASE ? "null" : "NULL" | |
MATCHING_KEY = COLUMNS.first(); | |
def record(columns, dataRow) { | |
OUT.append(KW_UPDATE_TEXT) | |
if (TABLE == null) OUT.append("MY_TABLE") | |
else OUT.append(TABLE.getParent().getName()).append(".").append(TABLE.getName()).append(KW_SET) | |
columns.eachWithIndex { column, idx -> | |
//OUT.append(column.name()).append(idx != columns.size() - 1 ? SEP : "") | |
OUT.append(column.name()).append(" = ") | |
def value = dataRow.value(column) | |
def stringValue = value == null ? KW_NULL : FORMATTER.formatValue(value, column) | |
def isStringLiteral = value != null && FORMATTER.isStringLiteral(value, column) | |
if (isStringLiteral && DIALECT.getDbms().isMysql()) stringValue = stringValue.replace("\\", "\\\\") | |
OUT.append(isStringLiteral ? (STRING_PREFIX + QUOTE) : "") | |
.append(isStringLiteral ? stringValue.replace(QUOTE, QUOTE + QUOTE) : stringValue) | |
.append(isStringLiteral ? QUOTE : "") | |
.append(idx != columns.size() - 1 ? SEP : "") | |
} | |
def keyValue = FORMATTER.formatValue(dataRow.value(MATCHING_KEY), MATCHING_KEY) | |
OUT.append(KW_MATCHER) | |
.append(MATCHING_KEY.name()) | |
.append(" = ") | |
.append(keyValue) | |
OUT.append(";").append(NEWLINE) | |
} | |
ROWS.each { row -> record(COLUMNS, row) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment