Database Reference
In-Depth Information
grid = null; cv?.close(); cv = null;
essCube.clearActive(); essSvr.disconnect(); essHome.signOff();
allmatch = true
sqlVals.each{ key, val ->
println "${key}: ${val} vs. ${essVals[key]} match:
${val==essVals[key]}"
if (val != essVals[key]) { allmatch = false }
}
System.exit(allmatch ? 0 : 1)
This script builds two maps with identical keys and verifies they have identical values.
Actually, essvals has an extra entry (“Diet”), but we do not test this shared member. We
let sqlvals drive the comparison loop, and that member is not in there.
Since the SQL query part is made up, the only way to get this code to run is to com-
ment out that section and add the line below, which populates the map with the correct
values.
sqlVals = ['100':106134 ,'200':109086, '300':101405, '400':84230]
The 1..<grid.countrows we use to loop on the grid rows is a special “exclusive” syntax
for ranges, which stops just short of executing on the upper bound (see Section 9.4.6).
The allmatch ? 0 : 1 construction is using the common C-style ternary operator. If the
first segment evaluates to true, then the middle segment is returned, otherwise the last
segment is returned.
We end the script by exiting with a numeric code: 0 is good, 1 is bad. This is in antici-
pation of the next recipe.
9.5.7 Calling Other Scripts
We may write some very useful utility scripts that we want to use from within our other
scripts. We need to know how to launch external scripts and get information back from
them. There are a number of ways. We demonstrate three ways to call the script defined
in the previous recipe, assuming it has been saved to a file named QueryData.groovy.
The first way uses an execute method that groovy adds to String objects:
proc = "groovy QueryData.groovy".execute()
inStr = proc.in.text //the text _out_ of the child process comes
_in_ to us
errStr = proc.err.text //a separate stream of error text
//bothStr = proc.text //combined in & err: have to use EITHER this
OR the lines above
sts = proc.exitValue()
if (sts) {println "Oh, No! Sample.Basic is out of balance!"}
else {println "WOO HOO!"}
now. we see why we put in the System.exit call above. We can access that exit code
from other scripts using this technique.
Another technique uses the AntBuilder class. Ant is a Java-based tool that uses
xmL configuration files to run complex build processes for applications. Among the
many tasks it can do, such as compile, copy, and test files, is the ability to run pro-
grams. groovy ships with Ant included plus a builder for xmL structures that Ant
can process.
Search WWH ::




Custom Search