From 4f102a7c77a321699663e9fba7449725e52d86ba Mon Sep 17 00:00:00 2001 From: Srinivasa Segu Date: Tue, 20 Feb 2007 08:59:10 +0000 Subject: [PATCH] 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 --- .../StateComparisonVersionStrategy.java | 10 +++- .../apache/openjpa/jdbc/sql/PrimaryRow.java | 10 +++- .../org/apache/openjpa/jdbc/sql/RowImpl.java | 59 ++++++++++--------- .../apache/openjpa/jdbc/sql/SecondaryRow.java | 10 +++- 4 files changed, 52 insertions(+), 37 deletions(-) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StateComparisonVersionStrategy.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StateComparisonVersionStrategy.java index 0e2b4a598..e168ecc66 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StateComparisonVersionStrategy.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/meta/strats/StateComparisonVersionStrategy.java @@ -223,7 +223,11 @@ public class StateComparisonVersionStrategy implements RowManager { 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(); } - protected RowImpl newInstance(Table table, int action) { - return new CustomUpdate(table); + protected RowImpl newInstance(Column[] cols, int action) { + return new CustomUpdate(cols); } ///////////////////////////// diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java index dda803669..71a9c619b 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/PrimaryRow.java @@ -59,7 +59,11 @@ public class PrimaryRow * Constructor; supply table and action. */ 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; } @@ -385,8 +389,8 @@ public class PrimaryRow return super.generateSQL(dict); } - protected RowImpl newInstance(Table table, int action) { - return new PrimaryRow(table, action, _pk); + protected RowImpl newInstance(Column[] cols, int action) { + return new PrimaryRow(cols, action, _pk); } public void copyInto(RowImpl row, boolean whereOnly) { diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/RowImpl.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/RowImpl.java index 38180c595..ac0808458 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/RowImpl.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/RowImpl.java @@ -59,7 +59,7 @@ public class RowImpl private static final int RAW = Integer.MIN_VALUE; protected byte flags = 0; - private final Table _table; + private final Column[] _cols; private final int _action; private final Object[] _vals; private final int[] _types; @@ -73,12 +73,16 @@ public class RowImpl * @param action the action on the row */ public RowImpl(Table table, int action) { - _table = table; + this(table.getColumns(), action); + } + + protected RowImpl(Column[] cols, int action) { + _cols = cols; _action = action; // 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 - int len = table.getColumns().length; + int len = _cols.length; if (action != ACTION_INSERT) len *= 2; _vals = new Object[len]; @@ -86,7 +90,11 @@ public class RowImpl } public Table getTable() { - return _table; + return _cols[0].getTable(); + } + + public Column[] getColumns() { + return _cols; } public int getAction() { @@ -168,7 +176,7 @@ public class RowImpl boolean set) throws SQLException { ClassMapping mapping = (ClassMapping) sm.getMetaData(); - while (mapping.getTable() != _table) + while (mapping.getTable() != getTable()) mapping = mapping.getPCSuperclassMapping(); Column[] cols = mapping.getPrimaryKeyColumns(); flushJoinValues(sm, cols, cols, io, set); @@ -730,15 +738,14 @@ public class RowImpl buf.append("UPDATE ").append(dict.getFullName(getTable(), false)). append(" SET "); - Column[] cols = getTable().getColumns(); boolean hasVal = false; - for (int i = 0; i < cols.length; i++) { + for (int i = 0; i < _cols.length; i++) { if (_vals[i] == null) continue; if (hasVal) buf.append(", "); - buf.append(cols[i]); + buf.append(_cols[i]); if (_types[i] == RAW) buf.append(" = ").append(_vals[i]); else @@ -759,9 +766,8 @@ public class RowImpl buf.append("INSERT INTO "). append(dict.getFullName(getTable(), false)).append(" ("); - Column[] cols = getTable().getColumns(); boolean hasVal = false; - for (int i = 0; i < cols.length; i++) { + for (int i = 0; i < _cols.length; i++) { if (_vals[i] == null) continue; @@ -769,7 +775,7 @@ public class RowImpl buf.append(", "); vals.append(", "); } - buf.append(cols[i]); + buf.append(_cols[i]); if (_types[i] == RAW) vals.append(_vals[i]); else @@ -796,10 +802,9 @@ public class RowImpl * Appends the where clause onto the given sql buffer. */ private void appendWhere(StringBuffer buf) { - Column[] cols = getTable().getColumns(); boolean hasWhere = false; - for (int i = 0; i < cols.length; i++) { - if (_vals[getWhereIndex(cols[i])] == null) + for (int i = 0; i < _cols.length; i++) { + if (_vals[getWhereIndex(_cols[i])] == null) continue; if (!hasWhere) @@ -809,12 +814,12 @@ public class RowImpl // sqlserver seems to have problems using null parameters in the // where clause - if (_vals[getWhereIndex(cols[i])] == NULL) - buf.append(cols[i]).append(" IS NULL"); + if (_vals[getWhereIndex(_cols[i])] == NULL) + buf.append(_cols[i]).append(" IS NULL"); else if (_types[i] == RAW) - buf.append(cols[i]).append(" = ").append(_vals[i]); + buf.append(_cols[i]).append(" = ").append(_vals[i]); else - buf.append(cols[i]).append(" = ?"); + buf.append(_cols[i]).append(" = ?"); hasWhere = true; } } @@ -841,14 +846,13 @@ public class RowImpl public void flush(PreparedStatement stmnt, int idx, DBDictionary dict, JDBCStore store) throws SQLException { - Column[] cols = getTable().getColumns(); // this simple method works because the SQL is always prepared // based on the indexing of the columns in the table object -- the // same ordering we use when storing values and meta types. skip // updates when setting params for DELETEs; the updates are just there // to let us eval fk constraints - int i = (getAction() == ACTION_DELETE) ? cols.length : 0; + int i = (getAction() == ACTION_DELETE) ? _cols.length: 0; Column col; Object val; 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 // array length; it repeats for where values - if (i < cols.length) - col = cols[i]; + if (i < _cols.length) + col = _cols[i]; else - col = cols[i - cols.length]; + col = _cols[i - _cols.length]; val = _vals[i]; if (val == NULL) @@ -890,14 +894,14 @@ public class RowImpl * The array value array index for the given column's value. */ private int getWhereIndex(Column col) { - return col.getIndex() + getTable().getColumns().length; + return col.getIndex() + _cols.length; } /** * Performs a proper deep clone. */ public Object clone() { - RowImpl clone = newInstance(getTable(), getAction()); + RowImpl clone = newInstance(getColumns(), getAction()); copyInto(clone, false); return clone; } @@ -905,8 +909,8 @@ public class RowImpl /** * Return a new row. */ - protected RowImpl newInstance(Table table, int action) { - return new RowImpl(table, action); + protected RowImpl newInstance(Column[] cols, int action) { + return new RowImpl(cols, action); } /** @@ -939,4 +943,3 @@ public class RowImpl row.setValid(true); } } - diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SecondaryRow.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SecondaryRow.java index 5ed5062f2..87dfbcf05 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SecondaryRow.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/SecondaryRow.java @@ -43,7 +43,11 @@ public class SecondaryRow * Constructor; supply table and 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) @@ -161,8 +165,8 @@ public class SecondaryRow return super.generateSQL(dict); } - protected RowImpl newInstance(Table table, int action) { - return new SecondaryRow(table, action); + protected RowImpl newInstance(Column[] cols, int action) { + return new SecondaryRow(cols, action); } public void copyInto(RowImpl row, boolean whereOnly) {