Class TableImpl
- java.lang.Object
-
- org.datanucleus.store.rdbms.table.AbstractTable
-
- org.datanucleus.store.rdbms.table.TableImpl
-
- All Implemented Interfaces:
Table
,org.datanucleus.store.schema.table.Table
- Direct Known Subclasses:
AbstractClassTable
,JoinTable
,ProbeTable
,SchemaTable
,SequenceTable
public abstract class TableImpl extends AbstractTable
Class representing a table in a datastore (RDBMS). Provides a series of methods for validating the aspects of the table, namely- validate - Validate the table
- validateColumns - Validate the columns in the table
- validatePrimaryKey - Validate the primary key
- validateIndices - Validate the indices on the table
- validateForeignKeys - Validate all FKs for the table
- validateConstraints - Validate the indices and FKs.
-
-
Field Summary
-
Fields inherited from class org.datanucleus.store.rdbms.table.AbstractTable
columns, columnsByIdentifier, dba, existsInDatastore, identifier, state, storeMgr, TABLE_STATE_INITIALIZED, TABLE_STATE_INITIALIZED_MODIFIED, TABLE_STATE_NEW, TABLE_STATE_PK_INITIALIZED, TABLE_STATE_VALIDATED
-
-
Constructor Summary
Constructors Constructor Description TableImpl(DatastoreIdentifier name, RDBMSStoreManager storeMgr)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private boolean
createCandidateKeys(java.sql.Connection conn, java.util.Collection<java.lang.Throwable> autoCreateErrors, java.util.Map<DatastoreIdentifier,CandidateKey> actualCandidateKeysByName)
Method to create any Candidate keys on this table in the datastore, and auto create any that are missing where required.boolean
createConstraints(java.sql.Connection conn, java.util.Collection<java.lang.Throwable> autoCreateErrors, org.datanucleus.ClassLoaderResolver clr)
Method used to create all constraints for a brand new table.private boolean
createForeignKeys(java.sql.Connection conn, java.util.Collection<java.lang.Throwable> autoCreateErrors, org.datanucleus.ClassLoaderResolver clr, java.util.Map actualForeignKeysByName)
Method to create any foreign keys on this table in the datastoreprivate boolean
createIndices(java.sql.Connection conn, java.util.Collection<java.lang.Throwable> autoCreateErrors, org.datanucleus.ClassLoaderResolver clr, java.util.Map actualIndicesByName)
Method to create any indices for this tablevoid
dropConstraints(java.sql.Connection conn)
Method to drop the constraints for the table from the datastore.private java.util.Map<DatastoreIdentifier,CandidateKey>
getExistingCandidateKeys(java.sql.Connection conn)
Accessor for the candidate keys for this table.private java.util.Map<DatastoreIdentifier,ForeignKey>
getExistingForeignKeys(java.sql.Connection conn)
Accessor for the foreign keys for this table.private java.util.Map<DatastoreIdentifier,Index>
getExistingIndices(java.sql.Connection conn)
Accessor for indices on the actual table.private java.util.Map<DatastoreIdentifier,PrimaryKey>
getExistingPrimaryKeys(java.sql.Connection conn)
Accessor for the primary keys for this table in the datastore.protected java.util.List<CandidateKey>
getExpectedCandidateKeys()
Accessor for the expected candidate keys for this table in the datastore.java.util.List<ForeignKey>
getExpectedForeignKeys(org.datanucleus.ClassLoaderResolver clr)
Accessor for the expected foreign keys for this table in the datastore.protected java.util.Set<Index>
getExpectedIndices(org.datanucleus.ClassLoaderResolver clr)
Accessor for the indices for this table in the datastore.PrimaryKey
getPrimaryKey()
Accessor for the primary key for this table.protected java.util.Map<java.lang.String,java.lang.String>
getSQLAddCandidateKeyStatements(java.util.Map<DatastoreIdentifier,CandidateKey> actualCandidateKeysByName)
Get SQL statements to add expected Candidate Keys that are not yet on the table.protected java.util.Map<java.lang.String,java.lang.String>
getSQLAddFKStatements(java.util.Map actualForeignKeysByName, org.datanucleus.ClassLoaderResolver clr)
Get SQL statements to add expected Foreign Keys that are not yet at the table.protected java.util.Map<java.lang.String,java.lang.String>
getSQLCreateIndexStatements(java.util.Map actualIndicesByName, org.datanucleus.ClassLoaderResolver clr)
Accessor for the CREATE INDEX statements for this table.protected java.util.List<java.lang.String>
getSQLCreateStatements(java.util.Properties props)
Accessor for the SQL CREATE statements for this table.protected java.util.List<java.lang.String>
getSQLDropStatements()
Accessor for the DROP statements for this table.void
initializeColumnInfoForPrimaryKeyColumns(java.sql.Connection conn)
Utility to load the structure/metadata of primary key columns of the table.void
initializeColumnInfoFromDatastore(java.sql.Connection conn)
Initialize the default value for columns if null using the values from the datastore.private boolean
isIndexReallyNeeded(Index requiredIdx, java.util.Collection actualIndices)
Utility to check if an index is necessary.protected void
logMapping(java.lang.String memberName, JavaTypeMapping mapping)
Convenience logging method to output the mapping information for an element, key, value field.boolean
validate(java.sql.Connection conn, boolean validateColumnStructure, boolean autoCreate, java.util.Collection<java.lang.Throwable> autoCreateErrors)
Method to validate the table in the datastore.private boolean
validateCandidateKeys(java.sql.Connection conn, boolean autoCreate, java.util.Collection<java.lang.Throwable> autoCreateErrors)
Method to validate any Candidate keys on this table in the datastore, and auto create any that are missing where required.boolean
validateColumns(java.sql.Connection conn, boolean validateColumnStructure, boolean autoCreate, java.util.Collection<java.lang.Throwable> autoCreateErrors)
Utility to validate the columns of the table.boolean
validateConstraints(java.sql.Connection conn, boolean autoCreate, java.util.Collection<java.lang.Throwable> autoCreateErrors, org.datanucleus.ClassLoaderResolver clr)
Method to validate any constraints, and auto create them if required.private boolean
validateForeignKeys(java.sql.Connection conn, boolean autoCreate, java.util.Collection<java.lang.Throwable> autoCreateErrors, org.datanucleus.ClassLoaderResolver clr)
Method to validate any foreign keys on this table in the datastore, and auto create any that are missing where required.private boolean
validateIndices(java.sql.Connection conn, boolean autoCreate, java.util.Collection<java.lang.Throwable> autoCreateErrors, org.datanucleus.ClassLoaderResolver clr)
Method to validate any indices for this table, and auto create any missing ones where required.protected boolean
validatePrimaryKey(java.sql.Connection conn)
Utility method to validate the primary key of the table.-
Methods inherited from class org.datanucleus.store.rdbms.table.AbstractTable
addColumn, addColumnInternal, allowDDLOutput, assertIsInitialized, assertIsInitializedModified, assertIsPKInitialized, assertIsPKUninitialized, assertIsUninitialized, assertIsValidated, create, drop, equals, executeDdlStatement, executeDdlStatementList, exists, getCatalogName, getClassMetaData, getColumn, getColumnForName, getColumnForPosition, getColumns, getDatastoreIdentifierFullyQualified, getDiscriminatorMetaData, getIdentifier, getMemberColumnMappingForEmbeddedMember, getMemberColumnMappingForMember, getMemberColumnMappings, getName, getNumberOfColumns, getSchemaName, getStoreManager, getSurrogateColumn, getSurrogateMapping, getVersionMetaData, hasColumn, hasColumnName, hashCode, isInitialized, isInitializedModified, isPKInitialized, isValidated, postInitialize, preInitialize, tableExistsInDatastore, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.datanucleus.store.rdbms.table.Table
getIdMapping, getMemberMapping, initialize
-
-
-
-
Constructor Detail
-
TableImpl
public TableImpl(DatastoreIdentifier name, RDBMSStoreManager storeMgr)
Constructor.- Parameters:
name
- The name of the table (in SQL).storeMgr
- The StoreManager for this table.
-
-
Method Detail
-
getPrimaryKey
public PrimaryKey getPrimaryKey()
Accessor for the primary key for this table. Will always return a PrimaryKey but if we have defined no columns, the pk.size() will be 0.- Returns:
- The primary key.
-
validate
public boolean validate(java.sql.Connection conn, boolean validateColumnStructure, boolean autoCreate, java.util.Collection<java.lang.Throwable> autoCreateErrors) throws java.sql.SQLException
Method to validate the table in the datastore.- Parameters:
conn
- The JDBC ConnectionvalidateColumnStructure
- Whether to validate the column structure, or just the column existenceautoCreate
- Whether to update the table to fix any validation errors. Only applies to missing columns.autoCreateErrors
- Exceptions found in the "auto-create" process- Returns:
- Whether the database was modified
- Throws:
java.sql.SQLException
- Thrown when an error occurs in the JDBC calls
-
validateColumns
public boolean validateColumns(java.sql.Connection conn, boolean validateColumnStructure, boolean autoCreate, java.util.Collection<java.lang.Throwable> autoCreateErrors) throws java.sql.SQLException
Utility to validate the columns of the table. Will throw a MissingColumnException if a column is not found (and is not required to auto create it)- Parameters:
conn
- Connection to use for validationvalidateColumnStructure
- Whether to validate down to the structure of the columns, or just their existenceautoCreate
- Whether to auto create any missing columnsautoCreateErrors
- Exceptions found in the "auto-create" process- Returns:
- Whether it validates
- Throws:
java.sql.SQLException
- Thrown if an error occurs in the validation process
-
initializeColumnInfoForPrimaryKeyColumns
public void initializeColumnInfoForPrimaryKeyColumns(java.sql.Connection conn) throws java.sql.SQLException
Utility to load the structure/metadata of primary key columns of the table.- Parameters:
conn
- Connection to use for validation- Throws:
java.sql.SQLException
- Thrown if an error occurs in the initialization process
-
initializeColumnInfoFromDatastore
public void initializeColumnInfoFromDatastore(java.sql.Connection conn) throws java.sql.SQLException
Initialize the default value for columns if null using the values from the datastore.- Parameters:
conn
- The JDBC Connection- Throws:
java.sql.SQLException
- Thrown if an error occurs in the default initialisation.
-
validatePrimaryKey
protected boolean validatePrimaryKey(java.sql.Connection conn) throws java.sql.SQLException
Utility method to validate the primary key of the table. Will throw a WrongPrimaryKeyException if the PK is incorrect. TODO Add an auto_create parameter on this- Parameters:
conn
- Connection to use- Returns:
- Whether it validates
- Throws:
java.sql.SQLException
- When an error occurs in the valdiation
-
validateConstraints
public boolean validateConstraints(java.sql.Connection conn, boolean autoCreate, java.util.Collection<java.lang.Throwable> autoCreateErrors, org.datanucleus.ClassLoaderResolver clr) throws java.sql.SQLException
Method to validate any constraints, and auto create them if required.- Parameters:
conn
- The JDBC ConnectionautoCreate
- Whether to auto create the constraints if not existingautoCreateErrors
- Errors found in the "auto-create" processclr
- The ClassLoaderResolver- Returns:
- Whether the database was modified
- Throws:
java.sql.SQLException
- Thrown when an error occurs in the JDBC calls
-
createConstraints
public boolean createConstraints(java.sql.Connection conn, java.util.Collection<java.lang.Throwable> autoCreateErrors, org.datanucleus.ClassLoaderResolver clr) throws java.sql.SQLException
Method used to create all constraints for a brand new table.- Parameters:
conn
- The JDBC ConnectionautoCreateErrors
- Errors found in the "auto-create" processclr
- The ClassLoaderResolver- Returns:
- Whether the database was modified
- Throws:
java.sql.SQLException
- Thrown when an error occurs in the JDBC calls
-
validateForeignKeys
private boolean validateForeignKeys(java.sql.Connection conn, boolean autoCreate, java.util.Collection<java.lang.Throwable> autoCreateErrors, org.datanucleus.ClassLoaderResolver clr) throws java.sql.SQLException
Method to validate any foreign keys on this table in the datastore, and auto create any that are missing where required.- Parameters:
conn
- The JDBC ConnectionautoCreate
- Whether to auto create the FKs if not existingautoCreateErrors
- Errors found during the auto-create process- Returns:
- Whether the database was modified
- Throws:
java.sql.SQLException
- Thrown when an error occurs in the JDBC calls
-
createForeignKeys
private boolean createForeignKeys(java.sql.Connection conn, java.util.Collection<java.lang.Throwable> autoCreateErrors, org.datanucleus.ClassLoaderResolver clr, java.util.Map actualForeignKeysByName) throws java.sql.SQLException
Method to create any foreign keys on this table in the datastore- Parameters:
conn
- The JDBC ConnectionautoCreateErrors
- Errors found during the auto-create processactualForeignKeysByName
- the current foreign keys- Returns:
- Whether the database was modified
- Throws:
java.sql.SQLException
- Thrown when an error occurs in the JDBC calls
-
validateIndices
private boolean validateIndices(java.sql.Connection conn, boolean autoCreate, java.util.Collection<java.lang.Throwable> autoCreateErrors, org.datanucleus.ClassLoaderResolver clr) throws java.sql.SQLException
Method to validate any indices for this table, and auto create any missing ones where required.- Parameters:
conn
- The JDBC ConnectionautoCreate
- Whether to auto create any missing indicesautoCreateErrors
- Errors found during the auto-create process- Returns:
- Whether the database was changed
- Throws:
java.sql.SQLException
- Thrown when an error occurs in the JDBC calls
-
createIndices
private boolean createIndices(java.sql.Connection conn, java.util.Collection<java.lang.Throwable> autoCreateErrors, org.datanucleus.ClassLoaderResolver clr, java.util.Map actualIndicesByName) throws java.sql.SQLException
Method to create any indices for this table- Parameters:
conn
- The JDBC ConnectionautoCreateErrors
- Errors found during the auto-create processactualIndicesByName
- the actual indices by name- Returns:
- Whether the database was changed
- Throws:
java.sql.SQLException
- Thrown when an error occurs in the JDBC calls
-
validateCandidateKeys
private boolean validateCandidateKeys(java.sql.Connection conn, boolean autoCreate, java.util.Collection<java.lang.Throwable> autoCreateErrors) throws java.sql.SQLException
Method to validate any Candidate keys on this table in the datastore, and auto create any that are missing where required.- Parameters:
conn
- The JDBC ConnectionautoCreate
- Whether to auto create the Candidate Keys if not existingautoCreateErrors
- Errors found during the auto-create process- Returns:
- Whether the database was modified
- Throws:
java.sql.SQLException
- Thrown when an error occurs in the JDBC calls
-
createCandidateKeys
private boolean createCandidateKeys(java.sql.Connection conn, java.util.Collection<java.lang.Throwable> autoCreateErrors, java.util.Map<DatastoreIdentifier,CandidateKey> actualCandidateKeysByName) throws java.sql.SQLException
Method to create any Candidate keys on this table in the datastore, and auto create any that are missing where required.- Parameters:
conn
- The JDBC ConnectionautoCreateErrors
- Errors found during the auto-create process- Returns:
- Whether the database was modified
- Throws:
java.sql.SQLException
- Thrown when an error occurs in the JDBC calls
-
dropConstraints
public void dropConstraints(java.sql.Connection conn) throws java.sql.SQLException
Method to drop the constraints for the table from the datastore.- Parameters:
conn
- The JDBC Connection- Throws:
java.sql.SQLException
- Thrown when an error occurs in the JDBC call.
-
getExpectedForeignKeys
public java.util.List<ForeignKey> getExpectedForeignKeys(org.datanucleus.ClassLoaderResolver clr)
Accessor for the expected foreign keys for this table in the datastore. Currently only checks the columns for referenced tables (i.e relationships) and returns those.- Parameters:
clr
- The ClassLoaderResolver- Returns:
- List of foreign keys.
-
getExpectedCandidateKeys
protected java.util.List<CandidateKey> getExpectedCandidateKeys()
Accessor for the expected candidate keys for this table in the datastore. Currently returns an empty list.- Returns:
- List of candidate keys.
-
getExpectedIndices
protected java.util.Set<Index> getExpectedIndices(org.datanucleus.ClassLoaderResolver clr)
Accessor for the indices for this table in the datastore.- Parameters:
clr
- The ClassLoaderResolver- Returns:
- Set of indices expected.
-
getExistingPrimaryKeys
private java.util.Map<DatastoreIdentifier,PrimaryKey> getExistingPrimaryKeys(java.sql.Connection conn) throws java.sql.SQLException
Accessor for the primary keys for this table in the datastore.- Parameters:
conn
- The JDBC Connection- Returns:
- Map of primary keys
- Throws:
java.sql.SQLException
- Thrown when an error occurs in the JDBC call.
-
getExistingForeignKeys
private java.util.Map<DatastoreIdentifier,ForeignKey> getExistingForeignKeys(java.sql.Connection conn) throws java.sql.SQLException
Accessor for the foreign keys for this table.- Parameters:
conn
- The JDBC Connection- Returns:
- Map of foreign keys
- Throws:
java.sql.SQLException
- Thrown when an error occurs in the JDBC call.
-
getExistingCandidateKeys
private java.util.Map<DatastoreIdentifier,CandidateKey> getExistingCandidateKeys(java.sql.Connection conn) throws java.sql.SQLException
Accessor for the candidate keys for this table.- Parameters:
conn
- The JDBC Connection- Returns:
- Map of candidate keys
- Throws:
java.sql.SQLException
- Thrown when an error occurs in the JDBC call.
-
getExistingIndices
private java.util.Map<DatastoreIdentifier,Index> getExistingIndices(java.sql.Connection conn) throws java.sql.SQLException
Accessor for indices on the actual table.- Parameters:
conn
- The JDBC Connection- Returns:
- Map of indices (keyed by the index name)
- Throws:
java.sql.SQLException
- Thrown when an error occurs in the JDBC call.
-
getSQLCreateStatements
protected java.util.List<java.lang.String> getSQLCreateStatements(java.util.Properties props)
Accessor for the SQL CREATE statements for this table.- Specified by:
getSQLCreateStatements
in classAbstractTable
- Parameters:
props
- Properties for controlling the table creation- Returns:
- List of statements.
-
getSQLAddFKStatements
protected java.util.Map<java.lang.String,java.lang.String> getSQLAddFKStatements(java.util.Map actualForeignKeysByName, org.datanucleus.ClassLoaderResolver clr)
Get SQL statements to add expected Foreign Keys that are not yet at the table. If the returned Map is empty, the current FK setup is correct.- Parameters:
actualForeignKeysByName
- Actual Map of foreign keysclr
- The ClassLoaderResolver- Returns:
- a Map with the SQL statements
-
getSQLAddCandidateKeyStatements
protected java.util.Map<java.lang.String,java.lang.String> getSQLAddCandidateKeyStatements(java.util.Map<DatastoreIdentifier,CandidateKey> actualCandidateKeysByName)
Get SQL statements to add expected Candidate Keys that are not yet on the table. If the returned Map is empty, the current Candidate Key setup is correct.- Parameters:
actualCandidateKeysByName
- Actual Map of candidate keys- Returns:
- a Map with the SQL statements
-
isIndexReallyNeeded
private boolean isIndexReallyNeeded(Index requiredIdx, java.util.Collection actualIndices)
Utility to check if an index is necessary.- Parameters:
requiredIdx
- The indexactualIndices
- The actual indexes- Returns:
- Whether the index is needed (i.e not present in the actual indexes)
-
getSQLCreateIndexStatements
protected java.util.Map<java.lang.String,java.lang.String> getSQLCreateIndexStatements(java.util.Map actualIndicesByName, org.datanucleus.ClassLoaderResolver clr)
Accessor for the CREATE INDEX statements for this table.- Parameters:
actualIndicesByName
- Map of actual indexesclr
- The ClassLoaderResolver- Returns:
- Map of statements
-
getSQLDropStatements
protected java.util.List<java.lang.String> getSQLDropStatements()
Accessor for the DROP statements for this table.- Specified by:
getSQLDropStatements
in classAbstractTable
- Returns:
- List of statements
-
logMapping
protected void logMapping(java.lang.String memberName, JavaTypeMapping mapping)
Convenience logging method to output the mapping information for an element, key, value field.- Parameters:
memberName
- Name of the membermapping
- The mapping
-
-