Class 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.
    • 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​(Connection conn,
                                boolean validateColumnStructure,
                                boolean autoCreate,
                                Collection<Throwable> autoCreateErrors)
                         throws SQLException
        Method to validate the table in the datastore.
        Parameters:
        conn - The JDBC Connection
        validateColumnStructure - Whether to validate the column structure, or just the column existence
        autoCreate - 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:
        SQLException - Thrown when an error occurs in the JDBC calls
      • validateColumns

        public boolean validateColumns​(Connection conn,
                                       boolean validateColumnStructure,
                                       boolean autoCreate,
                                       Collection autoCreateErrors)
                                throws 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 validation
        validateColumnStructure - Whether to validate down to the structure of the columns, or just their existence
        autoCreate - Whether to auto create any missing columns
        autoCreateErrors - Exceptions found in the "auto-create" process
        Returns:
        Whether it validates
        Throws:
        SQLException - Thrown if an error occurs in the validation process
      • initializeColumnInfoForPrimaryKeyColumns

        public void initializeColumnInfoForPrimaryKeyColumns​(Connection conn)
                                                      throws SQLException
        Utility to load the structure/metadata of primary key columns of the table.
        Parameters:
        conn - Connection to use for validation
        Throws:
        SQLException - Thrown if an error occurs in the initialization process
      • initializeColumnInfoFromDatastore

        public void initializeColumnInfoFromDatastore​(Connection conn)
                                               throws SQLException
        Initialize the default value for columns if null using the values from the datastore.
        Parameters:
        conn - The JDBC Connection
        Throws:
        SQLException - Thrown if an error occurs in the default initialisation.
      • validatePrimaryKey

        protected boolean validatePrimaryKey​(Connection conn)
                                      throws 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:
        SQLException - When an error occurs in the valdiation
      • validateConstraints

        public boolean validateConstraints​(Connection conn,
                                           boolean autoCreate,
                                           Collection autoCreateErrors,
                                           org.datanucleus.ClassLoaderResolver clr)
                                    throws SQLException
        Method to validate any constraints, and auto create them if required.
        Parameters:
        conn - The JDBC Connection
        autoCreate - Whether to auto create the constraints if not existing
        autoCreateErrors - Errors found in the "auto-create" process
        clr - The ClassLoaderResolver
        Returns:
        Whether the database was modified
        Throws:
        SQLException - Thrown when an error occurs in the JDBC calls
      • createConstraints

        public boolean createConstraints​(Connection conn,
                                         Collection autoCreateErrors,
                                         org.datanucleus.ClassLoaderResolver clr)
                                  throws SQLException
        Method used to create all constraints for a brand new table.
        Parameters:
        conn - The JDBC Connection
        autoCreateErrors - Errors found in the "auto-create" process
        clr - The ClassLoaderResolver
        Returns:
        Whether the database was modified
        Throws:
        SQLException - Thrown when an error occurs in the JDBC calls
      • dropConstraints

        public void dropConstraints​(Connection conn)
                             throws SQLException
        Method to drop the constraints for the table from the datastore.
        Parameters:
        conn - The JDBC Connection
        Throws:
        SQLException - Thrown when an error occurs in the JDBC call.
      • getExpectedForeignKeys

        public 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 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 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.
      • getSQLCreateStatements

        protected List<String> getSQLCreateStatements​(Properties props)
        Accessor for the SQL CREATE statements for this table.
        Specified by:
        getSQLCreateStatements in class AbstractTable
        Parameters:
        props - Properties for controlling the table creation
        Returns:
        List of statements.
      • getSQLAddFKStatements

        protected Map<String,​String> getSQLAddFKStatements​(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 keys
        clr - The ClassLoaderResolver
        Returns:
        a Map with the SQL statements
      • getSQLAddCandidateKeyStatements

        protected Map<String,​String> getSQLAddCandidateKeyStatements​(Map 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
      • getSQLCreateIndexStatements

        protected Map<String,​String> getSQLCreateIndexStatements​(Map actualIndicesByName,
                                                                       org.datanucleus.ClassLoaderResolver clr)
        Accessor for the CREATE INDEX statements for this table.
        Parameters:
        actualIndicesByName - Map of actual indexes
        clr - The ClassLoaderResolver
        Returns:
        Map of statements
      • logMapping

        protected void logMapping​(String memberName,
                                  JavaTypeMapping mapping)
        Convenience logging method to output the mapping information for an element, key, value field.
        Parameters:
        memberName - Name of the member
        mapping - The mapping