Java Code Examples for com.mysql.cj.interceptors.QueryInterceptor

The following examples show how to use com.mysql.cj.interceptors.QueryInterceptor. These examples are extracted from open source projects. 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 Project: lams   Source File: NativeProtocol.java    License: GNU General Public License v2.0 6 votes vote down vote up
public <T extends Resultset> T invokeQueryInterceptorsPre(Supplier<String> sql, Query interceptedQuery, boolean forceExecute) {
    T previousResultSet = null;

    for (int i = 0, s = this.queryInterceptors.size(); i < s; i++) {
        QueryInterceptor interceptor = this.queryInterceptors.get(i);

        boolean executeTopLevelOnly = interceptor.executeTopLevelOnly();
        boolean shouldExecute = (executeTopLevelOnly && (this.statementExecutionDepth == 1 || forceExecute)) || (!executeTopLevelOnly);

        if (shouldExecute) {
            T interceptedResultSet = interceptor.preProcess(sql, interceptedQuery);

            if (interceptedResultSet != null) {
                previousResultSet = interceptedResultSet;
            }
        }
    }

    return previousResultSet;
}
 
Example 2
Source Project: lams   Source File: NativeProtocol.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * 
 * @param queryPacket
 * @param forceExecute
 * @return
 */
public <M extends Message> M invokeQueryInterceptorsPre(M queryPacket, boolean forceExecute) {
    M previousPacketPayload = null;

    for (int i = 0, s = this.queryInterceptors.size(); i < s; i++) {
        QueryInterceptor interceptor = this.queryInterceptors.get(i);

        // TODO how to handle executeTopLevelOnly in such case ?
        //            boolean executeTopLevelOnly = interceptor.executeTopLevelOnly();
        //            boolean shouldExecute = (executeTopLevelOnly && (this.statementExecutionDepth == 1 || forceExecute)) || (!executeTopLevelOnly);
        //            if (shouldExecute) {

        M interceptedPacketPayload = interceptor.preProcess(queryPacket);
        if (interceptedPacketPayload != null) {
            previousPacketPayload = interceptedPacketPayload;
        }
        //            }
    }

    return previousPacketPayload;
}
 
Example 3
Source Project: lams   Source File: NativeProtocol.java    License: GNU General Public License v2.0 6 votes vote down vote up
public <T extends Resultset> T invokeQueryInterceptorsPost(Supplier<String> sql, Query interceptedQuery, T originalResultSet, boolean forceExecute) {

        for (int i = 0, s = this.queryInterceptors.size(); i < s; i++) {
            QueryInterceptor interceptor = this.queryInterceptors.get(i);

            boolean executeTopLevelOnly = interceptor.executeTopLevelOnly();
            boolean shouldExecute = (executeTopLevelOnly && (this.statementExecutionDepth == 1 || forceExecute)) || (!executeTopLevelOnly);

            if (shouldExecute) {
                T interceptedResultSet = interceptor.postProcess(sql, interceptedQuery, originalResultSet, this.serverSession);

                if (interceptedResultSet != null) {
                    originalResultSet = interceptedResultSet;
                }
            }
        }

        return originalResultSet;
    }
 
Example 4
Source Project: lams   Source File: NativeProtocol.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param forceExecute
 */
public <M extends Message> M invokeQueryInterceptorsPost(M queryPacket, M originalResponsePacket, boolean forceExecute) {

    for (int i = 0, s = this.queryInterceptors.size(); i < s; i++) {
        QueryInterceptor interceptor = this.queryInterceptors.get(i);

        // TODO how to handle executeTopLevelOnly in such case ?
        //            boolean executeTopLevelOnly = interceptor.executeTopLevelOnly();
        //            boolean shouldExecute = (executeTopLevelOnly && (this.statementExecutionDepth == 1 || forceExecute)) || (!executeTopLevelOnly);
        //            if (shouldExecute) {

        M interceptedPacketPayload = interceptor.postProcess(queryPacket, originalResponsePacket);
        if (interceptedPacketPayload != null) {
            originalResponsePacket = interceptedPacketPayload;
        }
        //            }
    }

    return originalResponsePacket;
}
 
Example 5
/**
 * Creates a new physical connection for the given {@link HostInfo} and updates required internal mappings and statistics for that connection.
 * 
 * @param hostInfo
 *            The host info instance.
 * @return
 *         The new Connection instance.
 */
@Override
public synchronized ConnectionImpl createConnectionForHost(HostInfo hostInfo) throws SQLException {
    ConnectionImpl conn = super.createConnectionForHost(hostInfo);

    this.liveConnections.put(hostInfo.getHostPortPair(), conn);
    this.connectionsToHostsMap.put(conn, hostInfo.getHostPortPair());

    this.totalPhysicalConnections++;

    for (QueryInterceptor stmtInterceptor : conn.getQueryInterceptorsInstances()) {
        if (stmtInterceptor instanceof LoadBalancedAutoCommitInterceptor) {
            ((LoadBalancedAutoCommitInterceptor) stmtInterceptor).resumeCounters();
            break;
        }
    }

    return conn;
}
 
Example 6
public QueryInterceptor init(MysqlConnection connection, Properties props, Log log) {
    this.conn = (JdbcConnection) connection;

    String autoCommitSwapThresholdAsString = props.getProperty(PropertyDefinitions.PNAME_loadBalanceAutoCommitStatementThreshold, "0");
    try {
        this.matchingAfterStatementThreshold = Integer.parseInt(autoCommitSwapThresholdAsString);
    } catch (NumberFormatException nfe) {
        // nothing here, being handled in LoadBalancedConnectionProxy.
    }
    String autoCommitSwapRegex = props.getProperty(PropertyDefinitions.PNAME_loadBalanceAutoCommitStatementRegex, "");
    if (!"".equals(autoCommitSwapRegex)) {
        this.matchingAfterStatementRegex = autoCommitSwapRegex;
    }
    return this;

}
 
Example 7
/**
 * Creates a new physical connection for the given {@link HostInfo} and updates required internal mappings and statistics for that connection.
 * 
 * @param hostInfo
 *            The host info instance.
 * @return
 *         The new Connection instance.
 * @throws SQLException
 *             if an error occurs
 */
@Override
public synchronized ConnectionImpl createConnectionForHost(HostInfo hostInfo) throws SQLException {
    ConnectionImpl conn = super.createConnectionForHost(hostInfo);

    this.liveConnections.put(hostInfo.getHostPortPair(), conn);
    this.connectionsToHostsMap.put(conn, hostInfo.getHostPortPair());

    this.totalPhysicalConnections++;

    for (QueryInterceptor stmtInterceptor : conn.getQueryInterceptorsInstances()) {
        if (stmtInterceptor instanceof LoadBalancedAutoCommitInterceptor) {
            ((LoadBalancedAutoCommitInterceptor) stmtInterceptor).resumeCounters();
            break;
        }
    }

    return conn;
}
 
Example 8
@Override
public QueryInterceptor init(MysqlConnection connection, Properties props, Log log) {
    this.conn = (JdbcConnection) connection;

    String autoCommitSwapThresholdAsString = props.getProperty(PropertyKey.loadBalanceAutoCommitStatementThreshold.getKeyName(), "0");
    try {
        this.matchingAfterStatementThreshold = Integer.parseInt(autoCommitSwapThresholdAsString);
    } catch (NumberFormatException nfe) {
        // nothing here, being handled in LoadBalancedConnectionProxy.
    }
    String autoCommitSwapRegex = props.getProperty(PropertyKey.loadBalanceAutoCommitStatementRegex.getKeyName(), "");
    if (!"".equals(autoCommitSwapRegex)) {
        this.matchingAfterStatementRegex = autoCommitSwapRegex;
    }
    return this;

}
 
