Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**********************************************************************
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.DNStateManager;
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.InExpression;
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.store.types.converters.TypeConversionHelper;
import org.datanucleus.util.ClassUtils;
import org.datanucleus.util.Localiser;
import org.datanucleus.util.NucleusLogger;
/**
* 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;
ClassLoaderResolver clr = 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
* @param cmd Metadata for the class whose objects we are locating
* @param clr ClassLoader resolver
*/
public LocateBulkRequest(DatastoreClass table, AbstractClassMetaData cmd, ClassLoaderResolver clr)
{
super(table);
this.cmd = cmd;
this.clr = clr;
}
protected String getStatement(DatastoreClass table, DNStateManager[] sms, boolean lock)
{
RDBMSStoreManager storeMgr = table.getStoreManager();
SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory();
ExecutionContext ec = sms[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)
{
// Add IN clause with values
SQLExpression[] readIdExprs = new SQLExpression[tenantReadIds.length];
for (int i=0;i 0)
{
str.append(", ");
}
str.append(sms[i].getInternalObjectId());
}
NucleusLogger.PERSISTENCE.debug(Localiser.msg("052223", str.toString(), table));
}
ExecutionContext ec = sms[0].getExecutionContext();
RDBMSStoreManager storeMgr = table.getStoreManager();
AbstractClassMetaData cmd = sms[0].getClassMetaData();
// Override with pessimistic lock where specified
LockMode lockType = ec.getLockManager().getLockMode(sms[0].getInternalObjectId());
boolean locked = (lockType == LockMode.LOCK_PESSIMISTIC_READ || lockType == LockMode.LOCK_PESSIMISTIC_WRITE) ?
true : ec.getSerializeReadForClass(cmd.getFullClassName());
String statement = getStatement(table, sms, 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[missingSMs.length];
for (int i=0;i exceptions = new ArrayList<>();
exceptions.add(sqle);
while ((sqle = sqle.getNextException()) != null)
{
exceptions.add(sqle);
}
throw new NucleusDataStoreException(msg, exceptions.toArray(new Throwable[exceptions.size()]));
}
}
private DNStateManager[] processResults(ResultSet rs, DNStateManager[] sms)
throws SQLException
{
List missingSMs = new ArrayList<>();
for (int i=0;i 0)
{
sm.replaceFields(unloadedMemberNums, resultFM);
}
// Load version if present and not yet set
JavaTypeMapping versionMapping = table.getSurrogateMapping(SurrogateColumnType.VERSION, false);
if (sm.getTransactionalVersion() == null && versionMapping != null)
{
VersionMetaData currentVermd = table.getVersionMetaData();
Object datastoreVersion = null;
if (currentVermd != null)
{
if (currentVermd.getMemberName() == 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 = sm.provideField(cmd.getAbsolutePositionOfMember(currentVermd.getMemberName()));
}
sm.setVersion(datastoreVersion);
}
}
}
}
if (!missingSMs.isEmpty())
{
return missingSMs.toArray(new DNStateManager[missingSMs.size()]);
}
return null;
}
}