org.formaria.data.sql
Class DataConnection

java.lang.Object
  extended by org.formaria.data.sql.DataConnection

public class DataConnection
extends java.lang.Object

An extended database connection that allows integration of a local database and replication to/from a remote database.

Copyright (c) Formaria Ltd. 2008

$Revision: 2.13 $ License: see license.txt


Constructor Summary
DataConnection(Project currentProject, java.lang.String name)
          Construct a new connection
DataConnection(Project currentProject, java.lang.String name, boolean replicate)
          Construct a new connection
 
Method Summary
 ConnectionObject borrowConnection()
          Borrow the connection object
 void closePreparedStatement(java.sql.PreparedStatement ps)
          Close a prepared statement.
 void closeQuery()
          Close the resources used by the query
 void closeStatement(java.sql.Statement s)
          Close a statement.
 void commit()
          Explicitly force a commit
 java.sql.PreparedStatement createPreparedStatement(java.lang.String sql, java.lang.String connName)
          Create a prepared statement.
 java.sql.PreparedStatement createPreparedStatement(java.lang.String sql, java.lang.String connName, boolean forwardOnly)
          Create a prepared statement.
 java.sql.ResultSet doQuery(java.lang.String sql)
          Invoke a SQL query and return the result set
 int doUpdate(java.lang.String sql)
          Invoke a SQL update query
 java.sql.ResultSet executeQuery(java.lang.String sql)
          Execute a SQL query
 java.sql.ResultSet executeQuery(java.lang.String sql, java.lang.String connName, boolean writable)
          Execute a SQL query.
 int executeUpdate(java.lang.String sql)
          Execute a SQL update statement.
 ConnectionObject getConnectionObject()
          Get a connection
 boolean getDoesEscapeProcessing()
          Gets the escape processing flag for the underlying JDBC implementation
 java.sql.ResultSet getMetaData(java.lang.String tableName)
          Get a list of the fields in a table.
 long getNextId(java.lang.String tableName)
          Get the next ID for the named table, assuming one of the table's fields is a managed field.
 long getNextPseudoId(java.lang.String tableName)
          Get the next PSUEDOID for the names table
 ReplicationEngine getReplicationEngine()
          Gets the replication engine.
static java.lang.String getTableName(java.lang.String sql)
          Retrives the table name from the given sql
 void returnConnection()
          Return the connection object to the pool
 void setAutoCommit(boolean state)
          Set the auto commit state
 void setDoesEscapeProcessing(boolean escapeProcessing)
          Set the escape processing flag for the underlying JDBC implementation
static void setReplicationEnabled(boolean state)
          Set the replication mode
 void setReplicationEngine(ReplicationEngine engine)
          Sets the replication engine object
static void shutdown(java.lang.String connParamName)
          Execute a SHUTDOWN statement
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DataConnection

public DataConnection(Project currentProject,
                      java.lang.String name)
Construct a new connection

Parameters:
currentProject - the current or owner project
name - the connection name

DataConnection

public DataConnection(Project currentProject,
                      java.lang.String name,
                      boolean replicate)
Construct a new connection

Parameters:
currentProject - the current or owner project
name - the connection name
replicate - true to attempt use of the replication service
Method Detail

getReplicationEngine

public ReplicationEngine getReplicationEngine()
Gets the replication engine.

Returns:
ReplicationEngine object.

setReplicationEngine

public void setReplicationEngine(ReplicationEngine engine)
Sets the replication engine object

Parameters:
engine - the ReplicationEngine object to be set

getConnectionObject

public ConnectionObject getConnectionObject()
Get a connection


executeUpdate

public int executeUpdate(java.lang.String sql)
                  throws java.lang.Exception
Execute a SQL update statement.

Parameters:
sql - the SQL statement to be executed
Returns:
the result of the update query
Throws:
java.lang.Exception

executeQuery

public java.sql.ResultSet executeQuery(java.lang.String sql)
                                throws java.sql.SQLException
Execute a SQL query

Parameters:
sql - the SQL statement to be executed
Returns:
the result of the query statement
Throws:
java.sql.SQLException

getTableName

public static java.lang.String getTableName(java.lang.String sql)
Retrives the table name from the given sql


executeQuery

public java.sql.ResultSet executeQuery(java.lang.String sql,
                                       java.lang.String connName,
                                       boolean writable)
                                throws java.sql.SQLException
Execute a SQL query. Applications muct call closeQuery for every time they use this method.

Parameters:
sql - the SQL statement
connName - the connection name
writable - true to attempt to create an updatable resultset
Returns:
the results of the query
Throws:
java.sql.SQLException

closeQuery

public void closeQuery()
Close the resources used by the query


createPreparedStatement

public java.sql.PreparedStatement createPreparedStatement(java.lang.String sql,
                                                          java.lang.String connName,
                                                          boolean forwardOnly)
                                                   throws java.sql.SQLException
Create a prepared statement. Note that you MUST release the PreparedStatement by calling returnPreparedStatement when finished with the statement

Parameters:
sql - the SQL statement
connName - the connection name
forwardOnly - true for a result set that is ResultSet.TYPE_FORRWAD_ONLY
Returns:
the PreparedStatement
Throws:
java.sql.SQLException

createPreparedStatement

public java.sql.PreparedStatement createPreparedStatement(java.lang.String sql,
                                                          java.lang.String connName)
                                                   throws java.sql.SQLException
Create a prepared statement. Note that you MUST release the PreparedStatement by calling returnPreparedStatement when finished with the statement

Parameters:
sql - the SQL statement
connName - the connection name
Returns:
the PreparedStatement
Throws:
java.sql.SQLException

closePreparedStatement

public void closePreparedStatement(java.sql.PreparedStatement ps)
Close a prepared statement. Note that you MUST release the PreparedStatement by calling returnPreparedStatement when finished with the

Parameters:
ps - the PreparedStatement to be returned

closeStatement

public void closeStatement(java.sql.Statement s)
Close a statement. Note that you MUST release the Statement by calling returnStatement when finished with the

Parameters:
s - the Statement to be returned

getMetaData

public java.sql.ResultSet getMetaData(java.lang.String tableName)
Get a list of the fields in a table.

Parameters:
tableName - the table name
Returns:
the meta data result set

borrowConnection

public ConnectionObject borrowConnection()
                                  throws java.sql.SQLException
Borrow the connection object

Returns:
the connection object
Throws:
java.sql.SQLException

returnConnection

public void returnConnection()
Return the connection object to the pool


doQuery

public java.sql.ResultSet doQuery(java.lang.String sql)
Invoke a SQL query and return the result set

Parameters:
sql - the query string
Returns:
the result set or null on failure

doUpdate

public int doUpdate(java.lang.String sql)
Invoke a SQL update query

Parameters:
sql - the query string
Returns:
the result set or Integer.MIN_VALUE on failure

shutdown

public static void shutdown(java.lang.String connParamName)
                     throws java.lang.Exception
Execute a SHUTDOWN statement

Parameters:
connParamName - the name of the connection on which the shutdown statement is executed, or null for the default connection
Throws:
java.lang.Exception

setReplicationEnabled

public static void setReplicationEnabled(boolean state)
Set the replication mode

Parameters:
true - to allow replication, false to turn it off

commit

public void commit()
            throws java.sql.SQLException
Explicitly force a commit

Throws:
java.sql.SQLException

setAutoCommit

public void setAutoCommit(boolean state)
                   throws java.sql.SQLException
Set the auto commit state

Parameters:
true - to turn auto commit on
Throws:
java.sql.SQLException

getDoesEscapeProcessing

public boolean getDoesEscapeProcessing()
Gets the escape processing flag for the underlying JDBC implementation

Returns:
true for setEscapeProcssing( true )

setDoesEscapeProcessing

public void setDoesEscapeProcessing(boolean escapeProcessing)
Set the escape processing flag for the underlying JDBC implementation

Parameters:
doesEscapeProcessing - true to setEscapeProcssing( true )

getNextPseudoId

public long getNextPseudoId(java.lang.String tableName)
Get the next PSUEDOID for the names table

Parameters:
tableName - the table name whose ID is to be inspected
Returns:
the next ID or a negative value if invalid

getNextId

public long getNextId(java.lang.String tableName)
Get the next ID for the named table, assuming one of the table's fields is a managed field.

Parameters:
tableName - the table name whose ID is to be inspected
Returns:
the next ID or a negative value if invalid