FullRowStructure.java

/*
** Module   : FullRowStructure.java
** Abstract : An row structure that will pick every property of a DMO in order.
**
** Copyright (c) 2023, Golden Code Development Corporation.
**
** -#- -I- --Date-- ----------------------------------------Description---------------------------------------
** 001 AL2 20231128 Initial version.
**     AL2 20231211 Recieve the DMO meta directly in the constructor.
**     AL2 20231212 Use dmoMeta directly, without getter.
**     AL2 20231211 Extract the property iteration out of asserts, otherwise they may be missed.
*/

/*
** 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.PersistenceException;
import com.goldencode.p2j.persist.TempRecord;

/**
 * Very fast row structure that knows that all properties should be hydrated and the order in the structure
 * is the same as the one desired by the hydrated record. The primary key is always first, followed by _temp
 * specific fields if any (including multiplex). All other fields are retrieved in the order specified by the
 * data array, honoring any datetime-tz fields (that require 2 columns from the result-set), expanded
 * extents (nothing special here) and non-expanded extents (eagerly hydrated using separate loading SQLs.
 */
public class FullRowStructure
extends AbstractRowStructure
{

   /**
    * Basic constructor over a specific DMO class.
    * 
    * @param   meta
    *          A DMO meta for the DMO over which this row structure is constructed.
    */
   public FullRowStructure(DmoMeta meta)
   {
      super(meta);
      hasExtents = dmoMeta.hasNotExpandedExtents();
   }

   /**
    * Append the recid to the row structure.
    * 
    * This is a no-op, as this kind of row structure (full) already presumes that the recid is part of the
    * 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.
    */
   @Override
   public boolean addRecid(Property recid)
   {
      return false;
   }

   /**
    * Append the multiplex to the row structure.
    * 
    * This is a no-op, as this kind of row structure (full) already presumes that the multiplex 
    * may be part of the structure (if temp).
    * 
    * @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.
    */
   @Override
   public boolean addMultiplex(Property multiplex)
   {
      return false;
   }

   /**
    * Append a property to the row structure. This doesn't check if the property actually belongs to the
    * DMO class used for this structure.
    * 
    * This is a no-op, as this kind of row structure (full) already presumes that all properties of a
    * specifc DMO are already part of the 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.
    */
   @Override
   public boolean addProperty(Property prop)
   {
      return false;
   }

   /**
    * 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) 
    */
   @Override
   public int getCount()
   {
      return dmoMeta.getSqlFieldCount();
   }

   /**
    * 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.
    */
   @Override
   public Iterator<Property> getProps()
   {
      return dmoMeta.getFields(true);
   }

   /**
    * Hydrate a record knowing that the result-set selected ALL of its properties in order. 
    * Using this invariant, the recid is always the first property. It is followed by the 
    * multiplex and _temp reserved properties (if the table is temporary). Afterwards, all
    * of the DMO properties are listed in order. If there are any non-expanded extents, they
    * will be loaded at the end of this method.
    * 
    * @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.   
    */
   @Override
   public int hydrate(Session session, ResultSet resultSet, int rsOffset, BaseRecord r)
   throws PersistenceException,
          SQLException
   {
      Iterator<Property> props = getProps();
      
      Property recidP = props.next();
      assert (recidP.id == ReservedProperty.ID_PRIMARY_KEY); // skip PK
      r.primaryKey(resultSet.getLong(rsOffset++));
      if (isTempTable())
      {
         Property multiplexP = props.next();
         assert (multiplexP.id == ReservedProperty.ID_MULTIPLEX); // skip multiplex
         ((TempRecord) r)._multiplex(resultSet.getInt(rsOffset++));
      }

      int recOffset = 0;
      while (props.hasNext())
      {
         Property prop = props.next();
         if (prop != null && prop.extent != 0 && prop.index == 0)
         {
            continue;
         }
         rsOffset += r.readProperty(recOffset++, resultSet, rsOffset);
      }
      
      if (hasExtents && session != null)
      {
         hydrateExtents(session, r, recOffset);
      }
      
      return rsOffset;
   }
   
   /**
    * Check if this structure is used for an incomplete record. 
    * 
    * This specific row structure (full) will always retrieve the whole record.
    */
   @Override
   public boolean isIncomplete() 
   {
      return false;
   }

}