Java SQL Unescape unescapeMySQLString(String s)

Here you can find the source of unescapeMySQLString(String s)

Description

Unescape any MySQL escape sequences.

License

Apache License

Parameter

Parameter Description
s string to unescape, with the surrounding quotes.

Exception

Parameter Description
IllegalArgumentException if s is not a valid MySQL string.

Return

unescaped string, without the surrounding quotes.

Declaration

public static String unescapeMySQLString(String s) throws IllegalArgumentException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from  ww w  .j a  v  a2 s.  co m*/
     * Unescape any MySQL escape sequences.
     * See MySQL language reference Chapter 6 at
     * <a href="http://www.mysql.com/doc/">http://www.mysql.com/doc/</a>.
     * This function will <strong>not</strong> work for other SQL-like
     * dialects.
     *
     * @param s string to unescape, with the surrounding quotes.
     * @return unescaped string, without the surrounding quotes.
     * @throws IllegalArgumentException if s is not a valid MySQL string.
     */
    public static String unescapeMySQLString(String s) throws IllegalArgumentException {
        // note: the same buffer is used for both reading and writing
        // it works because the writer can never outrun the reader
        char chars[] = s.toCharArray();

        // the string must be quoted 'like this' or "like this"
        if (chars.length < 2 || chars[0] != chars[chars.length - 1] || (chars[0] != '\'' && chars[0] != '"')) {
            throw new IllegalArgumentException("not a valid MySQL string: " + s);
        }

        // parse the string and decode the backslash sequences; in addition,
        // quotes can be escaped 'like this: ''', "like this: """, or 'like this: "'
        int j = 1; // write position in the string (never exceeds read position)
        int f = 0; // state: 0 (normal), 1 (backslash), 2 (quote)
        for (int i = 1; i < chars.length - 1; i++) {
            if (f == 0) { // previous character was normal
                if (chars[i] == '\\') {
                    f = 1; // backslash
                } else if (chars[i] == chars[0]) {
                    f = 2; // quoting character
                } else {
                    chars[j++] = chars[i];
                }
            } else if (f == 1) { // previous character was a backslash
                switch (chars[i]) {
                case '0':
                    chars[j++] = '\0';
                    break;
                case '\'':
                    chars[j++] = '\'';
                    break;
                case '"':
                    chars[j++] = '"';
                    break;
                case 'b':
                    chars[j++] = '\b';
                    break;
                case 'n':
                    chars[j++] = '\n';
                    break;
                case 'r':
                    chars[j++] = '\r';
                    break;
                case 't':
                    chars[j++] = '\t';
                    break;
                case 'z':
                    chars[j++] = '\032';
                    break;
                case '\\':
                    chars[j++] = '\\';
                    break;
                default:
                    // if the character is not special, backslash disappears
                    chars[j++] = chars[i];
                    break;
                }
                f = 0;
            } else { // previous character was a quote
                // quoting characters must be doubled inside a string
                if (chars[i] != chars[0]) {
                    throw new IllegalArgumentException("not a valid MySQL string: " + s);
                }
                chars[j++] = chars[0];
                f = 0;
            }
        }
        // string contents cannot end with a special character
        if (f != 0) {
            throw new IllegalArgumentException("not a valid MySQL string: " + s);
        }

        // done
        return new String(chars, 1, j - 1);
    }
}

Related

  1. unescapeSQL(String input)
  2. unescapeSQLString(String b)
  3. unescapeSQLString(String b)
  4. unescapeSqlString(String input)