Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/rxm.git

Added working FROM clause.

Author Dave Jarvis <email>
Date 2015-03-16 20:00:12 GMT-0700
Commit 13074c77330a092e26c82b0d0da55d3d3a07518d
Parent 42309d5
source/java/com/whitemagicsoftware/rxm/QueryBuilder.java
}
- /**
- * TODO: implement
- */
private String getFromClause() {
- return "FROM person person ";
+ return ((SelectClauseTree<Payload>)getSelectClauseTree()).getFromClause();
}
source/java/com/whitemagicsoftware/rxm/tree/TransformationTree.java
* @return A non-null string with 'spaces' amount of space.
*/
- protected String getWhitespace( int spaces ) {
+ protected String getSpace( int spaces ) {
return beautify() ? getNewline() + getIndent( spaces ) : " ";
}
/**
- * Helper method.
+ * Helper method to return the default space amount.
*
* @return A non-null string with the default amount of space.
*/
- protected String getWhitespace() {
- return getWhitespace( getIndentBy() );
+ protected String getSpace() {
+ return getSpace( getIndentBy() );
}
source/java/com/whitemagicsoftware/rxm/tree/Tree.java
return index > -1 && index < getBranches().size();
}
+
+ /**
+ * Returns the first branch from this tree's list of branches.
+ *
+ * @return The first branch, possibly null.
+ */
+ public Tree<T> firstBranch() {
+ return getBranches().get(0);
+ }
}
source/java/com/whitemagicsoftware/rxm/tree/xml/PayloadParserRuleContext.java
package com.whitemagicsoftware.rxm.tree.xml;
-import com.whitemagicsoftware.rxm.grammar.QueryParser;
import com.whitemagicsoftware.rxm.tree.Payload;
import com.whitemagicsoftware.rxm.tree.Token;
import com.whitemagicsoftware.rxm.tree.Tree;
+
+import com.whitemagicsoftware.rxm.grammar.QueryParser;
import org.antlr.v4.runtime.ParserRuleContext;
source/java/com/whitemagicsoftware/rxm/tree/xml/RootContext.java
}
+
source/java/com/whitemagicsoftware/rxm/tree/xml/SelectClauseTree.java
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());
}
Delta 49 lines added, 11 lines removed, 38-line increase