| |
"An Introduction to Service Data Objects"
Vol. 9, Issue 10, p. 16
Listing 1
//Create the basic schema description
//This describes the physical database table
//used to populate the datagraph
MetadataFactory mFactory = MetadataFactory.eINSTANCE.
Metadata metadata = mFactory.createMetadata();
Table custTable = metadata.addTable("CUSTOMER");
//The generated SQL SELECT will start from the single specified
//root table
custTable.beRoot();
Column custID = custTable.addIntegerColumn("CUSTOMERID");
custID.setNullable(false);
custTable.addStringColumn("CUSTFIRSTNAME");
custTable.addStringColumn("CUSTLASTNAME");
custTable.setPrimaryKey(custID);
//Add a "lastname" filter to the Customer table descriptor
Filter filter = mFactory.createFilter();
filter.setPredicate("CUSTOMER.CUSTLASTNAME = ?");
FilterArgument arg = mFactory.createFilterArgument();
arg.setName("CUSTLASTNAME");
arg.setType(Column.STRING);
filter.getFilterArguments().add(arg);
custTable.setFilter(filter);
Listing 2
//Prior to this point, the application must have
// acquired a JDBC connection for the mediator to use
...
//Wrap the connection. The wrapper indicates whether or not the //DMS will actively
manage the transaction. The default factory
//method produces an "active" wrapper. Another method is //provided to create a
"passive" wrapper which allows
//participation in a larger transaction scope
ConnectionWrapperFactory factory =
ConnectionWrapperFactory.soleInstance;
wrapper = factory.createConnectionWrapper(connection);
JDBCMediatorFactory fact = MediatorFactoryImpl.soleInstance;
JDBCMediator mediator = fact.createMediator(metadata, wrapper);
Listing 3
<?xml version="1.0" encoding="ASCII"?>
<com.ibm.websphere.sdo.mediator.jdbc.metadata:Metadata xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI" xmlns:com.ibm.websphere.sdo.mediator.jdbc.metadata=
"http:///com/ibm/websphere/sdo/mediator/jdbc/metadata.ecore"
rootTable="//@tables.0">
<tables name="CUSTOMER">
<primaryKey columns="//@tables.0/@columns.0"/>
<columns name="CUSTOMERID"/>
<columns name="CUSTFIRSTNAME" type="4" nullable="true"/>
<columns name="CUSTLASTNAME" type="4" nullable="true"/>
<filter predicate="CUSTOMER.CUSTLASTNAME = ?">
<filterArguments name="CUSTLASTNAME" type="4"/>
</filter>
</tables>
</com.ibm.websphere.sdo.mediator.jdbc.metadata:Metadata>
Listing 4
//Prior to this point, the application must have
// acquired a JDBC connection for the Mediator to use
...
//Wrap the connection
ConnectionWrapperFactory factory =
ConnectionWrapperFactory.soleInstance;
wrapper = factory.createConnectionWrapper(connection);
InputStream stream = new FileInputStream("myMetadata.xmi");
JDBCMediatorFactory fact = MediatorFactoryImpl.soleInstance;
JDBCMediator mediator = fact.createMediator(stream, wrapper);
Listing 5
CountingPager pager =
PagerFactory.soleInstance.createCountingPager(5);
int count = pager.pageCount(mediator);
for (int pageNum = 1, pageNum <= count, pageNum++) {
DataObject graph = pager.page(pageNum, mediator);
//Iterate through all returned customers in this page
Iterator i = graph.getList("CUSTOMER").iterator();
while (i.hasNext()) {
DataObject cust = (DataObject) i.next();
System.out.println(cust.getString("CUSTFIRSTNAME"));
}
}
|
|