001package io.ebeaninternal.dbmigration.ddlgeneration.platform; 002 003import io.ebean.config.DatabaseConfig; 004import io.ebean.config.PropertiesWrapper; 005import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite; 006import io.ebeaninternal.dbmigration.migration.AddColumn; 007import io.ebeaninternal.dbmigration.migration.AlterColumn; 008import io.ebeaninternal.dbmigration.migration.Column; 009import io.ebeaninternal.dbmigration.migration.DropColumn; 010import io.ebeaninternal.dbmigration.model.MTable; 011 012import java.io.IOException; 013import java.util.List; 014 015public class HanaTableDdl extends BaseTableDdl { 016 017 private final HanaHistoryDdl historyDdl; 018 private final boolean generateUniqueDdl; 019 020 public HanaTableDdl(DatabaseConfig config, PlatformDdl platformDdl) { 021 super(config, platformDdl); 022 this.historyDdl = (HanaHistoryDdl) platformDdl.historyDdl; 023 if (config.getProperties() != null) { 024 PropertiesWrapper wrapper = new PropertiesWrapper("ebean", "hana", config.getProperties(), config.getClassLoadConfig()); 025 this.generateUniqueDdl = wrapper.getBoolean("generateUniqueDdl", false); 026 } else { 027 this.generateUniqueDdl = false; 028 } 029 } 030 031 @Override 032 protected void alterColumnDefaultValue(DdlWrite writer, AlterColumn alter) throws IOException { 033 // done in alterColumnBaseAttributes 034 } 035 036 @Override 037 public void generate(DdlWrite writer, AddColumn addColumn) throws IOException { 038 String tableName = addColumn.getTableName(); 039 MTable table = writer.getTable(tableName); 040 if (table == null) { 041 super.generate(writer, addColumn); 042 return; 043 } 044 045 boolean manageSystemVersioning = isTrue(table.isWithHistory()) && historyDdl.isSystemVersioningEnabled(tableName); 046 047 if (manageSystemVersioning) { 048 historyDdl.disableSystemVersioning(writer.apply(), table.getName(), this.generateUniqueDdl); 049 } 050 051 super.generate(writer, addColumn); 052 053 if (manageSystemVersioning) { 054 // make same changes to the history table 055 String historyTable = historyTable(tableName); 056 List<Column> columns = addColumn.getColumn(); 057 for (Column column : columns) { 058 alterTableAddColumn(writer.apply(), historyTable, column, true, true); 059 } 060 061 historyDdl.enableSystemVersioning(writer.apply(), table.getName(), historyTable, false, this.generateUniqueDdl); 062 } 063 } 064 065 @Override 066 public void generate(DdlWrite writer, AlterColumn alterColumn) throws IOException { 067 String tableName = alterColumn.getTableName(); 068 MTable table = writer.getTable(tableName); 069 if (table == null) { 070 super.generate(writer, alterColumn); 071 return; 072 } 073 074 boolean manageSystemVersioning = isTrue(table.isWithHistory()) && historyDdl.isSystemVersioningEnabled(tableName); 075 076 if (manageSystemVersioning) { 077 historyDdl.disableSystemVersioning(writer.apply(), tableName, this.generateUniqueDdl); 078 } 079 080 super.generate(writer, alterColumn); 081 082 if (manageSystemVersioning) { 083 // make same changes to the history table 084 String historyTable = historyTable(tableName); 085 if (hasValue(alterColumn.getType()) || hasValue(alterColumn.getDefaultValue()) || alterColumn.isNotnull() != null) { 086 AlterColumn alterHistoryColumn = new AlterColumn(); 087 alterHistoryColumn.setTableName(historyTable); 088 alterHistoryColumn.setColumnName(alterColumn.getColumnName()); 089 alterHistoryColumn.setType(alterColumn.getType()); 090 alterHistoryColumn.setDefaultValue(alterColumn.getDefaultValue()); 091 alterHistoryColumn.setNotnull(alterColumn.isNotnull()); 092 alterHistoryColumn.setCurrentType(alterColumn.getCurrentType()); 093 alterHistoryColumn.setCurrentDefaultValue(alterColumn.getCurrentDefaultValue()); 094 alterHistoryColumn.setCurrentNotnull(alterColumn.isCurrentNotnull()); 095 String histColumnDdl = platformDdl.alterColumnBaseAttributes(alterHistoryColumn); 096 097 // write the apply to history table 098 writer.apply().append(histColumnDdl).endOfStatement(); 099 } 100 101 historyDdl.enableSystemVersioning(writer.apply(), tableName, historyTable, false, this.generateUniqueDdl); 102 } 103 } 104 105 @Override 106 public void generate(DdlWrite writer, DropColumn dropColumn) throws IOException { 107 String tableName = dropColumn.getTableName(); 108 MTable table = writer.getTable(tableName); 109 if (table == null) { 110 super.generate(writer, dropColumn); 111 return; 112 } 113 114 boolean manageSystemVersioning = isTrue(table.isWithHistory()) && historyDdl.isSystemVersioningEnabled(tableName); 115 116 if (manageSystemVersioning) { 117 historyDdl.disableSystemVersioning(writer.apply(), tableName, this.generateUniqueDdl); 118 } 119 120 super.generate(writer, dropColumn); 121 122 if (manageSystemVersioning) { 123 // also drop from the history table 124 String historyTable = historyTable(tableName); 125 alterTableDropColumn(writer.apply(), historyTable, dropColumn.getColumnName()); 126 127 historyDdl.enableSystemVersioning(writer.apply(), tableName, historyTable, false, this.generateUniqueDdl); 128 } 129 } 130 131}