Utility methods for strategies using value handlers.
| Method from org.apache.openjpa.jdbc.meta.strats.HandlerStrategies Detail: |
public static void assertJoinable(ValueMapping vm) {
ClassMapping rel = vm.getTypeMapping();
if (rel != null && (rel.getTable() == null
|| !rel.getTable().equals(vm.getFieldMapping().getTable())))
throw RelationStrategies.unjoinable(vm);
}
Throw the proper exception if the given handler-controlled value
represents an unjoinable relation. |
public static Object loadDataStore(ValueMapping vm,
Result res,
Joins joins,
Column[] cols) throws SQLException {
if (cols.length == 0)
return null;
if (cols.length == 1)
return res.getObject(cols[0], vm.getHandler().
getResultArgument(vm), joins);
Object[] vals = new Object[cols.length];
Object[] args = (Object[]) vm.getHandler().getResultArgument(vm);
for (int i = 0; i < cols.length; i++)
vals[i] = res.getObject(cols[i], (args == null) ? null : args[i],
joins);
return vals;
}
Load the datastore value from the given result. This method does
not process the loaded value through
ValueHandler#toObjectValue . |
public static Object loadObject(ValueMapping vm,
OpenJPAStateManager sm,
JDBCStore store,
JDBCFetchConfiguration fetch,
Result res,
Joins joins,
Column[] cols,
boolean objectValueRequiresLoad) throws SQLException {
if (cols.length == 0)
throw new InvalidStateException(_loc.get("cant-project-owned",
vm));
Object val = loadDataStore(vm, res, joins, cols);
if (objectValueRequiresLoad)
return vm.getHandler().toObjectValue(vm, val, sm, store, fetch);
return vm.getHandler().toObjectValue(vm, val);
}
Load the Object value from the given result. |
public static Column[] map(ValueMapping vm,
String name,
ColumnIO io,
boolean adapt) {
ValueMappingInfo vinfo = vm.getValueInfo();
vinfo.assertNoJoin(vm, true);
vinfo.assertNoForeignKey(vm, !adapt);
Column[] cols = vm.getHandler().map(vm, name, io, adapt);
if (cols.length > 0 && cols[0].getTable() == null) {
cols = vinfo.getColumns(vm, name, cols,
vm.getFieldMapping().getTable(), adapt);
ColumnIO mappedIO = vinfo.getColumnIO();
vm.setColumns(cols);
vm.setColumnIO(mappedIO);
if (mappedIO != null) {
for (int i = 0; i < cols.length; i++) {
io.setInsertable(i, mappedIO.isInsertable(i, false));
io.setNullInsertable(i, mappedIO.isInsertable(i, true));
io.setUpdatable(i, mappedIO.isUpdatable(i, false));
io.setNullUpdatable(i, mappedIO.isUpdatable(i, true));
}
}
}
vm.mapConstraints(name, adapt);
return cols;
}
|
public static void set(ValueMapping vm,
Object val,
JDBCStore store,
Row row,
Column[] cols,
ColumnIO io,
boolean nullNone) throws SQLException {
if (!canSetAny(row, io, cols))
return;
ValueHandler handler = vm.getHandler();
val = handler.toDataStoreValue(vm, val, store);
if (val == null) {
for (int i = 0; i < cols.length; i++)
if (canSet(row, io, i, true))
set(row, cols[i], null, handler, nullNone);
} else if (cols.length == 1) {
if (canSet(row, io, 0, val == null))
set(row, cols[0], val, handler, nullNone);
} else {
Object[] vals = (Object[]) val;
for (int i = 0; i < vals.length; i++)
if (canSet(row, io, i, vals[i] == null))
set(row, cols[i], vals[i], handler, nullNone);
}
}
Set the given value into the given row. |
public static Object toDataStoreValue(ValueMapping vm,
Object val,
Column[] cols,
JDBCStore store) {
ValueHandler handler = vm.getHandler();
val = handler.toDataStoreValue(vm, val, store);
if (val == null) {
if (cols.length > 1)
return new Object[cols.length];
return null;
}
// relation ids are returned as state managers; resolve the final
// datastore value immediately
Object[] vals;
for (int i = 0; i < cols.length; i++) {
if (!cols[i].isRelationId())
continue;
if (!(handler instanceof RelationId))
break;
if (cols.length == 1) {
val = ((RelationId) handler).toRelationDataStoreValue
((OpenJPAStateManager) val, cols[i]);
} else {
vals = (Object[]) val;
vals[i] = ((RelationId) handler).toRelationDataStoreValue
((OpenJPAStateManager) vals[i], cols[i]);
}
}
return val;
}
Convert the given object to its datastore value(s). Relation ids are
converted to their final values immediately. |
public static void where(ValueMapping vm,
Object val,
JDBCStore store,
Row row,
Column[] cols) throws SQLException {
if (cols.length == 0)
return;
val = toDataStoreValue(vm, val, cols, store);
if (val == null)
for (int i = 0; i < cols.length; i++)
row.whereNull(cols[i]);
else if (cols.length == 1)
where(row, cols[0], val);
else {
Object[] vals = (Object[]) val;
for (int i = 0; i < vals.length; i++)
where(row, cols[i], vals[i]);
}
}
Add where conditions to the given row. |