|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.triactive.jdo.store.DatabaseAdapter
public class DatabaseAdapter
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 getInstance(java.sql.Connection)
method.
DatabaseMetaData
Field Summary | |
---|---|
protected int |
databaseMajorVersion
The major version number of the underlying database. |
protected int |
databaseMinorVersion
The minor version number of the underlying database. |
protected java.lang.String |
databaseProductName
The name of the underlying database. |
protected java.lang.String |
databaseProductVersion
The version number of the underlying database as a string. |
protected java.lang.String |
identifierQuoteString
The String used to quote SQL identifiers. |
protected java.util.HashSet |
keywords
The set of SQL keywords for this DBMS, in upper-case. |
protected int |
maxColumnNameLength
The maximum length to be used for a column name. |
protected int |
maxConstraintNameLength
The maximum length to be used for a table constraint name. |
protected int |
maxIndexNameLength
The maximum length to be used for an index name. |
protected int |
maxTableNameLength
The maximum length to be used for a table name. |
protected boolean |
storesLowerCaseIdentifiers
true if the database stores all identifiers in lower-case. |
protected boolean |
storesUpperCaseIdentifiers
true if the database stores all identifiers in upper-case. |
protected java.util.HashMap |
typeMappings
|
protected java.util.HashMap |
typesByTypeNumber
|
Constructor Summary | |
---|---|
protected |
DatabaseAdapter(java.sql.DatabaseMetaData metadata)
Constructs a database adapter based on the given JDBC metadata. |
Method Summary | |
---|---|
void |
closeConnection(java.sql.Connection conn)
|
boolean |
createIndexesBeforeForeignKeys()
|
protected void |
createTypeInfo(java.sql.DatabaseMetaData metadata)
Creates TypeInfo objects for all of the data types and indexes them in the typesByTypeNumber map by their JDBC data type number. |
java.lang.String |
getAddCandidateKeyStatement(SQLIdentifier ckName,
CandidateKey ck)
Returns the appropriate SQL to add a candidate key to its table. |
java.lang.String |
getAddForeignKeyStatement(SQLIdentifier fkName,
ForeignKey fk)
Returns the appropriate SQL to add a foreign key to its table. |
java.lang.String |
getAddPrimaryKeyStatement(SQLIdentifier pkName,
PrimaryKey pk)
Returns the appropriate SQL to add a primary key to its table. |
java.sql.Connection |
getConnection(javax.sql.DataSource ds,
java.lang.String userName,
java.lang.String password,
int isolationLevel)
|
java.lang.String |
getCreateIndexStatement(SQLIdentifier idxName,
Index idx)
Returns the appropriate SQL to add an index to its table. |
java.lang.String |
getCreateTableStatement(BaseTable table,
Column[] columns)
Returns the appropriate SQL to create the given table having the given columns. |
java.lang.String |
getDropTableStatement(BaseTable table)
Returns the appropriate SQL to drop the given table. |
java.lang.String |
getDropViewStatement(View view)
Returns the appropriate SQL to drop the given view. |
java.lang.String |
getIdentifierQuoteString()
|
static DatabaseAdapter |
getInstance(java.sql.Connection conn)
Returns a DatabaseAdapter object appropriate for the database currently underlying the given Connection . |
Mapping |
getMapping(java.lang.Class c)
|
Mapping |
getMapping(ClassBaseTable table,
int relativeFieldNumber)
|
ColumnMapping |
getMapping(Column col)
|
protected java.lang.Class |
getMappingClass(java.lang.Class c)
|
int |
getMaxColumnNameLength()
|
int |
getMaxConstraintNameLength()
|
int |
getMaxIndexNameLength()
|
int |
getMaxTableNameLength()
|
java.lang.String |
getSchemaName(java.sql.Connection conn)
|
SQLState |
getSQLState(java.sql.SQLException se)
Returns a SQLState object for the specified SQLException, if one is present and valid. |
TypeInfo |
getTypeInfo(int dataType)
Returns type information for the database type that best implements the given JDBC type. |
TypeInfo |
getTypeInfo(int[] candidateDataTypes)
Returns type information for the first one of the given candidate JDBC data types supported by this database. |
int |
getUnlimitedLengthPrecisionValue(TypeInfo typeInfo)
Returns the precision value to be used when creating string columns of "unlimited" length. |
java.lang.String |
getVendorID()
|
boolean |
includeOrderByColumnsInSelect()
|
boolean |
isEmbeddedType(java.lang.Class c)
|
boolean |
isSQLKeyword(java.lang.String word)
Tests if a given string is a SQL key word. |
NumericExpression |
lengthMethod(CharacterExpression str)
Returns the appropriate SQL expression for the JDOQL String.length() method. |
ColumnInfo |
newColumnInfo(java.sql.ResultSet rs)
A factory for ColumnInfo objects. |
javax.jdo.JDOException |
newDataStoreException(java.lang.String message,
java.sql.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. |
ForeignKeyInfo |
newForeignKeyInfo(java.sql.ResultSet rs)
A factory for ForeignKeyInfo objects. |
QueryStatement |
newQueryStatement(Table table)
|
QueryStatement |
newQueryStatement(Table table,
SQLIdentifier rangeVar)
|
TableExpression |
newTableExpression(QueryStatement qs,
Table table,
SQLIdentifier rangeVar)
Returns a new TableExpression object appropriate for this DBMS. |
protected TypeInfo |
newTypeInfo(java.sql.ResultSet rs)
A factory for TypeInfo objects. |
protected java.util.Set |
parseKeywordList(java.lang.String list)
|
boolean |
storesLowerCaseIdentifiers()
|
boolean |
storesUpperCaseIdentifiers()
|
CharacterExpression |
substringMethod(CharacterExpression str,
NumericExpression begin)
Returns the appropriate SQL expression for the JDOQL String.substring(str,begin) method. |
CharacterExpression |
substringMethod(CharacterExpression str,
NumericExpression begin,
NumericExpression end)
Returns the appropriate SQL expression for the JDOQL String.substring(str,begin,end) method. |
boolean |
supportsAlterTableDropConstraint()
|
boolean |
supportsBooleanComparison()
|
boolean |
supportsDeferredConstraints()
|
boolean |
supportsNullsInCandidateKeys()
|
java.lang.String |
toString()
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
protected java.lang.String databaseProductName
protected java.lang.String databaseProductVersion
protected int databaseMajorVersion
protected int databaseMinorVersion
protected int maxTableNameLength
protected int maxConstraintNameLength
protected int maxIndexNameLength
protected int maxColumnNameLength
protected boolean storesLowerCaseIdentifiers
protected boolean storesUpperCaseIdentifiers
protected java.lang.String identifierQuoteString
protected final java.util.HashSet keywords
protected final java.util.HashMap typesByTypeNumber
protected final java.util.HashMap typeMappings
Constructor Detail |
---|
protected DatabaseAdapter(java.sql.DatabaseMetaData metadata)
metadata
- the database metadata.Method Detail |
---|
public static DatabaseAdapter getInstance(java.sql.Connection conn) throws java.sql.SQLException
Connection
. Multiple calls to
this method with connections having the same database/driver/version will
return the same DatabaseAdapter object.
conn
- An open database connection.
java.sql.SQLException
- If a database error occurs.public java.lang.String getVendorID()
public int getMaxTableNameLength()
public int getMaxConstraintNameLength()
public int getMaxIndexNameLength()
public int getMaxColumnNameLength()
public boolean storesLowerCaseIdentifiers()
public boolean storesUpperCaseIdentifiers()
public SQLState getSQLState(java.sql.SQLException se)
se
- A caught SQL exception.
null
if se does not
contain a valid 5-character SQLSTATE.public javax.jdo.JDOException newDataStoreException(java.lang.String message, java.sql.SQLException e)
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.
message
- The message to include in the JDODataStoreException.e
- The SQLException to create a JDODataStoreException for.
JDODataStoreException
or
JDOFatalDataStoreException
that wraps the
given SQLException
. A fatal exception is used
to indicate that the active transaction has been aborted.protected void createTypeInfo(java.sql.DatabaseMetaData metadata) throws java.sql.SQLException
java.sql.SQLException
protected TypeInfo newTypeInfo(java.sql.ResultSet rs)
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()
.
rs
- The result set returned from DatabaseMetaData.getTypeInfo().
null
if the type indicated by this row should be
excluded from use.public ColumnInfo newColumnInfo(java.sql.ResultSet rs)
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()
.
rs
- The result set returned from DatabaseMetaData.getColumns().public ForeignKeyInfo newForeignKeyInfo(java.sql.ResultSet rs)
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()
.
rs
- The result set returned from DatabaseMetaData.get??portedKeys().protected java.util.Set parseKeywordList(java.lang.String list)
public boolean isSQLKeyword(java.lang.String 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.
word
- The word to test.
true
if word is a SQL key word for this
DBMS. The comparison is case-insensitive.SQL92Constants
public TypeInfo getTypeInfo(int dataType) throws UnsupportedDataTypeException
dataType
- JDBC type number of the data type.
UnsupportedDataTypeException
public TypeInfo getTypeInfo(int[] candidateDataTypes) throws UnsupportedDataTypeException
candidateDataTypes
- array of JDBC type numbers of the data types to be checked
in order of preference.
UnsupportedDataTypeException
public int getUnlimitedLengthPrecisionValue(TypeInfo typeInfo)
TypeInfo.precision
). However, for some
types in some databases the value must be computed specially.
typeInfo
- the typeInfo object for which the precision value is
needed.
public boolean isEmbeddedType(java.lang.Class c)
public Mapping getMapping(java.lang.Class c)
public ColumnMapping getMapping(Column col)
public Mapping getMapping(ClassBaseTable table, int relativeFieldNumber)
protected java.lang.Class getMappingClass(java.lang.Class c)
public java.sql.Connection getConnection(javax.sql.DataSource ds, java.lang.String userName, java.lang.String password, int isolationLevel) throws java.sql.SQLException
java.sql.SQLException
public void closeConnection(java.sql.Connection conn) throws java.sql.SQLException
java.sql.SQLException
public java.lang.String getSchemaName(java.sql.Connection conn) throws java.sql.SQLException
java.sql.SQLException
public java.lang.String getIdentifierQuoteString()
public boolean createIndexesBeforeForeignKeys()
public boolean includeOrderByColumnsInSelect()
public boolean supportsAlterTableDropConstraint()
public boolean supportsDeferredConstraints()
public boolean supportsBooleanComparison()
public boolean supportsNullsInCandidateKeys()
public QueryStatement newQueryStatement(Table table)
public QueryStatement newQueryStatement(Table table, SQLIdentifier rangeVar)
public TableExpression newTableExpression(QueryStatement qs, Table table, SQLIdentifier rangeVar)
qs
- The query statement in which the table expression will
be included.table
- The main table in the expression.rangeVar
- The SQL alias, or "range variable", to assign to the
expression or to the main table.public java.lang.String getCreateTableStatement(BaseTable table, Column[] columns)
CREATE TABLE FOO ( BAR VARCHAR(30), BAZ INTEGER )
table
- The table to create.columns
- The columns of the table.
public java.lang.String getAddPrimaryKeyStatement(SQLIdentifier pkName, PrimaryKey pk)
ALTER TABLE FOO ADD CONSTRAINT FOO_PK PRIMARY KEY (BAR)
pkName
- The name of the primary key to add.pk
- An object describing the primary key.
public java.lang.String getAddCandidateKeyStatement(SQLIdentifier ckName, CandidateKey ck)
ALTER TABLE FOO ADD CONSTRAINT FOO_CK UNIQUE (BAZ)
ckName
- The name of the candidate key to add.ck
- An object describing the candidate key.
public java.lang.String getAddForeignKeyStatement(SQLIdentifier fkName, ForeignKey fk)
ALTER TABLE FOO ADD CONSTRAINT FOO_FK1 FOREIGN KEY (BAR, BAZ) REFERENCES ABC (COL1, COL2)
fkName
- The name of the foreign key to add.fk
- An object describing the foreign key.
public java.lang.String getCreateIndexStatement(SQLIdentifier idxName, Index idx)
CREATE INDEX FOO_N1 ON FOO (BAR,BAZ) CREATE UNIQUE INDEX FOO_U1 ON FOO (BAR,BAZ)
idxName
- The name of the index to add.idx
- An object describing the index.
public java.lang.String getDropTableStatement(BaseTable table)
DROP TABLE FOO CASCADE
table
- The table to drop.
public java.lang.String getDropViewStatement(View view)
DROP VIEW FOO
view
- The view to drop.
public NumericExpression lengthMethod(CharacterExpression str)
CHAR_LENGTH(str)
str
- The argument to the length() method.
public CharacterExpression substringMethod(CharacterExpression str, NumericExpression begin)
Note that the value of begin is base 0 (Java-style), while most SQL string functions use base 1.SUBSTRING(str FROM begin)
str
- The first argument to the substring() method.begin
- The second argument to the substring() method.
public CharacterExpression substringMethod(CharacterExpression str, NumericExpression begin, NumericExpression end)
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.SUBSTRING(str FROM begin FOR len)
str
- The first argument to the substring() method.begin
- The second argument to the substring() method.end
- The third argument to the substring() method.
public java.lang.String toString()
toString
in class java.lang.Object
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |