| java.lang.Object com.triactive.jdo.store.DatabaseAdapter
All known Subclasses: com.triactive.jdo.store.HSQLDBAdapter, com.triactive.jdo.store.FirebirdAdapter, com.triactive.jdo.store.MSSQLServerAdapter, com.triactive.jdo.store.SAPDBAdapter, com.triactive.jdo.store.PointBaseAdapter, com.triactive.jdo.store.OracleAdapter, com.triactive.jdo.store.CloudscapeAdapter, com.triactive.jdo.store.MySQLAdapter, com.triactive.jdo.store.DB2Adapter, com.triactive.jdo.store.PostgreSQLAdapter,
DatabaseAdapter | public class DatabaseAdapter (Code) | | Provides methods for adapting SQL language elements to a specific vendor's
database. A database adapter is primarily used to map generic JDBC data
types and SQL identifiers to specific types/identifiers suitable for the
database in use.
Each database adapter corresponds to a particular combination of database,
database version, driver, and driver version, as provided by the driver's
own metadata. Database adapters cannot be constructed directly, but must be
obtained using the
DatabaseAdapter.getInstance method.
author: Mike Martin author: Christopher Walk version: $Revision: 1.30 $ See Also: java.sql.DatabaseMetaData |
Method Summary | |
public void | closeConnection(Connection conn) | public boolean | createIndexesBeforeForeignKeys() | protected void | createTypeInfo(DatabaseMetaData metadata) Creates TypeInfo objects for all of the data types and indexes them
in the typesByTypeNumber map by their JDBC data type number. | public String | getAddCandidateKeyStatement(SQLIdentifier ckName, CandidateKey ck) Returns the appropriate SQL to add a candidate key to its table.
It should return something like:
ALTER TABLE FOO ADD CONSTRAINT FOO_CK UNIQUE (BAZ)
Parameters: ckName - The name of the candidate key to add. Parameters: ck - An object describing the candidate key. | public String | getAddForeignKeyStatement(SQLIdentifier fkName, ForeignKey fk) Returns the appropriate SQL to add a foreign key to its table.
It should return something like:
ALTER TABLE FOO ADD CONSTRAINT FOO_FK1 FOREIGN KEY (BAR, BAZ) REFERENCES ABC (COL1, COL2)
Parameters: fkName - The name of the foreign key to add. Parameters: fk - An object describing the foreign key. | public String | getAddPrimaryKeyStatement(SQLIdentifier pkName, PrimaryKey pk) Returns the appropriate SQL to add a primary key to its table.
It should return something like:
ALTER TABLE FOO ADD CONSTRAINT FOO_PK PRIMARY KEY (BAR)
Parameters: pkName - The name of the primary key to add. Parameters: pk - An object describing the primary key. | public Connection | getConnection(DataSource ds, String userName, String password, int isolationLevel) | public String | getCreateIndexStatement(SQLIdentifier idxName, Index idx) Returns the appropriate SQL to add an index to its table.
It should return something like:
CREATE INDEX FOO_N1 ON FOO (BAR,BAZ)
CREATE UNIQUE INDEX FOO_U1 ON FOO (BAR,BAZ)
Parameters: idxName - The name of the index to add. Parameters: idx - An object describing the index. | public String | getCreateTableStatement(BaseTable table, Column[] columns) Returns the appropriate SQL to create the given table having the given
columns. | public String | getDropTableStatement(BaseTable table) Returns the appropriate SQL to drop the given table.
It should return something like:
DROP TABLE FOO CASCADE
Parameters: table - The table to drop. | public String | getDropViewStatement(View view) Returns the appropriate SQL to drop the given view.
It should return something like:
DROP VIEW FOO
Parameters: view - The view to drop. | public String | getIdentifierQuoteString() | public static synchronized DatabaseAdapter | getInstance(Connection conn) Returns a DatabaseAdapter object appropriate for the database
currently underlying the given
Connection . | public Mapping | getMapping(Class c) | public ColumnMapping | getMapping(Column col) | public Mapping | getMapping(ClassBaseTable table, int relativeFieldNumber) | protected Class | getMappingClass(Class c) | public int | getMaxColumnNameLength() | public int | getMaxConstraintNameLength() | public int | getMaxIndexNameLength() | public int | getMaxTableNameLength() | public SQLState | getSQLState(SQLException se) Returns a SQLState object for the specified SQLException, if one is
present and valid. | public String | getSchemaName(Connection conn) | public TypeInfo | getTypeInfo(int dataType) Returns type information for the database type that best implements the
given JDBC type.
Parameters: dataType - JDBC type number of the data type. | public TypeInfo | getTypeInfo(int[] candidateDataTypes) Returns type information for the first one of the given candidate JDBC
data types supported by this database.
Parameters: candidateDataTypes - array of JDBC type numbers of the data types to be checkedin order of preference. | public int | getUnlimitedLengthPrecisionValue(TypeInfo typeInfo) Returns the precision value to be used when creating string columns of
"unlimited" length. | public String | getVendorID() | public boolean | includeOrderByColumnsInSelect() | public boolean | isEmbeddedType(Class c) | public boolean | isSQLKeyword(String word) Tests if a given string is a SQL key word.
The list of key words tested against is defined to contain all SQL/92
key words, plus any additional key words reported by the JDBC driver
for this adapter via DatabaseMetaData.getSQLKeywords() .
In general, use of a SQL key word as an identifier should be avoided.
SQL/92 key words are divided into reserved and non-reserved words.
If a reserved word is used as an identifier it must be quoted with double
quotes.
Strictly speaking, the same is not true of non-reserved words.
However, as C.J. | public NumericExpression | lengthMethod(CharacterExpression str) Returns the appropriate SQL expression for the JDOQL String.length()
method.
It should return something like:
CHAR_LENGTH(str)
Parameters: str - The argument to the length() method. | public ColumnInfo | newColumnInfo(ResultSet rs) A factory for ColumnInfo objects. | public JDOException | newDataStoreException(String message, SQLException e) Create the appropriate JDODataStoreException or
JDOFatalDataStoreException for the given
SQLException based on whether the action causing
the exception has aborted the current transaction.
For historical reasons, the design of this method is flawed.
To conform correctly to the spec, if a JDOFatalDataStoreException is
returned then there should be some coordination with the appropriate
PersistenceManager and its Transaction to allow them to reflect the fact
that a transaction is no longer active.
At the least, that means that this method would have to be passed a
reference to a PersistenceManager.
An outstanding question remains how we can reliably determine via JDBC
whether or not a failed statement has aborted the current database
transaction.
Bottom line, this area is ripe for refactoring.
The current implementation in this class always returns a new
JDODataStoreException and never a JDOFatalDataStoreException.
Parameters: message - The message to include in the JDODataStoreException. Parameters: e - The SQLException to create a JDODataStoreException for. | public ForeignKeyInfo | newForeignKeyInfo(ResultSet rs) A factory for ForeignKeyInfo objects. | public QueryStatement | newQueryStatement(Table table) | public QueryStatement | newQueryStatement(Table table, SQLIdentifier rangeVar) | public TableExpression | newTableExpression(QueryStatement qs, Table table, SQLIdentifier rangeVar) Returns a new TableExpression object appropriate for this DBMS.
This should be an instance of one of the three built-in styles of table
expression:
- TableExprAsJoins
- TableExprAsSubjoins
- TableExprAsSubquery
TableExprAsSubjoins is the default, which arguably produces the most
readable SQL but doesn't work on all DBMS's. | protected TypeInfo | newTypeInfo(ResultSet rs) A factory for TypeInfo objects. | protected Set | parseKeywordList(String list) | public boolean | storesLowerCaseIdentifiers() | public boolean | storesUpperCaseIdentifiers() | public CharacterExpression | substringMethod(CharacterExpression str, NumericExpression begin) Returns the appropriate SQL expression for the JDOQL
String.substring(str,begin) method.
It should return something like:
SUBSTRING(str FROM begin)
Note that the value of begin is base 0 (Java-style), while most
SQL string functions use base 1.
Parameters: str - The first argument to the substring() method. Parameters: begin - The second argument to the substring() method. | public CharacterExpression | substringMethod(CharacterExpression str, NumericExpression begin, NumericExpression end) Returns the appropriate SQL expression for the JDOQL
String.substring(str,begin,end) method.
It should return something like:
SUBSTRING(str FROM begin FOR len)
Note that the value of begin is base 0 (Java-style), while most
SQL string functions use base 1.
Note also that an end position is given, while most SQL substring
functions take a length.
Parameters: str - The first argument to the substring() method. Parameters: begin - The second argument to the substring() method. Parameters: end - The third argument to the substring() method. | public boolean | supportsAlterTableDropConstraint() | public boolean | supportsBooleanComparison() Indicates whether or not two boolean expressions can be directly compared
to each other using the = or <> operator. | public boolean | supportsDeferredConstraints() | public boolean | supportsNullsInCandidateKeys() | public String | toString() |
databaseMajorVersion | protected int databaseMajorVersion(Code) | | The major version number of the underlying database.
|
databaseMinorVersion | protected int databaseMinorVersion(Code) | | The minor version number of the underlying database.
|
databaseProductName | protected String databaseProductName(Code) | | The name of the underlying database.
|
databaseProductVersion | protected String databaseProductVersion(Code) | | The version number of the underlying database as a string.
|
identifierQuoteString | protected String identifierQuoteString(Code) | | The String used to quote SQL identifiers.
|
keywords | final protected HashSet keywords(Code) | | The set of SQL keywords for this DBMS, in upper-case.
|
maxColumnNameLength | protected int maxColumnNameLength(Code) | | The maximum length to be used for a column name.
|
maxConstraintNameLength | protected int maxConstraintNameLength(Code) | | The maximum length to be used for a table constraint name.
|
maxIndexNameLength | protected int maxIndexNameLength(Code) | | The maximum length to be used for an index name.
|
maxTableNameLength | protected int maxTableNameLength(Code) | | The maximum length to be used for a table name.
|
storesLowerCaseIdentifiers | protected boolean storesLowerCaseIdentifiers(Code) | | true if the database stores all identifiers in lower-case.
|
storesUpperCaseIdentifiers | protected boolean storesUpperCaseIdentifiers(Code) | | true if the database stores all identifiers in upper-case.
|
DatabaseAdapter | protected DatabaseAdapter(DatabaseMetaData metadata)(Code) | | Constructs a database adapter based on the given JDBC metadata.
Parameters: metadata - the database metadata. |
createIndexesBeforeForeignKeys | public boolean createIndexesBeforeForeignKeys()(Code) | | |
createTypeInfo | protected void createTypeInfo(DatabaseMetaData metadata) throws SQLException(Code) | | Creates TypeInfo objects for all of the data types and indexes them
in the typesByTypeNumber map by their JDBC data type number.
|
getAddCandidateKeyStatement | public String getAddCandidateKeyStatement(SQLIdentifier ckName, CandidateKey ck)(Code) | | Returns the appropriate SQL to add a candidate key to its table.
It should return something like:
ALTER TABLE FOO ADD CONSTRAINT FOO_CK UNIQUE (BAZ)
Parameters: ckName - The name of the candidate key to add. Parameters: ck - An object describing the candidate key. The text of the SQL statement. |
getAddForeignKeyStatement | public String getAddForeignKeyStatement(SQLIdentifier fkName, ForeignKey fk)(Code) | | Returns the appropriate SQL to add a foreign key to its table.
It should return something like:
ALTER TABLE FOO ADD CONSTRAINT FOO_FK1 FOREIGN KEY (BAR, BAZ) REFERENCES ABC (COL1, COL2)
Parameters: fkName - The name of the foreign key to add. Parameters: fk - An object describing the foreign key. The text of the SQL statement. |
getAddPrimaryKeyStatement | public String getAddPrimaryKeyStatement(SQLIdentifier pkName, PrimaryKey pk)(Code) | | Returns the appropriate SQL to add a primary key to its table.
It should return something like:
ALTER TABLE FOO ADD CONSTRAINT FOO_PK PRIMARY KEY (BAR)
Parameters: pkName - The name of the primary key to add. Parameters: pk - An object describing the primary key. The text of the SQL statement. |
getCreateIndexStatement | public String getCreateIndexStatement(SQLIdentifier idxName, Index idx)(Code) | | Returns the appropriate SQL to add an index to its table.
It should return something like:
CREATE INDEX FOO_N1 ON FOO (BAR,BAZ)
CREATE UNIQUE INDEX FOO_U1 ON FOO (BAR,BAZ)
Parameters: idxName - The name of the index to add. Parameters: idx - An object describing the index. The text of the SQL statement. |
getCreateTableStatement | public String getCreateTableStatement(BaseTable table, Column[] columns)(Code) | | Returns the appropriate SQL to create the given table having the given
columns. No column constraints or key definitions should be included.
It should return something like:
CREATE TABLE FOO ( BAR VARCHAR(30), BAZ INTEGER )
Parameters: table - The table to create. Parameters: columns - The columns of the table. The text of the SQL statement. |
getDropTableStatement | public String getDropTableStatement(BaseTable table)(Code) | | Returns the appropriate SQL to drop the given table.
It should return something like:
DROP TABLE FOO CASCADE
Parameters: table - The table to drop. The text of the SQL statement. |
getDropViewStatement | public String getDropViewStatement(View view)(Code) | | Returns the appropriate SQL to drop the given view.
It should return something like:
DROP VIEW FOO
Parameters: view - The view to drop. The text of the SQL statement. |
getIdentifierQuoteString | public String getIdentifierQuoteString()(Code) | | |
getInstance | public static synchronized DatabaseAdapter getInstance(Connection conn) throws SQLException(Code) | | Returns a DatabaseAdapter object appropriate for the database
currently underlying the given
Connection . Multiple calls to
this method with connections having the same database/driver/version will
return the same DatabaseAdapter object.
Parameters: conn - An open database connection. a database adapter object for the database underlying thegiven connection. exception: SQLException - If a database error occurs. |
getMaxColumnNameLength | public int getMaxColumnNameLength()(Code) | | |
getMaxConstraintNameLength | public int getMaxConstraintNameLength()(Code) | | |
getMaxIndexNameLength | public int getMaxIndexNameLength()(Code) | | |
getMaxTableNameLength | public int getMaxTableNameLength()(Code) | | |
getSQLState | public SQLState getSQLState(SQLException se)(Code) | | Returns a SQLState object for the specified SQLException, if one is
present and valid.
Parameters: se - A caught SQL exception.A SQLState object, or null if se does notcontain a valid 5-character SQLSTATE. |
getTypeInfo | public TypeInfo getTypeInfo(int dataType) throws UnsupportedDataTypeException(Code) | | Returns type information for the database type that best implements the
given JDBC type.
Parameters: dataType - JDBC type number of the data type. type information for the best matching type. |
getTypeInfo | public TypeInfo getTypeInfo(int[] candidateDataTypes) throws UnsupportedDataTypeException(Code) | | Returns type information for the first one of the given candidate JDBC
data types supported by this database.
Parameters: candidateDataTypes - array of JDBC type numbers of the data types to be checkedin order of preference. type information for the first supported type. |
getUnlimitedLengthPrecisionValue | public int getUnlimitedLengthPrecisionValue(TypeInfo typeInfo)(Code) | | Returns the precision value to be used when creating string columns of
"unlimited" length. Usually, if this value is needed it is provided in
the database metadata (
TypeInfo.precision ). However, for some
types in some databases the value must be computed specially.
Parameters: typeInfo - the typeInfo object for which the precision value isneeded. the precision value to be used when creating the column, or -1if no value should be used. |
includeOrderByColumnsInSelect | public boolean includeOrderByColumnsInSelect()(Code) | | |
isEmbeddedType | public boolean isEmbeddedType(Class c)(Code) | | |
isSQLKeyword | public boolean isSQLKeyword(String word)(Code) | | Tests if a given string is a SQL key word.
The list of key words tested against is defined to contain all SQL/92
key words, plus any additional key words reported by the JDBC driver
for this adapter via DatabaseMetaData.getSQLKeywords() .
In general, use of a SQL key word as an identifier should be avoided.
SQL/92 key words are divided into reserved and non-reserved words.
If a reserved word is used as an identifier it must be quoted with double
quotes.
Strictly speaking, the same is not true of non-reserved words.
However, as C.J. Date writes in A Guide To The SQL Standard:
The rule by which it is determined within the standard that one key word
needs to be reserved while another need not is not clear to this writer.
In practice, it is probably wise to treat all key words as reserved.
Parameters: word - The word to test. true if word is a SQL key word for thisDBMS. The comparison is case-insensitive. See Also: SQL92Constants |
lengthMethod | public NumericExpression lengthMethod(CharacterExpression str)(Code) | | Returns the appropriate SQL expression for the JDOQL String.length()
method.
It should return something like:
CHAR_LENGTH(str)
Parameters: str - The argument to the length() method. The text of the SQL expression. |
newColumnInfo | public ColumnInfo newColumnInfo(ResultSet rs)(Code) | | A factory for ColumnInfo objects. This method should always be used
instead of directly constructing ColumnInfo objects in order to give the
DatabaseAdapter an opportunity to modify and/or correct the metadata
obtained from the JDBC driver.
The column information object is constructed from the current row of the
given result set. The
ResultSet object passed must have been
obtained from a call to DatabaseMetaData.getColumns().
The constructor only retrieves the values from the current row; the
caller is required to advance to the next row with
ResultSet.next .
Parameters: rs - The result set returned from DatabaseMetaData.getColumns(). |
newDataStoreException | public JDOException newDataStoreException(String message, SQLException e)(Code) | | Create the appropriate JDODataStoreException or
JDOFatalDataStoreException for the given
SQLException based on whether the action causing
the exception has aborted the current transaction.
For historical reasons, the design of this method is flawed.
To conform correctly to the spec, if a JDOFatalDataStoreException is
returned then there should be some coordination with the appropriate
PersistenceManager and its Transaction to allow them to reflect the fact
that a transaction is no longer active.
At the least, that means that this method would have to be passed a
reference to a PersistenceManager.
An outstanding question remains how we can reliably determine via JDBC
whether or not a failed statement has aborted the current database
transaction.
Bottom line, this area is ripe for refactoring.
The current implementation in this class always returns a new
JDODataStoreException and never a JDOFatalDataStoreException.
Parameters: message - The message to include in the JDODataStoreException. Parameters: e - The SQLException to create a JDODataStoreException for. A JDODataStoreException orJDOFatalDataStoreException that wraps thegiven SQLException . A fatal exception is usedto indicate that the active transaction has been aborted. |
newForeignKeyInfo | public ForeignKeyInfo newForeignKeyInfo(ResultSet rs)(Code) | | A factory for ForeignKeyInfo objects. This method should always be used
instead of directly constructing ForeignKeyInfo objects in order to give
the DatabaseAdapter an opportunity to modify and/or correct the metadata
obtained from the JDBC driver.
The column information object is constructed from the current row of the
given result set. The
ResultSet object passed must have been
obtained from a call to DatabaseMetaData.getImportedKeys() or
DatabaseMetaData.getExportedKeys().
The constructor only retrieves the values from the current row; the
caller is required to advance to the next row with
ResultSet.next .
Parameters: rs - The result set returned from DatabaseMetaData.get??portedKeys(). |
newTableExpression | public TableExpression newTableExpression(QueryStatement qs, Table table, SQLIdentifier rangeVar)(Code) | | Returns a new TableExpression object appropriate for this DBMS.
This should be an instance of one of the three built-in styles of table
expression:
- TableExprAsJoins
- TableExprAsSubjoins
- TableExprAsSubquery
TableExprAsSubjoins is the default, which arguably produces the most
readable SQL but doesn't work on all DBMS's. TableExprAsSubjoins
should work anywhere, but may be less efficient.
Parameters: qs - The query statement in which the table expression willbe included. Parameters: table - The main table in the expression. Parameters: rangeVar - The SQL alias, or "range variable", to assign to theexpression or to the main table. |
newTypeInfo | protected TypeInfo newTypeInfo(ResultSet rs)(Code) | | A factory for TypeInfo objects. This method should always be used
instead of directly constructing TypeInfo objects in order to give the
DatabaseAdapter an opportunity to modify and/or correct the metadata
obtained from the JDBC driver.
The type information object is constructed from the current row of the
given result set. The
ResultSet object passed must have been
obtained from a call to DatabaseMetaData.getTypeInfo().
The constructor only retrieves the values from the current row; the
caller is required to advance to the next row with
ResultSet.next .
Parameters: rs - The result set returned from DatabaseMetaData.getTypeInfo().A TypeInfo object constructed from the current result set row, ornull if the type indicated by this row should beexcluded from use. |
storesLowerCaseIdentifiers | public boolean storesLowerCaseIdentifiers()(Code) | | |
storesUpperCaseIdentifiers | public boolean storesUpperCaseIdentifiers()(Code) | | |
substringMethod | public CharacterExpression substringMethod(CharacterExpression str, NumericExpression begin)(Code) | | Returns the appropriate SQL expression for the JDOQL
String.substring(str,begin) method.
It should return something like:
SUBSTRING(str FROM begin)
Note that the value of begin is base 0 (Java-style), while most
SQL string functions use base 1.
Parameters: str - The first argument to the substring() method. Parameters: begin - The second argument to the substring() method. The text of the SQL expression. |
substringMethod | public CharacterExpression substringMethod(CharacterExpression str, NumericExpression begin, NumericExpression end)(Code) | | Returns the appropriate SQL expression for the JDOQL
String.substring(str,begin,end) method.
It should return something like:
SUBSTRING(str FROM begin FOR len)
Note that the value of begin is base 0 (Java-style), while most
SQL string functions use base 1.
Note also that an end position is given, while most SQL substring
functions take a length.
Parameters: str - The first argument to the substring() method. Parameters: begin - The second argument to the substring() method. Parameters: end - The third argument to the substring() method. The text of the SQL expression. |
supportsAlterTableDropConstraint | public boolean supportsAlterTableDropConstraint()(Code) | | |
supportsBooleanComparison | public boolean supportsBooleanComparison()(Code) | | Indicates whether or not two boolean expressions can be directly compared
to each other using the = or <> operator.
Some DBMS's allow you to say WHERE (X = 12) = (Y = 34), others don't.
The default value returned by the implementation in this class is
true .
true if boolean expressions can be compared,false otherwise. |
supportsDeferredConstraints | public boolean supportsDeferredConstraints()(Code) | | |
supportsNullsInCandidateKeys | public boolean supportsNullsInCandidateKeys()(Code) | | |
|
|