SDO Code Sample

Code Sample using Dynamic SDO API

The SDO API is dynamic in nature, allowing applications to be built without the need for code generation.

DataGraph graph = db.executeQuery( select * from customer );
DataObject root = graph.getRootObject();

// use xpath to get the name of the first customer
String firstCustName = root.getString( customer[0]/name );

// iterate through all customers
Iterator iter = root.getList( customer ).iterator();
while (iter.hasNext()) {
DataObject dataObject = (DataObject) iter.next();
String custName = dataObject.getString( name );
}

Code Sample using Code Generated Static SDO API

FireStorm/SDO can generate static classes that extend DataObject and provide schema-specific type-safe methods for accessing data while still allowing the dynamic SDO API methods to be used. Using the code-generated classes, it is possible to cast DataObjects to specific application classes (in this example, Customer is a generated class that extends DataObject).

// iterate through all customers
Iterator iter = root.getList( customer ).iterator();
while (iter.hasNext()) {
Customer customer = (Customer) iter.next();
String custName = customer.getName();
}

SDO Example: SDO Metadata

As well as providing a standardized API for accessing data across multiple data sources, SDO also provides a common API for accessing metadata about the data source. While the SDO data access API provides DataGraph and DataObject interfaces for accessing and updating data, the SDO metadata API provides Type and Property interfaces.

// create the Data Access Service
SQLDataAccessService das = SQLDataAccessServiceFactory.create( "sampledb" );

// select some rows from the customer table
DataGraph data = das.selectRows( "customer", "state = ?", new Object[] { "MA" } );
DataObject root = data.getRootObject();

// retrieve a customer and query its type
DataObject customer = root.getDataObject( "customer.0" );
Type customerType = customer.getType();

// look up a property (column) of the customer object
Property idProp = customerType.getProperty( "id" );

// examine the data type of the "id" property
assertEquals( "commonj.sdo", idProp.getType().getURI() );
assertEquals( "Int", idProp.getType().getName() );

SDO Example: SDO Scripting

<%@ include file="header.jsp"%>

<h1>Customers</h1>

<table border="1">
<tr>
<td>Customer Name</td>
<td>Country</td>
<td>Licenses</td>
</tr>

<%
// get a list of customer objects
List list = das.getTableRows( "customer", "1=1" );

// iterate through the list
Iterator iter = list.iterator();
while (iter.hasNext()) {
DataObject customer = (DataObject) iter.next();
%>
<tr>
<td><%= customer.getString( "name") %></td>
<td><%= customer.getString( "country") %></td>
<td><%= customer.getString( "licenses") %></td>
</tr>

<%
}
%>

</table>

<%@ include file="footer.jsp"%>

Download FireStorm/SDO with SDO Code Sample

FireStorm/SDO includes a number of SDO code samples and tutorials.

CodeFutures has jointly developed FireStorm/SDO as part of an alliance with Rogue Wave Software.

You can download the product, branded as HydraSDO for Databases, from the Rogue Wave Download Center.