Class DBQuery

All Implemented Interfaces:
Entity

public class DBQuery extends DBRowSet
This class can be used to wrap a query from a DBCommand and use it like a DBRowSet.
You may use this class for two purposes:
  • In oder to define subqueries simply define a command object with the subquery and wrap it inside a DBQuery. Then in a second command object you can reference this Query to join with your other tables and views. In order to join other columns with your query use column(DBColumnExpr expr) to get the query column object for a given column expression in the underlying select clause.
  • With a key supplied you can have an updateable query that will update several records at once.
  • Field Details

    • cmdExpr

      protected final DBCommandExpr cmdExpr
    • queryColumns

      protected final DBColumnExpr[] queryColumns
    • keyColumns

      protected final DBColumn[] keyColumns
    • alias

      protected final String alias
    • updateable

      protected boolean updateable
  • Constructor Details

    • DBQuery

      public DBQuery(DBCommandExpr cmd, DBColumn[] keyColumns, String alias)
      Constructor initializes the query object. Saves the queryColumns and the primary key of this query.
      Parameters:
      cmd - the SQL-Command
      keyColumns - an array of the primary key queryColumns
      alias - the query alias
    • DBQuery

      public DBQuery(DBCommandExpr cmd, DBColumn[] keyColumns)
      Constructor initializes the query object. Saves the queryColumns and the primary key of this query.
      Parameters:
      cmd - the SQL-Command
      keyColumns - an array of the primary key queryColumns
    • DBQuery

      public DBQuery(DBCommandExpr cmd, DBColumn keyColumn, String alias)
      Constructs a new DBQuery object initialize the query object. Save the queryColumns and the primary key of this query.
      Parameters:
      cmd - the SQL-Command
      keyColumn - the primary key column
      alias - the query alias
    • DBQuery

      public DBQuery(DBCommandExpr cmd, DBColumn keyColumn)
      Constructs a new DBQuery object initialize the query object. Save the queryColumns and the primary key of this query.
      Parameters:
      cmd - the SQL-Command
      keyColumn - the primary key column
    • DBQuery

      public DBQuery(DBCommandExpr cmd, String alias)
      Creates a DBQuery object from a given command object.
      Parameters:
      cmd - the command object representing an SQL-Command.
      alias - the query alias
    • DBQuery

      public DBQuery(DBCommandExpr cmd)
      Creates a DBQuery object from a given command object.
      Parameters:
      cmd - the command object representing an SQL-Command.
    • DBQuery

      public DBQuery(DBQuery other, String newAlias)
      Creates a copy of an existing DBQuery object.
      Parameters:
      other - the query which to copy
      newAlias - the new alias for the cloned query
  • 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
    • getCommandExpr

      public DBCommandExpr getCommandExpr()
      returns the underlying command expression
      Returns:
      the command used for this query
    • getName

      public String getName()
      not applicable - returns null
      Specified by:
      getName in class DBRowSet
    • getAlias

      public String getAlias()
      not applicable - returns null
      Specified by:
      getAlias in class DBRowSet
    • isUpdateable

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

      public void setUpdateable(boolean updateable)
      Sets whether or not records for this query can be updated Initially DBQuery records are not updateable For a query to be updateable it must have key columns
      Parameters:
      updateable - flag whether or not the column can be updated
    • column

      public DBQueryColumn column(DBColumnExpr expr)
      Returns the query column for a underlying column expression Use this function to map table columns to query columns
      Parameters:
      expr - the DBColumnExpr object
      Returns:
      the query column
    • column

      public DBQueryColumn column(String columnName)
      Returns the query column for a given column name in the underlying query
      Parameters:
      columnName - the column name in the underlying query
      Returns:
      the query column
    • column

      public DBQueryColumn column(int columnIndex)
      Returns the query column for a particular index Same as getColumn(columnIndex) but throws an exception if index is out of range
      Parameters:
      columnIndex - the index of the query column
      Returns:
      the located column
    • findColumn

      @Deprecated public DBQueryColumn findColumn(DBColumnExpr expr)
      Deprecated.
      Returns the query column for a underlying column expression Deprecated: use column(expr) instead
      Parameters:
      expr - the DBColumnExpr object
      Returns:
      the query column
    • findColumn

      @Deprecated public DBQueryColumn findColumn(String name)
      Deprecated.
      Returns the query column for a given column name Deprecated: use column(name) instead
      Parameters:
      name - the column name
      Returns:
      the query column
    • getQueryColumns

      @Deprecated public DBQueryColumn[] getQueryColumns()
      Deprecated.
      Gets all columns of this rowset (e.g. for cmd.select()). Deprecated: use getColumns() or getAllColumns() instead
      Returns:
      all columns of this rowset
    • getQueryColumnExprs

      public DBColumnExpr[] getQueryColumnExprs()
      Returns the select expressions of the underlying query
      Returns:
      the select expressions of the underlying query
    • getQueryColumnExpr

      public DBColumnExpr getQueryColumnExpr(int columnIndex)
      Returns the query column expression at a given index
      Parameters:
      columnIndex - the column index
      Returns:
      the query column expression at that index
    • getQueryColumnIndex

      public int getQueryColumnIndex(ColumnExpr columnExpr)
      Returns the index of a column in the underlying query
      Parameters:
      columnExpr - the column to find
      Returns:
      the index of the column
    • getKeyColumns

      public DBColumn[] getKeyColumns()
      Specified by:
      getKeyColumns in interface Entity
      Specified by:
      getKeyColumns in class DBRowSet
      Returns:
      the key columns
    • getRecordKey

      protected Object[] getRecordKey(DBRecordBase record)
      Returns the record key for a given record.
      Parameters:
      record - a record for this DBQuery
      Returns:
      the record key
    • addSQL

      public void addSQL(DBSQLBuilder sql, long context)
      Adds the select SQL Command of this object to the specified StringBuilder object.
      Specified by:
      addSQL in class DBExpr
      Parameters:
      sql - the SQL-Command
      context - the current SQL-Command context
    • initRecord

      public void initRecord(DBRecordBase record, RecordData recData, boolean newRecord)
      Add rowset data
      Overrides:
      initRecord in class DBRowSet
      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
    • createRecord

      public void createRecord(DBRecordBase record, Object[] initalKey, boolean deferredInit)
      Returns an error, because it is not possible to add a record to a query.
      Specified by:
      createRecord in class DBRowSet
      Parameters:
      record - the DBRecord object, contains all fields and the field properties
      initalKey - the initial record key
      deferredInit - flag whether to defer record initialization
      Throws:
      NotImplementedException - because this is not implemented
    • readRecord

      public void readRecord(DBRecordBase record, DBCompareExpr whereConstraints)
      Creates a select SQL-Command of the query call the InitRecord method to execute the SQL-Command.
      Overrides:
      readRecord in class DBRowSet
      Parameters:
      record - the DBRecord object, contains all fields and the field properties
      whereConstraints - the constraint for querying the record
    • updateRecord

      public void updateRecord(DBRecordBase record)
      Updates a query record by creating individual update commands for each table.
      Overrides:
      updateRecord in class DBRowSet
      Parameters:
      record - the DBRecord object. contains all fields and the field properties
    • deleteRecord

      public void deleteRecord(Object[] key, DBContext context)
      Deletes a record identified by its primary key from the database.
      Specified by:
      deleteRecord in class DBRowSet
      Parameters:
      key - array of primary key values
      context - the database context
    • addJoinRestriction

      protected boolean addJoinRestriction(DBCommand cmd, DBColumn updCol, DBColumn joinCol, Object[] key, DBRecordBase record)
      Adds join restrictions to the supplied command object.
      Parameters:
      cmd - the command
      updCol - the update column
      joinCol - the join column
      key - the record key
      record - the record
      Returns:
      flag whether the join restriction could be added
    • getCommandFromExpression

      protected DBCommand getCommandFromExpression()
      returns the command from the underlying command expression or throws an exception
      Returns:
      the command used for this query
    • createQueryColumn

      protected DBQueryColumn createQueryColumn(DBColumnExpr expr, int index)
      factory method for column expressions in order to allow overrides
      Parameters:
      expr - the column expression for the query column
      index - the column index
      Returns:
      the query column
    • getColumnIndex

      public int getColumnIndex(ColumnExpr columnExpr)
      Gets the index of a particular column expression.
      Overrides:
      getColumnIndex in class DBRowSet
      Parameters:
      columnExpr - the column expression for which to get the index of
      Returns:
      the position of a column expression
    • getColumnIndex

      public int getColumnIndex(DBColumn column)
      Description copied from class: DBRowSet
      Gets the index of a particular column expression.
      Overrides:
      getColumnIndex in class DBRowSet
      Parameters:
      column - column the DBColumn to get the index for
      Returns:
      the position of a column expression
    • getColumnExprAt

      protected DBColumnExpr getColumnExprAt(int index)
      Description copied from class: DBRowSet
      Returns the column expression at a given column index Allow overrides in derived classes
      Overrides:
      getColumnExprAt in class DBRowSet
      Parameters:
      index - the index of the desired expression
      Returns:
      the column expression
    • cloneColumn

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