LogSqlBuilder { public String buildDynamicSqlForLog(String sql, Object[] args, int[] types) { boolean endsWithPlacholder = sql.endsWith("?"); String[] parts = sql.split("\\?"); if (endsWithPlacholder) { parts = (String[]) ArrayUtils.addAll(parts, new String[]{""}); } if (parts.length == 1 || args == null || args.length == 0) { return sql; } StringBuilder dynamicSql = new StringBuilder(256); for (int i = 0; i < parts.length; i++) { dynamicSql.append(parts[i]); if (args.length > i) { int type = (types != null && types.length > i) ? types[i] : Types.OTHER; dynamicSql.append(formatValue(args[i], type)); } } return dynamicSql.toString(); }  void logSql(Logger loggerArg, String sql, Object[] args, int[] types, long executionTimeMillis); void logSql(Logger loggerArg, String message, String sql, Object[] args, int[] types, long executionTimeMillis); SQLException logSqlAfterException(Logger loggerArg, String sql, Object[] args, SQLException e); String buildDynamicSqlForLog(String sql, Object[] args, int[] types); String formatValue(Object object, int type); boolean isUseJdbcTimestampFormat(); void setUseJdbcTimestampFormat(boolean useJdbcTimestampFormat); int getLogSlowSqlThresholdMillis(); void setLogSlowSqlThresholdMillis(int logSlowSqlThresholdMillis); boolean isLogSqlParametersInline(); void setLogSqlParametersInline(boolean logSqlParametersInline);  }
@Test public void testNoPlachodlers() { final String SQL = "select * from sym_data where data_id = 234234;"; LogSqlBuilder builder = new LogSqlBuilder(); String result = builder.buildDynamicSqlForLog(SQL, null, null); assertEquals(SQL, result); }
@Test public void testSinglePlachodler() { final String SQL = "select * from sym_data where data_id = ?"; LogSqlBuilder builder = new LogSqlBuilder(); String result = builder.buildDynamicSqlForLog(SQL, new Object[] {Integer.valueOf(234234)}, new int[] {Types.BIGINT}); System.out.println(result); assertEquals("select * from sym_data where data_id = 234234", result); }
@Test public void testSinglePlachodlerNoTypes() { final String SQL = "select * from sym_data where data_id = ?"; LogSqlBuilder builder = new LogSqlBuilder(); String result = builder.buildDynamicSqlForLog(SQL, new Object[] {Integer.valueOf(234234)}, null); System.out.println(result); assertEquals("select * from sym_data where data_id = 234234", result); }
@Test public void testSinglePlachodlerNoTypesString() { final String SQL = "select * from sym_data where data_id = ?"; LogSqlBuilder builder = new LogSqlBuilder(); String result = builder.buildDynamicSqlForLog(SQL, new Object[] {"234234"}, null); System.out.println(result); assertEquals("select * from sym_data where data_id = '234234'", result); }
@Test public void testMultiPlaceholders() { final String SQL = "select * from sym_data where data_id between ? and ?"; LogSqlBuilder builder = new LogSqlBuilder(); String result = builder.buildDynamicSqlForLog(SQL, new Object[] {12,21}, new int[] {Types.BIGINT, Types.BIGINT}); System.out.println(result); assertEquals("select * from sym_data where data_id between 12 and 21", result); }
@Test public void testTypes() { final String SQL = "update sym_data set data = ? where data_id =? and create_time > ? and table_name = ? and time>=? and blob_colum = ?"; LogSqlBuilder builder = new LogSqlBuilder(); Date date = FormatUtils.parseDate("2016-04-20 17:12:57", FormatUtils.TIMESTAMP_PATTERNS); String result = builder.buildDynamicSqlForLog(SQL, new Object[] {"\"002\",\"hostname\"",21, date, "sym_node_host", date, new byte[] {0,2,8}}, new int[] {Types.CLOB, Types.BIGINT, Types.TIMESTAMP, Types.VARCHAR, Types.TIME, Types.BINARY}); System.out.println(result); assertEquals("update sym_data set data = '\"002\",\"hostname\"' where data_id =21 and create_time > {ts '2016-04-20 17:12:57.000'} and table_name = 'sym_node_host' and time>={ts '17:12:57.000'} and blob_colum = '000208'", result); }
@Test public void testEscapes() { final String SQL = "update sym_data set table_name = ?"; LogSqlBuilder builder = new LogSqlBuilder(); String result = builder.buildDynamicSqlForLog(SQL, new Object[] {"'?\\/"} , new int[] {Types.CLOB}); System.out.println(result); assertEquals("update sym_data set table_name = '''?\\/'", result); }