/**
   * Creates foreign keys for subclass tables that are mapped using table per subclass. Further
   * information is available in the <a href="http://jira.grails.org/browse/GRAILS-7729">JIRA
   * ticket</a>
   */
  private void createSubclassForeignKeys() {
    if (subclassForeignKeysCreated) {
      return;
    }

    for (PersistentClass persistentClass : classes.values()) {
      if (persistentClass instanceof RootClass) {
        RootClass rootClass = (RootClass) persistentClass;

        if (rootClass.hasSubclasses()) {
          Iterator subclasses = rootClass.getSubclassIterator();

          while (subclasses.hasNext()) {

            Object subclass = subclasses.next();

            // This test ensures that foreign keys will only be created for subclasses that are
            // mapped using "table per subclass"
            if (subclass instanceof JoinedSubclass) {
              JoinedSubclass joinedSubclass = (JoinedSubclass) subclass;
              joinedSubclass.createForeignKey();
            }
          }
        }
      }
    }

    subclassForeignKeysCreated = true;
  }
  public String[] getCleanSql() {
    if (cleanSql == null) {
      // loop over all foreign key constraints
      List dropForeignKeysSql = new ArrayList();
      List createForeignKeysSql = new ArrayList();
      Iterator iter = configuration.getTableMappings();
      while (iter.hasNext()) {
        Table table = (Table) iter.next();
        if (table.isPhysicalTable()) {
          Iterator subIter = table.getForeignKeyIterator();
          while (subIter.hasNext()) {
            ForeignKey fk = (ForeignKey) subIter.next();
            if (fk.isPhysicalConstraint()) {
              // collect the drop key constraint
              dropForeignKeysSql.add(
                  fk.sqlDropString(
                      dialect,
                      properties.getProperty(Environment.DEFAULT_CATALOG),
                      properties.getProperty(Environment.DEFAULT_SCHEMA)));
              createForeignKeysSql.add(
                  fk.sqlCreateString(
                      dialect,
                      mapping,
                      properties.getProperty(Environment.DEFAULT_CATALOG),
                      properties.getProperty(Environment.DEFAULT_SCHEMA)));
            }
          }
        }
      }

      List deleteSql = new ArrayList();
      iter = configuration.getTableMappings();
      while (iter.hasNext()) {
        Table table = (Table) iter.next();
        deleteSql.add("delete from " + table.getName());
      }

      List cleanSqlList = new ArrayList();
      cleanSqlList.addAll(dropForeignKeysSql);
      cleanSqlList.addAll(deleteSql);
      cleanSqlList.addAll(createForeignKeysSql);

      cleanSql = (String[]) cleanSqlList.toArray(new String[cleanSqlList.size()]);
    }
    return cleanSql;
  }