Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example how to convert a result set (not a list of domain objects) #29

Open
hansgru opened this issue Jan 26, 2015 · 5 comments
Open

Example how to convert a result set (not a list of domain objects) #29

hansgru opened this issue Jan 26, 2015 · 5 comments

Comments

@hansgru
Copy link

hansgru commented Jan 26, 2015

Please add an example of how to export a result set (not just list of domain objects)

@jakubnabrdalik
Copy link
Collaborator

Not sure, what you mean. What class do you want to convert? Can you give an example?

@hansgru
Copy link
Author

hansgru commented Jan 26, 2015

Not sure, what you mean. What class do you want to convert? Can you give an example?

I don't want to convert a domain object but directly the result set of a direct SQL Select query.
Of course, I could convert the result in a list of maps and than pass it wot the excel-export closure, but that would be quite inefficient.

This is mostly required, since reports are too complicated to be able to express them as simple objects queries, and also span too many entities, hence the requirement for direct SQLs (resultset to XSLX) conversion.

@jakubnabrdalik
Copy link
Collaborator

Now I get it. But I don't have an example (never had that business case in grails).
Perhaps you can make a pull request, once you have that working?

@hansgru
Copy link
Author

hansgru commented May 8, 2015

Perhaps you can make a pull request, once you have that working?

I have it working as I mentioned above: " I could convert the result in a list of maps and than pass it wot the excel-export closure, but that would be quite inefficient."

class ExportController {
    def dataSource
    def reportOne() {
        def sql = new Sql(dataSource)

        // file with complicated SQL
        def filePath = "resources/sqls/export_report_one.sql"

        // This line lets the application know where the file is located:
        def fileHolder = grailsApplication.parentContext.getResource("classpath:$filePath")

        // headers to extract from the result
        def headers = ['col1','col2', ...]  

        // copy everything from the resultset.
        def exports  = []
        sql.eachRow(sqlString, {
            exports << it.toRowResult()
        })
        def exportFile = "export_report_one_"+(new Date().format("yyyy-MM-dd_HH-mm-ss"))+".xlsx"
        response.setHeader("Content-Disposition", "attachment; filename=${exportFile}");

        new WebXlsxExporter().with {
            fillHeader(headers)
            add(exports, headers)
            save(response.outputStream)
        }
    }
}

POI has a streaming API however, so that must be used in order to be able to push something directly from the resultset.

@jravinder
Copy link

I think you can simply use the results from the sql.rows() as is

       results = sql.rows(query)

        WebXlsxExporter webXlsxExporter = new WebXlsxExporter()

        webXlsxExporter.with {
            setResponseHeaders(response)
            fillHeader(headers)
            add(results, withProperties)
            save(response.outputStream)
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants