de.ispsoft.jaxme
Class JMJdbcManager

java.lang.Object
  extended byde.ispsoft.jaxme.JMManagerImpl
      extended byde.ispsoft.jaxme.JMJdbcManager
All Implemented Interfaces:
JMManager
Direct Known Subclasses:
JMJdbcDataSourceManager

public abstract class JMJdbcManager
extends JMManagerImpl

A Manager implementation for XML documents read from or written to an SQL database.

Author:
Jochen Wiedmann

Nested Class Summary
 
Nested classes inherited from class de.ispsoft.jaxme.JMManagerImpl
JMManagerImpl.MyObserver
 
Field Summary
static java.lang.String CONFIGURATION_NODE_NAME
          Name of our private configuration node (JMJdbcManager in namespace JMManagerFactoryImpl.NAMESPACE_URI)
protected static int CREATE_KEY_DONT
          Constant for not creating a primary key as part of the INSERT operation.
protected static int CREATE_KEY_POST_INSERT
          Constant for creating a primary key after the INSERT operation.
protected static int CREATE_KEY_PRE_INSERT
          Constant for creating a primary key before the INSERT operation.
protected  java.lang.String datasource
           
static java.lang.String DATASOURCE_NODE_NAME
          Name of the DataSource node, used by subclasses only
protected  java.lang.String driver
           
static java.lang.String DRIVER_NODE_NAME
          Name of the Driver node
protected  java.lang.String password
           
static java.lang.String PASSWORD_NODE_NAME
          Name of the Password node
protected  java.lang.String url
           
static java.lang.String URL_NODE_NAME
          Name of the Url node
protected  boolean useDataSource
           
protected  java.lang.String user
           
static java.lang.String USER_NODE_NAME
          Name of the User node
 
Fields inherited from class de.ispsoft.jaxme.JMManagerImpl
ELEMENT_CLASS_NAME, HANDLER_CLASS_NAME, jmAnyElementClass, jmContentHandlerClass
 
Fields inherited from interface de.ispsoft.jaxme.JMManager
BIGINT, BIT, BLOB, BOOLEAN, CLOB, DECIMAL, DOUBLE, FLOAT, INTEGER, SMALLINT, TIMESTAMP, TINYINT, VARBINARY, VARCHAR
 
Constructor Summary
JMJdbcManager()
          Creates a new instance of JMJdbcManager
 
Method Summary
 void delete(JMAnyElement pElement)
          Deletes the given element in the database.
protected  void executeQueries(java.sql.Connection pConn, java.sql.PreparedStatement[] pStmt)
           
 void executeStatement(java.lang.String pQuery, java.lang.Object[] pPlaceHolderArgs)
          Executes the given statement.
 java.sql.Connection getConnection()
          Returns an open JDBC connection
protected  java.sql.PreparedStatement[] getDeleteQueries(java.sql.Connection pConn, JMJdbcElement pElement)
          Returns an array of prepared statements, that are being performed to delete the element in the database.
protected  java.lang.String getFieldList()
          Returns the field list to build an SQL SELECT statement.
protected  int getInsertCreatesPrimaryKey()
          Returns whether a primary key should be created before the insert (CREATE_KEY_PRE_INSERT), after the insert (CREATE_KEY_POST_INSERT) or not at all (CREATE_KEY_DONT).
protected  java.sql.PreparedStatement[] getInsertQueries(java.sql.Connection pConn, JMJdbcElement pElement)
          Returns an array of prepared statements, that are being performed to insert the element into the database.
protected  java.sql.PreparedStatement getPreparedStatement(java.sql.Connection pConn, java.lang.String pQuery, java.lang.Object[] pPlaceHolderArgs)
          Prepares the placeholders of the given statement.
protected  java.lang.String getQuery(java.lang.String pQuery)
          Parses and converts the query.
protected  java.lang.String getTableList()
          Returns the table list to build an SQL SELECT statement.
protected  java.sql.PreparedStatement[] getUpdateQueries(java.sql.Connection pConn, JMJdbcElement pElement)
          Returns an array of prepared statements, that are being performed to update the element in the database.
protected  java.lang.String getWhere()
          Returns an additional filter for the WHERE clause.
 void init(JMManagerFactory pFactory, java.lang.String pNamespaceURI, java.lang.String pLocalName)
          Initializes the manager.
protected abstract  void init(java.sql.ResultSet rs, JMJdbcElement pElement)
          Initializes the element from the resultset.
 void insert(JMAnyElement pElement)
          Calls newPrimaryKey() and inserts the given element into the database.
