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

The following examples show how to use antlr.collections.AST#setNextSibling() . 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: QueryNode.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public final OrderByClause getOrderByClause() {
	if ( orderByClause == null ) {
		orderByClause = locateOrderByClause();

		// if there is no order by, make one
		if ( orderByClause == null ) {
			LOG.debug( "getOrderByClause() : Creating a new ORDER BY clause" );
			orderByClause = (OrderByClause) getWalker().getASTFactory().create( SqlTokenTypes.ORDER, "ORDER" );

			// Find the WHERE; if there is no WHERE, find the FROM...
			AST prevSibling = ASTUtil.findTypeInChildren( this, SqlTokenTypes.WHERE );
			if ( prevSibling == null ) {
				prevSibling = ASTUtil.findTypeInChildren( this, SqlTokenTypes.FROM );
			}

			// Now, inject the newly built ORDER BY into the tree
			orderByClause.setNextSibling( prevSibling.getNextSibling() );
			prevSibling.setNextSibling( orderByClause );
		}
	}
	return orderByClause;
}
 
Example 2
Source File: AbstractRestrictableStatement.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public final AST getWhereClause() {
	if ( whereClause == null ) {
		whereClause = locateWhereClause();
		// If there is no WHERE node, make one.
		if ( whereClause == null ) {
			getLog().debug( "getWhereClause() : Creating a new WHERE clause..." );
			whereClause = getWalker().getASTFactory().create( HqlSqlTokenTypes.WHERE, "WHERE" );
			// inject the WHERE after the parent
			AST parent = ASTUtil.findTypeInChildren( this, getWhereClauseParentTokenType() );
			whereClause.setNextSibling( parent.getNextSibling() );
			parent.setNextSibling( whereClause );
		}
	}
	return whereClause;
}
 
Example 3
Source File: AbstractRestrictableStatement.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * @see org.hibernate.hql.ast.tree.RestrictableStatement#getWhereClause
 */
public final AST getWhereClause() {
	if ( whereClause == null ) {
		whereClause = locateWhereClause();
		// If there is no WHERE node, make one.
		if ( whereClause == null ) {
			getLog().debug( "getWhereClause() : Creating a new WHERE clause..." );
			whereClause = ASTUtil.create( getWalker().getASTFactory(), HqlSqlTokenTypes.WHERE, "WHERE" );
			// inject the WHERE after the parent
			AST parent = ASTUtil.findTypeInChildren( this, getWhereClauseParentTokenType() );
			whereClause.setNextSibling( parent.getNextSibling() );
			parent.setNextSibling( whereClause );
		}
	}
	return whereClause;
}
 
Example 4
Source File: QueryNode.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public final OrderByClause getOrderByClause() {
	if ( orderByClause == null ) {
		orderByClause = locateOrderByClause();

		// if there is no order by, make one
		if ( orderByClause == null ) {
			log.debug( "getOrderByClause() : Creating a new ORDER BY clause" );
			orderByClause = ( OrderByClause ) ASTUtil.create( getWalker().getASTFactory(), SqlTokenTypes.ORDER, "ORDER" );

			// Find the WHERE; if there is no WHERE, find the FROM...
			AST prevSibling = ASTUtil.findTypeInChildren( this, SqlTokenTypes.WHERE );
			if ( prevSibling == null ) {
				prevSibling = ASTUtil.findTypeInChildren( this, SqlTokenTypes.FROM );
			}

			// Now, inject the newly built ORDER BY into the tree
			orderByClause.setNextSibling( prevSibling.getNextSibling() );
			prevSibling.setNextSibling( orderByClause );
		}
	}
	return orderByClause;
}
 
Example 5
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 6
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 7
Source File: HqlSqlWalker.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void createSelectClauseFromFromClause(QueryNode qn) throws SemanticException {
	AST select = astFactory.create( SELECT_CLAUSE, "{derived select clause}" );
	AST sibling = qn.getFromClause();
	qn.setFirstChild( select );
	select.setNextSibling( sibling );
	selectClause = ( SelectClause ) select;
	selectClause.initializeDerivedSelectClause( currentFromClause );
	if ( log.isDebugEnabled() ) {
		log.debug( "Derived SELECT clause created." );
	}
}
 
Example 8
Source File: HqlParser.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private AST processIsEmpty(AST node, boolean negated) {
	node.setNextSibling( null );
	// NOTE: Because we're using ASTUtil.createParent(), the tree must be created from the bottom up.
	// IS EMPTY x => (EXISTS (QUERY (SELECT_FROM (FROM x) ) ) )
	AST ast = createSubquery( node );
	ast = ASTUtil.createParent( astFactory, EXISTS, "exists", ast );
	// Add NOT if it's negated.
	if ( !negated ) {
		ast = ASTUtil.createParent( astFactory, NOT, "not", ast );
	}
	return ast;
}
 
Example 9
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 10
Source File: ASTFactory.java    From dacapobench with Apache License 2.0 5 votes vote down vote up
/** Duplicate tree including siblings of root. */
public AST dupList(AST t) {
    AST result = dupTree(t);            // if t == null, then result==null
    AST nt = result;
    while (t != null) {						// for each sibling of the root
        t = t.getNextSibling();
        nt.setNextSibling(dupTree(t));	// dup each subtree, building new tree
        nt = nt.getNextSibling();
    }
    return result;
}
 
Example 11
Source File: HqlParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private AST processIsEmpty(AST node, boolean negated) {
	node.setNextSibling( null );
	// NOTE: Because we're using ASTUtil.createParent(), the tree must be created from the bottom up.
	// IS EMPTY x => (EXISTS (QUERY (SELECT_FROM (FROM x) ) ) )
	AST ast = createSubquery( node );
	ast = ASTUtil.createParent( astFactory, EXISTS, "exists", ast );
	// Add NOT if it's negated.
	if ( !negated ) {
		ast = ASTUtil.createParent( astFactory, NOT, "not", ast );
	}
	return ast;
}
 
Example 12
Source File: HqlSqlWalker.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void createSelectClauseFromFromClause(QueryNode qn) throws SemanticException {
	AST select = astFactory.create( SELECT_CLAUSE, "{derived select clause}" );
	AST sibling = qn.getFromClause();
	qn.setFirstChild( select );
	select.setNextSibling( sibling );
	selectClause = (SelectClause) select;
	selectClause.initializeDerivedSelectClause( currentFromClause );
	LOG.debug( "Derived SELECT clause created." );
}
 
Example 13
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 14
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 15
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 16
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 17
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 18
Source File: ASTUtil.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static void appendSibling(AST n, AST s) {
	while ( n.getNextSibling() != null ) {
		n = n.getNextSibling();
	}
	n.setNextSibling( s );
}
 
Example 19
Source File: HqlParser.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private AST createIsNullParent(AST node, boolean negated) {
	node.setNextSibling( null );
	int type = negated ? IS_NOT_NULL : IS_NULL;
	String text = negated ? "is not null" : "is null";
	return ASTUtil.createParent( astFactory, type, text, node );
}
 
Example 20
Source File: ASTUtil.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Inserts a node into a child subtree as a particularly positioned
 * sibling taking care to properly reorganize the tree to account for this
 * new addition.
 *
 * @param node The node to insert
 * @param prevSibling The previous node at the sibling position
 * where we want this node inserted.
 *
 * @return The return is the same as the node parameter passed in.
 */
public static AST insertSibling(AST node, AST prevSibling) {
	node.setNextSibling( prevSibling.getNextSibling() );
	prevSibling.setNextSibling( node );
	return node;
}