org.datanucleus.store.rdbms.request.LocateRequest Maven / Gradle / Ivy
/**********************************************************************
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.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
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.identity.IdentityUtils;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.IdentityType;
import org.datanucleus.state.LockMode;
import org.datanucleus.state.DNStateManager;
import org.datanucleus.store.connection.ManagedConnection;
import org.datanucleus.store.rdbms.mapping.java.JavaTypeMapping;
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.sql.SQLStatement;
import org.datanucleus.store.rdbms.sql.SelectStatement;
import org.datanucleus.store.rdbms.sql.expression.InExpression;
import org.datanucleus.store.rdbms.sql.expression.NullLiteral;
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.Localiser;
import org.datanucleus.util.NucleusLogger;
/**
* Request to locate a record in the data store. Performs an SQL statement like
*
* SELECT 1 FROM CANDIDATE_TABLE WHERE ID = ?
*
* and checks if the ResultSet is empty
*/
public class LocateRequest extends Request
{
/** JDBC locate statement without locking. */
private String statementUnlocked;
/** JDBC locate statement with locking. */
private String statementLocked;
/** Definition of mappings in the SQL statement. */
private StatementClassMapping mappingDefinition;
/** ALl non-null PK members */
private int[] nonNullPkMembers = null;
/**
* 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 we are locating an instance of
* @param clr ClassLoader resolver
* @param nullPkFields Fields in PK that are null - only used if PC model has objects with nullable PK fields
*/
public LocateRequest(DatastoreClass table, AbstractClassMetaData cmd, ClassLoaderResolver clr, BitSet nullPkFields)
{
super(table);
RDBMSStoreManager storeMgr = table.getStoreManager();
SelectStatement sqlStatement = new SelectStatement(storeMgr, table, null, null);
mappingDefinition = new StatementClassMapping();
SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory();
JavaTypeMapping m = storeMgr.getMappingManager().getMapping(Integer.class);
sqlStatement.select(exprFactory.newLiteral(sqlStatement, m, 1), null);
// Add WHERE clause restricting to the identity of an object
int inputParamNum = 1;
if (table.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 (table.getIdentityType() == IdentityType.APPLICATION)
{
nonNullPkMembers = Arrays.stream(cmd.getPKMemberPositions())
.filter(pos -> nullPkFields==null || !nullPkFields.get(pos))
.toArray();
// Application identity value(s) for input
int[] pkNums = cmd.getPKMemberPositions();
for (int i=0;i 0)
{
// Hardcode the IN clause with values
SQLExpression[] readIdExprs = new SQLExpression[tenantReadIds.length];
for (int i=0;i 0)
{
// Using IN clause so nothing to do since hardcoded
}
else
{
// Set MultiTenancy tenant id in statement
StatementMappingIndex multitenancyIdx = mappingDefinition.getMappingForMemberPosition(SurrogateColumnType.MULTITENANCY.getFieldNumber());
String tenantId = ec.getTenantId();
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()]));
}
}
}
}