de.mguennewig.pobjects.jdbc
Class JdbcContainer

java.lang.Object
  extended by de.mguennewig.pobjects.AbstractContainer
      extended by de.mguennewig.pobjects.jdbc.JdbcContainer
All Implemented Interfaces:
Container
Direct Known Subclasses:
MySQLContainer, OracleContainer, PostgreSQLContainer, SQLiteContainer, SqlServerContainer

public abstract class JdbcContainer
extends AbstractContainer

Common base class for standard JDBC based database connections.

This container caches the PObject instances for faster access.

Author:
Michael Günnewig

Field Summary
 
Fields inherited from interface de.mguennewig.pobjects.Container
SUPPORTS_BOOLEAN, SUPPORTS_INHERITS, SUPPORTS_JOIN, SUPPORTS_LIMIT_OFFSET, SUPPORTS_ORACLE_JOIN, SUPPORTS_SQL99_JOIN, USE_ARRAY_FOR_LOB, USE_STREAM_TO_INSERT_LOB
 
Constructor Summary
JdbcContainer(java.sql.Connection conn, PObjDictionary dict, java.lang.String schema)
          Creates a container with a specified connection to a DBMS.
JdbcContainer(PObjDictionary dict, java.lang.String schema)
          Creates a container without a connection to any DBMS.
JdbcContainer(java.lang.String hostname, java.lang.Integer port, java.lang.String database, java.lang.String username, java.lang.String password, PObjDictionary dict, java.lang.String schema)
          Creates a container that opens a connection to the specified database.
 
Method Summary
 void addArguments(java.sql.PreparedStatement statm, SqlStatement sql, java.lang.Object[] arguments)
          Adds the arguments for the given SQL statement to the prepared statement.
 void addParameter(java.sql.PreparedStatement statm, int pos, Type t, java.lang.Object value)
           
 void beginTransaction()
          Starts a new transaction.
 void close()
          
 void commitTransaction()
          Commits all statements since the last Container.beginTransaction().
 int deleteAll(ClassDecl te)
          Deletes all entries within the specified table.
 int deleteAll(ClassDecl cl, Column fk, java.lang.Object value)
          Deletes all entries within the specified table whose foreign key fk equals the given value.
 void deleteObject(PObject obj)
          Deletes the given persistent object from the database.
 boolean deleteRecord(Record obj)
          
 java.sql.Connection getConnection()
          Returns the connection to the DBMS if any exists.
 java.util.Properties getConnectProperties()
           
abstract  java.lang.String getConnectURL(java.lang.String hostname, java.lang.Integer port, java.lang.String database)
           
 PObject getObject(ClassDecl cdecl, java.lang.String id)
          Returns a PObject instance for the given table expression without reading any data from the DB.
 boolean insertRecord(Record obj)
          
 java.lang.String mapClassDeclToPClassId(ClassDecl cd)
          Maps the given ClassDecl to the pclass id used in the DBMS.
 ClassDecl mapPClassIdToClassDecl(java.lang.String id)
          Maps the given pclass id as used by the DBMS to the correct ClassDecl.
 EvalContext newEvalContext(int numColumns)
          
 Query newQuery()
          Creates a new empty query.
 java.sql.PreparedStatement prepareStatement(java.lang.String sql)
           
 java.sql.PreparedStatement prepareStatement(java.lang.String sql, int scrollType, int concurrType)
           
 void reset()
          Resets the container.
 void retrieveData(PObject obj)
          Retrieves the data for the given persistent object from the database.
 void rollbackTransaction()
          Rolls back all statements since the last Container.beginTransaction().
 void setConnection(java.sql.Connection connection)
          Sets the connection to the DBMS.
 void updateObject(PObject obj)
          Updates the given persistent object in the database.
 
