Exemple #1
0
  /** {@inheritDoc} */
  public void generateHeader(final DDLWriter writer) {
    Configuration conf = getConfiguration();

    writer.println("/* ");
    writer.println(new Date());
    writer.println("Castor DDL Generator from mapping for DB2");
    writer.println(conf.getStringValue(DDLGenConfiguration.HEADER_COMMENT_KEY, ""));
    writer.println("*/");
  }
  /** {@inheritDoc} */
  public void toCreateDDL(final KeyGenerator key, final DDLWriter writer) {
    SequenceKeyGenerator sequenceKey = (SequenceKeyGenerator) key;
    String tableName = sequenceKey.getTable().getName();
    String pkList = toPrimaryKeyList(key.getTable());
    String pkTypeList = toPrimaryKeyTypeList(key.getTable());
    String sequenceName =
        MessageFormat.format(sequenceKey.getSequence(), new Object[] {tableName, pkList});

    writer.println();
    writer.println();
    writer.print("CREATE SEQUENCE ");
    writer.println(sequenceName);
    writer.print("MAXVALUE ");
    writer.println(Integer.MAX_VALUE);
    writer.print("INCREMENT BY 1 START WITH 1");
    writer.println(DDLGenConfiguration.DEFAULT_STATEMENT_DELIMITER);

    if (sequenceKey.isTrigger()) {
      String triggerName = null;
      if (sequenceName.matches(".*SEQ.*")) {
        triggerName = sequenceName.replaceAll("SEQ", "TRG");
      } else {
        triggerName = "TRG" + sequenceName;
      }
      DDLGenConfiguration conf = sequenceKey.getConfiguration();
      String triggerTemp = conf.getStringValue(DDLGenConfiguration.TRIGGER_TEMPLATE_KEY, "");

      triggerTemp = triggerTemp.replaceAll("<trigger_name>", triggerName);
      triggerTemp = triggerTemp.replaceAll("<sequence_name>", sequenceName);
      triggerTemp = triggerTemp.replaceAll("<table_name>", tableName);
      triggerTemp = triggerTemp.replaceAll("<pk_name>", pkList);
      triggerTemp = triggerTemp.replaceAll("<pk_type>", pkTypeList);

      writer.println();
      writer.println();
      writer.println(triggerTemp);
    }
  }