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

The following examples show how to use antlr.collections.AST#getNumberOfChildren() . 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: AbstractTableBasedBulkIdHandler.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Interprets the {@code WHERE} clause from the user-defined update/delete  query
 *
 * @param whereClause The user-defined {@code WHERE} clause
 *
 * @return The bulk-id-ready {@code WHERE} clause representation
 */
@SuppressWarnings("unchecked")
protected ProcessedWhereClause processWhereClause(AST whereClause) {
	if ( whereClause.getNumberOfChildren() != 0 ) {
		// If a where clause was specified in the update/delete query, use it to limit the
		// ids that will be returned and inserted into the id table...
		try {
			SqlGenerator sqlGenerator = new SqlGenerator( sessionFactory );
			sqlGenerator.whereClause( whereClause );
			String userWhereClause = sqlGenerator.getSQL().substring( 7 );  // strip the " where "
			List<ParameterSpecification> idSelectParameterSpecifications = sqlGenerator.getCollectedParameters();

			return new ProcessedWhereClause( userWhereClause, idSelectParameterSpecifications );
		}
		catch ( RecognitionException e ) {
			throw new HibernateException( "Unable to generate id select for DML operation", e );
		}
	}
	else {
		return ProcessedWhereClause.NO_WHERE_CLAUSE;
	}
}
 
Example 2
Source File: SelectExpressionList.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Returns an array of SelectExpressions gathered from the children of the given parent AST node.
 *
 * @return an array of SelectExpressions gathered from the children of the given parent AST node.
 */
public SelectExpression[] collectSelectExpressions() {
	// Get the first child to be considered.  Sub-classes may do this differently in order to skip nodes that
	// are not select expressions (e.g. DISTINCT).
	AST firstChild = getFirstSelectExpression();
	AST parent = this;
	ArrayList list = new ArrayList( parent.getNumberOfChildren() );
	for ( AST n = firstChild; n != null; n = n.getNextSibling() ) {
		if ( n instanceof SelectExpression ) {
			list.add( n );
		}
		else {
			throw new IllegalStateException( "Unexpected AST: " + n.getClass().getName() + " " + new ASTPrinter( SqlTokenTypes.class ).showAsString( n, "" ) );
		}
	}
	return ( SelectExpression[] ) list.toArray( new SelectExpression[list.size()] );
}
 
Example 3
Source File: HqlParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Post process equality expressions, clean up the subtree.
 *
 * @param x The equality expression.
 *
 * @return AST - The clean sub-tree.
 */
@Override
public AST processEqualityExpression(AST x) {
	if ( x == null ) {
		LOG.processEqualityExpression();
		return null;
	}

	int type = x.getType();
	if ( type == EQ || type == NE ) {
		boolean negated = type == NE;
		if ( x.getNumberOfChildren() == 2 ) {
			AST a = x.getFirstChild();
			AST b = a.getNextSibling();
			// (EQ NULL b) => (IS_NULL b)
			if ( a.getType() == NULL && b.getType() != NULL ) {
				return createIsNullParent( b, negated );
			}
			// (EQ a NULL) => (IS_NULL a)
			else if ( b.getType() == NULL && a.getType() != NULL ) {
				return createIsNullParent( a, negated );
			}
			else if ( b.getType() == EMPTY ) {
				return processIsEmpty( a, negated );
			}
			else {
				return x;
			}
		}
		else {
			return x;
		}
	}
	else {
		return x;
	}
}
 
Example 4
Source File: HqlParser.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Post process equality expressions, clean up the subtree.
 *
 * @param x The equality expression.
 * @return AST - The clean sub-tree.
 */
public AST processEqualityExpression(AST x) {
	if ( x == null ) {
		log.warn( "processEqualityExpression() : No expression to process!" );
		return null;
	}

	int type = x.getType();
	if ( type == EQ || type == NE ) {
		boolean negated = type == NE;
		if ( x.getNumberOfChildren() == 2 ) {
			AST a = x.getFirstChild();
			AST b = a.getNextSibling();
			// (EQ NULL b) => (IS_NULL b)
			if ( a.getType() == NULL && b.getType() != NULL ) {
				return createIsNullParent( b, negated );
			}
			// (EQ a NULL) => (IS_NULL a)
			else if ( b.getType() == NULL && a.getType() != NULL ) {
				return createIsNullParent( a, negated );
			}
			else if ( b.getType() == EMPTY ) {
				return processIsEmpty( a, negated );
			}
			else {
				return x;
			}
		}
		else {
			return x;
		}
	}
	else {
		return x;
	}
}
 
Example 5
Source File: AbstractRestrictableStatement.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public final boolean hasWhereClause() {
	AST whereClause = locateWhereClause();
	return whereClause != null && whereClause.getNumberOfChildren() > 0;
}
 
Example 6
Source File: AbstractRestrictableStatement.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * @see RestrictableStatement#hasWhereClause
 */
public final boolean hasWhereClause() {
	AST whereClause = locateWhereClause();
	return whereClause != null && whereClause.getNumberOfChildren() > 0;
}
 
Example 7
Source File: AbstractStatementExecutor.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected String generateIdInsertSelect(Queryable persister, String tableAlias, AST whereClause) {
	Select select = new Select( getFactory().getDialect() );
	SelectFragment selectFragment = new SelectFragment()
			.addColumns( tableAlias, persister.getIdentifierColumnNames(), persister.getIdentifierColumnNames() );
	select.setSelectClause( selectFragment.toFragmentString().substring( 2 ) );

	String rootTableName = persister.getTableName();
	String fromJoinFragment = persister.fromJoinFragment( tableAlias, true, false );
	String whereJoinFragment = persister.whereJoinFragment( tableAlias, true, false );

	select.setFromClause( rootTableName + ' ' + tableAlias + fromJoinFragment );

	if ( whereJoinFragment == null ) {
		whereJoinFragment = "";
	}
	else {
		whereJoinFragment = whereJoinFragment.trim();
		if ( whereJoinFragment.startsWith( "and" ) ) {
			whereJoinFragment = whereJoinFragment.substring( 4 );
		}
	}

	String userWhereClause = "";
	if ( whereClause.getNumberOfChildren() != 0 ) {
		// If a where clause was specified in the update/delete query, use it to limit the
		// returned ids here...
		try {
			SqlGenerator sqlGenerator = new SqlGenerator( getFactory() );
			sqlGenerator.whereClause( whereClause );
			userWhereClause = sqlGenerator.getSQL().substring( 7 );  // strip the " where "
		}
		catch ( RecognitionException e ) {
			throw new HibernateException( "Unable to generate id select for DML operation", e );
		}
		if ( whereJoinFragment.length() > 0 ) {
			whereJoinFragment += " and ";
		}
	}

	select.setWhereClause( whereJoinFragment + userWhereClause );

	InsertSelect insert = new InsertSelect( getFactory().getDialect() );
	if ( getFactory().getSettings().isCommentsEnabled() ) {
		insert.setComment( "insert-select for " + persister.getEntityName() + " ids" );
	}
	insert.setTableName( persister.getTemporaryIdTableName() );
	insert.setSelect( select );
	return insert.toStatementString();
}