001package io.ebean.config.dbplatform.db2;
002
003import io.ebean.BackgroundExecutor;
004import io.ebean.annotation.PersistBatch;
005import io.ebean.annotation.Platform;
006import io.ebean.config.dbplatform.DatabasePlatform;
007import io.ebean.config.dbplatform.DbPlatformType;
008import io.ebean.config.dbplatform.DbType;
009import io.ebean.config.dbplatform.PlatformIdGenerator;
010import io.ebean.config.dbplatform.SqlErrorCodes;
011
012import javax.sql.DataSource;
013import java.sql.Types;
014
015/**
016 * DB2 specific platform.
017 */
018public class DB2Platform extends DatabasePlatform {
019
020  public DB2Platform() {
021    super();
022    this.platform = Platform.DB2;
023    this.maxTableNameLength = 18;
024    this.maxConstraintNameLength = 18;
025    this.truncateTable = "truncate table %s reuse storage ignore delete triggers immediate";
026    this.sqlLimiter = new Db2SqlLimiter();
027
028    this.dbIdentity.setSupportsGetGeneratedKeys(true);
029    this.dbIdentity.setSupportsSequence(true);
030
031    this.exceptionTranslator =
032      new SqlErrorCodes()
033        .addAcquireLock("40001","57033") // key -911/-913
034        .addDuplicateKey("23505") // -803
035        // .addDataIntegrity("-407","-530","-531","-532","-543","-544","-545","-603","-667")
036        // we need SQLState, not code: https://www.ibm.com/support/knowledgecenter/en/SSEPEK_10.0.0/codes/src/tpc/db2z_n.html
037        .addDataIntegrity("23502","23503","23504","23511","23512","23511","42917","23515")
038        .build();
039
040    booleanDbType = Types.BOOLEAN;
041    dbTypeMap.put(DbType.TINYINT, new DbPlatformType("smallint", false));
042    dbTypeMap.put(DbType.INTEGER, new DbPlatformType("integer", false));
043    dbTypeMap.put(DbType.BIGINT, new DbPlatformType("bigint", false));
044    dbTypeMap.put(DbType.REAL, new DbPlatformType("real"));
045    dbTypeMap.put(DbType.DECIMAL, new DbPlatformType("decimal", 16, 3));
046    persistBatchOnCascade = PersistBatch.NONE;
047  }
048
049  /**
050   * Return a DB2 specific sequence IdGenerator that supports batch fetching
051   * sequence values.
052   */
053  @Override
054  public PlatformIdGenerator createSequenceIdGenerator(BackgroundExecutor be, DataSource ds, int stepSize, String seqName) {
055
056    return new DB2SequenceIdGenerator(be, ds, seqName, sequenceBatchSize);
057  }
058
059}