Java String Quote quoteIdentifier(String identifier, boolean isPedantic)

Here you can find the source of quoteIdentifier(String identifier, boolean isPedantic)

Description

Surrounds identifier with "`" and duplicates these symbols inside the identifier.

License

Open Source License

Parameter

Parameter Description
identifier in pedantic mode (connection property pedantic=true) identifier is treated as unquoted (as it is stored in the database) even if it starts and ends with "`"; in non-pedantic mode if identifier starts and ends with "`" method treats it as already quoted and doesn't modify.
isPedantic are we in pedantic mode

Return

  • null -> null
  • abc -> `abc`
  • ab`c -> `ab``c`
  • ab"c -> `ab"c`
  • `ab``c` -> `ab``c` in non-pedantic mode or ```ab````c``` in pedantic mode
  • Declaration

    public static String quoteIdentifier(String identifier, boolean isPedantic) 
    

    Method Source Code

    //package com.java2s;
    /*//from w w  w  . j av a 2s .  c om
     Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
        
        
      The MySQL Connector/J is licensed under the terms of the GPLv2
      <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
      There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
      this software, see the FLOSS License Exception
      <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
        
      This program is free software; you can redistribute it and/or modify it under the terms
      of the GNU General Public License as published by the Free Software Foundation; version 2
      of the License.
        
      This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
      without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      See the GNU General Public License for more details.
        
      You should have received a copy of the GNU General Public License along with this
      program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
      Floor, Boston, MA 02110-1301  USA
        
        
        
     */
    
    public class Main {
        /**
         * Surrounds identifier with quoteChar and duplicates these symbols inside the identifier.
         *
         * @param quoteChar ` or "
         * @param identifier in pedantic mode (connection property pedantic=true) identifier is treated as unquoted
         *        (as it is stored in the database) even if it starts and ends with quoteChar;
         *        in non-pedantic mode if identifier starts and ends with quoteChar method treats it as already quoted and doesn't modify.
         * @param isPedantic are we in pedantic mode
         *
         * @return
         * With quoteChar="`":<br>
         * <li>null -> null</li>
         * <li>abc -> `abc`</li>
         * <li>ab`c -> `ab``c`</li>
         * <li>ab"c -> `ab"c`</li>
         * <li>`ab``c` -> `ab``c` in non-pedantic mode or ```ab````c``` in pedantic mode</li>
         * With quoteChar="\"":<br>
         * <li>null -> null</li>
         * <li>abc -> "abc"</li>
         * <li>ab`c -> "ab`c"</li>
         * <li>ab"c -> "ab""c"</li>
         * <li>"ab""c" -> "ab""c" in non-pedantic mode or """ab""""c""" in pedantic mode</li>
         */
        public static String quoteIdentifier(String identifier, String quoteChar, boolean isPedantic) {
            if (identifier == null) {
                return null;
            }
    
            if (!isPedantic && identifier.startsWith(quoteChar) && identifier.endsWith(quoteChar)) {
                return identifier;
            }
    
            return quoteChar + identifier.replaceAll(quoteChar, quoteChar + quoteChar) + quoteChar;
        }
    
        /**
         * Surrounds identifier with "`" and duplicates these symbols inside the identifier.
         *
         * @param identifier in pedantic mode (connection property pedantic=true) identifier is treated as unquoted
         *        (as it is stored in the database) even if it starts and ends with "`";
         *        in non-pedantic mode if identifier starts and ends with "`" method treats it as already quoted and doesn't modify.
         * @param isPedantic are we in pedantic mode
         *
         * @return
         * <li>null -> null</li>
         * <li>abc -> `abc`</li>
         * <li>ab`c -> `ab``c`</li>
         * <li>ab"c -> `ab"c`</li>
         * <li>`ab``c` -> `ab``c` in non-pedantic mode or ```ab````c``` in pedantic mode</li>
         */
        public static String quoteIdentifier(String identifier, boolean isPedantic) {
            return quoteIdentifier(identifier, "`", isPedantic);
        }
    
        private static boolean startsWith(byte[] dataFrom, String chars) {
            for (int i = 0; i < chars.length(); i++) {
                if (dataFrom[i] != chars.charAt(i)) {
                    return false;
                }
            }
            return true;
        }
    
        private static boolean endsWith(byte[] dataFrom, String suffix) {
            for (int i = 1; i <= suffix.length(); i++) {
                int dfOffset = dataFrom.length - i;
                int suffixOffset = suffix.length() - i;
                if (dataFrom[dfOffset] != suffix.charAt(suffixOffset)) {
                    return false;
                }
            }
            return true;
        }
    }
    

    Related

    1. quoteHtml(final String s)
    2. quoteHtml(String s)
    3. quoteHTML(String string)
    4. quoteHtmlContent(String x)
    5. quoteIdentifier(String identifier)
    6. quoteIdentifier(String identifier, boolean isPedantic)
    7. quoteIdentifier(String identifier, String quoteChar)
    8. quoteIdentifier(String s)
    9. quoteIdentifier(String s)