Example 9
@Override
public QueryInterceptor init(MysqlConnection conn, Properties props, Log log) {
    String regexFromUser = props.getProperty(PNAME_resultSetScannerRegex);

    if (regexFromUser == null || regexFromUser.length() == 0) {
        throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("ResultSetScannerInterceptor.0"));
    }

    try {
        this.regexP = Pattern.compile(regexFromUser);
    } catch (Throwable t) {
        throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("ResultSetScannerInterceptor.1"), t);
    }
    return this;

}
 
Example 10
Source Project: FoxTelem   Source File: NativeProtocol.java    License: GNU General Public License v3.0 6 votes vote down vote up
public <T extends Resultset> T invokeQueryInterceptorsPre(Supplier<String> sql, Query interceptedQuery, boolean forceExecute) {
    T previousResultSet = null;

    for (int i = 0, s = this.queryInterceptors.size(); i < s; i++) {
        QueryInterceptor interceptor = this.queryInterceptors.get(i);

        boolean executeTopLevelOnly = interceptor.executeTopLevelOnly();
        boolean shouldExecute = (executeTopLevelOnly && (this.statementExecutionDepth == 1 || forceExecute)) || (!executeTopLevelOnly);

        if (shouldExecute) {
            T interceptedResultSet = interceptor.preProcess(sql, interceptedQuery);

            if (interceptedResultSet != null) {
                previousResultSet = interceptedResultSet;
            }
        }
    }

    return previousResultSet;
}
 
Example 11
Source Project: FoxTelem   Source File: NativeProtocol.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * 
 * @param <M>
 *            extends {@link Message}
 * @param queryPacket
 *            {@link NativePacketPayload} containing query
 * @param forceExecute
 *            currently ignored
 * @return M instance
 */
public <M extends Message> M invokeQueryInterceptorsPre(M queryPacket, boolean forceExecute) {
    M previousPacketPayload = null;

    for (int i = 0, s = this.queryInterceptors.size(); i < s; i++) {
        QueryInterceptor interceptor = this.queryInterceptors.get(i);

        // TODO how to handle executeTopLevelOnly in such case ?
        //            boolean executeTopLevelOnly = interceptor.executeTopLevelOnly();
        //            boolean shouldExecute = (executeTopLevelOnly && (this.statementExecutionDepth == 1 || forceExecute)) || (!executeTopLevelOnly);
        //            if (shouldExecute) {

        M interceptedPacketPayload = interceptor.preProcess(queryPacket);
        if (interceptedPacketPayload != null) {
            previousPacketPayload = interceptedPacketPayload;
        }
        //            }
    }

    return previousPacketPayload;
}
 
Example 12
Source Project: FoxTelem   Source File: NativeProtocol.java    License: GNU General Public License v3.0 6 votes vote down vote up
public <T extends Resultset> T invokeQueryInterceptorsPost(Supplier<String> sql, Query interceptedQuery, T originalResultSet, boolean forceExecute) {

        for (int i = 0, s = this.queryInterceptors.size(); i < s; i++) {
            QueryInterceptor interceptor = this.queryInterceptors.get(i);

            boolean executeTopLevelOnly = interceptor.executeTopLevelOnly();
            boolean shouldExecute = (executeTopLevelOnly && (this.statementExecutionDepth == 1 || forceExecute)) || (!executeTopLevelOnly);

            if (shouldExecute) {
                T interceptedResultSet = interceptor.postProcess(sql, interceptedQuery, originalResultSet, this.serverSession);

                if (interceptedResultSet != null) {
                    originalResultSet = interceptedResultSet;
                }
            }
        }

        return originalResultSet;
    }
 
Example 13
Source Project: FoxTelem   Source File: NativeProtocol.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * 
 * @param <M>
 *            extends {@link Message}
 * @param queryPacket
 *            {@link NativePacketPayload} containing query
 * @param originalResponsePacket
 *            {@link NativePacketPayload} containing response
 * @param forceExecute
 *            currently ignored
 * @return T instance
 */
