Skip to main content
Example scripts
arrow icon
To homepage
Confluence
Data centre icon
Data Center

Display SQL Results from an External Database Macro

Features
Macros
Created 1 year ago, Updated 2 month(s) ago
App in script
ScriptRunner For Confluence
ScriptRunner For Confluence
by Adaptavist
Compatibility
compatibility bullet
Confluence (7.15 - 8.6)
compatibility bullet
ScriptRunner For Confluence (7.10.0)
Language |
groovy
import com.onresolve.scriptrunner.db.DatabaseUtil
import com.onresolve.scriptrunner.db.NoSuchDataSourceException
import groovy.xml.MarkupBuilder

/* You will replace 'petstore' with whatever you named your pool when
configuring your Resource. */
def rows
try {
    rows = DatabaseUtil.withSql('petstore') { sql ->
        sql.rows('select NAME, SPECIES, ID From petstore')
    }
}
catch (NoSuchDataSourceException e) {
    return "Data source is invalid: ${e.message}"
}

def writer = new StringWriter()
/* Notice our use of Groovy’s MarkupBuilder here. This is a helper class for creating XML or HTML markup.
It’s very important to use the MarkupBuilder here to ensure your HTML is safe. */
def builder = new MarkupBuilder(writer)

builder.table('class': 'aui') {
    tr {
        rows.first().keySet().each { key ->
            th {
                mkp.yield(key)
            }
        }
    }
    rows.each { columns ->
        tr {
            columns.each { Map.Entry cell ->
                td {
                    if (cell.value) {
                        mkp.yield(cell.value)
                    }
                }
            }
        }
    }
}

writer.toString()
Having an issue with this script?
Report it here