ProfilingPreparedStatementHandler(PreparedStatement statement, String sql) { this.statement = statement; this.sql = sql; sqlParams = new Object[SqlLogFormatter.countArguments(sql)]; }
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(); }
@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); } }
@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"); }
@Test public void reformatParams_returns_blank_if_zero_params() { String formattedParams = SqlLogFormatter.reformatParams(new Object[0]); assertThat(formattedParams).isEqualTo(""); }
@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.formatSql(sql)); if (sqlParams.length > 0) { profiler.addContext("params", SqlLogFormatter.formatParams(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); } }
public static String formatParams(Object[] params) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < params.length; i++) { if (i > 0) { sb.append(", "); } sb.append(formatParam(params[i])); } return sb.toString(); }
@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.formatSql(sql)); profiler.stopTrace(""); } return result; } else { return InvocationUtils.invokeQuietly(statement, method, args); } } }
@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); } } }
@Test public void reformatParams() { String formattedParams = SqlLogFormatter.reformatParams(new Object[] {"foo", 42, null, true}); assertThat(formattedParams).isEqualTo("foo, 42, [null], true"); }
@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); } }
@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); } }
@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 "); }
@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); } } }
ProfilingPreparedStatementHandler(PreparedStatement statement, String sql) { this.statement = statement; this.sql = sql; sqlParams = new Object[SqlLogFormatter.countArguments(sql)]; }
@Test public void reformatParam_escapes_newlines() { assertThat(SqlLogFormatter.reformatParam("foo\n bar\nbaz")).isEqualTo("foo\\n bar\\nbaz"); }
ProfilingPreparedStatementHandler(PreparedStatement statement, String sql) { this.statement = statement; this.sql = sql; sqlParams = new Object[SqlLogFormatter.countArguments(sql)]; }
@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)); }
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(); }