#737 bindValue bug fix

This commit is contained in:
Woonduk Kang
2015-07-17 14:34:40 +09:00
parent 4e969e7c5a
commit 9c8c6bdcb8
5 changed files with 136 additions and 29 deletions
@@ -18,14 +18,51 @@ package com.navercorp.pinpoint.plugin.jdbc.common.bindvalue;
import com.navercorp.pinpoint.bootstrap.util.StringUtils;
import java.util.Map;
/**
* duplicate : com.navercorp.pinpoint.profiler.modifier.db.interceptor.BindValueUtils
* @author emeroad
*/
public class BindValueUtils {
public final class BindValueUtils {
private BindValueUtils() {
}
public static String bindValueToString(final Map<Integer, String> bindValueMap, int limit) {
if (bindValueMap == null) {
return "";
}
if (bindValueMap.isEmpty()) {
return "";
}
final int maxParameterIndex = getMaxParameterIndex(bindValueMap);
if (maxParameterIndex <= 0) {
return "";
}
final String[] temp = new String[maxParameterIndex];
for (Map.Entry<Integer, String> entry : bindValueMap.entrySet()) {
final int parameterIndex = entry.getKey() - 1;
if (parameterIndex < 0) {
// invalid index. PreparedStatement first parameterIndex is 1
continue;
}
if (temp.length <= parameterIndex) {
continue;
}
temp[parameterIndex] = entry.getValue();
}
return bindValueToString(temp, limit);
}
private static int getMaxParameterIndex(Map<Integer, String> bindValueMap) {
int maxIndex = 0;
for (Integer idx : bindValueMap.keySet()) {
maxIndex = Math.max(maxIndex, idx);
}
return maxIndex;
}
public static String bindValueToString(String[] bindValueArray, int limit) {
if (bindValueArray == null) {
return "";
@@ -39,7 +76,8 @@ public class BindValueUtils {
appendLength(sb, length);
break;
}
StringUtils.appendDrop(sb, bindValueArray[i], limit);
final String bindValue = StringUtils.defaultString(bindValueArray[i], "");
StringUtils.appendDrop(sb, bindValue, limit);
if (i < end) {
sb.append(", ");
}
@@ -130,17 +130,7 @@ public class PreparedStatementExecuteQueryInterceptor implements SimpleAroundInt
}
private String toBindVariable(Map<Integer, String> bindValue) {
final String[] temp = new String[bindValue.size()];
for (Map.Entry<Integer, String> entry : bindValue.entrySet()) {
Integer key = entry.getKey() - 1;
if (temp.length <= key) {
continue;
}
temp[key] = entry.getValue();
}
return BindValueUtils.bindValueToString(temp, maxSqlBindValueLength);
return BindValueUtils.bindValueToString(bindValue, maxSqlBindValueLength);
}
@Override
@@ -18,14 +18,50 @@ package com.navercorp.pinpoint.profiler.modifier.db.interceptor;
import com.navercorp.pinpoint.bootstrap.util.StringUtils;
import java.util.Map;
/**
* @author emeroad
*/
public class BindValueUtils {
public final class BindValueUtils {
private BindValueUtils() {
}
public static String bindValueToString(final Map<Integer, String> bindValueMap, int limit) {
if (bindValueMap == null) {
return "";
}
if (bindValueMap.isEmpty()) {
return "";
}
final int maxParameterIndex = getMaxParameterIndex(bindValueMap);
if (maxParameterIndex <= 0) {
return "";
}
final String[] temp = new String[maxParameterIndex];
for (Map.Entry<Integer, String> entry : bindValueMap.entrySet()) {
final int parameterIndex = entry.getKey() - 1;
if (parameterIndex < 0) {
// invalid index. PreparedStatement first parameterIndex is 1
continue;
}
if (temp.length <= parameterIndex) {
continue;
}
temp[parameterIndex] = entry.getValue();
}
return bindValueToString(temp, limit);
}
private static int getMaxParameterIndex(Map<Integer, String> bindValueMap) {
int maxIndex = 0;
for (Integer idx : bindValueMap.keySet()) {
maxIndex = Math.max(maxIndex, idx);
}
return maxIndex;
}
public static String bindValueToString(String[] bindValueArray, int limit) {
if (bindValueArray == null) {
return "";
@@ -39,7 +75,8 @@ public class BindValueUtils {
appendLength(sb, length);
break;
}
StringUtils.appendDrop(sb, bindValueArray[i], limit);
final String bindValue = StringUtils.defaultString(bindValueArray[i], "");
StringUtils.appendDrop(sb, bindValue, limit);
if (i < end) {
sb.append(", ");
}
@@ -106,17 +106,7 @@ public class PreparedStatementExecuteQueryInterceptor implements SimpleAroundInt
}
private String toBindVariable(Map<Integer, String> bindValue) {
final String[] temp = new String[bindValue.size()];
for (Map.Entry<Integer, String> entry : bindValue.entrySet()) {
Integer key = entry.getKey() - 1;
if (temp.length < key) {
continue;
}
temp[key] = entry.getValue();
}
return BindValueUtils.bindValueToString(temp, maxSqlBindValueLength);
return BindValueUtils.bindValueToString(bindValue, maxSqlBindValueLength);
}
@Override
@@ -17,10 +17,10 @@
package com.navercorp.pinpoint.profiler.modifier.db.interceptor;
import org.junit.Assert;
import org.junit.Test;
import com.navercorp.pinpoint.profiler.modifier.db.interceptor.BindValueUtils;
import java.util.HashMap;
import java.util.Map;
public class BindValueUtilsTest {
@@ -85,7 +85,7 @@ public class BindValueUtilsTest {
@Test
public void testBindValueToString_null() throws Exception {
String result = BindValueUtils.bindValueToString(null, 10);
String result = BindValueUtils.bindValueToString((String[])null, 10);
Assert.assertEquals("", result);
}
@@ -109,4 +109,56 @@ public class BindValueUtilsTest {
String result = BindValueUtils.bindValueToString(bindValue, 5);
Assert.assertEquals("12345...(6), ...(2)", result);
}
// #737 https://github.com/naver/pinpoint/issues/737
@Test
public void test_734_bug_regression() throws Exception {
Map<Integer, String> bindValue = new HashMap<Integer, String>();
bindValue.put(1, "1");
bindValue.put(2, "2");
// skip 3
bindValue.put(4, "4");
String bindValueToString = BindValueUtils.bindValueToString(bindValue, 100);
Assert.assertEquals("1, 2, , 4", bindValueToString);
}
@Test
public void test_index_error_zero() throws Exception {
Map<Integer, String> bindValue = new HashMap<Integer, String>();
bindValue.put(0, "0");
String bindValueToString = BindValueUtils.bindValueToString(bindValue, 100);
Assert.assertEquals("", bindValueToString);
}
@Test
public void test_index_error_negative() throws Exception {
Map<Integer, String> bindValue = new HashMap<Integer, String>();
bindValue.put(-2, "-2");
String bindValueToString = BindValueUtils.bindValueToString(bindValue, 100);
Assert.assertEquals("", bindValueToString);
}
@Test
public void test_index_error_complex() throws Exception {
Map<Integer, String> bindValue = new HashMap<Integer, String>();
bindValue.put(-2, "-2");
bindValue.put(0, "0");
bindValue.put(1, "1");
bindValue.put(3, "3");
String bindValueToString = BindValueUtils.bindValueToString(bindValue, 100);
Assert.assertEquals("1, , 3", bindValueToString);
}
@Test
public void test_NullElement() throws Exception {
String[] temp = {"1", null, "3"};
String bindValueToString = BindValueUtils.bindValueToString(temp, 100);
Assert.assertEquals("1, , 3", bindValueToString);
}
}