Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DB2: Add Tablespace support #2608

Merged
merged 2 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ebean-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-annotation</artifactId>
<version>7.6</version>
<version>7.7</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public enum EntityType {
private final boolean softDelete;
private final String draftTable;
private final PartitionMeta partitionMeta;
private final TablespaceMeta tablespaceMeta;
private final String storageEngine;
private final String dbComment;
private final boolean readAuditing;
Expand Down Expand Up @@ -275,6 +276,7 @@ public BeanDescriptor(BeanDescriptorMap owner, DeployBeanDescriptor<T> deploy) {
this.dependentTables = deploy.getDependentTables();
this.dbComment = deploy.getDbComment();
this.partitionMeta = deploy.getPartitionMeta();
this.tablespaceMeta = deploy.getTablespaceMeta();
this.storageEngine = deploy.getStorageEngine();
this.autoTunable = beanFinder == null && (entityType == EntityType.ORM || entityType == EntityType.VIEW);
// helper object used to derive lists of properties
Expand Down Expand Up @@ -2671,6 +2673,13 @@ public boolean suppressForeignKey() {
public PartitionMeta partitionMeta() {
return partitionMeta;
}

/**
* Return the tablespace details of the bean.
*/
public TablespaceMeta tablespaceMeta() {
return tablespaceMeta;
}

/**
* Return the storage engine.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.ebeaninternal.server.deploy;

import java.util.Objects;

/**
* Meta data for table spaces. If table space is configured, tablespaceName, indexTablespace, lobTablespace is never null;
*
* @author Noemi Szemenyei, FOCONIS AG
*
*/
public final class TablespaceMeta {

private final String tablespaceName;
private final String indexTablespace;
private final String lobTablespace;

public TablespaceMeta(String tablespaceName, String indexTablespace, String lobTablespace) {
this.tablespaceName = tablespaceName;
this.indexTablespace = indexTablespace;
this.lobTablespace = lobTablespace;
}

public String getTablespaceName() {
return tablespaceName;
}

public String getIndexTablespace() {
return indexTablespace;
}

public String getLobTablespace() {
return lobTablespace;
}

@Override
public int hashCode() {
return Objects.hash(indexTablespace, tablespaceName, lobTablespace);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TablespaceMeta other = (TablespaceMeta) obj;
return Objects.equals(indexTablespace, other.indexTablespace)
&& Objects.equals(tablespaceName, other.tablespaceName)
&& Objects.equals(lobTablespace, other.lobTablespace);
}

@Override
public String toString() {
return "tablespace=" + tablespaceName + ", indexTablespace=" + indexTablespace + ", lobTablespace=" + lobTablespace;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.ebeaninternal.server.deploy.InheritInfo;
import io.ebeaninternal.server.deploy.PartitionMeta;
import io.ebeaninternal.server.deploy.TableJoin;
import io.ebeaninternal.server.deploy.TablespaceMeta;
import io.ebeaninternal.server.deploy.parse.DeployBeanInfo;
import io.ebeaninternal.server.idgen.UuidV1IdGenerator;
import io.ebeaninternal.server.idgen.UuidV1RndIdGenerator;
Expand Down Expand Up @@ -135,6 +136,7 @@ public int compare(DeployBeanProperty o1, DeployBeanProperty o2) {
private ChangeLogFilter changeLogFilter;
private String dbComment;
private PartitionMeta partitionMeta;
private TablespaceMeta tablespaceMeta;
/**
* One of NONE, INDEX or EMBEDDED.
*/
Expand Down Expand Up @@ -252,6 +254,14 @@ public PartitionMeta getPartitionMeta() {
}
return partitionMeta;
}

public void setTablespaceMeta(TablespaceMeta tablespaceMeta) {
this.tablespaceMeta = tablespaceMeta;
}

public TablespaceMeta getTablespaceMeta() {
return tablespaceMeta;
}

public void setDraftable() {
draftable = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
import io.ebean.annotation.InvalidateQueryCache;
import io.ebean.annotation.ReadAudit;
import io.ebean.annotation.StorageEngine;
import io.ebean.annotation.Tablespace;
import io.ebean.annotation.View;
import io.ebean.config.TableName;
import io.ebeaninternal.api.CoreLog;
import io.ebeaninternal.server.deploy.BeanDescriptor.EntityType;
import io.ebeaninternal.server.deploy.IndexDefinition;
import io.ebeaninternal.server.deploy.InheritInfo;
import io.ebeaninternal.server.deploy.PartitionMeta;
import io.ebeaninternal.server.deploy.TablespaceMeta;
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;

import javax.persistence.AttributeOverride;
Expand Down Expand Up @@ -154,6 +156,18 @@ private void read(Class<?> cls) {
if (partition != null) {
descriptor.setPartitionMeta(new PartitionMeta(partition.mode(), partition.property()));
}
Tablespace tablespace = typeGet(cls, Tablespace.class);
if (tablespace != null) {
String indexTs = tablespace.index();
if("".equals(indexTs)) {
indexTs = tablespace.value();
}
String lobTs = tablespace.lob();
if("".equals(lobTs)) {
lobTs = tablespace.value();
}
descriptor.setTablespaceMeta(new TablespaceMeta(tablespace.value(), indexTs, lobTs));
}
Draftable draftable = typeGet(cls, Draftable.class);
if (draftable != null) {
descriptor.setDraftable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.ebeaninternal.dbmigration.migration.AddUniqueConstraint;
import io.ebeaninternal.dbmigration.migration.AlterColumn;
import io.ebeaninternal.dbmigration.migration.AlterForeignKey;
import io.ebeaninternal.dbmigration.migration.AlterTable;
import io.ebeaninternal.dbmigration.migration.ChangeSet;
import io.ebeaninternal.dbmigration.migration.CreateIndex;
import io.ebeaninternal.dbmigration.migration.CreateTable;
Expand Down Expand Up @@ -48,6 +49,8 @@ public void generate(DdlWrite writer, ChangeSet changeSet) {
// ignore
} else if (change instanceof DropTable) {
generate(writer, (DropTable) change);
} else if (change instanceof AlterTable) {
generate(writer, (AlterTable) change);
} else if (change instanceof AddTableComment) {
generate(writer, (AddTableComment) change);
} else if (change instanceof CreateIndex) {
Expand Down Expand Up @@ -94,6 +97,11 @@ public void generate(DdlWrite writer, DropTable dropTable) {
tableDdl.generate(writer, dropTable);
}

@Override
public void generate(DdlWrite writer, AlterTable alterTable) {
tableDdl.generate(writer, alterTable);
}

@Override
public void generate(DdlWrite writer, AddTableComment addTableComment) {
tableDdl.generate(writer, addTableComment);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.ebeaninternal.dbmigration.migration.AddUniqueConstraint;
import io.ebeaninternal.dbmigration.migration.AlterColumn;
import io.ebeaninternal.dbmigration.migration.AlterForeignKey;
import io.ebeaninternal.dbmigration.migration.AlterTable;
import io.ebeaninternal.dbmigration.migration.ChangeSet;
import io.ebeaninternal.dbmigration.migration.CreateIndex;
import io.ebeaninternal.dbmigration.migration.CreateTable;
Expand All @@ -24,6 +25,8 @@ public interface DdlHandler {
void generate(DdlWrite writer, CreateTable createTable);

void generate(DdlWrite writer, DropTable dropTable);

void generate(DdlWrite writer, AlterTable dropTable);

void generate(DdlWrite writer, AddTableComment addTableComment);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.ebeaninternal.dbmigration.migration.AddUniqueConstraint;
import io.ebeaninternal.dbmigration.migration.AlterColumn;
import io.ebeaninternal.dbmigration.migration.AlterForeignKey;
import io.ebeaninternal.dbmigration.migration.AlterTable;
import io.ebeaninternal.dbmigration.migration.CreateIndex;
import io.ebeaninternal.dbmigration.migration.CreateTable;
import io.ebeaninternal.dbmigration.migration.DropColumn;
Expand All @@ -27,6 +28,11 @@ public interface TableDdl {
* Write the drop column change.
*/
void generate(DdlWrite writer, DropTable dropTable);

/**
* Write alter table changes.
*/
void generate(DdlWrite writer, AlterTable dropTable);

/**
* Write the add column change.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.ebeaninternal.dbmigration.migration.AddUniqueConstraint;
import io.ebeaninternal.dbmigration.migration.AlterColumn;
import io.ebeaninternal.dbmigration.migration.AlterForeignKey;
import io.ebeaninternal.dbmigration.migration.AlterTable;
import io.ebeaninternal.dbmigration.migration.Column;
import io.ebeaninternal.dbmigration.migration.CreateIndex;
import io.ebeaninternal.dbmigration.migration.CreateTable;
Expand Down Expand Up @@ -236,6 +237,7 @@ public void generate(DdlWrite writer, CreateTable createTable) {
writeInlineForeignKeys(apply, createTable);
}
apply.newLine().append(")");
addTableTableSpaces(apply, createTable);
addTableStorageEngine(apply, createTable);
addTableCommentInline(apply, createTable);
if (partitionMode != null) {
Expand Down Expand Up @@ -267,6 +269,8 @@ public void generate(DdlWrite writer, CreateTable createTable) {
}
}



private String sequenceName(CreateTable createTable, List<Column> pk) {
return namingConvention.getSequenceName(createTable.getName(), pk.get(0).getName());
}
Expand All @@ -288,6 +292,19 @@ private void addComments(DdlBuffer apply, CreateTable createTable) {
}
}

/**
* Add tablespace declaration.
*/
protected void addTableTableSpaces(DdlBuffer apply, CreateTable createTable) {
String tableSpace = platformDdl.extract(createTable.getTablespace());
if (hasValue(tableSpace)) {
platformDdl.addTablespace(apply,
tableSpace,
platformDdl.extract(createTable.getIndexTablespace()),
platformDdl.extract(createTable.getLobTablespace()));
}
}

/**
* Add the table storage engine clause.
*/
Expand Down Expand Up @@ -647,6 +664,25 @@ public void generate(DdlWrite writer, DropTable dropTable) {
}
}

/**
* Add table related changes to DDL (tableSpace,...)
*/
@Override
public void generate(DdlWrite writer, AlterTable alterTable) {
String tableSpace = platformDdl.extract(alterTable.getTablespace());
String indexSpace = platformDdl.extract(alterTable.getIndexTablespace());
String lobSpace = platformDdl.extract(alterTable.getLobTablespace());
if (hasValue(tableSpace)
|| hasValue(indexSpace)
|| hasValue(lobSpace)) {

writer.apply().appendStatement(platformDdl.alterTableTablespace(alterTable.getName(),
DdlHelp.toTablespace(tableSpace),
DdlHelp.toTablespace(indexSpace),
DdlHelp.toTablespace(lobSpace)));
}
}

/**
* Add drop column DDL.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* DB2 platform specific DDL.
*/
public class DB2Ddl extends PlatformDdl {
private static final String MOVE_TABLE = "CALL SYSPROC.ADMIN_MOVE_TABLE(CURRENT_SCHEMA,'%s','%s','%s','%s','','','','','','MOVE')";

public DB2Ddl(DatabasePlatform platform) {
super(platform);
Expand All @@ -29,6 +30,16 @@ public DB2Ddl(DatabasePlatform platform) {
this.historyDdl = new Db2HistoryDdl();
}

@Override
public String alterTableTablespace(String tablename, String tableSpace, String indexSpace, String lobSpace) {
if(tableSpace == null) {
// if no tableSpace set, use the default tablespace USERSPACE1
return String.format(MOVE_TABLE, tablename.toUpperCase(), "USERSPACE1", "USERSPACE1", "USERSPACE1");
} else {
return String.format(MOVE_TABLE, tablename.toUpperCase(), tableSpace, indexSpace, lobSpace);
}
}

@Override
public String alterTableAddUniqueConstraint(String tableName, String uqName, String[] columns, String[] nullableColumns) {
if (nullableColumns == null || nullableColumns.length == 0) {
Expand All @@ -51,6 +62,11 @@ public String alterTableAddUniqueConstraint(String tableName, String uqName, Str
return sb.toString();
}

@Override
public void addTablespace(DdlBuffer apply, String tablespaceName, String indexTablespace, String lobTablespace) {
apply.append(" in ").append(tablespaceName).append(" index in ").append(indexTablespace).append(" long in ").append(lobTablespace);
}

@Override
public void alterTableAddColumn(DdlWrite writer, String tableName, Column column, boolean onHistoryTable, String defaultValue) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,20 @@ public void createWithHistory(DdlWrite writer, MTable table) {

// DB2 requires an EXACT copy (same column types with null/non-null, same order)
addSysPeriodColumns(writer, tableName);
writer.applyPostAlter().append("create table ").append(historyTableName)
.append(" as (select * from ").append(tableName).append(") with no data").endOfStatement();

DdlBuffer tableBuf = writer.applyPostAlter();
tableBuf.append("create table ").append(historyTableName)
.append(" as (select * from ").append(tableName).append(") with no data");

if (table.getTablespaceMeta() != null) {
String tableSpace = platformDdl.extract(table.getTablespaceMeta().getTablespaceName());
if (tableSpace != null && !tableSpace.isEmpty()) {
platformDdl.addTablespace(tableBuf,
tableSpace,
platformDdl.extract(table.getTablespaceMeta().getIndexTablespace()),
platformDdl.extract(table.getTablespaceMeta().getLobTablespace()));
}
}
tableBuf.endOfStatement();
enableSystemVersioning(writer.applyPostAlter(), tableName);
platformDdl.alterTable(writer, tableName).setHistoryHandled();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ protected void addSysPeriodColumns(DdlWrite writer, String baseTableName, String
protected void createHistoryTable(DdlBuffer apply, MTable table) {
createHistoryTableAs(apply, table);
createHistoryTableWithPeriod(apply);
// TODO: add tablespace here (currently no DbTriggerBased platforms with tablespace support)
apply.endOfStatement();
}

protected void createHistoryTableAs(DdlBuffer apply, MTable table) {
Expand All @@ -183,7 +185,7 @@ protected void createHistoryTableWithPeriod(DdlBuffer apply) {
writeColumnDefinition(apply, sysPeriodStart, sysPeriodType);
apply.append(",").newLine();
writeColumnDefinition(apply, sysPeriodEnd, sysPeriodType);
apply.newLine().append(")").endOfStatement();
apply.newLine().append(")");
}

/**
Expand Down
Loading