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