Author: gk Date: Thu Feb 6 16:43:19 2025 New Revision: 1923618 URL: http://svn.apache.org/viewvc?rev=1923618&view=rev Log: TORQUE-372: Update recordMapperBase.vm
Modified: db/torque/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/recordmapper/base/recordMapperBase.vm Modified: db/torque/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/recordmapper/base/recordMapperBase.vm URL: http://svn.apache.org/viewvc/db/torque/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/recordmapper/base/recordMapperBase.vm?rev=1923618&r1=1923617&r2=1923618&view=diff ============================================================================== --- db/torque/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/recordmapper/base/recordMapperBase.vm (original) +++ db/torque/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/recordmapper/base/recordMapperBase.vm Thu Feb 6 16:43:19 2025 @@ -1,236 +1,237 @@ -## Licensed to the Apache Software Foundation (ASF) under one -## or more contributor license agreements. See the NOTICE file -## distributed with this work for additional information -## regarding copyright ownership. The ASF licenses this file -## to you under the Apache License, Version 2.0 (the -## "License"); you may not use this file except in compliance -## with the License. You may obtain a copy of the License at -## -## http://www.apache.org/licenses/LICENSE-2.0 -## -## Unless required by applicable law or agreed to in writing, -## software distributed under the License is distributed on an -## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -## KIND, either express or implied. See the License for the -## specific language governing permissions and limitations -## under the License. -## -###### -## -## version $Id$ -## -## Creates the base peer's mapper class. -## -## This template expects the current source element to be a "table" element -## from the torque schema. -## The schema needs to be processed by the OMTransformer. -## The attributes of the current source element must be set -## as velocity variables. -## -package ${baseRecordMapperPackage}; - -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.torque.Column; -import org.apache.torque.TorqueException; -import org.apache.torque.criteria.Criteria; -import org.apache.torque.om.mapper.RecordMapper; -import org.apache.torque.om.mapper.MappingStrategy; - -#foreach ($columnElement in $torqueGen.getSourceElement().getChildren("column")) - #set ($colEnumPackage = $columnElement.getAttribute("enumPackage")) - #set ($colEnumClassName = $columnElement.getAttribute("enumClassName")) - #if ($columnElement.getAttribute("isEnum") == "true" && $colEnumPackage != $baseRecordMapperPackage) -import ${colEnumPackage}.${colEnumClassName}; - #end -#end -#if (${baseRecordMapperPackage} != $dbObjectPackage) -import ${dbObjectPackage}.${dbObjectClassName}; -#end -#if (${baseRecordMapperPackage} != $peerPackage) -import ${peerPackage}.${peerClassName}; -#end -#if (${baseRecordMapperPackage} != $basePeerPackage) -import ${basePeerPackage}.${basePeerClassName}; -#end - -#set ( $inheritanceBaseColumnElements = $torqueGen.getChildren("inheritance-column")) -## there should at most be one inheritance-column in each table -#foreach ($inheritanceBaseColumnElement in $inheritanceBaseColumnElements) - #set ( $columnElement = $inheritanceBaseColumnElement.getChild("column") ) - #set ( $inheritanceElements = $columnElement.getChildren("inheritance")) - #if ($inheritanceElements.size() > 0) - #if (${baseRecordMapperPackage} != $dbObjectPackage) - #foreach ($inheritanceElement in $inheritanceElements) - #set ( $inheritanceClassName = $inheritanceElement.getAttribute("className") ) -import ${dbObjectPackage}.${inheritanceClassName}; - #end - #end - #end -#end -/** - * Maps ResultSet rows into ${dbObjectClassName} objects. - * -#if ($torqueGen.booleanOption("torque.om.addTimeStamp")) - * The skeleton for this class was autogenerated by Torque on: - * - * [${torqueGen.now()}] - * -#end - */ -@SuppressWarnings("unused") -public class ${baseRecordMapperClassName} implements RecordMapper<${dbObjectClassName}> -{ -#if ($torqueGen.booleanOption("torque.om.addTimeStamp")) - /** Serial version */ - private static final long serialVersionUID = ${torqueGen.now().Time}L; - -#else - /** Serial version */ - private static final long serialVersionUID = 1L; - -#end - /** The class log. */ - private static Log log - = LogFactory.getLog(${baseRecordMapperClassName}.class); - - ## TORQUE-364: Cached SQL expressions to speed up looking for columns selected by a given Criteria -#foreach ($columnElement in $torqueGen.getChildren("column")) - #set ( $peerColumnName = $columnElement.getAttribute("peerColumnName") ) - private static final String ${peerColumnName}_EXPRESSION = ${basePeerClassName}.${peerColumnName}.getSqlExpression(); -#end - - private boolean useMappingStrategy = #if("${useMappingStrategy}" == "true")true#{else}false#end; - - ## TORQUE-364: Should this be cached per RecordMapper (Thread safety/Multi query safety?) - private MappingStrategy<${dbObjectClassName}> strategy; - - ## TORQUE-364: init a new Strategy implementation - public void initStrategy() - { - this.strategy = new MappingStrategy<${dbObjectClassName}>(); - } - #if("${simpleMapping}" == "true") #set( $useSimpleMapping=1 ) #end - - /** - * Constructs the object from the current row in the resultSet. - * - * @param resultSet the resultSet to operate on, already pointing - * to the correct row. Not null. - * @param offset a possible offset in the columns to be considered - * (if previous columns contain other objects), - * or 0 for no offset. - * @param criteria The criteria which created the result set. - * If set, the attributes to set in the data object - * are determined from the select columns in the criteria; - * if no matching column can be found, null is returned. - * If not set, all of the table's columns are read from the - * result set in the order defined in the table definition. - * - * @return the mapped object, not null. - * - * @throws TorqueException when reading fields from the RecordSet fails - * or if a Criteria is passed which contains select columns other - * than the columns in the ${name} table. - */ - public $dbObjectClassName processRow( - ResultSet resultSet, - int offset, - Criteria criteria) - throws TorqueException - { -#set ( $inheritanceBaseColumnElement = $torqueGen.getChild("inheritance-column") ) -#if ($inheritanceBaseColumnElement) -$torqueGen.mergepoint("createDbObjectInstanceFromInheritance") -#else -$torqueGen.mergepoint("createDbObjectInstance") -#end -#if ($torqueGen.booleanOption("torque.om.trackLoading")) - try - { - ${field}.setLoading(true); -#end - // TORQUE-364 - if (criteria == null #if ($useSimpleMapping) || (criteria.getJoins().isEmpty() && criteria.getSelectColumns().size() - offset == ${basePeerClassName}.numColumns) #end) - { -#set ( $n = 1 ) -#foreach ($columnElement in $torqueGen.getChildren("column")) - #set ( $setter = $columnElement.getAttribute("setter") ) - #set ( $getter = $columnElement.getAttribute("getter") ) - ${field}.${setter}( - ${getter}(resultSet, offset + $n)); - #set ( $n = $n + 1 ) -#end - } - else - { - if (useMappingStrategy) - { - initStrategy(); - } - // try to get columns to be mapped - // from criteria's select columns - int totalOffset = offset + 1; - List<Column> selectColumns = criteria.getSelectColumns(); - List<Column> columnsWithoutOffset = selectColumns.subList( - offset, - selectColumns.size()); - Set<String> columnsMapped = new HashSet<String>(); - for (Column column : columnsWithoutOffset) - { - final int nextOffset = totalOffset; ## leaking assignment -#set ( $else = "" ) -#foreach ($columnElement in $torqueGen.getChildren("column")) - #set ( $setter = $columnElement.getAttribute("setter") ) - #set ( $getter = $columnElement.getAttribute("getter") ) - #set ( $peerColumnName = $columnElement.getAttribute("peerColumnName") ) - - ${else}if (!columnsMapped.contains(${peerColumnName}_EXPRESSION ) && ${peerColumnName}_EXPRESSION.equals( - column.getSqlExpression())) - { - if (useMappingStrategy) - { - strategy.addColumn(nextOffset, - (res, inst) -> inst.${setter}( ${getter}(res, nextOffset))); - } else - { - ${field}.${setter}( ${getter}(resultSet, totalOffset)); - } - columnsMapped.add( ${peerColumnName}_EXPRESSION ); - } - #set ( $else = "else ") -#end - totalOffset++; - } - if (columnsMapped.isEmpty()) - { - log.debug("no columns to map found in criteria, " - + "returning null"); - return null; - } - if (useMappingStrategy) - { - this.strategy.finish($torqueGen.getChildren("column").size(), #if("${mappingStrategySort}"=="true")true#{else}false#end); - this.strategy.execute(resultSet, $field); - } - } - ${field}.setNew(false); - ${field}.setModified(false); -#if ($torqueGen.booleanOption("torque.om.trackLoading")) - } - finally - { - ${field}.setLoading(false); - } -#end - return ${field}; - } - -$torqueGen.mergepoint("dbObjectFieldGetters") -} +## Licensed to the Apache Software Foundation (ASF) under one +## or more contributor license agreements. See the NOTICE file +## distributed with this work for additional information +## regarding copyright ownership. The ASF licenses this file +## to you under the Apache License, Version 2.0 (the +## "License"); you may not use this file except in compliance +## with the License. You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, +## software distributed under the License is distributed on an +## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +## KIND, either express or implied. See the License for the +## specific language governing permissions and limitations +## under the License. +## +###### +## +## version $Id$ +## +## Creates the base peer's mapper class. +## +## This template expects the current source element to be a "table" element +## from the torque schema. +## The schema needs to be processed by the OMTransformer. +## The attributes of the current source element must be set +## as velocity variables. +## +package ${baseRecordMapperPackage}; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.torque.Column; +import org.apache.torque.TorqueException; +import org.apache.torque.criteria.Criteria; +import org.apache.torque.om.mapper.RecordMapper; +import org.apache.torque.om.mapper.MappingStrategy; + +#foreach ($columnElement in $torqueGen.getSourceElement().getChildren("column")) + #set ($colEnumPackage = $columnElement.getAttribute("enumPackage")) + #set ($colEnumClassName = $columnElement.getAttribute("enumClassName")) + #if ($columnElement.getAttribute("isEnum") == "true" && $colEnumPackage != $baseRecordMapperPackage) +import ${colEnumPackage}.${colEnumClassName}; + #end +#end +#if (${baseRecordMapperPackage} != $dbObjectPackage) +import ${dbObjectPackage}.${dbObjectClassName}; +#end +#if (${baseRecordMapperPackage} != $peerPackage) +import ${peerPackage}.${peerClassName}; +#end +#if (${baseRecordMapperPackage} != $basePeerPackage) +import ${basePeerPackage}.${basePeerClassName}; +#end + +#set ( $inheritanceBaseColumnElements = $torqueGen.getChildren("inheritance-column")) +## there should at most be one inheritance-column in each table +#foreach ($inheritanceBaseColumnElement in $inheritanceBaseColumnElements) + #set ( $columnElement = $inheritanceBaseColumnElement.getChild("column") ) + #set ( $inheritanceElements = $columnElement.getChildren("inheritance")) + #if ($inheritanceElements.size() > 0) + #if (${baseRecordMapperPackage} != $dbObjectPackage) + #foreach ($inheritanceElement in $inheritanceElements) + #set ( $inheritanceClassName = $inheritanceElement.getAttribute("className") ) +import ${dbObjectPackage}.${inheritanceClassName}; + #end + #end + #end +#end +/** + * Maps ResultSet rows into ${dbObjectClassName} objects. + * +#if ($torqueGen.booleanOption("torque.om.addTimeStamp")) + * The skeleton for this class was autogenerated by Torque on: + * + * [${torqueGen.now()}] + * +#end + */ +@SuppressWarnings("unused") +public class ${baseRecordMapperClassName} implements RecordMapper<${dbObjectClassName}> +{ +#if ($torqueGen.booleanOption("torque.om.addTimeStamp")) + /** Serial version */ + private static final long serialVersionUID = ${torqueGen.now().Time}L; + +#else + /** Serial version */ + private static final long serialVersionUID = 1L; + +#end + /** The class log. */ + private static Log log + = LogFactory.getLog(${baseRecordMapperClassName}.class); + + ## TORQUE-364: Cached SQL expressions to speed up looking for columns selected by a given Criteria +#foreach ($columnElement in $torqueGen.getChildren("column")) + #set ( $peerColumnName = $columnElement.getAttribute("peerColumnName") ) + private static final String ${peerColumnName}_EXPRESSION = ${basePeerClassName}.${peerColumnName}.getSqlExpression(); +#end + + ## TORQUE-364: Should this be cached per RecordMapper (Thread safety/Multi query safety?) + ##private MappingStrategy<${dbObjectClassName}> strategy; + + ## TORQUE-364: init a new Strategy implementation + ## TORQUE-372: init is not Thread save cause Array ConcurrentModificationException + public MappingStrategy<${dbObjectClassName}> initStrategy() + { + #if("${useMappingStrategy}" == "true") + return new MappingStrategy<${dbObjectClassName}>(); + #else + // no MappingStrategy since useMappingStrategy not set + return null; + #end + } + + /** + * Constructs the object from the current row in the resultSet. + * + * @param resultSet the resultSet to operate on, already pointing + * to the correct row. Not null. + * @param offset a possible offset in the columns to be considered + * (if previous columns contain other objects), + * or 0 for no offset. + * @param criteria The criteria which created the result set. + * If set, the attributes to set in the data object + * are determined from the select columns in the criteria; + * if no matching column can be found, null is returned. + * If not set, all of the table's columns are read from the + * result set in the order defined in the table definition. + * + * @return the mapped object, not null. + * + * @throws TorqueException when reading fields from the RecordSet fails + * or if a Criteria is passed which contains select columns other + * than the columns in the ${name} table. + */ + public $dbObjectClassName processRow( + ResultSet resultSet, + int offset, + Criteria criteria) + throws TorqueException + { +#set ( $inheritanceBaseColumnElement = $torqueGen.getChild("inheritance-column") ) +#if ($inheritanceBaseColumnElement) +$torqueGen.mergepoint("createDbObjectInstanceFromInheritance") +#else +$torqueGen.mergepoint("createDbObjectInstance") +#end +#if ($torqueGen.booleanOption("torque.om.trackLoading")) + try + { + ${field}.setLoading(true); +#end + if (criteria == null) + { +#set ( $n = 1 ) +#foreach ($columnElement in $torqueGen.getChildren("column")) + #set ( $setter = $columnElement.getAttribute("setter") ) + #set ( $getter = $columnElement.getAttribute("getter") ) + ${field}.${setter}( + ${getter}(resultSet, offset + $n)); + #set ( $n = $n + 1 ) +#end + } + else + { + ## TORQUE-372: thread save + MappingStrategy<${dbObjectClassName}> strategyLoc= initStrategy(); + + // try to get columns to be mapped + // from criteria's select columns + int totalOffset = offset + 1; + List<Column> selectColumns = criteria.getSelectColumns(); + List<Column> columnsWithoutOffset = selectColumns.subList( + offset, + selectColumns.size()); + Set<String> columnsMapped = new HashSet<String>(); + for (Column column : columnsWithoutOffset) + { + final int nextOffset = totalOffset; ## leaking assignment +#set ( $else = "" ) +#foreach ($columnElement in $torqueGen.getChildren("column")) + #set ( $setter = $columnElement.getAttribute("setter") ) + #set ( $getter = $columnElement.getAttribute("getter") ) + #set ( $peerColumnName = $columnElement.getAttribute("peerColumnName") ) + ${else} + if (!columnsMapped.contains(${peerColumnName}_EXPRESSION ) && ${peerColumnName}_EXPRESSION.equals( + column.getSqlExpression())) + { + if (strategyLoc!=null) + { + strategyLoc.addColumn(nextOffset, + (res, inst) -> inst.${setter}( ${getter}(res, nextOffset))); + } else + { + ${field}.${setter}( ${getter}(resultSet, totalOffset)); + } + columnsMapped.add( ${peerColumnName}_EXPRESSION ); + } + #set ( $else = "else ") +#end + totalOffset++; + } + if (columnsMapped.isEmpty()) + { + log.debug("no columns to map found in criteria, " + + "returning null"); + return null; + } + if (strategyLoc!=null) + { + strategyLoc.finish($torqueGen.getChildren("column").size(), #if("${mappingStrategySort}"=="true")true#{else}false#end); + strategyLoc.execute(resultSet, $field); + } + } + ${field}.setNew(false); + ${field}.setModified(false); +#if ($torqueGen.booleanOption("torque.om.trackLoading")) + } + finally + { + ${field}.setLoading(false); + } +#end + return ${field}; + } + +$torqueGen.mergepoint("dbObjectFieldGetters") +} --------------------------------------------------------------------- To unsubscribe, e-mail: torque-dev-unsubscr...@db.apache.org For additional commands, e-mail: torque-dev-h...@db.apache.org