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

org.datanucleus.store.rdbms.request.LocateRequest 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.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.NucleusObjectNotFoundException;
import org.datanucleus.metadata.AbstractClassMetaData;
import org.datanucleus.metadata.AbstractMemberMetaData;
import org.datanucleus.metadata.IdentityType;
import org.datanucleus.state.LockMode;
import org.datanucleus.state.ObjectProvider;
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.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; /** * 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 LocateRequest(DatastoreClass table) { super(table); RDBMSStoreManager storeMgr = table.getStoreManager(); ClassLoaderResolver clr = storeMgr.getNucleusContext().getClassLoaderResolver(null); 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 AbstractClassMetaData cmd = storeMgr.getMetaDataManager().getMetaDataForClass(table.getType(), clr); 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) { // Application identity value(s) for input int[] pkNums = cmd.getPKMemberPositions(); for (int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy