protected void dropTemporaryTableIfNecessary(
      final Queryable persister, final SessionImplementor session) {
    if (getFactory().getDialect().dropTemporaryTableAfterUse()) {
      IsolatedWork work =
          new IsolatedWork() {
            public void doWork(Connection connection) throws HibernateException {
              Statement stmnt = null;
              try {
                stmnt = connection.createStatement();
                stmnt.executeUpdate("drop table " + persister.getTemporaryIdTableName());
              } catch (Throwable t) {
                log.warn("unable to drop temporary id table after use [" + t.getMessage() + "]");
              } finally {
                if (stmnt != null) {
                  try {
                    stmnt.close();
                  } catch (Throwable ignore) {
                    // ignore
                  }
                }
              }
            }
          };

      if (shouldIsolateTemporaryTableDDL()) {
        if (getFactory().getSettings().isDataDefinitionInTransactionSupported()) {
          Isolater.doIsolatedWork(work, session);
        } else {
          Isolater.doNonTransactedWork(work, session);
        }
      } else {
        work.doWork(session.getJDBCContext().getConnectionManager().getConnection());
        session.getJDBCContext().getConnectionManager().afterStatement();
      }
    } else {
      // at the very least cleanup the data :)
      PreparedStatement ps = null;
      try {
        ps =
            session
                .getBatcher()
                .prepareStatement("delete from " + persister.getTemporaryIdTableName());
        ps.executeUpdate();
      } catch (Throwable t) {
        log.warn("unable to cleanup temporary id table after use [" + t + "]");
      } finally {
        if (ps != null) {
          try {
            session.getBatcher().closeStatement(ps);
          } catch (Throwable ignore) {
            // ignore
          }
        }
      }
    }
  }
  protected String generateIdInsertSelect(Queryable persister, String tableAlias, AST whereClause) {
    Select select = new Select(getFactory().getDialect());
    SelectFragment selectFragment =
        new SelectFragment()
            .addColumns(
                tableAlias,
                persister.getIdentifierColumnNames(),
                persister.getIdentifierColumnNames());
    select.setSelectClause(selectFragment.toFragmentString().substring(2));

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

    String userWhereClause = "";
    if (whereClause.getNumberOfChildren() != 0) {
      // If a where clause was specified in the update/delete query, use it to limit the
      // returned ids here...
      try {
        SqlGenerator sqlGenerator = new SqlGenerator(getFactory());
        sqlGenerator.whereClause(whereClause);
        userWhereClause = sqlGenerator.getSQL().substring(7); // strip the " where "
      } catch (RecognitionException e) {
        throw new HibernateException("Unable to generate id select for DML operation", e);
      }
      if (whereJoinFragment.length() > 0) {
        whereJoinFragment += " and ";
      }
    }

    select.setWhereClause(whereJoinFragment + userWhereClause);

    InsertSelect insert = new InsertSelect(getFactory().getDialect());
    if (getFactory().getSettings().isCommentsEnabled()) {
      insert.setComment("insert-select for " + persister.getEntityName() + " ids");
    }
    insert.setTableName(persister.getTemporaryIdTableName());
    insert.setSelect(select);
    return insert.toStatementString();
  }
 protected void releaseTempTable(Queryable persister, SessionImplementor session) {
   if (session.getFactory().getDialect().dropTemporaryTableAfterUse()) {
     TemporaryTableDropWork work = new TemporaryTableDropWork(persister, session);
     if (shouldIsolateTemporaryTableDDL(session)) {
       session
           .getTransactionCoordinator()
           .getTransaction()
           .createIsolationDelegate()
           .delegateWork(
               work, session.getFactory().getSettings().isDataDefinitionInTransactionSupported());
     } else {
       final Connection connection =
           session
               .getTransactionCoordinator()
               .getJdbcCoordinator()
               .getLogicalConnection()
               .getShareableConnectionProxy();
       work.execute(connection);
       session
           .getTransactionCoordinator()
           .getJdbcCoordinator()
           .getLogicalConnection()
           .afterStatementExecution();
     }
   } else {
     // at the very least cleanup the data :)
     PreparedStatement ps = null;
     try {
       final String sql = "delete from " + persister.getTemporaryIdTableName();
       ps =
           session
               .getTransactionCoordinator()
               .getJdbcCoordinator()
               .getStatementPreparer()
               .prepareStatement(sql, false);
       ps.executeUpdate();
     } catch (Throwable t) {
       log.unableToCleanupTemporaryIdTable(t);
     } finally {
       if (ps != null) {
         try {
           ps.close();
         } catch (Throwable ignore) {
           // ignore
         }
       }
     }
   }
 }
 @Override
 public void execute(Connection connection) {
   final String command =
       session.getFactory().getDialect().getDropTemporaryTableString()
           + ' '
           + persister.getTemporaryIdTableName();
   try {
     Statement statement = connection.createStatement();
     try {
       statement = connection.createStatement();
       statement.executeUpdate(command);
     } finally {
       try {
         statement.close();
       } catch (Throwable ignore) {
         // ignore
       }
     }
   } catch (Exception e) {
     log.warn("unable to drop temporary id table after use [" + e.getMessage() + "]");
   }
 }
 protected String generateIdSubselect(Queryable persister) {
   return "select "
       + StringHelper.join(", ", persister.getIdentifierColumnNames())
       + " from "
       + persister.getTemporaryIdTableName();
 }