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

The following examples show how to use com.mysql.cj.util.StringUtils#startsWithIgnoreCase() . 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: LoadBalancedAutoCommitInterceptor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("resource")
public <T extends Resultset> T postProcess(Supplier<String> sql, Query interceptedQuery, T originalResultSet, ServerSession serverSession) {

    try {
        // Don't count SETs neither SHOWs. Those are mostly used internally and must not trigger a connection switch.
        if (!this.countStatements || StringUtils.startsWithIgnoreCase(sql.get(), "SET") || StringUtils.startsWithIgnoreCase(sql.get(), "SHOW")) {
            return originalResultSet;
        }

        // Don't care if auto-commit is not enabled.
        if (!this.conn.getAutoCommit()) {
            this.matchingAfterStatementCount = 0;
            return originalResultSet;
        }

        if (this.proxy == null && this.conn.isProxySet()) {
            JdbcConnection lcl_proxy = this.conn.getMultiHostSafeProxy();
            while (lcl_proxy != null && !(lcl_proxy instanceof LoadBalancedMySQLConnection)) {
                lcl_proxy = lcl_proxy.getMultiHostSafeProxy();
            }
            if (lcl_proxy != null) {
                this.proxy = ((LoadBalancedMySQLConnection) lcl_proxy).getThisAsProxy();
            }
        }

        // Connection is not ready to rebalance yet.
        if (this.proxy == null) {
            return originalResultSet;
        }

        // Increment the match count if no regex specified, or if matches.
        if (this.matchingAfterStatementRegex == null || sql.get().matches(this.matchingAfterStatementRegex)) {
            this.matchingAfterStatementCount++;
        }

        // Trigger rebalance if count exceeds threshold.
        if (this.matchingAfterStatementCount >= this.matchingAfterStatementThreshold) {
            this.matchingAfterStatementCount = 0;
            try {
                this.proxy.pickNewConnection();
            } catch (SQLException e) {
                // eat this exception, the auto-commit statement completed, but we could not rebalance for some reason.  User may get exception when using
                // connection next.
            }
        }
    } catch (SQLException ex) {
        throw ExceptionFactory.createException(ex.getMessage(), ex);
    }
    // always return the original result set.
    return originalResultSet;
}
 
Example 2
Source File: EscapeProcessor.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Re-writes {fn convert (expr, type)} as cast(expr AS type)
 * 
 * @param functionToken
 * @throws SQLException
 */
private static String processConvertToken(String functionToken, ExceptionInterceptor exceptionInterceptor) throws SQLException {
    // The JDBC spec requires these types:
    //
    // BIGINT
    // BINARY
    // BIT
    // CHAR
    // DATE
    // DECIMAL
    // DOUBLE
    // FLOAT
    // INTEGER
    // LONGVARBINARY
    // LONGVARCHAR
    // REAL
    // SMALLINT
    // TIME
    // TIMESTAMP
    // TINYINT
    // VARBINARY
    // VARCHAR

    // MySQL supports these types:
    //
    // BINARY
    // CHAR
    // DATE
    // DATETIME
    // SIGNED (integer)
    // UNSIGNED (integer)
    // TIME

    int firstIndexOfParen = functionToken.indexOf("(");

    if (firstIndexOfParen == -1) {
        throw SQLError.createSQLException(Messages.getString("EscapeProcessor.4", new Object[] { functionToken }), MysqlErrorNumbers.SQL_STATE_SYNTAX_ERROR,
                exceptionInterceptor);
    }

    int indexOfComma = functionToken.lastIndexOf(",");

    if (indexOfComma == -1) {
        throw SQLError.createSQLException(Messages.getString("EscapeProcessor.5", new Object[] { functionToken }), MysqlErrorNumbers.SQL_STATE_SYNTAX_ERROR,
                exceptionInterceptor);
    }

    int indexOfCloseParen = functionToken.indexOf(')', indexOfComma);

    if (indexOfCloseParen == -1) {
        throw SQLError.createSQLException(Messages.getString("EscapeProcessor.6", new Object[] { functionToken }), MysqlErrorNumbers.SQL_STATE_SYNTAX_ERROR,
                exceptionInterceptor);

    }

    String expression = functionToken.substring(firstIndexOfParen + 1, indexOfComma);
    String type = functionToken.substring(indexOfComma + 1, indexOfCloseParen);

    String newType = null;

    String trimmedType = type.trim();

    if (StringUtils.startsWithIgnoreCase(trimmedType, "SQL_")) {
        trimmedType = trimmedType.substring(4, trimmedType.length());
    }

    newType = JDBC_CONVERT_TO_MYSQL_TYPE_MAP.get(trimmedType.toUpperCase(Locale.ENGLISH));

    if (newType == null) {
        throw SQLError.createSQLException(Messages.getString("EscapeProcessor.7", new Object[] { type.trim() }), MysqlErrorNumbers.SQL_STATE_GENERAL_ERROR,
                exceptionInterceptor);
    }

    int replaceIndex = newType.indexOf("?");

    if (replaceIndex != -1) {
        StringBuilder convertRewrite = new StringBuilder(newType.substring(0, replaceIndex));
        convertRewrite.append(expression);
        convertRewrite.append(newType.substring(replaceIndex + 1, newType.length()));

        return convertRewrite.toString();
    }

    StringBuilder castRewrite = new StringBuilder("CAST(");
    castRewrite.append(expression);
    castRewrite.append(" AS ");
    castRewrite.append(newType);
    castRewrite.append(")");

    return castRewrite.toString();

}
 
Example 3
Source File: LoadBalancedAutoCommitInterceptor.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
@Override
@SuppressWarnings("resource")
public <T extends Resultset> T postProcess(Supplier<String> sql, Query interceptedQuery, T originalResultSet, ServerSession serverSession) {

    try {
        // Don't count SETs neither SHOWs. Those are mostly used internally and must not trigger a connection switch.
        if (!this.countStatements || StringUtils.startsWithIgnoreCase(sql.get(), "SET") || StringUtils.startsWithIgnoreCase(sql.get(), "SHOW")) {
            return originalResultSet;
        }

        // Don't care if auto-commit is not enabled.
        if (!this.conn.getAutoCommit()) {
            this.matchingAfterStatementCount = 0;
            return originalResultSet;
        }

        if (this.proxy == null && this.conn.isProxySet()) {
            JdbcConnection lcl_proxy = this.conn.getMultiHostSafeProxy();
            while (lcl_proxy != null && !(lcl_proxy instanceof LoadBalancedMySQLConnection)) {
                lcl_proxy = lcl_proxy.getMultiHostSafeProxy();
            }
            if (lcl_proxy != null) {
                this.proxy = ((LoadBalancedMySQLConnection) lcl_proxy).getThisAsProxy();
            }
        }

        // Connection is not ready to rebalance yet.
        if (this.proxy == null) {
            return originalResultSet;
        }

        // Increment the match count if no regex specified, or if matches.
        if (this.matchingAfterStatementRegex == null || sql.get().matches(this.matchingAfterStatementRegex)) {
            this.matchingAfterStatementCount++;
        }

        // Trigger rebalance if count exceeds threshold.
        if (this.matchingAfterStatementCount >= this.matchingAfterStatementThreshold) {
            this.matchingAfterStatementCount = 0;
            try {
                this.proxy.pickNewConnection();
            } catch (SQLException e) {
                // eat this exception, the auto-commit statement completed, but we could not rebalance for some reason.  User may get exception when using
                // connection next.
            }
        }
    } catch (SQLException ex) {
        throw ExceptionFactory.createException(ex.getMessage(), ex);
    }
    // always return the original result set.
    return originalResultSet;
}