QualifiedTableName.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2004-2008, DbUnit.org
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  */
  21. package org.dbunit.util;

  22. import org.dbunit.DatabaseUnitRuntimeException;
  23. import org.dbunit.database.DatabaseConfig;
  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;

  26. /**
  27.  * Utility to parse a fully qualified table name into its components <i>schema</i> and <i>table</i>.
  28.  * @author gommma
  29.  * @author Last changed by: $Author$
  30.  * @version $Revision$ $Date$
  31.  * @since 2.3.0
  32.  */
  33. public class QualifiedTableName
  34. {
  35.     /**
  36.      * Logger for this class
  37.      */
  38.     private static final Logger logger = LoggerFactory.getLogger(QualifiedTableName.class);

  39.     private String schema;
  40.     private String table;
  41.     private String escapePattern;
  42.    
  43.     /**
  44.      * Creates an object parsing the given tableName.
  45.      * @param tableName The table name, either qualified or unqualified. If it is qualified (like "MYSCHEMA.MYTABLE")
  46.      * this schema name has precedence before the given <code>defaultSchema</code> parameter.
  47.      * @param defaultSchema The schema that is used when the given tableName is not fully qualified
  48.      * (i.e. it is not like "MYSCHEMA.MYTABLE"). Can be null
  49.      */
  50.     public QualifiedTableName(String tableName, String defaultSchema)
  51.     {
  52.         this(tableName, defaultSchema, null);
  53.     }
  54.    
  55.     /**
  56.      * Creates an object parsing the given tableName.
  57.      * @param tableName The table name, either qualified or unqualified. If it is qualified (like "MYSCHEMA.MYTABLE")
  58.      * this schema name has precedence before the given <code>defaultSchema</code> parameter.
  59.      * @param defaultSchema The schema that is used when the given tableName is not fully qualified
  60.      * (i.e. it is not like "MYSCHEMA.MYTABLE"). Can be null
  61.      * @param escapePattern The escape pattern to be applied on the prefix and the name. Can be null.
  62.      */
  63.     public QualifiedTableName(String tableName, String defaultSchema, String escapePattern)
  64.     {
  65.         if(tableName==null){
  66.             throw new NullPointerException("The parameter 'tableName' must not be null");
  67.         }
  68.         parseFullTableName(tableName, defaultSchema);
  69.         this.escapePattern = escapePattern;
  70.     }

  71.     /**
  72.      * Parses the given full table name into a schema name and a table name if available. If
  73.      * no schema is set the value of the {@link #getSchema()} is null.
  74.      * Sets the corresponding members of this class if found.
  75.      * @param fullTableName potentially fully qualified table name
  76.      * @param defaultSchema The schema that is used when the given tableName is not fully qualified
  77.      * (i.e. it is not like "MYSCHEMA.MYTABLE"). Can be null
  78.      */
  79.     private void parseFullTableName(String fullTableName, String defaultSchema)
  80.     {
  81.         if(fullTableName==null){
  82.             throw new NullPointerException("The parameter 'fullTableName' must not be null");
  83.         }
  84.         // check if a schema is in front
  85.         int firstDotIndex = fullTableName.indexOf(".");
  86.         if (firstDotIndex != -1) {
  87.             // set schema
  88.             this.schema = fullTableName.substring(0, firstDotIndex);
  89.             // set table name without schema
  90.             this.table = fullTableName.substring(firstDotIndex + 1);
  91.         }
  92.         else
  93.         {
  94.             // No schema name found in table
  95.             this.table = fullTableName;
  96.             // If the schema has not been found in the given table name
  97.             // (that means there is no "MYSCHEMA.MYTABLE" but only a "MYTABLE")
  98.             // then set the schema to the given default schema
  99.             this.schema = defaultSchema;
  100.         }
  101.     }

  102.     /**
  103.      * @return The schema name which can be null if no schema has been given in the constructor
  104.      */
  105.     public String getSchema() {
  106.         return schema;
  107.     }

  108.     /**
  109.      * @return The name of the plain, unqualified table
  110.      */
  111.     public String getTable() {
  112.         return table;
  113.     }

  114.     /**
  115.      * @return The qualified table name with the prepended schema if a schema is available
  116.      */
  117.     public String getQualifiedName()
  118.     {
  119.         logger.debug("getQualifiedName() - start");
  120.        
  121.         return getQualifiedName(this.schema, this.table, this.escapePattern);
  122.     }

  123.     /**
  124.      * Returns the qualified name using the values given in the constructor.
  125.      * The qualified table name is <b>only</b> returned if the feature
  126.      * {@link DatabaseConfig#FEATURE_QUALIFIED_TABLE_NAMES} is set. Otherwise the given
  127.      * name is returned unqualified (i.e. without prepending the prefix/schema).
  128.      * @return The qualified table name with the prepended schema if a schema is available.
  129.      * The qualified table name is <b>only</b> returned if the feature
  130.      * {@link DatabaseConfig#FEATURE_QUALIFIED_TABLE_NAMES} is set in the given <code>config</code>.
  131.      */
  132.     public String getQualifiedNameIfEnabled(DatabaseConfig config)
  133.     {
  134.         logger.debug("getQualifiedNameIfEnabled(config={}) - start", config);

  135.         boolean feature = config.getFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES);
  136.         if (feature)
  137.         {
  138.             logger.debug("Qualified table names feature is enabled. Returning qualified table name");
  139.             return getQualifiedName(this.schema, this.table, this.escapePattern);
  140.         }
  141.         else
  142.         {
  143.             logger.debug("Qualified table names feature is disabled. Returning plain table name");
  144. //          return this.table;
  145.             return getQualifiedName(null, this.table, this.escapePattern);
  146.         }
  147.     }

  148.     public String toString() {
  149.         final StringBuilder sb = new StringBuilder();
  150.         sb.append(getClass().getName()).append("[");
  151.         sb.append("schema=").append(schema);
  152.         sb.append(", table=").append(table);
  153.         sb.append("]");
  154.         return sb.toString();
  155.     }
  156.    
  157.    
  158.    
  159.     /**
  160.      * Returns the specified name qualified with the specified prefix. The name
  161.      * is not modified if the prefix is <code>null</code> or if the name is
  162.      * already qualified.
  163.      * <p>
  164.      * Example: <br>
  165.      * <code>getQualifiedName(null, "NAME")</code> returns
  166.      * <code>"NAME"</code>. <code>getQualifiedName("PREFIX", "NAME")</code>
  167.      * returns <code>"PREFIX.NAME"</code> and
  168.      * <code>getQualifiedName("PREFIX2", "PREFIX1.NAME")</code>
  169.      * returns <code>"PREFIX1.NAME"</code>.
  170.      *
  171.      * @param prefix the prefix that qualifies the name and is prepended if the name is not qualified yet
  172.      * @param name the name The name to be qualified if it is not qualified already
  173.      * @param escapePattern The escape pattern to be applied on the prefix and the name. Can be null.
  174.      * @return The qualified name
  175.      */
  176.     private String getQualifiedName(String prefix, String name,
  177.             String escapePattern)
  178.     {
  179.         if(logger.isDebugEnabled())
  180.             logger.debug("getQualifiedName(prefix={}, name={}, escapePattern={}) - start",
  181.                     new String[] {prefix, name, escapePattern});

  182.         if (escapePattern != null)
  183.         {
  184.             prefix = getEscapedName(prefix, escapePattern);
  185.             name = getEscapedName(name, escapePattern);
  186.         }

  187.         if (prefix == null || prefix.equals("") || name.indexOf(".") >= 0)
  188.         {
  189.             return name;
  190.         }

  191.         return prefix + "." + name;
  192.     }
  193.    
  194.    
  195.     /**
  196.      * @param name
  197.      * @param escapePattern
  198.      * @return
  199.      */
  200.     private String getEscapedName(String name, String escapePattern)
  201.     {
  202.         logger.debug("getEscapedName(name={}, escapePattern={}) - start", name, escapePattern);

  203.         if (name == null)
  204.         {
  205.             return name;
  206.         }

  207.         if (escapePattern == null)
  208.         {
  209.             throw new NullPointerException(
  210.                     "The parameter 'escapePattern' must not be null");
  211.         }
  212.         if(escapePattern.trim().equals(""))
  213.         {
  214.             throw new DatabaseUnitRuntimeException("Empty string is an invalid escape pattern!");
  215.         }
  216.    
  217.         int split = name.indexOf(".");
  218.         if (split > 1)
  219.         {
  220.             return getEscapedName(name.substring(0, split), escapePattern) + "." + getEscapedName(name.substring(split + 1), escapePattern);
  221.         }

  222.         int index = escapePattern.indexOf("?");
  223.         if (index >=0 )
  224.         {
  225.             String prefix = escapePattern.substring(0, index);
  226.             String suffix = escapePattern.substring(index + 1);

  227.             return prefix + name + suffix;
  228.         }
  229.         else if(escapePattern.length() == 1)
  230.         {
  231.             // No "?" in the escape pattern and only one character.
  232.             // use the given escapePattern to surround the given name
  233.             return escapePattern + name + escapePattern;
  234.         }
  235.         else
  236.         {
  237.             logger.warn("Invalid escape pattern '" + escapePattern + "'. Will not escape name '" + name + "'.");
  238.             return name;
  239.         }
  240.     }

  241. }