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,69 @@
package io.ebean.platform.mysql;
import io.ebean.Query;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.config.dbplatform.DbType;
import io.ebean.config.dbplatform.IdType;
import io.ebean.config.dbplatform.SqlErrorCodes;
import java.sql.Types;
/**
* Base platform for both MySql and MariaDB.
*/
public abstract class BaseMySqlPlatform extends DatabasePlatform {
public BaseMySqlPlatform() {
super();
this.useExtraTransactionOnIterateSecondaryQueries = true;
this.selectCountWithAlias = true;
this.supportsSavepointId = false;
this.inlineSqlUpdateLimit = true;
this.dbEncrypt = new MySqlDbEncrypt();
this.historySupport = new MySqlHistorySupport();
this.dbIdentity.setIdType(IdType.IDENTITY);
this.dbIdentity.setSupportsGetGeneratedKeys(true);
this.dbIdentity.setSupportsIdentity(true);
this.dbIdentity.setSupportsSequence(false);
this.dbDefaultValue.setNow("now(6)"); // must have same precision as TIMESTAMP
this.dbDefaultValue.setFalse("0");
this.dbDefaultValue.setTrue("1");
this.exceptionTranslator =
new SqlErrorCodes()
.addAcquireLock("1205")
.addDuplicateKey("1062", "1169")
.addDataIntegrity("630", "839", "840", "893", "1215", "1216", "1217", "1364", "1451", "1452", "1557")
.build();
this.openQuote = "`";
this.closeQuote = "`";
// use pipe for escaping as it depends if mysql runs in no_backslash_escapes or not.
this.likeClauseRaw = "like ? escape''";
this.likeClauseEscaped = "like ? escape'|'";
this.forwardOnlyHintOnFindIterate = true;
this.booleanDbType = Types.BIT;
dbTypeMap.put(DbType.BIT, new DbPlatformType("tinyint(1)"));
dbTypeMap.put(DbType.BOOLEAN, new DbPlatformType("tinyint(1)"));
dbTypeMap.put(DbType.TIMESTAMP, new DbPlatformType("datetime(6)"));
dbTypeMap.put(DbType.LOCALDATETIME, new DbPlatformType("datetime(6)"));
dbTypeMap.put(DbType.CLOB, new MySqlClob());
dbTypeMap.put(DbType.BLOB, new MySqlBlob());
dbTypeMap.put(DbType.BINARY, new DbPlatformType("binary", 255));
dbTypeMap.put(DbType.VARBINARY, new DbPlatformType("varbinary", 255));
dbTypeMap.put(DbType.JSON, new DbPlatformType("json", false));
dbTypeMap.put(DbType.JSONB, new DbPlatformType("json", false));
}
@Override
protected String withForUpdate(String sql, Query.LockWait lockWait, Query.LockType lockType) {
// NOWAIT and SKIP LOCKED currently not supported with MySQL
return sql + " for update";
}
}
@@ -0,0 +1,15 @@
package io.ebean.platform.mysql;
import io.ebean.annotation.Platform;
import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.config.dbplatform.DbType;
public class MySql55Platform extends BaseMySqlPlatform {
public MySql55Platform() {
super();
this.platform = Platform.MYSQL55;
dbTypeMap.put(DbType.TIMESTAMP, new DbPlatformType("datetime"));
dbTypeMap.put(DbType.LOCALDATETIME, new DbPlatformType("datetime"));
}
}
@@ -0,0 +1,37 @@
package io.ebean.platform.mysql;
import io.ebean.config.dbplatform.DbPlatformType;
/**
* Support for blob, mediumblob or longblob selection based on the deployment
* length.
* <p>
* If no deployment length is defined longblob is used.
* </p>
*/
public class MySqlBlob extends DbPlatformType {
private static final int POWER_2_16 = 65536;
private static final int POWER_2_24 = 16777216;
public MySqlBlob() {
super("blob");
}
@Override
public String renderType(int deployLength, int deployScale) {
if (deployLength >= POWER_2_24) {
return "longblob";
}
if (deployLength >= POWER_2_16) {
return "mediumblob";
}
if (deployLength < 1) {
// length not explicitly defined
return "longblob";
}
return "blob";
}
}
@@ -0,0 +1,37 @@
package io.ebean.platform.mysql;
import io.ebean.config.dbplatform.DbPlatformType;
/**
* Support for text, mediumtext or longtext selection based on the deployment
* length.
* <p>
* If no deployment length is defined longtext is used.
* </p>
*/
public class MySqlClob extends DbPlatformType {
private static final int POWER_2_16 = 65536;
private static final int POWER_2_24 = 16777216;
public MySqlClob() {
super("text");
}
@Override
public String renderType(int deployLength, int deployScale) {
if (deployLength >= POWER_2_24) {
return "longtext";
}
if (deployLength >= POWER_2_16) {
return "mediumtext";
}
if (deployLength < 1) {
// length not explicitly defined
return "longtext";
}
return "text";
}
}
@@ -0,0 +1,41 @@
package io.ebean.platform.mysql;
import io.ebean.config.dbplatform.AbstractDbEncrypt;
import io.ebean.config.dbplatform.DbEncryptFunction;
/**
* MySql aes_encrypt aes_decrypt based encryption support.
*/
public class MySqlDbEncrypt extends AbstractDbEncrypt {
public MySqlDbEncrypt() {
this.varcharEncryptFunction = new MyVarcharFunction();
this.dateEncryptFunction = new MyDateFunction();
}
private static class MyVarcharFunction implements DbEncryptFunction {
@Override
public String getDecryptSql(String columnWithTableAlias) {
return "CONVERT(AES_DECRYPT(" + columnWithTableAlias + ",?) USING UTF8)";
}
@Override
public String getEncryptBindSql() {
return "AES_ENCRYPT(?,?)";
}
}
private static class MyDateFunction implements DbEncryptFunction {
@Override
public String getDecryptSql(String columnWithTableAlias) {
return "STR_TO_DATE(AES_DECRYPT(" + columnWithTableAlias + ",?),'%Y%d%m')";
}
@Override
public String getEncryptBindSql() {
return "AES_ENCRYPT(DATE_FORMAT(?,'%Y%d%m'),?)";
}
}
}
@@ -0,0 +1,10 @@
package io.ebean.platform.mysql;
import io.ebean.config.dbplatform.DbViewHistorySupport;
/**
* Runtime support for @History with MySql.
*/
public class MySqlHistorySupport extends DbViewHistorySupport {
}
@@ -0,0 +1,15 @@
package io.ebean.platform.mysql;
import io.ebean.annotation.Platform;
/**
* MySQL specific platform.
*/
public class MySqlPlatform extends BaseMySqlPlatform {
public MySqlPlatform() {
super();
this.platform = Platform.MYSQL;
}
}
@@ -0,0 +1,58 @@
package io.ebean.platform.mysql;
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;
/**
* MySql platform provider.
*/
public class MySqlPlatformProvider implements DatabasePlatformProvider {
@Override
public String toString() {
return "MySql";
}
@Override
public boolean match(String name) {
return name.startsWith("mysql");
}
@Override
public DatabasePlatform create(String name) {
if (name.equals("mysql")) {
return new MySqlPlatform();
}
if (name.equals("mysql55")) {
return new MySql55Platform();
}
throw new IllegalArgumentException("Unexpected MySql platform name " + name);
}
@Override
public boolean matchByProductName(String productName) {
return productName.contains("mysql");
}
@Override
public DatabasePlatform create(int majorVersion, int minorVersion, DatabaseMetaData meta, Connection connection) {
if (majorVersion <= 5 && minorVersion <= 5) {
return new MySql55Platform();
}
return new MySqlPlatform();
}
@Override
public boolean matchPlatform(Platform platform) {
return Platform.MYSQL.equals(platform.base());
}
@Override
public DatabasePlatform create(Platform platform) {
return platform == Platform.MYSQL55 ? new MySql55Platform() : new MySqlPlatform();
}
}
@@ -0,0 +1,4 @@
/**
* MySql specific support.
*/
package io.ebean.platform.mysql;
@@ -0,0 +1,8 @@
module io.ebean.platform.mysql {
exports io.ebean.platform.mysql;
requires transitive io.ebean.api;
provides io.ebean.config.dbplatform.DatabasePlatformProvider with io.ebean.platform.mysql.MysqlPlatformProvider;
}
@@ -0,0 +1 @@
io.ebean.platform.mysql.MySqlPlatformProvider
@@ -0,0 +1,35 @@
package io.ebean.platform.mysql;
import io.ebean.config.PlatformConfig;
import io.ebean.config.dbplatform.DbPlatformType;
import io.ebean.platform.mysql.MySqlPlatform;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class MySqlPlatformTest {
@Test
public void uuid_default() {
MySqlPlatform platform = new MySqlPlatform();
platform.configure(new PlatformConfig());
DbPlatformType dbType = platform.getDbTypeMap().get(DbPlatformType.UUID);
assertThat(dbType.renderType(0, 0)).isEqualTo("varchar(40)");
}
@Test
public void uuid_as_binary() {
MySqlPlatform platform = new MySqlPlatform();
PlatformConfig config = new PlatformConfig();
config.setDbUuid(PlatformConfig.DbUuid.AUTO_BINARY);
platform.configure(config);
DbPlatformType dbType = platform.getDbTypeMap().get(DbPlatformType.UUID);
assertThat(dbType.renderType(0, 0)).isEqualTo("binary(16)");
}
}