The example below shows the working code. I want to make working code one more step and save the SQL in an XML file. However, as soon as I read it from the XML file, I cannot get groovy to process the SQL statement as a GString.
Here is a working example:
private void testRefCursors(){
//object owner paramter
String owner = 'HR';
//Block of Oracle SQL to execute, returning 4 parameters
def sqlBlock =
"""
declare
type crsr is ref cursor;
tables crsr;
objects crsr;
begin
select count(*) into ${Sql.INTEGER} from all_tables where owner= ${owner} ;
open tables for select * from all_tables where owner= ${owner} ;
${Sql.resultSet OracleTypes.CURSOR} := tables;
select count(*) into ${Sql.INTEGER} from all_objects where owner= ${owner} ;
open objects for select * from all_objects where owner= ${owner};
${Sql.resultSet OracleTypes.CURSOR} := objects;
end;
"""
//note the order below, is the order of the 'types'
//in the SQL block used in the closure as parameters.
sqlSEDREF.call(sqlBlock){
t,user_tables,o,user_objects ->
println "found ${t} tables from a total of ${o} objects"
user_tables.eachRow(){x ->println "table:${x.table_name}"}
user_objects.eachRow(){println "object:${it.object_name}"}
}
}
Now when I change the example to read the SQL block from the XML file; I don't know how (or if possible) to treat this value as a GString:
private void testRefCursors(){
String owner = 'HR';
def configFile = new File("config.xml");
config = new XmlSlurper().parse(configFile);
GString sqlBlock = config.'sql-test-cursors'
sqlSEDREF.call(sqlBlock){
t,user_tables,o,user_objects ->
println "found ${t} tables from a total of ${o} objects"
user_tables.eachRow(){x ->println "table:${x.table_name}"}
user_objects.eachRow(){println "object:${it.object_name}"}
}
}
The returned error is the result of the inability to use the implementation of GString SQL.call (GSTring, Closure):
Caught: groovy.lang.MissingMethodException: No signature method: groovy.sql.Sql.call () is applicable for argument types: (Java.lang.String, tools.UAT $ _testRefCursors_closure2)
config.xml CONFIG.groovy GStrings .groovy XML. .