Package org.hibernate.sql

Examples of org.hibernate.sql.Select


      return ProcessedWhereClause.NO_WHERE_CLAUSE;
    }
  }

  protected String generateIdInsertSelect(Queryable persister, String tableAlias, ProcessedWhereClause whereClause) {
    Select select = new Select( sessionFactory.getDialect() );
    SelectValues selectClause = new SelectValues( sessionFactory.getDialect() )
        .addColumns( tableAlias, persister.getIdentifierColumnNames(), persister.getIdentifierColumnNames() );
    addAnyExtraIdSelectValues( selectClause );
    select.setSelectClause( selectClause.render() );

    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 );
      }
    }

    if ( whereClause.getUserWhereClauseFragment().length() > 0 ) {
      if ( whereJoinFragment.length() > 0 ) {
        whereJoinFragment += " and ";
      }
    }
    select.setWhereClause( whereJoinFragment + whereClause.getUserWhereClauseFragment() );

    InsertSelect insert = new InsertSelect( sessionFactory.getDialect() );
    if ( sessionFactory.getSettings().isCommentsEnabled() ) {
      insert.setComment( "insert-select for " + persister.getEntityName() + " ids" );
    }
View Full Code Here


    }

  }

  protected String generateIdByUniqueKeySelectString(String uniquePropertyName) {
    Select select = new Select( getFactory().getDialect() );

    if ( getFactory().getSettings().isCommentsEnabled() ) {
      select.setComment( "resolve id by unique property [" + getEntityName() + "." + uniquePropertyName + "]" );
    }

    final String rooAlias = getRootAlias();

    select.setFromClause( fromTableFragment( rooAlias ) + fromJoinFragment( rooAlias, true, false ) );

    SelectFragment selectFragment = new SelectFragment();
    selectFragment.addColumns( rooAlias, getIdentifierColumnNames(), getIdentifierAliases() );
    select.setSelectClause( selectFragment );

    StringBuilder whereClauseBuffer = new StringBuilder();
    final int uniquePropertyIndex = getSubclassPropertyIndex( uniquePropertyName );
    final String uniquePropertyTableAlias = generateTableAlias(
        rooAlias,
        getSubclassPropertyTableNumber( uniquePropertyIndex )
    );
    String sep = "";
    for ( String columnTemplate : getSubclassPropertyColumnReaderTemplateClosure()[uniquePropertyIndex] ) {
      if ( columnTemplate == null ) {
        continue;
      }
      final String columnReference = StringHelper.replace( columnTemplate, Template.TEMPLATE, uniquePropertyTableAlias );
      whereClauseBuffer.append( sep ).append( columnReference ).append( "=?" );
      sep = " and ";
    }
    for ( String formulaTemplate : getSubclassPropertyFormulaTemplateClosure()[uniquePropertyIndex] ) {
      if ( formulaTemplate == null ) {
        continue;
      }
      final String formulaReference = StringHelper.replace( formulaTemplate, Template.TEMPLATE, uniquePropertyTableAlias );
      whereClauseBuffer.append( sep ).append( formulaReference ).append( "=?" );
      sep = " and ";
    }
    whereClauseBuffer.append( whereJoinFragment( rooAlias, true, false ) );

    select.setWhereClause( whereClauseBuffer.toString() );

    return select.setOuterJoins( "", "" ).toStatementString();
  }
View Full Code Here

  protected String generateUpdateGeneratedValuesSelectString() {
    return generateGeneratedValuesSelectString( GenerationTiming.ALWAYS );
  }

  private String generateGeneratedValuesSelectString(final GenerationTiming generationTimingToMatch) {
    Select select = new Select( getFactory().getDialect() );

    if ( getFactory().getSettings().isCommentsEnabled() ) {
      select.setComment( "get generated state " + getEntityName() );
    }

    String[] aliasedIdColumns = StringHelper.qualify( getRootAlias(), getIdentifierColumnNames() );

    // Here we render the select column list based on the properties defined as being generated.
    // For partial component generation, we currently just re-select the whole component
    // rather than trying to handle the individual generated portions.
    String selectClause = concretePropertySelectFragment(
        getRootAlias(),
        new InclusionChecker() {
          @Override
          public boolean includeProperty(int propertyNumber) {
            final InDatabaseValueGenerationStrategy generationStrategy
                = entityMetamodel.getInDatabaseValueGenerationStrategies()[propertyNumber];
            return generationStrategy != null
                && timingsMatch( generationStrategy.getGenerationTiming(), generationTimingToMatch );
          }
        }
    );
    selectClause = selectClause.substring( 2 );

    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();
  }
View Full Code Here

  protected String generateSnapshotSelectString() {

    //TODO: should we use SELECT .. FOR UPDATE?

    Select select = new Select( getFactory().getDialect() );

    if ( getFactory().getSettings().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();

    /*if ( isVersioned() ) {
      where.append(" and ")
        .append( getVersionColumnName() )
        .append("=?");
    }*/

    return select.setSelectClause( selectClause )
        .setFromClause( fromClause )
        .setOuterJoins( "", "" )
        .setWhereClause( whereClause )
        .toStatementString();
  }
View Full Code Here

    //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().getSettings().isCommentsEnabled() ) {
      select.setComment( "sequential select " + getEntityName() );
    }
    return select.toStatementString();
  }
