String addStmt = getAddStmt(ecs);
try
{
ExecutionContext ec = sm.getExecutionContext();
ManagedConnection mconn = ecs.getStoreManager().getConnection(ec);
SQLController sqlControl = storeMgr.getSQLController();
try
{
// Shift any existing elements so that we can insert the new element(s) at their position
if (!atEnd && start != currentListSize)
{
boolean batched = currentListSize - start > 0;
for (int i = currentListSize - 1; i >= start; i--)
{
// Shift the index for this row by "shift"
internalShift(sm, mconn, batched, i, shift, (i == start), ecs);
}
}
else
{
start = currentListSize;
}
// Insert the elements at their required location
int jdbcPosition = 1;
boolean batched = (c.size() > 1);
Iterator iter = c.iterator();
while (iter.hasNext())
{
Object element = iter.next();
PreparedStatement ps = sqlControl.getStatementForUpdate(mconn, addStmt, batched);
try
{
JavaTypeMapping orderMapping = ecs.getOrderMapping();
JavaTypeMapping elementMapping = ecs.getElementMapping();
JavaTypeMapping relationDiscriminatorMapping = ecs.getRelationDiscriminatorMapping();
jdbcPosition = 1;
jdbcPosition = BackingStoreHelper.populateOwnerInStatement(sm, ec, ps, jdbcPosition, ecs);
jdbcPosition = BackingStoreHelper.populateElementInStatement(ec, ps, element, jdbcPosition, elementMapping);
if (orderMapping != null)
{
jdbcPosition = BackingStoreHelper.populateOrderInStatement(ec, ps, start, jdbcPosition, orderMapping);
}
if (relationDiscriminatorMapping != null)
{
jdbcPosition =
BackingStoreHelper.populateRelationDiscriminatorInStatement(ec, ps, jdbcPosition, ecs);
}
start++;
// Execute the statement
sqlControl.executeStatementUpdate(mconn, addStmt, ps, !iter.hasNext());
}
finally
{
sqlControl.closeStatement(mconn, ps);
}
}
}
finally
{
mconn.release();
}
}
catch (MappedDatastoreException e)
{
throw new NucleusDataStoreException(localiser.msg("056009", addStmt), e);