In order to help the navigation and the manipulation in the tree generated by the parser, the library provides two useful features...
In addition of letting us browse the ADQL tree, ADQLIterator has the function replace(ADQLObject) which is able to replace objects inside the target object.
The interface ISearchHandler has been extended so that adding the function searchAndReplace(ADQLQuery): IReplaceHandler. Similarly, the class SimpleSearchHandler has been extended to get SimpleReplaceHandler. This implementation has one additional abstract function: getReplacer(ADQLObject). This function must return the ADQL object which should replace the given one.
If you do not want to replace an ADQL object, getReplacer(ADQLObject) must return the object given in parameter.
There is no way to get the parent of a given ADQL object with a SimpleReplaceHandler! So, your search condition must be the object which will be modified or replaced. See the following examples.
If getReplacer(ADQLObject)
returns null
, the given object will be removed from its parent.
It is exactly what does the class RemoveHandler.
When extending SimpleReplaceHandler, you have to override two functions: match(ADQLObject) and getReplacer(ADQLObject).
Now, let's transform the previous example - SearchPointHandler - in
ReplacePointHandler in order to replace all POINT(..., ra, dec)
by
the column coord
:
public class ReplacePointHandler extends SimpleReplaceHandler { public ReplacePointHandler() { super(); } public ReplacePointHandler(boolean recursive) { super(recursive); } public ReplacePointHandler(boolean recursive, boolean onlyFirstMatch) { super(recursive, onlyFirstMatch); } /* Only the POINT functions which use the 2 columns of the table "data": "ra" and "dec".*/ public boolean match(ADQLObject obj) { ... // see SearchColumnHandler.match(ADQLObject) } /* Replace by: "data.coord" (or <tableAlias>".coord"). */ public ADQLObject getReplacer(ADQLObject objToReplace) { try{ PointFunction point = (PointFunction)objToReplace; ADQLColumn col1 = (ADQLColumn)point.getCoord1(); if (col1.getAdqlTable() != null){ String tableAlias = col1.getAdqlTable().getAlias(); if (tableAlias != null) return new ADQLColumn(tableAlias, "coord"); else return new ADQLColumn("basic_data", "coord"); }else return new ADQLColumn("basic_data", "coord"); }catch(ClassCastException cce){;} return null; } }