-
-
Save pthiers/6afa65b68947770e5b7ec07bec2117f2 to your computer and use it in GitHub Desktop.
| /* | |
| * 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 = "\'" | |
| NEWLINE = System.getProperty("line.separator") | |
| count = 0; | |
| OUT.append("[").append(NEWLINE) | |
| def printRow = { values, valueToString -> | |
| OUT.append("\t[").append(NEWLINE) | |
| values.eachWithIndex { value, idx -> | |
| def str = valueToString(value) | |
| str = str.replace("'","\\'") | |
| OUT.append("\t\t'").append(value.name()).append("' => ") | |
| if(str == "NULL" || str.isNumber()) { | |
| OUT.append(str).append(",") | |
| } else { | |
| OUT.append("'").append(str).append("',") | |
| } | |
| OUT.append(NEWLINE) | |
| } | |
| OUT.append("\t],") | |
| OUT.append(NEWLINE) | |
| } | |
| ROWS.each { row -> printRow(COLUMNS, { FORMATTER.format(row, it) }) } | |
| OUT.append("];") |
Thank you so much !!
Thank you
Thank you!
Based on this, but added tabs and newlines for more readability
/*
* 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 = "\'"
NEWLINE = System.getProperty("line.separator")
count = 0;
OUT.append("[").append(NEWLINE)
def printRow = { values, valueToString ->
OUT.append("\t[").append(NEWLINE)
values.eachWithIndex { value, idx ->
def str = valueToString(value)
str = str.replace("'","\'")
OUT.append("\t\t'").append(value.name()).append("' => ")
if(str == "NULL") {
OUT.append(str).append(",")
} else {
OUT.append("'").append(str).append("',")
}
OUT.append(NEWLINE)
}
OUT.append("\t],")
OUT.append(NEWLINE)
}
ROWS.each { row -> printRow(COLUMNS, { FORMATTER.format(row, it) }) }
OUT.append("];")Based on this, but added tabs and newlines for more readability
/* * 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 = "\'" NEWLINE = System.getProperty("line.separator") count = 0; OUT.append("[").append(NEWLINE) def printRow = { values, valueToString -> OUT.append("\t[").append(NEWLINE) values.eachWithIndex { value, idx -> def str = valueToString(value) str = str.replace("'","\'") OUT.append("\t\t'").append(value.name()).append("' => ") if(str == "NULL") { OUT.append(str).append(",") } else { OUT.append("'").append(str).append("',") } OUT.append(NEWLINE) } OUT.append("\t],") OUT.append(NEWLINE) } ROWS.each { row -> printRow(COLUMNS, { FORMATTER.format(row, it) }) } OUT.append("];")
Cool!!! Now it's updated! Thanks!
I had an issue with single quotes in my values, so had to ammend…
str = str.replace("'","\'")
… for …
str = str.replace("'","\\'")
… in case it helps someone.
I had an issue with single quotes in my values, so had to ammend…
str = str.replace("'","\'")… for …
str = str.replace("'","\\'")… in case it helps someone.
That helped, thank you very much!
I had an issue with single quotes in my values, so had to ammend…
str = str.replace("'","\'")… for …
str = str.replace("'","\\'")… in case it helps someone.
Fixed! Thanks!!!
I was having an issue with some null values not being detected, as well as certain numbers (zip-codes starting with 0) being incorrectly parsed, so I replaced
if(str == "NULL") {
OUT.append(str).append(",")
} else {
OUT.append("'").append(str).append("',")
}
with
if(str == "NULL" || str == "null") {
OUT.append(str).append(",")
} else if (str.isNumber() && str ==~ /^(0\.\d+|[1-9]\d*|\d)$/) {
// The string is a number and doesn't start with 0
// unless it is 0, or a decimal starting with 0
OUT.append(str).append(",")
} else {
OUT.append("'").append(str).append("',")
}
the follow changes, should handle correctly also the boolean values:
SEPARATOR = ","
QUOTE = "\'"
NEWLINE = System.getProperty("line.separator")
OUT.append("[").append(NEWLINE)
def printRow = { columns, row ->
OUT.append("\t[").append(NEWLINE)
columns.each { col ->
def rawValue = row.value(col)
OUT.append("\t\t'").append(col.name()).append("' => ")
if (rawValue == null) {
OUT.append("null")
} else if (rawValue instanceof Boolean) {
// Converts boolean to PHP literals: true or false
OUT.append(rawValue ? "true" : "false")
} else if (rawValue instanceof Number) {
// Convert Number to String to avoid MissingMethodException
OUT.append(String.valueOf(rawValue))
} else {
// Handle strings: escape and wrap in quotes
def formattedValue = FORMATTER.format(row, col).replace("'", "\\'")
OUT.append("'").append(formattedValue).append("'")
}
OUT.append(",").append(NEWLINE)
}
OUT.append("\t],").append(NEWLINE)
}
ROWS.each { row -> printRow(COLUMNS, row) }
OUT.append("];")
Works like a charm. Thank you so much!!