001package io.ebean.config.dbplatform.db2;
002
003import io.ebean.BackgroundExecutor;
004import io.ebean.annotation.Platform;
005import io.ebean.config.dbplatform.DatabasePlatform;
006import io.ebean.config.dbplatform.DbPlatformType;
007import io.ebean.config.dbplatform.DbType;
008import io.ebean.config.dbplatform.PlatformIdGenerator;
009import io.ebean.config.dbplatform.SqlErrorCodes;
010
011import javax.sql.DataSource;
012import java.sql.Types;
013
014/**
015 * DB2 specific platform.
016 */
017public abstract class BaseDB2Platform extends DatabasePlatform {
018
019  public BaseDB2Platform() {
020    super();
021    this.platform = Platform.DB2;
022    this.supportsNativeJavaTime = false;
023    this.truncateTable = "truncate table %s reuse storage ignore delete triggers immediate";
024    this.likeClauseRaw = "like ?";
025    this.sqlLimiter = new Db2SqlLimiter();
026
027    this.dbIdentity.setSupportsGetGeneratedKeys(true);
028    this.dbIdentity.setSupportsSequence(true);
029    this.idInExpandedForm = true; // Db2 does not support (a,b) in ((?,?),(?,?))
030    this.exceptionTranslator =
031      new SqlErrorCodes()
032        .addAcquireLock("40001","57033") // key -911/-913
033        .addDuplicateKey("23505") // -803
034        // .addDataIntegrity("-407","-530","-531","-532","-543","-544","-545","-603","-667")
035        // we need SQLState, not code: https://www.ibm.com/support/knowledgecenter/en/SSEPEK_10.0.0/codes/src/tpc/db2z_n.html
036        .addDataIntegrity("23502","23503","23504","23511","23512","23511","42917","23515")
037        .build();
038
039    historySupport = new DB2HistorySupport();
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  }
047
048  /**
049   * Return a DB2 specific sequence IdGenerator that supports batch fetching
050   * sequence values.
051   */
052  @Override
053  public PlatformIdGenerator createSequenceIdGenerator(BackgroundExecutor be, DataSource ds, int stepSize, String seqName) {
054
055    return new DB2SequenceIdGenerator(be, ds, seqName, sequenceBatchSize);
056  }
057
058}