| Method from org.jboss.ejb.plugins.jaws.jdbc.JDBCStoreEntityCommand Detail: |
protected final boolean changed(Object current,
Object old) {
return (current == null) ? (old != null) : (old == null ? true : !current.equals(old));
}
|
public void execute(EntityEnterpriseContext ctx) {
// Check for read-only
// JF: Shouldn't this throw an exception?
if (jawsEntity.isReadOnly())
{
return;
}
ExecutionState es = new ExecutionState();
es.ctx = ctx;
es.currentState = getState(ctx);
boolean dirty = false;
boolean tuned = jawsEntity.hasTunedUpdates();
// For tuned updates, need to see which fields have changed
if (tuned)
{
es.dirtyField = new boolean[es.currentState.length];
Object[] oldState =
((JAWSPersistenceManager.PersistenceContext)ctx.getPersistenceContext()).state;
for (int i = 0; i < es.currentState.length; i++)
{
es.dirtyField[i] = changed(es.currentState[i], oldState[i]);
dirty |= es.dirtyField[i];
}
}
if (!tuned || dirty)
{
try
{
// Update db
jdbcExecute(es);
} catch (Exception e)
{
throw new EJBException("Store failed", e);
}
}
}
if the readOnly flag is specified in the xml file this won't store.
if not a tuned or untuned update is issued. |
protected String getSQL(Object argOrArgs) throws Exception {
boolean tuned = jawsEntity.hasTunedUpdates();
return tuned ? makeSQL(argOrArgs) : super.getSQL(argOrArgs);
}
Returns dynamically-generated SQL if this entity
has tuned updates, otherwise static SQL. |
protected Object handleResult(int rowsAffected,
Object argOrArgs) throws Exception {
ExecutionState es = (ExecutionState)argOrArgs;
boolean tuned = jawsEntity.hasTunedUpdates();
if (tuned)
{
// Save current state for tuned updates
JAWSPersistenceManager.PersistenceContext pCtx =
(JAWSPersistenceManager.PersistenceContext)es.ctx.getPersistenceContext();
pCtx.state = es.currentState;
}
return null;
}
|
protected String makeSQL(Object argOrArgs) {
ExecutionState es = (ExecutionState)argOrArgs; // NB: null if tuned
boolean tuned = jawsEntity.hasTunedUpdates();
StringBuffer sb = new StringBuffer(200);
sb.append("UPDATE ");
sb.append(jawsEntity.getTableName());
sb.append(" SET ");
Iterator iter = jawsEntity.getCMPFields();
int i = 0;
boolean first = true;
while (iter.hasNext())
{
CMPFieldMetaData cmpField = (CMPFieldMetaData)iter.next();
if (!tuned || es.dirtyField[i++])
{
if(first) {
first=false;
}else {
sb.append(',");
}
sb.append(cmpField.getColumnName());
sb.append("=?");
}
}
sb.append(" WHERE ");
sb.append(getPkColumnWhereList());
return sb.toString();
}
Used to create static SQL (tuned = false) or dynamic SQL (tuned = true). |
protected void setParameters(PreparedStatement stmt,
Object argOrArgs) throws Exception {
ExecutionState es = (ExecutionState)argOrArgs;
boolean tuned = jawsEntity.hasTunedUpdates();
int idx = 1;
Iterator iter = jawsEntity.getCMPFields();
int i = 0;
while (iter.hasNext())
{
CMPFieldMetaData cmpField = (CMPFieldMetaData)iter.next();
if (!tuned || es.dirtyField[i])
{
setParameter(stmt, idx++, cmpField.getJDBCType(), es.currentState[i]);
}
i++;
}
setPrimaryKeyParameters(stmt, idx, es.ctx.getId());
}
|