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

The following examples show how to use antlr.collections.AST#addChild() . 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: OrderByFragmentParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected AST resolveIdent(AST ident) {
	/*
	 * Semantic action used during recognition of an identifier.  This identifier might be a column name, it might
	 * be a property name.
	 */
	String text = ident.getText();
	SqlValueReference[] sqlValueReferences;
	try {
		sqlValueReferences = context.getColumnMapper().map( text );
	}
	catch (Throwable t) {
		sqlValueReferences = null;
	}

	if ( sqlValueReferences == null || sqlValueReferences.length == 0 ) {
		return getASTFactory().create( OrderByTemplateTokenTypes.IDENT, makeColumnReference( text ) );
	}
	else if ( sqlValueReferences.length == 1 ) {
		return processSqlValueReference( sqlValueReferences[0] );
	}
	else {
		final AST root = getASTFactory().create( OrderByTemplateTokenTypes.IDENT_LIST, "{ident list}" );
		for ( SqlValueReference sqlValueReference : sqlValueReferences ) {
			root.addChild( processSqlValueReference( sqlValueReference ) );
		}
		return root;
	}
}
 
Example 3
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 4
Source File: HqlParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void processMemberOf(Token n, AST p, ASTPair currentAST) {
	// convert MEMBER OF to the equivalent IN ELEMENTS structure...
	AST inNode = n == null ? astFactory.create( IN, "in" ) : astFactory.create( NOT_IN, "not in" );
	astFactory.makeASTRoot( currentAST, inNode );

	AST inListNode = astFactory.create( IN_LIST, "inList" );
	inNode.addChild( inListNode );
	AST elementsNode = astFactory.create( ELEMENTS, "elements" );
	inListNode.addChild( elementsNode );
	elementsNode.addChild( p );
}
 
Example 5
Source File: ASTFactory.java    From dacapobench with Apache License 2.0 5 votes vote down vote up
/** Make an AST the root of current AST */
public void makeASTRoot(ASTPair currentAST, AST root) {
    if (root != null) {
        // Add the current root as a child of new root
        root.addChild(currentAST.root);
        // The new current child is the last sibling of the old root
        currentAST.child = currentAST.root;
        currentAST.advanceChildToEnd();
        // Set the new root
        currentAST.root = root;
    }
}
 
Example 6
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 7
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 8
Source File: HqlParser.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void processMemberOf(Token n, AST p, ASTPair currentAST) {
	AST inAst = n == null ? astFactory.create( IN, "in" ) : astFactory.create( NOT_IN, "not in" );
	astFactory.makeASTRoot( currentAST, inAst );
	AST ast = createSubquery( p );
	ast = ASTUtil.createParent( astFactory, IN_LIST, "inList", ast );
	inAst.addChild( ast );
}
 
Example 9
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 10
Source File: HqlSqlWalker.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
	protected void prepareFromClauseInputTree(AST fromClauseInput) {
		if ( !isSubQuery() ) {
//			// inject param specifications to account for dynamic filter param values
//			if ( ! getEnabledFilters().isEmpty() ) {
//				Iterator filterItr = getEnabledFilters().values().iterator();
//				while ( filterItr.hasNext() ) {
//					FilterImpl filter = ( FilterImpl ) filterItr.next();
//					if ( ! filter.getFilterDefinition().getParameterNames().isEmpty() ) {
//						Iterator paramItr = filter.getFilterDefinition().getParameterNames().iterator();
//						while ( paramItr.hasNext() ) {
//							String parameterName = ( String ) paramItr.next();
//							// currently param filters *only* work with single-column parameter types;
//							// if that limitation is ever lifted, this logic will need to change to account for that
//							ParameterNode collectionFilterKeyParameter = ( ParameterNode ) astFactory.create( PARAM, "?" );
//							DynamicFilterParameterSpecification paramSpec = new DynamicFilterParameterSpecification(
//									filter.getName(),
//									parameterName,
//									filter.getFilterDefinition().getParameterType( parameterName ),
//									 positionalParameterCount++
//							);
//							collectionFilterKeyParameter.setHqlParameterSpecification( paramSpec );
//							parameterSpecs.add( paramSpec );
//						}
//					}
//				}
//			}

			if ( isFilter() ) {
				// Handle collection-filter compilation.
				// IMPORTANT NOTE: This is modifying the INPUT (HQL) tree, not the output tree!
				QueryableCollection persister = sessionFactoryHelper.getCollectionPersister( collectionFilterRole );
				Type collectionElementType = persister.getElementType();
				if ( !collectionElementType.isEntityType() ) {
					throw new QueryException( "collection of values in filter: this" );
				}

				String collectionElementEntityName = persister.getElementPersister().getEntityName();
				ASTFactory inputAstFactory = hqlParser.getASTFactory();
				AST fromElement = inputAstFactory.create( HqlTokenTypes.FILTER_ENTITY, collectionElementEntityName );
				ASTUtil.createSibling( inputAstFactory, HqlTokenTypes.ALIAS, "this", fromElement );
				fromClauseInput.addChild( fromElement );
				// Show the modified AST.
				LOG.debug( "prepareFromClauseInputTree() : Filter - Added 'this' as a from element..." );
				queryTranslatorImpl.showHqlAst( hqlParser.getAST() );

				// Create a parameter specification for the collection filter...
				final Type collectionFilterKeyType = sessionFactoryHelper.requireQueryableCollection( collectionFilterRole )
						.getKeyType();
				final ParameterNode collectionFilterKeyParameter = (ParameterNode) astFactory.create( PARAM, "?" );
				final CollectionFilterKeyParameterSpecification collectionFilterKeyParameterSpec = new CollectionFilterKeyParameterSpecification(
						collectionFilterRole,
						collectionFilterKeyType
				);
				parameterCount++;
				collectionFilterKeyParameter.setHqlParameterSpecification( collectionFilterKeyParameterSpec );
				parameterSpecs.add( collectionFilterKeyParameterSpec );
			}
		}
	}
 
