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