/** * Generate the SQL DELETE that deletes all rows */ @Override protected String generateDeleteString() { Delete delete = new Delete() .setTableName( qualifiedTableName ) .addPrimaryKeyColumns( keyColumnNames ); if ( hasWhere ) delete.setWhere( sqlWhereString ); if ( getFactory().getSettings().isCommentsEnabled() ) { delete.setComment( "delete collection " + getRole() ); } return delete.toStatementString(); }
/** * Generate the SQL DELETE that deletes a particular row */ @Override protected String generateDeleteRowString() { Delete delete = new Delete() .setTableName( qualifiedTableName ); if ( hasIdentifier ) { delete.addPrimaryKeyColumns( new String[]{ identifierColumnName } ); } else if ( hasIndex && !indexContainsFormula ) { delete.addPrimaryKeyColumns( ArrayHelper.join( keyColumnNames, indexColumnNames ) ); } else { delete.addPrimaryKeyColumns( keyColumnNames ); delete.addPrimaryKeyColumns( elementColumnNames, elementColumnIsInPrimaryKey, elementColumnWriters ); } if ( getFactory().getSettings().isCommentsEnabled() ) { delete.setComment( "delete collection row " + getRole() ); } return delete.toStatementString(); }
/** * Generate the SQL DELETE that deletes a particular row */ protected String generateDeleteRowString() { Delete delete = new Delete() .setTableName( qualifiedTableName ); if ( hasIdentifier ) { delete.setPrimaryKeyColumnNames( new String[]{ identifierColumnName } ); } else if ( hasIndex && !indexContainsFormula ) { delete.setPrimaryKeyColumnNames( ArrayHelper.join( keyColumnNames, indexColumnNames ) ); } else { delete.setPrimaryKeyColumnNames( ArrayHelper.join( keyColumnNames, elementColumnNames, elementColumnIsInPrimaryKey ) ); } if ( getFactory().getSettings().isCommentsEnabled() ) { delete.setComment( "delete collection row " + getRole() ); } return delete.toStatementString(); }
/** * Generate the SQL that deletes a row by id (and version) */ protected String generateDeleteString(int j) { Delete delete = new Delete() .setTableName( getTableName( j ) ) .addPrimaryKeyColumns( getKeyColumns( j ) ); if ( j == 0 ) { delete.setVersionColumnName( getVersionColumnName() ); } if ( getFactory().getSettings().isCommentsEnabled() ) { delete.setComment( "delete " + getEntityName() ); } return delete.toStatementString(); }
private String[] generateSQLDeletStrings(Object[] loadedState) { int span = getTableSpan(); String[] deleteStrings = new String[span]; for ( int j = span - 1; j >= 0; j-- ) { Delete delete = new Delete() .setTableName( getTableName( j ) ) .addPrimaryKeyColumns( getKeyColumns( j ) ); if ( getFactory().getSettings().isCommentsEnabled() ) { delete.setComment( "delete " + getEntityName() + " [" + j + "]" ); } boolean[] versionability = getPropertyVersionability(); Type[] types = getPropertyTypes(); for ( int i = 0; i < entityMetamodel.getPropertySpan(); i++ ) { if ( isPropertyOfTable( i, j ) && versionability[i] ) { // this property belongs to the table and it is not specifically // excluded from optimistic locking by optimistic-lock="false" String[] propertyColumnNames = getPropertyColumnNames( i ); boolean[] propertyNullness = types[i].toColumnNullness( loadedState[i], getFactory() ); for ( int k = 0; k < propertyNullness.length; k++ ) { if ( propertyNullness[k] ) { delete.addWhereFragment( propertyColumnNames[k] + " = ?" ); } else { delete.addWhereFragment( propertyColumnNames[k] + " is null" ); } } } } deleteStrings[j] = delete.toStatementString(); } return deleteStrings; }
private String generateDelete(String tableName, String[] columnNames, String idSubselect, String comment) { final Delete delete = new Delete() .setTableName( tableName ) .setWhere( "(" + StringHelper.join( ", ", columnNames ) + ") IN (" + idSubselect + ")" ); if ( factory().getSettings().isCommentsEnabled() ) { delete.setComment( comment ); } return delete.toStatementString(); }
/** * Generate the SQL that deletes a row by id (and version) */ protected String generateDeleteString(int j) { Delete delete = new Delete() .setTableName( getTableName( j ) ) .setPrimaryKeyColumnNames( getKeyColumns( j ) ); if ( j == 0 ) { delete.setVersionColumnName( getVersionColumnName() ); } if ( getFactory().getSettings().isCommentsEnabled() ) { delete.setComment( "delete " + getEntityName() ); } return delete.toStatementString(); }
private String[] generateSQLDeletStrings(Object[] loadedState) { int span = getTableSpan(); String[] deleteStrings = new String[span]; for ( int j = span - 1; j >= 0; j-- ) { Delete delete = new Delete() .setTableName( getTableName( j ) ) .setPrimaryKeyColumnNames( getKeyColumns( j ) ); if ( getFactory().getSettings().isCommentsEnabled() ) { delete.setComment( "delete " + getEntityName() + " [" + j + "]" ); } boolean[] versionability = getPropertyVersionability(); Type[] types = getPropertyTypes(); for ( int i = 0; i < entityMetamodel.getPropertySpan(); i++ ) { if ( isPropertyOfTable( i, j ) && versionability[i] ) { // this property belongs to the table and it is not specifically // excluded from optimistic locking by optimistic-lock="false" String[] propertyColumnNames = getPropertyColumnNames( i ); boolean[] propertyNullness = types[i].toColumnNullness( loadedState[i], getFactory() ); for ( int k = 0; k < propertyNullness.length; k++ ) { if ( propertyNullness[k] ) { delete.addWhereFragment( propertyColumnNames[k] + " = ?" ); } else { delete.addWhereFragment( propertyColumnNames[k] + " is null" ); } } } } deleteStrings[j] = delete.toStatementString(); } return deleteStrings; }
/** * Generate the SQL DELETE that deletes all rows */ protected String generateDeleteString() { Delete delete = new Delete() .setTableName( qualifiedTableName ) .setPrimaryKeyColumnNames( keyColumnNames ); if ( hasWhere ) delete.setWhere( sqlWhereString ); if ( getFactory().getSettings().isCommentsEnabled() ) { delete.setComment( "delete collection " + getRole() ); } return delete.toStatementString(); }
public MultiTableDeleteExecutor(HqlSqlWalker walker) { super( walker, log ); if ( !walker.getSessionFactoryHelper().getFactory().getDialect().supportsTemporaryTables() ) { throw new HibernateException( "cannot perform multi-table deletes using dialect not supporting temp tables" ); } DeleteStatement deleteStatement = ( DeleteStatement ) walker.getAST(); FromElement fromElement = deleteStatement.getFromClause().getFromElement(); String bulkTargetAlias = fromElement.getTableAlias(); this.persister = fromElement.getQueryable(); this.idInsertSelect = generateIdInsertSelect( persister, bulkTargetAlias, deleteStatement.getWhereClause() ); log.trace( "Generated ID-INSERT-SELECT SQL (multi-table delete) : " + idInsertSelect ); String[] tableNames = persister.getConstraintOrderedTableNameClosure(); String[][] columnNames = persister.getContraintOrderedTableKeyColumnClosure(); String idSubselect = generateIdSubselect( persister ); deletes = new String[tableNames.length]; for ( int i = tableNames.length - 1; i >= 0; i-- ) { // TODO : an optimization here would be to consider cascade deletes and not gen those delete statements; // the difficulty is the ordering of the tables here vs the cascade attributes on the persisters -> // the table info gotten here should really be self-contained (i.e., a class representation // defining all the needed attributes), then we could then get an array of those final Delete delete = new Delete() .setTableName( tableNames[i] ) .setWhere( "(" + StringHelper.join( ", ", columnNames[i] ) + ") IN (" + idSubselect + ")" ); if ( getFactory().getSettings().isCommentsEnabled() ) { delete.setComment( "bulk delete" ); } deletes[i] = delete.toStatementString(); } }
private String generateDelete(String tableName, String[] columnNames, String idSubselect, String comment) { final Delete delete = new Delete().setTableName( tableName).setWhere( "(" + StringHelper.join( ", ", columnNames) + ") IN (" + idSubselect + ")"); if (factory().getSettings().isCommentsEnabled()) { delete.setComment(comment); } return delete.toStatementString(); }
private String generateDelete(String tableName, String[] columnNames, String idSubselect, String comment) { final Delete delete = new Delete().setTableName(tableName).setWhere( "(" + StringHelper.join(", ", columnNames) + ") IN (" + idSubselect + ")"); if (factory().getSettings().isCommentsEnabled()) { delete.setComment(comment); } return delete.toStatementString(); }
public DeleteExecutor(HqlSqlWalker walker, Queryable persister) { super( walker, persister ); final SessionFactoryImplementor factory = walker.getSessionFactoryHelper().getFactory(); final Dialect dialect = factory.getDialect(); try { final DeleteStatement deleteStatement = (DeleteStatement) walker.getAST(); final String idSubselectWhere; if ( deleteStatement.hasWhereClause() ) { final AST whereClause = deleteStatement.getWhereClause(); final SqlGenerator gen = new SqlGenerator( factory ); gen.whereClause( whereClause ); parameterSpecifications = gen.getCollectedParameters(); idSubselectWhere = gen.getSQL().length() > 7 ? gen.getSQL() : ""; } else { parameterSpecifications = new ArrayList<ParameterSpecification>(); idSubselectWhere = ""; } // If many-to-many, delete the FK row in the collection table. for ( Type type : persister.getPropertyTypes() ) { if ( type.isCollectionType() ) { final CollectionType cType = (CollectionType) type; final AbstractCollectionPersister cPersister = (AbstractCollectionPersister) factory .getCollectionPersister( cType.getRole() ); if ( cPersister.isManyToMany() ) { if ( persister.getIdentifierColumnNames().length > 1 && !dialect.supportsTuplesInSubqueries() ) { LOG.warn( "This dialect is unable to cascade the delete into the many-to-many join table" + " when the entity has multiple primary keys. Either properly setup cascading on" + " the constraints or manually clear the associations prior to deleting the entities." ); } else { final String idSubselect = "(select " + StringHelper.join( ", ", persister.getIdentifierColumnNames() ) + " from " + persister.getTableName() + idSubselectWhere + ")"; final String where = "(" + StringHelper.join( ", ", cPersister.getKeyColumnNames() ) + ") in " + idSubselect; final Delete delete = new Delete().setTableName( cPersister.getTableName() ).setWhere( where ); if ( factory.getSettings().isCommentsEnabled() ) { delete.setComment( "delete FKs in join table" ); } deletes.add( delete.toStatementString() ); } } } } } catch (RecognitionException e) { throw new HibernateException( "Unable to delete the FKs in the join table!", e ); } }