提交 6bb00084 authored 作者: Thomas Mueller's avatar Thomas Mueller

The exception message of failed INSERT or MERGE statements now includes all…

The exception message of failed INSERT or MERGE statements now includes all values and the row number.
上级 7146e176
...@@ -15,6 +15,7 @@ import org.h2.engine.Session; ...@@ -15,6 +15,7 @@ import org.h2.engine.Session;
import org.h2.expression.Expression; import org.h2.expression.Expression;
import org.h2.expression.Parameter; import org.h2.expression.Parameter;
import org.h2.index.Index; import org.h2.index.Index;
import org.h2.jdbc.JdbcSQLException;
import org.h2.message.Message; import org.h2.message.Message;
import org.h2.result.LocalResult; import org.h2.result.LocalResult;
import org.h2.util.ObjectArray; import org.h2.util.ObjectArray;
...@@ -367,4 +368,47 @@ public abstract class Prepared { ...@@ -367,4 +368,47 @@ public abstract class Prepared {
return sqlStatement; return sqlStatement;
} }
protected String getSQL(Value[] values) {
StringBuffer buff = new StringBuffer();
for (int i = 0; i < values.length; i++) {
if (i > 0) {
buff.append(", ");
}
Value v = values[i];
if (v != null) {
buff.append(v.getSQL());
}
}
return buff.toString();
}
protected String getSQL(Expression[] list) {
StringBuffer buff = new StringBuffer();
for (int i = 0; i < list.length; i++) {
if (i > 0) {
buff.append(", ");
}
Expression e = list[i];
if (e != null) {
buff.append(e.getSQL());
}
}
return buff.toString();
}
protected SQLException setRow(SQLException ex, int rowId, String values) {
if (ex instanceof JdbcSQLException) {
JdbcSQLException e = (JdbcSQLException) ex;
StringBuffer buff = new StringBuffer("VALUES(");
buff.append(values).append(")");
if (rowId > 0) {
buff.append(" -- row #");
buff.append(rowId + 1);
}
e.setSQL(buff.toString());
}
return ex;
}
} }
...@@ -84,8 +84,12 @@ public class Insert extends Prepared { ...@@ -84,8 +84,12 @@ public class Insert extends Prepared {
if (e != null) { if (e != null) {
// e can be null (DEFAULT) // e can be null (DEFAULT)
e = e.optimize(session); e = e.optimize(session);
Value v = e.getValue(session).convertTo(c.getType()); try {
newRow.setValue(index, v); Value v = e.getValue(session).convertTo(c.getType());
newRow.setValue(index, v);
} catch (SQLException ex) {
throw setRow(ex, x, getSQL(expr));
}
} }
} }
checkCanceled(); checkCanceled();
...@@ -113,8 +117,12 @@ public class Insert extends Prepared { ...@@ -113,8 +117,12 @@ public class Insert extends Prepared {
for (int j = 0; j < columns.length; j++) { for (int j = 0; j < columns.length; j++) {
Column c = columns[j]; Column c = columns[j];
int index = c.getColumnId(); int index = c.getColumnId();
Value v = r[j].convertTo(c.getType()); try {
newRow.setValue(index, v); Value v = r[j].convertTo(c.getType());
newRow.setValue(index, v);
} catch (SQLException ex) {
throw setRow(ex, count, getSQL(r));
}
} }
table.validateConvertUpdateSequence(session, newRow); table.validateConvertUpdateSequence(session, newRow);
table.fireBeforeRow(session, null, newRow); table.fireBeforeRow(session, null, newRow);
......
...@@ -118,8 +118,12 @@ public class Merge extends Prepared { ...@@ -118,8 +118,12 @@ public class Merge extends Prepared {
Expression e = expr[i]; Expression e = expr[i];
if (e != null) { if (e != null) {
// e can be null (DEFAULT) // e can be null (DEFAULT)
Value v = expr[i].getValue(session).convertTo(c.getType()); try {
newRow.setValue(index, v); Value v = expr[i].getValue(session).convertTo(c.getType());
newRow.setValue(index, v);
} catch (SQLException ex) {
throw setRow(ex, count, getSQL(expr));
}
} }
} }
merge(newRow); merge(newRow);
...@@ -139,8 +143,12 @@ public class Merge extends Prepared { ...@@ -139,8 +143,12 @@ public class Merge extends Prepared {
for (int j = 0; j < columns.length; j++) { for (int j = 0; j < columns.length; j++) {
Column c = columns[j]; Column c = columns[j];
int index = c.getColumnId(); int index = c.getColumnId();
Value v = r[j].convertTo(c.getType()); try {
newRow.setValue(index, v); Value v = r[j].convertTo(c.getType());
newRow.setValue(index, v);
} catch (SQLException ex) {
throw setRow(ex, count, getSQL(r));
}
} }
merge(newRow); merge(newRow);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论