Java String Quote quoteSqlIdentifier(String identifier)

Here you can find the source of quoteSqlIdentifier(String identifier)

Description

Quote an SQL identifier by enclosing it in double-quote characters and escaping any double-quote characters with an extra double-quote character.

License

Apache License

Parameter

Parameter Description
identifier the identifier to quote

Return

the quoted identifier

Declaration

static String quoteSqlIdentifier(String identifier) 

Method Source Code

//package com.java2s;
/*//w  w w  .  j  av  a  2 s. c o m
 * Apache Derby is a subproject of the Apache DB project, and is licensed under
 * the Apache License, Version 2.0 (the "License"); you may not use these files
 * 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.
 *
 * Splice Machine, Inc. has modified this file.
 *
 * All Splice Machine modifications are Copyright 2012 - 2016 Splice Machine, Inc.,
 * and are licensed to you under the License; you may not use this file except in
 * compliance with the License.
 *
 * 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 {
    /**
     * Quote an SQL identifier by enclosing it in double-quote characters
     * and escaping any double-quote characters with an extra double-quote
     * character.
     *
     * @param identifier the identifier to quote
     * @return the quoted identifier
     */
    static String quoteSqlIdentifier(String identifier) {
        // In the common case the length of the return value is the length of
        // the identifier plus the two surrounding double quotes. Use that as
        // the initial capacity of the buffer.
        StringBuffer retValue = new StringBuffer(identifier.length() + 2);

        final char quote = '"';

        retValue.append(quote);

        for (int i = 0; i < identifier.length(); i++) {
            char ch = identifier.charAt(i);
            if (ch == quote) {
                retValue.append(quote);
            }
            retValue.append(ch);
        }

        retValue.append(quote);

        return retValue.toString();
    }
}

Related

  1. quotesEscape(String text)
  2. QuoteSingle(String S)
  3. quoteSpecial(String orig)
  4. quoteSpecialCharacters(final String oldString)
  5. quoteSQL(String string)
  6. quoteSqlIdentifier(String name)
  7. quoteSQLString(String s)
  8. quoteStr(Object o)
  9. quoteStrictOccurrences(final String textToMarkup, final String filter)