Java Code Examples for antlr.collections.AST#setFirstChild()

The following examples show how to use antlr.collections.AST#setFirstChild() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OrderByFragmentParser.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private SortSpecification createSortSpecification(
		AST ident,
		CollationSpecification collationSpecification,
		OrderingSpecification orderingSpecification) {
	AST sortSpecification = getASTFactory().create( SORT_SPEC, "{{sort specification}}" );
	AST sortKey = getASTFactory().create( SORT_KEY, "{{sort key}}" );
	AST newIdent = getASTFactory().create( ident.getType(), ident.getText() );
	sortKey.setFirstChild( newIdent );
	sortSpecification.setFirstChild( sortKey );
	if ( collationSpecification != null ) {
		sortSpecification.addChild( collationSpecification );
	}
	if ( orderingSpecification != null ) {
		sortSpecification.addChild( orderingSpecification );
	}
	return (SortSpecification) sortSpecification;
}
 
Example 2
Source File: HqlSqlWalker.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private AST generateSyntheticDotNodeForNonQualifiedPropertyRef(AST property, FromElement fromElement) {
	AST dot = getASTFactory().create( DOT, "{non-qualified-property-ref}" );
	// TODO : better way?!?
	( (DotNode) dot ).setPropertyPath( ( (FromReferenceNode) property ).getPath() );

	IdentNode syntheticAlias = (IdentNode) getASTFactory().create( IDENT, "{synthetic-alias}" );
	syntheticAlias.setFromElement( fromElement );
	syntheticAlias.setResolved();

	dot.setFirstChild( syntheticAlias );
	dot.addChild( property );

	return dot;
}
 
Example 3
Source File: SyntheticAndFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void addDiscriminatorWhereFragment(RestrictableStatement statement, Queryable persister, Map enabledFilters, String alias) {
	String whereFragment = persister.filterFragment( alias, enabledFilters ).trim();
	if ( "".equals( whereFragment ) ) {
		return;
	}
	if ( whereFragment.startsWith( "and" ) ) {
		whereFragment = whereFragment.substring( 4 );
	}

	// Need to parse off the column qualifiers; this is assuming (which is true as of now)
	// that this is only used from update and delete HQL statement parsing
	whereFragment = StringHelper.replace( whereFragment, persister.generateFilterConditionAlias( alias ) + ".", "" );

	// Note: this simply constructs a "raw" SQL_TOKEN representing the
	// where fragment and injects this into the tree.  This "works";
	// however it is probably not the best long-term solution.
	//
	// At some point we probably want to apply an additional grammar to
	// properly tokenize this where fragment into constituent parts
	// focused on the operators embedded within the fragment.
	AST discrimNode = astFactory.create( SQL_TOKEN, whereFragment );

	if ( statement.getWhereClause().getNumberOfChildren() == 0 ) {
		statement.getWhereClause().setFirstChild( discrimNode );
	}
	else {
		AST and = astFactory.create( AND, "{and}" );
		AST currentFirstChild = statement.getWhereClause().getFirstChild();
		and.setFirstChild( discrimNode );
		and.addChild( currentFirstChild );
		statement.getWhereClause().setFirstChild( and );
	}
}
 
Example 4
Source File: ASTUtil.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Inserts the child as the first child of the parent, all other children are shifted over to the 'right'.
 *
 * @param parent the parent
 * @param child  the new first child
 */
public static void insertChild(AST parent, AST child) {
	if ( parent.getFirstChild() == null ) {
		parent.setFirstChild( child );
	}
	else {
		AST n = parent.getFirstChild();
		parent.setFirstChild( child );
		child.setNextSibling( n );
	}
}
 
Example 5
Source File: ASTUtil.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Makes the child node a sibling of the parent, reconnecting all siblings.
 *
 * @param parent the parent
 * @param child  the child
 */
public static void makeSiblingOfParent(AST parent, AST child) {
	AST prev = findPreviousSibling( parent, child );
	if ( prev != null ) {
		prev.setNextSibling( child.getNextSibling() );
	}
	else { // child == parent.getFirstChild()
		parent.setFirstChild( child.getNextSibling() );
	}
	child.setNextSibling( parent.getNextSibling() );
	parent.setNextSibling( child );
}
 
