Skip to content

Instantly share code, notes, and snippets.

@leo-from-spb
Last active November 5, 2024 07:56
Show Gist options
  • Save leo-from-spb/ec438f1fe9995b910ad816aabe47dd4f to your computer and use it in GitHub Desktop.
Save leo-from-spb/ec438f1fe9995b910ad816aabe47dd4f to your computer and use it in GitHub Desktop.
DataGrip data exporting script
SEP = ", "
QUOTE = "\'"
NEWLINE = System.getProperty("line.separator")
begin = true
def record(columns, dataRow) {
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) }
@ProjectCleverWeb
Copy link

A version of this that supports batch inserts may be found here: https://gist.github.com/ProjectCleverWeb/d2362b082af1d7054ebfd464f202ec1b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment