org.rimudb.sql
Class DefaultAdapter

java.lang.Object
  extended by org.rimudb.sql.DefaultAdapter
All Implemented Interfaces:
ISQLAdapter
Direct Known Subclasses:
DB2Adapter, DB2ISeriesAdapter, DerbyAdapter, H2Adapter, HsqldbAdapter, MySQLAdapter, Oracle8iAdapter, PostgreSQLAdapter, SQLServerAdapter

public abstract class DefaultAdapter
extends java.lang.Object
implements ISQLAdapter

This abstract class is used to contain the functionality used to construct SQL statements. It is overridden for behavior for specific databases.

Author:
Simon Ritchie

Field Summary
static int DB_H2
           
static int DB_HSQLDB
           
static int DB_ISERIES
           
static int DB_MYSQL
           
static int DB_SQLSERVER
           
static int DB_UNKNOWN
           
static int IDX_INDEX
           
static int IDX_PRIMARY
           
static int IDX_UNIQUE
           
static int LOCK_DELETE
           
static int LOCK_INSERT
           
static int LOCK_NONE
           
static int LOCK_SELECT
           
static int LOCK_UPDATE
           
 
Fields inherited from interface org.rimudb.sql.ISQLAdapter
IDENTITY_GENERATED_KEYS, IDENTITY_INSERT_STMT, IDENTITY_SEPARATE_STATEMENT
 
Constructor Summary
DefaultAdapter()
           
 
Method Summary
 java.lang.String build(java.lang.String selectClause, java.lang.String tableName, java.lang.String whereClause, java.lang.String groupByClause, java.lang.String orderByClause, int maxRecords)
          Build the SQL statement from a KeyList and a WhereList
 java.lang.String build(TableMetaData tableMetaData, java.lang.String tableName, WhereList whereList, GroupByList groupByList, OrderByList orderByList, int maxRecords, boolean useLockingHints)
          Build the SQL statement from a KeyList and a WhereList
protected  java.lang.String buildGroupByClause(TableMetaData tableMetaData, GroupByList groupByList)
          Return the group by clause
protected  java.lang.String buildOrderByClause(TableMetaData tableMetaData, OrderByList orderByList)
          Return the SQL ORDER BY clause.
protected  java.lang.String buildSelectClause(TableMetaData tableMetaData, GroupByList groupByList)
          Return the select clause
protected  java.lang.String buildSQL(TableMetaData tableMetaData, GroupByList groupByList)
          Build SQL for a GroupByList
protected  java.lang.String buildSQL(TableMetaData tableMetaData, OrderByList orderByList)
          Build SQL for an OrderByList
protected  java.lang.String buildSQL(TableMetaData tableMetaData, WhereList whereList)
           
protected  java.lang.String buildWhereClause(TableMetaData tableMetaData, WhereList whereList)
          Return the where clause
 java.lang.String convertNullCapable(boolean nullCapable, boolean isAutoIncrement)
          Return the String for null capability.
 java.lang.String convertSQLType(int type, long precision, int scale)
          Convert the SQL type to a type that the database can handle.
 void correctRecordData(TableMetaData tableMetaData, Record record)
          Transform record data that will not be able to be written to this database without some alteration.
 java.lang.String getBeginLimitClause(int maxRecords, int offset)
          Returns a clause that is used before the select statement (usually in conjunction with an end limit clause that limits the number of records returned to the first n records.
 char getCloseQuote()
          Return the database close quote character.
abstract  java.lang.String getCreateIfExistsClause()
          Return the If Exists clause for a create table statement
 java.lang.String getCreateTableSQL(java.lang.String qualifiedTableName, TableMetaData tableMetaData, boolean ignoreIfExists, boolean prettyFormat)
          Return the create table SQL.
protected  DatabaseConfiguration getDatabaseConfiguration()
           
 RimuDBDatabaseMetaData getDatabaseMetaData()
          Return RimuDBDatabaseMetaData
 java.lang.String getDatabaseNames()
          Return a String containing an ordered list of database names.
 java.lang.String getDeleteStatement(TableMetaData tableMetaData, java.lang.String tableName, WhereList whereList)
          Return a parameterized delete statement that will remove records that match the column names on the KeyList and WhereList.
abstract  java.lang.String getDropIfExistsClause()
          Return the clause for "drop if exists".
 java.lang.String getDropTableSQL(java.lang.String qualifiedTableName, boolean ignoreIfExists)
           
 java.lang.String getIdentitySelectSQL(java.lang.String tableName, java.lang.String columnName, int columnType)
          Return an SQL statement that will return the value assigned to an auto-increment column.
 java.lang.String getInsertStatement(TableMetaData tableMetaData, java.lang.String tableName)
          Return a parameterized insert statement.
 java.lang.String getLockingHint(LockHintType lockHintType)
          Return the locking hint for this database.
 char getOpenQuote()
          Return the database open quote character.
 java.lang.String getPrimaryKeySelectStatement(Lock lock, TableMetaData tableMetaData, java.lang.String tableName)
          Return an SQL statement to retrieve all the columns for the primary key.
 java.lang.String getPrimaryKeyUpdateStatement(TableMetaData tableMetaData, java.lang.String tableName, OptimisticLocking optimisticLocking, java.util.List<java.lang.String> changedPropertyList, java.util.List<java.lang.String> nullColumnList)
          Return an update statement that sets all the columns in a record for a given primary key.
 ColumnQuoter getQuoter()
           
 java.lang.String getSelectAllSQL(java.lang.String tableName)
          Return an SQL Statement that will select all the records in the table.
 java.lang.String getSelectCountSQL(TableMetaData tableMetaData, java.lang.String tableName, WhereList whereList)
          Build the SQL statement for a record count from a NamedKeyList and/or a WhereList
 java.lang.String getTableType()
          Return the type of table for the CREATE TABLE clause.
 java.lang.String getTruncateTableSQL(java.lang.String tableName)
          Return an SQL Statement that will truncate the table.
 boolean hasPrimaryKeyClause()
          Return true if the Create Table statement should have a PRIMARY KEY clause
 boolean isInsertIndentityStatementSupported()
          Return true if the database supports a awy of extracting the identity value from the insert statement.
 void setDatabaseConfiguration(DatabaseConfiguration databaseConfiguration)
          Set the DatabaseConfiguration.
 void setDatabaseMetaData(RimuDBDatabaseMetaData dbMetaData)
          Set the RimuDBDatabaseMetaData.
 void setUseQuotes(boolean useQuotes)
          Set the useQuotes property.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.rimudb.sql.ISQLAdapter
convertAutoIncrement, getEndLimitClause, getSupportsMultipleGetGeneratedKeys, getSupportsReturnGeneratedKeys
 

Field Detail

IDX_UNIQUE

public static final int IDX_UNIQUE
See Also:
Constant Field Values

IDX_INDEX

public static final int IDX_INDEX
See Also:
Constant Field Values

IDX_PRIMARY

public static final int IDX_PRIMARY
See Also:
Constant Field Values

LOCK_NONE

public static final int LOCK_NONE
See Also:
Constant Field Values

LOCK_SELECT

public static final int LOCK_SELECT
See Also:
Constant Field Values

LOCK_INSERT

public static final int LOCK_INSERT
See Also:
Constant Field Values

LOCK_UPDATE

public static final int LOCK_UPDATE
See Also:
Constant Field Values

LOCK_DELETE

public static final int LOCK_DELETE
See Also:
Constant Field Values

DB_UNKNOWN

public static final int DB_UNKNOWN
See Also:
Constant Field Values

DB_MYSQL

public static final int DB_MYSQL
See Also:
Constant Field Values

DB_ISERIES

public static final int DB_ISERIES
See Also:
Constant Field Values

DB_SQLSERVER

public static final int DB_SQLSERVER
See Also:
Constant Field Values

DB_HSQLDB

public static final int DB_HSQLDB
See Also:
Constant Field Values

DB_H2

public static final int DB_H2
See Also:
Constant Field Values
Constructor Detail

DefaultAdapter

public DefaultAdapter()
Method Detail

convertSQLType

public java.lang.String convertSQLType(int type,
                                       long precision,
                                       int scale)
Description copied from interface: ISQLAdapter
Convert the SQL type to a type that the database can handle.

Specified by:
convertSQLType in interface ISQLAdapter
Parameters:
type - int
precision - long
scale - int
Returns:
String

convertNullCapable

public java.lang.String convertNullCapable(boolean nullCapable,
                                           boolean isAutoIncrement)
Description copied from interface: ISQLAdapter
Return the String for null capability.

Specified by:
convertNullCapable in interface ISQLAdapter
Parameters:
nullCapable - boolean
isAutoIncrement - boolean
Returns:
String

getCreateTableSQL

public java.lang.String getCreateTableSQL(java.lang.String qualifiedTableName,
                                          TableMetaData tableMetaData,
                                          boolean ignoreIfExists,
                                          boolean prettyFormat)
Description copied from interface: ISQLAdapter
Return the create table SQL.

Specified by:
getCreateTableSQL in interface ISQLAdapter
Parameters:
qualifiedTableName - String
tableMetaData - TableMetaData
ignoreIfExists - boolean
Returns:
String

getDropTableSQL

public java.lang.String getDropTableSQL(java.lang.String qualifiedTableName,
                                        boolean ignoreIfExists)

getCreateIfExistsClause

public abstract java.lang.String getCreateIfExistsClause()
Description copied from interface: ISQLAdapter
Return the If Exists clause for a create table statement

Specified by:
getCreateIfExistsClause in interface ISQLAdapter
Returns:
String

getDropIfExistsClause

public abstract java.lang.String getDropIfExistsClause()
Description copied from interface: ISQLAdapter
Return the clause for "drop if exists".

Specified by:
getDropIfExistsClause in interface ISQLAdapter
Returns:
String

hasPrimaryKeyClause

public boolean hasPrimaryKeyClause()
Description copied from interface: ISQLAdapter
Return true if the Create Table statement should have a PRIMARY KEY clause

Specified by:
hasPrimaryKeyClause in interface ISQLAdapter
Returns:
boolean

getTableType

public java.lang.String getTableType()
Description copied from interface: ISQLAdapter
Return the type of table for the CREATE TABLE clause.

Specified by:
getTableType in interface ISQLAdapter
Returns:
String

build

public java.lang.String build(TableMetaData tableMetaData,
                              java.lang.String tableName,
                              WhereList whereList,
                              GroupByList groupByList,
                              OrderByList orderByList,
                              int maxRecords,
                              boolean useLockingHints)
                       throws RimuDBException
Build the SQL statement from a KeyList and a WhereList

Specified by:
build in interface ISQLAdapter
Throws:
RimuDBException

build

public java.lang.String build(java.lang.String selectClause,
                              java.lang.String tableName,
                              java.lang.String whereClause,
                              java.lang.String groupByClause,
                              java.lang.String orderByClause,
                              int maxRecords)
                       throws RimuDBException
Build the SQL statement from a KeyList and a WhereList

Specified by:
build in interface ISQLAdapter
Throws:
RimuDBException

getSelectCountSQL

public java.lang.String getSelectCountSQL(TableMetaData tableMetaData,
                                          java.lang.String tableName,
                                          WhereList whereList)
                                   throws RimuDBException
Build the SQL statement for a record count from a NamedKeyList and/or a WhereList

Specified by:
getSelectCountSQL in interface ISQLAdapter
Parameters:
tableMetaData - TableMetaData
tableName - String
whereList - WhereList
Returns:
String
Throws:
RimuDBException

getSelectAllSQL

public java.lang.String getSelectAllSQL(java.lang.String tableName)
Return an SQL Statement that will select all the records in the table.

Specified by:
getSelectAllSQL in interface ISQLAdapter
Parameters:
tableName -
Returns:
String

getTruncateTableSQL

public java.lang.String getTruncateTableSQL(java.lang.String tableName)
Return an SQL Statement that will truncate the table.

Specified by:
getTruncateTableSQL in interface ISQLAdapter
Parameters:
tableName -
Returns:
String

buildSelectClause

protected java.lang.String buildSelectClause(TableMetaData tableMetaData,
                                             GroupByList groupByList)
Return the select clause


buildWhereClause

protected java.lang.String buildWhereClause(TableMetaData tableMetaData,
                                            WhereList whereList)
                                     throws RimuDBException
Return the where clause

Throws:
RimuDBException

buildSQL

protected java.lang.String buildSQL(TableMetaData tableMetaData,
                                    WhereList whereList)
                             throws RimuDBException
Throws:
RimuDBException

buildOrderByClause

protected java.lang.String buildOrderByClause(TableMetaData tableMetaData,
                                              OrderByList orderByList)
Return the SQL ORDER BY clause. If the orderByList is null then it is assumed to be an OrderByList.PRIMARY_KEY. If the orderByList has no entries then it is assumed to be an OrderByList.NONE. If the OrderByList is OrderByList.NONE then nothing is returned. If the OrderByList is OrderByList.PRIMARY_KEY then the primary key columns are return. Otherwise the keys from the orderByList are returned.

Parameters:
tableMetaData -
orderByList -
Returns:
String The Order By clause

buildGroupByClause

protected java.lang.String buildGroupByClause(TableMetaData tableMetaData,
                                              GroupByList groupByList)
Return the group by clause


buildSQL

protected java.lang.String buildSQL(TableMetaData tableMetaData,
                                    OrderByList orderByList)
Build SQL for an OrderByList

Parameters:
tableMetaData - TableMetaData
orderByList - OrderByList
Returns:
String

buildSQL

protected java.lang.String buildSQL(TableMetaData tableMetaData,
                                    GroupByList groupByList)
Build SQL for a GroupByList

Parameters:
groupByList - GroupByList
Returns:
String

getPrimaryKeySelectStatement

public java.lang.String getPrimaryKeySelectStatement(Lock lock,
                                                     TableMetaData tableMetaData,
                                                     java.lang.String tableName)
Description copied from interface: ISQLAdapter
Return an SQL statement to retrieve all the columns for the primary key.

Specified by:
getPrimaryKeySelectStatement in interface ISQLAdapter
Parameters:
lock - Lock
tableMetaData - TableMetaData
tableName - String
Returns:
String

getPrimaryKeyUpdateStatement

public java.lang.String getPrimaryKeyUpdateStatement(TableMetaData tableMetaData,
                                                     java.lang.String tableName,
                                                     OptimisticLocking optimisticLocking,
                                                     java.util.List<java.lang.String> changedPropertyList,
                                                     java.util.List<java.lang.String> nullColumnList)
Description copied from interface: ISQLAdapter
Return an update statement that sets all the columns in a record for a given primary key.

Specified by:
getPrimaryKeyUpdateStatement in interface ISQLAdapter
Parameters:
tableMetaData - TableMetaData
tableName - String
optimisticLocking - OptimisticLocking
changedPropertyList - List
nullColumnList - List
Returns:
String

getDeleteStatement

public java.lang.String getDeleteStatement(TableMetaData tableMetaData,
                                           java.lang.String tableName,
                                           WhereList whereList)
                                    throws RimuDBException
Description copied from interface: ISQLAdapter
Return a parameterized delete statement that will remove records that match the column names on the KeyList and WhereList.

Specified by:
getDeleteStatement in interface ISQLAdapter
Parameters:
tableMetaData - TableMetaData
tableName - String
whereList - WhereList
Returns:
String
Throws:
RimuDBException

getInsertStatement

public java.lang.String getInsertStatement(TableMetaData tableMetaData,
                                           java.lang.String tableName)
Description copied from interface: ISQLAdapter
Return a parameterized insert statement.

Specified by:
getInsertStatement in interface ISQLAdapter
Parameters:
tableMetaData - TableMetaData
tableName - String
Returns:
String

setDatabaseConfiguration

public void setDatabaseConfiguration(DatabaseConfiguration databaseConfiguration)
Description copied from interface: ISQLAdapter
Set the DatabaseConfiguration.

Specified by:
setDatabaseConfiguration in interface ISQLAdapter
Parameters:
databaseConfiguration -

getDatabaseConfiguration

protected DatabaseConfiguration getDatabaseConfiguration()

getDatabaseNames

public java.lang.String getDatabaseNames()
Description copied from interface: ISQLAdapter
Return a String containing an ordered list of database names.

Specified by:
getDatabaseNames in interface ISQLAdapter
Returns:
String

setDatabaseMetaData

public void setDatabaseMetaData(RimuDBDatabaseMetaData dbMetaData)
Description copied from interface: ISQLAdapter
Set the RimuDBDatabaseMetaData.

Specified by:
setDatabaseMetaData in interface ISQLAdapter
Parameters:
dbMetaData - RimuDBDatabaseMetaData

getDatabaseMetaData

public RimuDBDatabaseMetaData getDatabaseMetaData()
Return RimuDBDatabaseMetaData

Specified by:
getDatabaseMetaData in interface ISQLAdapter
Returns:
RimuDBDatabaseMetaData

isInsertIndentityStatementSupported

public boolean isInsertIndentityStatementSupported()
Description copied from interface: ISQLAdapter
Return true if the database supports a awy of extracting the identity value from the insert statement.

Specified by:
isInsertIndentityStatementSupported in interface ISQLAdapter

getIdentitySelectSQL

public java.lang.String getIdentitySelectSQL(java.lang.String tableName,
                                             java.lang.String columnName,
                                             int columnType)
Return an SQL statement that will return the value assigned to an auto-increment column.

Specified by:
getIdentitySelectSQL in interface ISQLAdapter
Parameters:
tableName - String
columnName - String
columnType - int
Returns:
String

getOpenQuote

public char getOpenQuote()
Description copied from interface: ISQLAdapter
Return the database open quote character.

Specified by:
getOpenQuote in interface ISQLAdapter
Returns:
char

getCloseQuote

public char getCloseQuote()
Description copied from interface: ISQLAdapter
Return the database close quote character.

Specified by:
getCloseQuote in interface ISQLAdapter
Returns:
char

setUseQuotes

public void setUseQuotes(boolean useQuotes)
Description copied from interface: ISQLAdapter
Set the useQuotes property.

Specified by:
setUseQuotes in interface ISQLAdapter
Parameters:
useQuotes - boolean

getQuoter

public ColumnQuoter getQuoter()

getBeginLimitClause

public java.lang.String getBeginLimitClause(int maxRecords,
                                            int offset)
Description copied from interface: ISQLAdapter
Returns a clause that is used before the select statement (usually in conjunction with an end limit clause that limits the number of records returned to the first n records.

Specified by:
getBeginLimitClause in interface ISQLAdapter
Parameters:
maxRecords - int
offset - int
Returns:
String

getLockingHint

public java.lang.String getLockingHint(LockHintType lockHintType)
Return the locking hint for this database.

Specified by:
getLockingHint in interface ISQLAdapter
Parameters:
lockHintType - LockHintType
Returns:
String

correctRecordData

public void correctRecordData(TableMetaData tableMetaData,
                              Record record)
                       throws RimuDBException
Description copied from interface: ISQLAdapter
Transform record data that will not be able to be written to this database without some alteration. This is used when copying a table from one database to another. Sometimes column values that are permissible in the source database are not permissible in the target database. This method allows the values of the target database to alter the values prior to being written.

Specified by:
correctRecordData in interface ISQLAdapter
Parameters:
tableMetaData - TableMetaData
record - Record
Throws:
RimuDBException


Copyright © 2008-2011 Simon Ritchie. All Rights Reserved.