Java Wildcard wildcardUserToSql(String exp)

Here you can find the source of wildcardUserToSql(String exp)

Description

Turn a user supplied wildcard expression with * into an SQL LIKE/NOT LIKE expression with %'s and other special characters.

License

GNU General Public License

Parameter

Parameter Description
exp the SQL LIKE parameter

Return

the equivalent wildcard expression

Declaration

public static String wildcardUserToSql(String exp) 

Method Source Code

//package com.java2s;
/*//from ww  w  .  ja v  a  2s.  c  o  m
 * Copyright (C) 2002-2017 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

public class Main {
    /**
     * Turn a user supplied wildcard expression with * into an SQL LIKE/NOT LIKE
     * expression with %'s and other special characters. Please note that constraint
     * value is saved in created java object (constraint) in form with '%' and in
     * this form is saved in xml as well.
     *
     * @param exp  the SQL LIKE parameter
     * @return     the equivalent wildcard expression
     */
    public static String wildcardUserToSql(String exp) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < exp.length(); i++) {
            String substring = exp.substring(i);
            if (substring.startsWith("*")) {
                sb.append("%");
            } else if (substring.startsWith("?")) {
                sb.append("_");
            } else if (substring.startsWith("\\*")) {
                sb.append("*");
                i++;
            } else if (substring.startsWith("\\?")) {
                sb.append("?");
                i++;
            } else if (substring.startsWith("%")) {
                sb.append("\\%");
            } else if (substring.startsWith("_")) {
                sb.append("\\_");
            } else if (substring.startsWith("\\")) {
                sb.append("\\\\");
            } else {
                sb.append(substring.charAt(0));
            }
        }

        return sb.toString();
    }
}

Related

  1. wildcardOrIsNullIfEmpty(String column, String inString)
  2. wildcardSearchToRegex(final String wildcard)
  3. wildcardSqlToUser(String exp)
  4. wildCardsToX(String s)
  5. wildcardToCaseInsensitiveRegex(final String pattern)