Capturing reference to the Column[] at the creation time in the RowImpl instead of the Table, to work with DynamicSchemaFactory where the Table's columns can get modified after the creation of the RowImpl before the flush.

git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@509473 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Srinivasa Segu 2007-02-20 08:59:10 +00:00
parent 09e7aab777
commit 4f102a7c77
4 changed files with 52 additions and 37 deletions

View File

@ -223,7 +223,11 @@ public class StateComparisonVersionStrategy
implements RowManager { implements RowManager {
private CustomUpdate(Table table) { private CustomUpdate(Table table) {
super(table, Row.ACTION_UPDATE); this(table.getColumns());
}
private CustomUpdate(Column[] cols) {
super(cols, Row.ACTION_UPDATE);
} }
/** /**
@ -252,8 +256,8 @@ public class StateComparisonVersionStrategy
return buf.toString(); return buf.toString();
} }
protected RowImpl newInstance(Table table, int action) { protected RowImpl newInstance(Column[] cols, int action) {
return new CustomUpdate(table); return new CustomUpdate(cols);
} }
///////////////////////////// /////////////////////////////

View File

@ -59,7 +59,11 @@ public class PrimaryRow
* Constructor; supply table and action. * Constructor; supply table and action.
*/ */
public PrimaryRow(Table table, int action, OpenJPAStateManager owner) { public PrimaryRow(Table table, int action, OpenJPAStateManager owner) {
super(table, action); this(table.getColumns(), action, owner);
}
protected PrimaryRow(Column[] cols, int action, OpenJPAStateManager owner) {
super(cols, action);
_pk = owner; _pk = owner;
} }
@ -385,8 +389,8 @@ public class PrimaryRow
return super.generateSQL(dict); return super.generateSQL(dict);
} }
protected RowImpl newInstance(Table table, int action) { protected RowImpl newInstance(Column[] cols, int action) {
return new PrimaryRow(table, action, _pk); return new PrimaryRow(cols, action, _pk);
} }
public void copyInto(RowImpl row, boolean whereOnly) { public void copyInto(RowImpl row, boolean whereOnly) {

View File

@ -59,7 +59,7 @@ public class RowImpl
private static final int RAW = Integer.MIN_VALUE; private static final int RAW = Integer.MIN_VALUE;
protected byte flags = 0; protected byte flags = 0;
private final Table _table; private final Column[] _cols;
private final int _action; private final int _action;
private final Object[] _vals; private final Object[] _vals;
private final int[] _types; private final int[] _types;
@ -73,12 +73,16 @@ public class RowImpl
* @param action the action on the row * @param action the action on the row
*/ */
public RowImpl(Table table, int action) { public RowImpl(Table table, int action) {
_table = table; this(table.getColumns(), action);
}
protected RowImpl(Column[] cols, int action) {
_cols = cols;
_action = action; _action = action;
// we need room for values and types for all columns; if an update or // we need room for values and types for all columns; if an update or
// delete, then we need to double that for where column conditions // delete, then we need to double that for where column conditions
int len = table.getColumns().length; int len = _cols.length;
if (action != ACTION_INSERT) if (action != ACTION_INSERT)
len *= 2; len *= 2;
_vals = new Object[len]; _vals = new Object[len];
@ -86,7 +90,11 @@ public class RowImpl
} }
public Table getTable() { public Table getTable() {
return _table; return _cols[0].getTable();
}
public Column[] getColumns() {
return _cols;
} }
public int getAction() { public int getAction() {
@ -168,7 +176,7 @@ public class RowImpl
boolean set) boolean set)
throws SQLException { throws SQLException {
ClassMapping mapping = (ClassMapping) sm.getMetaData(); ClassMapping mapping = (ClassMapping) sm.getMetaData();
while (mapping.getTable() != _table) while (mapping.getTable() != getTable())
mapping = mapping.getPCSuperclassMapping(); mapping = mapping.getPCSuperclassMapping();
Column[] cols = mapping.getPrimaryKeyColumns(); Column[] cols = mapping.getPrimaryKeyColumns();
flushJoinValues(sm, cols, cols, io, set); flushJoinValues(sm, cols, cols, io, set);
@ -730,15 +738,14 @@ public class RowImpl
buf.append("UPDATE ").append(dict.getFullName(getTable(), false)). buf.append("UPDATE ").append(dict.getFullName(getTable(), false)).
append(" SET "); append(" SET ");
Column[] cols = getTable().getColumns();
boolean hasVal = false; boolean hasVal = false;
for (int i = 0; i < cols.length; i++) { for (int i = 0; i < _cols.length; i++) {
if (_vals[i] == null) if (_vals[i] == null)
continue; continue;
if (hasVal) if (hasVal)
buf.append(", "); buf.append(", ");
buf.append(cols[i]); buf.append(_cols[i]);
if (_types[i] == RAW) if (_types[i] == RAW)
buf.append(" = ").append(_vals[i]); buf.append(" = ").append(_vals[i]);
else else
@ -759,9 +766,8 @@ public class RowImpl
buf.append("INSERT INTO "). buf.append("INSERT INTO ").
append(dict.getFullName(getTable(), false)).append(" ("); append(dict.getFullName(getTable(), false)).append(" (");
Column[] cols = getTable().getColumns();
boolean hasVal = false; boolean hasVal = false;
for (int i = 0; i < cols.length; i++) { for (int i = 0; i < _cols.length; i++) {
if (_vals[i] == null) if (_vals[i] == null)
continue; continue;
@ -769,7 +775,7 @@ public class RowImpl
buf.append(", "); buf.append(", ");
vals.append(", "); vals.append(", ");
} }
buf.append(cols[i]); buf.append(_cols[i]);
if (_types[i] == RAW) if (_types[i] == RAW)
vals.append(_vals[i]); vals.append(_vals[i]);
else else
@ -796,10 +802,9 @@ public class RowImpl
* Appends the where clause onto the given sql buffer. * Appends the where clause onto the given sql buffer.
*/ */
private void appendWhere(StringBuffer buf) { private void appendWhere(StringBuffer buf) {
Column[] cols = getTable().getColumns();
boolean hasWhere = false; boolean hasWhere = false;
for (int i = 0; i < cols.length; i++) { for (int i = 0; i < _cols.length; i++) {
if (_vals[getWhereIndex(cols[i])] == null) if (_vals[getWhereIndex(_cols[i])] == null)
continue; continue;
if (!hasWhere) if (!hasWhere)
@ -809,12 +814,12 @@ public class RowImpl
// sqlserver seems to have problems using null parameters in the // sqlserver seems to have problems using null parameters in the
// where clause // where clause
if (_vals[getWhereIndex(cols[i])] == NULL) if (_vals[getWhereIndex(_cols[i])] == NULL)
buf.append(cols[i]).append(" IS NULL"); buf.append(_cols[i]).append(" IS NULL");
else if (_types[i] == RAW) else if (_types[i] == RAW)
buf.append(cols[i]).append(" = ").append(_vals[i]); buf.append(_cols[i]).append(" = ").append(_vals[i]);
else else
buf.append(cols[i]).append(" = ?"); buf.append(_cols[i]).append(" = ?");
hasWhere = true; hasWhere = true;
} }
} }
@ -841,14 +846,13 @@ public class RowImpl
public void flush(PreparedStatement stmnt, int idx, DBDictionary dict, public void flush(PreparedStatement stmnt, int idx, DBDictionary dict,
JDBCStore store) JDBCStore store)
throws SQLException { throws SQLException {
Column[] cols = getTable().getColumns();
// this simple method works because the SQL is always prepared // this simple method works because the SQL is always prepared
// based on the indexing of the columns in the table object -- the // based on the indexing of the columns in the table object -- the
// same ordering we use when storing values and meta types. skip // same ordering we use when storing values and meta types. skip
// updates when setting params for DELETEs; the updates are just there // updates when setting params for DELETEs; the updates are just there
// to let us eval fk constraints // to let us eval fk constraints
int i = (getAction() == ACTION_DELETE) ? cols.length : 0; int i = (getAction() == ACTION_DELETE) ? _cols.length: 0;
Column col; Column col;
Object val; Object val;
int half = _vals.length / 2; int half = _vals.length / 2;
@ -863,10 +867,10 @@ public class RowImpl
// if this is an update the vals array will be 2 x the cols // if this is an update the vals array will be 2 x the cols
// array length; it repeats for where values // array length; it repeats for where values
if (i < cols.length) if (i < _cols.length)
col = cols[i]; col = _cols[i];
else else
col = cols[i - cols.length]; col = _cols[i - _cols.length];
val = _vals[i]; val = _vals[i];
if (val == NULL) if (val == NULL)
@ -890,14 +894,14 @@ public class RowImpl
* The array value array index for the given column's value. * The array value array index for the given column's value.
*/ */
private int getWhereIndex(Column col) { private int getWhereIndex(Column col) {
return col.getIndex() + getTable().getColumns().length; return col.getIndex() + _cols.length;
} }
/** /**
* Performs a proper deep clone. * Performs a proper deep clone.
*/ */
public Object clone() { public Object clone() {
RowImpl clone = newInstance(getTable(), getAction()); RowImpl clone = newInstance(getColumns(), getAction());
copyInto(clone, false); copyInto(clone, false);
return clone; return clone;
} }
@ -905,8 +909,8 @@ public class RowImpl
/** /**
* Return a new row. * Return a new row.
*/ */
protected RowImpl newInstance(Table table, int action) { protected RowImpl newInstance(Column[] cols, int action) {
return new RowImpl(table, action); return new RowImpl(cols, action);
} }
/** /**
@ -939,4 +943,3 @@ public class RowImpl
row.setValid(true); row.setValid(true);
} }
} }

View File

@ -43,7 +43,11 @@ public class SecondaryRow
* Constructor; supply table and action. * Constructor; supply table and action.
*/ */
public SecondaryRow(Table table, int action) { public SecondaryRow(Table table, int action) {
super(table, action); this(table.getColumns(), action);
}
protected SecondaryRow(Column[] cols, int action) {
super(cols, action);
} }
public void setForeignKey(ForeignKey fk, OpenJPAStateManager sm) public void setForeignKey(ForeignKey fk, OpenJPAStateManager sm)
@ -161,8 +165,8 @@ public class SecondaryRow
return super.generateSQL(dict); return super.generateSQL(dict);
} }
protected RowImpl newInstance(Table table, int action) { protected RowImpl newInstance(Column[] cols, int action) {
return new SecondaryRow(table, action); return new SecondaryRow(cols, action);
} }
public void copyInto(RowImpl row, boolean whereOnly) { public void copyInto(RowImpl row, boolean whereOnly) {