Permalink
Browse files

docs

  • Loading branch information...
1 parent 704814c commit 46b870e42b48aeb910413fbc60c74d106ad00b1d @tokuhirom committed Apr 9, 2016
View
@@ -1,10 +1,10 @@
-# jdbc-tracer
+# JDBC QueryLog
-[![Maven Central](https://maven-badges.herokuapp.com/maven-central/me.geso.jdbcquerylog/jdbc-tracer/badge.svg)](https://maven-badges.herokuapp.com/maven-central/me.geso.jdbcquerylog/jdbc-tracer)
-[![javadoc.io](https://javadocio-badges.herokuapp.com/me.geso.jdbcquerylog/jdbc-tracer/badge.svg)](https://javadocio-badges.herokuapp.com/me.geso.jdbcquerylog/jdbc-tracer)
+[![Maven Central](https://maven-badges.herokuapp.com/maven-central/me.geso.jdbcquerylog/jdbc-querylog/badge.svg)](https://maven-badges.herokuapp.com/maven-central/me.geso.jdbcquerylog/jdbc-querylog)
+[![javadoc.io](https://javadocio-badges.herokuapp.com/me.geso.jdbcquerylog/jdbc-querylog/badge.svg)](https://javadocio-badges.herokuapp.com/me.geso.jdbcquerylog/jdbc-querylog)
[![Circle CI](https://circleci.com/gh/tokuhirom/jdbc-querylog.svg?style=svg)](https://circleci.com/gh/tokuhirom/jdbc-querylog)
-jdbc-tracer is a JDBC driver, which provides hook points to trace all queries.
+jdbc-querylog is a JDBC driver, which provides hook points to trace all queries.
## Use cases
@@ -14,9 +14,55 @@ jdbc-tracer is a JDBC driver, which provides hook points to trace all queries.
* Logging queries and parameters.
* Detect webapp controller, that sends too much SQL queries.
-## Listeners
+## Usage
-You need to implement PreparedStatementListener or ResultSetListener.
+If your JDBC uri is this:
+
+ jdbc:mysql://localhost/test
+
+Then, you need to rewrite the uri as following:
+
+ jdbc:querylog:mysql://localhost/test
+
+That's all.
+
+### Options
+
+#### `QueryLogDriver.setExplain(true)` : Default false
+
+Enable automatic EXPLAIN statement generation.
+
+(Note, this feature only supports mysql. patches welcome)
+
+#### `QueryLogDriver.setCompact(true)` : Default true
+
+Compact SQL query before query logging.
+
+#### `QueryLogDriver.setQueryHandler(Connection connection, String query)`
+
+ QueryLogDriver.setQueryHandler((connection, query) -> {
+ System.err.println("Query: " + query);
+ });
+
+Added query handler. If you set this handler, querylog pass the query to
+the callback handler.
+
+#### `QueryLogDriver.setExplainHandler(ExplainHandler printExplain)`
+
+ QueryLogDriver.setExplainHandler((connection, query, header, rows) -> {
+ System.err.println("Query: " + query);
+ System.err.println("Header: " + Arrays.toString(header));
+ System.err.println("Rows: " + rows.stream()
+ .map(it -> Arrays.stream(it).collect(Collectors.joining(",")))
+ .collect(Collectors.joining("\n")));
+ });
+
+Set explain handler. If you call `QueryLogDriver.setExplain(true)`, querylog
+sends EXPLAIN statement to DB server, and pass the results to the callback listener.
+
+## jdbc-tracer
+
+jdbc-querylog is based on jdbc-tracer. jdbc-tracer provides following two listener interfaces.
### PreparedStatementListener
@@ -41,6 +87,12 @@ You can get following values for each got rows:
You can install this library from maven central.
+## SEE ALSO
+
+This is a port of Perl5's DBIx::QueryLog.
+
+http://search.cpan.org/perldoc?DBIx%3A%3AQueryLog
+
## License
The MIT License (MIT)
@@ -11,7 +11,6 @@
public class PreparedStatementLogger implements PreparedStatementListener {
private static final Pattern RE = Pattern.compile("(\\?)");
- private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(QueryLogDriver.class);
@Override
public void trace(Connection connection, long elapsed, String query, List<Object> args) throws SQLException {
@@ -53,7 +52,7 @@ public void trace(Connection connection, long elapsed, String query, List<Object
rows.add(row);
}
- QueryLogDriver.getPrintExplain().accept(
+ QueryLogDriver.getExplainHandler().accept(
connection,
query,
header,
@@ -22,7 +22,7 @@
private static BiConsumer<Connection, String> printQuery = (connection, query) -> {
System.err.println(query);
};
- private static PrintExplainCallback printExplain = (connection, query, header, rows) -> {
+ private static ExplainHandler explainHandler = (connection, query, header, rows) -> {
V2_AsciiTable at = new V2_AsciiTable();
at.addRule();
at.addRow((Object[]) header);
@@ -69,24 +69,24 @@ public static void setCompact(boolean compact) {
QueryLogDriver.compact = compact;
}
- public static void setPrintQuery(BiConsumer<Connection, String> printQuery) {
+ public static void setQueryHandler(BiConsumer<Connection, String> printQuery) {
QueryLogDriver.printQuery = printQuery;
}
public static BiConsumer<Connection, String> getPrintQuery() {
return QueryLogDriver.printQuery;
}
- public static PrintExplainCallback getPrintExplain() {
- return printExplain;
+ public static ExplainHandler getExplainHandler() {
+ return explainHandler;
}
- public static void setPrintExplain(PrintExplainCallback printExplain) {
- QueryLogDriver.printExplain = printExplain;
+ public static void setExplainHandler(ExplainHandler explainHandler) {
+ QueryLogDriver.explainHandler = explainHandler;
}
@FunctionalInterface
- public interface PrintExplainCallback {
+ public interface ExplainHandler {
void accept(Connection connection, String query, String[] header, List<String[]> rows);
}
@@ -3,6 +3,8 @@
import me.geso.jdbcquerylog.QueryLogDriver;
import java.sql.*;
+import java.util.Arrays;
+import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
@@ -19,6 +21,16 @@ public static void main(String[] args) throws SQLException, ClassNotFoundExcepti
private static void doMain() throws SQLException {
QueryLogDriver.setExplain(true);
+ QueryLogDriver.setQueryHandler((connection, query) -> {
+ System.err.println("Query: " + query);
+ });
+ QueryLogDriver.setExplainHandler((connection, query, header, rows) -> {
+ System.err.println("Query: " + query);
+ System.err.println("Header: " + Arrays.toString(header));
+ System.err.println("Rows: " + rows.stream()
+ .map(it -> Arrays.stream(it).collect(Collectors.joining(",")))
+ .collect(Collectors.joining("\n")));
+ });
try (Connection conn = DriverManager.getConnection("jdbc:querylog:h2:mem:test")) {
try (PreparedStatement stmt = conn.prepareStatement("SELECT * FROM user WHERE id=?")) {

0 comments on commit 46b870e

Please sign in to comment.