| root > people, # "root" keyword starts the document | ||
| person > person, # maps table context to a node | ||
| +.age > @age, # @ maps a column to an attribute node | ||
| .first_name > first, # maps a column to a node | ||
| .last_name > name/last, # maps a column to an ancestor node | ||
| -.age > @age, # @ maps a column to an attribute node | ||
| account.person_id +> person.person_id, # +> performs an INNER JOIN | ||
| account > account, # context is now "account" node |
| import java.nio.CharBuffer; | ||
| +import java.util.ArrayDeque; | ||
| import java.util.ArrayList; | ||
| +import java.util.Deque; | ||
| import java.util.List; | ||
| + | ||
| +import org.antlr.v4.runtime.ParserRuleContext; | ||
| import org.antlr.v4.runtime.ANTLRInputStream; | ||
| /** Tracks the SQL statement insertion point. */ | ||
| private int depth = 0; | ||
| + | ||
| + private Tree<ParserRuleContext> tree; | ||
| /** | ||
| ParseTreeWalker walker = new ParseTreeWalker(); | ||
| walker.walk( this, ctx ); | ||
| + } | ||
| + | ||
| + @Override | ||
| + public synchronized void enterQuery( QueryParser.QueryContext ctx ) { | ||
| + setTree( new Tree<ParserRuleContext>( ctx ) ); | ||
| } | ||
| @Override | ||
| public synchronized void enterRoot( QueryParser.RootContext ctx ) { | ||
| String root = ctx.getChild(2).getText(); | ||
| - push( "SELECT XMLROOT(" ); | ||
| + push( "XMLROOT(" ); | ||
| + push( String.format( "XMLELEMENT( NAME \"%s\"", root ) ); | ||
| - push( "XMLELEMENT( NAME " + root ); | ||
| + decreaseDepth(); | ||
| push( ")" ); | ||
| - | ||
| + decreaseDepth(); | ||
| push( ", VERSION '1.0', STANDALONE YES)" ); | ||
| } | ||
| private int getDepth() { | ||
| return this.depth; | ||
| + } | ||
| + | ||
| + private void setTree( Tree<ParserRuleContext> tree ) { | ||
| + this.tree = tree; | ||
| + } | ||
| + | ||
| + private Tree<ParserRuleContext> getTree() { | ||
| + return this.tree; | ||
| } | ||
| } | ||
| +package com.whitemagicsoftware.rxm; | ||
| + | ||
| +import java.util.ArrayList; | ||
| +import java.util.HashMap; | ||
| +import java.util.List; | ||
| +import java.util.Map; | ||
| + | ||
| +/** | ||
| + * Stores a tree of values. | ||
| + */ | ||
| +public class Tree<T> { | ||
| + private T payload; | ||
| + | ||
| + private Tree<T> parent; | ||
| + private List<Tree<T>> leaves = new ArrayList<Tree<T>>(); | ||
| + private Map<T, Tree<T>> map = new HashMap<T, Tree<T>>(); | ||
| + | ||
| + public Tree( T payload ) { | ||
| + setPayload( payload ); | ||
| + put( payload, this ); | ||
| + } | ||
| + | ||
| + public Tree<T> addLeaf( T leaf ) { | ||
| + Tree<T> tree = new Tree<T>( leaf ); | ||
| + add( tree ); | ||
| + | ||
| + put( leaf, tree ); | ||
| + return tree; | ||
| + } | ||
| + | ||
| + public void addLeaf( T root, T leaf ) { | ||
| + if( contains( root ) ) { | ||
| + get( root ).addLeaf( leaf ); | ||
| + } | ||
| + else { | ||
| + addLeaf( root ).addLeaf( leaf ); | ||
| + } | ||
| + } | ||
| + | ||
| + private void add( Tree<T> tree ) { | ||
| + getLeaves().add( tree ); | ||
| + tree.setParent( this ); | ||
| + tree.setMap( getMap() ); | ||
| + } | ||
| + | ||
| + private boolean contains( T root ) { | ||
| + return getMap().containsKey( root ); | ||
| + } | ||
| + | ||
| + private void put( T payload, Tree<T> parent ) { | ||
| + getMap().put( payload, parent ); | ||
| + } | ||
| + | ||
| + private Tree<T> get( T payload ) { | ||
| + return getMap().get( payload ); | ||
| + } | ||
| + | ||
| + private void setParent( Tree<T> parent ) { | ||
| + this.parent = parent; | ||
| + } | ||
| + | ||
| + private Tree<T> getParent() { | ||
| + return this.parent; | ||
| + } | ||
| + | ||
| + private void setPayload( T payload ) { | ||
| + this.payload = payload; | ||
| + } | ||
| + | ||
| + public T getPayload() { | ||
| + return this.payload; | ||
| + } | ||
| + | ||
| + private void setMap( Map<T, Tree<T>> map ) { | ||
| + this.map = map; | ||
| + } | ||
| + | ||
| + private Map<T, Tree<T>> getMap() { | ||
| + return this.map; | ||
| + } | ||
| + | ||
| + private List<Tree<T>> getLeaves() { | ||
| + return this.leaves; | ||
| + } | ||
| +} | ||
| + | ||
| root > people, | ||
| person > person, | ||
| +.age > @age, | ||
| .first_name > first, | ||
| .last_name > name/last, | ||
| -.age > @age, | ||
| account.person_id +> person.person_id, | ||
| account > account, | ||
| .id > @id, | ||
| ^, | ||
| address > address, | ||
| .*, | ||
| -account.person_id -> company.person_id, | ||
| +^, | ||
| +company.person_id -> person.person_id, | ||
| +.id > @id, | ||
| +.legal_name > name, | ||
| ; | ||
| Author | Dave Jarvis <email> |
|---|---|
| Date | 2015-03-04 23:59:08 GMT-0800 |
| Commit | f79fa776d18a0446051a2945e8f769e0fa525483 |
| Parent | cff74d9 |
| Delta | 115 lines added, 6 lines removed, 109-line increase |