| | import com.whitemagicsoftware.rxm.tree.Tree; |
| | |
| | +import com.whitemagicsoftware.rxm.grammar.QueryParser; |
| | + |
| | +import org.antlr.v4.runtime.ParserRuleContext; |
| | import org.antlr.v4.runtime.Token; |
| | |
| | /** |
| | * Stores a tree of Payload instances. This is instantiated by |
| | * the parser to create a transformation tree capable of generating a |
| | * SQL/XML SELECT clause. See the Payload class for details on how |
| | * the implicit factory is used to create the correct transformer class. |
| | */ |
| | -public class SelectClauseTree<T extends Payload> extends TransformationTree<T> { |
| | +public class SelectClauseTree<T extends Payload> |
| | + extends TransformationTree<T> { |
| | /** |
| | * Constructs a tree capable of transforming itself into a SQL/XML |
 |
| | } |
| | |
| | + /** |
| | + * Returns the SELECT portion of the transformed query. |
| | + */ |
| | public String toString() { |
| | - String w = getWhitespace(0); |
| | + String w = getSpace(0); |
| | return String.format( "SELECT%s%s%s", w, super.toString(), w ); |
| | + } |
| | + |
| | + /** |
| | + * Returns the from clause, found by finding the first table. |
| | + * |
| | + * TODO: Take into consideration modules. |
| | + */ |
| | + public String getFromClause() { |
| | + // Returns the branch in the tree that contains the first table mapping. |
| | + SelectClauseTree<Payload> fromClause = getFirstTableMap(); |
| | + Payload payload = fromClause.getPayload(); |
| | + ParserRuleContext ctx = payload.getParserRuleContext(); |
| | + |
| | + String tableName = ctx.getChild(0).getText(); |
| | + |
| | + // Create an alias of the same name as the table. |
| | + return String.format( "FROM%s%s %s%s", |
| | + getSpace(), tableName, tableName, getSpace(0) ); |
| | + } |
| | + |
| | + @SuppressWarnings( "unchecked" ) |
| | + private SelectClauseTree<Payload> getFirstTableMap() { |
| | + return (SelectClauseTree<Payload>)(getRoot().firstBranch()); |
| | } |
| | |