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

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

上级 88e2a51c
......@@ -274,7 +274,7 @@ public abstract class Command implements CommandInterface {
}
public String toString() {
return sql + getParameters();
return sql + Trace.formatParams(getParameters());
}
public boolean isCacheable() {
......
......@@ -244,7 +244,7 @@ public class CommandRemote implements CommandInterface {
}
public String toString() {
return sql + getParameters();
return sql + Trace.formatParams(getParameters());
}
public int getCommandType() {
......
......@@ -13,6 +13,7 @@ import org.h2.engine.Session;
import org.h2.expression.Expression;
import org.h2.expression.Parameter;
import org.h2.message.DbException;
import org.h2.message.Trace;
import org.h2.result.ResultInterface;
import org.h2.util.StatementBuilder;
import org.h2.value.Value;
......@@ -307,19 +308,7 @@ public abstract class Prepared {
void trace(long startTime, int count) {
if (session.getTrace().isInfoEnabled()) {
long time = System.currentTimeMillis() - startTime;
String params;
if (parameters.size() > 0) {
StatementBuilder buff = new StatementBuilder(" {");
int i = 0;
for (Expression e : parameters) {
buff.appendExceptFirst(", ");
Value v = e.getValue(session);
buff.append(++i).append(": ").append(v.getTraceSQL());
}
params = buff.append('}').toString();
} else {
params = "";
}
String params = Trace.formatParams(parameters);
session.getTrace().infoSQL(sqlStatement, params, count, time);
}
}
......
......@@ -45,6 +45,7 @@ public class TestPreparedStatement extends TestBase {
deleteDb("preparedStatement");
Connection conn = getConnection("preparedStatement");
testToString(conn);
testExecuteUpdateCall(conn);
testPrepareExecute(conn);
testUUID(conn);
......@@ -79,6 +80,15 @@ public class TestPreparedStatement extends TestBase {
deleteDb("preparedStatement");
}
private void testToString(Connection conn) throws SQLException {
PreparedStatement prep = conn.prepareStatement("call 1");
assertEquals("prep0: call 1", prep.toString());
prep = conn.prepareStatement("call ?");
assertEquals("prep1: call ? {1: NULL}", prep.toString());
prep.setString(1, "Hello World");
assertEquals("prep1: call ? {1: 'Hello World'}", prep.toString());
}
private void testExecuteUpdateCall(Connection conn) throws SQLException {
Statement stat = conn.createStatement();
try {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论