Beispiel #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
  protected String getKey(final PrimaryKey value) {

    return value.getColumnName();
  }