001package io.ebeaninternal.dbmigration.ddlgeneration.platform; 002 003import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer; 004import io.ebeaninternal.dbmigration.model.MConfiguration; 005 006import java.io.IOException; 007import java.io.StringWriter; 008 009/** 010 * Base implementation of DdlBuffer using an underlying writer. 011 */ 012public class BaseDdlBuffer implements DdlBuffer { 013 014 protected final StringWriter writer; 015 016 protected final MConfiguration configuration; 017 018 public BaseDdlBuffer(MConfiguration configuration) { 019 this.configuration = configuration; 020 this.writer = new StringWriter(); 021 } 022 023 @Override 024 public MConfiguration getConfiguration() { 025 return configuration; 026 } 027 028 @Override 029 public boolean isEmpty() { 030 return writer.getBuffer().length() == 0; 031 } 032 033 @Override 034 public DdlBuffer appendWithSpace(String foreignKeyRestrict) throws IOException { 035 if (foreignKeyRestrict != null && !foreignKeyRestrict.isEmpty()) { 036 writer.append(" ").append(foreignKeyRestrict); 037 } 038 return this; 039 } 040 041 @Override 042 public DdlBuffer appendStatement(String content) throws IOException { 043 if (content != null && !content.isEmpty()) { 044 writer.append(content); 045 endOfStatement(); 046 } 047 return this; 048 } 049 050 @Override 051 public DdlBuffer append(String content) throws IOException { 052 writer.append(content); 053 return this; 054 } 055 056 @Override 057 public DdlBuffer append(String content, int space) throws IOException { 058 writer.append(content); 059 appendSpace(space, content); 060 return this; 061 } 062 063 protected void appendSpace(int max, String content) throws IOException { 064 int space = max - content.length(); 065 if (space > 0) { 066 for (int i = 0; i < space; i++) { 067 append(" "); 068 } 069 } 070 append(" "); 071 } 072 073 @Override 074 public DdlBuffer endOfStatement() throws IOException { 075 writer.append(";\n"); 076 return this; 077 } 078 079 /** 080 * Used to demarcate the end of a series of statements. 081 * This should be just whitespace or a sql comment. 082 */ 083 @Override 084 public DdlBuffer end() throws IOException { 085 if (!isEmpty()) { 086 writer.append("\n"); 087 } 088 return this; 089 } 090 091 @Override 092 public DdlBuffer newLine() throws IOException { 093 writer.append("\n"); 094 return this; 095 } 096 097 @Override 098 public String getBuffer() { 099 return writer.toString(); 100 } 101}