Database Xpath-extension Functions in SOA 12c

 In SOA, we can connect with databases using a database jca adapter and configuring the data source and connection factory in the WebLogic server. Sometimes, we may want to query the database directly within the BPEL component or XSLT without configuring a database jca adapter. For such use cases, we can take leverage of database functions that can be used within BPEL Assign Activity or XSLT transformations.

We have 3 different database functions available, that can be used in different use cases.

  1. oraeext:sequence-next-val()
  2. oraext:lookup-table()
  3. oraext:query-database()

1. oraext:sequence-next-val()

Syntax:

oraext:sequence-next-val(sequence as string, dataSource as string)
sequence: Specify the sequence available in the database for which the next value needs to be extracted.
dataSource: Specify the Data source JNDI name as configured in WebLogic server or JDBC string in the format jdbc:oracle:thin:username/password@host:port:sid
Note: Only oracle thin driver is supported if used as JDBC string.

This function is used when we want to fetch the next value of defined sequence in the database. It can be used in cases where we want each request to adhere with a particular sequence id which can be used to identify each unique record where using a db adapter might create overhead and extra transformation. This function is basically an alternative to the below SQL statement.

SELECT SEQUENCE_TEST.NEXTVAL FROM DUAL;

Here, SEQUENCE_TEST is the name of the sequence defined in the database.

Example usage in XSLT:

XSL:
<xsl:template match="/">
    <root>
      <seqJDBCString><xsl:value-of select="oraext:sequence-next-val('SEQUENCE_TEST','jdbc:oracle:thin:mark1/password@localhost:1521:XE' )"/></seqJDBCString>
    </root>
  </xsl:template>
OUTPUT:
<root>
   <seqJDBCString>2</seqJDBCString>
</root>

2. oraext:lookup-table()

Syntax

oraext:lookup-table(tableName as string, inputColumn as string, inputData as string, outputColumn as string, dataSource as string)

tableName: Name of the table in database, from which value need to be fetched.
inputColumn: Column name of the table for which we will supply the data.
inputData: Data that will be provided as input to match a value in inputColumn in the table.
outputColumn: Column name of table for which value needs to fetched.
dataSource: Specify the Data source JNDI name as configured in WebLogic server or JDBC string in the format jdbc:oracle:thin:username/password@host:port:sid
Note: Only oracle thin driver is supported if used as JDBC string.

Suppose we have a table in our database which contains names of employee corresponding to the empIds. Now, we want to extract the name of employee corresponding to empId which we are getting in input. To do so, one way could be to have a db adapter created and use similar SQL statement as below to retrieve data.

SELECT EMP_NAME FROM EMPLOYEE_TABLE WHERE (EMP_ID=#inputId)

If we want to fetch the value directly in assign activity or xslt, we can use the oraext:lookup-table(). This can be treated as alternative approach to dvm lookup where in dvm files are stored local to composite or in MDS.

EMPLOYEE_TABLE

EMP_ID EMP_NAME
1002 DecipherMiddleware

INPUT:
<input xmlns="http://test.com/tns"> <empId>1002</empId>
</input>
XSL:
<xsl:template match="/">
    <root>
      <empName><xsl:value-of select="oraext:lookup-table('EMPLOYEE_TABLE','EMP_ID',/ns0:input/ns0:/empId,'EMP_NAME','jdbc:oracle:thin:mark1/password@localhost:1521:XE' )"/></empName>
    </root>
</xsl:template>
OUTPUT:
<root>
   <empName>DecipherMiddleware</empName>
</root>

3. oraext:query-database()

oraext:query-database(sqlQuery as string, rowSet as boolean, row as boolean, dataSource as string)

sqlQuery: SQL query that needs to be executed.
rowSet: true(), if we need tag for output rowSet().
row: true(), if we need tag for each row.
dataSource: Specify the Data source JNDI name as configured in WebLogic server or JDBC string in the format jdbc:oracle:thin:username/password@host:port:sid
Note: Only oracle thin driver is supported if used as JDBC string.

This function can be used to execute SQL statements without configuring a JCA adapter in SOA composite. Let us assume we have a category table like below, from where we need to query all columns. 

CATEGORY TABLE

CATEGORYID CATEGORYNAME DESCRIPTION
1 Beverages Soft drinks, coffees, teas, beers, and ales
2 Condiments Sweet and savory sauces, relishes, spreads, and seasonings
3 Confections Desserts, candies, and sweet breads
4 Dairy Products Cheeses
5 Grains/Cereals Breads, crackers, pasta, and cereal

SQL Query: SELECT * FROM category;
XSL:
<xsl:template match="/">
  <xsl:variable name="dbData" select="oraext:query-database('SELECT * FROM category',true(),true(),'jdbc:oracle:thin:mark1/password@localhost:1521:XE' )"/>
    <root>
      <categoryData>
      <xsl:copy-of select="$dbData"/>
      </categoryData>
    </root>
</xsl:template>
OUTPUT:
<root>
   <categoryData>
      <ROWSET>
         <ROW num="1">
            <CATEGORYID>1</CATEGORYID>
            <CATEGORYNAME>Beverages</CATEGORYNAME>
            <DESCRIPTION>Soft drinks, coffees, teas, beers, and ales</DESCRIPTION>
         </ROW>
         <ROW num="2">
            <CATEGORYID>2</CATEGORYID>
            <CATEGORYNAME>Condiments</CATEGORYNAME>
            <DESCRIPTION>Sweet and savory sauces, relishes, spreads, and seasonings</DESCRIPTION>
         </ROW>
         <ROW num="3">
            <CATEGORYID>3</CATEGORYID>
            <CATEGORYNAME>Confections</CATEGORYNAME>
            <DESCRIPTION>Desserts, candies, and sweet breads</DESCRIPTION>
         </ROW>
         <ROW num="4">
            <CATEGORYID>4</CATEGORYID>
            <CATEGORYNAME>Dairy Products</CATEGORYNAME>
            <DESCRIPTION>Cheeses</DESCRIPTION>
         </ROW>
         <ROW num="5">
            <CATEGORYID>5</CATEGORYID>
            <CATEGORYNAME>Grains/Cereals</CATEGORYNAME>
            <DESCRIPTION>Breads, crackers, pasta, and cereal</DESCRIPTION>
         </ROW>
      </ROWSET>
   </categoryData>
</root>

Since we have kept rowSet and row as true(), we are seeing tags coming in output from function result.

References:

https://docs.oracle.com/en/middleware/soa-suite/soa/12.2.1.3/develop/xpath-extension-functions.html#GUID-73F8AE17-41AC-474E-8AF3-6027CAEADB55

Please share your valuable feedback in the comments section below 😊



Comments

Post a Comment

Popular posts from this blog

DateTime formatting using xp20:format-dateTime ()

Handling XML response with or without namespace in Postman

Create Delimited String from XML Nodes and Vice Versa in SOA 12c