001package io.ebeaninternal.dbmigration.ddlgeneration; 002 003import io.ebeaninternal.dbmigration.ddlgeneration.platform.BaseDdlBuffer; 004import io.ebeaninternal.dbmigration.model.MConfiguration; 005import io.ebeaninternal.dbmigration.model.MTable; 006import io.ebeaninternal.dbmigration.model.ModelContainer; 007 008/** 009 * Write context holding the buffers for both apply and rollback DDL. 010 */ 011public class DdlWrite { 012 013 private final ModelContainer currentModel; 014 015 private final DdlBuffer applyDropDependencies; 016 017 private final DdlBuffer apply; 018 019 private final DdlBuffer applyForeignKeys; 020 021 private final DdlBuffer applyHistoryView; 022 023 private final DdlBuffer applyHistoryTrigger; 024 025 private final DdlBuffer dropAllForeignKeys; 026 027 private final DdlBuffer dropAll; 028 029 private final DdlOptions options; 030 031 /** 032 * Create without any configuration or current model (no history support). 033 */ 034 public DdlWrite() { 035 this(new MConfiguration(), new ModelContainer(), new DdlOptions()); 036 } 037 038 /** 039 * Create with a configuration. 040 */ 041 public DdlWrite(MConfiguration configuration, ModelContainer currentModel, DdlOptions options) { 042 this.currentModel = currentModel; 043 this.applyDropDependencies = new BaseDdlBuffer(configuration); 044 this.apply = new BaseDdlBuffer(configuration); 045 this.applyForeignKeys = new BaseDdlBuffer(configuration); 046 this.applyHistoryView = new BaseDdlBuffer(configuration); 047 this.applyHistoryTrigger = new BaseDdlBuffer(configuration); 048 this.dropAllForeignKeys = new BaseDdlBuffer(configuration); 049 this.dropAll = new BaseDdlBuffer(configuration); 050 this.options = options; 051 } 052 053 /** 054 * Return the DDL options. 055 */ 056 public DdlOptions getOptions() { 057 return options; 058 } 059 060 /** 061 * Return the Table information from the current model. 062 * <p> 063 * This is typically required for the history support (used to determine the list of columns 064 * included in the history when creating or recreating the associated trigger/stored procedure). 065 * </p> 066 */ 067 public MTable getTable(String tableName) { 068 return currentModel.getTable(tableName); 069 } 070 071 /** 072 * Return true if the apply buffers are all empty. 073 */ 074 public boolean isApplyEmpty() { 075 return apply.getBuffer().isEmpty() 076 && applyForeignKeys.getBuffer().isEmpty() 077 && applyHistoryView.getBuffer().isEmpty() 078 && applyHistoryTrigger.getBuffer().isEmpty() 079 && applyDropDependencies.getBuffer().isEmpty(); 080 } 081 082 /** 083 * Return the buffer that APPLY DDL is written to. 084 */ 085 public DdlBuffer apply() { 086 return apply; 087 } 088 089 /** 090 * Return the buffer that executes early to drop dependencies like views etc. 091 */ 092 public DdlBuffer applyDropDependencies() { 093 return applyDropDependencies; 094 } 095 096 /** 097 * Return the buffer that APPLY DDL is written to for foreign keys and their associated indexes. 098 * <p> 099 * Statements added to this buffer are executed after all the normal apply statements and 100 * typically 'add foreign key' is added to this buffer. 101 */ 102 public DdlBuffer applyForeignKeys() { 103 return applyForeignKeys; 104 } 105 106 /** 107 * Return the buffer that apply history-view DDL is written to. 108 */ 109 public DdlBuffer applyHistoryView() { 110 return applyHistoryView; 111 } 112 113 /** 114 * Return the buffer that apply history-trigger DDL is written to. 115 */ 116 public DdlBuffer applyHistoryTrigger() { 117 return applyHistoryTrigger; 118 } 119 120 /** 121 * Return the buffer used for the 'drop all DDL' for dropping foreign keys and associated indexes. 122 */ 123 public DdlBuffer dropAllForeignKeys() { 124 return dropAllForeignKeys; 125 } 126 127 /** 128 * Return the buffer used for the 'drop all DDL' to drop tables, views and history triggers etc. 129 */ 130 public DdlBuffer dropAll() { 131 return dropAll; 132 } 133 134}