Java Code Examples for com.mysql.cj.util.StringUtils#indexOfIgnoreCase()

The following examples show how to use com.mysql.cj.util.StringUtils#indexOfIgnoreCase() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MemorySizePropertyDefinition.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Integer parseObject(String value, ExceptionInterceptor exceptionInterceptor) {
    this.multiplier = 1;

    if (value.endsWith("k") || value.endsWith("K") || value.endsWith("kb") || value.endsWith("Kb") || value.endsWith("kB") || value.endsWith("KB")) {
        this.multiplier = 1024;
        int indexOfK = StringUtils.indexOfIgnoreCase(value, "k");
        value = value.substring(0, indexOfK);
    } else if (value.endsWith("m") || value.endsWith("M") || value.endsWith("mb") || value.endsWith("Mb") || value.endsWith("mB") || value.endsWith("MB")) {
        this.multiplier = 1024 * 1024;
        int indexOfM = StringUtils.indexOfIgnoreCase(value, "m");
        value = value.substring(0, indexOfM);
    } else if (value.endsWith("g") || value.endsWith("G") || value.endsWith("gb") || value.endsWith("Gb") || value.endsWith("gB") || value.endsWith("GB")) {
        this.multiplier = 1024 * 1024 * 1024;
        int indexOfG = StringUtils.indexOfIgnoreCase(value, "g");
        value = value.substring(0, indexOfG);
    }

    return super.parseObject(value, exceptionInterceptor);
}
 
Example 2
Source File: ParseInfo.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
protected static boolean canRewrite(String sql, boolean isOnDuplicateKeyUpdate, int locationOfOnDuplicateKeyUpdate, int statementStartPos) {
    // Needs to be INSERT or REPLACE.
    // Can't have INSERT ... SELECT or INSERT ... ON DUPLICATE KEY UPDATE with an id=LAST_INSERT_ID(...).

    if (StringUtils.startsWithIgnoreCaseAndWs(sql, "INSERT", statementStartPos)) {
        if (StringUtils.indexOfIgnoreCase(statementStartPos, sql, "SELECT", "\"'`", "\"'`", StringUtils.SEARCH_MODE__MRK_COM_WS) != -1) {
            return false;
        }
        if (isOnDuplicateKeyUpdate) {
            int updateClausePos = StringUtils.indexOfIgnoreCase(locationOfOnDuplicateKeyUpdate, sql, " UPDATE ");
            if (updateClausePos != -1) {
                return StringUtils.indexOfIgnoreCase(updateClausePos, sql, "LAST_INSERT_ID", "\"'`", "\"'`", StringUtils.SEARCH_MODE__MRK_COM_WS) == -1;
            }
        }
        return true;
    }

    return StringUtils.startsWithIgnoreCaseAndWs(sql, "REPLACE", statementStartPos)
            && StringUtils.indexOfIgnoreCase(statementStartPos, sql, "SELECT", "\"'`", "\"'`", StringUtils.SEARCH_MODE__MRK_COM_WS) == -1;
}
 
Example 3
Source File: ParseInfo.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected static boolean canRewrite(String sql, boolean isOnDuplicateKeyUpdate, int locationOfOnDuplicateKeyUpdate, int statementStartPos) {
    // Needs to be INSERT or REPLACE.
    // Can't have INSERT ... SELECT or INSERT ... ON DUPLICATE KEY UPDATE with an id=LAST_INSERT_ID(...).

    if (StringUtils.startsWithIgnoreCaseAndWs(sql, "INSERT", statementStartPos)) {
        if (StringUtils.indexOfIgnoreCase(statementStartPos, sql, "SELECT", "\"'`", "\"'`", StringUtils.SEARCH_MODE__MRK_COM_WS) != -1) {
            return false;
        }
        if (isOnDuplicateKeyUpdate) {
            int updateClausePos = StringUtils.indexOfIgnoreCase(locationOfOnDuplicateKeyUpdate, sql, " UPDATE ");
            if (updateClausePos != -1) {
                return StringUtils.indexOfIgnoreCase(updateClausePos, sql, "LAST_INSERT_ID", "\"'`", "\"'`", StringUtils.SEARCH_MODE__MRK_COM_WS) == -1;
            }
        }
        return true;
    }

    return StringUtils.startsWithIgnoreCaseAndWs(sql, "REPLACE", statementStartPos)
            && StringUtils.indexOfIgnoreCase(statementStartPos, sql, "SELECT", "\"'`", "\"'`", StringUtils.SEARCH_MODE__MRK_COM_WS) == -1;
}
 
