com.mysql.cj.interceptors.QueryInterceptor Java Examples

The following examples show how to use com.mysql.cj.interceptors.QueryInterceptor. 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: NativeProtocol.java    From FoxTelem with 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 #2
Source File: LoadBalancedConnectionProxy.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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 #3
Source File: LoadBalancedAutoCommitInterceptor.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
@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 #4
Source File: LoadBalancedAutoCommitInterceptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
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 #5
Source File: ResultSetScannerInterceptor.java    From FoxTelem with GNU General Public License v3.0 6 votes vote down vote up
@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 #6
Source File: NativeProtocol.java    From FoxTelem with 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 #7
Source File: LoadBalancedConnectionProxy.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 #8
Source File: NativeProtocol.java    From FoxTelem with 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 #9
Source File: NativeProtocol.java    From FoxTelem with 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 #10
Source File: NativeProtocol.java    From lams with 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 #11
Source File: NativeProtocol.java    From lams with 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 #12
Source File: NativeProtocol.java    From lams with 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 #13
Source File: NativeProtocol.java    From lams with 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 #14
Source File: TracingQueryInterceptor.java    From brave with 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 #15
Source File: NoSubInterceptorWrapper.java    From FoxTelem with GNU General Public License v3.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 #16
Source File: ConnectionImpl.java    From FoxTelem with 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 #17
Source File: LoadBalancedConnectionProxy.java    From FoxTelem with GNU General Public License v3.0 5 votes vote down vote up
@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 File: ResultSetScannerInterceptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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 #19
Source File: NoSubInterceptorWrapper.java    From lams with 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 #20
Source File: LoadBalancedConnectionProxy.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 #21
Source File: NativeProtocol.java    From lams with 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 File: MySQLQueryInterceptor.java    From core-ng-project with Apache License 2.0 4 votes vote down vote up
@Override
public QueryInterceptor init(MysqlConnection connection, Properties properties, Log log) {
    return this;
}
 
Example #23
Source File: BaseQueryInterceptor.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
public QueryInterceptor init(MysqlConnection conn, Properties props, Log log) {
    return this;
}
 
Example #24
Source File: NativeSession.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
public void setQueryInterceptors(List<QueryInterceptor> queryInterceptors) {
    ((NativeProtocol) this.protocol).setQueryInterceptors(queryInterceptors);
}
 
Example #25
Source File: NoSubInterceptorWrapper.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
public QueryInterceptor getUnderlyingInterceptor() {
    return this.underlyingInterceptor;
}
 
Example #26
Source File: NoSubInterceptorWrapper.java    From FoxTelem with GNU General Public License v3.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 #27
Source File: NativeProtocol.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public List<QueryInterceptor> getQueryInterceptors() {
    return this.queryInterceptors;
}
 
Example #28
Source File: NativeProtocol.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
public List<QueryInterceptor> getQueryInterceptors() {
    return this.queryInterceptors;
}
 
Example #29
Source File: NativeProtocol.java    From FoxTelem with GNU General Public License v3.0 4 votes vote down vote up
public void setQueryInterceptors(List<QueryInterceptor> queryInterceptors) {
    this.queryInterceptors = queryInterceptors.isEmpty() ? null : queryInterceptors;
}
 
Example #30
Source File: NoSubInterceptorWrapper.java    From lams with 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;
}