Пример #1
0
  public void toCreateSQL(PrintWriter out) {
    out.println("CREATE TABLE " + name + " (");
    for (Iterator<Column> i = columns.iterator(); i.hasNext(); ) {
      Column c = i.next();
      out.print("  " + c.getName() + " " + c.getTypeName());
      if (c.getNullable() == DatabaseMetaData.columnNoNulls) out.print(" NOT NULL");
      if (i.hasNext()) out.println(',');
    }

    if (!primaryKeys.isEmpty()) {
      out.println(',');
      Iterator<PrimaryKey> i = primaryKeys.iterator();
      PrimaryKey pk = i.next();
      out.print("  ");
      if (pk.getName() != null) {
        out.print("CONSTRAINT " + pk.getName() + " ");
      }
      out.print("PRIMARY KEY (" + pk.getColumnName());
      while (i.hasNext()) {
        pk = i.next();
        out.print(", " + pk.getColumnName());
      }
      out.print(")");
    }
    if (!foreignKeys.isEmpty()) {
      out.println(',');
      for (Iterator<ForeignKey> i = foreignKeys.iterator(); i.hasNext(); ) {
        ForeignKey fk = i.next();
        out.print("  ");
        if (fk.getName() != null) out.print("CONSTRAINT " + fk.getName() + " ");
        out.print("FOREIGN KEY (" + fk.getColumnName() + ") REFERENCES " + fk.getPkTableName());
        if (fk.getPkColumnName() != null) out.print(" (" + fk.getPkColumnName() + ")");
        if (i.hasNext()) out.println(',');
      }
    }
    out.println();
    out.print(")");
  }
  @Override
  public Change[] fixMissing(
      DatabaseObject missingObject,
      DiffOutputControl control,
      Database referenceDatabase,
      Database comparisonDatabase,
      ChangeGeneratorChain chain) {
    List<Change> returnList = new ArrayList<Change>();

    PrimaryKey pk = (PrimaryKey) missingObject;

    AddPrimaryKeyChange change = new AddPrimaryKeyChange();
    change.setTableName(pk.getTable().getName());
    if (control.getIncludeCatalog()) {
      change.setCatalogName(pk.getTable().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
      change.setSchemaName(pk.getTable().getSchema().getName());
    }
    change.setConstraintName(pk.getName());
    change.setColumnNames(pk.getColumnNames());
    if (control.getIncludeTablespace()) {
      change.setTablespace(pk.getTablespace());
    }

    if (referenceDatabase instanceof MSSQLDatabase
        && pk.getBackingIndex() != null
        && pk.getBackingIndex().getClustered() != null
        && !pk.getBackingIndex().getClustered()) {
      change.setClustered(false);
    }

    if (comparisonDatabase instanceof OracleDatabase
        || (comparisonDatabase instanceof DB2Database
            && pk.getBackingIndex() != null
            && !comparisonDatabase.isSystemObject(pk.getBackingIndex()))) {
      Index backingIndex = pk.getBackingIndex();
      if (backingIndex != null && backingIndex.getName() != null) {
        try {
          if (!control.getIncludeCatalog() && !control.getIncludeSchema()) {
            CatalogAndSchema schema =
                comparisonDatabase.getDefaultSchema().customize(comparisonDatabase);
            backingIndex
                .getTable()
                .setSchema(
                    schema.getCatalogName(),
                    schema
                        .getSchemaName()); // set table schema so it is found in the correct schema
          }
          if (referenceDatabase.equals(comparisonDatabase)
              || !SnapshotGeneratorFactory.getInstance().has(backingIndex, comparisonDatabase)) {
            Change[] fixes =
                ChangeGeneratorFactory.getInstance()
                    .fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);

            if (fixes != null) {
              for (Change fix : fixes) {
                if (fix != null) {
                  returnList.add(fix);
                }
              }
            }
          }
        } catch (Exception e) {
          throw new UnexpectedLiquibaseException(e);
        }

        change.setForIndexName(backingIndex.getName());
        Schema schema = backingIndex.getSchema();
        if (schema != null) {
          if (control.getIncludeCatalog()) {
            change.setForIndexCatalogName(schema.getCatalogName());
          }
          if (control.getIncludeSchema()) {
            change.setForIndexSchemaName(schema.getName());
          }
        }
      }
    }

    control.setAlreadyHandledMissing(pk.getBackingIndex());
    returnList.add(change);

    return returnList.toArray(new Change[returnList.size()]);
  }