Example 4
Source File: MemorySizePropertyDefinition.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Integer parseObject(String value, ExceptionInterceptor exceptionInterceptor) {
    this.multiplier = 1;

    if (value.endsWith("k") || value.endsWith("K") || value.endsWith("kb") || value.endsWith("Kb") || value.endsWith("kB") || value.endsWith("KB")) {
        this.multiplier = 1024;
        int indexOfK = StringUtils.indexOfIgnoreCase(value, "k");
        value = value.substring(0, indexOfK);
    } else if (value.endsWith("m") || value.endsWith("M") || value.endsWith("mb") || value.endsWith("Mb") || value.endsWith("mB") || value.endsWith("MB")) {
        this.multiplier = 1024 * 1024;
        int indexOfM = StringUtils.indexOfIgnoreCase(value, "m");
        value = value.substring(0, indexOfM);
    } else if (value.endsWith("g") || value.endsWith("G") || value.endsWith("gb") || value.endsWith("Gb") || value.endsWith("gB") || value.endsWith("GB")) {
        this.multiplier = 1024 * 1024 * 1024;
        int indexOfG = StringUtils.indexOfIgnoreCase(value, "g");
        value = value.substring(0, indexOfG);
    }

    return super.parseObject(value, exceptionInterceptor);
}
 