Example 6
Source File: QueryTranslatorImpl.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void handleDotStructure(AST dotStructureRoot) {
	String expression = ASTUtil.getPathText( dotStructureRoot );
	Object constant = ReflectHelper.getConstantValue( expression );
	if ( constant != null ) {
		dotStructureRoot.setFirstChild( null );
		dotStructureRoot.setType( HqlTokenTypes.JAVA_CONSTANT );
		dotStructureRoot.setText( expression );
	}
}
 
Example 7
Source File: BinaryLogicOperatorNode.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Mutate the subtree relating to a row-value-constructor to instead use
 * a series of ANDed predicates.  This allows multi-column type comparisons
 * and explicit row-value-constructor syntax even on databases which do
 * not support row-value-constructor.
 * <p/>
 * For example, here we'd mutate "... where (col1, col2) = ('val1', 'val2) ..." to
 * "... where col1 = 'val1' and col2 = 'val2' ..."
 *
 * @param valueElements The number of elements in the row value constructor list.
 */
private void mutateRowValueConstructorSyntax(int valueElements) {
	// mutation depends on the types of nodes invloved...
	int comparisonType = getType();
	String comparisonText = getText();
	setType( HqlSqlTokenTypes.AND );
	setText( "AND" );
	String[] lhsElementTexts = extractMutationTexts( getLeftHandOperand(), valueElements );
	String[] rhsElementTexts = extractMutationTexts( getRightHandOperand(), valueElements );

	AST container = this;
	for ( int i = valueElements - 1; i > 0; i-- ) {

		if ( i == 1 ) {
			AST op1 = getASTFactory().create( comparisonType, comparisonText );
			AST lhs1 = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, lhsElementTexts[0] );
			AST rhs1 = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, rhsElementTexts[0] );
			op1.setFirstChild( lhs1 );
			lhs1.setNextSibling( rhs1 );
			container.setFirstChild( op1 );
			AST op2 = getASTFactory().create( comparisonType, comparisonText );
			AST lhs2 = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, lhsElementTexts[1] );
			AST rhs2 = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, rhsElementTexts[1] );
			op2.setFirstChild( lhs2 );
			lhs2.setNextSibling( rhs2 );
			op1.setNextSibling( op2 );
		}
		else {
			AST op = getASTFactory().create( comparisonType, comparisonText );
			AST lhs = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, lhsElementTexts[i] );
			AST rhs = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, rhsElementTexts[i] );
			op.setFirstChild( lhs );
			lhs.setNextSibling( rhs );
			AST newContainer = getASTFactory().create( HqlSqlTokenTypes.AND, "AND" );
			container.setFirstChild( newContainer );
			newContainer.setNextSibling( op );
			container = newContainer;
		}
	}
}
 
Example 8
Source File: ASTFactory.java    From dacapobench with Apache License 2.0 5 votes vote down vote up
/** Make a tree from a list of nodes.  The first element in the
 *  array is the root.  If the root is null, then the tree is
 *  a simple list not a tree.  Handles null children nodes correctly.
 *  For example, build(a, b, null, c) yields tree (a b c).  build(null,a,b)
 *  yields tree (nil a b).
 */
public AST make(AST[] nodes) {
    if (nodes == null || nodes.length == 0) return null;
    AST root = nodes[0];
    AST tail = null;
    if (root != null) {
        root.setFirstChild(null);	// don't leave any old pointers set
    }
    // link in children;
    for (int i = 1; i < nodes.length; i++) {
        if (nodes[i] == null) continue;	// ignore null nodes
        if (root == null) {
            // Set the root and set it up for a flat list
            root = tail = nodes[i];
        }
        else if (tail == null) {
            root.setFirstChild(nodes[i]);
            tail = root.getFirstChild();
        }
        else {
            tail.setNextSibling(nodes[i]);
            tail = tail.getNextSibling();
        }
        // Chase tail to last sibling
        while (tail.getNextSibling() != null) {
            tail = tail.getNextSibling();
        }
    }
    return root;
}
 
Example 9
Source File: ASTFactory.java    From dacapobench with Apache License 2.0 5 votes vote down vote up
/**Duplicate a tree, assuming this is a root node of a tree--
 * duplicate that node and what's below; ignore siblings of root node.
 */
public AST dupTree(AST t) {
    AST result = dup(t);		// make copy of root
    // copy all children of root.
    if (t != null) {
        result.setFirstChild(dupList(t.getFirstChild()));
    }
    return result;
}
 
Example 10
Source File: HqlSqlWalker.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private AST generateSyntheticDotNodeForNonQualifiedPropertyRef(AST property, FromElement fromElement) {
	AST dot = getASTFactory().create( DOT, "{non-qualified-property-ref}" );
	// TODO : better way?!?
	( ( DotNode ) dot ).setPropertyPath( ( ( FromReferenceNode ) property ).getPath() );

	IdentNode syntheticAlias = ( IdentNode ) getASTFactory().create( IDENT, "{synthetic-alias}" );
	syntheticAlias.setFromElement( fromElement );
	syntheticAlias.setResolved();

	dot.setFirstChild( syntheticAlias );
	dot.addChild( property );

	return dot;
}
 
Example 11
Source File: ASTUtil.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static void appendChild(AST parent, AST child) {
	if ( parent.getFirstChild() == null ) {
		parent.setFirstChild( child );
	}
	else {
		getLastChild( parent ).setNextSibling( child );
	}
}
 
Example 12
Source File: ASTUtil.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Inserts the child as the first child of the parent, all other children are shifted over to the 'right'.
 *
 * @param parent the parent
 * @param child the new first child
 */
public static void insertChild(AST parent, AST child) {
	if ( parent.getFirstChild() == null ) {
		parent.setFirstChild( child );
	}
	else {
		AST n = parent.getFirstChild();
		parent.setFirstChild( child );
		child.setNextSibling( n );
	}
}
 
Example 13
Source File: ASTUtil.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Makes the child node a sibling of the parent, reconnecting all siblings.
 *
 * @param parent the parent
 * @param child the child
 */
public static void makeSiblingOfParent(AST parent, AST child) {
	AST prev = findPreviousSibling( parent, child );
	if ( prev != null ) {
		prev.setNextSibling( child.getNextSibling() );
	}
	else { // child == parent.getFirstChild()
		parent.setFirstChild( child.getNextSibling() );
	}
	child.setNextSibling( parent.getNextSibling() );
	parent.setNextSibling( child );
}
 
Example 14
Source File: QueryTranslatorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void handleDotStructure(AST dotStructureRoot) {
	final String expression = ASTUtil.getPathText( dotStructureRoot );
	final Object constant = ReflectHelper.getConstantValue( expression, factory );
	if ( constant != null ) {
		dotStructureRoot.setFirstChild( null );
		dotStructureRoot.setType( HqlTokenTypes.JAVA_CONSTANT );
		dotStructureRoot.setText( expression );
	}
}
 
Example 15
Source File: AbstractNullnessCheckNode.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void mutateRowValueConstructorSyntax(int operandColumnSpan) {
	final int comparisonType = getType();
	final String comparisonText = getText();

	final int expansionConnectorType = getExpansionConnectorType();
	final String expansionConnectorText = getExpansionConnectorText();

	setType( expansionConnectorType );
	setText( expansionConnectorText );

	String[] mutationTexts = extractMutationTexts( getOperand(), operandColumnSpan );

	AST container = this;
	for ( int i = operandColumnSpan - 1; i > 0; i-- ) {
		if ( i == 1 ) {
			AST op1 = getASTFactory().create( comparisonType, comparisonText );
			AST operand1 = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, mutationTexts[0] );
			op1.setFirstChild( operand1 );
			container.setFirstChild( op1 );
			AST op2 = getASTFactory().create( comparisonType, comparisonText );
			AST operand2 = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, mutationTexts[1] );
			op2.setFirstChild( operand2 );
			op1.setNextSibling( op2 );
		}
		else {
			AST op = getASTFactory().create( comparisonType, comparisonText );
			AST operand = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, mutationTexts[i] );
			op.setFirstChild( operand );
			AST newContainer = getASTFactory().create( expansionConnectorType, expansionConnectorText );
			container.setFirstChild( newContainer );
			newContainer.setNextSibling( op );
			container = newContainer;
		}
	}
}
 
