Class DBRowSet

All Implemented Interfaces:
Entity
Direct Known Subclasses:
DBCommandExpr.DBCmdQuery, DBQuery, DBTable, DBView

public abstract class DBRowSet extends DBExpr implements Entity
This class is the base class for all the DBTable, DBView and DBQuery classes this class contains all the columns of the tables, views or queries
  • Field Details

    • log

      protected static final org.slf4j.Logger log
    • db

      protected final DBDatabase db
    • comment

      protected String comment
    • entityName

      protected String entityName
    • timestampColumn

      protected DBColumn timestampColumn
    • columnReferences

      protected Map<DBColumn,DBColumn> columnReferences
    • columns

      protected List<DBColumn> columns
    • attributes

      protected Attributes attributes
    • beanType

      protected Class<?> beanType
  • Constructor Details

    • DBRowSet

      public DBRowSet(DBDatabase db)
      Constructs a DBRecord object set the current database object.
      Parameters:
      db - the database object
  • Method Details

    • checkParamRecord

      protected void checkParamRecord(DBRecordBase record, boolean checkValid)
      Internally used for parameter checking
      Parameters:
      record - the record to check
      checkValid - flag whether to check the validity
    • key

      public static DBColumn[] key(DBColumn... parts)
      varArgs to Array
      Parameters:
      parts - the parts
      Returns:
      the key columns array
    • getIdentifier

      public String getIdentifier()
      Gets an identifier for this RowSet Object
      Returns:
      the rowset identifier
    • getAttribute

      public Object getAttribute(String name)
      Returns a named attribute for this table
      Parameters:
      name - the attribute name
      Returns:
      the attribute value
    • getAttributes

      public Set<Attributes.Attribute> getAttributes()
      Returns all metadata attributes.
      Returns:
      set of metadata attributes
    • setAttribute

      public <T extends DBTable> T setAttribute(String name, Object value)
      Sets the value of a attribute.
      Type Parameters:
      T - the table type
      Parameters:
      name - the attribute name
      value - the value of the attribute
      Returns:
      returns self (this)
    • hashCode

      public int hashCode()
      Custom serialization for transient database. private void writeObject(ObjectOutputStream strm) throws IOException { // Database strm.writeObject(db.getIdentifier()); // write the rest strm.defaultWriteObject(); } private void readObject(ObjectInputStream strm) throws IOException, ClassNotFoundException { // Database String dbid = String.valueOf(strm.readObject()); // find database DBDatabase dbo = DBDatabase.findByIdentifier(dbid); if (dbo==null) throw new ItemNotFoundException(dbid); // set final field ClassUtils.setPrivateFieldValue(DBRowSet.class, this, "db", dbo); // read the rest strm.defaultReadObject(); }
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object other)
      Overrides:
      equals in class Object
    • isSame

      public boolean isSame(DBRowSet other)
      Compares the rowset to another one
      Parameters:
      other - the rowset to compare this rowset with
      Returns:
      true if it is the same rowset (but maybe a different instance)
    • getName

      public abstract String getName()
    • getAlias

      public abstract String getAlias()
    • isUpdateable

      public abstract boolean isUpdateable()
    • createRecord

      public abstract void createRecord(DBRecordBase record, Object[] initalKey, boolean deferredInit)
    • deleteRecord

      public abstract void deleteRecord(Object[] key, DBContext context)
    • getKeyColumns

      public abstract DBColumn[] getKeyColumns()
      Specified by:
      getKeyColumns in interface Entity
    • getFullName

      public String getFullName()
      Returns the full qualified name of the rowset.

      Returns:
      the full qualified name
    • getEntityName

      public String getEntityName()
      Returns the entity name creating qualified names. This is usually the same as "getName()" but it may be overridden to return singular instead of plural
      Specified by:
      getEntityName in interface Entity
      Returns:
      the entity name
    • setEntityName

      protected void setEntityName(String entityName)
      sets the entity name for creating qualified names. This is usefull if the table or view name is plural but you want to qualifiy names in singular
      Parameters:
      entityName - the entity name
    • getBeanType

      public Class<?> getBeanType()
      returns the bean type for this rowset
      Specified by:
      getBeanType in interface Entity
      Returns:
      the bean type
    • setBeanType

      public void setBeanType(Class<?> beanType)
      sets the bean type for this rowset
      Parameters:
      beanType - the bean type for this rowset
    • setBeanType

      public <T> void setBeanType(Class<T> beanType, DBBeanListFactory<T> factory)
      sets the bean type for this rowset
      Type Parameters:
      T - the bean type
      Parameters:
      beanType - the bean type for this rowset
      factory - the bean factory
    • addReferencedColumns

      public void addReferencedColumns(Set<DBColumn> list)
      Description copied from class: DBExpr
      Internal function to obtain all DBColumnExpr-objects used by this expression.
      Specified by:
      addReferencedColumns in class DBExpr
      Parameters:
      list - list to which all used column expressions must be added
      See Also:
    • getDatabase

      public DBDatabase getDatabase()
      Returns the current DBDatabase object.

      Specified by:
      getDatabase in class DBObject
      Returns:
      the current DBDatabase object
    • getColumnCount

      public int getColumnCount()
      Returns the number of columns in this rowset
      Returns:
      the column count
    • getColumns

      public List<DBColumn> getColumns()
      Returns a list of all columns of this rowset (e.g. for cmd.select()).
      Specified by:
      getColumns in interface Entity
      Returns:
      all columns of this rowset
    • getAllColumns

      public DBColumn[] getAllColumns()
      Returns an array of all columns of this rowset
      Returns:
      all columns of this rowset
    • getColumnIndex

      public int getColumnIndex(DBColumn column)
      Gets the index of a particular column expression.
      Parameters:
      column - column the DBColumn to get the index for
      Returns:
      the position of a column expression
    • getColumnIndex

      public int getColumnIndex(ColumnExpr columnExpr)
      Gets the index of a particular column expression.
      Parameters:
      columnExpr - the Column to get the index for
      Returns:
      the position of a column expression
    • getColumn

      public DBColumn getColumn(int iColumn)
      Returns a DBColumn object by a specified index value.
      Parameters:
      iColumn - the index to get the DBColumn for
      Returns:
      the index value
    • getColumn

      public DBColumn getColumn(String name)
      Gets the column Expression with a particular name.
      Specified by:
      getColumn in interface Entity
      Parameters:
      name - the name of the column to look for
      Returns:
      the column Expression at position
    • isColumnReadOnly

      public boolean isColumnReadOnly(DBColumn col)
      Checks whether a column is read only or writable. Only the timestamp column is read only by default. The primary is read only if the column is of type.
      Parameters:
      col - the column object
      Returns:
      a new DBCountExpr object
    • isKeyColumn

      public boolean isKeyColumn(DBColumn column)
      Checks whether a given column is part of the primary key for this RowSet
      Parameters:
      column - the column to check
      Returns:
      true if the column is part of the primary key or false otherwise
    • getComment

      public String getComment()
      Returns:
      Returns the comment.
    • setComment

      public void setComment(String comment)
      Parameters:
      comment - The comment to set.
    • getTimestampColumn

      public DBColumn getTimestampColumn()
      Returns:
      Returns the timestampColumn.
    • setTimestampColumn

      public void setTimestampColumn(DBColumn timestampColumn)
      Parameters:
      timestampColumn - The timestampColumn to set.
    • getColumnReferences

      public Map<DBColumn,DBColumn> getColumnReferences()
      Returns the a list of column references.
      Returns:
      a list of references
    • addColumnReference

      protected void addColumnReference(DBColumn source, DBColumn target)
      Adds a column reference to the list of table references. This method is internally called from DBDatabase.addReleation().
      Parameters:
      source - a column reference for one of this table's column
      target - the target column to which the source column references
    • count

      public DBColumnExpr count()
      Returns a new DBCountExpr object.
      Returns:
      a new DBCountExpr object
    • getRenameTablePhrase

      protected String getRenameTablePhrase()
      Returns the sql phrase for renaming tables. usually just a space character ' '
      Returns:
      the table rename phrase
    • getColumnExprAt

      protected DBColumnExpr getColumnExprAt(int index)
      Returns the column expression at a given column index Allow overrides in derived classes
      Parameters:
      index - the index of the desired expression
      Returns:
      the column expression
    • on

      Creates a join expression based on a compare expression
      Parameters:
      cmp - the compare expression for the join
      Returns:
      the join expression
    • on

      public DBCrossJoinExpr on(DBRowSet right)
      Creates a cross join expression
      Parameters:
      right - the rowset which to cross join
      Returns:
      the cross join expression
    • initRecord

      protected void initRecord(DBRecordBase record, Object[] key, DBRowSet.FieldInitMode fieldInitMode, boolean newRecord)
      Initializes a DBRecord for this RowSet and sets primary key values. The record may then be modified and updated.

      Parameters:
      record - the Record object
      key - an array of the values for the primary key
      fieldInitMode - indicates how to initializes the record fields
      newRecord - true if the record is new or false if it is an existing record
    • initRecord

      public void initRecord(DBRecordBase record, RecordData recData, boolean newRecord)
      Initializes a DBRecord for this rowset using the record data provided (i.e. from a DBReader)
      The record may then be modified and updated.
      At least all primary key columns must be supplied.
      We strongly recommend to supply the value of the update timestamp column in order to detect concurrent changes.

      Parameters:
      record - the record object
      recData - the record data from which to initialized the record
      newRecord - true if the record is new or false if it is an existing record
    • initRecord

      public final void initRecord(DBRecordBase record, RecordData recData)
      Initializes a DBRecord for this rowset for an existing record
      Parameters:
      record - the record object
      recData - the record data from which to initialized the record
      See Also:
    • initRecordDefaultValues

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

      protected void prepareInitRecord(DBRecordBase record, boolean newRecord)
      Initialize this DBRowSet object and sets it's initial state.
      Parameters:
      record - the DBRecord object to initialize this DBRowSet object
      newRecord - flag whether the record is new (non-existing) in the database
    • completeInitRecord

      protected void completeInitRecord(DBRecordBase record)
      Completes the record initialization.
      Override this function to do post initialization processing.
      Parameters:
      record - the DBRecord object to initialize
    • getKeyConstraints

      protected DBCompareExpr getKeyConstraints(Object[] key)
      Set the constraints for a single record from a supplied key
      Parameters:
      key - the record key
      Returns:
      the primary key constraint expression
    • readRecord

      protected void readRecord(DBRecordBase record, DBCommand cmd)
      Reads a single record from the database using the given command object.
      If a record is found the DBRecord object will hold all record data.
      Parameters:
      record - the DBRecord object which holds the record data
      cmd - the SQL-Command used to query the record
    • readRecord

      public void readRecord(DBRecordBase record, DBCompareExpr whereConstraints)
      Reads a record from the database
      Parameters:
      record - the DBRecord object which will hold the record data
      whereConstraints - the constraint for querying the record
    • readRecord

      public void readRecord(DBRecordBase record, DBCompareExpr whereConstraints, DBRowSet.PartialMode mode, DBColumn... columns)
      Reads the partial record for a given primary key from the database
      Parameters:
      record - the DBRecord object which will hold the record data
      whereConstraints - a compare expression for querying the record
      mode - flag whether to include only the given columns or whether to add all but the given columns
      columns - the columns to include or exclude (depending on mode)
    • recordExists

      public boolean recordExists(Object[] key, DBContext context)
      Returns true if the record exists in the database or false otherwise.

      Parameters:
      key - an array of the primary key columns
      context - the DBContext
      Returns:
      true if the record exists or false otherwise
    • recordExists

      public final boolean recordExists(Object id, DBContext context)
      Returns true if the record exists in the database or false otherwise.
      Parameters:
      id - id of the record
      context - the DBContext
      Returns:
      true if the record exists or false otherwise
    • updateRecord

      public <R extends DBRecordBase> void updateRecord(R record)
      Updates or Inserts a record in the database.
      Whether an update or insert is performed depends on the record state.
      Only modified fields will be inserted or updated in the database.

      If a timestamp-column is set for this RowSet then a constraint will be added in the update statement in order to detect concurrent changes.
      If the record has been modified by another user, an error of type DBErrors.RecordUpdateFailed will be set.

      Type Parameters:
      R - the record type
      Parameters:
      record - the DBRecord object. contains all fields and the field properties
    • deleteRecord

      public final void deleteRecord(long id, DBContext context)
      Deletes a single record from the database.

      Parameters:
      id - the record's primary key
      context - the DBContext
    • deleteAllReferences

      protected void deleteAllReferences(Object[] key, DBContext context)
      Deletes all records which reference this table.

      Parameters:
      key - the key the record to be deleted
      context - the DBContext
    • deleteReferenceRecords

      protected void deleteReferenceRecords(DBRelation.DBReference[] refs, Object[] parentKey, DBContext context)
      Deletes all records which are referenced by a particular relation.

      Parameters:
      refs - the reference columns belonging to the relation
      parentKey - the key of the parent element
      context - the DBContext
    • createRecordCommand

      protected DBCommand createRecordCommand(DBContext context)
      Creates a new command object for record handing May use Prepared statements even if disabled in context
      Parameters:
      context - the db context
      Returns:
      the command object
    • getRowsetData

      protected final Object getRowsetData(DBRecordBase record)
      Returns additional data stored on a record by the RowSet
      Parameters:
      record - the record
      Returns:
      the rowset data
    • setRowsetData

      protected final void setRowsetData(DBRecordBase record, Object rowsetData)
      May be used by a Rowset to store additional data on a record
      Parameters:
      record - the record
      rowsetData - the extra data to set on this record
    • initClonedFields

      protected <T extends DBRowSet> void initClonedFields(T clone) throws CloneNotSupportedException
      Clone columns
      Type Parameters:
      T - the rowset type
      Parameters:
      clone - the cloned rowset
      Throws:
      CloneNotSupportedException - exception if cloning the fields fails
    • cloneColumn

      protected abstract DBColumn cloneColumn(DBRowSet clone, DBColumn scourceColumn)
      Clones a RowSet column
      Parameters:
      clone - the cloned rowset
      scourceColumn - the source column
      Returns:
      the cloned column