[#1177] Add SQL Console module to jOOQ - Statement matcher for server
level filtering of data to log.
This commit is contained in:
parent
ba99d55333
commit
5b11d96233
@ -39,6 +39,7 @@ package org.jooq.debug;
|
||||
import java.sql.CallableStatement;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.jooq.ExecuteContext;
|
||||
@ -147,30 +148,34 @@ public class DebugListener extends DefaultExecuteListener {
|
||||
}
|
||||
}
|
||||
QueryLoggingData queryLoggingData = new QueryLoggingData(sqlQueryType, sql, parameterDescription, startPreparationTime == 0? null: aggregatedPreparationDuration, startBindTime == 0? null: endBindTime - startBindTime, endExecutionTime - startExecutionTime);
|
||||
final List<LoggingListener> loggingListenerList = new ArrayList<LoggingListener>(debuggerList.size());
|
||||
for(Debugger listener: debuggerList) {
|
||||
LoggingListener loggingListener = listener.getLoggingListener();
|
||||
if(loggingListener != null) {
|
||||
loggingListener.logQueries(queryLoggingData);
|
||||
StatementMatcher[] loggingStatementMatchers = listener.getLoggingStatementMatchers();
|
||||
if(loggingStatementMatchers == null) {
|
||||
loggingListenerList.add(loggingListener);
|
||||
loggingListener.logQueries(queryLoggingData);
|
||||
} else for(StatementMatcher statementMatcher: loggingStatementMatchers) {
|
||||
if(statementMatcher.matches(queryLoggingData)) {
|
||||
loggingListenerList.add(loggingListener);
|
||||
loggingListener.logQueries(queryLoggingData);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if(resultSet != null) {
|
||||
if(resultSet != null && !loggingListenerList.isEmpty()) {
|
||||
final int queryLoggingDataID = queryLoggingData.getID();
|
||||
ResultSet newResultSet = new UsageTrackingResultSet(resultSet) {
|
||||
@Override
|
||||
protected void notifyData(long lifeTime, int readRows, int readCount, int writeCount) {
|
||||
List<Debugger> debuggerList = DebuggerRegistry.getDebuggerList();
|
||||
if(debuggerList.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
ResultSetLoggingData resultSetLoggingData = null;
|
||||
for(Debugger debugger: debuggerList) {
|
||||
LoggingListener loggingListener = debugger.getLoggingListener();
|
||||
if(loggingListener != null) {
|
||||
if(resultSetLoggingData == null) {
|
||||
resultSetLoggingData = new ResultSetLoggingData(lifeTime, readRows, readCount, writeCount);
|
||||
}
|
||||
loggingListener.logResultSet(queryLoggingDataID, resultSetLoggingData);
|
||||
for(LoggingListener loggingListener: loggingListenerList) {
|
||||
if(resultSetLoggingData == null) {
|
||||
resultSetLoggingData = new ResultSetLoggingData(lifeTime, readRows, readCount, writeCount);
|
||||
}
|
||||
loggingListener.logResultSet(queryLoggingDataID, resultSetLoggingData);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -47,6 +47,10 @@ public interface Debugger {
|
||||
|
||||
public LoggingListener getLoggingListener();
|
||||
|
||||
public void setLoggingStatementMatchers(StatementMatcher[] loggingStatementMatchers);
|
||||
|
||||
public StatementMatcher[] getLoggingStatementMatchers();
|
||||
|
||||
public boolean isExecutionSupported();
|
||||
|
||||
public StatementExecutor createStatementExecutor();
|
||||
|
||||
@ -59,6 +59,18 @@ public class LocalDebugger implements Debugger {
|
||||
return loggingListener;
|
||||
}
|
||||
|
||||
private StatementMatcher[] loggingStatementMatchers;
|
||||
|
||||
@Override
|
||||
public void setLoggingStatementMatchers(StatementMatcher[] loggingStatementMatchers) {
|
||||
this.loggingStatementMatchers = loggingStatementMatchers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public StatementMatcher[] getLoggingStatementMatchers() {
|
||||
return loggingStatementMatchers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isExecutionSupported() {
|
||||
return databaseDescriptor != null;
|
||||
|
||||
@ -36,36 +36,28 @@
|
||||
*/
|
||||
package org.jooq.debug;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @author Christopher Deckers
|
||||
*/
|
||||
public class QueryLoggingData implements Serializable {
|
||||
@SuppressWarnings("serial")
|
||||
public class QueryLoggingData extends StatementInfo {
|
||||
|
||||
private static volatile int nextID;
|
||||
private static AtomicInteger nextID = new AtomicInteger();
|
||||
|
||||
private int id;
|
||||
private SqlQueryType queryType;
|
||||
private String[] queries;
|
||||
private String parameterDescription;
|
||||
private Long preparationDuration;
|
||||
private Long bindingDuration;
|
||||
private long executionDuration;
|
||||
private String threadName;
|
||||
private long threadID;
|
||||
private StackTraceElement[] callerStackTraceElements;
|
||||
|
||||
public QueryLoggingData(SqlQueryType queryType, String[] queries, String parameterDescription, Long preparationDuration, Long bindingDuration, long executionDuration) {
|
||||
this.id = nextID++;
|
||||
Thread currentThread = Thread.currentThread();
|
||||
this.threadName = currentThread.getName();
|
||||
this.threadID = currentThread.getId();
|
||||
super(queryType, queries, parameterDescription);
|
||||
this.id = nextID.getAndIncrement();
|
||||
this.callerStackTraceElements = new Exception().getStackTrace();
|
||||
this.queryType = queryType;
|
||||
this.queries = queries;
|
||||
this.parameterDescription = parameterDescription;
|
||||
this.preparationDuration = preparationDuration;
|
||||
this.bindingDuration = bindingDuration;
|
||||
this.executionDuration = executionDuration;
|
||||
@ -75,33 +67,10 @@ public class QueryLoggingData implements Serializable {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getThreadName() {
|
||||
return threadName;
|
||||
}
|
||||
|
||||
public long getThreadID() {
|
||||
return threadID;
|
||||
}
|
||||
|
||||
public StackTraceElement[] getCallerStackTraceElements() {
|
||||
return callerStackTraceElements;
|
||||
}
|
||||
|
||||
public SqlQueryType getQueryType() {
|
||||
return queryType;
|
||||
}
|
||||
|
||||
public String[] getQueries() {
|
||||
return queries;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return non null if queries consist of a single prepared statement with parameters.
|
||||
*/
|
||||
public String getParameterDescription() {
|
||||
return parameterDescription;
|
||||
}
|
||||
|
||||
public Long getPreparedStatementPreparationDuration() {
|
||||
return preparationDuration;
|
||||
}
|
||||
|
||||
@ -48,6 +48,7 @@ import org.jooq.debug.LoggingListener;
|
||||
import org.jooq.debug.QueryLoggingData;
|
||||
import org.jooq.debug.ResultSetLoggingData;
|
||||
import org.jooq.debug.StatementExecutor;
|
||||
import org.jooq.debug.StatementMatcher;
|
||||
|
||||
/**
|
||||
* @author Christopher Deckers
|
||||
@ -67,17 +68,25 @@ public class RemoteDebuggerClient implements Debugger {
|
||||
try {
|
||||
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
|
||||
for(Message o; (o=(Message)in.readObject()) != null; ) {
|
||||
if(o instanceof ClientDebugQueriesMessage) {
|
||||
if(loggingListener != null) {
|
||||
if(o instanceof ClientDebugQueriesMessage) {
|
||||
LoggingListener loggingListener_;
|
||||
synchronized (LOCK) {
|
||||
loggingListener_ = loggingListener;
|
||||
}
|
||||
if(loggingListener_ != null) {
|
||||
QueryLoggingData sqlQueryDebuggerData = ((ClientDebugQueriesMessage) o).getSqlQueryDebuggerData();
|
||||
loggingListener.logQueries(sqlQueryDebuggerData);
|
||||
loggingListener_.logQueries(sqlQueryDebuggerData);
|
||||
}
|
||||
} else if(o instanceof ClientDebugResultSetMessage) {
|
||||
if(loggingListener != null) {
|
||||
LoggingListener loggingListener_;
|
||||
synchronized (LOCK) {
|
||||
loggingListener_ = loggingListener;
|
||||
}
|
||||
if(loggingListener_ != null) {
|
||||
ClientDebugResultSetMessage m = (ClientDebugResultSetMessage) o;
|
||||
int sqlQueryDebuggerDataID = m.getSqlQueryDebuggerDataID();
|
||||
ResultSetLoggingData clientDebugResultSetData = m.getSqlQueryDebuggerResultSetData();
|
||||
loggingListener.logResultSet(sqlQueryDebuggerDataID, clientDebugResultSetData);
|
||||
loggingListener_.logResultSet(sqlQueryDebuggerDataID, clientDebugResultSetData);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -94,10 +103,30 @@ public class RemoteDebuggerClient implements Debugger {
|
||||
|
||||
@Override
|
||||
public void setLoggingListener(LoggingListener loggingListener) {
|
||||
this.loggingListener = loggingListener;
|
||||
synchronized (LOCK) {
|
||||
this.loggingListener = loggingListener;
|
||||
try {
|
||||
out.writeObject(new ServerLoggingActivationMessage(loggingListener != null));
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoggingListener getLoggingListener() {
|
||||
return loggingListener;
|
||||
}
|
||||
|
||||
private StatementMatcher[] loggingStatementMatchers;
|
||||
|
||||
@Override
|
||||
public void setLoggingStatementMatchers(StatementMatcher[] loggingStatementMatchers) {
|
||||
this.loggingStatementMatchers = loggingStatementMatchers;
|
||||
try {
|
||||
synchronized (LOCK) {
|
||||
out.writeObject(new ServerLoggingActivationMessage(loggingListener != null));
|
||||
out.writeObject(new ServerLoggingStatementMatchersMessage(loggingStatementMatchers));
|
||||
out.flush();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
@ -106,8 +135,8 @@ public class RemoteDebuggerClient implements Debugger {
|
||||
}
|
||||
|
||||
@Override
|
||||
public LoggingListener getLoggingListener() {
|
||||
return loggingListener;
|
||||
public StatementMatcher[] getLoggingStatementMatchers() {
|
||||
return loggingStatementMatchers;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -87,7 +87,7 @@ public class RemoteDebuggerServer {
|
||||
}
|
||||
|
||||
private void startServerToClientThread(final Socket socket, int port) {
|
||||
Thread clientThread = new Thread("SQL Remote Debugger Server on port " + port) {
|
||||
Thread clientThread = new Thread("SQL Remote Debugger Server-Client on port " + port) {
|
||||
@Override
|
||||
public void run() {
|
||||
Debugger debugger = null;
|
||||
@ -125,6 +125,8 @@ public class RemoteDebuggerServer {
|
||||
} else {
|
||||
debugger.setLoggingListener(null);
|
||||
}
|
||||
} else if(o instanceof ServerLoggingStatementMatchersMessage) {
|
||||
debugger.setLoggingStatementMatchers(((ServerLoggingStatementMatchersMessage) o).getStatementMatchers());
|
||||
}
|
||||
}
|
||||
} catch(Exception e) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user