Refactor extract separate modules for each platform from ebean-api dbplatform

This commit is contained in:
Rob Bygrave committed 2022-03-30 17:11:36 +13:00
1 parent 514bba4130
commit e71e16c1ee
259 files changed
+3183 -1125

No files matched your search

@@ -0,0 +1,58 @@
package io.ebean.platform.db2;
import io.ebean.BackgroundExecutor;
import io.ebean.annotation.Platform;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.config.dbplatform.DbType;
import io.ebean.config.dbplatform.PlatformIdGenerator;
import io.ebean.config.dbplatform.SqlErrorCodes;
import javax.sql.DataSource;
import java.sql.Types;
/**
* DB2 specific platform.
*/
public abstract class BaseDB2Platform extends DatabasePlatform {
public BaseDB2Platform() {
super();
this.platform = Platform.DB2;
this.supportsNativeJavaTime = false;
this.truncateTable = "truncate table %s reuse storage ignore delete triggers immediate";
this.likeClauseRaw = "like ?";
this.sqlLimiter = new Db2SqlLimiter();
this.dbIdentity.setSupportsGetGeneratedKeys(true);
this.dbIdentity.setSupportsSequence(true);
this.idInExpandedForm = true; // Db2 does not support (a,b) in ((?,?),(?,?))
this.exceptionTranslator =
new SqlErrorCodes()
.addAcquireLock("40001","57033") // key -911/-913
.addDuplicateKey("23505") // -803
// .addDataIntegrity("-407","-530","-531","-532","-543","-544","-545","-603","-667")
// we need SQLState, not code: https://www.ibm.com/support/knowledgecenter/en/SSEPEK_10.0.0/codes/src/tpc/db2z_n.html
.addDataIntegrity("23502","23503","23504","23511","23512","23511","42917","23515")
.build();
historySupport = new DB2HistorySupport();
booleanDbType = Types.BOOLEAN;
dbTypeMap.put(DbType.TINYINT, new DbPlatformType("smallint", false));
dbTypeMap.put(DbType.INTEGER, new DbPlatformType("integer", false));
dbTypeMap.put(DbType.BIGINT, new DbPlatformType("bigint", false));
dbTypeMap.put(DbType.REAL, new DbPlatformType("real"));
dbTypeMap.put(DbType.DECIMAL, new DbPlatformType("decimal", 16, 3));
}
/**
* Return a DB2 specific sequence IdGenerator that supports batch fetching
* sequence values.
*/
@Override
public PlatformIdGenerator createSequenceIdGenerator(BackgroundExecutor be, DataSource ds, int stepSize, String seqName) {
return new DB2SequenceIdGenerator(be, ds, seqName, sequenceBatchSize);
}
}
@@ -0,0 +1,39 @@
package io.ebean.platform.db2;
import io.ebean.annotation.Platform;
import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.config.dbplatform.DbType;
import io.ebean.config.dbplatform.SqlErrorCodes;
import java.sql.Types;
/**
* DB2 specific platform for i Series.
*
* @author Cédric Sougné
*/
public class DB2ForIPlatform extends BaseDB2Platform {
public DB2ForIPlatform() {
super();
this.platform = Platform.DB2FORI;
// Note: IBM i from 7.1 allow up to to 128
// TODO: Check if we need to introduce older platform (DB2ForI_6 ? but older
// documentation is not anymore published on ibm.com),
this.maxTableNameLength = 128;
this.maxConstraintNameLength = 128;
this.dbIdentity.setSupportsIdentity(true);
this.exceptionTranslator = new SqlErrorCodes().addAcquireLock("57033") // key -913
.addDuplicateKey("23505") // -803
// .addDataIntegrity("-407","-530","-531","-532","-543","-544","-545","-603","-667")
// we need SQLState, not code:
// https://www.ibm.com/support/knowledgecenter/en/SSEPEK_10.0.0/codes/src/tpc/db2z_n.html
.addDataIntegrity("23502", "23503", "23504", "23507", "23511", "23512", "23513", "42917", "23515")
.build();
booleanDbType = Types.SMALLINT;
dbTypeMap.put(DbType.BOOLEAN, new DbPlatformType("smallint default 0"));
}
}
@@ -0,0 +1,30 @@
package io.ebean.platform.db2;
import io.ebean.config.dbplatform.DbStandardHistorySupport;
/**
* DB2 based history support.
*/
public class DB2HistorySupport extends DbStandardHistorySupport {
@Override
public String getAsOfViewSuffix(String asOfViewSuffix) {
return " for system_time as of ?";
}
@Override
public String getVersionsBetweenSuffix(String asOfViewSuffix) {
return " for system_time between ? and ?";
}
@Override
public String getSysPeriodLower(String tableAlias, String sysPeriod) {
return tableAlias + "." + sysPeriod + "_start";
}
@Override
public String getSysPeriodUpper(String tableAlias, String sysPeriod) {
return tableAlias + "." + sysPeriod + "_end";
}
}
@@ -0,0 +1,22 @@
package io.ebean.platform.db2;
import io.ebean.annotation.PersistBatch;
import io.ebean.annotation.Platform;
/**
* DB2 specific platform for older DB2 versions. This platform is here for
* compatibility reasons. It uses a length limit of 18 chars for table and
* constraint names. Newer DB2 versions will support up to 128. It is strongly
* recommended to migrate to db2luw/DB2ForI or db2zos platform.
*/
public class DB2LegacyPlatform extends BaseDB2Platform {
public DB2LegacyPlatform() {
super();
this.platform = Platform.DB2;
// Note: DB2 (at least LUW supports length up to 128)
// TOOD: Check if we need to introduce a new platform (DB2_LUW_11 ?)
this.maxTableNameLength = 18;
this.maxConstraintNameLength = 18;
this.persistBatchOnCascade = PersistBatch.NONE;
}
}
@@ -0,0 +1,17 @@
package io.ebean.platform.db2;
import io.ebean.annotation.Platform;
/**
* DB2 platform for Linux/Unix/Windows.
* @author Roland Praml, FOCONIS AG
*
*/
public class DB2LuwPlatform extends BaseDB2Platform {
public DB2LuwPlatform() {
super();
this.platform = Platform.DB2LUW;
this.maxTableNameLength = 128;
this.maxConstraintNameLength = 128;
}
}
@@ -0,0 +1,35 @@
package io.ebean.platform.db2;
import io.ebean.BackgroundExecutor;
import io.ebean.config.dbplatform.SequenceBatchIdGenerator;
import javax.sql.DataSource;
/**
* DB2 specific sequence Id Generator.
*/
public class DB2SequenceIdGenerator extends SequenceBatchIdGenerator {
private final String baseSql;
private final String unionBaseSql;
/**
* Construct given a dataSource and sql to return the next sequence value.
*/
public DB2SequenceIdGenerator(BackgroundExecutor be, DataSource ds, String seqName, int batchSize) {
super(be, ds, seqName, batchSize);
this.baseSql = "values nextval for " + seqName;
this.unionBaseSql = ", nextval for " + seqName;
}
@Override
public String getSql(int batchSize) {
StringBuilder sb = new StringBuilder();
sb.append(baseSql);
for (int i = 1; i < batchSize; i++) {
sb.append(unionBaseSql);
}
return sb.toString();
}
}
@@ -0,0 +1,18 @@
package io.ebean.platform.db2;
import io.ebean.annotation.Platform;
/**
* DB2 platform for z/OS.
* Note: This platform is currently not tested!
* @author Roland Praml, FOCONIS AG
*
*/
public class DB2ZosPlatform extends BaseDB2Platform {
public DB2ZosPlatform() {
super();
this.platform = Platform.DB2ZOS;
this.maxTableNameLength = 128;
this.maxConstraintNameLength = 128;
}
}
@@ -0,0 +1,73 @@
package io.ebean.platform.db2;
import io.ebean.annotation.Platform;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.config.dbplatform.DatabasePlatformProvider;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
/**
* DB2 platform provider.
*/
public class Db2PlatformProvider implements DatabasePlatformProvider {
@Override
public String toString() {
return "DB2";
}
@Override
public boolean match(String name) {
return name.startsWith("db2");
}
@Override
public DatabasePlatform create(String name) {
if (name.equals("db2")) {
throw new IllegalArgumentException("Please choose the more specific db2luw/db2zos/db2fori platform. Refer to issue #2514 for details");
}
if (name.equals("db2legacy")) {
return new DB2LegacyPlatform();
}
if (name.equals("db2zos")) {
return new DB2ZosPlatform();
}
if (name.equals("db2fori")) {
return new DB2ForIPlatform();
}
if (name.equals("db2luw")) {
return new DB2LuwPlatform();
}
throw new IllegalArgumentException("Unknown DB2 platform, expecting db2luw/db2zos/db2fori but got "+ name);
}
@Override
public boolean matchByProductName(String productName) {
if (productName.contains("db2")) {
throw new IllegalArgumentException("For DB2 please explicitly choose either db2legacy/db2luw/db2zos/db2fori platform. Refer to issue #2514 for details");
}
return false;
}
@Override
public DatabasePlatform create(int majorVersion, int minorVersion, DatabaseMetaData meta, Connection connection) {
throw new IllegalStateException("Never called");
}
@Override
public boolean matchPlatform(Platform platform) {
return platform.base().equals(Platform.DB2);
}
@Override
public DatabasePlatform create(Platform platform) {
switch (platform) {
case DB2FORI: return new DB2ForIPlatform();
case DB2ZOS: return new DB2ZosPlatform();
case DB2LUW: return new DB2LuwPlatform();
case DB2: return new DB2LegacyPlatform();
}
throw new IllegalStateException("Unknown DB2 platform " + platform);
}
}
@@ -0,0 +1,30 @@
package io.ebean.platform.db2;
import io.ebean.config.dbplatform.SqlLimitRequest;
import io.ebean.config.dbplatform.SqlLimitResponse;
import io.ebean.config.dbplatform.SqlLimiter;
public class Db2SqlLimiter implements SqlLimiter {
@Override
public SqlLimitResponse limit(SqlLimitRequest request) {
StringBuilder sb = new StringBuilder(512);
sb.append("select ");
if (request.isDistinct()) {
sb.append("distinct ");
}
sb.append(request.getDbSql());
int firstRow = request.getFirstRow();
if (firstRow > 0) {
sb.append(" offset ").append(firstRow).append(" rows");
}
int maxRows = request.getMaxRows();
if (maxRows > 0) {
sb.append(" fetch next ").append(maxRows).append(" rows only");
}
String sql = request.getDbPlatform().completeSql(sb.toString(), request.getOrmQuery());
return new SqlLimitResponse(sql);
}
}
@@ -0,0 +1,4 @@
/**
* DB2 specific support.
*/
package io.ebean.platform.db2;
@@ -0,0 +1,8 @@
module io.ebean.platform.db2 {
exports io.ebean.platform.db2;
requires transitive io.ebean.api;
provides io.ebean.config.dbplatform.DatabasePlatformProvider with io.ebean.platform.db2.Db2PlatformProvider;
}
@@ -0,0 +1 @@
io.ebean.platform.db2.Db2PlatformProvider