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

dacastro4 commented Nov 30, 2018

Copy link
Copy Markdown

Works like a charm. Thank you so much!!

@jerfeson

jerfeson commented Aug 4, 2019

Copy link
Copy Markdown

Thank you so much !!

@jgusta

jgusta commented Apr 1, 2020

Copy link
Copy Markdown

Thank you

@stepchik

stepchik commented May 6, 2020

Copy link
Copy Markdown

Thank you!

@mrbig00

mrbig00 commented Nov 26, 2020

Copy link
Copy Markdown

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

@pthiers

pthiers commented Dec 9, 2020

Copy link
Copy Markdown
Author

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!

@toby-griffiths

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.

@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