View Full Code Here

      naturalIdMarkers[ naturalIdPropertyIndexes[i] ] = true;
    }

    ///////////////////////////////////////////////////////////////////////
    // TODO : look at perhaps caching this...
    Select select = new Select( getFactory().getDialect() );
    if ( getFactory().getSettings().isCommentsEnabled() ) {
      select.setComment( "get current natural-id state " + getEntityName() );
    }
    select.setSelectClause( concretePropertySelectFragmentSansLeadingComma( getRootAlias(), naturalIdMarkers ) );
    select.setFromClause( fromTableFragment( getRootAlias() ) + fromJoinFragment( getRootAlias(), true, false ) );

    String[] aliasedIdColumns = StringHelper.qualify( getRootAlias(), getIdentifierColumnNames() );
    String whereClause = new StringBuilder()
      .append( StringHelper.join( "=? and ",
          aliasedIdColumns ) )
      .append( "=?" )
      .append( whereJoinFragment( getRootAlias(), true, false ) )
      .toString();

    String sql = select.setOuterJoins( "", "" )
        .setWhereClause( whereClause )
        .toStatementString();
    ///////////////////////////////////////////////////////////////////////

    Object[] snapshot = new Object[ naturalIdPropertyCount ];
View Full Code Here

      if ( rootPersister instanceof AbstractEntityPersister ) {
        return ( (AbstractEntityPersister) rootPersister ).generateEntityIdByNaturalIdSql( valueNullness );
      }
    }

    Select select = new Select( getFactory().getDialect() );
    if ( getFactory().getSettings().isCommentsEnabled() ) {
      select.setComment( "get current natural-id->entity-id state " + getEntityName() );
    }

    final String rootAlias = getRootAlias();

    select.setSelectClause( identifierSelectFragment( rootAlias, "" ) );
    select.setFromClause( fromTableFragment( rootAlias ) + fromJoinFragment( rootAlias, true, false ) );

    final StringBuilder whereClause = new StringBuilder();
    final int[] propertyTableNumbers = getPropertyTableNumbers();
    final int[] naturalIdPropertyIndexes = this.getNaturalIdentifierProperties();
    int valuesIndex = -1;
    for ( int propIdx = 0; propIdx < naturalIdPropertyIndexes.length; propIdx++ ) {
      valuesIndex++;
      if ( propIdx > 0 ) {
        whereClause.append( " and " );
      }

      final int naturalIdIdx = naturalIdPropertyIndexes[propIdx];
      final String tableAlias = generateTableAlias( rootAlias, propertyTableNumbers[naturalIdIdx] );
      final String[] propertyColumnNames = getPropertyColumnNames( naturalIdIdx );
      final String[] aliasedPropertyColumns = StringHelper.qualify( tableAlias, propertyColumnNames );

      if ( valueNullness != null && valueNullness[valuesIndex] ) {
        whereClause.append( StringHelper.join( " is null and ", aliasedPropertyColumns ) ).append( " is null" );
      }
      else {
        whereClause.append( StringHelper.join( "=? and ", aliasedPropertyColumns ) ).append( "=?" );
      }
    }

    whereClause.append( whereJoinFragment( getRootAlias(), true, false ) );

    return select.setOuterJoins( "", "" ).setWhereClause( whereClause.toString() ).toStatementString();
  }
View Full Code Here

    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().getSettings().isCommentsEnabled() ) {
      select.setComment( getComment() );
    }
    sql = select.toStatementString();
  }
View Full Code Here

  protected String generateUpdateGeneratedValuesSelectString() {
    return generateGeneratedValuesSelectString( getPropertyUpdateGenerationInclusions() );
  }

  private String generateGeneratedValuesSelectString(ValueInclusion[] inclusions) {
    Select select = new Select( getFactory().getDialect() );

    if ( getFactory().getSettings().isCommentsEnabled() ) {
      select.setComment( "get generated state " + getEntityName() );
    }

    String[] aliasedIdColumns = StringHelper.qualify( getRootAlias(), getIdentifierColumnNames() );

    // Here we render the select column list based on the properties defined as being generated.
    // For partial component generation, we currently just re-select the whole component
    // rather than trying to handle the individual generated portions.
    String selectClause = concretePropertySelectFragment( getRootAlias(), inclusions );
    selectClause = selectClause.substring( 2 );

    String fromClause = fromTableFragment( getRootAlias() ) +
        fromJoinFragment( getRootAlias(), true, false );

    String whereClause = new StringBuffer()
      .append( StringHelper.join( "=? and ", aliasedIdColumns ) )
      .append( "=?" )
      .append( whereJoinFragment( getRootAlias(), true, false ) )
      .toString();

    return select.setSelectClause( selectClause )
        .setFromClause( fromClause )
        .setOuterJoins( "", "" )
        .setWhereClause( whereClause )
        .toStatementString();
  }
View Full Code Here

  protected String generateSnapshotSelectString() {

    //TODO: should we use SELECT .. FOR UPDATE?

    Select select = new Select( getFactory().getDialect() );

    if ( getFactory().getSettings().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 StringBuffer()
      .append( StringHelper.join( "=? and ",
          aliasedIdColumns ) )
      .append( "=?" )
      .append( whereJoinFragment( getRootAlias(), true, false ) )
      .toString();

    /*if ( isVersioned() ) {
      where.append(" and ")
        .append( getVersionColumnName() )
        .append("=?");
    }*/

    return select.setSelectClause( selectClause )
        .setFromClause( fromClause )
        .setOuterJoins( "", "" )
        .setWhereClause( whereClause )
        .toStatementString();
  }
View Full Code Here

TOP

Related Classes of org.hibernate.sql.Select

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.