A path represents a traversal into fields of a candidate object.
| Method from org.apache.openjpa.jdbc.kernel.exps.PCPath Detail: |
public void addVariableAction(Variable var) {
_varName = var.getName();
}
Set the path as a binding of the given variable. |
public void appendIsEmpty(Select sel,
ExpContext ctx,
ExpState state,
SQLBuffer sql) {
PathExpState pstate = (PathExpState) state;
if (pstate.field == null)
sql.append(FALSE);
else
pstate.field.appendIsEmpty(sql, sel, pstate.joins);
}
|
public void appendIsNotEmpty(Select sel,
ExpContext ctx,
ExpState state,
SQLBuffer sql) {
PathExpState pstate = (PathExpState) state;
if (pstate.field == null)
sql.append(FALSE);
else
pstate.field.appendIsNotEmpty(sql, sel, pstate.joins);
}
|
public void appendIsNotNull(Select sel,
ExpContext ctx,
ExpState state,
SQLBuffer sql) {
PathExpState pstate = (PathExpState) state;
if (pstate.field == null)
sql.append(TRUE);
else
pstate.field.appendIsNotNull(sql, sel, pstate.joins);
}
|
public void appendIsNull(Select sel,
ExpContext ctx,
ExpState state,
SQLBuffer sql) {
PathExpState pstate = (PathExpState) state;
if (pstate.field == null)
sql.append(FALSE);
else
pstate.field.appendIsNull(sql, sel, pstate.joins);
}
|
public void appendSize(Select sel,
ExpContext ctx,
ExpState state,
SQLBuffer sql) {
PathExpState pstate = (PathExpState) state;
if (pstate.field == null)
sql.append("1");
else
pstate.field.appendSize(sql, sel, pstate.joins);
}
|
public void appendTo(Select sel,
ExpContext ctx,
ExpState state,
SQLBuffer sql,
int index) {
Column col = getColumns(state)[index];
// if select is null, it means we are not aliasing columns
// (e.g., during a bulk update)
if (sel == null)
sql.append(col.getName());
else if (_type == XPATH)
// if this is an xpath, append xpath string
sql.append(getXPath());
else
sql.append(sel.getColumnAlias(col, state.joins));
}
|
public void calculateValue(Select sel,
ExpContext ctx,
ExpState state,
Val other,
ExpState otherState) {
// we don't create the SQL b/c it forces the Select to cache aliases
// for the tables we use, and these aliases might not ever be used if
// we eventually call appendIsEmpty or appendIsNull rather than appendTo
}
|
public boolean equals(Object other) {
if (other == this)
return true;
if (!(other instanceof PCPath))
return false;
PCPath path = (PCPath) other;
return ObjectUtils.equals(_candidate, path._candidate)
&& ObjectUtils.equals(_actions, path._actions);
}
|
public void get(FieldMetaData field,
boolean nullTraversal) {
if (_actions == null)
_actions = new LinkedList();
Action action = new Action();
action.op = (nullTraversal) ? Action.GET_OUTER : Action.GET;
action.data = field;
_actions.add(action);
if (_type == UNACCESSED_VAR)
_type = BOUND_VAR;
_cast = null;
_key = false;
}
|
public void get(FieldMetaData fmd,
XMLMetaData meta) {
if (_actions == null)
_actions = new LinkedList();
Action action = new Action();
action.op = Action.GET_XPATH;
action.data = meta;
_actions.add(action);
_cast = null;
_key = false;
_type = XPATH;
_xmlfield = fmd;
}
|
public void get(XMLMetaData meta,
String name) {
Action action = new Action();
action.op = Action.GET_XPATH;
action.data = meta.getFieldMapping(name);
_actions.add(action);
_cast = null;
_key = false;
_type = XPATH;
}
|
public ClassMapping getClassMapping(ExpState state) {
PathExpState pstate = (PathExpState) state;
if (pstate.field == null)
return _class;
if (_key) {
if (pstate.field.getKey().getTypeCode() == JavaTypes.PC)
return pstate.field.getKeyMapping().getTypeMapping();
return null;
}
if (pstate.field.getElement().getTypeCode() == JavaTypes.PC)
return pstate.field.getElementMapping().getTypeMapping();
if (pstate.field.getTypeCode() == JavaTypes.PC)
return pstate.field.getTypeMapping();
return null;
}
|
public Column[] getColumns(ExpState state) {
PathExpState pstate = (PathExpState) state;
if (pstate.cols == null)
pstate.cols = calculateColumns(pstate);
return pstate.cols;
}
|
public FieldMapping getFieldMapping(ExpState state) {
return ((PathExpState) state).field;
}
|
public synchronized void getKey() {
if (_cid)
return;
// change the last action to a get key
Action action = (Action) _actions.getLast();
action.op = Action.GET_KEY;
_cast = null;
_key = true;
}
|
public ClassMetaData getMetaData() {
return _class;
}
|
public String getPath() {
if (_actions == null)
return (_varName == null) ? "" : _varName + ".";
StringBuffer path = new StringBuffer();
Action action;
for (Iterator itr = _actions.iterator(); itr.hasNext();) {
action = (Action) itr.next();
if (action.op == Action.VAR || action.op == Action.SUBQUERY)
path.append(action.data);
else if (action.op == Action.UNBOUND_VAR)
path.append(((Variable) action.data).getName());
else
path.append(((FieldMapping) action.data).getName());
path.append('.");
}
if (_varName != null)
path.append(_varName).append('.");
return path.toString();
}
|
public Class getType() {
if (_cast != null)
return _cast;
Action act = lastFieldAction();
if (act != null && act.op == Action.GET_XPATH)
return ((XMLMetaData) act.data).getType();
FieldMetaData fld = (act == null) ? null : (FieldMetaData) act.data;
boolean key = act != null && act.op == Action.GET_KEY;
if (fld != null) {
switch (fld.getDeclaredTypeCode()) {
case JavaTypes.ARRAY:
if (fld.getDeclaredType() == byte[].class
|| fld.getDeclaredType() == Byte[].class
|| fld.getDeclaredType() == char[].class
|| fld.getDeclaredType() == Character[].class)
return fld.getDeclaredType();
return fld.getElement().getDeclaredType();
case JavaTypes.MAP:
if (key)
return fld.getKey().getDeclaredType();
return fld.getElement().getDeclaredType();
case JavaTypes.COLLECTION:
return fld.getElement().getDeclaredType();
default:
return fld.getDeclaredType();
}
}
if (_class != null)
return _class.getDescribedType();
return Object.class;
}
|
public String getXPath() {
StringBuffer xpath = new StringBuffer();
Action action;
Iterator itr = _actions.iterator();
// Skip variable actions since they are not part of the xpath
// until we reach the first xpath action.
// The first xpath action maps to the root of an xml document.
do
action = (Action) itr.next();
while (action.op != Action.GET_XPATH);
// Skip XmlRootElement:
// We can't rely on the accuracy of the name of the root element,
// because it could be set to some default by JAXB XML Binding.
// The caller(DBDictionary) should start with "/*" or "/*/",
// we build the remaining xpath that follows the root element.
while (itr.hasNext()) {
action = (Action) itr.next();
if (((XMLMetaData) action.data).getXmlname() != null)
xpath.append(((XMLMetaData) action.data).getXmlname());
else
xpath.append("*");
if (itr.hasNext())
xpath.append("/");
}
return xpath.toString();
}
|
public XMLMetaData getXmlMapping() {
Action act = (Action) _actions.getLast();
if (act != null)
return (XMLMetaData) act.data;
return null;
}
|
public void groupBy(Select sel,
ExpContext ctx,
ExpState state) {
ClassMapping mapping = getClassMapping(state);
PathExpState pstate = (PathExpState) state;
if (mapping == null || !pstate.joinedRel)
sel.groupBy(getColumns(state), sel.outer(pstate.joins));
else {
int subs = (_type == UNBOUND_VAR) ? Select.SUBS_JOINABLE
: Select.SUBS_ANY_JOINABLE;
sel.groupBy(mapping, subs, ctx.store, ctx.fetch,
sel.outer(pstate.joins));
}
}
|
public int hashCode() {
if (_actions == null)
return _candidate.hashCode();
return _candidate.hashCode() ^ _actions.hashCode();
}
|
public ExpState initialize(Select sel,
ExpContext ctx,
int flags) {
PathExpState pstate = new PathExpState(sel.newJoins());
boolean key = false;
boolean forceOuter = false;
ClassMapping rel = _candidate;
// iterate to the final field
ClassMapping owner;
ClassMapping from, to;
Action action;
Variable var;
Iterator itr = (_actions == null) ? null : _actions.iterator();
FieldMapping field;
while (itr != null && itr.hasNext()) {
action = (Action) itr.next();
// treat subqueries like variables for alias generation purposes
if (action.op == Action.VAR)
pstate.joins = pstate.joins.setVariable((String) action.data);
else if (action.op == Action.SUBQUERY)
pstate.joins = pstate.joins.setSubselect((String) action.data);
else if (action.op == Action.UNBOUND_VAR) {
// unbound vars are cross-joined to the candidate table
var = (Variable) action.data;
rel = (ClassMapping) var.getMetaData();
pstate.joins = pstate.joins.setVariable(var.getName());
pstate.joins = pstate.joins.crossJoin(_candidate.getTable(),
rel.getTable());
} else {
// move past the previous field, if any
field = (action.op == Action.GET_XPATH) ? (FieldMapping) _xmlfield :
(FieldMapping) action.data;
if (pstate.field != null) {
// if this is the second-to-last field and the last is
// the related field this field joins to, no need to
// traverse: just use this field's fk columns
if (!itr.hasNext() && (flags & JOIN_REL) == 0
&& isJoinedField(pstate.field, key, field)) {
pstate.cmpfield = field;
break;
}
rel = traverseField(pstate, key, forceOuter, false);
}
// mark if the next traversal should go through
// the key rather than value
key = action.op == Action.GET_KEY;
forceOuter |= action.op == Action.GET_OUTER;
// get mapping for the current field
pstate.field = field;
owner = pstate.field.getDefiningMapping();
if (pstate.field.getManagement()
!= FieldMapping.MANAGE_PERSISTENT)
throw new UserException(_loc.get("non-pers-field",
pstate.field));
// find the most-derived type between the declared relation
// type and the field's owner, and join from that type to
// the lesser derived type
if (rel != owner && rel != null) {
if (rel.getDescribedType().isAssignableFrom
(owner.getDescribedType())) {
from = owner;
to = rel;
} else {
from = rel;
to = owner;
}
for (; from != null && from != to;
from = from.getJoinablePCSuperclassMapping())
pstate.joins = from.joinSuperclass(pstate.joins, false);
}
// nothing more to do from here on as we encountered an xpath action
if (action.op == Action.GET_XPATH)
break;
}
}
if (_varName != null)
pstate.joins = pstate.joins.setVariable(_varName);
// if we're not comparing to null or doing an isEmpty, then
// join into the data on the final field; obviously we can't do these
// joins when comparing to null b/c the whole purpose is to see
// whether the joins even exist
if ((flags & NULL_CMP) == 0)
traverseField(pstate, key, forceOuter, true);
pstate.joinedRel = false;
if ((flags & JOIN_REL) != 0)
joinRelation(pstate, key, forceOuter || (flags & FORCE_OUTER) != 0,
false);
return pstate;
}
|
public boolean isKey() {
return _key;
}
|
public boolean isUnaccessedVariable() {
return _type == UNACCESSED_VAR;
}
Return true if this is a bound variable that has not been accessed
after binding. Useful for filters like
"coll.contains (var) && var == null", which should really
just act like "coll.contains (null)". |
public boolean isVariable() {
if (_actions == null)
return false;
Action action = (Action) _actions.getLast();
return action.op == Action.UNBOUND_VAR || action.op == Action.VAR;
}
|
public boolean isVariablePath() {
return _type != PATH;
}
Return whether this is a path involving a variable. |
public boolean isXPath() {
return _type == XPATH;
}
|
public FieldMetaData last() {
Action act = lastFieldAction();
return (act == null) ? null : isXPath() ? _xmlfield :
(FieldMetaData) act.data;
}
|
public int length(Select sel,
ExpContext ctx,
ExpState state) {
return getColumns(state).length;
}
|
public Object load(ExpContext ctx,
ExpState state,
Result res) throws SQLException {
return load(ctx, state, res, false);
}
|
Object load(ExpContext ctx,
ExpState state,
Result res,
boolean pks) throws SQLException {
ClassMapping mapping = getClassMapping(state);
PathExpState pstate = (PathExpState) state;
if (mapping != null && (pstate.field == null
|| !pstate.field.isEmbedded())) {
if (pks)
return mapping.getObjectId(ctx.store, res, null, true,
pstate.joins);
return res.load(mapping, ctx.store, ctx.fetch, pstate.joins);
}
Object ret;
if (_key)
ret = pstate.field.loadKeyProjection(ctx.store, ctx.fetch, res,
pstate.joins);
else
ret = pstate.field.loadProjection(ctx.store, ctx.fetch, res,
pstate.joins);
if (_cast != null)
ret = Filters.convert(ret, _cast);
return ret;
}
|
public void orderBy(Select sel,
ExpContext ctx,
ExpState state,
boolean asc) {
sel.orderBy(getColumns(state), asc, sel.outer(state.joins), false);
}
|
public void select(Select sel,
ExpContext ctx,
ExpState state,
boolean pks) {
selectColumns(sel, ctx, state, pks);
}
|
public void selectColumns(Select sel,
ExpContext ctx,
ExpState state,
boolean pks) {
ClassMapping mapping = getClassMapping(state);
PathExpState pstate = (PathExpState) state;
if (mapping == null || !pstate.joinedRel)
sel.select(getColumns(state), pstate.joins);
else if (pks)
sel.select(mapping.getPrimaryKeyColumns(), pstate.joins);
else {
// select the mapping; allow any subs because we know this must
// be either a relation, in which case it will already be
// constrained by the joins, or 'this', in which case the
// JDBCExpressionFactory takes care of adding class conditions for
// the candidate class on the select
int subs = (_type == UNBOUND_VAR) ? Select.SUBS_JOINABLE
: Select.SUBS_ANY_JOINABLE;
sel.select(mapping, subs, ctx.store, ctx.fetch,
JDBCFetchConfiguration.EAGER_NONE, sel.outer(pstate.joins));
}
}
|
public synchronized void setContainsId(String id) {
if (_cid)
return;
// treat it just like a unique variable
Action action = new Action();
action.op = Action.VAR;
action.data = id;
if (_actions == null)
_actions = new LinkedList();
_actions.add(action);
_cid = true;
}
If this path is part of a contains clause, then alias it to the
proper contains id before initialization. |
public void setImplicitType(Class type) {
_cast = type;
}
|
public void setMetaData(ClassMetaData meta) {
_class = (ClassMapping) meta;
}
|
public Object toDataStoreValue(Select sel,
ExpContext ctx,
ExpState state,
Object val) {
PathExpState pstate = (PathExpState) state;
FieldMapping field = (pstate.cmpfield != null) ? pstate.cmpfield
: pstate.field;
if (isXPath())
return val;
if (field != null) {
if (_key)
return field.toKeyDataStoreValue(val, ctx.store);
if (field.getElement().getDeclaredTypeCode() != JavaTypes.OBJECT)
return field.toDataStoreValue(val, ctx.store);
val = field.getExternalValue(val, ctx.store.getContext());
return field.toDataStoreValue(val, ctx.store);
}
return _class.toDataStoreValue(val, _class.getPrimaryKeyColumns(),
ctx.store);
}
|