background image
<< Performing referential actions | Methods called for declared SQL types >>
<< Performing referential actions | Methods called for declared SQL types >>

Programming Derby-style table functions

Derby Developer's Guide
51
Trigger B, it is as if Trigger A had thrown the exception. In that case, the statement that
caused Trigger A to fire is rolled back, along with any work done by both of the triggers.
Aborting statements and transactions
You might want a trigger action to be able to abort the triggering statement or even the
entire transaction.
Triggers that use the current connection are not permitted to commit or roll back the
connection, so how do you do that? The answer is: have the trigger throw an exception,
which is by default a statement-level exception (which rolls back the statement). The
application-side code that contains the statement that caused the trigger to fire can then
roll back the entire connection if desired. Programming triggers in this respect is no
different from programming any database-side JDBC method.
Programming Derby-style table functions
Derby lets you create table functions. Table functions are functions which package up
external data to look like Derby tables. The external data can be an XML file, a table in a
foreign database, a live data feed--in short, any information source that can be presented
as a JDBC ResultSet.
Derby-style table functions let you efficiently import foreign data into Derby tables. Table
functions let you join Derby tables with any of the following data sources:
· XML-formatted reports and logs
· Queries that run in foreign databases
· Streaming data from sensors
· RSS feeds
See "CREATE FUNCTION statement" in the Derby Reference Manual for the complete
syntax needed to declare Derby-style table functions. The following topics provide
information on how to write Java methods which wrap foreign data sources inside
ResultSets.
Overview of Derby-style table functions
A Derby-style table function is a method which returns a JDBC ResultSet.
Most of the ResultSet methods can be written as stubs which simply raise exceptions.
However, the Derby-style table function must implement the following ResultSet
methods:
· next()
· close()
· wasNull()
· getXXX() - When invoking a Derby-style table function at runtime, Derby calls a
getXXX() method on each referenced column. The particular getXXX() method is
based on the column's data type as declared in the
CREATE FUNCTION
statement.
Preferred getXXX() methods for Derby-style table functions
explains how Derby
selects an appropriate getXXX() method. However, nothing prevents application
code from calling other getXXX() methods on the ResultSet. The returned ResultSet
needs to implement the getXXX() methods which Derby will call as well as all
getXXX() methods which the application will call.
A Derby-style table function is materialized by a public static method which returns a
ResultSet:
public static ResultSet read() {...}