Oracle Stored Procs in Grails
If you find yourself in a situation where you need to wire up a legacy Oracle stored procedure to a grails controller action, groovy makes this pretty simple. In my example I have a stored procedure with a single parameter that returns results through a reference cursor. Here’s our stored proc:
Obviously this is a pretty contrived example. You’re not likely to call an oracle stored procedure just to transform a string to uppercase (unless you’re a complete moron), but it serves as a simplified version of what you might run into when trying to incorporate existing Oracle procs into your application.
Now, let’s take a look at a controller that calls this proc and renders the value it grabs from the cursor:
So first off we’re injecting the dataSource so we can make our call to the Oracle database we’re using as part of our application. You’ll also see that we can iterate through the reference cursor results by iterating over the cursorResults variable (of type GroovyResultSet) in the sql.call closure. This is how you can grab the individual values you need from your cursor.
If you need to get at a field that isn’t properly aliased in your proc you can still access it by its index. For example the follow would also work in the above controller:
See Also: Groovy Database Features