Example 11
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 12
Source File: HqlSqlWalker.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected void prepareFromClauseInputTree(AST fromClauseInput) {
		if ( !isSubQuery() ) {
//			// inject param specifications to account for dynamic filter param values
//			if ( ! getEnabledFilters().isEmpty() ) {
//				Iterator filterItr = getEnabledFilters().values().iterator();
//				while ( filterItr.hasNext() ) {
//					FilterImpl filter = ( FilterImpl ) filterItr.next();
//					if ( ! filter.getFilterDefinition().getParameterNames().isEmpty() ) {
//						Iterator paramItr = filter.getFilterDefinition().getParameterNames().iterator();
//						while ( paramItr.hasNext() ) {
//							String parameterName = ( String ) paramItr.next();
//							// currently param filters *only* work with single-column parameter types;
//							// if that limitation is ever lifted, this logic will need to change to account for that
//							ParameterNode collectionFilterKeyParameter = ( ParameterNode ) astFactory.create( PARAM, "?" );
//							DynamicFilterParameterSpecification paramSpec = new DynamicFilterParameterSpecification(
//									filter.getName(),
//									parameterName,
//									filter.getFilterDefinition().getParameterType( parameterName ),
//									 positionalParameterCount++
//							);
//							collectionFilterKeyParameter.setHqlParameterSpecification( paramSpec );
//							parameters.add( paramSpec );
//						}
//					}
//				}
//			}

			if ( isFilter() ) {
				// Handle collection-fiter compilation.
				// IMPORTANT NOTE: This is modifying the INPUT (HQL) tree, not the output tree!
				QueryableCollection persister = sessionFactoryHelper.getCollectionPersister( collectionFilterRole );
				Type collectionElementType = persister.getElementType();
				if ( !collectionElementType.isEntityType() ) {
					throw new QueryException( "collection of values in filter: this" );
				}

				String collectionElementEntityName = persister.getElementPersister().getEntityName();
				ASTFactory inputAstFactory = hqlParser.getASTFactory();
				AST fromElement = ASTUtil.create( inputAstFactory, HqlTokenTypes.FILTER_ENTITY, collectionElementEntityName );
				ASTUtil.createSibling( inputAstFactory, HqlTokenTypes.ALIAS, "this", fromElement );
				fromClauseInput.addChild( fromElement );
				// Show the modified AST.
				if ( log.isDebugEnabled() ) {
					log.debug( "prepareFromClauseInputTree() : Filter - Added 'this' as a from element..." );
				}
				queryTranslatorImpl.showHqlAst( hqlParser.getAST() );

				// Create a parameter specification for the collection filter...
				Type collectionFilterKeyType = sessionFactoryHelper.requireQueryableCollection( collectionFilterRole ).getKeyType();
				ParameterNode collectionFilterKeyParameter = ( ParameterNode ) astFactory.create( PARAM, "?" );
				CollectionFilterKeyParameterSpecification collectionFilterKeyParameterSpec = new CollectionFilterKeyParameterSpecification(
						collectionFilterRole, collectionFilterKeyType, positionalParameterCount++
				);
				collectionFilterKeyParameter.setHqlParameterSpecification( collectionFilterKeyParameterSpec );
				parameters.add( collectionFilterKeyParameterSpec );
			}
		}
	}
 
Example 13
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 );
	}
}