org.datanucleus.store.rdbms.request.FetchRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datanucleus-rdbms Show documentation
Show all versions of datanucleus-rdbms Show documentation
Plugin for DataNucleus providing persistence to RDBMS datastores.
/**********************************************************************
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.PropertyNames;
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.LockManager;
import org.datanucleus.state.ObjectProvider;
import org.datanucleus.store.connection.ManagedConnection;
import org.datanucleus.store.rdbms.mapping.MappingCallbacks;
import org.datanucleus.store.rdbms.mapping.StatementClassMapping;
import org.datanucleus.store.rdbms.mapping.StatementMappingIndex;
import org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping;
import org.datanucleus.store.rdbms.mapping.java.PersistableMapping;
import org.datanucleus.store.rdbms.RDBMSStoreManager;
import org.datanucleus.store.rdbms.SQLController;
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.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.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.getDatastoreIdMapping();
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(StatementClassMapping.MEMBER_DATASTORE_ID);
if (datastoreIdx == null)
{
datastoreIdx = new StatementMappingIndex(datastoreIdMapping);
mappingDefinition.addMappingForMember(StatementClassMapping.MEMBER_DATASTORE_ID, 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.getSelectStatement().toSQL();
sqlStatement.addExtension("lock-for-update", Boolean.TRUE);
statementLocked = sqlStatement.getSelectStatement().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 (isFetchingVersionOnly() && isVersionLoaded(op))
{
// Don't fetch if we have only the version to fetch and it is already loaded
// Debug comment until we're sure this is a valid thing to do
}
else if (statementLocked != null)
{
ExecutionContext ec = op.getExecutionContext();
RDBMSStoreManager storeMgr = table.getStoreManager();
boolean locked = ec.getSerializeReadForClass(op.getClassMetaData().getFullClassName());
short lockType = ec.getLockManager().getLockMode(op.getInternalObjectId());
if (lockType != LockManager.LOCK_MODE_NONE)
{
if (lockType == LockManager.LOCK_MODE_PESSIMISTIC_READ ||
lockType == LockManager.LOCK_MODE_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.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(
StatementClassMapping.MEMBER_DATASTORE_ID);
for (int i=0;i 0)
{
depth = 0;
}
}
}
}
SQLStatementHelper.selectMemberOfSourceInStatement(sqlStatement, mappingDef, null,
sqlTbl, mmd, clr, depth);
number++;
}
if (mapping instanceof MappingCallbacks)
{
// TODO Need to add that this mapping is for base object or base.field1, etc
fetchCallbacks.add(mapping);
}
}
}
}
JavaTypeMapping verMapping = table.getVersionMapping(true);
if (verMapping != null)
{
// Select version
StatementMappingIndex verMapIdx = new StatementMappingIndex(verMapping);
SQLTable verSqlTbl = SQLStatementHelper.getSQLTableForMappingOfTable(sqlStatement, sqlTbl,
verMapping);
int[] cols = sqlStatement.select(verSqlTbl, verMapping, null);
verMapIdx.setColumnPositions(cols);
mappingDef.addMappingForMember(StatementClassMapping.MEMBER_VERSION, verMapIdx);
}
return number;
}
}