Package org.apache.cayenne.dba
Class JdbcAdapter
- java.lang.Object
-
- org.apache.cayenne.dba.JdbcAdapter
-
- All Implemented Interfaces:
DbAdapter
- Direct Known Subclasses:
DB2Adapter
,DerbyAdapter
,FirebirdAdapter
,FrontBaseAdapter
,H2Adapter
,HSQLDBAdapter
,IngresAdapter
,MySQLAdapter
,OpenBaseAdapter
,OracleAdapter
,PostgresAdapter
,SQLiteAdapter
,SybaseAdapter
public class JdbcAdapter extends Object implements DbAdapter
A generic DbAdapter implementation. Can be used as a default adapter or as a superclass of a concrete adapter implementation.
-
-
Field Summary
Fields Modifier and Type Field Description protected BatchTranslatorFactory
batchQueryBuilderFactory
Deprecated.since 4.0 BatchQueryBuilderfactory is attached to the DataNode.protected boolean
caseInsensitiveCollations
protected EJBQLTranslatorFactory
ejbqlTranslatorFactory
protected ExtendedTypeMap
extendedTypes
protected JdbcEventLogger
logger
protected QuotingStrategy
quotingStrategy
protected ResourceLocator
resourceLocator
protected boolean
supportsBatchUpdates
protected boolean
supportsGeneratedKeys
protected boolean
supportsUniqueConstraints
protected TypesHandler
typesHandler
-
Constructor Summary
Constructors Constructor Description JdbcAdapter(RuntimeProperties runtimeProperties, List<ExtendedType> defaultExtendedTypes, List<ExtendedType> userExtendedTypes, List<ExtendedTypeFactory> extendedTypeFactories, ResourceLocator resourceLocator, ValueObjectTypeRegistry valueObjectTypeRegistry)
Creates new JdbcAdapter with a set of default parameters.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
bindParameter(PreparedStatement statement, ParameterBinding binding)
Binds an object value to PreparedStatement's parameter.DbAttribute
buildAttribute(String name, String typeName, int type, int size, int scale, boolean allowNulls)
Creates and returns a DbAttribute based on supplied parameters (usually obtained from database meta data).protected void
configureExtendedTypes(ExtendedTypeMap map)
Called frominitExtendedTypes(List, List, List, ValueObjectTypeRegistry)
to load adapter-specific types into the ExtendedTypeMap right after the default types are loaded, but before the DI overrides are.protected EJBQLTranslatorFactory
createEJBQLTranslatorFactory()
Creates and returns anEJBQLTranslatorFactory
used to generate visitors for EJBQL to SQL translations.String
createFkConstraint(DbRelationship rel)
Returns a SQL string that can be used to create a foreign key constraint for the relationship.protected PkGenerator
createPkGenerator()
Creates and returns a primary key generator.protected QuotingStrategy
createQuotingStrategy()
String
createTable(DbEntity entity)
Returns a SQL string that can be used to create database table corresponding toent
parameter.void
createTableAppendColumn(StringBuffer sqlBuffer, DbAttribute column)
Appends SQL for column creation to CREATE TABLE buffer.protected void
createTableAppendPKClause(StringBuffer sqlBuffer, DbEntity entity)
String
createUniqueConstraint(DbEntity source, Collection<DbAttribute> columns)
Returns a DDL string to create a unique constraint over a set of columns.Collection<String>
dropTableStatements(DbEntity table)
Returns a collection of SQL statements needed to drop a database table.String[]
externalTypesForJdbcType(int type)
Returns an array of RDBMS types that can be used with JDBCtype
.protected URL
findResource(String name)
Locates and returns a named adapter resource.SQLAction
getAction(Query query, DataNode node)
Uses JdbcActionBuilder to create the right action.BatchTranslatorFactory
getBatchQueryBuilderFactory()
Deprecated.since 4.0 BatchQueryBuilderfactory is attached to the DataNode.String
getBatchTerminator()
Returns default separator - a semicolon.EJBQLTranslatorFactory
getEjbqlTranslatorFactory()
Returns a translator factory for EJBQL to SQL translation.ExtendedTypeMap
getExtendedTypes()
Returns a map of ExtendedTypes that is used to translate values between Java and JDBC layer.JdbcEventLogger
getJdbcEventLogger()
PkGenerator
getPkGenerator()
Returns primary key generator associated with this DbAdapter.QualifierTranslator
getQualifierTranslator(QueryAssembler queryAssembler)
Creates and returns a default implementation of a qualifier translator.QuotingStrategy
getQuotingStrategy()
Returns SQL identifier quoting strategy objectQuotingStrategy
getQuotingStrategy(boolean needQuotes)
Deprecated.since 4.0 usegetQuotingStrategy()
.SelectTranslator
getSelectTranslator(SelectQuery<?> query, EntityResolver entityResolver)
Returns a SelectTranslator that works with the adapter target database.static String
getType(DbAdapter adapter, DbAttribute column)
protected void
initExtendedTypes(List<ExtendedType> defaultExtendedTypes, List<ExtendedType> userExtendedTypes, List<ExtendedTypeFactory> extendedTypeFactories, ValueObjectTypeRegistry valueObjectTypeRegistry)
void
setBatchQueryBuilderFactory(BatchTranslatorFactory batchQueryBuilderFactory)
Deprecated.since 4.0 BatchQueryBuilderfactory is attached to the DataNode.void
setEjbqlTranslatorFactory(EJBQLTranslatorFactory ejbqlTranslatorFactory)
Sets a translator factory for EJBQL to SQL translation.void
setPkGenerator(PkGenerator pkGenerator)
Sets new primary key generator.void
setSupportsBatchUpdates(boolean flag)
void
setSupportsGeneratedKeys(boolean flag)
void
setSupportsUniqueConstraints(boolean flag)
static String
sizeAndPrecision(DbAdapter adapter, DbAttribute column)
boolean
supportsBatchUpdates()
Returnstrue
if the target database supports batch updates.boolean
supportsCatalogsOnReverseEngineering()
Returns true.boolean
supportsGeneratedKeys()
Returns true if a target database supports key autogeneration.boolean
supportsUniqueConstraints()
Returns true.String
tableTypeForTable()
Returns the name of the table type (as returned byDatabaseMetaData.getTableTypes
) for a simple user table.String
tableTypeForView()
Returns the name of the table type (as returned byDatabaseMetaData.getTableTypes
) for a view table.boolean
typeSupportsLength(int type)
Returns true if supplied type can have a length attribute as a part of column definitionDbAdapter
unwrap()
Simply returns this, as JdbcAdapter is not a wrapper.
-
-
-
Field Detail
-
quotingStrategy
protected QuotingStrategy quotingStrategy
-
typesHandler
protected TypesHandler typesHandler
-
extendedTypes
protected ExtendedTypeMap extendedTypes
-
supportsBatchUpdates
protected boolean supportsBatchUpdates
-
supportsUniqueConstraints
protected boolean supportsUniqueConstraints
-
supportsGeneratedKeys
protected boolean supportsGeneratedKeys
-
ejbqlTranslatorFactory
protected EJBQLTranslatorFactory ejbqlTranslatorFactory
-
resourceLocator
protected ResourceLocator resourceLocator
-
caseInsensitiveCollations
protected boolean caseInsensitiveCollations
-
batchQueryBuilderFactory
protected BatchTranslatorFactory batchQueryBuilderFactory
Deprecated.since 4.0 BatchQueryBuilderfactory is attached to the DataNode.- Since:
- 3.1
-
logger
protected JdbcEventLogger logger
-
-
Constructor Detail
-
JdbcAdapter
public JdbcAdapter(RuntimeProperties runtimeProperties, List<ExtendedType> defaultExtendedTypes, List<ExtendedType> userExtendedTypes, List<ExtendedTypeFactory> extendedTypeFactories, ResourceLocator resourceLocator, ValueObjectTypeRegistry valueObjectTypeRegistry)
Creates new JdbcAdapter with a set of default parameters.
-
-
Method Detail
-
getBatchTerminator
public String getBatchTerminator()
Returns default separator - a semicolon.- Specified by:
getBatchTerminator
in interfaceDbAdapter
- Since:
- 1.0.4
-
getJdbcEventLogger
public JdbcEventLogger getJdbcEventLogger()
- Since:
- 3.1
-
findResource
protected URL findResource(String name)
Locates and returns a named adapter resource. A resource can be an XML file, etc.This implementation is based on the premise that each adapter is located in its own Java package and all resources are in the same package as well. Resource lookup is recursive, so that if DbAdapter is a subclass of another adapter, parent adapter package is searched as a failover.
- Since:
- 3.0
-
configureExtendedTypes
protected void configureExtendedTypes(ExtendedTypeMap map)
Called frominitExtendedTypes(List, List, List, ValueObjectTypeRegistry)
to load adapter-specific types into the ExtendedTypeMap right after the default types are loaded, but before the DI overrides are. This method has specific implementations in JdbcAdapter subclasses.
-
initExtendedTypes
protected void initExtendedTypes(List<ExtendedType> defaultExtendedTypes, List<ExtendedType> userExtendedTypes, List<ExtendedTypeFactory> extendedTypeFactories, ValueObjectTypeRegistry valueObjectTypeRegistry)
- Since:
- 3.1
-
createPkGenerator
protected PkGenerator createPkGenerator()
Creates and returns a primary key generator. This factory method should be overriden by JdbcAdapter subclasses to provide custom implementations of PKGenerator.
-
createEJBQLTranslatorFactory
protected EJBQLTranslatorFactory createEJBQLTranslatorFactory()
Creates and returns anEJBQLTranslatorFactory
used to generate visitors for EJBQL to SQL translations. This method should be overriden by subclasses that need to customize EJBQL generation.- Since:
- 3.0
-
getPkGenerator
public PkGenerator getPkGenerator()
Returns primary key generator associated with this DbAdapter.- Specified by:
getPkGenerator
in interfaceDbAdapter
-
setPkGenerator
public void setPkGenerator(PkGenerator pkGenerator)
Sets new primary key generator.- Since:
- 1.1
-
supportsUniqueConstraints
public boolean supportsUniqueConstraints()
Returns true.- Specified by:
supportsUniqueConstraints
in interfaceDbAdapter
- Since:
- 1.1
-
supportsCatalogsOnReverseEngineering
public boolean supportsCatalogsOnReverseEngineering()
Returns true.- Specified by:
supportsCatalogsOnReverseEngineering
in interfaceDbAdapter
- Since:
- 4.0
-
setSupportsUniqueConstraints
public void setSupportsUniqueConstraints(boolean flag)
- Since:
- 1.1
-
typeSupportsLength
public boolean typeSupportsLength(int type)
Returns true if supplied type can have a length attribute as a part of column definition- Specified by:
typeSupportsLength
in interfaceDbAdapter
- Since:
- 4.0
-
dropTableStatements
public Collection<String> dropTableStatements(DbEntity table)
Description copied from interface:DbAdapter
Returns a collection of SQL statements needed to drop a database table.- Specified by:
dropTableStatements
in interfaceDbAdapter
- Since:
- 3.0
-
createTable
public String createTable(DbEntity entity)
Returns a SQL string that can be used to create database table corresponding toent
parameter.- Specified by:
createTable
in interfaceDbAdapter
-
createTableAppendPKClause
protected void createTableAppendPKClause(StringBuffer sqlBuffer, DbEntity entity)
- Since:
- 1.2
-
createTableAppendColumn
public void createTableAppendColumn(StringBuffer sqlBuffer, DbAttribute column)
Appends SQL for column creation to CREATE TABLE buffer.- Specified by:
createTableAppendColumn
in interfaceDbAdapter
- Parameters:
sqlBuffer
- theStringBuffer
to append the column type tocolumn
- theDbAttribute
defining the column to append type for- Since:
- 1.2
-
sizeAndPrecision
public static String sizeAndPrecision(DbAdapter adapter, DbAttribute column)
-
getType
public static String getType(DbAdapter adapter, DbAttribute column)
-
createUniqueConstraint
public String createUniqueConstraint(DbEntity source, Collection<DbAttribute> columns)
Returns a DDL string to create a unique constraint over a set of columns.- Specified by:
createUniqueConstraint
in interfaceDbAdapter
- Since:
- 1.1
-
createFkConstraint
public String createFkConstraint(DbRelationship rel)
Returns a SQL string that can be used to create a foreign key constraint for the relationship.- Specified by:
createFkConstraint
in interfaceDbAdapter
-
externalTypesForJdbcType
public String[] externalTypesForJdbcType(int type)
Description copied from interface:DbAdapter
Returns an array of RDBMS types that can be used with JDBCtype
. Valid JDBC types are defined in java.sql.Types.- Specified by:
externalTypesForJdbcType
in interfaceDbAdapter
-
getExtendedTypes
public ExtendedTypeMap getExtendedTypes()
Description copied from interface:DbAdapter
Returns a map of ExtendedTypes that is used to translate values between Java and JDBC layer.- Specified by:
getExtendedTypes
in interfaceDbAdapter
-
buildAttribute
public DbAttribute buildAttribute(String name, String typeName, int type, int size, int scale, boolean allowNulls)
Description copied from interface:DbAdapter
Creates and returns a DbAttribute based on supplied parameters (usually obtained from database meta data).- Specified by:
buildAttribute
in interfaceDbAdapter
- Parameters:
name
- database column nametypeName
- database specific type name, may be used as a hint to determine the right JDBC type.type
- JDBC column typesize
- database column size (ignored if less than zero)scale
- database column scale, i.e. the number of decimal digits (ignored if less than zero)allowNulls
- database column nullable parameter
-
tableTypeForTable
public String tableTypeForTable()
Description copied from interface:DbAdapter
Returns the name of the table type (as returned byDatabaseMetaData.getTableTypes
) for a simple user table.- Specified by:
tableTypeForTable
in interfaceDbAdapter
-
tableTypeForView
public String tableTypeForView()
Description copied from interface:DbAdapter
Returns the name of the table type (as returned byDatabaseMetaData.getTableTypes
) for a view table.- Specified by:
tableTypeForView
in interfaceDbAdapter
-
getQualifierTranslator
public QualifierTranslator getQualifierTranslator(QueryAssembler queryAssembler)
Creates and returns a default implementation of a qualifier translator.- Specified by:
getQualifierTranslator
in interfaceDbAdapter
-
getAction
public SQLAction getAction(Query query, DataNode node)
Uses JdbcActionBuilder to create the right action.
-
getSelectTranslator
public SelectTranslator getSelectTranslator(SelectQuery<?> query, EntityResolver entityResolver)
Description copied from interface:DbAdapter
Returns a SelectTranslator that works with the adapter target database.- Specified by:
getSelectTranslator
in interfaceDbAdapter
-
bindParameter
public void bindParameter(PreparedStatement statement, ParameterBinding binding) throws SQLException, Exception
Description copied from interface:DbAdapter
Binds an object value to PreparedStatement's parameter.- Specified by:
bindParameter
in interfaceDbAdapter
- Throws:
SQLException
Exception
-
supportsBatchUpdates
public boolean supportsBatchUpdates()
Description copied from interface:DbAdapter
Returnstrue
if the target database supports batch updates.- Specified by:
supportsBatchUpdates
in interfaceDbAdapter
-
setSupportsBatchUpdates
public void setSupportsBatchUpdates(boolean flag)
-
supportsGeneratedKeys
public boolean supportsGeneratedKeys()
Description copied from interface:DbAdapter
Returns true if a target database supports key autogeneration. This feature also requires JDBC3-compliant driver.- Specified by:
supportsGeneratedKeys
in interfaceDbAdapter
- Since:
- 1.2
-
setSupportsGeneratedKeys
public void setSupportsGeneratedKeys(boolean flag)
- Since:
- 1.2
-
getEjbqlTranslatorFactory
public EJBQLTranslatorFactory getEjbqlTranslatorFactory()
Returns a translator factory for EJBQL to SQL translation. The factory is normally initialized in constructor by callingcreateEJBQLTranslatorFactory()
, and can be changed later by callingsetEjbqlTranslatorFactory(EJBQLTranslatorFactory)
.- Specified by:
getEjbqlTranslatorFactory
in interfaceDbAdapter
- Since:
- 3.0
-
setEjbqlTranslatorFactory
public void setEjbqlTranslatorFactory(EJBQLTranslatorFactory ejbqlTranslatorFactory)
Sets a translator factory for EJBQL to SQL translation. This property is normally initialized in constructor by callingcreateEJBQLTranslatorFactory()
, so users would only override it if they need to customize EJBQL translation.- Since:
- 3.0
-
createQuotingStrategy
protected QuotingStrategy createQuotingStrategy()
- Returns:
- Since:
- 4.0
-
getQuotingStrategy
@Deprecated public QuotingStrategy getQuotingStrategy(boolean needQuotes)
Deprecated.since 4.0 usegetQuotingStrategy()
.- Specified by:
getQuotingStrategy
in interfaceDbAdapter
- Since:
- 3.0
-
getQuotingStrategy
public QuotingStrategy getQuotingStrategy()
Description copied from interface:DbAdapter
Returns SQL identifier quoting strategy object- Specified by:
getQuotingStrategy
in interfaceDbAdapter
- Since:
- 4.0
-
getBatchQueryBuilderFactory
@Deprecated public BatchTranslatorFactory getBatchQueryBuilderFactory()
Deprecated.since 4.0 BatchQueryBuilderfactory is attached to the DataNode.- Since:
- 3.1
-
setBatchQueryBuilderFactory
@Deprecated public void setBatchQueryBuilderFactory(BatchTranslatorFactory batchQueryBuilderFactory)
Deprecated.since 4.0 BatchQueryBuilderfactory is attached to the DataNode.- Since:
- 3.1
-
-