001package io.ebean.config;
002
003/**
004 * TableName holds catalog, schema and table name.
005 *
006 * @author emcgreal
007 */
008public final class TableName {
009
010  /**
011   * The catalog.
012   */
013  private String catalog;
014
015  /**
016   * The schema.
017   */
018  private String schema;
019
020  /**
021   * The name.
022   */
023  private String name;
024
025  /**
026   * Construct with the given catalog schema and table name.
027   * <p>
028   * Note the catalog and schema can be null.
029   * </p>
030   */
031  public TableName(String catalog, String schema, String name) {
032    super();
033    this.catalog = catalog != null ? catalog.trim() : null;
034    this.schema = schema != null ? schema.trim() : null;
035    this.name = name != null ? name.trim() : null;
036  }
037
038  /**
039   * Construct splitting the qualifiedTableName potentially into catalog, schema
040   * and name.
041   * <p>
042   * The qualifiedTableName can take the form of catalog.schema.tableName and is
043   * split on the '.' period character. The catalog and schema are optional.
044   * </p>
045   *
046   * @param qualifiedTableName the fully qualified table name using '.' between schema and table
047   *                           name etc (with catalog and schema optional).
048   */
049  public TableName(String qualifiedTableName) {
050    String[] split = qualifiedTableName.split("\\.");
051    int len = split.length;
052    if (split.length > 3) {
053      String m = "Error splitting " + qualifiedTableName + ". Expecting at most 2 '.' characters";
054      throw new RuntimeException(m);
055    }
056    if (len == 3) {
057      this.catalog = split[0];
058    }
059    if (len >= 2) {
060      this.schema = split[len - 2];
061    }
062    this.name = split[len - 1];
063  }
064
065  /**
066   * Parse a qualifiedTableName that might include a catalog and schema and just return the table name.
067   */
068  public static String parse(String qualifiedTableName) {
069    return new TableName(qualifiedTableName).getName();
070  }
071
072  @Override
073  public String toString() {
074    return getQualifiedName();
075  }
076
077  /**
078   * Gets the catalog.
079   *
080   * @return the catalog
081   */
082  public String getCatalog() {
083    return catalog;
084  }
085
086  /**
087   * Gets the schema.
088   *
089   * @return the schema
090   */
091  public String getSchema() {
092    return schema;
093  }
094
095  /**
096   * Gets the name.
097   *
098   * @return the name
099   */
100  public String getName() {
101    return name;
102  }
103
104  /**
105   * Returns the qualified name in the form catalog.schema.name.
106   * <p>
107   * Catalog and schema are optional.
108   * </p>
109   *
110   * @return the qualified name
111   */
112  public String getQualifiedName() {
113
114    StringBuilder buffer = new StringBuilder();
115
116    // Add catalog
117    if (catalog != null) {
118      buffer.append(catalog);
119    }
120
121    // Add schema
122    if (schema != null) {
123      if (buffer.length() > 0) {
124        buffer.append(".");
125      }
126      buffer.append(schema);
127    }
128
129    if (buffer.length() > 0) {
130      buffer.append(".");
131    }
132    buffer.append(name);
133
134    return buffer.toString();
135  }
136
137  /**
138   * Append a catalog and schema prefix if they exist to the string builder.
139   */
140  public void appendCatalogAndSchema(StringBuilder buffer) {
141    if (catalog != null) {
142      buffer.append(catalog).append(".");
143    }
144    if (schema != null) {
145      buffer.append(schema).append(".");
146    }
147  }
148
149  /**
150   * Checks if is table name is valid i.e. it has at least a name.
151   *
152   * @return true, if is valid
153   */
154  public boolean isValid() {
155    return name != null && !name.isEmpty();
156  }
157}