| Method from org.apache.openjpa.kernel.exps.AbstractExpressionBuilder Detail: |
protected ClassMetaData addAccessPath(ClassMetaData meta) {
_accessPath.add(meta);
return meta;
}
Register the specified metadata as being in the query's access path. |
protected void assertUnboundVariablesValid() {
if (_seenVars == null)
return;
Map.Entry entry;
Value var;
for (Iterator itr = _seenVars.entrySet().iterator(); itr.hasNext();) {
entry = (Map.Entry) itr.next();
var = (Value) entry.getValue();
if (var.getMetaData() == null && !isBound(var)
&& !isDeclaredVariable((String) entry.getKey())) {
throw parseException(EX_USER, "not-unbound-var",
new Object[]{ entry.getKey() }, null);
}
}
}
Validate that all unbound variables are of a PC type. If not, assume
that the user actually made a typo that we took for an implicit
unbound variable. |
protected void bind(Value var) {
if (_boundVars == null)
_boundVars = new HashSet();
_boundVars.add(var);
}
Record that the given variable is bound. |
abstract protected String currentQuery()
Returns the current string being parsed; used for error messages. |
protected ClassMetaData[] getAccessPath() {
return (ClassMetaData[]) _accessPath.toArray
(new ClassMetaData[_accessPath.size()]);
}
Return the recorded query access path. |
abstract protected ClassLoader getClassLoader()
Returns the class loader that should be used for resolving
class names (in addition to the resolver in the query). |
abstract protected Class getDeclaredVariableType(String name)
Returns the type of the named variabe if it has been declared. |
abstract protected Localizer getLocalizer()
Used for obtaining the Localizer to use for translating
error messages. |
protected ClassMetaData getMetaData(Class c,
boolean required) {
return getMetaData(c, required, getClassLoader());
}
Convenience method to get metadata for the given type. |
protected ClassMetaData getMetaData(Class c,
boolean required,
ClassLoader loader) {
return resolver.getConfiguration().getMetaDataRepositoryInstance().
getMetaData(c, loader, required);
}
Convenience method to get metadata for the given type. |
protected Value getVariable(String id,
boolean bind) {
// check for already constructed var
if (isSeenVariable(id))
return (Value) _seenVars.get(id);
// create and cache var
Class type = getDeclaredVariableType(id);
// add this type to the set of classes in the filter's access path
ClassMetaData meta = null;
if (type == null)
type = TYPE_OBJECT;
else
meta = getMetaData(type, false);
if (meta != null)
_accessPath.add(meta);
Value var;
if (bind)
var = factory.newBoundVariable(id, type);
else
var = factory.newUnboundVariable(id, type);
var.setMetaData(meta);
if (_seenVars == null)
_seenVars = new HashMap();
_seenVars.put(id, var);
return var;
}
Returns a value for the given id. |
protected boolean isBound(Value var) {
return _boundVars != null && _boundVars.contains(var);
}
Return true if the given variable has been bound. |
abstract protected boolean isDeclaredVariable(String id)
Returns whether the specified variable name has been explicitly
declared. Note all query languages necessarily support declaring
variables. |
protected boolean isSeenVariable(String id) {
return _seenVars != null && _seenVars.containsKey(id);
}
Return whether the given id has been used as a variable. |
protected OpenJPAException parseException(int e,
String token,
Object[] args,
Exception nest) {
String argStr;
if (args == null)
argStr = getLocalizer().get(token).getMessage();
else
argStr = getLocalizer().get(token, args).getMessage();
Message msg = _loc.get("parse-error", argStr, currentQuery());
switch (e) {
case EX_FATAL:
throw new InternalException(msg, nest);
case EX_UNSUPPORTED:
throw new UnsupportedException(msg, nest);
default:
throw new UserException(msg, nest);
}
}
Create a proper parse exception for the given reason. |
protected void setImplicitContainsTypes(Value val1,
Value val2,
int op) {
if (val1.getType() == TYPE_OBJECT) {
if (op == CONTAINS_TYPE_ELEMENT)
val1.setImplicitType(Collection.class);
else
val1.setImplicitType(Map.class);
}
if (val2.getType() == TYPE_OBJECT && val1 instanceof Path) {
FieldMetaData fmd = ((Path) val1).last();
ClassMetaData meta;
if (fmd != null) {
if (op == CONTAINS_TYPE_ELEMENT || op == CONTAINS_TYPE_VALUE) {
val2.setImplicitType(fmd.getElement().getDeclaredType());
meta = fmd.getElement().getDeclaredTypeMetaData();
if (meta != null) {
val2.setMetaData(meta);
addAccessPath(meta);
}
} else {
val2.setImplicitType(fmd.getKey().getDeclaredType());
meta = fmd.getKey().getDeclaredTypeMetaData();
if (meta != null) {
val2.setMetaData(meta);
addAccessPath(meta);
}
}
}
}
}
Set the implicit types of the given values based on the fact that
the first is supposed to contain the second. |
protected static void setImplicitType(Value val,
Class expected) {
// we never expect a pc type, so no need to worry about metadata
if (val.getType() == TYPE_OBJECT)
val.setImplicitType(expected);
}
Set the implicit type of the given value to the given class. |
protected void setImplicitTypes(Value val1,
Value val2,
Class expected) {
Class c1 = val1.getType();
Class c2 = val2.getType();
boolean o1 = c1 == TYPE_OBJECT;
boolean o2 = c2 == TYPE_OBJECT;
if (o1 && !o2) {
val1.setImplicitType(c2);
if (val1.getMetaData() == null && !val1.isXPath())
val1.setMetaData(val2.getMetaData());
} else if (!o1 && o2) {
val2.setImplicitType(c1);
if (val2.getMetaData() == null && !val1.isXPath())
val2.setMetaData(val1.getMetaData());
} else if (o1 && o2 && expected != null) {
// we never expect a pc type, so don't bother with metadata
val1.setImplicitType(expected);
val2.setImplicitType(expected);
} else if (isNumeric(val1.getType()) != isNumeric(val2.getType())) {
if (resolver.getConfiguration().getCompatibilityInstance().
getQuotedNumbersInQueries())
convertTypesQuotedNumbers(val1, val2);
else
convertTypes(val1, val2);
}
}
Set the implicit types of the given values based on the fact that
they're used together, and based on the operator type. |
protected Value traversePath(Path path,
String field) {
return traversePath(path, field, false, false);
}
Traverse the given field in the given path. |
protected Value traversePath(Path path,
String field,
boolean pcOnly,
boolean allowNull) {
ClassMetaData meta = path.getMetaData();
if (meta == null)
throw parseException(EX_USER, "path-no-meta",
new Object[]{ field, path.getType() }, null);
FieldMetaData fmd = meta.getField(field);
if (fmd == null) {
Object val = traverseStaticField(meta.getDescribedType(), field);
if (val == null)
throw parseException(EX_USER, "no-field",
new Object[]{ meta.getDescribedType(), field }, null);
return factory.newLiteral(val, Literal.TYPE_UNKNOWN);
}
if (fmd.isEmbedded())
meta = fmd.getEmbeddedMetaData();
else
meta = fmd.getDeclaredTypeMetaData();
if (meta != null) {
addAccessPath(meta);
path.setMetaData(meta);
}
else {
// xmlsupport xpath
XMLMetaData xmlmeta = fmd.getRepository().getXMLMetaData(fmd);
if (xmlmeta != null) {
path.get(fmd, xmlmeta);
return path;
}
}
if (meta != null || !pcOnly)
path.get(fmd, allowNull);
return path;
}
Traverse the given field in the given path. |
protected Object traverseStaticField(Class cls,
String field) {
try {
return cls.getField(field).get(null);
} catch (Exception e) {
// count not locate the field: return null
return null;
}
}
Return a constant containing the value of the given static field. |
protected Value traverseXPath(Path path,
String field) {
XMLMetaData meta = path.getXmlMapping();
if (meta.getFieldMapping(field) == null) {
throw parseException(EX_USER, "no-field",
new Object[]{ meta.getType(), field }, null);
}
else {
// collection-valued xpath is not allowed
int type = meta.getFieldMapping(field).getTypeCode();
switch (type) {
case JavaTypes.ARRAY:
case JavaTypes.COLLECTION:
case JavaTypes.MAP:
throw new UserException(_loc.get("collection-valued-path",
field));
}
}
path.get(meta, field);
return path;
}
|