protected  void newPrimaryKey(java.sql.Connection pConn, JMAnyElement pElement)
          Creates a new primary key for the element.
 void parseConfiguration(org.w3c.dom.Element pElement)
          Parses a configuration element.
 void select(Observer pObserver, java.lang.String pQuery, java.lang.Object[] pPlaceHolderArgs, int pStart, int pMax)
          Reads documents matching the given query.
 void update(JMAnyElement pElement)
          Inserts the given element into the database.
 
Methods inherited from class de.ispsoft.jaxme.JMManagerImpl
create, getJMAnyElementClass, getJMContentHandler, getLocalName, getNamespaceSupport, getNamespaceURI, select, select, select, select, select, select, select, toSAX, toWriter, toXML
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CONFIGURATION_NODE_NAME

public static final java.lang.String CONFIGURATION_NODE_NAME

Name of our private configuration node (JMJdbcManager in namespace JMManagerFactoryImpl.NAMESPACE_URI)

See Also:
Constant Field Values

DRIVER_NODE_NAME

public static final java.lang.String DRIVER_NODE_NAME

Name of the Driver node

See Also:
Constant Field Values

URL_NODE_NAME

public static final java.lang.String URL_NODE_NAME

Name of the Url node

See Also:
Constant Field Values

USER_NODE_NAME

public static final java.lang.String USER_NODE_NAME

Name of the User node

See Also:
Constant Field Values

PASSWORD_NODE_NAME

public static final java.lang.String PASSWORD_NODE_NAME

Name of the Password node

See Also:
Constant Field Values

DATASOURCE_NODE_NAME

public static final java.lang.String DATASOURCE_NODE_NAME

Name of the DataSource node, used by subclasses only

See Also:
Constant Field Values

driver

protected java.lang.String driver

url

protected java.lang.String url

user

protected java.lang.String user

password

protected java.lang.String password

datasource

protected java.lang.String datasource

useDataSource

protected boolean useDataSource

CREATE_KEY_DONT

protected static final int CREATE_KEY_DONT

Constant for not creating a primary key as part of the INSERT operation.

See Also:
Constant Field Values

CREATE_KEY_PRE_INSERT

protected static final int CREATE_KEY_PRE_INSERT

Constant for creating a primary key before the INSERT operation.

See Also:
Constant Field Values

CREATE_KEY_POST_INSERT

protected static final int CREATE_KEY_POST_INSERT

Constant for creating a primary key after the INSERT operation.

See Also:
Constant Field Values
Constructor Detail

JMJdbcManager

public JMJdbcManager()
Creates a new instance of JMJdbcManager

Method Detail

parseConfiguration

public void parseConfiguration(org.w3c.dom.Element pElement)

Parses a configuration element. This method is called twice: Once for the default configuration and once for the specific configuration.

Overrides:
parseConfiguration in class JMManagerImpl

init

public void init(JMManagerFactory pFactory,
                 java.lang.String pNamespaceURI,
                 java.lang.String pLocalName)
          throws org.xml.sax.SAXException
Initializes the manager.

Specified by:
init in interface JMManager
Overrides:
init in class JMManagerImpl
Parameters:
pFactory - The factory that created this Manager.
pNamespaceURI - The document types namespace URI
pLocalName - The document types local name
Throws:
org.xml.sax.SAXException

getConnection

public java.sql.Connection getConnection()
                                  throws java.sql.SQLException,
                                         org.xml.sax.SAXException

Returns an open JDBC connection

Throws:
java.sql.SQLException
org.xml.sax.SAXException

executeQueries

protected void executeQueries(java.sql.Connection pConn,
                              java.sql.PreparedStatement[] pStmt)
                       throws java.sql.SQLException
Throws:
java.sql.SQLException

getInsertCreatesPrimaryKey

protected int getInsertCreatesPrimaryKey()

Returns whether a primary key should be created before the insert (CREATE_KEY_PRE_INSERT), after the insert (CREATE_KEY_POST_INSERT) or not at all (CREATE_KEY_DONT).


newPrimaryKey

protected void newPrimaryKey(java.sql.Connection pConn,
                             JMAnyElement pElement)
                      throws java.sql.SQLException

Creates a new primary key for the element. Does nothing, to be overwritten by subclasses.

Throws:
java.sql.SQLException

insert

public void insert(JMAnyElement pElement)
            throws org.xml.sax.SAXException

Calls newPrimaryKey() and inserts the given element into the database.

Specified by:
insert in interface JMManager
Overrides:
insert in class JMManagerImpl
Throws:
org.xml.sax.SAXException

update

public void update(JMAnyElement pElement)
            throws org.xml.sax.SAXException

Inserts the given element into the database.

Specified by:
update in interface JMManager
Overrides:
update in class JMManagerImpl
Throws:
org.xml.sax.SAXException

delete

public void delete(JMAnyElement pElement)
            throws org.xml.sax.SAXException

Deletes the given element in the database.

Specified by:
delete in interface JMManager
Overrides:
delete in class JMManagerImpl
Throws:
org.xml.sax.SAXException

select

public void select(Observer pObserver,
                   java.lang.String pQuery,
                   java.lang.Object[] pPlaceHolderArgs,
                   int pStart,
                   int pMax)
            throws org.xml.sax.SAXException

Reads documents matching the given query. For any document matching, the Observer's notify method is executed with the matching document as an argument.

The query may contain placeholders. If it does, you have to supply an object array with two elements per placeholder: An Integer with a java.sql.Types type and the actual placeholder value. Example:

   manager.select("Name = ? and Id = ?",
                  new Object[]{JMManager.VARCHAR,
                               "Someone",
                               JMManager.INTEGER,
                               4}, 0, 0);
 

Specified by:
select in interface JMManager
Overrides:
select in class JMManagerImpl
Parameters:
pObserver - This Observer is notified for any matching document. The document is added as an argument.
pQuery - The query to perform. May contain placeholders.
pPlaceHolderArgs - An array of objects or null, if the query doesn't contain any placeholders.
pStart - Ignore the given number of result documents at the beginning. A value of zero will return all documents.
pMax - Return at most the given number of documents. A value of zero will return all documents.
Throws:
org.xml.sax.SAXException

getQuery

protected java.lang.String getQuery(java.lang.String pQuery)
                             throws org.xml.sax.SAXException

Parses and converts the query.

Throws:
org.xml.sax.SAXException

init

protected abstract void init(java.sql.ResultSet rs,
                             JMJdbcElement pElement)
                      throws java.sql.SQLException,
                             org.xml.sax.SAXException

Initializes the element from the resultset. In other words, reads one row from the result set.

Throws:
java.sql.SQLException
org.xml.sax.SAXException

getFieldList

protected java.lang.String getFieldList()
                                 throws org.xml.sax.SAXException

Returns the field list to build an SQL SELECT statement.

Throws:
org.xml.sax.SAXException

getTableList

protected java.lang.String getTableList()
                                 throws org.xml.sax.SAXException

Returns the table list to build an SQL SELECT statement.

Throws:
org.xml.sax.SAXException

getInsertQueries

protected java.sql.PreparedStatement[] getInsertQueries(java.sql.Connection pConn,
                                                        JMJdbcElement pElement)
                                                 throws org.xml.sax.SAXException,
                                                        java.sql.SQLException

Returns an array of prepared statements, that are being performed to insert the element into the database.

Throws:
org.xml.sax.SAXException
java.sql.SQLException

getUpdateQueries

protected java.sql.PreparedStatement[] getUpdateQueries(java.sql.Connection pConn,
                                                        JMJdbcElement pElement)
                                                 throws org.xml.sax.SAXException,
                                                        java.sql.SQLException

Returns an array of prepared statements, that are being performed to update the element in the database.

Throws:
org.xml.sax.SAXException
java.sql.SQLException

getDeleteQueries

protected java.sql.PreparedStatement[] getDeleteQueries(java.sql.Connection pConn,
                                                        JMJdbcElement pElement)
                                                 throws org.xml.sax.SAXException,
                                                        java.sql.SQLException

Returns an array of prepared statements, that are being performed to delete the element in the database.

Throws:
org.xml.sax.SAXException
java.sql.SQLException

getWhere

protected java.lang.String getWhere()
                             throws org.xml.sax.SAXException

Returns an additional filter for the WHERE clause. You should probably not use this, as it requires heavy parsing of the query to support sorting and the like. It is only there for databases not supporting the "a JOIN b ON a.foo=b.bar WHERE ..." syntax, in which case we need to write "a, b WHERE (a.foo=b.bar) AND (...)".

Throws:
org.xml.sax.SAXException

executeStatement

public void executeStatement(java.lang.String pQuery,
                             java.lang.Object[] pPlaceHolderArgs)
                      throws org.xml.sax.SAXException

Executes the given statement.

Throws:
org.xml.sax.SAXException

getPreparedStatement

protected java.sql.PreparedStatement getPreparedStatement(java.sql.Connection pConn,
                                                          java.lang.String pQuery,
                                                          java.lang.Object[] pPlaceHolderArgs)
                                                   throws java.sql.SQLException

Prepares the placeholders of the given statement.

Throws:
java.sql.SQLException