Example 5
Source File: CallableStatement.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private String extractProcedureName() throws SQLException {
    String sanitizedSql = StringUtils.stripComments(((PreparedQuery<?>) this.query).getOriginalSql(), "`\"'", "`\"'", true, false, true, true);

    // TODO: Do this with less memory allocation
    int endCallIndex = StringUtils.indexOfIgnoreCase(sanitizedSql, "CALL ");
    int offset = 5;

    if (endCallIndex == -1) {
        endCallIndex = StringUtils.indexOfIgnoreCase(sanitizedSql, "SELECT ");
        offset = 7;
    }

    if (endCallIndex != -1) {
        StringBuilder nameBuf = new StringBuilder();

        String trimmedStatement = sanitizedSql.substring(endCallIndex + offset).trim();

        int statementLength = trimmedStatement.length();

        for (int i = 0; i < statementLength; i++) {
            char c = trimmedStatement.charAt(i);

            if (Character.isWhitespace(c) || (c == '(') || (c == '?')) {
                break;
            }
            nameBuf.append(c);

        }

        return nameBuf.toString();
    }

    throw SQLError.createSQLException(Messages.getString("CallableStatement.1"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR, getExceptionInterceptor());
}
 
Example 6
Source File: ConnectionImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void setupServerForTruncationChecks() throws SQLException {
    synchronized (getConnectionMutex()) {
        ModifiableProperty<Boolean> jdbcCompliantTruncation = this.propertySet
                .<Boolean> getModifiableProperty(PropertyDefinitions.PNAME_jdbcCompliantTruncation);
        if (jdbcCompliantTruncation.getValue()) {
            String currentSqlMode = this.session.getServerSession().getServerVariable("sql_mode");

            boolean strictTransTablesIsSet = StringUtils.indexOfIgnoreCase(currentSqlMode, "STRICT_TRANS_TABLES") != -1;

            if (currentSqlMode == null || currentSqlMode.length() == 0 || !strictTransTablesIsSet) {
                StringBuilder commandBuf = new StringBuilder("SET sql_mode='");

                if (currentSqlMode != null && currentSqlMode.length() > 0) {
                    commandBuf.append(currentSqlMode);
                    commandBuf.append(",");
                }

                commandBuf.append("STRICT_TRANS_TABLES'");

                this.session.execSQL(null, commandBuf.toString(), -1, null, false, this.nullStatementResultSetFactory, this.database, null, false);

                jdbcCompliantTruncation.setValue(false); // server's handling this for us now
            } else if (strictTransTablesIsSet) {
                // We didn't set it, but someone did, so we piggy back on it
                jdbcCompliantTruncation.setValue(false); // server's handling this for us now
            }
        }
    }
}
 
Example 7
Source File: ConnectionImpl.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
private void setupServerForTruncationChecks() throws SQLException {
    synchronized (getConnectionMutex()) {
        RuntimeProperty<Boolean> jdbcCompliantTruncation = this.propertySet.getProperty(PropertyKey.jdbcCompliantTruncation);
        if (jdbcCompliantTruncation.getValue()) {
            String currentSqlMode = this.session.getServerSession().getServerVariable("sql_mode");

            boolean strictTransTablesIsSet = StringUtils.indexOfIgnoreCase(currentSqlMode, "STRICT_TRANS_TABLES") != -1;

            if (currentSqlMode == null || currentSqlMode.length() == 0 || !strictTransTablesIsSet) {
                StringBuilder commandBuf = new StringBuilder("SET sql_mode='");

                if (currentSqlMode != null && currentSqlMode.length() > 0) {
                    commandBuf.append(currentSqlMode);
                    commandBuf.append(",");
                }

                commandBuf.append("STRICT_TRANS_TABLES'");

                this.session.execSQL(null, commandBuf.toString(), -1, null, false, this.nullStatementResultSetFactory, this.database, null, false);

                jdbcCompliantTruncation.setValue(false); // server's handling this for us now
            } else if (strictTransTablesIsSet) {
                // We didn't set it, but someone did, so we piggy back on it
                jdbcCompliantTruncation.setValue(false); // server's handling this for us now
            }
        }
    }
}
 
Example 8
Source File: CallableStatement.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
private String extractProcedureName() throws SQLException {
    String sanitizedSql = StringUtils.stripComments(((PreparedQuery<?>) this.query).getOriginalSql(), "`\"'", "`\"'", true, false, true, true);

    // TODO: Do this with less memory allocation
    int endCallIndex = StringUtils.indexOfIgnoreCase(sanitizedSql, "CALL ");
    int offset = 5;

    if (endCallIndex == -1) {
        endCallIndex = StringUtils.indexOfIgnoreCase(sanitizedSql, "SELECT ");
        offset = 7;
    }

    if (endCallIndex != -1) {
        StringBuilder nameBuf = new StringBuilder();

        String trimmedStatement = sanitizedSql.substring(endCallIndex + offset).trim();

        int statementLength = trimmedStatement.length();

        for (int i = 0; i < statementLength; i++) {
            char c = trimmedStatement.charAt(i);

            if (Character.isWhitespace(c) || (c == '(') || (c == '?')) {
                break;
            }
            nameBuf.append(c);

        }

        return nameBuf.toString();
    }

    throw SQLError.createSQLException(Messages.getString("CallableStatement.1"), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR, getExceptionInterceptor());
}
 
Example 9
Source File: CallableStatement.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
private void generateParameterMap() throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        if (this.paramInfo == null) {
            return;
        }

        // if the user specified some parameters as literals, we need to provide a map from the specified placeholders to the actual parameter numbers

        int parameterCountFromMetaData = this.paramInfo.getParameterCount();

        // Ignore the first ? if this is a stored function, it doesn't count

        if (this.callingStoredFunction) {
            parameterCountFromMetaData--;
        }

        PreparedQuery<?> q = ((PreparedQuery<?>) this.query);
        if (this.paramInfo != null && q.getParameterCount() != parameterCountFromMetaData) {
            this.placeholderToParameterIndexMap = new int[q.getParameterCount()];

            int startPos = this.callingStoredFunction ? StringUtils.indexOfIgnoreCase(q.getOriginalSql(), "SELECT")
                    : StringUtils.indexOfIgnoreCase(q.getOriginalSql(), "CALL");

            if (startPos != -1) {
                int parenOpenPos = q.getOriginalSql().indexOf('(', startPos + 4);

                if (parenOpenPos != -1) {
                    int parenClosePos = StringUtils.indexOfIgnoreCase(parenOpenPos, q.getOriginalSql(), ")", "'", "'", StringUtils.SEARCH_MODE__ALL);

                    if (parenClosePos != -1) {
                        List<?> parsedParameters = StringUtils.split(q.getOriginalSql().substring(parenOpenPos + 1, parenClosePos), ",", "'\"", "'\"",
                                true);

                        int numParsedParameters = parsedParameters.size();

                        // sanity check

                        if (numParsedParameters != q.getParameterCount()) {
                            // bail?
                        }

                        int placeholderCount = 0;

                        for (int i = 0; i < numParsedParameters; i++) {
                            if (((String) parsedParameters.get(i)).equals("?")) {
                                this.placeholderToParameterIndexMap[placeholderCount++] = i;
                            }
                        }
                    }
                }
            }
        }
    }
}
 
Example 10
Source File: ParseInfo.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
public static int getOnDuplicateKeyLocation(String sql, boolean dontCheckOnDuplicateKeyUpdateInSQL, boolean rewriteBatchedStatements,
        boolean noBackslashEscapes) {
    return dontCheckOnDuplicateKeyUpdateInSQL && !rewriteBatchedStatements ? -1 : StringUtils.indexOfIgnoreCase(0, sql, ON_DUPLICATE_KEY_UPDATE_CLAUSE,
            "\"'`", "\"'`", noBackslashEscapes ? StringUtils.SEARCH_MODE__MRK_COM_WS : StringUtils.SEARCH_MODE__ALL);
}
 
Example 11
Source File: ParseInfo.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
private String extractValuesClause(String sql, String quoteCharStr) {
    int indexOfValues = -1;
    int valuesSearchStart = this.statementStartPos;

    while (indexOfValues == -1) {
        if (quoteCharStr.length() > 0) {
            indexOfValues = StringUtils.indexOfIgnoreCase(valuesSearchStart, sql, "VALUES", quoteCharStr, quoteCharStr,
                    StringUtils.SEARCH_MODE__MRK_COM_WS);
        } else {
            indexOfValues = StringUtils.indexOfIgnoreCase(valuesSearchStart, sql, "VALUES");
        }

        if (indexOfValues > 0) {
            /* check if the char immediately preceding VALUES may be part of the table name */
            char c = sql.charAt(indexOfValues - 1);
            if (!(Character.isWhitespace(c) || c == ')' || c == '`')) {
                valuesSearchStart = indexOfValues + 6;
                indexOfValues = -1;
            } else {
                /* check if the char immediately following VALUES may be whitespace or open parenthesis */
                c = sql.charAt(indexOfValues + 6);
                if (!(Character.isWhitespace(c) || c == '(')) {
                    valuesSearchStart = indexOfValues + 6;
                    indexOfValues = -1;
                }
            }
        } else {
            break;
        }
    }

    if (indexOfValues == -1) {
        return null;
    }

    int indexOfFirstParen = sql.indexOf('(', indexOfValues + 6);

    if (indexOfFirstParen == -1) {
        return null;
    }

    int endOfValuesClause = this.isOnDuplicateKeyUpdate ? this.locationOfOnDuplicateKeyUpdate : sql.length();

    return sql.substring(indexOfFirstParen, endOfValuesClause);
}
 
Example 12
Source File: ParseInfo.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public static int getOnDuplicateKeyLocation(String sql, boolean dontCheckOnDuplicateKeyUpdateInSQL, boolean rewriteBatchedStatements,
        boolean noBackslashEscapes) {
    return dontCheckOnDuplicateKeyUpdateInSQL && !rewriteBatchedStatements ? -1 : StringUtils.indexOfIgnoreCase(0, sql, ON_DUPLICATE_KEY_UPDATE_CLAUSE,
            "\"'`", "\"'`", noBackslashEscapes ? StringUtils.SEARCH_MODE__MRK_COM_WS : StringUtils.SEARCH_MODE__ALL);
}
 
Example 13
Source File: ParseInfo.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private String extractValuesClause(String sql, String quoteCharStr) {
    int indexOfValues = -1;
    int valuesSearchStart = this.statementStartPos;

    while (indexOfValues == -1) {
        if (quoteCharStr.length() > 0) {
            indexOfValues = StringUtils.indexOfIgnoreCase(valuesSearchStart, sql, "VALUES", quoteCharStr, quoteCharStr,
                    StringUtils.SEARCH_MODE__MRK_COM_WS);
        } else {
            indexOfValues = StringUtils.indexOfIgnoreCase(valuesSearchStart, sql, "VALUES");
        }

        if (indexOfValues > 0) {
            /* check if the char immediately preceding VALUES may be part of the table name */
            char c = sql.charAt(indexOfValues - 1);
            if (!(Character.isWhitespace(c) || c == ')' || c == '`')) {
                valuesSearchStart = indexOfValues + 6;
                indexOfValues = -1;
            } else {
                /* check if the char immediately following VALUES may be whitespace or open parenthesis */
                c = sql.charAt(indexOfValues + 6);
                if (!(Character.isWhitespace(c) || c == '(')) {
                    valuesSearchStart = indexOfValues + 6;
                    indexOfValues = -1;
                }
            }
        } else {
            break;
        }
    }

    if (indexOfValues == -1) {
        return null;
    }

    int indexOfFirstParen = sql.indexOf('(', indexOfValues + 6);

    if (indexOfFirstParen == -1) {
        return null;
    }

    int endOfValuesClause = sql.lastIndexOf(')');

    if (endOfValuesClause == -1) {
        return null;
    }

    if (this.isOnDuplicateKeyUpdate) {
        endOfValuesClause = this.locationOfOnDuplicateKeyUpdate - 1;
    }

    return sql.substring(indexOfFirstParen, endOfValuesClause + 1);
}
 
Example 14
Source File: CallableStatement.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void generateParameterMap() throws SQLException {
    synchronized (checkClosed().getConnectionMutex()) {
        if (this.paramInfo == null) {
            return;
        }

        // if the user specified some parameters as literals, we need to provide a map from the specified placeholders to the actual parameter numbers

        int parameterCountFromMetaData = this.paramInfo.getParameterCount();

        // Ignore the first ? if this is a stored function, it doesn't count

        if (this.callingStoredFunction) {
            parameterCountFromMetaData--;
        }

        PreparedQuery<?> q = ((PreparedQuery<?>) this.query);
        if (this.paramInfo != null && q.getParameterCount() != parameterCountFromMetaData) {
            this.placeholderToParameterIndexMap = new int[q.getParameterCount()];

            int startPos = this.callingStoredFunction ? StringUtils.indexOfIgnoreCase(q.getOriginalSql(), "SELECT")
                    : StringUtils.indexOfIgnoreCase(q.getOriginalSql(), "CALL");

            if (startPos != -1) {
                int parenOpenPos = q.getOriginalSql().indexOf('(', startPos + 4);

                if (parenOpenPos != -1) {
                    int parenClosePos = StringUtils.indexOfIgnoreCase(parenOpenPos, q.getOriginalSql(), ")", "'", "'", StringUtils.SEARCH_MODE__ALL);

                    if (parenClosePos != -1) {
                        List<?> parsedParameters = StringUtils.split(q.getOriginalSql().substring(parenOpenPos + 1, parenClosePos), ",", "'\"", "'\"",
                                true);

                        int numParsedParameters = parsedParameters.size();

                        // sanity check

                        if (numParsedParameters != q.getParameterCount()) {
                            // bail?
                        }

                        int placeholderCount = 0;

                        for (int i = 0; i < numParsedParameters; i++) {
                            if (((String) parsedParameters.get(i)).equals("?")) {
                                this.placeholderToParameterIndexMap[placeholderCount++] = i;
                            }
                        }
                    }
                }
            }
        }
    }
}
 
Example 15
Source File: ConnectionUrlParser.java    From lams with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Parses the host information using the alternate syntax "address=(key1=value1)(key2=value2)...".
 * 
 * @param user
 *            the user to include in the resulting {@link HostInfo}
 * @param password
 *            the password to include in the resulting {@link HostInfo}
 * @param hostInfo
 *            the string containing the host information part
 * @return the {@link HostInfo} instance containing the parsed information or <code>null</code> if unable to parse the host information
 */
private HostInfo buildHostInfoResortingToAddressEqualsSyntaxParser(String user, String password, String hostInfo) {
    int p = StringUtils.indexOfIgnoreCase(hostInfo, ADDRESS_EQUALS_HOST_INFO_PREFIX);
    if (p != 0) {
        // This pattern won't work.
        return null;
    }
    hostInfo = hostInfo.substring(p + ADDRESS_EQUALS_HOST_INFO_PREFIX.length()).trim();
    return new HostInfo(this, null, -1, user, password, processKeyValuePattern(ADDRESS_EQUALS_HOST_PTRN, hostInfo));
}
 
Example 16
Source File: ConnectionUrlParser.java    From FoxTelem with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Parses the host information using the alternate syntax "address=(key1=value1)(key2=value2)...".
 * 
 * @param user
 *            the user to include in the resulting {@link HostInfo}
 * @param password
 *            the password to include in the resulting {@link HostInfo}
 * @param hostInfo
 *            the string containing the host information part
 * @return the {@link HostInfo} instance containing the parsed information or <code>null</code> if unable to parse the host information
 */
private HostInfo buildHostInfoResortingToAddressEqualsSyntaxParser(String user, String password, String hostInfo) {
    int p = StringUtils.indexOfIgnoreCase(hostInfo, ADDRESS_EQUALS_HOST_INFO_PREFIX);
    if (p != 0) {
        // This pattern won't work.
        return null;
    }
    hostInfo = hostInfo.substring(p + ADDRESS_EQUALS_HOST_INFO_PREFIX.length()).trim();
    return new HostInfo(this, null, -1, user, password, processKeyValuePattern(ADDRESS_EQUALS_HOST_PTRN, hostInfo));
}
 
Example 17
Source File: ServerController.java    From lams with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Is this ServerController running on a Windows operating system?
 * 
 * @return boolean if this ServerController is running on Windows
 */
private boolean runningOnWindows() {
    return StringUtils.indexOfIgnoreCase(Constants.OS_NAME, "WINDOWS") != -1;
}
 
Example 18
Source File: ServerController.java    From FoxTelem with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Is this ServerController running on a Windows operating system?
 * 
 * @return boolean if this ServerController is running on Windows
 */
private boolean runningOnWindows() {
    return StringUtils.indexOfIgnoreCase(Constants.OS_NAME, "WINDOWS") != -1;
}