All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.datanucleus.store.rdbms.request.FetchRequest Maven / Gradle / Ivy

There is a newer version: 6.0.9
Show newest version
/**********************************************************************
Copyright (c) 2009 Andy Jefferson and others. All rights reserved.
Licensed 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.

Contributors:
    ...
**********************************************************************/
package org.datanucleus.store.rdbms.request;

import java.lang.reflect.Modifier;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.ExecutionContext;
import org.datanucleus.exceptions.NucleusDataStoreException;
import org.datanucleus.exceptions.NucleusObjectNotFoundException;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.IdentityType;
import org.datanucleus.metadata.VersionMetaData;
import org.datanucleus.state.LockMode;
import org.datanucleus.state.ObjectProvider;
import org.datanucleus.store.connection.ManagedConnection;
import org.datanucleus.store.rdbms.mapping.MappingCallbacks;
import org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping;
import org.datanucleus.store.rdbms.mapping.java.PersistableMapping;
import org.datanucleus.store.rdbms.mapping.java.ReferenceMapping;
import org.datanucleus.store.rdbms.mapping.java.SingleCollectionMapping;
import org.datanucleus.store.rdbms.query.StatementClassMapping;
import org.datanucleus.store.rdbms.query.StatementMappingIndex;
import org.datanucleus.store.rdbms.RDBMSStoreManager;
import org.datanucleus.store.rdbms.SQLController;
import org.datanucleus.store.rdbms.fieldmanager.ParameterSetter;
import org.datanucleus.store.rdbms.fieldmanager.ResultSetGetter;
import org.datanucleus.store.rdbms.sql.SQLStatement;
import org.datanucleus.store.rdbms.sql.SQLStatementHelper;
import org.datanucleus.store.rdbms.sql.SQLTable;
import org.datanucleus.store.rdbms.sql.SelectStatement;
import org.datanucleus.store.rdbms.sql.expression.SQLExpression;
import org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory;
import org.datanucleus.store.rdbms.table.AbstractClassTable;
import org.datanucleus.store.rdbms.table.DatastoreClass;
import org.datanucleus.store.schema.table.SurrogateColumnType;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;

