📄 profiledconnection.java
字号:
} } swap(entries, first, index); quickSort(entries, sortByTime, first, index - 1); quickSort(entries, sortByTime, index + 1, last); } private static void swap(Object[] list, int i, int j) { Object tmp = list[i]; list[i] = list[j]; list[j] = tmp; } private static String removeQueryValues(String _sql) { int length = _sql.length(); if (_sql.indexOf("=") == -1) { return _sql; } StringBuilder sql = new StringBuilder(_sql); boolean inValue = false; boolean afterEquals = false; boolean hasQuotes = false; int startValue = -1; int endValue = -1; int charRemoved = 0; for (int x = 0; x < length; x++) { char c = _sql.charAt(x); switch (c) { case '=': { if (!afterEquals) { afterEquals = true; } break; } case ' ': { if (!hasQuotes && inValue) { endValue = x; inValue = false; hasQuotes = false; afterEquals = false; } break; } case '\'': { if (afterEquals && !inValue) { startValue = x; inValue = true; hasQuotes = true; } else if (afterEquals && inValue && hasQuotes) { endValue = x + 1; inValue = false; hasQuotes = false; afterEquals = false; } break; } case '-': { if (afterEquals && !inValue) { startValue = x; inValue = true; } break; } case '+': { if (afterEquals && !inValue) { startValue = x; inValue = true; } break; } case '0': { if (afterEquals && !inValue) { startValue = x; inValue = true; } break; } case '1': { if (afterEquals && !inValue) { startValue = x; inValue = true; } break; } case '2': { if (afterEquals && !inValue) { startValue = x; inValue = true; } break; } case '3': { if (afterEquals && !inValue) { startValue = x; inValue = true; } break; } case '4': { if (afterEquals && !inValue) { startValue = x; inValue = true; } break; } case '5': { if (afterEquals && !inValue) { startValue = x; inValue = true; } break; } case '6': { if (afterEquals && !inValue) { startValue = x; inValue = true; } break; } case '7': { if (afterEquals && !inValue) { startValue = x; inValue = true; } break; } case '8': { if (afterEquals && !inValue) { startValue = x; inValue = true; } break; } case '9': { if (afterEquals && !inValue) { startValue = x; inValue = true; } break; } default: { if (afterEquals && !inValue) { afterEquals = false; } } } if (x == length - 1 && afterEquals) { endValue = x + 1; } if (startValue != -1 && endValue != -1) { sql.replace(startValue - charRemoved, endValue - charRemoved, "?"); charRemoved += endValue - startValue - 1; startValue = -1; endValue = -1; } } return sql.toString(); } private static String reformatQuery(String _sql) { int length = _sql.length(); int charAdded = 0; StringBuilder sql = new StringBuilder(_sql); for (int x = 0; x < length; x++) { char c = _sql.charAt(x); if (c == ',' && x < length - 1 && _sql.charAt(x + 1) != ' ') { sql.replace(x + charAdded, x + 1 + charAdded, ", "); charAdded++; } } return sql.toString(); } //--------------------- Connection Wrapping Code ---------------------// /** * Creates a new ProfiledConnection that wraps the specified connection. * * @param connection the Connection to wrap and collect stats for. */ public ProfiledConnection(Connection connection) { super(connection); } public void close() throws SQLException { // Close underlying connection. if (connection != null) { connection.close(); } } public Statement createStatement() throws SQLException { // Returned a TimedStatement so that we can do db timings. return new TimedStatement(connection.createStatement()); } public PreparedStatement prepareStatement(String sql) throws SQLException { // Returned a TimedPreparedStatement so that we can do db timings. return new TimedPreparedStatement(connection.prepareStatement(sql), sql); } public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { return new TimedStatement(connection.createStatement(resultSetType, resultSetConcurrency)); } public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { return new TimedPreparedStatement(connection.prepareStatement(sql, resultSetType, resultSetConcurrency), sql); } public CallableStatement prepareCall(String sql) throws SQLException { return new TimedCallableStatement(connection.prepareCall(sql), sql); } public CallableStatement prepareCall(String sql, int i, int i1) throws SQLException { return new TimedCallableStatement(connection.prepareCall(sql, i, i1), sql); } /** * An implementation of the Statement interface that wraps an underlying * Statement object and performs timings of the database queries. The class * does not handle batch queries but should generally work otherwise. */ class TimedStatement extends StatementWrapper { private Statement stmt; /** * Creates a new TimedStatement that wraps <tt>stmt</tt>. */ public TimedStatement(Statement stmt) { super(stmt); this.stmt = stmt; } public boolean execute(String sql) throws SQLException { long t1 = System.currentTimeMillis(); boolean result = stmt.execute(sql); long t2 = System.currentTimeMillis(); // determine the type of query String sqlL = sql.toLowerCase().trim(); if (sqlL.startsWith("insert")) { addQuery(INSERT, sql, t2 - t1); } else if (sqlL.startsWith("update")) { addQuery(UPDATE, sql, t2 - t1); } else if (sqlL.startsWith("delete")) { addQuery(DELETE, sql, t2 - t1); } else { addQuery(SELECT, sql, t2 - t1); } return result; } public ResultSet executeQuery(String sql) throws SQLException { long t1 = System.currentTimeMillis(); ResultSet result = stmt.executeQuery(sql); long t2 = System.currentTimeMillis(); // determine the type of query String sqlL = sql.toLowerCase().trim(); if (sqlL.startsWith("insert")) { addQuery(INSERT, sql, t2 - t1); } else if (sqlL.startsWith("update")) { addQuery(UPDATE, sql, t2 - t1); } else if (sqlL.startsWith("delete")) { addQuery(DELETE, sql, t2 - t1); } else { addQuery(SELECT, sql, t2 - t1); } return result; } public int executeUpdate(String sql) throws SQLException { long t1 = System.currentTimeMillis(); int result = stmt.executeUpdate(sql); long t2 = System.currentTimeMillis(); // determine the type of query String sqlL = sql.toLowerCase().trim(); if (sqlL.startsWith("insert")) { addQuery(INSERT, sql, t2 - t1); } else if (sqlL.startsWith("update")) { addQuery(UPDATE, sql, t2 - t1); } else if (sqlL.startsWith("delete")) { addQuery(DELETE, sql, t2 - t1); } else { addQuery(SELECT, sql, t2 - t1); } return result; } } /** * An implementation of the PreparedStatement interface that wraps an * underlying PreparedStatement object and performs timings of the database * queries. */ class TimedPreparedStatement extends PreparedStatementWrapper { private String sql; private int type = SELECT; public TimedPreparedStatement(PreparedStatement pstmt, String sql) { super(pstmt); this.sql = sql; // determine the type of query String sqlL = sql.toLowerCase().trim(); if (sqlL.startsWith("insert")) { type = INSERT; } else if (sqlL.startsWith("update")) { type = UPDATE; } else if (sqlL.startsWith("delete")) { type = DELETE; } else { type = SELECT; } } public boolean execute() throws SQLException { // Perform timing of this method. long t1 = System.currentTimeMillis(); boolean result = pstmt.execute(); long t2 = System.currentTimeMillis();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -