提交 c1dba0ba authored 作者: Thomas Mueller's avatar Thomas Mueller

PreparedStatement.toString() now includes the parameter values in a human readable format.

...@@ -153,7 +153,7 @@ public abstract class Expression { ...@@ -153,7 +153,7 @@ public abstract class Expression {
/** /**
* Is the value of a parameter set. * Is the value of a parameter set.
* *
* @return if it is set * @return true if set
*/ */
public boolean isValueSet() { public boolean isValueSet() {
return false; return false;
......
...@@ -35,6 +35,13 @@ public interface ParameterInterface { ...@@ -35,6 +35,13 @@ public interface ParameterInterface {
*/ */
void checkSet(); void checkSet();
/**
* Is the value of a parameter set.
*
* @return true if set
*/
boolean isValueSet();
/** /**
* Get the expected data type of the parameter if no value is set, or the * Get the expected data type of the parameter if no value is set, or the
* data type of the value if one is set. * data type of the value if one is set.
......
...@@ -46,6 +46,10 @@ public class ParameterRemote implements ParameterInterface { ...@@ -46,6 +46,10 @@ public class ParameterRemote implements ParameterInterface {
} }
} }
public boolean isValueSet() {
return value != null;
}
public int getType() { public int getType() {
return value == null ? dataType : value.getType(); return value == null ? dataType : value.getType();
} }
......
...@@ -12,6 +12,7 @@ import org.h2.constant.SysProperties; ...@@ -12,6 +12,7 @@ import org.h2.constant.SysProperties;
import org.h2.expression.ParameterInterface; import org.h2.expression.ParameterInterface;
import org.h2.util.StatementBuilder; import org.h2.util.StatementBuilder;
import org.h2.util.StringUtils; import org.h2.util.StringUtils;
import org.h2.value.Value;
/** /**
* This class represents a trace module. * This class represents a trace module.
...@@ -216,13 +217,24 @@ public class Trace { ...@@ -216,13 +217,24 @@ public class Trace {
if (parameters.size() == 0) { if (parameters.size() == 0) {
return ""; return "";
} }
StatementBuilder buff = new StatementBuilder(" {"); StatementBuilder buff = new StatementBuilder();
int i = 0; int i = 0;
boolean params = false;
for (ParameterInterface p : parameters) { for (ParameterInterface p : parameters) {
if (p.isValueSet()) {
if (!params) {
buff.append(" {");
params = true;
}
buff.appendExceptFirst(", "); buff.appendExceptFirst(", ");
buff.append(++i).append(": ").append(p.getParamValue().getTraceSQL()); Value v = p.getParamValue();
buff.append(++i).append(": ").append(v.getTraceSQL());
}
}
if (params) {
buff.append('}');
} }
return buff.append('}').toString(); return buff.toString();
} }
/** /**
......
...@@ -42,7 +42,6 @@ public class TestPreparedStatement extends TestBase { ...@@ -42,7 +42,6 @@ public class TestPreparedStatement extends TestBase {
} }
public void test() throws Exception { public void test() throws Exception {
deleteDb("preparedStatement"); deleteDb("preparedStatement");
Connection conn = getConnection("preparedStatement"); Connection conn = getConnection("preparedStatement");
testToString(conn); testToString(conn);
...@@ -84,7 +83,7 @@ public class TestPreparedStatement extends TestBase { ...@@ -84,7 +83,7 @@ public class TestPreparedStatement extends TestBase {
PreparedStatement prep = conn.prepareStatement("call 1"); PreparedStatement prep = conn.prepareStatement("call 1");
assertTrue(prep.toString().endsWith(": call 1")); assertTrue(prep.toString().endsWith(": call 1"));
prep = conn.prepareStatement("call ?"); prep = conn.prepareStatement("call ?");
assertTrue(prep.toString().endsWith(": call ? {1: NULL}")); assertTrue(prep.toString().endsWith(": call ?"));
prep.setString(1, "Hello World"); prep.setString(1, "Hello World");
assertTrue(prep.toString().endsWith(": call ? {1: 'Hello World'}")); assertTrue(prep.toString().endsWith(": call ? {1: 'Hello World'}"));
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论