RowStructure.java

/*
** Module   : RowStructure.java
** Abstract : Defines a row structure in a query.
**
** Copyright (c) 2020-2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- ----------------------------------------Description---------------------------------------
** 001 CA  20200615 Created initial version.
** 002 SBI 20221031 Added a "fields" member. It is used as a storage of fields of "select" query.
** 003 AL2 20231123 Refactored this as an interface. Moved implementation in AbstractRowStructure.
**     AL2 20231211 Added getDmoMeta.
*/

/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
**   Under Section 7 of the GNU Affero GPL version 3, the following additional
**   terms apply to the works covered under the License.  These additional terms
**   are non-permissive additional terms allowed under Section 7 of the GNU
**   Affero GPL version 3 and may not be removed by you.
**
**   0. Attribution Requirement.
**
**     You must preserve all legal notices or author attributions in the covered
**     work or Appropriate Legal Notices displayed by works containing the covered
**     work.  You may not remove from the covered work any author or developer
**     credit already included within the covered work.
**
**   1. No License To Use Trademarks.
**
**     This license does not grant any license or rights to use the trademarks
**     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
**     of Golden Code Development Corporation. You are not authorized to use the
**     name Golden Code, FWD, or the names of any author or contributor, for
**     publicity purposes without written authorization.
**
**   2. No Misrepresentation of Affiliation.
**
**     You may not represent yourself as Golden Code Development Corporation or FWD.
**
**     You may not represent yourself for publicity purposes as associated with
**     Golden Code Development Corporation, FWD, or any author or contributor to
**     the covered work, without written authorization.
**
**   3. No Misrepresentation of Source or Origin.
**
**     You may not represent the covered work as solely your work.  All modified
**     versions of the covered work must be marked in a reasonable way to make it
**     clear that the modified work is not originating from Golden Code Development
**     Corporation or FWD.  All modified versions must contain the notices of
**     attribution required in this license.
*/

package com.goldencode.p2j.persist.orm;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;

import com.goldencode.p2j.persist.DataModelObject;
import com.goldencode.p2j.persist.PersistenceException;

/**
 * Defines a row structure in a SELECT query. There are multiplex flavors of such structure, most of them
 * attempting to optimize the hydration or representation of selected fields. Refer to 
 * {@link FullRowStructure} for an example of structure that represents the "select all" attempt.
 * 
 * Non-expanded properties are logically part of this structure, but will only affect the {@link #hasExtents()}
 * flag. In other words, non-expanded properties will be hydrated only if a non-expanded property is appended,
 * making {@link #hasExtents()} return true (or use {@link #forceExtents}. 
 * Otherwise, non-expanded extents won't be hydrated.
 */
public interface RowStructure
{
   /**
    * The DMO class whose properties are included in this structure. 
    * 
    * @return   The DMO class underlying this structure.
    */
   public Class<? extends DataModelObject> getDmoClass();

   /**
    * Append the recid to the row structure.
    * 
    * @param   recid 
    *          The recid property to be appended to the structure.
    * 
    * @return  {@code true} if adding the recid to the row structure succeeded
    *          If there is some invariant on the structure, adding props may fail.
    */
   public boolean addRecid(Property recid);

   /**
    * Append the multiplex to the row structure.
    * 
    * @param   multiplex 
    *          The multiplex property to be appended to the structure.
    * 
    * @return  {@code true} if adding the multiplex to the row structure succeeded
    *          If there is some invariant on the structure, adding props may fail.
    */
   public boolean addMultiplex(Property multiplex);

   /**
    * Append a property to the row structure. This doesn't check if the property actually belongs to the
    * DMO class used for this structure.
    * 
    * @param   prop 
    *          The property to be appended to the structure.
    * 
    * @return  {@code true} if adding the prop to the row structure succeeded
    *          If there is some invariant on the structure, adding props may fail.
    */
   public boolean addProperty(Property prop);

   /**
    * Retrieve the number of SQL fields that will be interpreted by this row structure. This is not the
    * same as the number of properties (e.g. datetime-tz consumes 2 SQL columns) 
    * 
    * @return   The number of SQL fields that are covered by this row structure.
    */
   public int getCount();
   
   /**
    * Retrieve an iterator to all properties in this structure, including the recid and multiplex.
    * 
    * @return   An iterator to all properties in this structure, in order of their append.
    */
   public Iterator<Property> getProps();

   /**
    * Check if this structure requires a hydration of non-expanded extent properties. This will only return true
    * if a non-expanded extent property was ever added through {@link #addProperty}  or {@link #forceExtents()} was
    * called.
    * 
    * @return   {@code true} if this structure will also hydrate non-expanded properties.
    */
   public boolean hasExtents();

   /**
    * Force the non-expanded extents to be hydrated.
    */
   public void forceExtents();

   /**
    * The core hydration logic based on the row structure. This class is specialized in traversing the 
    * result-set and populating the record with the data found there.
    * 
    * @param   session 
    *          The underlying session that generated the result-set. It is used only to emit
    *          load SQLs for the non-expanded extents. If this is {@code null}, the non-expanded
    *          extents will be skipped.
    * @param   resultSet
    *          The result-set that provides the low-level data that should be hydrated into
    *          the record.
    * @param   rsOffset
    *          The offset where the DMO starts. This is the case for queries that had joins,
    *          so the current record may not start at index 1.
    * @param   r
    *          The record to be hydrated.
    *          
    * @return  The next rsOffset, from where the next DMO in the result-set will start (if any).
    * 
    * @throws  PersistenceException
    *          thrown when hydrating extents fails (as it requires extra SQL queries to be run).
    * @throws  SQLException
    *          throw when failing to hydrate from the result-set.   
    */
   public int hydrate(Session session, ResultSet resultSet, int rsOffset, BaseRecord r)
   throws PersistenceException,
          SQLException;

   /** 
    * Retrieve the DMO meta used by the underlying DMO of this structure.
    * 
    * @return   The DMO meta of the DMO used by this row structure
    */
   public DmoMeta getDmoMeta();

   /**
    * Check if the structure is incomplete. Most implementations are hydrate the record partially,
    * except for {@link FullRowStructure}.
    * 
    * @return   {@code true} by default.
    */
   public default boolean isIncomplete()
   {
      return true;
   }
}