Skip to content

Instantly share code, notes, and snippets.

@pthiers
Last active May 11, 2026 10:20
Show Gist options
  • Select an option

  • Save pthiers/6afa65b68947770e5b7ec07bec2117f2 to your computer and use it in GitHub Desktop.

Select an option

Save pthiers/6afa65b68947770e5b7ec07bec2117f2 to your computer and use it in GitHub Desktop.
datagrip php array 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 = "\'"
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("];")
@BenjaminBrandtner

Copy link
Copy Markdown

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!

@pthiers

pthiers commented Jul 9, 2021

Copy link
Copy Markdown
Author

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!!!

@whitefang57

Copy link
Copy Markdown

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("',")
}

@fabiov

fabiov commented May 11, 2026

Copy link
Copy Markdown

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("];")

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