Search in sources :

Example 1 with InFragment

use of org.hibernate.sql.InFragment in project hibernate-orm by hibernate.

the class JoinWalker method whereString.

/**
	 * Render the where condition for a (batch) load by identifier / collection key
	 */
protected StringBuilder whereString(String alias, String[] columnNames, int batchSize) {
    if (columnNames.length == 1) {
        // if not a composite key, use "foo in (?, ?, ?)" for batching
        // if no batch, and not a composite key, use "foo = ?"
        InFragment in = new InFragment().setColumn(alias, columnNames[0]);
        for (int i = 0; i < batchSize; i++) {
            in.addValue("?");
        }
        return new StringBuilder(in.toFragmentString());
    } else {
        //a composite key
        ConditionFragment byId = new ConditionFragment().setTableAlias(alias).setCondition(columnNames, "?");
        StringBuilder whereString = new StringBuilder();
        if (batchSize == 1) {
            // if no batch, use "foo = ? and bar = ?"
            whereString.append(byId.toFragmentString());
        } else {
            // if a composite key, use "( (foo = ? and bar = ?) or (foo = ? and bar = ?) )" for batching
            //TODO: unnecessary for databases with ANSI-style joins
            whereString.append('(');
            DisjunctionFragment df = new DisjunctionFragment();
            for (int i = 0; i < batchSize; i++) {
                df.addCondition(byId);
            }
            whereString.append(df.toFragmentString());
            //TODO: unnecessary for databases with ANSI-style joins
            whereString.append(')');
        }
        return whereString;
    }
}
Also used : InFragment(org.hibernate.sql.InFragment) DisjunctionFragment(org.hibernate.sql.DisjunctionFragment) ConditionFragment(org.hibernate.sql.ConditionFragment)

Example 2 with InFragment

use of org.hibernate.sql.InFragment in project hibernate-orm by hibernate.

the class AbstractLoadQueryDetails method applyKeyRestriction.

private static void applyKeyRestriction(SelectStatementBuilder select, String alias, String[] keyColumnNames, int batchSize) {
    if (keyColumnNames.length == 1) {
        // NOT A COMPOSITE KEY
        // 		for batching, use "foo in (?, ?, ?)" for batching
        //		for no batching, use "foo = ?"
        // (that distinction is handled inside InFragment)
        final InFragment in = new InFragment().setColumn(alias, keyColumnNames[0]);
        for (int i = 0; i < batchSize; i++) {
            in.addValue("?");
        }
        select.appendRestrictions(in.toFragmentString());
    } else {
        // A COMPOSITE KEY...
        final ConditionFragment keyRestrictionBuilder = new ConditionFragment().setTableAlias(alias).setCondition(keyColumnNames, "?");
        final String keyRestrictionFragment = keyRestrictionBuilder.toFragmentString();
        StringBuilder restrictions = new StringBuilder();
        if (batchSize == 1) {
            // for no batching, use "foo = ? and bar = ?"
            restrictions.append(keyRestrictionFragment);
        } else {
            // for batching, use "( (foo = ? and bar = ?) or (foo = ? and bar = ?) )"
            restrictions.append('(');
            DisjunctionFragment df = new DisjunctionFragment();
            for (int i = 0; i < batchSize; i++) {
                df.addCondition(keyRestrictionFragment);
            }
            restrictions.append(df.toFragmentString());
            restrictions.append(')');
        }
        select.appendRestrictions(restrictions.toString());
    }
}
Also used : InFragment(org.hibernate.sql.InFragment) DisjunctionFragment(org.hibernate.sql.DisjunctionFragment) ConditionFragment(org.hibernate.sql.ConditionFragment)

Example 3 with InFragment

use of org.hibernate.sql.InFragment in project hibernate-orm by hibernate.

the class SingleTableEntityPersister method discriminatorFilterFragment.

private String discriminatorFilterFragment(String alias, Set<String> treatAsDeclarations) {
    final boolean hasTreatAs = treatAsDeclarations != null && !treatAsDeclarations.isEmpty();
    if (!needsDiscriminator() && !hasTreatAs) {
        return "";
    }
    final InFragment frag = new InFragment();
    if (isDiscriminatorFormula()) {
        frag.setFormula(alias, getDiscriminatorFormulaTemplate());
    } else {
        frag.setColumn(alias, getDiscriminatorColumnName());
    }
    if (hasTreatAs) {
        frag.addValues(decodeTreatAsRequests(treatAsDeclarations));
    } else {
        frag.addValues(fullDiscriminatorValues());
    }
    return " and " + frag.toFragmentString();
}
Also used : InFragment(org.hibernate.sql.InFragment)

Aggregations

InFragment (org.hibernate.sql.InFragment)3 ConditionFragment (org.hibernate.sql.ConditionFragment)2 DisjunctionFragment (org.hibernate.sql.DisjunctionFragment)2