org.datanucleus.store.rdbms.request.LocateBulkRequest 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) 2012 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.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.datanucleus.ClassLoaderResolver;
import org.datanucleus.ExecutionContext;
import org.datanucleus.exceptions.NucleusDataStoreException;
import org.datanucleus.exceptions.NucleusException;
import org.datanucleus.exceptions.NucleusObjectNotFoundException;
import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.identity.IdentityUtils;
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.fieldmanager.FieldManager;
import org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping;
import org.datanucleus.store.rdbms.mapping.java.PersistableMapping;
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.SelectStatement;
import org.datanucleus.store.rdbms.sql.expression.BooleanExpression;
import org.datanucleus.store.rdbms.sql.expression.SQLExpression;
import org.datanucleus.store.rdbms.sql.expression.SQLExpressionFactory;
import org.datanucleus.store.rdbms.table.DatastoreClass;
import org.datanucleus.store.schema.table.SurrogateColumnType;
import org.datanucleus.util.ClassUtils;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;
import org.datanucleus.util.TypeConversionHelper;
/**
* Request to locate a series of records in the data store (all present in the same table).
* Performs an SQL statement like
*
* SELECT ID [,FIELD1,FIELD2] FROM CANDIDATE_TABLE WHERE ID = ? OR ID = ? OR ID = ?
*
*/
public class LocateBulkRequest extends BulkRequest
{
AbstractClassMetaData cmd = null;
/** Definition of input mappings in the SQL statement. */
private StatementClassMapping[] mappingDefinitions;
/** Result mapping for the SQL statement. */
private StatementClassMapping resultMapping;
/**
* Constructor, taking the table. Uses the structure of the datastore table to build a basic query.
* @param table The Class Table representing the datastore table to retrieve
*/
public LocateBulkRequest(DatastoreClass table)
{
super(table);
}
protected String getStatement(DatastoreClass table, ObjectProvider[] ops, boolean lock)
{
RDBMSStoreManager storeMgr = table.getStoreManager();
ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null);
SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory();
cmd = storeMgr.getMetaDataManager().getMetaDataForClass(table.getType(), clr);
ExecutionContext ec = ops[0].getExecutionContext();
SelectStatement sqlStatement = new SelectStatement(storeMgr, table, null, null);
// SELECT fields we require
resultMapping = new StatementClassMapping();
// a). PK fields
if (table.getIdentityType() == IdentityType.DATASTORE)
{
JavaTypeMapping datastoreIdMapping = table.getSurrogateMapping(SurrogateColumnType.DATASTORE_ID, false);
SQLExpression expr = exprFactory.newExpression(sqlStatement, sqlStatement.getPrimaryTable(), datastoreIdMapping);
int[] cols = sqlStatement.select(expr, null);
StatementMappingIndex datastoreIdx = new StatementMappingIndex(datastoreIdMapping);
datastoreIdx.setColumnPositions(cols);
resultMapping.addMappingForMember(SurrogateColumnType.DATASTORE_ID.getFieldNumber(), datastoreIdx);
}
else if (table.getIdentityType() == IdentityType.APPLICATION)
{
int[] pkNums = cmd.getPKMemberPositions();
for (int i=0;i 0)
{
str.append(", ");
}
str.append(ops[i].getInternalObjectId());
}
NucleusLogger.PERSISTENCE.debug(Localiser.msg("052223", str.toString(), table));
}
ExecutionContext ec = ops[0].getExecutionContext();
RDBMSStoreManager storeMgr = table.getStoreManager();
AbstractClassMetaData cmd = ops[0].getClassMetaData();
boolean locked = ec.getSerializeReadForClass(cmd.getFullClassName());
LockMode lockType = ec.getLockManager().getLockMode(ops[0].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 = getStatement(table, ops, locked);
try
{
ManagedConnection mconn = storeMgr.getConnectionManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try
{
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, statement);
try
{
// Provide the primary key field(s)
for (int i=0;i 0)
{
NucleusObjectNotFoundException[] nfes = new NucleusObjectNotFoundException[missingOps.length];
for (int i=0;i missingOps = new ArrayList<>();
for (int i=0;i 0)
{
op.replaceFields(unloadedMemberNums, resultFM);
}
// Load version if present and not yet set
JavaTypeMapping versionMapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, false);
if (op.getTransactionalVersion() == null && versionMapping != null)
{
VersionMetaData currentVermd = table.getVersionMetaData();
Object datastoreVersion = null;
if (currentVermd != null)
{
if (currentVermd.getFieldName() == null)
{
// Surrogate version
versionMapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, true); // Why use true now?
StatementMappingIndex verIdx = resultMapping.getMappingForMemberPosition(SurrogateColumnType.VERSION.getFieldNumber());
datastoreVersion = versionMapping.getObject(ec, rs, verIdx.getColumnPositions());
}
else
{
datastoreVersion = op.provideField(cmd.getAbsolutePositionOfMember(currentVermd.getFieldName()));
}
op.setVersion(datastoreVersion);
}
}
}
}
if (!missingOps.isEmpty())
{
return missingOps.toArray(new ObjectProvider[missingOps.size()]);
}
return null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy