[#1472] Add a Settings.executeDebugging property, and move server-side

Console logic to core - Renamed SqlQueryType to QueryType
This commit is contained in:
Lukas Eder 2012-09-08 13:58:33 +02:00
parent d550287d4d
commit 189dc79af9
9 changed files with 100 additions and 73 deletions

View File

@ -135,7 +135,7 @@ public class DebugListener extends DefaultExecuteListener {
String parameterDescription = null;
if(statementInfo == null) {
String[] sql = ctx.batchSQL();
SqlQueryType sqlQueryType = SqlQueryType.detectType(sql[0]);
QueryType queryType = QueryType.detectType(sql[0]);
if(sql.length == 1) {
sql_ = sql[0];
PreparedStatement statement = ctx.statement();
@ -152,7 +152,7 @@ public class DebugListener extends DefaultExecuteListener {
}
sql_ = sb.toString();
}
statementInfo = new StatementInfo(sqlQueryType, sql, parameterDescription);
statementInfo = new StatementInfo(queryType, sql, parameterDescription);
}
if(breakpoint.matches(statementInfo, true)) {
matchingSQL = sql_;
@ -181,8 +181,8 @@ public class DebugListener extends DefaultExecuteListener {
for(Debugger debugger: debuggerList) {
LoggingListener loggingListener = debugger.getLoggingListener();
if(loggingListener != null) {
SqlQueryType sqlQueryType = SqlQueryType.detectType(sql);
QueryLoggingData queryLoggingData = new QueryLoggingData(sqlQueryType, new String[] {sql}, null, null, null, subEndExecutionTime - subStartExecutionTime);
QueryType queryType = QueryType.detectType(sql);
QueryLoggingData queryLoggingData = new QueryLoggingData(queryType, new String[] {sql}, null, null, null, subEndExecutionTime - subStartExecutionTime);
StatementMatcher[] loggingStatementMatchers = debugger.getLoggingStatementMatchers();
if(loggingStatementMatchers == null) {
loggingListener.logQueries(queryLoggingData);
@ -304,7 +304,7 @@ public class DebugListener extends DefaultExecuteListener {
}
if(hasListener) {
String[] sql = ctx.batchSQL();
SqlQueryType sqlQueryType = SqlQueryType.detectType(sql[0]);
QueryType queryType = QueryType.detectType(sql[0]);
String parameterDescription = null;
if(sql.length == 1) {
PreparedStatement statement = ctx.statement();
@ -312,7 +312,7 @@ public class DebugListener extends DefaultExecuteListener {
parameterDescription = ((UsageTrackingPreparedStatement) statement).getParameterDescription();
}
}
QueryLoggingData queryLoggingData = new QueryLoggingData(sqlQueryType, sql, parameterDescription, startPreparationTime == 0? null: aggregatedPreparationDuration, startBindTime == 0? null: endBindTime - startBindTime, endExecutionTime - startExecutionTime);
QueryLoggingData queryLoggingData = new QueryLoggingData(queryType, 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();
@ -369,8 +369,8 @@ public class DebugListener extends DefaultExecuteListener {
for(Debugger listener: debuggerList) {
LoggingListener loggingListener = listener.getLoggingListener();
if(loggingListener != null) {
SqlQueryType sqlQueryType = SqlQueryType.detectType(sql);
QueryLoggingData queryLoggingData = new QueryLoggingData(sqlQueryType, new String[] {sql}, null, null, null, subEndExecutionTime - subStartExecutionTime);
QueryType queryType = QueryType.detectType(sql);
QueryLoggingData queryLoggingData = new QueryLoggingData(queryType, new String[] {sql}, null, null, null, subEndExecutionTime - subStartExecutionTime);
StatementMatcher[] loggingStatementMatchers = listener.getLoggingStatementMatchers();
if(loggingStatementMatchers == null) {
loggingListener.logQueries(queryLoggingData);

View File

@ -82,7 +82,7 @@ public class LocalStatementExecutor implements StatementExecutor {
boolean isAllowed = true;
if(executorContext.isReadOnly()) {
String simplifiedSql = sql.replaceAll("'[^']*'", "");
switch(SqlQueryType.detectType(simplifiedSql)) {
switch(QueryType.detectType(simplifiedSql)) {
case SELECT:
String[] forbiddenWords = new String[] {
"INSERT",

View File

@ -53,7 +53,7 @@ public class QueryLoggingData extends StatementInfo {
private long executionDuration;
private StackTraceElement[] callerStackTraceElements;
public QueryLoggingData(SqlQueryType queryType, String[] queries, String parameterDescription, Long preparationDuration, Long bindingDuration, long executionDuration) {
public QueryLoggingData(QueryType queryType, String[] queries, String parameterDescription, Long preparationDuration, Long bindingDuration, long executionDuration) {
super(queryType, queries, parameterDescription);
this.id = nextID.getAndIncrement();
this.callerStackTraceElements = Thread.currentThread().getStackTrace();

View File

@ -41,82 +41,109 @@ import java.util.Locale;
/**
* @author Christopher Deckers
*/
public enum SqlQueryType {
public enum QueryType {
/**
* <code>SELECT</code> queries
*/
SELECT,
/**
* <code>INSERT</code> queries
*/
INSERT,
/**
* <code>UPDATE</code> queries
*/
UPDATE,
/**
* <code>DELETE</code> queries
*/
DELETE,
/**
* All other queries
*/
OTHER,
;
public static SqlQueryType detectType(String sql) {
public static QueryType detectType(String sql) {
String queryLC = sql.toLowerCase(Locale.ENGLISH).trim();
if(queryLC.startsWith("insert ")) {
return SqlQueryType.INSERT;
if (queryLC.startsWith("insert ")) {
return QueryType.INSERT;
}
if(queryLC.startsWith("update ")) {
return SqlQueryType.UPDATE;
if (queryLC.startsWith("update ")) {
return QueryType.UPDATE;
}
if(queryLC.startsWith("delete ")) {
return SqlQueryType.DELETE;
if (queryLC.startsWith("delete ")) {
return QueryType.DELETE;
}
if(queryLC.startsWith("select ")) {
return SqlQueryType.SELECT;
if (queryLC.startsWith("select ")) {
return QueryType.SELECT;
}
if(queryLC.startsWith("with ")) {
if (queryLC.startsWith("with ")) {
// Let's skip the table definition to see what action is performed.
queryLC = queryLC.replaceAll("[\\t\\n\\x0B\\f\\r]+", " ");
queryLC = queryLC.substring("with ".length());
boolean isFindingWithStatementEnd = true;
while(isFindingWithStatementEnd) {
while (isFindingWithStatementEnd) {
isFindingWithStatementEnd = false;
// Let's consider the query is properly created, and the "as" is at the right place.
int index = queryLC.indexOf(" as ");
if(index == -1) {
if (index == -1) {
break;
}
queryLC = queryLC.substring(index + " as ".length()).trim();
if(!queryLC.startsWith("(")) {
if (!queryLC.startsWith("(")) {
break;
}
int length = queryLC.length();
boolean isQuote = false;
int pCount = 1;
for(int i=1; i<length; i++) {
for (int i = 1; i < length; i++) {
char c = queryLC.charAt(i);
switch(c) {
switch (c) {
case '\'':
isQuote = !isQuote;
break;
case '(':
if(!isQuote) {
if (!isQuote) {
pCount++;
}
break;
case ')':
if(!isQuote) {
if (!isQuote) {
pCount--;
}
break;
}
if(pCount == 0) {
if (pCount == 0) {
queryLC = queryLC.substring(i + 1).trim();
if(queryLC.startsWith(",")) {
// Another table definition in the with clause, so let's do another round.
// Another table definition in the with clause, so let's do another round.
if (queryLC.startsWith(",")) {
isFindingWithStatementEnd = true;
break;
} else {
// We removed the with part. Let's ask the type of this.
}
// We removed the with part. Let's ask the type of this.
else {
return detectType(queryLC);
}
}
}
}
// If we couldn't find a proper with structure, default to OTHER.
return SqlQueryType.OTHER;
return QueryType.OTHER;
}
return SqlQueryType.OTHER;
return QueryType.OTHER;
}
}

View File

@ -44,13 +44,13 @@ import java.io.Serializable;
@SuppressWarnings("serial")
public class StatementInfo implements Serializable {
private SqlQueryType queryType;
private QueryType queryType;
private String[] queries;
private String parameterDescription;
private String threadName;
private long threadID;
public StatementInfo(SqlQueryType queryType, String[] queries, String parameterDescription) {
public StatementInfo(QueryType queryType, String[] queries, String parameterDescription) {
Thread currentThread = Thread.currentThread();
this.threadName = currentThread.getName();
this.threadID = currentThread.getId();
@ -67,7 +67,7 @@ public class StatementInfo implements Serializable {
return threadID;
}
public SqlQueryType getQueryType() {
public QueryType getQueryType() {
return queryType;
}

View File

@ -51,14 +51,14 @@ public class StatementMatcher implements Serializable {
private boolean isActive;
private TextMatcher threadNameTextMatcher;
private TextMatcher statementTextMatcher;
private Set<SqlQueryType> queryTypeSet;
private Set<QueryType> queryTypeSet;
/**
* @param threadNameTextMatcher a text matcher for thread name or null for no text matching.
* @param statementTextMatcher a text matcher for statement or null for no text matching.
* @param queryTypeSet some types or null for all types.
*/
public StatementMatcher(TextMatcher threadNameTextMatcher, TextMatcher statementTextMatcher, Set<SqlQueryType> queryTypeSet, boolean isActive) {
public StatementMatcher(TextMatcher threadNameTextMatcher, TextMatcher statementTextMatcher, Set<QueryType> queryTypeSet, boolean isActive) {
this.threadNameTextMatcher = threadNameTextMatcher;
this.statementTextMatcher = statementTextMatcher;
this.queryTypeSet = queryTypeSet == null? null: EnumSet.copyOf(queryTypeSet);
@ -99,7 +99,7 @@ public class StatementMatcher implements Serializable {
return statementTextMatcher;
}
public Set<SqlQueryType> getQueryTypeSet() {
public Set<QueryType> getQueryTypeSet() {
return queryTypeSet;
}

View File

@ -59,7 +59,7 @@ import javax.swing.JFormattedTextField;
import javax.swing.JPanel;
import org.jooq.debug.Breakpoint;
import org.jooq.debug.SqlQueryType;
import org.jooq.debug.QueryType;
import org.jooq.debug.StatementMatcher;
import org.jooq.debug.StatementProcessor;
import org.jooq.debug.console.misc.TextMatcher;
@ -117,7 +117,7 @@ public class BreakpointEditor extends JPanel {
statementTextMatcherPane = new TextMatcherPane(statementTextMatcher);
add(statementTextMatcherPane, new GridBagConstraints(1, y, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));
y++;
Set<SqlQueryType> queryTypeSet = statementMatcher.getQueryTypeSet();
Set<QueryType> queryTypeSet = statementMatcher.getQueryTypeSet();
statementTypeCheckBox = new JCheckBox("Type", queryTypeSet != null);
statementTypeCheckBox.setOpaque(false);
statementTypeCheckBox.addItemListener(new ItemListener() {
@ -129,19 +129,19 @@ public class BreakpointEditor extends JPanel {
add(statementTypeCheckBox, new GridBagConstraints(0, y, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
JPanel typesPane = new JPanel(new GridBagLayout());
typesPane.setOpaque(false);
statementTypeSelectCheckBox = new JCheckBox("SELECT", queryTypeSet != null && queryTypeSet.contains(SqlQueryType.SELECT));
statementTypeSelectCheckBox = new JCheckBox("SELECT", queryTypeSet != null && queryTypeSet.contains(QueryType.SELECT));
statementTypeSelectCheckBox.setOpaque(false);
typesPane.add(statementTypeSelectCheckBox, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
statementTypeUpdateCheckBox = new JCheckBox("UPDATE", queryTypeSet != null && queryTypeSet.contains(SqlQueryType.UPDATE));
statementTypeUpdateCheckBox = new JCheckBox("UPDATE", queryTypeSet != null && queryTypeSet.contains(QueryType.UPDATE));
statementTypeUpdateCheckBox.setOpaque(false);
typesPane.add(statementTypeUpdateCheckBox, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 0, 0));
statementTypeInsertCheckBox = new JCheckBox("INSERT", queryTypeSet != null && queryTypeSet.contains(SqlQueryType.INSERT));
statementTypeInsertCheckBox = new JCheckBox("INSERT", queryTypeSet != null && queryTypeSet.contains(QueryType.INSERT));
statementTypeInsertCheckBox.setOpaque(false);
typesPane.add(statementTypeInsertCheckBox, new GridBagConstraints(2, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 0, 0));
statementTypeDeleteCheckBox = new JCheckBox("DELETE", queryTypeSet != null && queryTypeSet.contains(SqlQueryType.DELETE));
statementTypeDeleteCheckBox = new JCheckBox("DELETE", queryTypeSet != null && queryTypeSet.contains(QueryType.DELETE));
statementTypeDeleteCheckBox.setOpaque(false);
typesPane.add(statementTypeDeleteCheckBox, new GridBagConstraints(3, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 0, 0));
statementTypeOtherCheckBox = new JCheckBox("OTHER", queryTypeSet != null && queryTypeSet.contains(SqlQueryType.OTHER));
statementTypeOtherCheckBox = new JCheckBox("OTHER", queryTypeSet != null && queryTypeSet.contains(QueryType.OTHER));
statementTypeOtherCheckBox.setOpaque(false);
typesPane.add(statementTypeOtherCheckBox, new GridBagConstraints(4, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 0, 0));
add(typesPane, new GridBagConstraints(1, y, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 0), 0, 0));
@ -289,23 +289,23 @@ public class BreakpointEditor extends JPanel {
boolean isActive = true;//activeCheckBox.isSelected();
TextMatcher threadNameTextMatcher = threadNameTextMatcherCheckBox.isSelected()? threadNameTextMatcherPane.getTextMatcher(): null;
TextMatcher statementTextMatcher = statementTextMatcherCheckBox.isSelected()? statementTextMatcherPane.getTextMatcher(): null;
Set<SqlQueryType> queryTypeSet;
Set<QueryType> queryTypeSet;
if(statementTypeCheckBox.isSelected()) {
List<SqlQueryType> typeList = new ArrayList<SqlQueryType>();
List<QueryType> typeList = new ArrayList<QueryType>();
if(statementTypeSelectCheckBox.isSelected()) {
typeList.add(SqlQueryType.SELECT);
typeList.add(QueryType.SELECT);
}
if(statementTypeUpdateCheckBox.isSelected()) {
typeList.add(SqlQueryType.UPDATE);
typeList.add(QueryType.UPDATE);
}
if(statementTypeInsertCheckBox.isSelected()) {
typeList.add(SqlQueryType.INSERT);
typeList.add(QueryType.INSERT);
}
if(statementTypeDeleteCheckBox.isSelected()) {
typeList.add(SqlQueryType.DELETE);
typeList.add(QueryType.DELETE);
}
if(statementTypeOtherCheckBox.isSelected()) {
typeList.add(SqlQueryType.OTHER);
typeList.add(QueryType.OTHER);
}
queryTypeSet = EnumSet.copyOf(typeList);
} else {

View File

@ -101,7 +101,7 @@ import org.jooq.debug.Debugger;
import org.jooq.debug.LoggingListener;
import org.jooq.debug.QueryLoggingData;
import org.jooq.debug.ResultSetLoggingData;
import org.jooq.debug.SqlQueryType;
import org.jooq.debug.QueryType;
import org.jooq.debug.StatementMatcher;
import org.jooq.debug.console.misc.InvisibleSplitPane;
import org.jooq.debug.console.misc.JTableX;
@ -456,7 +456,7 @@ public class LoggerPane extends JPanel {
public Class<?> getColumnClass(int columnIndex) {
switch(columnIndex) {
case COLUMN_LINE: return Integer.class;
case COLUMN_TYPE: return SqlQueryType.class;
case COLUMN_TYPE: return QueryType.class;
case COLUMN_PS_PREPARATION_DURATION: return Long.class;
case COLUMN_PS_BINDING_DURATION: return Long.class;
case COLUMN_EXEC_TIME: return Long.class;
@ -485,7 +485,7 @@ public class LoggerPane extends JPanel {
table.setColumnSelectionAllowed(true);
table.setFillsViewportHeight(true);
// ToolTipManager.sharedInstance().registerComponent(table);
table.setDefaultRenderer(SqlQueryType.class, new DefaultTableCellRenderer() {
table.setDefaultRenderer(QueryType.class, new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
@ -783,7 +783,7 @@ public class LoggerPane extends JPanel {
public long getExecutionDuration() {
return queryLoggingData.getExecutionDuration();
}
public SqlQueryType getQueryType() {
public QueryType getQueryType() {
return queryLoggingData.getQueryType();
}
public String[] getQueries() {

View File

@ -55,7 +55,7 @@ import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import org.jooq.debug.SqlQueryType;
import org.jooq.debug.QueryType;
import org.jooq.debug.StatementMatcher;
import org.jooq.debug.console.misc.TextMatcher;
@ -119,7 +119,7 @@ public class StatementMatcherPane extends JPanel {
statementTextMatcherPane = new TextMatcherPane(statementTextMatcher);
add(statementTextMatcherPane, new GridBagConstraints(1, y, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 0), 0, 0));
y++;
Set<SqlQueryType> queryTypeSet = statementMatcher.getQueryTypeSet();
Set<QueryType> queryTypeSet = statementMatcher.getQueryTypeSet();
statementTypeCheckBox = new JCheckBox("Type", queryTypeSet != null);
statementTypeCheckBox.addItemListener(new ItemListener() {
@Override
@ -129,15 +129,15 @@ public class StatementMatcherPane extends JPanel {
});
add(statementTypeCheckBox, new GridBagConstraints(0, y, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
JPanel typesPane = new JPanel(new GridBagLayout());
statementTypeSelectCheckBox = new JCheckBox("SELECT", queryTypeSet != null && queryTypeSet.contains(SqlQueryType.SELECT));
statementTypeSelectCheckBox = new JCheckBox("SELECT", queryTypeSet != null && queryTypeSet.contains(QueryType.SELECT));
typesPane.add(statementTypeSelectCheckBox, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
statementTypeUpdateCheckBox = new JCheckBox("UPDATE", queryTypeSet != null && queryTypeSet.contains(SqlQueryType.UPDATE));
statementTypeUpdateCheckBox = new JCheckBox("UPDATE", queryTypeSet != null && queryTypeSet.contains(QueryType.UPDATE));
typesPane.add(statementTypeUpdateCheckBox, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 0, 0));
statementTypeInsertCheckBox = new JCheckBox("INSERT", queryTypeSet != null && queryTypeSet.contains(SqlQueryType.INSERT));
statementTypeInsertCheckBox = new JCheckBox("INSERT", queryTypeSet != null && queryTypeSet.contains(QueryType.INSERT));
typesPane.add(statementTypeInsertCheckBox, new GridBagConstraints(2, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 0, 0));
statementTypeDeleteCheckBox = new JCheckBox("DELETE", queryTypeSet != null && queryTypeSet.contains(SqlQueryType.DELETE));
statementTypeDeleteCheckBox = new JCheckBox("DELETE", queryTypeSet != null && queryTypeSet.contains(QueryType.DELETE));
typesPane.add(statementTypeDeleteCheckBox, new GridBagConstraints(3, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 0, 0));
statementTypeOtherCheckBox = new JCheckBox("OTHER", queryTypeSet != null && queryTypeSet.contains(SqlQueryType.OTHER));
statementTypeOtherCheckBox = new JCheckBox("OTHER", queryTypeSet != null && queryTypeSet.contains(QueryType.OTHER));
typesPane.add(statementTypeOtherCheckBox, new GridBagConstraints(4, 0, 1, 1, 0, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 2, 0, 0), 0, 0));
add(typesPane, new GridBagConstraints(1, y, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 5, 0, 0), 0, 0));
y++;
@ -173,23 +173,23 @@ public class StatementMatcherPane extends JPanel {
boolean isActive = activeCheckBox.isSelected();
TextMatcher threadNameTextMatcher = threadNameTextMatcherCheckBox.isSelected()? threadNameTextMatcherPane.getTextMatcher(): null;
TextMatcher statementTextMatcher = statementTextMatcherCheckBox.isSelected()? statementTextMatcherPane.getTextMatcher(): null;
Set<SqlQueryType> queryTypeSet;
Set<QueryType> queryTypeSet;
if(statementTypeCheckBox.isSelected()) {
List<SqlQueryType> typeList = new ArrayList<SqlQueryType>();
List<QueryType> typeList = new ArrayList<QueryType>();
if(statementTypeSelectCheckBox.isSelected()) {
typeList.add(SqlQueryType.SELECT);
typeList.add(QueryType.SELECT);
}
if(statementTypeUpdateCheckBox.isSelected()) {
typeList.add(SqlQueryType.UPDATE);
typeList.add(QueryType.UPDATE);
}
if(statementTypeInsertCheckBox.isSelected()) {
typeList.add(SqlQueryType.INSERT);
typeList.add(QueryType.INSERT);
}
if(statementTypeDeleteCheckBox.isSelected()) {
typeList.add(SqlQueryType.DELETE);
typeList.add(QueryType.DELETE);
}
if(statementTypeOtherCheckBox.isSelected()) {
typeList.add(SqlQueryType.OTHER);
typeList.add(QueryType.OTHER);
}
queryTypeSet = EnumSet.copyOf(typeList);
} else {