public <M extends Message> M invokeQueryInterceptorsPost(M queryPacket, M originalResponsePacket, boolean forceExecute) {

    for (int i = 0, s = this.queryInterceptors.size(); i < s; i++) {
        QueryInterceptor interceptor = this.queryInterceptors.get(i);

        // TODO how to handle executeTopLevelOnly in such case ?
        //            boolean executeTopLevelOnly = interceptor.executeTopLevelOnly();
        //            boolean shouldExecute = (executeTopLevelOnly && (this.statementExecutionDepth == 1 || forceExecute)) || (!executeTopLevelOnly);
        //            if (shouldExecute) {

        M interceptedPacketPayload = interceptor.postProcess(queryPacket, originalResponsePacket);
        if (interceptedPacketPayload != null) {
            originalResponsePacket = interceptedPacketPayload;
        }
        //            }
    }

    return originalResponsePacket;
}
 
Example 14
Source Project: lams   Source File: NoSubInterceptorWrapper.java    License: GNU General Public License v2.0 5 votes vote down vote up
public NoSubInterceptorWrapper(QueryInterceptor underlyingInterceptor) {
    if (underlyingInterceptor == null) {
        throw new RuntimeException(Messages.getString("NoSubInterceptorWrapper.0"));
    }

    this.underlyingInterceptor = underlyingInterceptor;
}
 
Example 15
@Override
void syncSessionState(JdbcConnection source, JdbcConnection target, boolean readOnly) throws SQLException {
    LoadBalancedAutoCommitInterceptor lbAutoCommitStmtInterceptor = null;
    for (QueryInterceptor stmtInterceptor : target.getQueryInterceptorsInstances()) {
        if (stmtInterceptor instanceof LoadBalancedAutoCommitInterceptor) {
            lbAutoCommitStmtInterceptor = (LoadBalancedAutoCommitInterceptor) stmtInterceptor;
            lbAutoCommitStmtInterceptor.pauseCounters();
            break;
        }
    }
    super.syncSessionState(source, target, readOnly);
    if (lbAutoCommitStmtInterceptor != null) {
        lbAutoCommitStmtInterceptor.resumeCounters();
    }
}
 
Example 16
public QueryInterceptor init(MysqlConnection conn, Properties props, Log log) {
    String regexFromUser = props.getProperty(PropertyDefinitions.PNAME_resultSetScannerRegex);

    if (regexFromUser == null || regexFromUser.length() == 0) {
        throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("ResultSetScannerInterceptor.0"));
    }

    try {
        this.regexP = Pattern.compile(regexFromUser);
    } catch (Throwable t) {
        throw ExceptionFactory.createException(WrongArgumentException.class, Messages.getString("ResultSetScannerInterceptor.1"), t);
    }
    return this;

}
 
Example 17
@Override
void syncSessionState(JdbcConnection source, JdbcConnection target, boolean readOnly) throws SQLException {
    LoadBalancedAutoCommitInterceptor lbAutoCommitStmtInterceptor = null;
    for (QueryInterceptor stmtInterceptor : target.getQueryInterceptorsInstances()) {
        if (stmtInterceptor instanceof LoadBalancedAutoCommitInterceptor) {
            lbAutoCommitStmtInterceptor = (LoadBalancedAutoCommitInterceptor) stmtInterceptor;
            lbAutoCommitStmtInterceptor.pauseCounters();
            break;
        }
    }
    super.syncSessionState(source, target, readOnly);
    if (lbAutoCommitStmtInterceptor != null) {
        lbAutoCommitStmtInterceptor.resumeCounters();
    }
}
 
Example 18
Source Project: FoxTelem   Source File: ConnectionImpl.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
public void initializeSafeQueryInterceptors() throws SQLException {
    this.queryInterceptors = Util
            .<QueryInterceptor> loadClasses(this.propertySet.getStringProperty(PropertyKey.queryInterceptors).getStringValue(),
                    "MysqlIo.BadQueryInterceptor", getExceptionInterceptor())
            .stream().map(o -> new NoSubInterceptorWrapper(o.init(this, this.props, this.session.getLog()))).collect(Collectors.toList());
}
 
