Search in sources :

Example 1 with Select

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

the class AbstractTableBasedBulkIdHandler method generateIdSelect.

protected Select generateIdSelect(String tableAlias, ProcessedWhereClause whereClause) {
    final Dialect dialect = sessionFactory.getJdbcServices().getJdbcEnvironment().getDialect();
    final Select select = new Select(dialect);
    final SelectValues selectClause = new SelectValues(dialect).addColumns(tableAlias, getTargetedQueryable().getIdentifierColumnNames(), getTargetedQueryable().getIdentifierColumnNames());
    addAnyExtraIdSelectValues(selectClause);
    select.setSelectClause(selectClause.render());
    String rootTableName = getTargetedQueryable().getTableName();
    String fromJoinFragment = getTargetedQueryable().fromJoinFragment(tableAlias, true, false);
    String whereJoinFragment = getTargetedQueryable().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);
        }
    }
    if (whereClause.getUserWhereClauseFragment().length() > 0) {
        if (whereJoinFragment.length() > 0) {
            whereJoinFragment += " and ";
        }
    }
    select.setWhereClause(whereJoinFragment + whereClause.getUserWhereClauseFragment());
    return select;
}
Also used : Dialect(org.hibernate.dialect.Dialect) Select(org.hibernate.sql.Select) InsertSelect(org.hibernate.sql.InsertSelect) SelectValues(org.hibernate.sql.SelectValues)

Example 2 with Select

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

the class AbstractEntityJoinWalker method initStatementString.

private void initStatementString(final String projection, final String condition, final String orderBy, final String groupBy, final LockOptions lockOptions) throws MappingException {
    final int joins = countEntityPersisters(associations);
    suffixes = BasicLoader.generateSuffixes(joins + 1);
    JoinFragment ojf = mergeOuterJoins(associations);
    Select select = new Select(getDialect()).setLockOptions(lockOptions).setSelectClause(projection == null ? persister.selectFragment(alias, suffixes[joins]) + selectString(associations) : projection).setFromClause(getDialect().appendLockHint(lockOptions, persister.fromTableFragment(alias)) + persister.fromJoinFragment(alias, true, true)).setWhereClause(condition).setOuterJoins(ojf.toFromFragmentString(), ojf.toWhereFragmentString() + getWhereFragment()).setOrderByClause(orderBy(associations, orderBy)).setGroupByClause(groupBy);
    if (getFactory().getSessionFactoryOptions().isCommentsEnabled()) {
        select.setComment(getComment());
    }
    sql = select.toStatementString();
}
Also used : Select(org.hibernate.sql.Select) JoinFragment(org.hibernate.sql.JoinFragment)

Example 3 with Select

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

the class BasicCollectionJoinWalker method initStatementString.

private void initStatementString(final String alias, final int batchSize, final String subquery) throws MappingException {
    final int joins = countEntityPersisters(associations);
    final int collectionJoins = countCollectionPersisters(associations) + 1;
    suffixes = BasicLoader.generateSuffixes(joins);
    collectionSuffixes = BasicLoader.generateSuffixes(joins, collectionJoins);
    StringBuilder whereString = whereString(alias, collectionPersister.getKeyColumnNames(), subquery, batchSize);
    String manyToManyOrderBy = "";
    String filter = collectionPersister.filterFragment(alias, getLoadQueryInfluencers().getEnabledFilters());
    if (collectionPersister.isManyToMany()) {
        // from the collection of associations, locate OJA for the
        // ManyToOne corresponding to this persister to fully
        // define the many-to-many; we need that OJA so that we can
        // use its alias here
        // TODO : is there a better way here?
        Iterator itr = associations.iterator();
        AssociationType associationType = (AssociationType) collectionPersister.getElementType();
        while (itr.hasNext()) {
            OuterJoinableAssociation oja = (OuterJoinableAssociation) itr.next();
            if (oja.getJoinableType() == associationType) {
                // we found it
                filter += collectionPersister.getManyToManyFilterFragment(oja.getRHSAlias(), getLoadQueryInfluencers().getEnabledFilters());
                manyToManyOrderBy += collectionPersister.getManyToManyOrderByString(oja.getRHSAlias());
            }
        }
    }
    whereString.insert(0, StringHelper.moveAndToBeginning(filter));
    JoinFragment ojf = mergeOuterJoins(associations);
    Select select = new Select(getDialect()).setSelectClause(collectionPersister.selectFragment(alias, collectionSuffixes[0]) + selectString(associations)).setFromClause(collectionPersister.getTableName(), alias).setWhereClause(whereString.toString()).setOuterJoins(ojf.toFromFragmentString(), ojf.toWhereFragmentString());
    select.setOrderByClause(orderBy(associations, mergeOrderings(collectionPersister.getSQLOrderByString(alias), manyToManyOrderBy)));
    if (getFactory().getSettings().isCommentsEnabled()) {
        select.setComment("load collection " + collectionPersister.getRole());
    }
    sql = select.toStatementString();
}
Also used : AssociationType(org.hibernate.type.AssociationType) Iterator(java.util.Iterator) Select(org.hibernate.sql.Select) JoinFragment(org.hibernate.sql.JoinFragment) OuterJoinableAssociation(org.hibernate.loader.OuterJoinableAssociation)

Example 4 with Select

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

the class AbstractEntityPersister method generateSnapshotSelectString.

protected String generateSnapshotSelectString() {
    //TODO: should we use SELECT .. FOR UPDATE?
    Select select = new Select(getFactory().getDialect());
    if (getFactory().getSessionFactoryOptions().isCommentsEnabled()) {
        select.setComment("get current state " + getEntityName());
    }
    String[] aliasedIdColumns = StringHelper.qualify(getRootAlias(), getIdentifierColumnNames());
    String selectClause = StringHelper.join(", ", aliasedIdColumns) + concretePropertySelectFragment(getRootAlias(), getPropertyUpdateability());
    String fromClause = fromTableFragment(getRootAlias()) + fromJoinFragment(getRootAlias(), true, false);
    String whereClause = new StringBuilder().append(StringHelper.join("=? and ", aliasedIdColumns)).append("=?").append(whereJoinFragment(getRootAlias(), true, false)).toString();
    return select.setSelectClause(selectClause).setFromClause(fromClause).setOuterJoins("", "").setWhereClause(whereClause).toStatementString();
}
Also used : SimpleSelect(org.hibernate.sql.SimpleSelect) Select(org.hibernate.sql.Select)

Example 5 with Select

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

the class AbstractEntityPersister method renderSelect.

protected String renderSelect(final int[] tableNumbers, final int[] columnNumbers, final int[] formulaNumbers) {
    //get 'em in the right order (not that it really matters)
    Arrays.sort(tableNumbers);
    //render the where and from parts
    int drivingTable = tableNumbers[0];
    final String drivingAlias = generateTableAlias(getRootAlias(), drivingTable);
    //we *could* regerate this inside each called method!
    final String where = createWhereByKey(drivingTable, drivingAlias);
    final String from = createFrom(drivingTable, drivingAlias);
    //now render the joins
    JoinFragment jf = createJoin(tableNumbers, drivingAlias);
    //now render the select clause
    SelectFragment selectFragment = createSelect(columnNumbers, formulaNumbers);
    //now tie it all together
    Select select = new Select(getFactory().getDialect());
    select.setSelectClause(selectFragment.toFragmentString().substring(2));
    select.setFromClause(from);
    select.setWhereClause(where);
    select.setOuterJoins(jf.toFromFragmentString(), jf.toWhereFragmentString());
    if (getFactory().getSessionFactoryOptions().isCommentsEnabled()) {
        select.setComment("sequential select " + getEntityName());
    }
    return select.toStatementString();
}
Also used : SelectFragment(org.hibernate.sql.SelectFragment) SimpleSelect(org.hibernate.sql.SimpleSelect) Select(org.hibernate.sql.Select) JoinFragment(org.hibernate.sql.JoinFragment)

Aggregations

Select (org.hibernate.sql.Select)11 SimpleSelect (org.hibernate.sql.SimpleSelect)6 JoinFragment (org.hibernate.sql.JoinFragment)4 Dialect (org.hibernate.dialect.Dialect)2 InsertSelect (org.hibernate.sql.InsertSelect)2 SelectFragment (org.hibernate.sql.SelectFragment)2 AssociationType (org.hibernate.type.AssociationType)2 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Iterator (java.util.Iterator)1 MappingException (org.hibernate.MappingException)1 EntityKey (org.hibernate.engine.spi.EntityKey)1 OuterJoinableAssociation (org.hibernate.loader.OuterJoinableAssociation)1 JoinType (org.hibernate.sql.JoinType)1 SelectValues (org.hibernate.sql.SelectValues)1 InDatabaseValueGenerationStrategy (org.hibernate.tuple.InDatabaseValueGenerationStrategy)1 CollectionType (org.hibernate.type.CollectionType)1 ComponentType (org.hibernate.type.ComponentType)1 CompositeType (org.hibernate.type.CompositeType)1