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

org.datanucleus.store.rdbms.request.LocateBulkRequest Maven / Gradle / Ivy

There is a newer version: 6.0.8
Show newest version
/**********************************************************************
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.PropertyNames;
import org.datanucleus.exceptions.NucleusDataStoreException;
import org.datanucleus.exceptions.NucleusObjectNotFoundException;
import org.datanucleus.exceptions.NucleusUserException;
import org.datanucleus.identity.IdentityUtils;
import org.datanucleus.identity.OID;
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.fieldmanager.FieldManager;
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.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.util.ClassUtils;
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); SQLStatement sqlStatement = new SQLStatement(storeMgr, table, null, null); // SELECT fields we require resultMapping = new StatementClassMapping(); // a). PK fields if (table.getIdentityType() == IdentityType.DATASTORE) { JavaTypeMapping datastoreIdMapping = table.getDatastoreObjectIdMapping(); SQLExpression expr = exprFactory.newExpression(sqlStatement, sqlStatement.getPrimaryTable(), datastoreIdMapping); int[] cols = sqlStatement.select(expr, null); StatementMappingIndex datastoreIdx = new StatementMappingIndex(datastoreIdMapping); datastoreIdx.setColumnPositions(cols); resultMapping.addMappingForMember(StatementClassMapping.MEMBER_DATASTORE_ID, 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()); short lockType = ec.getLockManager().getLockMode(ops[0].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 = getStatement(table, ops, locked); try { ManagedConnection mconn = storeMgr.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 if (op.getTransactionalVersion() == null && table.getVersionMapping(false) != null) { VersionMetaData currentVermd = table.getVersionMetaData(); Object datastoreVersion = null; if (currentVermd != null && currentVermd.getFieldName() == null) { // Surrogate version StatementMappingIndex verIdx = resultMapping.getMappingForMemberPosition(StatementClassMapping.MEMBER_VERSION); datastoreVersion = table.getVersionMapping(true).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