org.eclipse.net4j.spi.db
Class DBAdapter

java.lang.Object
  extended by org.eclipse.net4j.spi.db.DBAdapter
All Implemented Interfaces:
IDBAdapter
Direct Known Subclasses:
DerbyAdapter, H2Adapter, HSQLDBAdapter, MYSQLAdapter, PostgreSQLAdapter

public abstract class DBAdapter
extends Object
implements IDBAdapter

A useful base class for implementing custom DB adapters.


Nested Class Summary
protected static class DBAdapter.FieldInfo
           
 
Field Summary
 
Fields inherited from interface org.eclipse.net4j.db.IDBAdapter
REGISTRY
 
Constructor Summary
DBAdapter(String name, String version)
           
 
Method Summary
 DBType adaptType(DBType type)
          Provide a way for the DBAdapter to override unsupported DB types with replacements.
protected  void addIndex(Connection connection, IDBTable table, String name, IDBIndex.Type type, List<DBAdapter.FieldInfo> fieldInfos)
           
protected  void addIndexField(StringBuilder builder, IDBField field)
           
protected  void alterTable(Connection connection, IDBTable table, IDBTableDelta delta)
           
protected  void appendFieldDefs(Appendable appendable, IDBTable table, String[] defs)
           
 void appendFieldNames(Appendable appendable, IDBTable table)
           
protected  String createConstraints(IDBTable table)
           
protected  String createFieldDefinition(IDBField field)
           
protected  String[] createFieldDefinitions(IDBTable table)
           
protected  void createIndex(Connection connection, IDBIndex index, IDBIndexDelta delta)
           
protected  void createIndex(IDBIndex index, Statement statement, int num)
           
protected  void createIndex(IDBIndex index, StringBuilder builder)
           
protected  void createIndexFields(IDBIndex index, StringBuilder builder)
           
 DataSource createJDBCDataSource()
          Deprecated. As of 4.2 no longer supported because of IP issues for external build dependencies (the vendor driver libs).
protected  void createPrimaryKey(IDBIndex index, StringBuilder builder)
           
protected  void createTable(Connection connection, IDBTable table, IDBTableDelta delta)
           
 boolean createTable(IDBTable table, Statement statement)
           
 Set<IDBTable> createTables(Iterable<? extends IDBTable> tables, Connection connection)
           
protected  void doCreateTable(IDBTable table, Statement statement)
           
protected  void dropIndex(Connection connection, IDBIndex index, IDBIndexDelta delta)
           
protected  void dropIndex(IDBIndex index, StringBuilder builder)
           
protected  void dropPrimaryKey(IDBIndex index, StringBuilder builder)
           
protected  void dropTable(Connection connection, IDBTable table, IDBTableDelta delta)
           
 boolean dropTable(IDBTable table, Statement statement)
           
 Collection<IDBTable> dropTables(Iterable<? extends IDBTable> tables, Connection connection)
           
 String format(PreparedStatement stmt)
           
 String format(ResultSet resultSet)
           
static int getDefaultDBLength(DBType type)
           
protected  String getDropTableSQL(IDBTable table)
           
 int getFieldLength(DBType type)
          Returns the column length for the given database type.
 Driver getJDBCDriver()
          Deprecated. As of 4.2 no longer supported because of IP issues for external build dependencies (the vendor driver libs).
 int getMaxFieldNameLength()
           
 int getMaxTableNameLength()
           
 String getName()
           
 String[] getSQL92ReservedWords()
           
protected  String getTypeName(IDBField field)
           
 String getVersion()
           
 boolean isColumnNotFoundException(SQLException ex)
           
 boolean isDuplicateKeyException(SQLException ex)
          Check if an exception indicates a constraint violation (duplicate key)
protected  boolean isPrimaryKeyShadow(Connection connection, IDBTable table, String name, IDBIndex.Type type, IDBField[] fields)
           
 boolean isReservedWord(String word)
           
 boolean isTableNotFoundException(SQLException ex)
           
 boolean isTypeIndexable(DBType type)
           
 boolean isValidFirstChar(char ch)
          Check if a character is valid as first character.
protected  void readFields(Connection connection, IDBTable table)
           
protected  void readIndices(Connection connection, DatabaseMetaData metaData, IDBTable table, String schemaName)
           
protected  void readIndices(Connection connection, ResultSet resultSet, IDBTable table, int indexNameColumn, int indexTypeColumn, int fieldNameColumn, int fieldPositionColumn)
           
 void readSchema(Connection connection, IDBSchema schema)
           
 IDBSchema readSchema(Connection connection, String name)
           
 String sqlModifyField(IDBField field)
           
protected  String sqlModifyField(String tableName, String fieldName, String definition)
           
 String sqlRenameField(IDBField field, String oldName)
           
 String toString()
           
 void updateSchema(Connection connection, IDBSchema schema, IDBSchemaDelta delta)
           
protected  void validateTable(IDBTable table, Statement statement)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.eclipse.net4j.db.IDBAdapter
getReservedWords
 

Constructor Detail

DBAdapter

public DBAdapter(String name,
                 String version)
Method Detail

getName

public String getName()
Specified by:
getName in interface IDBAdapter

getVersion

public String getVersion()
Specified by:
getVersion in interface IDBAdapter

getJDBCDriver

@Deprecated
public Driver getJDBCDriver()
Deprecated. As of 4.2 no longer supported because of IP issues for external build dependencies (the vendor driver libs).

Specified by:
getJDBCDriver in interface IDBAdapter
Since:
4.2

createJDBCDataSource

@Deprecated
public DataSource createJDBCDataSource()
Deprecated. As of 4.2 no longer supported because of IP issues for external build dependencies (the vendor driver libs).

Specified by:
createJDBCDataSource in interface IDBAdapter
Since:
4.2

readSchema

public IDBSchema readSchema(Connection connection,
                            String name)
Specified by:
readSchema in interface IDBAdapter
Since:
4.2

readSchema

public void readSchema(Connection connection,
                       IDBSchema schema)
Specified by:
readSchema in interface IDBAdapter
Since:
4.2

readFields

protected void readFields(Connection connection,
                          IDBTable table)
                   throws SQLException
Throws:
SQLException
Since:
4.2

readIndices

protected void readIndices(Connection connection,
                           DatabaseMetaData metaData,
                           IDBTable table,
                           String schemaName)
                    throws SQLException
Throws:
SQLException
Since:
4.2

readIndices

protected void readIndices(Connection connection,
                           ResultSet resultSet,
                           IDBTable table,
                           int indexNameColumn,
                           int indexTypeColumn,
                           int fieldNameColumn,
                           int fieldPositionColumn)
                    throws SQLException
Throws:
SQLException
Since:
4.2

addIndex

protected void addIndex(Connection connection,
                        IDBTable table,
                        String name,
                        IDBIndex.Type type,
                        List<DBAdapter.FieldInfo> fieldInfos)
Since:
4.2

isPrimaryKeyShadow

protected boolean isPrimaryKeyShadow(Connection connection,
                                     IDBTable table,
                                     String name,
                                     IDBIndex.Type type,
                                     IDBField[] fields)
Since:
4.2

updateSchema

public void updateSchema(Connection connection,
                         IDBSchema schema,
                         IDBSchemaDelta delta)
                  throws DBException
Specified by:
updateSchema in interface IDBAdapter
Throws:
DBException
Since:
4.2

createTable

protected void createTable(Connection connection,
                           IDBTable table,
                           IDBTableDelta delta)
Since:
4.2

dropTable

protected void dropTable(Connection connection,
                         IDBTable table,
                         IDBTableDelta delta)
Since:
4.2

alterTable

protected void alterTable(Connection connection,
                          IDBTable table,
                          IDBTableDelta delta)
Since:
4.2

createIndex

protected void createIndex(Connection connection,
                           IDBIndex index,
                           IDBIndexDelta delta)
Since:
4.2

createPrimaryKey

protected void createPrimaryKey(IDBIndex index,
                                StringBuilder builder)
Since:
4.2

createIndex

protected void createIndex(IDBIndex index,
                           StringBuilder builder)
Since:
4.2

createIndexFields

protected void createIndexFields(IDBIndex index,
                                 StringBuilder builder)
Since:
4.2

dropIndex

protected void dropIndex(Connection connection,
                         IDBIndex index,
                         IDBIndexDelta delta)
Since:
4.2

dropPrimaryKey

protected void dropPrimaryKey(IDBIndex index,
                              StringBuilder builder)
Since:
4.2

dropIndex

protected void dropIndex(IDBIndex index,
                         StringBuilder builder)
Since:
4.2

createTables

public Set<IDBTable> createTables(Iterable<? extends IDBTable> tables,
                                  Connection connection)
                           throws DBException
Specified by:
createTables in interface IDBAdapter
Throws:
DBException

createTable

public boolean createTable(IDBTable table,
                           Statement statement)
                    throws DBException
Specified by:
createTable in interface IDBAdapter
Throws:
DBException

dropTables

public Collection<IDBTable> dropTables(Iterable<? extends IDBTable> tables,
                                       Connection connection)
                                throws DBException
Specified by:
dropTables in interface IDBAdapter
Throws:
DBException

dropTable

public boolean dropTable(IDBTable table,
                         Statement statement)
Specified by:
dropTable in interface IDBAdapter

getDropTableSQL

protected String getDropTableSQL(IDBTable table)

getMaxTableNameLength

public int getMaxTableNameLength()
Specified by:
getMaxTableNameLength in interface IDBAdapter
Since:
2.0

getMaxFieldNameLength

public int getMaxFieldNameLength()
Specified by:
getMaxFieldNameLength in interface IDBAdapter
Since:
2.0

getFieldLength

public int getFieldLength(DBType type)
Description copied from interface: IDBAdapter
Returns the column length for the given database type.

Specified by:
getFieldLength in interface IDBAdapter
Parameters:
type - the DBType to check.
Returns:
the supported column length for the type.
Since:
4.2

isTypeIndexable

public boolean isTypeIndexable(DBType type)
Specified by:
isTypeIndexable in interface IDBAdapter

toString

public String toString()
Overrides:
toString in class Object

doCreateTable

protected void doCreateTable(IDBTable table,
                             Statement statement)
                      throws SQLException
Throws:
SQLException
Since:
2.0

createIndex

protected void createIndex(IDBIndex index,
                           Statement statement,
                           int num)
                    throws SQLException
Throws:
SQLException
Since:
2.0

addIndexField

protected void addIndexField(StringBuilder builder,
                             IDBField field)

createConstraints

protected String createConstraints(IDBTable table)
Since:
2.0

createFieldDefinition

protected String createFieldDefinition(IDBField field)
Since:
2.0

getTypeName

protected String getTypeName(IDBField field)

getSQL92ReservedWords

public String[] getSQL92ReservedWords()

isReservedWord

public boolean isReservedWord(String word)
Specified by:
isReservedWord in interface IDBAdapter

validateTable

protected void validateTable(IDBTable table,
                             Statement statement)
                      throws DBException
Throws:
DBException
Since:
2.0

createFieldDefinitions

protected String[] createFieldDefinitions(IDBTable table)
Since:
4.2

appendFieldNames

public void appendFieldNames(Appendable appendable,
                             IDBTable table)

appendFieldDefs

protected void appendFieldDefs(Appendable appendable,
                               IDBTable table,
                               String[] defs)
Since:
4.2

adaptType

public DBType adaptType(DBType type)
Description copied from interface: IDBAdapter
Provide a way for the DBAdapter to override unsupported DB types with replacements. The default implementation just returns the given type. Subclasses may override single types with replacements.

Specified by:
adaptType in interface IDBAdapter
Since:
3.0

isValidFirstChar

public boolean isValidFirstChar(char ch)
Description copied from interface: IDBAdapter
Check if a character is valid as first character. (e.g., underscores are forbidden as first character in Derby elements.

Specified by:
isValidFirstChar in interface IDBAdapter
Since:
4.0

isDuplicateKeyException

public boolean isDuplicateKeyException(SQLException ex)
Description copied from interface: IDBAdapter
Check if an exception indicates a constraint violation (duplicate key)

Specified by:
isDuplicateKeyException in interface IDBAdapter
Since:
4.0

isTableNotFoundException

public boolean isTableNotFoundException(SQLException ex)
Specified by:
isTableNotFoundException in interface IDBAdapter
Since:
4.2

isColumnNotFoundException

public boolean isColumnNotFoundException(SQLException ex)
Specified by:
isColumnNotFoundException in interface IDBAdapter
Since:
4.2

sqlRenameField

public String sqlRenameField(IDBField field,
                             String oldName)
Specified by:
sqlRenameField in interface IDBAdapter
Since:
4.2

sqlModifyField

public String sqlModifyField(IDBField field)
Specified by:
sqlModifyField in interface IDBAdapter
Since:
4.2

sqlModifyField

protected String sqlModifyField(String tableName,
                                String fieldName,
                                String definition)
Since:
4.2

format

public String format(PreparedStatement stmt)
Since:
4.2

format

public String format(ResultSet resultSet)
Since:
4.2

getDefaultDBLength

public static int getDefaultDBLength(DBType type)
Since:
4.2


Copyright (c) 2011, 2012 Eike Stepper (Berlin, Germany) and others.