Created
January 28, 2021 23:34
-
-
Save siosio/3ba6abfeb7a73869218510a43319b336 to your computer and use it in GitHub Desktop.
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_INSERT_INTO = "insertInto" | |
KW_VALUES = KEYWORDS_LOWERCASE ? "values" : "VALUES" | |
KW_NULL = KEYWORDS_LOWERCASE ? "null" : "NULL" | |
begin = true | |
def record(columns, dataRow) { | |
if (begin) { | |
OUT.append(KW_INSERT_INTO) | |
OUT.append("(\"") | |
if (TABLE == null) { | |
OUT.append("MY_TABLE") | |
} else { | |
OUT.append(TABLE.getParent().getName()).append(".").append(TABLE.getName()) | |
} | |
OUT.append("\") {").append(NEWLINE) | |
OUT.append("columns(") | |
columns.eachWithIndex { column, idx -> | |
OUT.append('"').append(column.name()).append('"').append(idx != columns.size() - 1 ? SEP : "") | |
} | |
OUT.append(")").append(NEWLINE) | |
begin = false | |
} | |
OUT.append("values(") | |
columns.eachWithIndex { column, idx -> | |
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(stringValue ? stringValue.replace(QUOTE, QUOTE + QUOTE) : stringValue) | |
.append(isStringLiteral ? QUOTE : "") | |
.append(idx != columns.size() - 1 ? SEP : "") | |
} | |
OUT.append(")").append(NEWLINE) | |
} | |
ROWS.each { row -> record(COLUMNS, row) } | |
OUT.append("}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment