de.mathema.pride
Class AbstractResourceAccessor

java.lang.Object
  extended by de.mathema.pride.AbstractResourceAccessor
All Implemented Interfaces:
ResourceAccessor, SQLFormatter
Direct Known Subclasses:
ResourceAccessorJ2EE, ResourceAccessorJ2SE

public abstract class AbstractResourceAccessor
extends java.lang.Object
implements ResourceAccessor


Nested Class Summary
 
Nested classes/interfaces inherited from interface de.mathema.pride.ResourceAccessor
ResourceAccessor.AutoKeyMode, ResourceAccessor.Config, ResourceAccessor.DBType
 
Field Summary
protected  int autoKeyMode
           
protected  java.text.SimpleDateFormat dateFormat
           
protected  java.lang.String dbPassword
           
protected  java.util.Date dbSystime
           
protected  java.lang.String dbType
           
protected  java.lang.String dbUser
           
protected  java.util.Properties props
           
protected  java.text.SimpleDateFormat timeFormat
           
 
Fields inherited from interface de.mathema.pride.ResourceAccessor
REVISION_ID, SYSTIME_DEFAULT
 
Constructor Summary
AbstractResourceAccessor()
           
AbstractResourceAccessor(java.util.Properties props)
          Create a new AbstractResourceAccessor according to the passed properties.
 
Method Summary
protected  java.lang.Object castJavaUtilDate(java.lang.Object value)
          This function maps java.util.Date to java.sql.Timestamp, assuming that java.util.Date members are supposed to provide highest possible precision.
protected  java.text.SimpleDateFormat commonDateFormat()
          Returns a common format for date and/or time values, based on the database type.
protected static java.lang.String escape(java.lang.String raw, char escapeChar)
          Makes '' from single-quotes in the passed string to make it suitable for SQL syntax
protected  java.lang.String escapeBackslahes(java.lang.String raw)
          Makes double backslashes from single backslashes which is only required for MySQL
protected static java.lang.String escapeQuotes(java.lang.String raw)
          Makes '' from single-quotes in the passed string to make it suitable for SQL syntax
protected  java.lang.String formatBoolean(java.lang.Boolean value)
           
protected  java.lang.String formatDate(java.sql.Date date)
          Like formatTime(java.sql.Timestamp) but for date values.
protected  java.lang.String formatEnum(java.lang.Enum value)
           
 java.lang.String formatOperator(java.lang.String operator, java.lang.Object value)
          Formats an operator for usage in SQL statements.
 java.lang.Object formatPreparedValue(java.lang.Object value)
          Converts the passed value for usage in a prepared statement write access function The function currently just runs formatEnum(java.lang.Enum) or castJavaUtilDate(java.lang.Object)
protected  java.lang.String formatTime(java.sql.Timestamp time)
          Formats a time value into an SQL-suitable string.
 java.lang.String formatValue(java.lang.Object value)
          Formats the passed object for SQL syntax, e.g. by putting single-quotes arround strings etc.
 java.lang.String[] getAutoFields(java.sql.Statement stmt, java.lang.String[] rawAutoFields)
          The function initializes the auto key fetching mode of not yet done by accessing the passed statement's meta data and calling supportsGetGeneratedKeys().
 java.sql.ResultSet getAutoFieldVals(java.sql.Statement stmt, java.lang.String[] rawAutoFields)
          Return the list of auto field values using JDBC's getGeneratedKeys() function on the passed statement.
 java.lang.String getDBType()
          Returns the type of database as specified by the constructor properties or null if no type is specified at all.
 java.util.Date getSystime()
          Retrieves the timestamp, that is currently used to identify, that a date has to be replaced by the database server's system time.
 java.lang.String getTableName(java.lang.String logicalTableName)
          Returns the passed logical table name as physical name
 java.lang.String getURL(java.lang.String db)
          Retrieves the URL for this DBMS represented by the ResourceAccessor.
 java.lang.String getUserName(java.lang.String db)
          Retrieves the user name as known to this database represented by the ResourceAccessor.
 boolean isLogging()
          Returns true if SQL logging is switched on
protected  void setAutoCommit(java.sql.Connection con, boolean state)
           
 boolean setLogging(Database db, boolean val)
          Switch SQL logging on and off.
 void sqlLog(Database db, java.lang.String operation)
          Writes the passed operation to the SQL log, if logging is enabled
 void sqlLogError(Database db, java.sql.SQLException sqlx)
          Writes the passed SQL exception to the log, if logging is enabled
static java.lang.String standardOperator(java.lang.String operator, java.lang.Object value)
          Formats an operator for usage in SQL statements.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface de.mathema.pride.ResourceAccessor
getConnection, releaseConnection, releaseConnection
 

Field Detail

dbType

protected java.lang.String dbType

dateFormat

protected java.text.SimpleDateFormat dateFormat

timeFormat

protected java.text.SimpleDateFormat timeFormat

dbUser

protected java.lang.String dbUser

dbPassword

protected java.lang.String dbPassword

dbSystime

protected java.util.Date dbSystime

props

protected java.util.Properties props

autoKeyMode

protected int autoKeyMode
Constructor Detail

AbstractResourceAccessor

public AbstractResourceAccessor(java.util.Properties props)
                         throws java.lang.Exception
Create a new AbstractResourceAccessor according to the passed properties. See interface ResourceAccessor.Config for available configuration properties.

Throws:
java.lang.Exception

AbstractResourceAccessor

public AbstractResourceAccessor()
                         throws java.lang.Exception
Throws:
java.lang.Exception
Method Detail

setLogging

public boolean setLogging(Database db,
                          boolean val)
Switch SQL logging on and off.

Specified by:
setLogging in interface ResourceAccessor
Returns:
the current logging state, i.e. usually the desired state passed in parameter val except when toggling doesn't work. E.g. switching logging on doesn't work if there is no SQL log file specified.

isLogging

public boolean isLogging()
Returns true if SQL logging is switched on

Specified by:
isLogging in interface ResourceAccessor

sqlLog

public void sqlLog(Database db,
                   java.lang.String operation)
Description copied from interface: ResourceAccessor
Writes the passed operation to the SQL log, if logging is enabled

Specified by:
sqlLog in interface ResourceAccessor

sqlLogError

public void sqlLogError(Database db,
                        java.sql.SQLException sqlx)
Description copied from interface: ResourceAccessor
Writes the passed SQL exception to the log, if logging is enabled

Specified by:
sqlLogError in interface ResourceAccessor

escapeQuotes

protected static java.lang.String escapeQuotes(java.lang.String raw)
Makes '' from single-quotes in the passed string to make it suitable for SQL syntax

Returns:
the string with the escaped single-quotes

escapeBackslahes

protected java.lang.String escapeBackslahes(java.lang.String raw)
Makes double backslashes from single backslashes which is only required for MySQL

Returns:
the string with the escaped single backslahes

escape

protected static java.lang.String escape(java.lang.String raw,
                                         char escapeChar)
Makes '' from single-quotes in the passed string to make it suitable for SQL syntax

Returns:
the string with the escaped single-quotes

formatBoolean

protected java.lang.String formatBoolean(java.lang.Boolean value)

commonDateFormat

protected java.text.SimpleDateFormat commonDateFormat()
Returns a common format for date and/or time values, based on the database type. For Oracle databases, the propriatary to_date syntax is used

Returns:
The common date format to use or null if there is no format known

formatDate

protected java.lang.String formatDate(java.sql.Date date)
Like formatTime(java.sql.Timestamp) but for date values. This function is synchronized due to the fact that DateFormat.format is not!!

Parameters:
time - The value to format
Returns:
The SQL-formatted value

formatTime

protected java.lang.String formatTime(java.sql.Timestamp time)
Formats a time value into an SQL-suitable string. The function uses the time format specified in the constructor or tries to get a get a common date-and-time format from commonDateFormat(). If there is nothing to find, it returns the value itself, surrounded by single-quotes. This function is synchronized due to the fact that DateFormat.format is not!!

Parameters:
time - The value to format
Returns:
The SQL-formatted value

formatEnum

protected java.lang.String formatEnum(java.lang.Enum value)

castJavaUtilDate

protected java.lang.Object castJavaUtilDate(java.lang.Object value)
This function maps java.util.Date to java.sql.Timestamp, assuming that java.util.Date members are supposed to provide highest possible precision. This mapping should match the specifications in ResultSetAccess and PreparedStatementAccess.

Returns:
A java.sql.Timestamp if the passed value was of type java.util.Date or a derivation other than java.sql.Date or java.sql.Timestamp. Otherwise the passed value itself.

formatValue

public java.lang.String formatValue(java.lang.Object value)
Formats the passed object for SQL syntax, e.g. by putting single-quotes arround strings etc. The function is not very flexible yet, it just supports special formatting for String, java.util.Date, java.sql.Date, java.sql.Timestamp, and null. In all other cases it performs value.toString()

Specified by:
formatValue in interface ResourceAccessor
Specified by:
formatValue in interface SQLFormatter

standardOperator

public static java.lang.String standardOperator(java.lang.String operator,
                                                java.lang.Object value)
Formats an operator for usage in SQL statements.

Parameters:
Any - of the operators in SQLExpression.Operator
value - The value to apply the operator to before it is formatted
Returns:
A valid SQL operator. By default the function just retuns the passed operator as is. If the value is NULL it returns "IS" for operator SQLExpression.Operator.EQUAL and "IS NOT" for operator SQLExpression.Operator.UNEQUAL.

formatOperator

public java.lang.String formatOperator(java.lang.String operator,
                                       java.lang.Object value)
Formats an operator for usage in SQL statements. See standardOperator(java.lang.String, java.lang.Object) for details

Specified by:
formatOperator in interface ResourceAccessor
Specified by:
formatOperator in interface SQLFormatter
Parameters:
operator - Any of the operators defined in SQLExpression.Operator
value - The raw value the operator is applied to before it is formated by ResourceAccessor.formatValue(java.lang.Object). May be null.

formatPreparedValue

public java.lang.Object formatPreparedValue(java.lang.Object value)
Converts the passed value for usage in a prepared statement write access function The function currently just runs formatEnum(java.lang.Enum) or castJavaUtilDate(java.lang.Object)

Specified by:
formatPreparedValue in interface ResourceAccessor
Specified by:
formatPreparedValue in interface SQLFormatter

getTableName

public java.lang.String getTableName(java.lang.String logicalTableName)
                              throws java.lang.Exception
Returns the passed logical table name as physical name

Specified by:
getTableName in interface ResourceAccessor
Throws:
java.lang.Exception

setAutoCommit

protected void setAutoCommit(java.sql.Connection con,
                             boolean state)
                      throws java.sql.SQLException
Throws:
java.sql.SQLException

getAutoFields

public java.lang.String[] getAutoFields(java.sql.Statement stmt,
                                        java.lang.String[] rawAutoFields)
The function initializes the auto key fetching mode of not yet done by accessing the passed statement's meta data and calling supportsGetGeneratedKeys().

Specified by:
getAutoFields in interface ResourceAccessor
Parameters:
stmt - The statement which is about to be executed
rawAutoFields - The auto fields to consider as declared in a descriptor
Returns:
the passed list of fields if auto key mode is set to STANDARD or null otherwise. For MySQL, the function always returns null because MySQL 3 supports getGeneratedKeys() but doesn't support passing the field list in insert execution (strange but true).

getAutoFieldVals

public java.sql.ResultSet getAutoFieldVals(java.sql.Statement stmt,
                                           java.lang.String[] rawAutoFields)
                                    throws java.sql.SQLException
Return the list of auto field values using JDBC's getGeneratedKeys() function on the passed statement.

Specified by:
getAutoFieldVals in interface ResourceAccessor
Parameters:
stmt - SQL statement object which was last be used to execute an insertion.
rawAutoFields - The auto fields to consider as declared in a descriptor
Throws:
java.sql.SQLException

getSystime

public java.util.Date getSystime()
Retrieves the timestamp, that is currently used to identify, that a date has to be replaced by the database server's system time. If a date for an sql statement matches this value, it will be replaced by a database specific string in insert and update statements.

Specified by:
getSystime in interface ResourceAccessor
Returns:
the current reference date

getURL

public java.lang.String getURL(java.lang.String db)
                        throws java.lang.Exception
Retrieves the URL for this DBMS represented by the ResourceAccessor.

Specified by:
getURL in interface ResourceAccessor
Parameters:
db -
Returns:
the database url
Throws:
java.lang.Exception
See Also:
DatabaseMetaData

getUserName

public java.lang.String getUserName(java.lang.String db)
                             throws java.lang.Exception
Retrieves the user name as known to this database represented by the ResourceAccessor.

Specified by:
getUserName in interface ResourceAccessor
Parameters:
db -
Returns:
the user name
Throws:
java.lang.Exception
See Also:
DatabaseMetaData

getDBType

public java.lang.String getDBType()
Returns the type of database as specified by the constructor properties or null if no type is specified at all.

Specified by:
getDBType in interface ResourceAccessor