001package io.ebeaninternal.dbmigration.ddlgeneration.platform;
002
003import java.util.regex.Matcher;
004import java.util.regex.Pattern;
005
006/**
007 * This class parses the inline column options of a create-statement (see
008 * https://www.ibm.com/docs/en/db2/11.5?topic=statements-create-table#sdx-synid_frag-column-options) into separate pieces if you
009 * want to alter a column. The later one has to be done in separate statements: See
010 * https://www.ibm.com/docs/en/db2/11.5?topic=statements-alter-table#sdx-synid_frag-column-options
011 * 
012 * <br>
013 * Note 1: This class parses only 'inline length', 'compact' and 'logged' options<br>
014 * Note 2: You cannot alter the compact or logged option on an existing column.
015 * 
016 * @author Roland Praml, FOCONIS AG
017 */
018public class DB2ColumnOptionsParser {
019  private static final Pattern INLINE_LENGTH = Pattern.compile(".*( inline length \\d+).*", Pattern.CASE_INSENSITIVE);
020  private static final Pattern NOT_LOGGED = Pattern.compile(".*?(( not)? logged).*", Pattern.CASE_INSENSITIVE);
021  private static final Pattern NOT_COMPACT = Pattern.compile(".*?(( not)? compact).*", Pattern.CASE_INSENSITIVE);
022
023  private final String type;
024  private final String inlineLength;
025  private final boolean logged;
026  private final boolean compact;
027  private boolean extraOptions;
028
029  DB2ColumnOptionsParser(String def) {
030    Matcher m = INLINE_LENGTH.matcher(def);
031    if (m.matches()) {
032      def = def.replace(m.group(1), "");
033      inlineLength = m.group(1).trim();
034    } else {
035      inlineLength = null;
036    }
037    m = NOT_LOGGED.matcher(def);
038    if (m.matches()) {
039      extraOptions = true;
040      def = def.replace(m.group(1), "");
041      logged = m.group(2) == null;
042    } else {
043      logged = true; // default
044    }
045
046    m = NOT_COMPACT.matcher(def);
047    if (m.matches()) {
048      extraOptions = true;
049      def = def.replace(m.group(1), "");
050      compact = m.group(2) == null;
051    } else {
052      compact = false; // default
053    }
054    type = def.trim();
055  }
056
057  public String getInlineLength() {
058    return inlineLength;
059  }
060
061  public String getType() {
062    return type;
063  }
064
065  public boolean isLogged() {
066    return logged;
067  }
068
069  public boolean isCompact() {
070    return compact;
071  }
072
073  public boolean hasExtraOptions() {
074    return extraOptions;
075  }
076
077}