Web services data sources - example queries
Examples of SQL queries for the Web services wrapper are shown.
Example 1: Using materialized query tables
- Create a local or base table:
You can use the local table to maintain all the values that you want to cache.CREATE TABLE mystocks(ticker VARCHAR(10)); - Insert all of the values that you want to cache into the table:
INSERT INTO mystocks VALUES('IBM'); INSERT INTO mystocks VALUES('MSFT'); ... - Create a Web services nickname:
CREATE NICKNAME stockquote_nn ( ticker VARCHAR(40) OPTIONS (TEMPLATE '&column'), price VARCHAR(16) OPTIONS (XPATH './Result/text()') ) FOR SERVER stock_server OPTIONS (TEMPLATE '<ticker>&column</ticker>' XPATH './Result/text()' ); - Create a view that consists of the nickname and the local table:
CREATE VIEW stock_quote_view (ticker, price) AS ( SELECT nn.ticker, nn.price FROM stockquote_nn nn, mystocks s WHERE nn.ticker=s.ticker ); - Create a materialized query table:
Include all of the VARCHAR columns in the join predicate (nn.ticker and s.ticker) in the materialized query table output list to maximize the opportunities that the materialized query table is used by the federated database.CREATE TABLE stockquote_MQT (ticker, ticker2, price) as (SELECT nn.ticker,s.ticker as ticker2, nn.price FROM stockquote_nn nn, mystocks s WHERE nn.ticker=s.ticker ) DATA INITIALLY DEFERRED REFRESH DEFERRED;To defer the refresh of the materialized query table, specify the REFRESH DEFERRED keyword. Materialized query tables that are specified with the REFRESH DEFERRED keyword do not reflect changes to the underlying base table. Use the clause DATA INITIALLY DEFERRED so that your data is not inserted into the table as part of the CREATE TABLE statement.
- Issue a REFRESH TABLE statement to populate the table. The data
in the table reflects the result of the query as a snapshot at the
time that you issue the REFRESH TABLE statement. The following example
populates the stockquote_MQT table and sets a value for the special
register with the current refresh age.
REFRESH TABLE stockquote_MQT; SET CURRENT REFRESH AGE any;
SELECT * FROM stock_quote_view
WHERE ticker='IBM';If you issue a query to select a
value that has not been cached, 0 rows are returned.Example 2: Issuing joins using the primary and foreign keys
The PRIMARY_KEY and FOREIGN_KEY columns define relationships between the parent and child nicknames. Each parent nickname must have a primary key column option. You define the children of a parent nickname with the foreign key column option that references the primary key column of a parent nickname. A nickname can have multiple children, but a nickname can have only one parent.
Because these columns contain only binary data, the columns are defined with the FOR BIT DATA NOT NULL keywords. You can explicitly define the PRIMARY_KEY and FOREIGN_KEY columns as FOR BIT DATA NOT NULL when you create the nickname.
CREATE NICKNAME zooport_getzooreport_nn (
zooid VARCHAR (48) OPTIONS(TEMPLATE '&column'),
zoo_id VARCHAR (48) OPTIONS(XPATH './ns1:Zoo/@id'),
report_zoo_zooname VARCHAR (48)
OPTIONS(XPATH './ns1:Zoo/ns1:ZooName/text()'),
report_zoo_count VARCHAR (48)
OPTIONS(XPATH './ns1:Zoo/ns1:Count/text()'),
report_zoo_lastmodified VARCHAR (48)
OPTIONS(XPATH './ns1:Zoo/ns1:LastModified/text()'),
nn_pk VARCHAR (16) NOT NULL OPTIONS(PRIMARY_KEY 'YES'),
url VARCHAR (256) OPTIONS(URLCOLUMN 'Y'),
soapaction VARCHAR (256) OPTIONS(SOAPACTIONCOLUMN 'Y')
) FOR SERVER "mytestsrvr"
OPTIONS(
URL 'http://localhost:9080/MaelstromTest/services/ZooPort',
SOAPACTION 'http://myzoo.com/getZooReport' ,
TEMPLATE '<soapenv:Envelope>
<soapenv:Body>
<zooId>&zooId[1,1]</zooId>
</soapenv:Body>
</soapenv:Envelope>',
XPATH '/soapenv:Envelope/soapenv:Body' ,
NAMESPACES ' soapenv="http://schemas.xmlsoap.org/soap/envelope/",
ns1="http://myzoo.com" '); CREATE NICKNAME zooport_getzooreport_report_zookeeper_nn (
nn_fk VARCHAR (16) NOT NULL
OPTIONS(FOREIGN_KEY 'ZOOPORT_GETZOOREPORT_NN'),
zookeeper_id VARCHAR (48) OPTIONS(XPATH './@id'),
report_zookeeper_name VARCHAR (48) OPTIONS(XPATH './ns1:Name/text()'),
zookeeper_numbercages VARCHAR(48)
OPTIONS(XPATH './ns1:NumberOfCages/text()'),
nn_pk VARCHAR (16) NOT NULL OPTIONS(PRIMARY_KEY 'YES')
)
FOR SERVER "MYTESTSRVR" OPTIONS(
XPATH './ns1:Zoo/ns1:Zookeeper' ,
NAMESPACES ' soapenv="http://schemas.xmlsoap.org/soap/envelope/",
ns1="http://myzoo.com" ');The foreign key, nn_fk, in nickname zooport_getzooreport_report_zookeeper_nn, refers to the parent nickname, zooport_getzooreport_nn in the foreign key option. The designated primary and foreign key nickname columns do not correspond to data in your WSDL document because these nickname columns contain keys that are generated by the wrapper. These keys identify a relationship between the parent and child nicknames that is unique only within a query. If the child nickname contains an input column, the parent nickname option template refers to that child nickname in the template structure with the nickname option.
SELECT *
FROM zooport_getzooreport_nn a,
zooport_getzooreport_report_zookeeper_nn z,
WHERE a.nn_pk = z.nn_fk
AND a.zooid = 100
;The following description explains how the Web services wrapper uses the TEMPLATE and XPATH nickname and column options during query execution. It is not intended as an example of a specific implementation.
When you join the primary and foreign key columns, the Web services wrapper sends a message to the Web services provider, and a set of rows is returned from the Web services provider. The wrapper generates a message for the parent nickname by substituting the values of the input column (a.zooid = 100) from the query for the reference in the column option template (ZOOID VARCHAR (48) OPTIONS(TEMPLATE '&column')), and then all of the column references in the nickname template option (<zooId>&zooId[1,1]</zooId>). The nickname template option can include column references or child nickname references. The message is then sent to the Web service.
The wrapper generates the rows for a nickname by applying the nickname option XPATH on the document that the Web service returns. If the nickname option XPATH returns multiple XML fragments, then the nickname contains multiple rows. The column XPATH option is applied on the resulting XML fragments that represent the rows to get the column values. If a nickname has one or more indirect parents, all of the parent nickname XPATH expressions are applied in the order from the top of the hierarchy down before the nickname option XPATH and the column option XPATH are applied for this nickname.