Database Reference
In-Depth Information
perspective, this is perfectly reasonable. These “checked” exceptions represent poten-
tially serious problems for which we should have plans in place. From another perspec-
tive, it is a waste of energy to force the programmer to check for an exception that,
through some means, can be assured never to happen. taking this second view, groovy
does not enforce these required exception handling rules. All exceptions are treated as
“unchecked,” meaning it is up to the developer to decide whether to write handling code
for them. This way, when exception handling is found in groovy code, we can be sure
it is there by choice.
Multiline strings —groovy allows a “triple-quote” string literal that makes creating
formatted multiline strings easier. These strings, sometimes called “heredocs,” treat line
breaks literally and do not need to be assembled from smaller strings.
thesisTitle = """\
Dr. Groovy
or: How I Learned to Stop Worrying
and Love the Closure """
The backslash on the first line convinces groovy to ignore the line break that imme-
diately follows, so that the first line of our string can start at the left margin, just like the
rest of the lines.
Currying —groovy provides a method to permanently lock down the values of one
or more closure arguments. For example:
defragDb = { app, db -> code_that_defrags_a_database() } // takes 2
parameters
defragSampleDb = defragDb.curry('Sample')
['Basic', 'Interntl', 'Xchgrate'].each { defragSampleDb(it) }
//takes only one
This is a concept taken from “functional” programming, where generic functions are
frequently defined, with more specific ones being derived from them in the manner as
above. he term currying is named for haskell Curry, an important figure in the func-
tional programming world.
This concludes our whirlwind tour of groovy. While we were not able to go very
deeply into each feature, we hope we have provided a nice jump start for the aspiring
groovy developer. We hope, at least, that we have provided enough information for the
rest of the chapter to make some sense.
9.5 what's Cookin'? groovy reCipes For the Busy Developer
What follows are several scripts that represent the minimum amount of code neces-
sary to get a number of different jobs done. For production code, it would be better
to lace them with try/catch/finally blocks as described in the previous chapter. most
of the snippets are fully functional programs, except that the correct strings for your
environment may need to be filled in where appropriate.
9.5.1 Clearing and Loading Data
our first recipe shows how to load data into Sample.Basic from data files that oracle
provides. It also clears existing data from the database. We demonstrate how we could
Search WWH ::




Custom Search