public final class Db2SequenceKeyGeneratorFactory extends SequenceKeyGeneratorFactory {
/**
* {@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 sequenceName = MessageFormat.format(sequenceKey.getSequence(),
new Object[]{tableName, pkList});
writer.println();
writer.println();
writer.print("CREATE SEQUENCE AS INTEGER ");
writer.println(sequenceName);
writer.println("START WITH 1 INCREMENT BY 1");
writer.print("MAXVALUE ");
writer.print(Integer.MAX_VALUE);
writer.println(DDLGenConfiguration.DEFAULT_STATEMENT_DELIMITER);
if (sequenceKey.isTrigger()) {
// refer to http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.
// jsp?topic=/com.ibm.db2.udb.doc/admin/r0000931.htm
String pkTypeList = toPrimaryKeyTypeList(key.getTable());
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);