Class DBTable

All Implemented Interfaces:
Cloneable, Entity
Direct Known Subclasses:
DBMSHandlerBase.DBSeqTable, OracleSYSDatabase.DBColComments, OracleSYSDatabase.DBColInfo, OracleSYSDatabase.DBConstraints, OracleSYSDatabase.DBTabComments, OracleSYSDatabase.DBUserConCol, TTable

public class DBTable extends DBRowSet implements Cloneable
This class represent one table of the database. It contains methods to get, add, update and delete records from the database.
  • Field Details

  • Constructor Details

    • DBTable

      public DBTable(String name, DBDatabase db, String alias)
      Construct a new DBTable object set the specified parameters to this object and add this object to the current database.
      Parameters:
      name - the table name
      db - the valid database object
      alias - the table alias
    • DBTable

      public DBTable(String name, DBDatabase db)
      Construct a new DBTable object set the specified parameters to this object and add this object to the current database.
      Parameters:
      name - the table name
      db - the valid database object
  • Method Details

    • generateAlias

      protected String generateAlias(String prefix)
      Automatically generates a new alias for this Object
      Parameters:
      prefix - the alias prefix
      Returns:
      an alias consisting of the prefix and a unique number
    • getName

      public String getName()
      Returns the table name of this object.
      Specified by:
      getName in class DBRowSet
      Returns:
      the table name of this object
    • getAlias

      public String getAlias()
      Returns the table alias name of this object.
      Specified by:
      getAlias in class DBRowSet
      Returns:
      the table alias name of this object
    • isUpdateable

      public boolean isUpdateable()
      Returns whether or not the table supports record updates. Default is true.
      Specified by:
      isUpdateable in class DBRowSet
      Returns:
      true if the table allows record updates
    • getKeyColumns

      public DBColumn[] getKeyColumns()
      Returns an array of all primary key columns.
      Specified by:
      getKeyColumns in interface Entity
      Specified by:
      getKeyColumns in class DBRowSet
      Returns:
      an array of all primary key columns
    • clone

      public Object clone() throws CloneNotSupportedException
      Clones this table and assigns a new table alias. This second instance of the same table can be used for self-joins.
       This method requires that all declared column fields are NOT declared final.
       i.e. instead of:
       
            public final DBTableColumn MYCOL;
       
       columns must be declared:
       
            public DBTableColumn MYCOL;
      
       A runtime exception for the CloneNotSupported will be thrown if references cannot be adjusted.
      
       Alternatively a second table instance may be created manually like this:
            
            public final MyTable MYTABLE1 = new MyTable();
            public final MyTable MYTABLE2 = new MyTable();
      
            ...
            cmd.join(MYTABLE1.ID, MYTABLE2.PARENTID); // self-join
            ...
       
      Overrides:
      clone in class Object
      Returns:
      a table clone with new table alias
      Throws:
      CloneNotSupportedException
    • clone

      public <T extends DBTable> T clone(String newAlias)
    • cloneColumn

      protected DBColumn cloneColumn(DBRowSet clone, DBColumn sourceColumn)
      Description copied from class: DBRowSet
      Clones a RowSet column
      Specified by:
      cloneColumn in class DBRowSet
      Parameters:
      clone - the cloned rowset
      sourceColumn - the source column
      Returns:
      the cloned column
    • clonePrimaryKey

      protected DBIndex clonePrimaryKey(DBRowSet clone)
    • addColumn

      protected void addColumn(DBTableColumn column)
      Adds a column to this table's column list.
      Parameters:
      column - a column object
    • createAndAppendColumn

      protected DBTableColumn createAndAppendColumn(String columnName, DataType type, double size, boolean required, Object defValue)
      Creates a new Column object and appends it to the column list
      Parameters:
      columnName - the column name
      type - the type of the column e.g. integer, text, date
      size - the column width
      required - true if not null column
      defValue - a Object object
      Returns:
      the new column object
    • addColumn

      public final DBTableColumn addColumn(String columnName, DataType type, double size, boolean required, Object defValue)
      Creates a new DBTableColumn object and adds it to the column collection. Instead of the data mode enum, a boolean flag is used to indicate whether the column is required or optional.
      Parameters:
      columnName - the column name
      type - the type of the column e.g. integer, text, date
      size - the column width
      required - true if not null column
      defValue - a Object object
      Returns:
      the new column object
    • addColumn

      public final DBTableColumn addColumn(String columnName, DataType type, double size, boolean required)
      Creates a new table column and adds it to the table's column list
      Parameters:
      columnName - the column name
      type - the type of the column e.g. integer, text, date
      size - the column width
      required - true if not null column
      Returns:
      the new column object
    • addColumn

      public final DBTableColumn addColumn(String columnName, DataType type, double size, boolean required, Options options)
      Creates a new table column with options and adds it to the table's column list This overload should be used for column containing enum values which have no default value.
      Parameters:
      columnName - the column name
      type - the type of the column e.g. integer, text, date
      size - the column width
      required - true if not null column
      options - this list of options
      Returns:
      the new column object
    • addColumn

      public final DBTableColumn addColumn(String columnName, DataType type, double size, boolean required, Options options, Object defValue)
      Creates a new table column with options and adds it to the table's column list This overload should be used for column containing enum values which have a default value.
      Parameters:
      columnName - the column name
      type - the type of the column e.g. integer, text, date
      size - the column width
      required - true if not null column
      options - this list of options
      defValue - the default value
      Returns:
      the new column object
    • addColumn

      public final DBTableColumn addColumn(String columnName, DataType type, double size, boolean required, Class<? extends Enum<?>> enumType)
      Creates a new table column with Enum-Options and adds it to the table's column list This overload should be used for column containing enum values which have no default value.
      Parameters:
      columnName - the column name
      type - the type of the column e.g. integer, text, date
      size - the column width
      required - true if not null column
      enumType - the class of the enum type
      Returns:
      the new column object
    • addColumn

      public final DBTableColumn addColumn(String columnName, DataType type, double size, boolean required, Enum<?> enumValue)
      Creates a new table column with Enum-Options and adds it to the table's column list This overload should be used for column containing enum values which have a default value.
      Parameters:
      columnName - the column name
      type - the type of the column e.g. integer, text, date
      size - the column width
      required - true if not null column
      enumValue - the default value
      Returns:
      the new column object
    • addIdentity

      public DBTableColumn addIdentity(String name, String seqName)
      Adds an Identity column to the table which also serves as the PrimaryKey An Identity Column is always an auto-generated Integer(Long) value
      Parameters:
      name - the name of the identity column
      seqName - (optional) name of the sequence if supported by DBMS
      Returns:
      the Identity column
    • addIdentity

      public final DBTableColumn addIdentity(String name)
      Adds an Identity column to the table which also serves as the PrimaryKey An Identity Column is always an auto-generated Integer(Long) value
      Parameters:
      name - the name of the identity column
      Returns:
      the Identity column
    • addForeignKey

      public DBTableColumn addForeignKey(String name, DBTable target, boolean required, Options options, DBRelation.DBCascadeAction cascadeAction)
      Adds a new ForgeinKey table column the column list The foreign table must have a single column foreign key
      Parameters:
      name - the name of the new column
      target - the table on which to reference
      required - true if the value is required
      options - (optional) a set of allowed values for this column
      cascadeAction - the cascade action
      Returns:
      the new column
    • addForeignKey

      public final DBTableColumn addForeignKey(String name, DBTable target, boolean required, boolean cascade)
      Adds a new ForgeinKey table column the column list The foreign table must have a single column foreign key
      Parameters:
      name - the name of the new column
      target - the table on which to reference
      required - true if the value is required
      cascade - whether or not to cascade deletes for this relation
      Returns:
      the new column
    • addForeignKey

      public final DBTableColumn addForeignKey(String name, DBTable target, boolean required)
      Adds a new ForgeinKey table column the column list The foreign table must have a single column foreign key
      Parameters:
      name - the name of the new column
      target - the table on which to reference
      required - true if the value is required
      Returns:
      the new column
    • addTimestamp

      public DBTableColumn addTimestamp(String name)
      Adds a Timestamp column to the current table which will be used for optimistic locking. There can only be one timestamp column per table
      Parameters:
      name - the name of the new column
      Returns:
      the new column
    • getPrimaryKey

      public DBIndex getPrimaryKey()
      Returns the primary key.
      Returns:
      the the DBIndex object -> primary key
    • getIndexes

      public List<DBIndex> getIndexes()
      Returns the list of indexes (except the primary key).
      Returns:
      a list of DBIndex objects
    • setPrimaryKey

      public void setPrimaryKey(DBColumn... columns)
      Sets the primary key.
      Parameters:
      columns - a array with one or more DBColumn objects
    • addIndex

      public DBIndex addIndex(DBIndex index)
      Adds an index.
      Parameters:
      index - the index to add
      Returns:
      the Index object
    • addIndex

      public final DBIndex addIndex(String name, DBIndex.DBIndexType type, DBColumn... columns)
      Adds an index.
      Parameters:
      name - the index name
      type - the index type
      columns - the columns indexed by this index
      Returns:
      the Index object
    • addIndex

      public final DBIndex addIndex(String name, boolean unique, DBColumn... columns)
      Adds an index. Overload for convenience
      Parameters:
      name - the index name
      unique - flag whether the index is unique
      columns - the index column
      Returns:
      the index object
    • removeIndex

      public void removeIndex(DBIndex index)
      removes an index.
      Parameters:
      index - the index to remove
    • addSQL

      public void addSQL(DBSQLBuilder sql, long context)
      Adds the table's name to the supplied sql command buffer.
      Specified by:
      addSQL in class DBExpr
      Parameters:
      sql - the SQL-Command
      context - the current SQL-Command context
    • createRecord

      public void createRecord(DBRecordBase record, Object[] initalKey, boolean deferredInit)
      Gets all table fields and the fields properties. Set this to the specified DBRecord object.
      Specified by:
      createRecord in class DBRowSet
      Parameters:
      record - the DBRecord object. contains all fields and the field properties
      initalKey - the initial key values
      deferredInit - flag whether to defer initialization
    • checkUniqueConstraints

      public DBIndex checkUniqueConstraints(DBRecordBase record)
      Checks weather a unique constraint is violated when inserting or updating a record.
      Parameters:
      record - the record to check
      Returns:
      the violated index
    • getDefaultCascadeDeleteAction

      public DBRelation.DBCascadeAction getDefaultCascadeDeleteAction()
      returns the default cascade action for deletes on this table. This is used as the default for newly created relations on this table and does not affect existing relations.
      Returns:
      the delete cascade action for new relations (DBRelation.DBCascadeAction.CASCADE_RECORDS) are enabled
    • setDefaultCascadeDeleteAction

      public void setDefaultCascadeDeleteAction(DBRelation.DBCascadeAction cascadeDeleteAction)
      sets the default cascade action for deletes on foreign key relations.
      Parameters:
      cascadeDeleteAction - cascade action for deletes (DBRelation.DBCascadeAction.CASCADE_RECORDS)
    • deleteRecord

      public void deleteRecord(Object[] key, DBContext context)
      Creates a delete SQL-Command by using the DBCommand getDelete method execute the the SQL-Command with the DBDatabase executeSQL method.
      Specified by:
      deleteRecord in class DBRowSet
      Parameters:
      key - an array of the primary key columns
      context - the database context
    • getForeignKeyRelations

      public List<DBRelation> getForeignKeyRelations()
      Returns a list of all foreign key relations for this table
      Returns:
      the list of foreign key relations
    • hasExistingReferences

      public boolean hasExistingReferences(Object[] key, DBContext context)
      Determines whether a record is references by other records through a foreign-key relation or not
      Parameters:
      key - the key the record to be deleted
      context - the db context
      Returns:
      true if the record is references or false otherwise
    • getExistingReferenceTables

      public List<DBTable> getExistingReferenceTables(Object[] key, DBContext context)
      Returns a list of all tables that contain records which reference this record
      Parameters:
      key - the key the record to be deleted
      context - the db context
      Returns:
      this list of all tables that contain a refernce to this record
    • validateValue

      protected Object validateValue(DBTableColumn column, Object value)
      validates a column value
      Parameters:
      column - the column
      value - the value to validate
      Returns:
      the validated (possibly converted) value
    • initRecordDefaultValues

      protected void initRecordDefaultValues(DBRecordBase record, DBRowSet.FieldInitMode fieldInitMode)
      initializes the Record Default Values
      Overrides:
      initRecordDefaultValues in class DBRowSet
      Parameters:
      record - the record
      fieldInitMode - the field initialization mode