Search in sources :

Example 11 with ParameterSpecification

use of org.hibernate.param.ParameterSpecification in project hibernate-orm by hibernate.

the class HqlSqlWalker method prepareVersioned.

@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);
            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);
    }
}
Also used : UpdateStatement(org.hibernate.hql.internal.ast.tree.UpdateStatement) UserVersionType(org.hibernate.usertype.UserVersionType) AST(antlr.collections.AST) CollectionFilterKeyParameterSpecification(org.hibernate.param.CollectionFilterKeyParameterSpecification) ParameterSpecification(org.hibernate.param.ParameterSpecification) PositionalParameterSpecification(org.hibernate.param.PositionalParameterSpecification) VersionTypeSeedParameterSpecification(org.hibernate.param.VersionTypeSeedParameterSpecification) NamedParameterSpecification(org.hibernate.param.NamedParameterSpecification) VersionTypeSeedParameterSpecification(org.hibernate.param.VersionTypeSeedParameterSpecification) ParameterNode(org.hibernate.hql.internal.ast.tree.ParameterNode) FromClause(org.hibernate.hql.internal.ast.tree.FromClause) Queryable(org.hibernate.persister.entity.Queryable) UserVersionType(org.hibernate.usertype.UserVersionType) VersionType(org.hibernate.type.VersionType) SemanticException(antlr.SemanticException)

Example 12 with ParameterSpecification

use of org.hibernate.param.ParameterSpecification in project hibernate-orm by hibernate.

the class BinaryLogicOperatorNode method mutateRowValueConstructorSyntax.

/**
	 * 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 involved...
    int comparisonType = getType();
    String comparisonText = getText();
    switch(comparisonType) {
        case HqlSqlTokenTypes.EQ:
            setType(HqlSqlTokenTypes.AND);
            setText("AND");
            break;
        case HqlSqlTokenTypes.NE:
            setType(HqlSqlTokenTypes.OR);
            setText("OR");
            break;
        default:
            throw new QuerySyntaxException(comparisonText + " operator not supported on composite types.");
    }
    String[] lhsElementTexts = extractMutationTexts(getLeftHandOperand(), valueElements);
    String[] rhsElementTexts = extractMutationTexts(getRightHandOperand(), valueElements);
    ParameterSpecification lhsEmbeddedCompositeParameterSpecification = getLeftHandOperand() == null || (!ParameterNode.class.isInstance(getLeftHandOperand())) ? null : ((ParameterNode) getLeftHandOperand()).getHqlParameterSpecification();
    ParameterSpecification rhsEmbeddedCompositeParameterSpecification = getRightHandOperand() == null || (!ParameterNode.class.isInstance(getRightHandOperand())) ? null : ((ParameterNode) getRightHandOperand()).getHqlParameterSpecification();
    translate(valueElements, comparisonType, comparisonText, lhsElementTexts, rhsElementTexts, lhsEmbeddedCompositeParameterSpecification, rhsEmbeddedCompositeParameterSpecification, this);
}
Also used : ParameterSpecification(org.hibernate.param.ParameterSpecification) QuerySyntaxException(org.hibernate.hql.internal.ast.QuerySyntaxException)

Aggregations

ParameterSpecification (org.hibernate.param.ParameterSpecification)12 PreparedStatement (java.sql.PreparedStatement)5 SQLException (java.sql.SQLException)5 AST (antlr.collections.AST)4 ArrayList (java.util.ArrayList)3 RecognitionException (antlr.RecognitionException)2 SemanticException (antlr.SemanticException)2 QueryException (org.hibernate.QueryException)2 SqlGenerator (org.hibernate.hql.internal.ast.SqlGenerator)2 ParameterNode (org.hibernate.hql.internal.ast.tree.ParameterNode)2 CollectionFilterKeyParameterSpecification (org.hibernate.param.CollectionFilterKeyParameterSpecification)2 NamedParameterSpecification (org.hibernate.param.NamedParameterSpecification)2 PositionalParameterSpecification (org.hibernate.param.PositionalParameterSpecification)2 VersionTypeSeedParameterSpecification (org.hibernate.param.VersionTypeSeedParameterSpecification)2 Queryable (org.hibernate.persister.entity.Queryable)2 VersionType (org.hibernate.type.VersionType)2 UserVersionType (org.hibernate.usertype.UserVersionType)2 ResultSet (java.sql.ResultSet)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1