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 final 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    this.catalog = catalog != null ? catalog.trim() : null;
033    this.schema = schema != null ? schema.trim() : null;
034    this.name = name != null ? name.trim() : null;
035  }
036
037  /**
038   * Construct splitting the qualifiedTableName potentially into catalog, schema
039   * and name.
040   * <p>
041   * The qualifiedTableName can take the form of catalog.schema.tableName and is
042   * split on the '.' period character. The catalog and schema are optional.
043   * </p>
044   *
045   * @param qualifiedTableName the fully qualified table name using '.' between schema and table
046   *                           name etc (with catalog and schema optional).
047   */
048  public TableName(String qualifiedTableName) {
049    String[] split = qualifiedTableName.split("\\.");
050    int len = split.length;
051    if (split.length > 3) {
052      String m = "Error splitting " + qualifiedTableName + ". Expecting at most 2 '.' characters";
053      throw new RuntimeException(m);
054    }
055    if (len == 3) {
056      this.catalog = split[0];
057    }
058    if (len >= 2) {
059      this.schema = split[len - 2];
060    }
061    this.name = split[len - 1];
062  }
063
064  /**
065   * Parse a qualifiedTableName that might include a catalog and schema and just return the table name.
066   */
067  public static String parse(String qualifiedTableName) {
068    return new TableName(qualifiedTableName).getName();
069  }
070
071  @Override
072  public String toString() {
073    return getQualifiedName();
074  }
075
076  /**
077   * Gets the catalog.
078   *
079   * @return the catalog
080   */
081  public String getCatalog() {
082    return catalog;
083  }
084
085  /**
086   * Gets the schema.
087   *
088   * @return the schema
089   */
090  public String getSchema() {
091    return schema;
092  }
093
094  /**
095   * Gets the name.
096   *
097   * @return the name
098   */
099  public String getName() {
100    return name;
101  }
102
103  /**
104   * Returns the qualified name in the form catalog.schema.name.
105   * <p>
106   * Catalog and schema are optional.
107   * </p>
108   *
109   * @return the qualified name
110   */
111  public String getQualifiedName() {
112    StringBuilder buffer = new StringBuilder();
113    // Add catalog
114    if (catalog != null) {
115      buffer.append(catalog);
116    }
117    // Add schema
118    if (schema != null) {
119      if (buffer.length() > 0) {
120        buffer.append(".");
121      }
122      buffer.append(schema);
123    }
124    if (buffer.length() > 0) {
125      buffer.append(".");
126    }
127    return buffer.append(name).toString();
128  }
129
130  /**
131   * Append a catalog and schema prefix if they exist to the string builder.
132   */
133  public String withCatalogAndSchema(String name) {
134    if (schema != null) {
135      name = schema + "." + name;
136    }
137    if (catalog != null) {
138      name = catalog + "." + name;
139    }
140    return name;
141  }
142
143  /**
144   * Checks if is table name is valid i.e. it has at least a name.
145   */
146  public boolean isValid() {
147    return name != null && !name.isEmpty();
148  }
149}