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

+31
View File
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>platforms</artifactId>
<groupId>io.ebean</groupId>
<version>12.16.2-SNAPSHOT</version>
</parent>
<artifactId>ebean-platform-oracle</artifactId>
<dependencies>
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-api</artifactId>
<version>12.16.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>junit</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,19 @@
package io.ebean.platform.oracle;
import io.ebean.annotation.Platform;
import io.ebean.config.dbplatform.IdType;
/**
* Oracle 11 platform using rownum sql limiting.
*/
public class Oracle11Platform extends OraclePlatform {
public Oracle11Platform() {
super();
this.platform = Platform.ORACLE11;
this.columnAliasPrefix = "c";
this.sqlLimiter = new OracleRownumSqlLimiter();
this.basicSqlLimiter = new OracleRownumBasicLimiter();
dbIdentity.setIdType(IdType.SEQUENCE);
}
}
@@ -0,0 +1,13 @@
package io.ebean.platform.oracle;
/**
* Oracle 12 platform using column alias.
*/
public class Oracle12Platform extends OraclePlatform {
public Oracle12Platform() {
super();
//this.platform = Platform.ORACLE12;
this.columnAliasPrefix = "c";
}
}
@@ -0,0 +1,34 @@
package io.ebean.platform.oracle;
import io.ebean.config.dbplatform.SqlLimitRequest;
import io.ebean.config.dbplatform.SqlLimitResponse;
import io.ebean.config.dbplatform.SqlLimiter;
/**
* Use ANSI offset rows syntax.
*/
class OracleAnsiSqlRowsLimiter implements SqlLimiter {
@Override
public SqlLimitResponse limit(SqlLimitRequest request) {
String dbSql = request.getDbSql();
StringBuilder sb = new StringBuilder(50 + dbSql.length());
int firstRow = request.getFirstRow();
int maxRows = request.getMaxRows();
sb.append("select ");
if (request.isDistinct()) {
sb.append("distinct ");
}
sb.append(dbSql);
if (firstRow > 0) {
sb.append(" offset ").append(firstRow).append(" rows");
}
if (maxRows > 0) {
sb.append(" fetch next ").append(maxRows).append(" rows only");
}
return new SqlLimitResponse(sb.toString());
}
}
@@ -0,0 +1,116 @@
package io.ebean.platform.oracle;
import io.ebean.config.dbplatform.AbstractDbEncrypt;
import io.ebean.config.dbplatform.DbEncryptFunction;
/**
* Oracle encryption support.
*
* <p>
* You will typically need to create your own encryption and decryption
* functions similar to the example ones below.
* </p>
*
* <pre class="code">
*
* // Remember your DB user needs execute privilege on DBMS_CRYPTO
* // as well as your encryption and decryption functions
*
*
* // This is an Example Encryption function only - please create your own.
*
* CREATE OR REPLACE FUNCTION eb_encrypt(data IN VARCHAR, key in VARCHAR) RETURN RAW IS
*
* encryption_mode NUMBER := DBMS_CRYPTO.ENCRYPT_AES128 + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_PKCS5;
*
* BEGIN
* RETURN DBMS_CRYPTO.ENCRYPT(UTL_I18N.STRING_TO_RAW (data, 'AL32UTF8'),
* encryption_mode, UTL_I18N.STRING_TO_RAW(key, 'AL32UTF8') );
* END;
* /
*
*
*
* // This is an Example Decryption function only - please create your own.
*
* CREATE OR REPLACE FUNCTION eb_decrypt(data IN RAW, key IN VARCHAR) RETURN VARCHAR IS
*
* encryption_mode NUMBER := DBMS_CRYPTO.ENCRYPT_AES128 + DBMS_CRYPTO.CHAIN_CBC + DBMS_CRYPTO.PAD_PKCS5;
*
* BEGIN
* RETURN UTL_RAW.CAST_TO_VARCHAR2(DBMS_CRYPTO.DECRYPT
* (data, encryption_mode, UTL_I18N.STRING_TO_RAW(key, 'AL32UTF8')));
* END;
* /
* </pre>
*/
public class OracleDbEncrypt extends AbstractDbEncrypt {
/**
* Constructs the Oracle10DbEncrypt with default encrypt and decrypt stored procedures.
*/
public OracleDbEncrypt() {
this("eb_encrypt", "eb_decrypt");
}
/**
* Constructs the Oracle10DbEncrypt specifying encrypt and decrypt stored procedures.
*
* @param encryptFunction the encrypt stored procedure
* @param decryptFunction the decrypt stored procedure
*/
public OracleDbEncrypt(String encryptFunction, String decryptFunction) {
this.varcharEncryptFunction = new OraVarcharFunction(encryptFunction, decryptFunction);
this.dateEncryptFunction = new OraDateFunction(encryptFunction, decryptFunction);
}
/**
* VARCHAR encryption/decryption function.
*/
private static class OraVarcharFunction implements DbEncryptFunction {
private final String encryptfunction;
private final String decryptfunction;
public OraVarcharFunction(String encryptfunction, String decryptfunction) {
this.encryptfunction = encryptfunction;
this.decryptfunction = decryptfunction;
}
@Override
public String getDecryptSql(String columnWithTableAlias) {
return decryptfunction + "(" + columnWithTableAlias + ",?)";
}
@Override
public String getEncryptBindSql() {
return encryptfunction + "(?,?)";
}
}
/**
* DATE encryption/decryption function.
*/
private static class OraDateFunction implements DbEncryptFunction {
private final String encryptfunction;
private final String decryptfunction;
public OraDateFunction(String encryptfunction, String decryptfunction) {
this.encryptfunction = encryptfunction;
this.decryptfunction = decryptfunction;
}
@Override
public String getDecryptSql(String columnWithTableAlias) {
return "to_date(" + decryptfunction + "(" + columnWithTableAlias + ",?),'YYYYMMDD')";
}
@Override
public String getEncryptBindSql() {
return encryptfunction + "(to_char(?,'YYYYMMDD'),?)";
}
}
}
@@ -0,0 +1,39 @@
package io.ebean.platform.oracle;
import io.ebean.config.dbplatform.DbStandardHistorySupport;
/**
* Oracle Total recall based history support.
*/
public class OracleDbHistorySupport extends DbStandardHistorySupport {
/**
* Return the ' as of timestamp ?' clause appended after the table name.
*/
@Override
public String getAsOfViewSuffix(String asOfViewSuffix) {
return " as of TIMESTAMP ?";
}
@Override
public String getVersionsBetweenSuffix(String asOfViewSuffix) {
return " versions between timestamp ? and ?";
}
/**
* Returns the Oracle specific effective start column.
*/
@Override
public String getSysPeriodLower(String tableAlias, String sysPeriod) {
return "versions_starttime";
}
/**
* Returns the Oracle specific effective end column.
*/
@Override
public String getSysPeriodUpper(String tableAlias, String sysPeriod) {
return "versions_endtime";
}
}
@@ -0,0 +1,89 @@
package io.ebean.platform.oracle;
import io.ebean.BackgroundExecutor;
import io.ebean.Query;
import io.ebean.annotation.Platform;
import io.ebean.config.dbplatform.BasicSqlAnsiLimiter;
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.PlatformIdGenerator;
import io.ebean.config.dbplatform.SqlErrorCodes;
import javax.sql.DataSource;
import java.sql.Types;
/**
* Oracle specific platform.
*/
public class OraclePlatform extends DatabasePlatform {
public OraclePlatform() {
super();
this.platform = Platform.ORACLE;
this.supportsDeleteTableAlias = true;
this.maxInBinding = 1000;
this.maxTableNameLength = 30;
this.maxConstraintNameLength = 30;
this.dbEncrypt = new OracleDbEncrypt();
this.sqlLimiter = new OracleAnsiSqlRowsLimiter();
this.basicSqlLimiter = new BasicSqlAnsiLimiter();
this.historySupport = new OracleDbHistorySupport();
this.truncateTable = "truncate table %s cascade";
dbIdentity.setIdType(IdType.IDENTITY);
dbIdentity.setSupportsSequence(true);
dbIdentity.setSupportsIdentity(true);
dbIdentity.setSupportsGetGeneratedKeys(true);
this.dbDefaultValue.setFalse("0");
this.dbDefaultValue.setTrue("1");
this.dbDefaultValue.setNow("current_timestamp");
this.treatEmptyStringsAsNull = true;
this.likeClauseRaw = "like ?";
this.exceptionTranslator =
new SqlErrorCodes()
//.addAcquireLock("")
.addDuplicateKey("1")
.addDataIntegrity("2291")
.addSerializableConflict("72000")
.build();
this.openQuote = "\"";
this.closeQuote = "\"";
booleanDbType = Types.INTEGER;
dbTypeMap.put(DbType.BOOLEAN, new DbPlatformType("number(1)"));
dbTypeMap.put(DbType.INTEGER, new DbPlatformType("number", 10));
dbTypeMap.put(DbType.BIGINT, new DbPlatformType("number", 19));
dbTypeMap.put(DbType.REAL, new DbPlatformType("number", 19, 4));
dbTypeMap.put(DbType.DOUBLE, new DbPlatformType("number", 19, 4));
dbTypeMap.put(DbType.SMALLINT, new DbPlatformType("number", 5));
dbTypeMap.put(DbType.TINYINT, new DbPlatformType("number", 3));
dbTypeMap.put(DbType.DECIMAL, new DbPlatformType("number", 16, 3));
dbTypeMap.put(DbType.VARCHAR, new DbPlatformType("varchar2", 255));
dbTypeMap.put(DbType.LONGVARBINARY, new DbPlatformType("blob"));
dbTypeMap.put(DbType.LONGVARCHAR, new DbPlatformType("clob"));
dbTypeMap.put(DbType.VARBINARY, new DbPlatformType("raw", 255));
dbTypeMap.put(DbType.BINARY, new DbPlatformType("raw", 255));
dbTypeMap.put(DbType.TIME, new DbPlatformType("timestamp"));
}
@Override
public PlatformIdGenerator createSequenceIdGenerator(BackgroundExecutor be, DataSource ds, int stepSize, String seqName) {
return new OracleSequenceIdGenerator(be, ds, seqName, sequenceBatchSize);
}
@Override
protected String withForUpdate(String sql, Query.LockWait lockWait, Query.LockType lockType) {
switch (lockWait) {
case SKIPLOCKED:
return sql + " for update skip locked";
case NOWAIT:
return sql + " for update nowait";
default:
return sql + " for update";
}
}
}
@@ -0,0 +1,67 @@
package io.ebean.platform.oracle;
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;
/**
* Oracle platform provider.
*/
public class OraclePlatformProvider implements DatabasePlatformProvider {
@Override
public String toString() {
return "Oracle";
}
@Override
public boolean match(String name) {
return name.startsWith("oracle");
}
@Override
public DatabasePlatform create(String name) {
if (name.equals("oracle11") || name.equals("oracle10") || name.equals("oracle9")) {
return new Oracle11Platform();
}
if (name.equals("oracle12")) {
return new Oracle12Platform();
}
return new OraclePlatform();
}
@Override
public boolean matchByProductName(String productName) {
return productName.contains("oracle");
}
@Override
public DatabasePlatform create(int majorVersion, int minorVersion, DatabaseMetaData meta, Connection connection) {
if (majorVersion < 12) {
return new Oracle11Platform();
}
if (majorVersion < 13) {
return new Oracle12Platform();
}
return new OraclePlatform();
}
@Override
public boolean matchPlatform(Platform platform) {
return Platform.ORACLE.equals(platform.base());
}
@Override
public DatabasePlatform create(Platform platform) {
if (platform.equals(Platform.ORACLE11)) {
return new Oracle11Platform();
}
if (platform.equals(Platform.ORACLE12)) {
return new Oracle12Platform();
}
return new OraclePlatform();
}
}
@@ -0,0 +1,35 @@
package io.ebean.platform.oracle;
import io.ebean.config.dbplatform.BasicSqlLimiter;
/**
* Row limiter for Oracle 9,10,11 using rownum.
*/
public class OracleRownumBasicLimiter implements BasicSqlLimiter {
@Override
public String limit(String dbSql, int firstRow, int maxRows) {
if (firstRow < 1 && maxRows < 1) {
return dbSql;
}
StringBuilder sb = new StringBuilder(60 + dbSql.length());
int lastRow = maxRows;
if (lastRow > 0) {
lastRow += firstRow;
}
sb.append("select * from (select ");
if (maxRows > 0) {
sb.append("/*+ FIRST_ROWS(").append(maxRows).append(") */ ");
}
sb.append("a.*, rownum rn_ from (");
sb.append(dbSql).append(") a ");
if (lastRow > 0) {
sb.append(" where rownum <= ").append(lastRow);
}
sb.append(") ");
if (firstRow > 0) {
sb.append(" where rn_ > ").append(firstRow);
}
return sb.toString();
}
}
@@ -0,0 +1,54 @@
package io.ebean.platform.oracle;
import io.ebean.config.dbplatform.SqlLimitRequest;
import io.ebean.config.dbplatform.SqlLimitResponse;
import io.ebean.config.dbplatform.SqlLimiter;
/**
* Add ROWNUM column etc around SQL query to limit results.
*/
class OracleRownumSqlLimiter implements SqlLimiter {
@Override
public SqlLimitResponse limit(SqlLimitRequest request) {
// select *
// from ( select /*+ FIRST_ROWS(n) */ a.*, ROWNUM rnum
// from ( your_query_goes_here,
// with order by ) a
// where ROWNUM <=
// :MAX_ROW_TO_FETCH )
// where rnum >= :MIN_ROW_TO_FETCH;
String dbSql = request.getDbSql();
StringBuilder sb = new StringBuilder(60 + dbSql.length());
int firstRow = request.getFirstRow();
int lastRow = request.getMaxRows();
if (lastRow > 0) {
lastRow += firstRow;
}
sb.append("select * from (select ");
if (request.getMaxRows() > 0) {
sb.append("/*+ FIRST_ROWS(").append(request.getMaxRows()).append(") */ ");
}
sb.append("a.*, rownum rn_ from (select ");
if (request.isDistinct()) {
sb.append("distinct ");
}
sb.append(dbSql).append(") a ");
if (lastRow > 0) {
sb.append(" where rownum <= ").append(lastRow);
}
sb.append(") ");
if (firstRow > 0) {
sb.append(" where rn_ > ").append(firstRow);
}
String sql = request.getDbPlatform().completeSql(sb.toString(), request.getOrmQuery());
return new SqlLimitResponse(sql);
}
}
@@ -0,0 +1,27 @@
package io.ebean.platform.oracle;
import io.ebean.BackgroundExecutor;
import io.ebean.config.dbplatform.SequenceBatchIdGenerator;
import javax.sql.DataSource;
/**
* Oracle specific sequence Id Generator.
*/
public class OracleSequenceIdGenerator extends SequenceBatchIdGenerator {
private final String baseSql;
/**
* Construct given a dataSource and sql to return the next sequence value.
*/
public OracleSequenceIdGenerator(BackgroundExecutor be, DataSource ds, String seqName, int batchSize) {
super(be, ds, seqName, batchSize);
this.baseSql = "select " + seqName + ".nextval, a from (select level as a FROM dual CONNECT BY level <= ";
}
@Override
public String getSql(int batchSize) {
return baseSql + batchSize + ")";
}
}
@@ -0,0 +1,4 @@
/**
* Oracle specific support.
*/
package io.ebean.platform.oracle;
@@ -0,0 +1,8 @@
module io.ebean.platform.oracle {
exports io.ebean.platform.oracle;
requires transitive io.ebean.api;
provides io.ebean.config.dbplatform.DatabasePlatformProvider with io.ebean.platform.oracle.OraclePlatformProvider;
}
@@ -0,0 +1 @@
io.ebean.platform.oracle.OraclePlatformProvider
@@ -0,0 +1,27 @@
package io.ebean.platform.oracle;
import io.ebean.annotation.Platform;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class OraclePlatformProviderTest {
OraclePlatformProvider provider = new OraclePlatformProvider();
@Test
void create() {
assertThat(provider.create(Platform.ORACLE)).isInstanceOf(OraclePlatform.class);
assertThat(provider.create(Platform.ORACLE11)).isInstanceOf(Oracle11Platform.class);
assertThat(provider.create(Platform.ORACLE12)).isInstanceOf(Oracle12Platform.class);
}
@Test
void create_byName() {
assertThat(provider.create("oracle")).isInstanceOf(OraclePlatform.class);
assertThat(provider.create("oracle12")).isInstanceOf(Oracle12Platform.class);
assertThat(provider.create("oracle11")).isInstanceOf(Oracle11Platform.class);
assertThat(provider.create("oracle10")).isInstanceOf(Oracle11Platform.class);
assertThat(provider.create("oracle9")).isInstanceOf(Oracle11Platform.class);
}
}
@@ -0,0 +1,48 @@
package io.ebean.platform.oracle;
import io.ebean.config.PlatformConfig;
import io.ebean.config.dbplatform.DbPlatformType;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class OraclePlatformTest {
@Test
void columnAliasPrefix_Oracle11Platform() {
Oracle11Platform platform11 = new Oracle11Platform();
assertThat(platform11.getColumnAliasPrefix()).isEqualTo("c");
}
@Test
void columnAliasPrefix_Oracle12Platform() {
Oracle12Platform platform12 = new Oracle12Platform();
assertThat(platform12.getColumnAliasPrefix()).isEqualTo("c");
}
@Test
void columnAliasPrefix_OraclePlatform() {
OraclePlatform platform = new OraclePlatform();
assertThat(platform.getColumnAliasPrefix()).isNull();
}
@Test
void uuid_default() {
OraclePlatform platform = new OraclePlatform();
platform.configure(new PlatformConfig());
DbPlatformType dbType = platform.getDbTypeMap().get(DbPlatformType.UUID);
assertThat(dbType.renderType(0, 0)).isEqualTo("varchar2(40)");
}
@Test
void uuid_as_binary() {
OraclePlatform platform = new OraclePlatform();
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("raw(16)");
}
}