Example 19
public NoSubInterceptorWrapper(QueryInterceptor underlyingInterceptor) {
    if (underlyingInterceptor == null) {
        throw new RuntimeException(Messages.getString("NoSubInterceptorWrapper.0"));
    }

    this.underlyingInterceptor = underlyingInterceptor;
}
 
Example 20
Source Project: brave   Source File: TracingQueryInterceptor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public QueryInterceptor init(MysqlConnection mysqlConnection, Properties properties,
  Log log) {
  String exceptionInterceptors = properties.getProperty("exceptionInterceptors");
  TracingQueryInterceptor interceptor = new TracingQueryInterceptor();
  interceptor.connection = mysqlConnection;
  interceptor.interceptingExceptions = exceptionInterceptors != null &&
    exceptionInterceptors.contains(TracingExceptionInterceptor.class.getName());
  if (!interceptor.interceptingExceptions) {
    log.logWarn("TracingExceptionInterceptor not enabled. It is highly recommended to "
      + "enable it for error logging to Zipkin.");
  }
  return interceptor;
}
 
Example 21
Source Project: lams   Source File: NativeProtocol.java    License: GNU General Public License v2.0 4 votes vote down vote up
public void setQueryInterceptors(List<QueryInterceptor> queryInterceptors) {
    this.queryInterceptors = queryInterceptors.isEmpty() ? null : queryInterceptors;
}
 
Example 22
Source Project: lams   Source File: NativeProtocol.java    License: GNU General Public License v2.0 4 votes vote down vote up
public List<QueryInterceptor> getQueryInterceptors() {
    return this.queryInterceptors;
}
 
Example 23
Source Project: lams   Source File: NoSubInterceptorWrapper.java    License: GNU General Public License v2.0 4 votes vote down vote up
public QueryInterceptor init(MysqlConnection conn, Properties props, Log log) {
    this.underlyingInterceptor.init(conn, props, log);
    return this;
}
 
Example 24
Source Project: lams   Source File: NoSubInterceptorWrapper.java    License: GNU General Public License v2.0 4 votes vote down vote up
public QueryInterceptor getUnderlyingInterceptor() {
    return this.underlyingInterceptor;
}
 
Example 25
public List<QueryInterceptor> getQueryInterceptorsInstances() {
    return getActiveMySQLConnection().getQueryInterceptorsInstances();
}
 
Example 26
Source Project: lams   Source File: ConnectionImpl.java    License: GNU General Public License v2.0 4 votes vote down vote up
public void initializeSafeQueryInterceptors() throws SQLException {
    this.queryInterceptors = Util
            .<QueryInterceptor> loadClasses(this.propertySet.getStringReadableProperty(PropertyDefinitions.PNAME_queryInterceptors).getStringValue(),
                    "MysqlIo.BadQueryInterceptor", getExceptionInterceptor())
            .stream().map(o -> new NoSubInterceptorWrapper(o.init(this, this.props, this.session.getLog()))).collect(Collectors.toList());
}
 
Example 27
Source Project: lams   Source File: ConnectionImpl.java    License: GNU General Public License v2.0 4 votes vote down vote up
public List<QueryInterceptor> getQueryInterceptorsInstances() {
    return this.queryInterceptors;
}
 
Example 28
@Override
public QueryInterceptor init(MysqlConnection conn, Properties props, Log log) {
    this.connection = (JdbcConnection) conn;
    return this;
}
 
Example 29
public QueryInterceptor init(MysqlConnection conn, Properties props, Log l) {
    this.connection = (JdbcConnection) conn;
    this.log = l;
    return this;
}
 
Example 30
Source Project: lams   Source File: ConnectionWrapper.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public List<QueryInterceptor> getQueryInterceptorsInstances() {
    return this.mc.getQueryInterceptorsInstances();
}