|
| 1 | + |
| 2 | +package io.ebeaninternal.server.query; |
| 3 | + |
| 4 | +import io.ebean.util.IOUtils; |
| 5 | +import io.ebean.util.StringHelper; |
| 6 | +import io.ebeaninternal.api.CoreLog; |
| 7 | +import io.ebeaninternal.api.SpiDbQueryPlan; |
| 8 | +import io.ebeaninternal.api.SpiQueryPlan; |
| 9 | +import io.ebeaninternal.server.bind.capture.BindCapture; |
| 10 | + |
| 11 | +import java.io.BufferedReader; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.sql.Connection; |
| 15 | +import java.sql.PreparedStatement; |
| 16 | +import java.sql.ResultSet; |
| 17 | +import java.sql.SQLException; |
| 18 | +import java.sql.Statement; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Random; |
| 21 | + |
| 22 | +import static java.lang.System.Logger.Level.WARNING; |
| 23 | + |
| 24 | +/** |
| 25 | + * A QueryPlanLogger for DB2. |
| 26 | + * <p> |
| 27 | + * To use query plan capturing, you have to install the explain tables with |
| 28 | + * <code>SYSPROC.SYSINSTALLOBJECTS( 'EXPLAIN', 'C' , '', CURRENT SCHEMA )</code>. |
| 29 | + * To do this in a repeatable script, you may use this statement: |
| 30 | + * |
| 31 | + * <pre> |
| 32 | + * BEGIN |
| 33 | + * IF NOT EXISTS (SELECT * FROM SYSCAT.TABLES WHERE TABSCHEMA = CURRENT SCHEMA AND TABNAME = 'EXPLAIN_STREAM') THEN |
| 34 | + * call SYSPROC.SYSINSTALLOBJECTS( 'EXPLAIN', 'C' , '', CURRENT SCHEMA ); |
| 35 | + * END IF; |
| 36 | + * END |
| 37 | + * </pre> |
| 38 | + * |
| 39 | + * @author Roland Praml, FOCONIS AG |
| 40 | + */ |
| 41 | +public final class QueryPlanLoggerDb2 extends QueryPlanLogger { |
| 42 | + |
| 43 | + private Random rnd = new Random(); |
| 44 | + |
| 45 | + private final String schema; |
| 46 | + |
| 47 | + private final boolean create; |
| 48 | + |
| 49 | + private static final String GET_PLAN_TEMPLATE = readReasource("QueryPlanLoggerDb2.sql"); |
| 50 | + |
| 51 | + private static final String CREATE_TEMPLATE = "BEGIN\n" |
| 52 | + + "IF NOT EXISTS (SELECT * FROM SYSCAT.TABLES WHERE TABSCHEMA = ${SCHEMA} AND TABNAME = 'EXPLAIN_STREAM') THEN\n" |
| 53 | + + " CALL SYSPROC.SYSINSTALLOBJECTS( 'EXPLAIN', 'C' , '', ${SCHEMA} );\n" |
| 54 | + + "END IF;\n" |
| 55 | + + "END"; |
| 56 | + |
| 57 | + public QueryPlanLoggerDb2(String opts) { |
| 58 | + Map<String, String> map = StringHelper.delimitedToMap(opts, ";", "="); |
| 59 | + create = !"false" .equals(map.get("create")); // default is create |
| 60 | + String schema = map.get("schema"); // should be null or SYSTOOLS |
| 61 | + if (schema == null || schema.isEmpty()) { |
| 62 | + this.schema = null; |
| 63 | + } else { |
| 64 | + this.schema = schema.toUpperCase(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private static String readReasource(String resName) { |
| 69 | + try (InputStream stream = QueryPlanLoggerDb2.class.getResourceAsStream(resName)) { |
| 70 | + if (stream == null) { |
| 71 | + throw new IllegalStateException("Could not find resource " + resName); |
| 72 | + } |
| 73 | + BufferedReader reader = IOUtils.newReader(stream); |
| 74 | + StringBuilder sb = new StringBuilder(); |
| 75 | + reader.lines().forEach(line -> sb.append(line).append('\n')); |
| 76 | + return sb.toString(); |
| 77 | + } catch (IOException e) { |
| 78 | + throw new IllegalStateException("Could not read resource " + resName, e); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public SpiDbQueryPlan collectPlan(Connection conn, SpiQueryPlan plan, BindCapture bind) { |
| 84 | + try (Statement stmt = conn.createStatement()) { |
| 85 | + if (create) { |
| 86 | + // create explain tables if neccessary |
| 87 | + if (schema == null) { |
| 88 | + stmt.execute(CREATE_TEMPLATE.replace("${SCHEMA}", "CURRENT USER")); |
| 89 | + } else { |
| 90 | + stmt.execute(CREATE_TEMPLATE.replace("${SCHEMA}", "'" + schema + "'")); |
| 91 | + } |
| 92 | + conn.commit(); |
| 93 | + } |
| 94 | + |
| 95 | + try { |
| 96 | + int queryNo = rnd.nextInt(Integer.MAX_VALUE); |
| 97 | + |
| 98 | + String sql = "EXPLAIN PLAN SET QUERYNO = " + queryNo + " FOR " + plan.sql(); |
| 99 | + try (PreparedStatement explainStmt = conn.prepareStatement(sql)) { |
| 100 | + bind.prepare(explainStmt, conn); |
| 101 | + explainStmt.execute(); |
| 102 | + } |
| 103 | + |
| 104 | + sql = schema == null |
| 105 | + ? GET_PLAN_TEMPLATE.replace("${SCHEMA}", conn.getMetaData().getUserName().toUpperCase()) |
| 106 | + : GET_PLAN_TEMPLATE.replace("${SCHEMA}", schema); |
| 107 | + |
| 108 | + try (PreparedStatement pstmt = conn.prepareStatement(sql)) { |
| 109 | + pstmt.setInt(1, queryNo); |
| 110 | + try (ResultSet rset = pstmt.executeQuery()) { |
| 111 | + return readQueryPlan(plan, bind, rset); |
| 112 | + } |
| 113 | + } |
| 114 | + } finally { |
| 115 | + conn.rollback(); // do not keep query plans in DB |
| 116 | + } |
| 117 | + } catch (SQLException e) { |
| 118 | + CoreLog.log.log(WARNING, "Could not log query plan", e); |
| 119 | + return null; |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments