org.apache.xalan.lib.sql
Class XConnection

java.lang.Object
  extended by org.apache.xalan.lib.sql.XConnection

public class XConnection
extends java.lang.Object

An XSLT extension that allows a stylesheet to access JDBC data. It is accessed by specifying a namespace URI as follows:

    xmlns:sql="http://xml.apache.org/xalan/sql"
 
From the stylesheet perspective, XConnection provides 3 extension functions: new(), query(), and close(). Use new() to call one of XConnection constructors, which establishes a JDBC driver connection to a data source and returns an XConnection object. Then use the XConnection object query() method to return a result set in the form of a row-set element. When you have finished working with the row-set, call the XConnection object close() method to terminate the connection.


Field Summary
private static boolean DEBUG
          Flag for DEBUG mode
private  java.sql.Connection m_Connection
          The DBMS Connection used to produce this SQL Document.
private  ConnectionPool m_ConnectionPool
          The Current Connection Pool in Use.
private  boolean m_DefaultPoolingEnabled
          If a default Connection Pool is used.
private  java.lang.Exception m_Error
          Allow the SQL Extensions to return null on error.
private  boolean m_FullErrors
          If true then full information should be returned about errors and warnings in getError().
private  boolean m_InlineVariables
           
private  boolean m_IsDefaultPool
           
private  boolean m_IsMultipleResultsEnabled
          This flag will be used to indicate if multiple result sets are supported from the database.
private  boolean m_IsStatementCachingEnabled
          This flag will be used to indicate if database preparedstatements should be cached.
private  boolean m_IsStreamingEnabled
          This flag will be used to indicate to the SQLDocument to use Streaming mode.
private  SQLDocument m_LastSQLDocumentWithError
          When the Stylesheet wants to review the errors from a paticular document, it asks the XConnection.
private  java.util.Vector m_OpenSQLDocuments
          As we do queries, we will produce SQL Documents.
private  java.util.Vector m_ParameterList
          For PreparedStatements, we need a place to to store the parameters in a vector.
private  ConnectionPoolManager m_PoolMgr
          Let's keep a copy of the ConnectionPoolMgr in alive here so we are keeping the static pool alive We will also use this Pool Manager to register our default pools.
private  SQLQueryParser m_QueryParser
          One a per XConnection basis there is a master QueryParser that is responsible for generating Query Parsers.
 
Constructor Summary
XConnection()
           
XConnection(ExpressionContext exprContext, org.w3c.dom.NodeList list)
           
XConnection(ExpressionContext exprContext, java.lang.String connPoolName)
          Constructs a new XConnection and attempts to connect to a datasource as defined in the connect(ExpressionContext exprContext, String connPoolName) method.
XConnection(ExpressionContext exprContext, java.lang.String driver, java.lang.String dbURL)
           
XConnection(ExpressionContext exprContext, java.lang.String driver, java.lang.String dbURL, org.w3c.dom.Element protocolElem)
           
XConnection(ExpressionContext exprContext, java.lang.String driver, java.lang.String dbURL, java.lang.String user, java.lang.String password)
           
 
Method Summary
 void addParameter(java.lang.String value)
          Add an untyped value to the parameter list.
 void addParameterFromElement(org.w3c.dom.Element e)
          Add a single parameter to the parameter list formatted as an Element
 void addParameterFromElement(org.w3c.dom.NodeList nl)
          Add a section of parameters to the Parameter List Do each element from the list
private  void addParameters(org.w3c.dom.Element elem)
           
 void addParameterWithType(java.lang.String value, java.lang.String Type)
          Add a typed parameter to the parameter list.
private  void addTypeToData(java.lang.String typeInfo)
           
private  SQLErrorDocument buildErrorDocument()
           
 void clearParameters()
           
 void close()
          Close the connection to the data source.
 void close(ExpressionContext exprContext, java.lang.Object doc)
          Close the connection to the data source.
 XBooleanStatic connect(ExpressionContext exprContext, org.w3c.dom.Element protocolElem)
           
 XBooleanStatic connect(ExpressionContext exprContext, org.w3c.dom.NodeList list)
           
 XBooleanStatic connect(ExpressionContext exprContext, java.lang.String name)
          Returns an XConnection from either a user created org.apache.xalan.lib.sql.ConnectionPool or a JNDI datasource.
 XBooleanStatic connect(ExpressionContext exprContext, java.lang.String driver, java.lang.String dbURL)
          Create an XConnection object with just a driver and database URL.
 XBooleanStatic connect(ExpressionContext exprContext, java.lang.String driver, java.lang.String dbURL, org.w3c.dom.Element protocolElem)
          Create an XConnection object with a connection protocol
 XBooleanStatic connect(ExpressionContext exprContext, java.lang.String driver, java.lang.String dbURL, java.lang.String user, java.lang.String password)
          Create an XConnection object with user ID and password.
 void disableDefaultConnectionPool()
          Deprecated. Use setFeature("default-pool-enabled", "false");
 void disableStreamingMode()
          Deprecated. Use setFeature("streaming", "false");
 void enableDefaultConnectionPool()
          Deprecated. Use setFeature("default-pool-enabled", "true");
 void enableStreamingMode()
          Deprecated. Use setFeature("streaming", "true");
protected  void finalize()
           
 ConnectionPool getConnectionPool()
          Allow the SQL Document to retrive a connection to be used to build the SQL Statement.
 DTM getError()
          Provide access to the last error that occued.
 java.lang.String getFeature(java.lang.String feature)
          Get feature options for this XConnection.
private  void init(java.lang.String driver, java.lang.String dbURL, java.util.Properties prop)
          Initilize is being called because we did not have an existing Connection Pool, so let's see if we created one already or lets create one ourselves.
private  void initFromElement(org.w3c.dom.Element e)
          Allow the database connection information to be sepcified in the XML tree.
private  SQLDocument locateSQLDocument(ExpressionContext exprContext, java.lang.Object doc)
          When an SQL Document is returned as a DTM object, the XSL variable is actually assigned as a DTMIterator.
 DTM pquery(ExpressionContext exprContext, java.lang.String queryString)
          Execute a parameterized query statement by instantiating an
 DTM pquery(ExpressionContext exprContext, java.lang.String queryString, java.lang.String typeInfo)
          Execute a parameterized query statement by instantiating an
 DTM query(ExpressionContext exprContext, java.lang.String queryString)
          Execute a query statement by instantiating an
 void setError(java.lang.Exception excp, ExpressionContext expr)
          This is an internal version of Set Error that is called withen XConnection where there is no SQLDocument created yet.
 void setError(java.lang.Exception excp, SQLDocument doc, java.sql.SQLWarning warn)
          Set an error and/or warning on this connection.
 void setFeature(java.lang.String feature, java.lang.String setting)
          Set feature options for this XConnection.
 void skipRec(ExpressionContext exprContext, java.lang.Object o, int value)
          The purpose of this routine is to force the DB cursor to skip forward N records.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG

private static final boolean DEBUG
Flag for DEBUG mode

See Also:
Constant Field Values

m_ConnectionPool

private ConnectionPool m_ConnectionPool
The Current Connection Pool in Use. An XConnection can only represent one query at a time, prior to doing some type of query.


m_Connection

private java.sql.Connection m_Connection
The DBMS Connection used to produce this SQL Document. Will be used to clear free up the database resources on close.


m_DefaultPoolingEnabled

private boolean m_DefaultPoolingEnabled
If a default Connection Pool is used. i.e. A connection Pool that is created internally, then do we actually allow pools to be created. Due to the archititure of the Xalan Extensions, there is no notification of when the Extension is being unloaded and as such, there is a good chance that JDBC COnnections are not closed. A finalized is provided to try and catch this situation but since support of finalizers is inconsistant across JVM's this may cause a problem. The robustness of the JDBC Driver is also at issue here. if a controlled shutdown is provided by the driver then default conntectiom pools are OK.


m_OpenSQLDocuments

private java.util.Vector m_OpenSQLDocuments
As we do queries, we will produce SQL Documents. Any ony may produce one or more SQL Documents so that the current connection information may be easilly reused. This collection will hold a collection of all the documents created. As Documents are closed, they will be removed from the collection and told to free all the used resources.


m_PoolMgr

private ConnectionPoolManager m_PoolMgr
Let's keep a copy of the ConnectionPoolMgr in alive here so we are keeping the static pool alive We will also use this Pool Manager to register our default pools.


m_ParameterList

private java.util.Vector m_ParameterList
For PreparedStatements, we need a place to to store the parameters in a vector.


m_Error

private java.lang.Exception m_Error
Allow the SQL Extensions to return null on error. The Error information will be stored in a seperate Error Document that can easily be retrived using the getError() method. %REVIEW% This functionality will probably be buried inside the SQLDocument.


m_LastSQLDocumentWithError

private SQLDocument m_LastSQLDocumentWithError
When the Stylesheet wants to review the errors from a paticular document, it asks the XConnection. We need to track what document in the list of managed documents caused the last error. As SetError is called, it will record the document that had the error.


m_FullErrors

private boolean m_FullErrors
If true then full information should be returned about errors and warnings in getError(). This includes chained errors and warnings. If false (the default) then getError() returns just the first SQLException.


m_QueryParser

private SQLQueryParser m_QueryParser
One a per XConnection basis there is a master QueryParser that is responsible for generating Query Parsers. This will allow us to cache previous instances so the inline parser execution time is minimized.


m_IsDefaultPool

private boolean m_IsDefaultPool

m_IsStreamingEnabled

private boolean m_IsStreamingEnabled
This flag will be used to indicate to the SQLDocument to use Streaming mode. Streeaming Mode will reduce the memory footprint to a fixed amount but will not let you traverse the tree more than once since the Row data will be reused for every Row in the Query.


m_InlineVariables

private boolean m_InlineVariables

m_IsMultipleResultsEnabled

private boolean m_IsMultipleResultsEnabled
This flag will be used to indicate if multiple result sets are supported from the database. If they are, then the metadata element is moved to insude the row-set element and multiple row-set elements may be included under the sql root element.


m_IsStatementCachingEnabled

private boolean m_IsStatementCachingEnabled
This flag will be used to indicate if database preparedstatements should be cached. This also controls if the Java statement object should be cached.

Constructor Detail

XConnection

public XConnection()

XConnection

public XConnection(ExpressionContext exprContext,
                   java.lang.String connPoolName)
Constructs a new XConnection and attempts to connect to a datasource as defined in the connect(ExpressionContext exprContext, String connPoolName) method. org.apache.xalan.lib.sql.ConnectionPool or a JNDI datasource.

Parameters:
exprContext - Context automatically passed from the XSLT sheet.
name - The name of the ConnectionPool or the JNDI DataSource path.

XConnection

public XConnection(ExpressionContext exprContext,
                   java.lang.String driver,
                   java.lang.String dbURL)
Parameters:
exprContext -
driver -
dbURL -

XConnection

public XConnection(ExpressionContext exprContext,
                   org.w3c.dom.NodeList list)
Parameters:
exprContext -
list -

XConnection

public XConnection(ExpressionContext exprContext,
                   java.lang.String driver,
                   java.lang.String dbURL,
                   java.lang.String user,
                   java.lang.String password)
Parameters:
exprContext -
driver -
dbURL -
user -
password -

XConnection

public XConnection(ExpressionContext exprContext,
                   java.lang.String driver,
                   java.lang.String dbURL,
                   org.w3c.dom.Element protocolElem)
Parameters:
exprContext -
driver -
dbURL -
protocolElem -
Method Detail

connect

public XBooleanStatic connect(ExpressionContext exprContext,
                              java.lang.String name)
Returns an XConnection from either a user created org.apache.xalan.lib.sql.ConnectionPool or a JNDI datasource. This method first tries to resolve the passed name against ConnectionPools registered with ConnectionPoolManager. If that fails, it attempts to find the name as a JNDI DataSource path.

Parameters:
exprContext - Context automatically passed from the XSLT sheet.
name - The name of the ConnectionPool or the JNDI DataSource path.

connect

public XBooleanStatic connect(ExpressionContext exprContext,
                              java.lang.String driver,
                              java.lang.String dbURL)
Create an XConnection object with just a driver and database URL.

Parameters:
exprContext -
driver - JDBC driver of the form foo.bar.Driver.
dbURL - database URL of the form jdbc:subprotocol:subname.

connect

public XBooleanStatic connect(ExpressionContext exprContext,
                              org.w3c.dom.Element protocolElem)
Parameters:
exprContext -
protocolElem -

connect

public XBooleanStatic connect(ExpressionContext exprContext,
                              org.w3c.dom.NodeList list)
Parameters:
exprContext -
list -

connect

public XBooleanStatic connect(ExpressionContext exprContext,
                              java.lang.String driver,
                              java.lang.String dbURL,
                              java.lang.String user,
                              java.lang.String password)
Create an XConnection object with user ID and password.

Parameters:
exprContext -
driver - JDBC driver of the form foo.bar.Driver.
dbURL - database URL of the form jdbc:subprotocol:subname.
user - user ID.
password - connection password.

connect

public XBooleanStatic connect(ExpressionContext exprContext,
                              java.lang.String driver,
                              java.lang.String dbURL,
                              org.w3c.dom.Element protocolElem)
Create an XConnection object with a connection protocol

Parameters:
exprContext -
driver - JDBC driver of the form foo.bar.Driver.
dbURL - database URL of the form jdbc:subprotocol:subname.
protocolElem - list of string tag/value connection arguments, normally including at least "user" and "password".

initFromElement

private void initFromElement(org.w3c.dom.Element e)
                      throws java.sql.SQLException
Allow the database connection information to be sepcified in the XML tree. The connection information could also be externally originated and passed in as an XSL Parameter. The required XML Format is as follows. A document fragment is needed to specify the connection information the top tag name is not specific for this code, we are only interested in the tags inside. Specify the driver name for this connection pool drivername Specify the URL for the driver in this connection pool url Specify the password for this connection pool password Specify the username for this connection pool username You can add extra protocol items including the User Name & Password with the protocol tag. For each extra protocol item, add a new element where the name of the item is specified as the name attribute and and its value as the elements value. value

Parameters:
e -
Throws:
java.sql.SQLException

init

private void init(java.lang.String driver,
                  java.lang.String dbURL,
                  java.util.Properties prop)
           throws java.sql.SQLException
Initilize is being called because we did not have an existing Connection Pool, so let's see if we created one already or lets create one ourselves.

Parameters:
driver -
dbURL -
prop -
Throws:
java.sql.SQLException

getConnectionPool

public ConnectionPool getConnectionPool()
Allow the SQL Document to retrive a connection to be used to build the SQL Statement.


query

public DTM query(ExpressionContext exprContext,
                 java.lang.String queryString)
Execute a query statement by instantiating an

Parameters:
exprContext -
queryString - the SQL query.
Returns:
XStatement implements NodeIterator.
Throws:
java.sql.SQLException

pquery

public DTM pquery(ExpressionContext exprContext,
                  java.lang.String queryString)
Execute a parameterized query statement by instantiating an

Parameters:
exprContext -
queryString - the SQL query.
Returns:
XStatement implements NodeIterator.
Throws:
java.sql.SQLException

pquery

public DTM pquery(ExpressionContext exprContext,
                  java.lang.String queryString,
                  java.lang.String typeInfo)
Execute a parameterized query statement by instantiating an

Parameters:
exprContext -
queryString - the SQL query.
typeInfo -
Returns:
XStatement implements NodeIterator.
Throws:
java.sql.SQLException

skipRec

public void skipRec(ExpressionContext exprContext,
                    java.lang.Object o,
                    int value)
The purpose of this routine is to force the DB cursor to skip forward N records. You should call this function after [p]query to help with pagination. i.e. Perfrom your select, then skip forward past the records you read previously.

Parameters:
exprContext -
o -
value -

addTypeToData

private void addTypeToData(java.lang.String typeInfo)

addParameter

public void addParameter(java.lang.String value)
Add an untyped value to the parameter list.

Parameters:
value -

addParameterWithType

public void addParameterWithType(java.lang.String value,
                                 java.lang.String Type)
Add a typed parameter to the parameter list.

Parameters:
value -
Type -

addParameterFromElement

public void addParameterFromElement(org.w3c.dom.Element e)
Add a single parameter to the parameter list formatted as an Element

Parameters:
e -

addParameterFromElement

public void addParameterFromElement(org.w3c.dom.NodeList nl)
Add a section of parameters to the Parameter List Do each element from the list

Parameters:
nl -

addParameters

private void addParameters(org.w3c.dom.Element elem)
Parameters:
elem -

clearParameters

public void clearParameters()

enableDefaultConnectionPool

public void enableDefaultConnectionPool()
Deprecated. Use setFeature("default-pool-enabled", "true");

There is a problem with some JDBC drivers when a Connection is open and the JVM shutsdown. If there is a problem, there is no way to control the currently open connections in the pool. So for the default connection pool, the actuall pooling mechinsm is disabled by default. The Stylesheet designer can re-enabled pooling to take advantage of connection pools. The connection pool can even be disabled which will close all outstanding connections.


disableDefaultConnectionPool

public void disableDefaultConnectionPool()
Deprecated. Use setFeature("default-pool-enabled", "false");

See enableDefaultConnectionPool


enableStreamingMode

public void enableStreamingMode()
Deprecated. Use setFeature("streaming", "true");

Control how the SQL Document uses memory. In Streaming Mode, memory consumption is greatly reduces so you can have queries of unlimited size but it will not let you traverse the data more than once.


disableStreamingMode

public void disableStreamingMode()
Deprecated. Use setFeature("streaming", "false");

Control how the SQL Document uses memory. In Streaming Mode, memory consumption is greatly reduces so you can have queries of unlimited size but it will not let you traverse the data more than once.


getError

public DTM getError()
Provide access to the last error that occued. This error may be over written when the next operation occurs.


close

public void close()
           throws java.sql.SQLException
Close the connection to the data source.

Throws:
java.sql.SQLException

close

public void close(ExpressionContext exprContext,
                  java.lang.Object doc)
           throws java.sql.SQLException
Close the connection to the data source. Only close the connections for a single document.

Throws:
java.sql.SQLException

locateSQLDocument

private SQLDocument locateSQLDocument(ExpressionContext exprContext,
                                      java.lang.Object doc)
When an SQL Document is returned as a DTM object, the XSL variable is actually assigned as a DTMIterator. This is a helper function that will allow you to get a reference to the original SQLDocument from the iterator. Original code submitted by Moraine Didier mailto://didier.moraine@winterthur.be

Parameters:
doc -
Returns:

buildErrorDocument

private SQLErrorDocument buildErrorDocument()
Parameters:
exprContext -
excp -

setError

public void setError(java.lang.Exception excp,
                     ExpressionContext expr)
This is an internal version of Set Error that is called withen XConnection where there is no SQLDocument created yet. As in the Connect statement or creation of the ConnectionPool.


setError

public void setError(java.lang.Exception excp,
                     SQLDocument doc,
                     java.sql.SQLWarning warn)
Set an error and/or warning on this connection.


setFeature

public void setFeature(java.lang.String feature,
                       java.lang.String setting)
Set feature options for this XConnection.

Parameters:
feature - The name of the feature being set, currently supports (streaming, inline-variables, multiple-results, cache-statements, default-pool-enabled).
setting - The new setting for the specified feature, currently "true" is true and anything else is false.

getFeature

public java.lang.String getFeature(java.lang.String feature)
Get feature options for this XConnection.

Parameters:
feature - The name of the feature to get the setting for.
Returns:
The setting of the specified feature. Will be "true" or "false" (null if the feature is not known)

finalize

protected void finalize()
Overrides:
finalize in class java.lang.Object