ProfilingPreparedStatementHandler(PreparedStatement statement, String sql) { this.statement = statement; this.sql = sql; sqlParams = new Object[SqlLogFormatter.countArguments(sql)]; }
@Override public Connection getConnection(String login, String password) throws SQLException { return connectionInterceptor.getConnection(this, login, password); }
private static void enableSqlLogging(ProfiledDataSource ds, boolean enable) { ds.setConnectionInterceptor(enable ? ProfiledConnectionInterceptor.INSTANCE : NullConnectionInterceptor.INSTANCE); }
@Override public Object invoke(Object target, Method method, Object[] args) throws Throwable { Object result = InvocationUtils.invokeQuietly(connection, method, args); if ("prepareStatement".equals(method.getName())) { PreparedStatement statement = (PreparedStatement) result; String sql = (String) args[0]; return buildStatementProxy(PreparedStatement.class, new ProfilingPreparedStatementHandler(statement, sql)); } if ("createStatement".equals(method.getName())) { Statement statement = (Statement) result; return buildStatementProxy(Statement.class, new ProfilingStatementHandler(statement)); } return result; }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().startsWith("execute")) { Profiler profiler = Profiler.create(ProfiledDataSource.SQL_LOGGER).start(); Object result = null; try { result = InvocationUtils.invokeQuietly(statement, method, args); } finally { profiler.addContext("sql", SqlLogFormatter.reformatSql(sql)); if (sqlParams.length > 0) { profiler.addContext("params", SqlLogFormatter.reformatParams(sqlParams)); } profiler.stopTrace(""); } return result; } else if (method.getName().startsWith("set") && args.length > 1) { sqlParams[(int) args[0] - 1] = args[1]; return InvocationUtils.invokeQuietly(statement, method, args); } else { return InvocationUtils.invokeQuietly(statement, method, args); } }
@Override public Connection getConnection(BasicDataSource dataSource) throws SQLException { return buildConnectionProxy(new ProfilingConnectionHandler(dataSource.getConnection())); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().startsWith("execute")) { Profiler profiler = Profiler.create(ProfiledDataSource.SQL_LOGGER).start(); Object result = null; try { result = InvocationUtils.invokeQuietly(statement, method, args); } finally { String sql = (String) args[0]; profiler.addContext("sql", SqlLogFormatter.reformatSql(sql)); profiler.stopTrace(""); } return result; } else { return InvocationUtils.invokeQuietly(statement, method, args); } } }
public static String reformatParams(Object[] params) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < params.length; i++) { if (i > 0) { sb.append(", "); } sb.append(reformatParam(params[i])); } return sb.toString(); }
private void checkConnection() { Connection connection = null; try { connection = datasource.getConnection(); dialect.init(connection.getMetaData()); } catch (SQLException e) { throw new IllegalStateException("Can not connect to database. Please check connectivity and settings (see the properties prefixed by 'sonar.jdbc.').", e); } finally { DbUtils.closeQuietly(connection); } }
@Test public void delegate_to_underlying_data_source() throws Exception { ProfiledDataSource proxy = new ProfiledDataSource(originDataSource, ProfiledConnectionInterceptor.INSTANCE); // painful to call all methods // so using reflection to check that calls does not fail // Limitation: methods with parameters are not tested and calls to // underlying datasource are not verified for (Method method : ProfiledDataSource.class.getDeclaredMethods()) { if (method.getParameterTypes().length == 0 && Modifier.isPublic(method.getModifiers())) { method.invoke(proxy); } } } }
@Test public void reformatParams_returns_blank_if_zero_params() { String formattedParams = SqlLogFormatter.reformatParams(new Object[0]); assertThat(formattedParams).isEqualTo(""); }
@Override public void stop() { if (datasource != null) { try { datasource.close(); } catch (SQLException e) { throw new IllegalStateException("Fail to stop JDBC connection pool", e); } } }
@Test public void reformatSql() { assertThat(SqlLogFormatter.reformatSql("")).isEqualTo(""); assertThat(SqlLogFormatter.reformatSql("select *")).isEqualTo("select *"); assertThat(SqlLogFormatter.reformatSql("select *\nfrom issues")).isEqualTo("select * from issues"); assertThat(SqlLogFormatter.reformatSql("select *\n from issues")).isEqualTo("select * from issues"); assertThat(SqlLogFormatter.reformatSql("select *\n from issues")).isEqualTo("select * from issues"); assertThat(SqlLogFormatter.reformatSql("select *\n from issues")).isEqualTo("select * from issues"); assertThat(SqlLogFormatter.reformatSql("select *\n\t\t from \tissues")).isEqualTo("select * from issues"); }
@Override public Connection getConnection(BasicDataSource dataSource, String login, String password) throws SQLException { return buildConnectionProxy(new ProfilingConnectionHandler(dataSource.getConnection(login, password))); }
@Test public void reformatParam() { assertThat(SqlLogFormatter.reformatParam(null)).isEqualTo("[null]"); assertThat(SqlLogFormatter.reformatParam("")).isEqualTo(""); assertThat(SqlLogFormatter.reformatParam("foo")).isEqualTo("foo"); assertThat(SqlLogFormatter.reformatParam("foo bar ")).isEqualTo("foo bar "); }
@Test public void countArguments() { assertThat(SqlLogFormatter.countArguments("select * from issues")).isEqualTo(0); assertThat(SqlLogFormatter.countArguments("select * from issues where id=?")).isEqualTo(1); assertThat(SqlLogFormatter.countArguments("select * from issues where id=? and kee=?")).isEqualTo(2); } }
@Override public Connection getConnection() throws SQLException { return connectionInterceptor.getConnection(delegate); }
@Test public void reformatParams() { String formattedParams = SqlLogFormatter.reformatParams(new Object[] {"foo", 42, null, true}); assertThat(formattedParams).isEqualTo("foo, 42, [null], true"); }
@Test public void reformatParam_escapes_newlines() { assertThat(SqlLogFormatter.reformatParam("foo\n bar\nbaz")).isEqualTo("foo\\n bar\\nbaz"); }
@Test public void reformatParam_truncates_if_too_long() { String param = repeat("a", SqlLogFormatter.PARAM_MAX_WIDTH + 10); String formattedParam = SqlLogFormatter.reformatParam(param); assertThat(formattedParam) .hasSize(SqlLogFormatter.PARAM_MAX_WIDTH) .endsWith("...") .startsWith(repeat("a", SqlLogFormatter.PARAM_MAX_WIDTH - 3)); }