Class AbstractTable

  • All Implemented Interfaces:
    Table, org.datanucleus.store.schema.table.Table
    Direct Known Subclasses:
    TableImpl, ViewImpl

    public abstract class AbstractTable
    extends Object
    implements Table
    Abstract implementation of a table in the datastore. The table exists in various states. After initialisation it can be created in the datastore by calling create. At any point after initialisation it can be modified, but only by addition of columns. The table can be dropped from the datastore by calling drop.
    • Field Detail

      • TABLE_STATE_NEW

        protected static final int TABLE_STATE_NEW
        Table object has just been created.
        See Also:
        Constant Field Values
      • TABLE_STATE_PK_INITIALIZED

        protected static final int TABLE_STATE_PK_INITIALIZED
        Table object is created and PK initialised.
        See Also:
        Constant Field Values
      • TABLE_STATE_INITIALIZED

        protected static final int TABLE_STATE_INITIALIZED
        Table object has been initialised.
        See Also:
        Constant Field Values
      • TABLE_STATE_INITIALIZED_MODIFIED

        protected static final int TABLE_STATE_INITIALIZED_MODIFIED
        Table object has been initialized but has had structural modifications since.
        See Also:
        Constant Field Values
      • TABLE_STATE_VALIDATED

        protected static final int TABLE_STATE_VALIDATED
        Table object has been validated.
        See Also:
        Constant Field Values
      • identifier

        protected final DatastoreIdentifier identifier
        Identifier name for the table. Includes the catalog/schema internally (if defined by the user).
      • state

        protected int state
        State of the table
      • columns

        protected List<org.datanucleus.store.schema.table.Column> columns
        Columns for this table.
      • columnsByIdentifier

        protected Map<DatastoreIdentifier,​Column> columnsByIdentifier
        Index to the columns, keyed by name identifier. TODO Key this by the column name, not its identifier.
      • existsInDatastore

        protected Boolean existsInDatastore
        Cache what we learned in a call to exists()
    • Constructor Detail

      • AbstractTable

        public AbstractTable​(DatastoreIdentifier identifier,
                             RDBMSStoreManager storeMgr)
        Constructor taking the table name and the RDBMSManager managing this table.
        Parameters:
        identifier - Name of the table
        storeMgr - The RDBMS Manager
    • Method Detail

      • isInitialized

        public boolean isInitialized()
        Accessor for whether the table is initialised.
        Specified by:
        isInitialized in interface Table
        Returns:
        Whether it is initialised
      • isPKInitialized

        public boolean isPKInitialized()
        Accessor for whether the primary key of the table is initialised.
        Returns:
        Whether the primary key of the table is initialised
      • isValidated

        public boolean isValidated()
        Accessor for whether the table is validated.
        Specified by:
        isValidated in interface Table
        Returns:
        Whether it is validated.
      • isInitializedModified

        public boolean isInitializedModified()
        Accessor for whether the table has been modified since initialisation.
        Specified by:
        isInitializedModified in interface Table
        Returns:
        Whether it is modified since initialisation.
      • getStoreManager

        public RDBMSStoreManager getStoreManager()
        Specified by:
        getStoreManager in interface Table
        Specified by:
        getStoreManager in interface org.datanucleus.store.schema.table.Table
      • getName

        public String getName()
        Specified by:
        getName in interface org.datanucleus.store.schema.table.Table
      • getCatalogName

        public String getCatalogName()
        Accessor for the Catalog Name. This will be part of the fully qualified name IF the user has specified the catalog in the MetaData, OR if they have specified the catalog in the PMF.
        Specified by:
        getCatalogName in interface org.datanucleus.store.schema.table.Table
        Returns:
        Catalog Name
      • getSchemaName

        public String getSchemaName()
        Accessor for the Schema Name. This will be part of the fully qualified name IF the user has specified the schema in the MetaData, OR if they have specified the schema in the PMF.
        Specified by:
        getSchemaName in interface org.datanucleus.store.schema.table.Table
        Returns:
        Schema Name
      • getClassMetaData

        public org.datanucleus.metadata.AbstractClassMetaData getClassMetaData()
        Specified by:
        getClassMetaData in interface org.datanucleus.store.schema.table.Table
      • getNumberOfColumns

        public int getNumberOfColumns()
        Specified by:
        getNumberOfColumns in interface org.datanucleus.store.schema.table.Table
      • getColumns

        public List<org.datanucleus.store.schema.table.Column> getColumns()
        Specified by:
        getColumns in interface org.datanucleus.store.schema.table.Table
      • getColumnForPosition

        public org.datanucleus.store.schema.table.Column getColumnForPosition​(int pos)
        Specified by:
        getColumnForPosition in interface org.datanucleus.store.schema.table.Table
      • getSurrogateColumn

        public Column getSurrogateColumn​(org.datanucleus.store.schema.table.SurrogateColumnType colType)
        Specified by:
        getSurrogateColumn in interface org.datanucleus.store.schema.table.Table
      • getColumnForName

        public org.datanucleus.store.schema.table.Column getColumnForName​(String name)
        Specified by:
        getColumnForName in interface org.datanucleus.store.schema.table.Table
      • getMemberColumnMappingForMember

        public org.datanucleus.store.schema.table.MemberColumnMapping getMemberColumnMappingForMember​(org.datanucleus.metadata.AbstractMemberMetaData mmd)
        Specified by:
        getMemberColumnMappingForMember in interface org.datanucleus.store.schema.table.Table
      • getMemberColumnMappingForEmbeddedMember

        public org.datanucleus.store.schema.table.MemberColumnMapping getMemberColumnMappingForEmbeddedMember​(List<org.datanucleus.metadata.AbstractMemberMetaData> mmds)
        Specified by:
        getMemberColumnMappingForEmbeddedMember in interface org.datanucleus.store.schema.table.Table
      • getMemberColumnMappings

        public Set<org.datanucleus.store.schema.table.MemberColumnMapping> getMemberColumnMappings()
        Specified by:
        getMemberColumnMappings in interface org.datanucleus.store.schema.table.Table
      • getSurrogateMapping

        public JavaTypeMapping getSurrogateMapping​(org.datanucleus.store.schema.table.SurrogateColumnType colType,
                                                   boolean allowSuperclasses)
        Description copied from interface: Table
        Accessor for the mapping for the specified surrogate type.
        Specified by:
        getSurrogateMapping in interface Table
        Parameters:
        colType - Column type for the surrogate
        allowSuperclasses - Whether to allow searching superclasses when not specified in this table.
        Returns:
        The mapping
      • getVersionMetaData

        public org.datanucleus.metadata.VersionMetaData getVersionMetaData()
        Accessor for Version MetaData
        Specified by:
        getVersionMetaData in interface Table
        Returns:
        Returns the Version MetaData.
      • getDiscriminatorMetaData

        public org.datanucleus.metadata.DiscriminatorMetaData getDiscriminatorMetaData()
        Accessor for Discriminator MetaData
        Specified by:
        getDiscriminatorMetaData in interface Table
        Returns:
        Returns the Discriminator MetaData.
      • addColumn

        public Column addColumn​(String storedJavaType,
                                DatastoreIdentifier name,
                                JavaTypeMapping mapping,
                                org.datanucleus.metadata.ColumnMetaData colmd)
        Creates a new column in the table. Will add the new Column and return it. If the new column clashes in name with an existing column of the required name will throw a DuplicateColumnNameException except when :-
        • The 2 columns are for same named fields in the class or its subclasses with the subclass(es) using "superclass-table" inheritance strategy. One of the columns has to come from a subclass - cant have both from the same class.
        Specified by:
        addColumn in interface Table
        Parameters:
        storedJavaType - the java type of the column
        name - the SQL identifier for the column to be added
        mapping - the mapping for the column to be added
        colmd - ColumnMetaData for the column to be added to the table
        Returns:
        the new Column
        Throws:
        DuplicateColumnException - if a column already exists with same name and not a supported situation.
      • hasColumn

        public boolean hasColumn​(DatastoreIdentifier identifier)
        Checks if there is a column for the identifier
        Specified by:
        hasColumn in interface Table
        Parameters:
        identifier - the identifier of the column
        Returns:
        true if the column exists for the identifier
      • getColumn

        public Column getColumn​(DatastoreIdentifier identifier)
        Accessor for the column with the specified identifier. Returns null if has no column of this name.
        Specified by:
        getColumn in interface Table
        Parameters:
        identifier - The name of the column
        Returns:
        The column
      • create

        public boolean create​(Connection conn)
                       throws SQLException
        Method to create this table.
        Specified by:
        create in interface Table
        Parameters:
        conn - Connection to the datastore.
        Returns:
        true if the table was created
        Throws:
        SQLException - Thrown if an error occurs creating the table.
      • drop

        public void drop​(Connection conn)
                  throws SQLException
        Method to drop this table.
        Specified by:
        drop in interface Table
        Parameters:
        conn - Connection to the datastore.
        Throws:
        SQLException - Thrown if an error occurs dropping the table.
      • exists

        public boolean exists​(Connection conn,
                              boolean auto_create)
                       throws SQLException
        Method to check the existence of the table/view, optionally auto creating it where required. If it doesn't exist and auto creation isn't specified this throws a MissingTableException.
        Specified by:
        exists in interface Table
        Parameters:
        conn - The JDBC Connection
        auto_create - Whether to auto create the table if not existing
        Returns:
        Whether the table was added
        Throws:
        SQLException - Thrown when an error occurs in the JDBC calls
      • equals

        public final boolean equals​(Object obj)
        Equality operator.
        Overrides:
        equals in class Object
        Parameters:
        obj - The object to compare against
        Returns:
        Whether the objects are equal
      • hashCode

        public final int hashCode()
        Accessor for the hash code of this table.
        Overrides:
        hashCode in class Object
        Returns:
        The hash code.
      • toString

        public final String toString()
        Method to return a string version of this table. This name is the fully-qualified name of the table,including catalog/schema names, where these are appropriate. They are included where the user has either specified the catalog/schema for the PMF, or in the MetaData. They are also only included where the datastore adapter supports their use.
        Overrides:
        toString in class Object
        Returns:
        String name of the table (catalog.schema.table)
      • getDatastoreIdentifierFullyQualified

        public DatastoreIdentifier getDatastoreIdentifierFullyQualified()
        Method that operates like toString except it returns a fully-qualified name that will always be fully-qualified even when the user hasnt specified the catalog/schema in PMF or MetaData. That is, it will add on any auto-calculated catalog/schema for the datastore. Note that this will never include any quoting strings required for insert/select etc.
        Returns:
        The fully qualified name
      • addColumnInternal

        protected void addColumnInternal​(Column col)
        Utility method to add a column to the internal representation
        Parameters:
        col - The column
      • hasColumnName

        protected boolean hasColumnName​(DatastoreIdentifier colName)
        Utility to return if a column of this name exists.
        Parameters:
        colName - The column name
        Returns:
        Whether the column of this name exists
      • getSQLCreateStatements

        protected abstract List<String> getSQLCreateStatements​(Properties props)
        Accessor for the SQL create statements.
        Parameters:
        props - Properties controlling the table creation
        Returns:
        The SQL Create statements
      • getSQLDropStatements

        protected abstract List<String> getSQLDropStatements()
        Accessor for the SQL drop statements.
        Returns:
        The SQL Drop statements
      • assertIsPKUninitialized

        protected void assertIsPKUninitialized()
      • assertIsUninitialized

        protected void assertIsUninitialized()
      • assertIsInitialized

        protected void assertIsInitialized()
      • assertIsInitializedModified

        protected void assertIsInitializedModified()
      • assertIsPKInitialized

        protected void assertIsPKInitialized()
      • assertIsValidated

        protected void assertIsValidated()
      • allowDDLOutput

        protected boolean allowDDLOutput()
        Determine whether we or our concrete class allow DDL to be written into a file instead of sending it to the DB. Defaults to true.
        Returns:
        Whether it allows DDL outputting
      • executeDdlStatementList

        protected void executeDdlStatementList​(List<String> stmts,
                                               Connection conn)
                                        throws SQLException
        Method to perform the required SQL statements.
        Parameters:
        stmts - A List of statements
        conn - The Connection to the datastore
        Throws:
        SQLException - Any exceptions thrown by the statements
      • executeDdlStatement

        protected void executeDdlStatement​(Statement stmt,
                                           String stmtText)
                                    throws SQLException
        Execute a single DDL SQL statement with appropriate logging. If ddlWriter is set, do not actually execute the SQL but write it to that Writer.
        Parameters:
        stmt - The JDBC Statement object to execute on
        stmtText - The actual SQL statement text
        Throws:
        SQLException - Thrown if an error occurs
      • tableExistsInDatastore

        protected boolean tableExistsInDatastore​(Connection conn)
                                          throws SQLException
        Determine whether our table exists in the datastore (without modifying datastore). Result is cached
        Parameters:
        conn - The Connection
        Returns:
        Whether the table exists in the datastore
        Throws:
        SQLException - Thrown if an error occurs