Skip to content

Commit 074cef5

Browse files
committed
Closes #152. Indentation for nested selects logging
1 parent ea4277a commit 074cef5

File tree

7 files changed

+68
-60
lines changed

7 files changed

+68
-60
lines changed

src/main/java/org/apache/ibatis/logging/jdbc/BaseJdbcLogger.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2011 the original author or authors.
2+
* Copyright 2009-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616
package org.apache.ibatis.logging.jdbc;
1717

1818
import java.util.ArrayList;
19+
import java.util.Arrays;
1920
import java.util.HashMap;
2021
import java.util.HashSet;
2122
import java.util.List;
@@ -38,13 +39,16 @@ public abstract class BaseJdbcLogger {
3839
private List<Object> columnNames = new ArrayList<Object>();
3940
private List<Object> columnValues = new ArrayList<Object>();
4041

41-
private Log statementLog;
42+
protected Log statementLog;
43+
protected int queryStack;
4244

4345
/*
4446
* Default constructor
4547
*/
46-
public BaseJdbcLogger(Log log) {
48+
public BaseJdbcLogger(Log log, int queryStack) {
4749
this.statementLog = log;
50+
if (queryStack == 0) queryStack = 1;
51+
this.queryStack = queryStack;
4852
}
4953

5054
static {
@@ -127,20 +131,28 @@ protected boolean isTraceEnabled() {
127131
return statementLog.isTraceEnabled();
128132
}
129133

130-
protected void debug(String text) {
134+
protected void debug(String text, boolean input) {
131135
if (statementLog.isDebugEnabled()) {
132-
statementLog.debug(text);
136+
statementLog.debug(prefix(input) + text);
133137
}
134138
}
135139

136-
protected void trace(String text) {
140+
protected void trace(String text, boolean input) {
137141
if (statementLog.isTraceEnabled()) {
138-
statementLog.trace(text);
142+
statementLog.trace(prefix(input) + text);
139143
}
140144
}
141145

142-
public Log getStatementLog() {
143-
return statementLog;
146+
private String prefix(boolean isInput) {
147+
char[] buffer = new char[queryStack * 2 + 2];
148+
Arrays.fill(buffer, '=');
149+
buffer[queryStack * 2 + 1] = ' ';
150+
if (isInput) {
151+
buffer[queryStack * 2] = '>';
152+
} else {
153+
buffer[0] = '<';
154+
}
155+
return new String(buffer);
144156
}
145157

146158
}

src/main/java/org/apache/ibatis/logging/jdbc/ConnectionLogger.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,40 +32,35 @@ public final class ConnectionLogger extends BaseJdbcLogger implements Invocation
3232

3333
private Connection connection;
3434

35-
private ConnectionLogger(Connection conn, Log statementLog) {
36-
super(statementLog);
35+
private ConnectionLogger(Connection conn, Log statementLog, int queryStack) {
36+
super(statementLog, queryStack);
3737
this.connection = conn;
38-
if (isDebugEnabled()) {
39-
debug("ooo Using Connection [" + conn + "]");
40-
}
4138
}
4239

4340
public Object invoke(Object proxy, Method method, Object[] params)
4441
throws Throwable {
4542
try {
43+
if (Object.class.equals(method.getDeclaringClass())) {
44+
return method.invoke(this, params);
45+
}
4646
if ("prepareStatement".equals(method.getName())) {
4747
if (isDebugEnabled()) {
48-
debug("==> Preparing: " + removeBreakingWhitespace((String) params[0]));
48+
debug(" Preparing: " + removeBreakingWhitespace((String) params[0]), true);
4949
}
5050
PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);
51-
stmt = PreparedStatementLogger.newInstance(stmt, getStatementLog());
51+
stmt = PreparedStatementLogger.newInstance(stmt, statementLog, queryStack);
5252
return stmt;
5353
} else if ("prepareCall".equals(method.getName())) {
5454
if (isDebugEnabled()) {
55-
debug("==> Preparing: " + removeBreakingWhitespace((String) params[0]));
55+
debug(" Preparing: " + removeBreakingWhitespace((String) params[0]), true);
5656
}
5757
PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);
58-
stmt = PreparedStatementLogger.newInstance(stmt, getStatementLog());
58+
stmt = PreparedStatementLogger.newInstance(stmt, statementLog, queryStack);
5959
return stmt;
6060
} else if ("createStatement".equals(method.getName())) {
6161
Statement stmt = (Statement) method.invoke(connection, params);
62-
stmt = StatementLogger.newInstance(stmt, getStatementLog());
62+
stmt = StatementLogger.newInstance(stmt, statementLog, queryStack);
6363
return stmt;
64-
} else if ("close".equals(method.getName())) {
65-
if (isDebugEnabled()) {
66-
debug("xxx Connection Closed");
67-
}
68-
return method.invoke(connection, params);
6964
} else {
7065
return method.invoke(connection, params);
7166
}
@@ -80,8 +75,8 @@ public Object invoke(Object proxy, Method method, Object[] params)
8075
* @param conn - the original connection
8176
* @return - the connection with logging
8277
*/
83-
public static Connection newInstance(Connection conn, Log statementLog) {
84-
InvocationHandler handler = new ConnectionLogger(conn, statementLog);
78+
public static Connection newInstance(Connection conn, Log statementLog, int queryStack) {
79+
InvocationHandler handler = new ConnectionLogger(conn, statementLog, queryStack);
8580
ClassLoader cl = Connection.class.getClassLoader();
8681
return (Connection) Proxy.newProxyInstance(cl, new Class[]{Connection.class}, handler);
8782
}

src/main/java/org/apache/ibatis/logging/jdbc/PreparedStatementLogger.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,25 @@ public final class PreparedStatementLogger extends BaseJdbcLogger implements Inv
3232

3333
private PreparedStatement statement;
3434

35-
private PreparedStatementLogger(PreparedStatement stmt, Log statementLog) {
36-
super(statementLog);
35+
private PreparedStatementLogger(PreparedStatement stmt, Log statementLog, int queryStack) {
36+
super(statementLog, queryStack);
3737
this.statement = stmt;
3838
}
3939

4040
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
4141
try {
42+
if (Object.class.equals(method.getDeclaringClass())) {
43+
return method.invoke(this, params);
44+
}
4245
if (EXECUTE_METHODS.contains(method.getName())) {
4346
if (isDebugEnabled()) {
44-
debug("==> Parameters: " + getParameterValueString());
47+
debug("Parameters: " + getParameterValueString(), true);
4548
}
4649
clearColumnInfo();
4750
if ("executeQuery".equals(method.getName())) {
4851
ResultSet rs = (ResultSet) method.invoke(statement, params);
4952
if (rs != null) {
50-
return ResultSetLogger.newInstance(rs, getStatementLog());
53+
return ResultSetLogger.newInstance(rs, statementLog, queryStack);
5154
} else {
5255
return null;
5356
}
@@ -64,21 +67,16 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
6467
} else if ("getResultSet".equals(method.getName())) {
6568
ResultSet rs = (ResultSet) method.invoke(statement, params);
6669
if (rs != null) {
67-
return ResultSetLogger.newInstance(rs, getStatementLog());
70+
return ResultSetLogger.newInstance(rs, statementLog, queryStack);
6871
} else {
6972
return null;
7073
}
7174
} else if ("getUpdateCount".equals(method.getName())) {
7275
int updateCount = (Integer) method.invoke(statement, params);
7376
if (updateCount != -1) {
74-
debug("<== Updates: " + updateCount);
77+
debug(" Updates: " + updateCount, false);
7578
}
7679
return updateCount;
77-
} else if ("equals".equals(method.getName())) {
78-
Object ps = params[0];
79-
return ps instanceof Proxy && proxy == ps;
80-
} else if ("hashCode".equals(method.getName())) {
81-
return proxy.hashCode();
8280
} else {
8381
return method.invoke(statement, params);
8482
}
@@ -94,8 +92,8 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
9492
* @param sql - the sql statement
9593
* @return - the proxy
9694
*/
97-
public static PreparedStatement newInstance(PreparedStatement stmt, Log statementLog) {
98-
InvocationHandler handler = new PreparedStatementLogger(stmt, statementLog);
95+
public static PreparedStatement newInstance(PreparedStatement stmt, Log statementLog, int queryStack) {
96+
InvocationHandler handler = new PreparedStatementLogger(stmt, statementLog, queryStack);
9997
ClassLoader cl = PreparedStatement.class.getClassLoader();
10098
return (PreparedStatement) Proxy.newProxyInstance(cl, new Class[]{PreparedStatement.class, CallableStatement.class}, handler);
10199
}

src/main/java/org/apache/ibatis/logging/jdbc/ResultSetLogger.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@ public final class ResultSetLogger extends BaseJdbcLogger implements InvocationH
5050
BLOB_TYPES.add(Types.VARBINARY);
5151
}
5252

53-
private ResultSetLogger(ResultSet rs, Log statementLog) {
54-
super(statementLog);
53+
private ResultSetLogger(ResultSet rs, Log statementLog, int queryStack) {
54+
super(statementLog, queryStack);
5555
this.rs = rs;
5656
}
5757

5858
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
5959
try {
60+
if (Object.class.equals(method.getDeclaringClass())) {
61+
return method.invoke(this, params);
62+
}
6063
Object o = method.invoke(rs, params);
6164
if ("next".equals(method.getName())) {
6265
if (((Boolean) o)) {
@@ -71,7 +74,7 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
7174
printColumnValues(columnCount);
7275
}
7376
} else {
74-
debug("<== Total: " + rows);
77+
debug(" Total: " + rows, false);
7578
}
7679
}
7780
clearColumnInfo();
@@ -83,7 +86,7 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
8386

8487
private void printColumnHeaders(ResultSetMetaData rsmd, int columnCount) throws SQLException {
8588
StringBuilder row = new StringBuilder();
86-
row.append("<== Columns: ");
89+
row.append(" Columns: ");
8790
for (int i = 1; i <= columnCount; i++) {
8891
if (BLOB_TYPES.contains(rsmd.getColumnType(i))) {
8992
blobColumns.add(i);
@@ -92,12 +95,12 @@ private void printColumnHeaders(ResultSetMetaData rsmd, int columnCount) throws
9295
row.append(colname);
9396
if (i != columnCount) row.append(", ");
9497
}
95-
trace(row.toString());
98+
trace(row.toString(), false);
9699
}
97100

98101
private void printColumnValues(int columnCount) throws SQLException {
99102
StringBuilder row = new StringBuilder();
100-
row.append("<== Row: ");
103+
row.append(" Row: ");
101104
for (int i = 1; i <= columnCount; i++) {
102105
String colname;
103106
try {
@@ -113,7 +116,7 @@ private void printColumnValues(int columnCount) throws SQLException {
113116
row.append(colname);
114117
if (i != columnCount) row.append(", ");
115118
}
116-
trace(row.toString());
119+
trace(row.toString(), false);
117120
}
118121

119122
/*
@@ -122,8 +125,8 @@ private void printColumnValues(int columnCount) throws SQLException {
122125
* @param rs - the ResultSet to proxy
123126
* @return - the ResultSet with logging
124127
*/
125-
public static ResultSet newInstance(ResultSet rs, Log statementLog) {
126-
InvocationHandler handler = new ResultSetLogger(rs, statementLog);
128+
public static ResultSet newInstance(ResultSet rs, Log statementLog, int queryStack) {
129+
InvocationHandler handler = new ResultSetLogger(rs, statementLog, queryStack);
127130
ClassLoader cl = ResultSet.class.getClassLoader();
128131
return (ResultSet) Proxy.newProxyInstance(cl, new Class[]{ResultSet.class}, handler);
129132
}

src/main/java/org/apache/ibatis/logging/jdbc/StatementLogger.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,24 @@ public final class StatementLogger extends BaseJdbcLogger implements InvocationH
3131

3232
private Statement statement;
3333

34-
private StatementLogger(Statement stmt, Log statementLog) {
35-
super(statementLog);
34+
private StatementLogger(Statement stmt, Log statementLog, int queryStack) {
35+
super(statementLog, queryStack);
3636
this.statement = stmt;
3737
}
3838

3939
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
4040
try {
41+
if (Object.class.equals(method.getDeclaringClass())) {
42+
return method.invoke(this, params);
43+
}
4144
if (EXECUTE_METHODS.contains(method.getName())) {
4245
if (isDebugEnabled()) {
43-
debug("==> Executing: " + removeBreakingWhitespace((String) params[0]));
46+
debug(" Executing: " + removeBreakingWhitespace((String) params[0]), true);
4447
}
4548
if ("executeQuery".equals(method.getName())) {
4649
ResultSet rs = (ResultSet) method.invoke(statement, params);
4750
if (rs != null) {
48-
return ResultSetLogger.newInstance(rs, getStatementLog());
51+
return ResultSetLogger.newInstance(rs, statementLog, queryStack);
4952
} else {
5053
return null;
5154
}
@@ -55,7 +58,7 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
5558
} else if ("getResultSet".equals(method.getName())) {
5659
ResultSet rs = (ResultSet) method.invoke(statement, params);
5760
if (rs != null) {
58-
return ResultSetLogger.newInstance(rs, getStatementLog());
61+
return ResultSetLogger.newInstance(rs, statementLog, queryStack);
5962
} else {
6063
return null;
6164
}
@@ -78,8 +81,8 @@ public Object invoke(Object proxy, Method method, Object[] params) throws Throwa
7881
* @param stmt - the statement
7982
* @return - the proxy
8083
*/
81-
public static Statement newInstance(Statement stmt, Log statementLog) {
82-
InvocationHandler handler = new StatementLogger(stmt, statementLog);
84+
public static Statement newInstance(Statement stmt, Log statementLog, int queryStack) {
85+
InvocationHandler handler = new StatementLogger(stmt, statementLog, queryStack);
8386
ClassLoader cl = Statement.class.getClassLoader();
8487
return (Statement) Proxy.newProxyInstance(cl, new Class[]{Statement.class}, handler);
8588
}

src/test/java/org/apache/ibatis/executor/BaseExecutorTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ public void shouldSelectDiscriminatedPost() throws Exception {
253253
}
254254
}
255255
} finally {
256-
executor.rollback(true);
257256
executor.close(false);
258257
}
259258
}
@@ -340,6 +339,7 @@ public void shouldFetchPostsForBlog() throws Exception {
340339
List<Post> posts = executor.query(selectPosts, 1, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
341340
executor.flushStatements();
342341
assertEquals(2, posts.size());
342+
assertTrue(posts.get(1).getClass().getName().contains("CGLIB"));
343343
assertNotNull(posts.get(1).getBlog());
344344
assertEquals(1, posts.get(1).getBlog().getId());
345345
executor.rollback(true);
@@ -392,7 +392,6 @@ public void shouldFetchPostWithBlogWithCompositeKey() throws Exception {
392392
}
393393
}
394394

395-
396395
@Test
397396
public void shouldFetchComplexBlogs() throws Exception {
398397

@@ -402,7 +401,6 @@ public void shouldFetchComplexBlogs() throws Exception {
402401
MappedStatement selectPosts = ExecutorTestHelper.prepareSelectPostsForBlogMappedStatement(config);
403402
config.addMappedStatement(selectBlog);
404403
config.addMappedStatement(selectPosts);
405-
config.setLazyLoadingEnabled(true);
406404
List<Blog> blogs = executor.query(selectBlog, 1, RowBounds.DEFAULT, Executor.NO_RESULT_HANDLER);
407405
executor.flushStatements();
408406
assertEquals(1, blogs.size());
@@ -411,7 +409,6 @@ public void shouldFetchComplexBlogs() throws Exception {
411409
assertEquals(1, blogs.get(0).getPosts().get(1).getBlog().getPosts().get(1).getBlog().getId());
412410
executor.rollback(true);
413411
} finally {
414-
config.setLazyLoadingEnabled(true);
415412
executor.rollback(true);
416413
executor.close(false);
417414
}

src/test/java/org/apache/ibatis/logging/jdbc/ResultSetLoggerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void setup(int type) throws SQLException {
5050
when(metaData.getColumnLabel(1)).thenReturn("ColumnName");
5151
when(rs.getString(1)).thenReturn("value");
5252
when(log.isTraceEnabled()).thenReturn(true);
53-
ResultSet resultSet = ResultSetLogger.newInstance(rs, log);
53+
ResultSet resultSet = ResultSetLogger.newInstance(rs, log, 1);
5454
resultSet.next();
5555
}
5656

0 commit comments

Comments
 (0)