001package io.ebean.config.dbplatform.db2;
002
003import io.ebean.BackgroundExecutor;
004import io.ebean.config.dbplatform.SequenceBatchIdGenerator;
005
006import javax.sql.DataSource;
007
008/**
009 * DB2 specific sequence Id Generator.
010 */
011public class DB2SequenceIdGenerator extends SequenceBatchIdGenerator {
012
013  private final String baseSql;
014  private final String unionBaseSql;
015
016  /**
017   * Construct given a dataSource and sql to return the next sequence value.
018   */
019  public DB2SequenceIdGenerator(BackgroundExecutor be, DataSource ds, String seqName, int batchSize) {
020    super(be, ds, seqName, batchSize);
021    this.baseSql = "values nextval for " + seqName;
022    this.unionBaseSql = " union " + baseSql;
023  }
024
025  @Override
026  public String getSql(int batchSize) {
027
028    StringBuilder sb = new StringBuilder();
029    sb.append(baseSql);
030    for (int i = 1; i < batchSize; i++) {
031      sb.append(unionBaseSql);
032    }
033    return sb.toString();
034  }
035}