Example 16
Source File: HqlSqlWalker.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected void prepareVersioned(AST updateNode, AST versioned) throws SemanticException {
	UpdateStatement updateStatement = (UpdateStatement) updateNode;
	FromClause fromClause = updateStatement.getFromClause();
	if ( versioned != null ) {
		// Make sure that the persister is versioned
		Queryable persister = fromClause.getFromElement().getQueryable();
		if ( !persister.isVersioned() ) {
			throw new SemanticException( "increment option specified for update of non-versioned entity" );
		}

		VersionType versionType = persister.getVersionType();
		if ( versionType instanceof UserVersionType ) {
			throw new SemanticException( "user-defined version types not supported for increment option" );
		}

		AST eq = getASTFactory().create( HqlSqlTokenTypes.EQ, "=" );
		AST versionPropertyNode = generateVersionPropertyNode( persister );

		eq.setFirstChild( versionPropertyNode );

		AST versionIncrementNode = null;
		if ( isTimestampBasedVersion( versionType ) ) {
			versionIncrementNode = getASTFactory().create( HqlSqlTokenTypes.PARAM, "?" );
			ParameterSpecification paramSpec = new VersionTypeSeedParameterSpecification( versionType );
			( (ParameterNode) versionIncrementNode ).setHqlParameterSpecification( paramSpec );
			parameterSpecs.add( 0, paramSpec );
		}
		else {
			// Not possible to simply re-use the versionPropertyNode here as it causes
			// OOM errors due to circularity :(
			versionIncrementNode = getASTFactory().create( HqlSqlTokenTypes.PLUS, "+" );
			versionIncrementNode.setFirstChild( generateVersionPropertyNode( persister ) );
			versionIncrementNode.addChild( getASTFactory().create( HqlSqlTokenTypes.IDENT, "1" ) );
		}

		eq.addChild( versionIncrementNode );

		evaluateAssignment( eq, persister, 0 );

		AST setClause = updateStatement.getSetClause();
		AST currentFirstSetElement = setClause.getFirstChild();
		setClause.setFirstChild( eq );
		eq.setNextSibling( currentFirstSetElement );
	}
}
 
Example 17
Source File: HqlSqlWalker.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected void prepareVersioned(AST updateNode, AST versioned) throws SemanticException {
	UpdateStatement updateStatement = ( UpdateStatement ) updateNode;
	FromClause fromClause = updateStatement.getFromClause();
	if ( versioned != null ) {
		// Make sure that the persister is versioned
		Queryable persister = fromClause.getFromElement().getQueryable();
		if ( !persister.isVersioned() ) {
			throw new SemanticException( "increment option specified for update of non-versioned entity" );
		}

		VersionType versionType = persister.getVersionType();
		if ( versionType instanceof UserVersionType ) {
			throw new SemanticException( "user-defined version types not supported for increment option" );
		}

		AST eq = getASTFactory().create( HqlSqlTokenTypes.EQ, "=" );
		AST versionPropertyNode = generateVersionPropertyNode( persister );

		eq.setFirstChild( versionPropertyNode );

		AST versionIncrementNode = null;
		if ( Date.class.isAssignableFrom( versionType.getReturnedClass() ) ) {
			versionIncrementNode = getASTFactory().create( HqlSqlTokenTypes.PARAM, "?" );
			ParameterSpecification paramSpec = new VersionTypeSeedParameterSpecification( versionType );
			( ( ParameterNode ) versionIncrementNode ).setHqlParameterSpecification( paramSpec );
			parameters.add( 0, paramSpec );
		}
		else {
			// Not possible to simply re-use the versionPropertyNode here as it causes
			// OOM errors due to circularity :(
			versionIncrementNode = getASTFactory().create( HqlSqlTokenTypes.PLUS, "+" );
			versionIncrementNode.setFirstChild( generateVersionPropertyNode( persister ) );
			versionIncrementNode.addChild( getASTFactory().create( HqlSqlTokenTypes.IDENT, "1" ) );
		}

		eq.addChild( versionIncrementNode );

		evaluateAssignment( eq, persister, 0 );

		AST setClause = updateStatement.getSetClause();
		AST currentFirstSetElement = setClause.getFirstChild();
		setClause.setFirstChild( eq );
		eq.setNextSibling( currentFirstSetElement );
	}
}
 
Example 18
Source File: SyntheticAndFactory.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void addDiscriminatorWhereFragment(
		RestrictableStatement statement,
		Queryable persister,
		Map enabledFilters,
		String alias) {
	String whereFragment = persister.filterFragment( alias, enabledFilters ).trim();
	if ( "".equals( whereFragment ) ) {
		return;
	}
	if ( whereFragment.startsWith( "and" ) ) {
		whereFragment = whereFragment.substring( 4 );
	}

	// Need to parse off the column qualifiers; this is assuming (which is true as of now)
	// that this is only used from update and delete HQL statement parsing
	whereFragment = StringHelper.replace(
			whereFragment,
			persister.generateFilterConditionAlias( alias ) + ".",
			""
	);

	// Note: this simply constructs a "raw" SQL_TOKEN representing the
	// where fragment and injects this into the tree.  This "works";
	// however it is probably not the best long-term solution.
	//
	// At some point we probably want to apply an additional grammar to
	// properly tokenize this where fragment into constituent parts
	// focused on the operators embedded within the fragment.
	SqlFragment discrimNode = (SqlFragment) create( SQL_TOKEN, whereFragment );

	JoinProcessor.processDynamicFilterParameters(
			whereFragment,
			discrimNode,
			hqlSqlWalker
	);

	if ( statement.getWhereClause().getNumberOfChildren() == 0 ) {
		statement.getWhereClause().setFirstChild( discrimNode );
	}
	else {
		AST and = create( AND, "{and}" );
		AST currentFirstChild = statement.getWhereClause().getFirstChild();
		and.setFirstChild( discrimNode );
		and.addChild( currentFirstChild );
		statement.getWhereClause().setFirstChild( and );
	}
}
 
Example 19
Source File: BinaryLogicOperatorNode.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
protected void translate(
		int valueElements, int comparisonType,
		String comparisonText, String[] lhsElementTexts,
		String[] rhsElementTexts,
		ParameterSpecification lhsEmbeddedCompositeParameterSpecification,
		ParameterSpecification rhsEmbeddedCompositeParameterSpecification,
		AST container) {
	for ( int i = valueElements - 1; i > 0; i-- ) {
		if ( i == 1 ) {
			AST op1 = getASTFactory().create( comparisonType, comparisonText );
			AST lhs1 = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, lhsElementTexts[0] );
			AST rhs1 = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, rhsElementTexts[0] );
			op1.setFirstChild( lhs1 );
			lhs1.setNextSibling( rhs1 );
			container.setFirstChild( op1 );
			AST op2 = getASTFactory().create( comparisonType, comparisonText );
			AST lhs2 = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, lhsElementTexts[1] );
			AST rhs2 = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, rhsElementTexts[1] );
			op2.setFirstChild( lhs2 );
			lhs2.setNextSibling( rhs2 );
			op1.setNextSibling( op2 );

			// "pass along" our initial embedded parameter node(s) to the first generated
			// sql fragment so that it can be handled later for parameter binding...
			SqlFragment fragment = (SqlFragment) lhs1;
			if ( lhsEmbeddedCompositeParameterSpecification != null ) {
				fragment.addEmbeddedParameter( lhsEmbeddedCompositeParameterSpecification );
			}
			if ( rhsEmbeddedCompositeParameterSpecification != null ) {
				fragment.addEmbeddedParameter( rhsEmbeddedCompositeParameterSpecification );
			}
		}
		else {
			AST op = getASTFactory().create( comparisonType, comparisonText );
			AST lhs = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, lhsElementTexts[i] );
			AST rhs = getASTFactory().create( HqlSqlTokenTypes.SQL_TOKEN, rhsElementTexts[i] );
			op.setFirstChild( lhs );
			lhs.setNextSibling( rhs );
			AST newContainer = getASTFactory().create( container.getType(), container.getText() );
			container.setFirstChild( newContainer );
			newContainer.setNextSibling( op );
			container = newContainer;
		}
	}
}