Skip to content

Instantly share code, notes, and snippets.

@TheConstructor
Created November 5, 2019 23:24
Show Gist options
  • Save TheConstructor/f3d10344317dd826afdc68775ecd067b to your computer and use it in GitHub Desktop.
Save TheConstructor/f3d10344317dd826afdc68775ecd067b to your computer and use it in GitHub Desktop.
SEP = ", "
QUOTE = "\'"
NAME_START = "["
NAME_END = "]"
NEWLINE = System.getProperty("line.separator")
KEYWORDS_LOWERCASE = com.intellij.database.util.DbSqlUtil.areKeywordsLowerCase(PROJECT)
KW_VALUES = KEYWORDS_LOWERCASE ? "( values" : "( VALUES"
KW_NULL = KEYWORDS_LOWERCASE ? "null" : "NULL"
def record(columns, dataRow, rowNo) {
if(rowNo != 0) { OUT.append(",")}
OUT.append(" (")
columns.eachWithIndex { column, idx ->
def value = dataRow.value(column)
def skipQuote = value.toString().isNumber() || value == null
def stringValue = value != null ? FORMATTER.format(dataRow, column) : KW_NULL
if (DIALECT.getDbms().isMysql()) stringValue = stringValue.replace("\\", "\\\\")
OUT.append(skipQuote ? "": QUOTE).append(stringValue.replace(QUOTE, QUOTE + QUOTE))
.append(skipQuote ? "": QUOTE).append(idx != columns.size() - 1 ? SEP : "")
}
OUT.append(")")
}
OUT.append(KW_VALUES)
ROWS.eachWithIndex { row, rowNo -> record(COLUMNS, row, rowNo) }
OUT.append(") ")
if (TABLE == null) OUT.append("VDT")
else OUT.append(NAME_START).append(TABLE.getParent().getName().replace(NAME_END, NAME_END + NAME_END)).append(".").append(TABLE.getName().replace(NAME_END, NAME_END + NAME_END)).append(NAME_END)
OUT.append(" (")
COLUMNS.eachWithIndex { column, idx ->
OUT.append(NAME_START).append(column.name().replace(NAME_END, NAME_END + NAME_END)).append(NAME_END).append(idx != COLUMNS.size() - 1 ? SEP : "")
}
OUT.append(")").append(NEWLINE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment