Normalize a SQL identifer, up-casing if , and handling of (SQL 2003, section 5.2). : String Parser « Data Type « Java






Normalize a SQL identifer, up-casing if , and handling of (SQL 2003, section 5.2).

    
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Properties;

/*

   Derby - Class org.apache.derby.iapi.util.PropertyUtil

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to you under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */



public class Main {


  /** Convert string to uppercase
   * Always use the java.util.ENGLISH locale
   * @param s   string to uppercase
   * @return uppercased string
   */
  public static String SQLToUpperCase(String s)
  {
      return s.toUpperCase(Locale.ENGLISH);
  }

  /** Compares two strings 
   * Strings will be uppercased in english and compared
   * equivalent to s1.equalsIgnoreCase(s2)
   * throws NPE if s1 is null
   *
   * @param s1  first string to compare
   * @param s2  second string to compare
   *
   * @return   true if the two upppercased ENGLISH values are equal
   *           return false if s2 is null
   */
  public static boolean SQLEqualsIgnoreCase(String s1, String s2)
  {
    if (s2 == null)
      return  false;
    else
      return SQLToUpperCase(s1).equals(SQLToUpperCase(s2));

  }
  /**
   * Normalize a SQL identifer, up-casing if <regular identifer>,
   * and handling of <delimited identifer> (SQL 2003, section 5.2).
   * The normal form is used internally in Derby.
   *
   * @param id syntacically correct SQL identifier
   */
  public static String normalizeSQLIdentifier(String id) {
    if (id.length() == 0) {
      return id;
    }

    if (id.charAt(0) == '"' &&
        id.length() >= 3   &&
        id.charAt(id.length() - 1) == '"') {
      // assume syntax is OK, thats is, any quotes inside are doubled:

      return compressQuotes(
        id.substring(1, id.length() - 1), "\"\"");

    } else {
      return SQLToUpperCase(id);
    }
  }

  /**
   * Compress 2 adjacent (single or double) quotes into a single (s or d)
   * quote when found in the middle of a String.
   *
   * NOTE:  """" or '''' will be compressed into "" or ''.
   * This function assumes that the leading and trailing quote from a
   * string or delimited identifier have already been removed.
   * @param source string to be compressed
   * @param quotes string containing two single or double quotes.
   * @return String where quotes have been compressed
   */
  public static String compressQuotes(String source, String quotes)
  {
    String  result = source;
    int   index;

    /* Find the first occurrence of adjacent quotes. */
    index = result.indexOf(quotes);

    /* Replace each occurrence with a single quote and begin the
     * search for the next occurrence from where we left off.
     */
    while (index != -1) {
      result = result.substring(0, index + 1) +
           result.substring(index + 2);
      index = result.indexOf(quotes, index + 1);
    }

    return result;
  }

}

   
    
    
    
  








Related examples in the same category

1.Parse Comma Delimited List
2.Parse Fraction
3.Parse String to array of Strings while treating quoted values as single element
4.Parse a method signature or method call signature
5.Parse basic types
6.Decodes a String with Numeric Character References
7.Returns true if the argument contains a number
8.Convert a String to an int, returning zero if the conversion fails.
9.Parsing primitives from String's without creating any objects
10.Checks whether the String a valid Java number.
11.Check whether the given String has actual text.