Permalink
Browse files

Docs.

  • Loading branch information...
1 parent 46b870e commit f0e33dce70c78ae232f161d40431aadb6fb9ada7 @tokuhirom committed Apr 9, 2016
View
@@ -6,6 +6,48 @@
jdbc-querylog is a JDBC driver, which provides hook points to trace all queries.
+## SYNOPSIS
+
+Use 'jdbc:querylog:' prefix for JDBC connection uri.
+
+ QueryLogDriver.setExplain(true);
+ try (Connection conn = DriverManager.getConnection("jdbc:querylog:h2:mem:test")) {
+ try (PreparedStatement stmt = conn.prepareStatement("SELECT * FROM user WHERE id=?")) {
+ stmt.setInt(1, 1);
+ try (ResultSet resultSet = stmt.executeQuery()) {
+ }
+ }
+ try (PreparedStatement stmt = conn.prepareStatement("SELECT * FROM user")) {
+ try (ResultSet resultSet = stmt.executeQuery()) {
+ }
+ }
+ }
+
+Then, you got automatic EXPLAIN.
+
+ Query: SELECT * FROM user WHERE id=1
+ ┌────────────────────────────────────────┐
+ │ PLAN │
+ ├────────────────────────────────────────┤
+ │ SELECT │
+ │ USER.ID, │
+ │ USER.NAME │
+ │ FROM PUBLIC.USER │
+ │ /* PUBLIC.PRIMARY_KEY_2: ID = 1 */ │
+ │ WHERE ID = 1 │
+ └────────────────────────────────────────┘
+
+ Query: SELECT * FROM user
+ ┌─────────────────────────────────┐
+ │ PLAN │
+ ├─────────────────────────────────┤
+ │ SELECT │
+ │ USER.ID, │
+ │ USER.NAME │
+ │ FROM PUBLIC.USER │
+ │ /* PUBLIC.USER.tableScan */ │
+ └─────────────────────────────────┘
+
## Use cases
* Record all statements executed by JDBC driver
@@ -35,8 +35,8 @@
* @return Created connection object.
*/
public static Connection newInstance(@NonNull Connection connection,
- @NonNull PreparedStatementListener psl,
- @NonNull ResultSetListener rsl) {
+ /* nullable */ PreparedStatementListener psl,
+ /* nullable */ ResultSetListener rsl) {
return (Connection) Proxy.newProxyInstance(
TracerConnection.class.getClassLoader(),
new Class<?>[]{Connection.class},
Binary file not shown.
@@ -1,10 +1,13 @@
package com.example;
+import de.vandermeer.asciitable.v2.RenderedTable;
+import de.vandermeer.asciitable.v2.V2_AsciiTable;
+import de.vandermeer.asciitable.v2.render.V2_AsciiTableRenderer;
+import de.vandermeer.asciitable.v2.render.WidthLongestLine;
+import de.vandermeer.asciitable.v2.themes.V2_E_TableThemes;
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 {
@@ -25,11 +28,18 @@ private static void doMain() throws SQLException {
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")));
+ V2_AsciiTable at = new V2_AsciiTable();
+ at.addRule();
+ at.addRow((Object[]) header);
+ at.addRule();
+ rows.forEach(at::addRow);
+ at.addRule();
+
+ V2_AsciiTableRenderer rend = new V2_AsciiTableRenderer();
+ rend.setTheme(V2_E_TableThemes.UTF_LIGHT.get());
+ rend.setWidth(new WidthLongestLine());
+ RenderedTable render = rend.render(at);
+ System.err.println(render.toString());
});
try (Connection conn = DriverManager.getConnection("jdbc:querylog:h2:mem:test")) {

0 comments on commit f0e33dc

Please sign in to comment.