@SuppressWarnings("deprecation")
public static void updateBlobColumn(ObjectProvider sm, DatastoreContainerObject datastoreContainer,
DatastoreMapping mapping, byte[] bytes)
{
ExecutionContext ec = sm.getExecutionContext();
RDBMSStoreManager storeMgr = (RDBMSStoreManager)ec.getStoreManager();
DatastoreClass classTable = (DatastoreClass)datastoreContainer; // Don't support join tables yet
SQLExpressionFactory exprFactory = storeMgr.getSQLExpressionFactory();
// Generate "SELECT {blobColumn} FROM TABLE WHERE ID=? FOR UPDATE" statement
SQLStatement sqlStmt = new SQLStatement(storeMgr, datastoreContainer, null, null);
sqlStmt.setClassLoaderResolver(ec.getClassLoaderResolver());
sqlStmt.addExtension("lock-for-update", true);
SQLTable blobSqlTbl = SQLStatementHelper.getSQLTableForMappingOfTable(sqlStmt, sqlStmt.getPrimaryTable(), mapping.getJavaTypeMapping());
sqlStmt.select(blobSqlTbl, mapping.getDatastoreField(), null);
StatementClassMapping mappingDefinition = new StatementClassMapping();
AbstractClassMetaData cmd = sm.getClassMetaData();
int inputParamNum = 1;
if (cmd.getIdentityType() == IdentityType.DATASTORE)
{
// Datastore identity value for input
JavaTypeMapping datastoreIdMapping = classTable.getDatastoreObjectIdMapping();
SQLExpression expr = exprFactory.newExpression(sqlStmt, sqlStmt.getPrimaryTable(),
datastoreIdMapping);
SQLExpression val = exprFactory.newLiteralParameter(sqlStmt, datastoreIdMapping, null, "ID");
sqlStmt.whereAnd(expr.eq(val), true);
StatementMappingIndex datastoreIdx = mappingDefinition.getMappingForMemberPosition(StatementClassMapping.MEMBER_DATASTORE_ID);
if (datastoreIdx == null)
{
datastoreIdx = new StatementMappingIndex(datastoreIdMapping);
mappingDefinition.addMappingForMember(StatementClassMapping.MEMBER_DATASTORE_ID, datastoreIdx);
}
datastoreIdx.addParameterOccurrence(new int[] {inputParamNum});
}
else if (cmd.getIdentityType() == IdentityType.APPLICATION)
{
// Application identity value(s) for input
int[] pkNums = cmd.getPKMemberPositions();
for (int i=0;i<pkNums.length;i++)
{
AbstractMemberMetaData mmd = cmd.getMetaDataForManagedMemberAtAbsolutePosition(pkNums[i]);
JavaTypeMapping pkMapping = classTable.getMemberMapping(mmd);
SQLExpression expr = exprFactory.newExpression(sqlStmt, sqlStmt.getPrimaryTable(),
pkMapping);
SQLExpression val = exprFactory.newLiteralParameter(sqlStmt, pkMapping, null, "PK" + i);
sqlStmt.whereAnd(expr.eq(val), true);
StatementMappingIndex pkIdx = mappingDefinition.getMappingForMemberPosition(pkNums[i]);
if (pkIdx == null)
{
pkIdx = new StatementMappingIndex(pkMapping);
mappingDefinition.addMappingForMember(pkNums[i], pkIdx);
}
int[] inputParams = new int[pkMapping.getNumberOfDatastoreMappings()];
for (int j=0;j<pkMapping.getNumberOfDatastoreMappings();j++)
{
inputParams[j] = inputParamNum++;
}
pkIdx.addParameterOccurrence(inputParams);
}
}
String textStmt = sqlStmt.getSelectStatement().toSQL();
if (sm.isEmbedded())
{
// This mapping is embedded, so navigate back to the real owner since that is the "id" in the table
ObjectProvider[] embeddedOwners = sm.getEmbeddedOwners();
if (embeddedOwners != null)
{
// Just use the first owner
// TODO Should check if the owner is stored in this table
sm = embeddedOwners[0];
}
}
try
{
ManagedConnection mconn = storeMgr.getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try
{
PreparedStatement ps = sqlControl.getStatementForQuery(mconn, textStmt);
try
{
// Provide the primary key field(s) to the JDBC statement
if (cmd.getIdentityType() == IdentityType.DATASTORE)
{
StatementMappingIndex datastoreIdx = mappingDefinition.getMappingForMemberPosition(
StatementClassMapping.MEMBER_DATASTORE_ID);
for (int i=0;i<datastoreIdx.getNumberOfParameterOccurrences();i++)
{
classTable.getDatastoreObjectIdMapping().setObject(ec, ps,
datastoreIdx.getParameterPositionsForOccurrence(i), sm.getInternalObjectId());
}
}
else if (cmd.getIdentityType() == IdentityType.APPLICATION)
{
sm.provideFields(cmd.getPKMemberPositions(),
storeMgr.getFieldManagerForStatementGeneration(sm, ps, mappingDefinition, false));
}
ResultSet rs = sqlControl.executeStatementQuery(mconn, textStmt, ps);
try
{
if (!rs.next())
{
throw new NucleusObjectNotFoundException("No such database row", sm.getInternalObjectId());
}
int jdbcMajorVersion = ((DatabaseAdapter)storeMgr.getDatastoreAdapter()).getDriverMajorVersion();
if (jdbcMajorVersion < 10)
{
// Oracle JDBC drivers version 9 and below use some sh*tty Oracle-specific BLOB type
// we have to cast to that, face west, pray whilst saying ommmmmmmmmmm
oracle.sql.BLOB blob = null;