Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@dacastro4 dacastro4 commented Nov 30, 2018

Works like a charm. Thank you so much!!

@jerfeson

This comment has been minimized.

Copy link

@jerfeson jerfeson commented Aug 4, 2019

Thank you so much !!

@jgusta

This comment has been minimized.

Copy link

@jgusta jgusta commented Apr 1, 2020

Thank you

@stepchik

This comment has been minimized.

Copy link

@stepchik stepchik commented May 6, 2020

Thank you!

@mrbig00

This comment has been minimized.

Copy link

@mrbig00 mrbig00 commented Nov 26, 2020

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

This comment has been minimized.

Copy link
Owner Author

@pthiers pthiers commented Dec 9, 2020

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

This comment has been minimized.

Copy link

@toby-griffiths toby-griffiths commented Feb 5, 2021

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.

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