Search in sources :

Example 1 with ConditionFragment

use of org.hibernate.sql.ConditionFragment 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 ConditionFragment

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

the class SizeExpression method toSqlString.

@Override
public String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    final String entityName = criteriaQuery.getEntityName(criteria, propertyName);
    final String role = entityName + '.' + criteriaQuery.getPropertyName(propertyName);
    final QueryableCollection cp = (QueryableCollection) criteriaQuery.getFactory().getCollectionPersister(role);
    final String[] fk = cp.getKeyColumnNames();
    final String[] pk = ((Loadable) cp.getOwnerEntityPersister()).getIdentifierColumnNames();
    final ConditionFragment subQueryRestriction = new ConditionFragment().setTableAlias(criteriaQuery.getSQLAlias(criteria, propertyName)).setCondition(pk, fk);
    return String.format(Locale.ROOT, "? %s (select count(*) from %s where %s)", op, cp.getTableName(), subQueryRestriction.toFragmentString());
}
Also used : Loadable(org.hibernate.persister.entity.Loadable) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) ConditionFragment(org.hibernate.sql.ConditionFragment)

Example 3 with ConditionFragment

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

the class AbstractEmptinessExpression method toSqlString.

@Override
public final String toSqlString(Criteria criteria, CriteriaQuery criteriaQuery) throws HibernateException {
    final String entityName = criteriaQuery.getEntityName(criteria, propertyName);
    final String actualPropertyName = criteriaQuery.getPropertyName(propertyName);
    final String sqlAlias = criteriaQuery.getSQLAlias(criteria, propertyName);
    final SessionFactoryImplementor factory = criteriaQuery.getFactory();
    final QueryableCollection collectionPersister = getQueryableCollection(entityName, actualPropertyName, factory);
    final String[] collectionKeys = collectionPersister.getKeyColumnNames();
    final String[] ownerKeys = ((Loadable) factory.getEntityPersister(entityName)).getIdentifierColumnNames();
    final String innerSelect = "(select 1 from " + collectionPersister.getTableName() + " where " + new ConditionFragment().setTableAlias(sqlAlias).setCondition(ownerKeys, collectionKeys).toFragmentString() + ")";
    return excludeEmpty() ? "exists " + innerSelect : "not exists " + innerSelect;
}
Also used : Loadable(org.hibernate.persister.entity.Loadable) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) QueryableCollection(org.hibernate.persister.collection.QueryableCollection) ConditionFragment(org.hibernate.sql.ConditionFragment)

Example 4 with ConditionFragment

use of org.hibernate.sql.ConditionFragment 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)

Aggregations

ConditionFragment (org.hibernate.sql.ConditionFragment)4 QueryableCollection (org.hibernate.persister.collection.QueryableCollection)2 Loadable (org.hibernate.persister.entity.Loadable)2 DisjunctionFragment (org.hibernate.sql.DisjunctionFragment)2 InFragment (org.hibernate.sql.InFragment)2 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1