/**
 * Class to retrieve the fields of an object of a specified class from the datastore.
 * If some of those fields are themselves persistent objects then this can optionally
 * retrieve fields of those objects in the same fetch.
 * 

* Any surrogate version stored in this table will be fetched *if* the object being updated doesn't * already have a value for it. If the caller wants the surrogate version to be updated then * they should nullify the "transactional" version before calling. *

*/ public class FetchRequest extends Request { /** JDBC fetch statement without locking. */ private String statementUnlocked; /** JDBC fetch statement with locking. */ private String statementLocked; /** Absolute numbers of the fields/properties of the class to fetch. */ private int[] memberNumbersToFetch = null; /** The mapping of the results of the SQL statement. */ private StatementClassMapping mappingDefinition; /** Callbacks for postFetch() operations, to be called after the fetch itself (relation fields). */ private final MappingCallbacks[] callbacks; private int numberOfFieldsToFetch = 0; /** Convenience string listing the fields to be fetched by this request. */ private final String fieldsToFetch; /** Whether we are fetching a surrogate version in this fetch. */ private boolean fetchingSurrogateVersion = false; /** Name of the version field. Only applies if the class has a version field (not surrogate). */ private String versionFieldName = null; /** * Constructor, taking the table. Uses the structure of the datastore table to build a basic query. * @param classTable The Class Table representing the datastore table to retrieve * @param mmds MetaData of the fields/properties to retrieve * @param cmd ClassMetaData of objects being fetched * @param clr ClassLoader resolver */ public FetchRequest(DatastoreClass classTable, AbstractMemberMetaData[] mmds, AbstractClassMetaData cmd, ClassLoaderResolver clr) { super(classTable); RDBMSStoreManager storeMgr = classTable.getStoreManager(); // Work out the real candidate table. // Instead of just taking the most derived table as the candidate we find the table closest to the root table necessary to retrieve the requested fields boolean found = false; DatastoreClass candidateTable = classTable; if (mmds != null) { while (candidateTable != null) { for (int i=0;i fetchCallbacks = new HashSet<>(); numberOfFieldsToFetch = processMembersOfClass(sqlStatement, mmds, table, sqlStatement.getPrimaryTable(), mappingDefinition, fetchCallbacks, clr); callbacks = fetchCallbacks.toArray(new MappingCallbacks[fetchCallbacks.size()]); memberNumbersToFetch = mappingDefinition.getMemberNumbers(); // Add WHERE clause restricting to an object of this type int inputParamNum = 1; SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory(); if (cmd.getIdentityType() == IdentityType.DATASTORE) { // Datastore identity value for input JavaTypeMapping datastoreIdMapping = table.getSurrogateMapping(SurrogateColumnType.DATASTORE_ID, false); SQLExpression expr = exprFactory.newExpression(sqlStatement, sqlStatement.getPrimaryTable(), datastoreIdMapping); SQLExpression val = exprFactory.newLiteralParameter(sqlStatement, datastoreIdMapping, null, "ID"); sqlStatement.whereAnd(expr.eq(val), true); StatementMappingIndex datastoreIdx = mappingDefinition.getMappingForMemberPosition(SurrogateColumnType.DATASTORE_ID.getFieldNumber()); if (datastoreIdx == null) { datastoreIdx = new StatementMappingIndex(datastoreIdMapping); mappingDefinition.addMappingForMember(SurrogateColumnType.DATASTORE_ID.getFieldNumber(), datastoreIdx); } datastoreIdx.addParameterOccurrence(new int[] {inputParamNum++}); } else if (cmd.getIdentityType() == IdentityType.APPLICATION) { // Application identity value(s) for input int[] pkNums = cmd.getPKMemberPositions(); for (int i=0;i 0) { str.append(','); } str.append(mmds[i].getName()); } } } if (fetchingSurrogateVersion) { // Add on surrogate version column if (str.length() > 0) { str.append(","); } str.append("[VERSION]"); } if (!fetchingSurrogateVersion && numberOfFieldsToFetch == 0) { fieldsToFetch = null; sqlStatement = null; mappingDefinition = null; } else { fieldsToFetch = str.toString(); // Generate the unlocked and locked JDBC statements statementUnlocked = sqlStatement.getSQLText().toSQL(); sqlStatement.addExtension(SQLStatement.EXTENSION_LOCK_FOR_UPDATE, Boolean.TRUE); statementLocked = sqlStatement.getSQLText().toSQL(); } } /* (non-Javadoc) * @see org.datanucleus.store.rdbms.request.Request#execute(org.datanucleus.state.ObjectProvider) */ public void execute(ObjectProvider op) { if (fieldsToFetch != null && NucleusLogger.PERSISTENCE.isDebugEnabled()) { // Debug information about what we are retrieving NucleusLogger.PERSISTENCE.debug(Localiser.msg("052218", op.getObjectAsPrintable(), fieldsToFetch, table)); } if (((fetchingSurrogateVersion || versionFieldName != null) && numberOfFieldsToFetch == 0) && op.isVersionLoaded()) { // Fetching only the version and it is already loaded, so do nothing } else if (statementLocked != null) { ExecutionContext ec = op.getExecutionContext(); RDBMSStoreManager storeMgr = table.getStoreManager(); boolean locked = ec.getSerializeReadForClass(op.getClassMetaData().getFullClassName()); LockMode lockType = ec.getLockManager().getLockMode(op.getInternalObjectId()); if (lockType != LockMode.LOCK_NONE) { if (lockType == LockMode.LOCK_PESSIMISTIC_READ || lockType == LockMode.LOCK_PESSIMISTIC_WRITE) { // Override with pessimistic lock locked = true; } } String statement = (locked ? statementLocked : statementUnlocked); StatementClassMapping mappingDef = mappingDefinition; /*if ((sm.isDeleting() || sm.isDetaching()) && mappingDefinition.hasChildMappingDefinitions()) { // Don't fetch any children since the object is being deleted mappingDef = mappingDefinition.cloneStatementMappingWithoutChildren(); }*/ try { ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec); SQLController sqlControl = storeMgr.getSQLController(); try { PreparedStatement ps = sqlControl.getStatementForQuery(mconn, statement); AbstractClassMetaData cmd = op.getClassMetaData(); try { // Provide the primary key field(s) to the JDBC statement if (cmd.getIdentityType() == IdentityType.DATASTORE) { StatementMappingIndex datastoreIdx = mappingDef.getMappingForMemberPosition(SurrogateColumnType.DATASTORE_ID.getFieldNumber()); for (int i=0;i 0) { depth = 0; } } } } else if (mappingToUse instanceof ReferenceMapping) { ReferenceMapping refMapping = (ReferenceMapping)mappingToUse; if (refMapping.getMappingStrategy() == ReferenceMapping.PER_IMPLEMENTATION_MAPPING) { JavaTypeMapping[] subMappings = refMapping.getJavaTypeMapping(); if (subMappings != null && subMappings.length == 1) { // Support special case of reference mapping with single implementation possible depth = 1; } } } // TODO We should use the actual FetchPlan, and the max fetch depth, so then it can pull in all related objects within reach. // But this will mean we cannot cache the statement, since it is for a specific ExecutionContext // TODO If this field is a 1-1 and the other side has a discriminator or version then we really ought to fetch it SQLStatementHelper.selectMemberOfSourceInStatement(sqlStatement, mappingDef, null, sqlTbl, mmd, clr, depth, null); number++; } if (mapping instanceof MappingCallbacks) { // TODO Need to add that this mapping is for base object or base.field1, etc fetchCallbacks.add(mapping); } } } } JavaTypeMapping versionMapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, true); if (versionMapping != null) { // Select version StatementMappingIndex verMapIdx = new StatementMappingIndex(versionMapping); SQLTable verSqlTbl = SQLStatementHelper.getSQLTableForMappingOfTable(sqlStatement, sqlTbl, versionMapping); int[] cols = sqlStatement.select(verSqlTbl, versionMapping, null); verMapIdx.setColumnPositions(cols); mappingDef.addMappingForMember(SurrogateColumnType.VERSION.getFieldNumber(), verMapIdx); } return number; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy