Skip to content

Instantly share code, notes, and snippets.

@erickvieira
Created January 31, 2022 19:06
Show Gist options
  • Save erickvieira/9e4bb9415790e20647f1c3e0ba3bae6f to your computer and use it in GitHub Desktop.
Save erickvieira/9e4bb9415790e20647f1c3e0ba3bae6f to your computer and use it in GitHub Desktop.
SQL-IN-Groovy Datagrip extractor
/*
* Available context bindings:
* COLUMNS List<DataColumn>
* ROWS Iterable<DataRow>
* OUT { append() }
* FORMATTER { format(row, col); formatValue(Object, col) }
* TRANSPOSED Boolean
* plus ALL_COLUMNS, TABLE, DIALECT
*
* where:
* DataRow { rowNumber(); first(); last(); data(): List<Object>; value(column): Object }
* DataColumn { columnNumber(), name() }
*/
SEPARATOR = ", "
QUOTE = "'"
Map<String, List<Object>> map = new LinkedHashMap<>()
ROWS.each { row ->
COLUMNS.each { column ->
value = FORMATTER.format(row, column)
map.computeIfAbsent((String) column.name(), { k -> new LinkedList<>() })
.add(FORMATTER.format(row, column))
}
}
firstCol = true
map.entrySet().each { e ->
if (!firstCol) {
OUT.append("\n AND ")
}
firstRow = true
OUT.append(e.key)
.append(" IN (")
e.getValue().each { value ->
def q = !value.isNumber()
OUT.append(firstRow ? "" : SEPARATOR)
.append(q ? QUOTE : "")
.append(value.replace(QUOTE, QUOTE + QUOTE))
.append(q ? QUOTE : "")
firstRow = false
}
OUT.append(")")
firstCol = false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment