Forked from leo-from-spb/SQL-Inserts-MultirowSynthax.sql.groovy
Last active
April 21, 2020 10:35
-
-
Save T3chW1zard/c647f37bb4470ff599cca9aeceae4754 to your computer and use it in GitHub Desktop.
DataGrip data exporting script (1000 inserts at a time)
This file contains hidden or 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
SEP = ", " | |
QUOTE = "\'" | |
NEWLINE = System.getProperty("line.separator") | |
begin = true | |
maxLines = 1000 | |
currentLine = 0 | |
def record(columns, dataRow) { | |
currentLine = currentLine + 1 | |
if (currentLine == maxLines) { | |
begin = true | |
currentLine = 0 | |
OUT.append(NEWLINE).append("GO").append(NEWLINE) | |
} | |
if (begin) { | |
OUT.append("INSERT INTO ") | |
if (TABLE == null) OUT.append("MY_TABLE") | |
else OUT.append(TABLE.getParent().getName()).append(".").append(TABLE.getName()) | |
OUT.append(" (") | |
columns.eachWithIndex { column, idx -> | |
OUT.append(column.name()).append(idx != columns.size() - 1 ? SEP : "") | |
} | |
OUT.append(")").append(NEWLINE) | |
OUT.append("VALUES").append(" (") | |
begin = false | |
} | |
else { | |
OUT.append(",").append(NEWLINE) | |
OUT.append(" (") | |
} | |
columns.eachWithIndex { column, idx -> | |
def skipQuote = dataRow.value(column).toString().isNumber() || dataRow.value(column) == null | |
def stringValue = FORMATTER.format(dataRow, column) | |
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(")") | |
} | |
ROWS.each { row -> record(COLUMNS, row) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment