| Author | Dave Jarvis <email> |
|---|---|
| Date | 2015-03-14 12:20:31 GMT-0700 |
| Commit | ab6658ae8310bed75190406d36a96116c8d4eaf9 |
| Parent | 890bc06 |
| import com.whitemagicsoftware.rxm.tree.Tree; | ||
| import com.whitemagicsoftware.rxm.tree.xml.SelectClauseTree; | ||
| -import com.whitemagicsoftware.rxm.tree.xml.FromClauseList; | ||
| +import com.whitemagicsoftware.rxm.tree.xml.JoinClauseList; | ||
| import org.antlr.v4.runtime.ANTLRInputStream; | ||
| /** List of inner and outer joins (tree used as flat hierarchy). */ | ||
| - private Tree<Payload> fromClauseList; | ||
| + private Tree<Payload> joinClauseList; | ||
| /** | ||
| String select = getSelectClause(); | ||
| String from = getFromClause(); | ||
| + String join = getJoinClause(); | ||
| String where = getWhereClause(); | ||
| - System.out.printf( "SELECT%n%s%nFROM%n%s%nWHERE%n%s%n", | ||
| - select, from, where ); | ||
| + System.out.printf( "SELECT%n%s%nFROM%n%s%n%s%nWHERE%n%s%n", | ||
| + select, from, join, where ); | ||
| } | ||
| private String getSelectClause() { | ||
| return getSelectClauseTree().getRoot().toString(); | ||
| + } | ||
| + | ||
| + /** | ||
| + * TODO: implement | ||
| + */ | ||
| + private String getFromClause() { | ||
| + return "person person"; | ||
| } | ||
| /** | ||
| * Returns the tables JOINed together that are used in the SELECT clause | ||
| * of the SQL statement. | ||
| * | ||
| * @return The FROM portion of a SQL statement. | ||
| */ | ||
| - private String getFromClause() { | ||
| - return getFromClauseList().toString(); | ||
| + private String getJoinClause() { | ||
| + return getJoinClauseList().toString(); | ||
| } | ||
| /** | ||
| * Adds a new leaf, representing a JOIN clause, to the from clause list. | ||
| * | ||
| * @param ctx The INNER/OUTER JOIN expression to add to the list. | ||
| */ | ||
| private void addJoinClause( ParserRuleContext ctx ) { | ||
| - getFromClauseList().addLeaf( createFromClauseList( ctx ) ); | ||
| + getJoinClauseList().addLeaf( createJoinClauseList( ctx ) ); | ||
| } | ||
| * Changes where the next mapped items will be attached. | ||
| */ | ||
| - private void setFromClauseList( Tree<Payload> fromClauseList ) { | ||
| - this.fromClauseList = fromClauseList; | ||
| + private void setJoinClauseList( Tree<Payload> joinClauseList ) { | ||
| + this.joinClauseList = joinClauseList; | ||
| } | ||
| /** | ||
| * Lazily-initializes the from clause list. | ||
| * | ||
| * @return The list that will contain the query's FROM clause. | ||
| */ | ||
| - private Tree<Payload> getFromClauseList() { | ||
| - Tree<Payload> list = this.fromClauseList; | ||
| + private Tree<Payload> getJoinClauseList() { | ||
| + Tree<Payload> list = this.joinClauseList; | ||
| if( list == null ) { | ||
| - setFromClauseList( list = createFromClauseList( null ) ); | ||
| + setJoinClauseList( list = createJoinClauseList( null ) ); | ||
| } | ||
| return list; | ||
| } | ||
| /** | ||
| - * Creates a new FromClauseList instance containing the given parser | ||
| + * Creates a new JoinClauseList instance containing the given parser | ||
| * rule context. The resulting tree should only have leaves added | ||
| * to the root, never branching beyond a flat hierarchy (one-level deep). | ||
| * | ||
| - * @return A new FromClauseList instance, never null, containing a payload | ||
| + * @return A new JoinClauseList instance, never null, containing a payload | ||
| * with the parser rule context. | ||
| */ | ||
| - private Tree<Payload> createFromClauseList( ParserRuleContext ctx ) { | ||
| - return new FromClauseList<Payload>( new Payload( ctx ) ); | ||
| + private Tree<Payload> createJoinClauseList( ParserRuleContext ctx ) { | ||
| + return new JoinClauseList<Payload>( new Payload( ctx ) ); | ||
| } | ||
| } | ||
| if( beautify() ) { | ||
| - indent = indent( depth ); | ||
| - newline = System.lineSeparator(); | ||
| + indent = getIndent( depth ); | ||
| + newline = getNewline(); | ||
| } | ||
| return sb.append( stop( tree ) ).toString(); | ||
| + } | ||
| + | ||
| + protected String getNewline() { | ||
| + return System.lineSeparator(); | ||
| } | ||
| * @param spaces The number of spaces to add to the string. | ||
| */ | ||
| - private String indent( int spaces ) { | ||
| + private String getIndent( int spaces ) { | ||
| return CharBuffer.allocate( spaces ).toString().replace( '\0', ' ' ); | ||
| } | ||
| return index > -1 && index < getBranches().size(); | ||
| } | ||
| - | ||
| - public String toString() { | ||
| - StringBuilder sb = new StringBuilder( 2048 ); | ||
| - | ||
| - for( Tree<T> branch : getBranches() ) { | ||
| - sb.append( branch.toString() ); | ||
| - } | ||
| - | ||
| - return sb.toString(); | ||
| - } | ||
| } | ||
| -package com.whitemagicsoftware.rxm.tree.xml; | ||
| - | ||
| -import com.whitemagicsoftware.rxm.tree.Payload; | ||
| -import com.whitemagicsoftware.rxm.tree.Tree; | ||
| -import com.whitemagicsoftware.rxm.tree.TransformationTree; | ||
| - | ||
| -import org.antlr.v4.runtime.ParserRuleContext; | ||
| - | ||
| -/** | ||
| - * <p> | ||
| - * Stores a list of Payload instances. This is instantiated by the parser | ||
| - * to create a transformation list capable of generating a SQL/XML FROM | ||
| - * clause. The superclass dynamically determines the set of wrapper classes | ||
| - * by virtue of being in the same package as this class. | ||
| - * </p> | ||
| - * <p> | ||
| - * The tree is being re-used as a list. (A list is simply a tree with | ||
| - * one branch and many siblings.) | ||
| - * </p> | ||
| - */ | ||
| -public class FromClauseList<T extends Payload> extends TransformationTree<T> { | ||
| - /** | ||
| - * Constructs a list capable of transforming itself into a SQL/XML | ||
| - * FROM clause. | ||
| - * | ||
| - * @param payload The data associated with this list (must not be null). | ||
| - */ | ||
| - public FromClauseList( T payload ) { | ||
| - super( payload ); | ||
| - } | ||
| - | ||
| - /** | ||
| - * Called when entering a new hierarchy in the transformation tree. | ||
| - * | ||
| - * @param tree Unused. | ||
| - * @return Empty string. | ||
| - */ | ||
| - protected String start( Tree<T> tree ) { | ||
| - return ""; | ||
| - } | ||
| - | ||
| - /** | ||
| - * Called when exiting a new hierarchy in the transformation tree. | ||
| - * | ||
| - * @param tree Unused. | ||
| - * @return Empty string. | ||
| - */ | ||
| - protected String stop( Tree<T> tree ) { | ||
| - return ""; | ||
| - } | ||
| - | ||
| - public String toString() { | ||
| - StringBuilder sb = new StringBuilder( 2048 ); | ||
| - | ||
| - for( Tree<T> branch : getBranches() ) { | ||
| - Payload payload = branch.getPayload(); | ||
| - | ||
| - ParserRuleContext ctx = payload.getParserRuleContext(); | ||
| - | ||
| - sb.append( ctx.getText() ); | ||
| - } | ||
| - | ||
| - return sb.toString(); | ||
| - } | ||
| -} | ||
| - | ||
| /** | ||
| * Transforms <b><code>table.column +> table.column</code></b> into a SQL | ||
| - * <code>JOIN</code> expression. | ||
| + * <code>INNER JOIN</code> expression. | ||
| */ | ||
| public class InnerMapContext extends PayloadParserRuleContext { | ||
| /** | ||
| - * | ||
| + * Returns the starting text for an INNER JOIN clause. | ||
| */ | ||
| @Override | ||
| public Token getStart() { | ||
| - return new Token( "TOKEN" ); | ||
| + return new Token( "INNER JOIN" ); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Returns the ending text for an INNER JOIN clause. | ||
| + */ | ||
| + @Override | ||
| + public Token getStop() { | ||
| + return new Token( "" ); | ||
| } | ||
| } | ||
| +package com.whitemagicsoftware.rxm.tree.xml; | ||
| + | ||
| +import com.whitemagicsoftware.rxm.tree.Payload; | ||
| +import com.whitemagicsoftware.rxm.tree.Tree; | ||
| +import com.whitemagicsoftware.rxm.tree.TransformationTree; | ||
| + | ||
| +import org.antlr.v4.runtime.ParserRuleContext; | ||
| +import org.antlr.v4.runtime.Token; | ||
| + | ||
| +/** | ||
| + * <p> | ||
| + * Stores a list of Payload instances. This is instantiated by the parser | ||
| + * to create a transformation list capable of generating a SQL/XML FROM | ||
| + * clause. The superclass dynamically determines the set of wrapper classes | ||
| + * by virtue of being in the same package as this class. | ||
| + * </p> | ||
| + * <p> | ||
| + * The tree is being re-used as a list. (A list is simply a tree with | ||
| + * one branch and many siblings.) | ||
| + * </p> | ||
| + */ | ||
| +public class JoinClauseList<T extends Payload> extends TransformationTree<T> { | ||
| + /** | ||
| + * Constructs a list capable of transforming itself into a SQL/XML | ||
| + * FROM clause. | ||
| + * | ||
| + * @param payload The data associated with this list (must not be null). | ||
| + */ | ||
| + public JoinClauseList( T payload ) { | ||
| + super( payload ); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Converts the list of <b>rxm</b> JOIN expressions into an equivalent SQL | ||
| + * FROM clause. | ||
| + */ | ||
| + @SuppressWarnings( "unchecked" ) | ||
| + public String toString() { | ||
| + StringBuilder sb = new StringBuilder( 2048 ); | ||
| + | ||
| + for( Tree<T> branch : getBranches() ) { | ||
| + Payload payload = branch.getPayload(); | ||
| + | ||
| + // Payload must be able to find the appropriate subclass to instantiate | ||
| + // based on its tree instance's package. See the getStart and getStop | ||
| + // methods in the Payload class for details. | ||
| + payload.setTree( (Tree<Payload>)this ); | ||
| + | ||
| + sb.append( payload.getStart() ); | ||
| + | ||
| + if( beautify() ) { | ||
| + sb.append( getNewline() ); | ||
| + } | ||
| + | ||
| + sb.append( payload.getStop() ); | ||
| + } | ||
| + | ||
| + return sb.toString(); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Called when entering a new hierarchy in the transformation tree. | ||
| + * | ||
| + * @param tree Unused. | ||
| + * @return Empty string. | ||
| + */ | ||
| + protected String start( Tree<T> tree ) { | ||
| + return ""; | ||
| + } | ||
| + | ||
| + /** | ||
| + * Called when exiting a new hierarchy in the transformation tree. | ||
| + * | ||
| + * @param tree Unused. | ||
| + * @return Empty string. | ||
| + */ | ||
| + protected String stop( Tree<T> tree ) { | ||
| + return ""; | ||
| + } | ||
| +} | ||
| + | ||
| Delta | 123 lines added, 98 lines removed, 25-line increase |
|---|