Methods inherited from class de.mguennewig.pobjects.AbstractContainer
createObject, deleteAll, getClassDecl, getClassDecls, getData, getDictionary, getObject, getQualifiedName, getSchema, getTableExpr, isStrictMode, makePersistent, newQuery, newQuery, newQuery, newQuery, newQuery, newQuery, newQuery, newQuery, notifyChange, setStrictMode, storeAll, supportsBoolean, supportsInherits, supportsLimitOffset, supportsOracleJoin, supportsSQL99Join, useArrayForLob, useStreamToInsertLob
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface de.mguennewig.pobjects.Container
getCapabilities, getSequenceCurrentValue, getSequenceNextValue, insertObject
 

Constructor Detail

JdbcContainer

public JdbcContainer(PObjDictionary dict,
                     java.lang.String schema)
Creates a container without a connection to any DBMS.


JdbcContainer

public JdbcContainer(java.lang.String hostname,
                     java.lang.Integer port,
                     java.lang.String database,
                     java.lang.String username,
                     java.lang.String password,
                     PObjDictionary dict,
                     java.lang.String schema)
              throws java.sql.SQLException
Creates a container that opens a connection to the specified database.

NOTE: Auto-commitment will be turned off which is normally turned on for JDBC.

Parameters:
hostname - The host name on which the DBMS runs. If null it defaults to localhost.
port - The port on which the DBMS listens. If null the default port is used.
database - The name of the database.
username - The user name that should be used for authentication.
password - The password that should be used for authentication.
dict -
schema -
Throws:
java.sql.SQLException - if no connection to the database can be opened, or an error occurred during initialization
See Also:
initializeConnection()

JdbcContainer

public JdbcContainer(java.sql.Connection conn,
                     PObjDictionary dict,
                     java.lang.String schema)
              throws java.sql.SQLException
Creates a container with a specified connection to a DBMS.

NOTE: Auto-commitment will be turned off which is normally turned on for JDBC.

Throws:
java.sql.SQLException - if any database error occurs during initialization.
See Also:
initializeConnection()
Method Detail

close

public void close()
           throws PObjSQLException

Throws:
PObjSQLException

getObject

public PObject getObject(ClassDecl cdecl,
                         java.lang.String id)
Returns a PObject instance for the given table expression without reading any data from the DB.

This version caches the database objects to save database queries.

Specified by:
getObject in interface Container
Overrides:
getObject in class AbstractContainer
See Also:
Container.getData(TableExpr), Container.retrieveData(PObject)

beginTransaction

public void beginTransaction()
                      throws PObjSQLException
Starts a new transaction.

Specified by:
beginTransaction in interface Container
Overrides:
beginTransaction in class AbstractContainer
Throws:
PObjSQLException - if the DBMS is unable to start a new transaction.

commitTransaction

public void commitTransaction()
                       throws PObjSQLException
Commits all statements since the last Container.beginTransaction().

If Container.isStrictMode() yields false then just a warning for each dirty object is written, otherwise an exception is raised.

Specified by:
commitTransaction in interface Container
Overrides:
commitTransaction in class AbstractContainer
Throws:
PObjSQLException - if the DBMS in unable to finish the current transaction by committing it or if an modified but unstored object is detected while in strict mode.

rollbackTransaction

public void rollbackTransaction()
                         throws PObjSQLException
Rolls back all statements since the last Container.beginTransaction().

The in-memory data will be reverted also as far as possible.

Specified by:
rollbackTransaction in interface Container
Overrides:
rollbackTransaction in class AbstractContainer
Throws:
PObjSQLException - if the DBMS is unable to stop the current transaction by rolling back all made changes within it.
See Also:
PObject.revert()

deleteAll

public int deleteAll(ClassDecl te)
              throws PObjSQLException,
                     PObjConstraintException
Deletes all entries within the specified table.

NOTE: The objects which represent a row of this table or refer to rows within it aren't updated. So the best way to ensure consistency is to finish the current active transaction before calling this method and afterwards doing a Container.reset().

Throws:
PObjSQLException - if any other database error occurs.
PObjConstraintException - if a database constraint will be violated.

deleteAll

public int deleteAll(ClassDecl cl,
                     Column fk,
                     java.lang.Object value)
              throws PObjSQLException,
                     PObjConstraintException
Deletes all entries within the specified table whose foreign key fk equals the given value.

NOTE: The objects which represent a row of this table or refer to rows within it aren't updated. So the best way to ensure consistency is to finish the current active transaction before calling this method and afterwards doing a Container.reset().

Throws:
PObjSQLException - if any other database error occurs.
PObjConstraintException - if a database constraint will be violated.

deleteObject

public void deleteObject(PObject obj)
                  throws PObjConstraintException,
                         PObjSQLException
Deletes the given persistent object from the database.

Throws:
PObjConstraintException - if a database constraint will be violated.
PObjSQLException - if any other database error occurs.
See Also:
PObject.isPersistent()

deleteRecord

public boolean deleteRecord(Record obj)
                     throws PObjConstraintException,
                            PObjSQLException

Throws:
PObjConstraintException
PObjSQLException

reset

public void reset()
Resets the container.

Use this method to forget all changes made to any object.

NOTE: Any running transaction will be rolled back.

Specified by:
reset in interface Container
Overrides:
reset in class AbstractContainer
See Also:
Container.rollbackTransaction()

newEvalContext

public EvalContext newEvalContext(int numColumns)


newQuery

public Query newQuery()
Creates a new empty query.


retrieveData

public void retrieveData(PObject obj)
Retrieves the data for the given persistent object from the database.


insertRecord

public boolean insertRecord(Record obj)
                     throws PObjConstraintException,
                            PObjSQLException

Throws:
PObjConstraintException
PObjSQLException

updateObject

public void updateObject(PObject obj)
                  throws PObjConstraintException,
                         PObjSQLException
Updates the given persistent object in the database.

Throws:
PObjConstraintException - if a database constraint will be violated.
PObjSQLException - if any other database error occurs.
See Also:
PObject.isPersistent()

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql)
                                            throws java.sql.SQLException
Throws:
java.sql.SQLException

prepareStatement

public java.sql.PreparedStatement prepareStatement(java.lang.String sql,
                                                   int scrollType,
                                                   int concurrType)
                                            throws java.sql.SQLException
Throws:
java.sql.SQLException

addArguments

public void addArguments(java.sql.PreparedStatement statm,
                         SqlStatement sql,
                         java.lang.Object[] arguments)
                  throws PObjSQLException
Adds the arguments for the given SQL statement to the prepared statement.

Throws:
PObjSQLException

addParameter

public void addParameter(java.sql.PreparedStatement statm,
                         int pos,
                         Type t,
                         java.lang.Object value)
                  throws PObjSQLException
Throws:
PObjSQLException

getConnectURL

public abstract java.lang.String getConnectURL(java.lang.String hostname,
                                               java.lang.Integer port,
                                               java.lang.String database)

getConnectProperties

public java.util.Properties getConnectProperties()

getConnection

public final java.sql.Connection getConnection()
Returns the connection to the DBMS if any exists.


setConnection

public void setConnection(java.sql.Connection connection)
                   throws java.sql.SQLException
Sets the connection to the DBMS.

NOTE: Any transaction will be rolled back and the cache is cleared.

Throws:
java.sql.SQLException
See Also:
reset()

mapPClassIdToClassDecl

public final ClassDecl mapPClassIdToClassDecl(java.lang.String id)
Maps the given pclass id as used by the DBMS to the correct ClassDecl.

Returns:
null if the id is unknown.
Throws:
java.lang.UnsupportedOperationException - if DB supports INHERITS.
See Also:
AbstractContainer.getClassDecl(String), registerClass(ClassDecl)

mapClassDeclToPClassId

public final java.lang.String mapClassDeclToPClassId(ClassDecl cd)
Maps the given ClassDecl to the pclass id used in the DBMS.

Returns:
null if the id is unknown.
Throws:
java.lang.UnsupportedOperationException - if DB supports INHERITS.