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

Improved error message when casting a value failed.

上级 2a199167
...@@ -105,7 +105,7 @@ public class Insert extends Prepared { ...@@ -105,7 +105,7 @@ public class Insert extends Prepared {
// e can be null (DEFAULT) // e can be null (DEFAULT)
e = e.optimize(session); e = e.optimize(session);
try { try {
Value v = e.getValue(session).convertTo(c.getType()); Value v = c.convert(e.getValue(session));
newRow.setValue(index, v); newRow.setValue(index, v);
} catch (SQLException ex) { } catch (SQLException ex) {
throw setRow(ex, x, getSQL(expr)); throw setRow(ex, x, getSQL(expr));
...@@ -136,7 +136,7 @@ public class Insert extends Prepared { ...@@ -136,7 +136,7 @@ public class Insert extends Prepared {
Column c = columns[j]; Column c = columns[j];
int index = c.getColumnId(); int index = c.getColumnId();
try { try {
Value v = r[j].convertTo(c.getType()); Value v = c.convert(r[j]);
newRow.setValue(index, v); newRow.setValue(index, v);
} catch (SQLException ex) { } catch (SQLException ex) {
throw setRow(ex, count, getSQL(r)); throw setRow(ex, count, getSQL(r));
......
...@@ -116,7 +116,7 @@ public class Merge extends Prepared { ...@@ -116,7 +116,7 @@ public class Merge extends Prepared {
if (e != null) { if (e != null) {
// e can be null (DEFAULT) // e can be null (DEFAULT)
try { try {
Value v = e.getValue(session).convertTo(c.getType()); Value v = c.convert(e.getValue(session));
newRow.setValue(index, v); newRow.setValue(index, v);
} catch (SQLException ex) { } catch (SQLException ex) {
throw setRow(ex, count, getSQL(expr)); throw setRow(ex, count, getSQL(expr));
...@@ -140,7 +140,7 @@ public class Merge extends Prepared { ...@@ -140,7 +140,7 @@ public class Merge extends Prepared {
Column c = columns[j]; Column c = columns[j];
int index = c.getColumnId(); int index = c.getColumnId();
try { try {
Value v = r[j].convertTo(c.getType()); Value v = c.convert(r[j]);
newRow.setValue(index, v); newRow.setValue(index, v);
} catch (SQLException ex) { } catch (SQLException ex) {
throw setRow(ex, count, getSQL(r)); throw setRow(ex, count, getSQL(r));
......
...@@ -99,7 +99,7 @@ public class Update extends Prepared { ...@@ -99,7 +99,7 @@ public class Update extends Prepared {
newValue = table.getDefaultValue(session, column); newValue = table.getDefaultValue(session, column);
} else { } else {
Column column = table.getColumn(i); Column column = table.getColumn(i);
newValue = newExpr.getValue(session).convertTo(column.getType()); newValue = column.convert(newExpr.getValue(session));
} }
newRow.setValue(i, newValue); newRow.setValue(i, newValue);
} }
......
...@@ -321,7 +321,7 @@ public class ConstraintReferential extends Constraint { ...@@ -321,7 +321,7 @@ public class ConstraintReferential extends Constraint {
Value v = newRow.getValue(idx); Value v = newRow.getValue(idx);
Column refCol = refColumns[i].column; Column refCol = refColumns[i].column;
int refIdx = refCol.getColumnId(); int refIdx = refCol.getColumnId();
check.setValue(refIdx, v.convertTo(refCol.getType())); check.setValue(refIdx, refCol.convert(v));
} }
if (!found(session, refIndex, check, null)) { if (!found(session, refIndex, check, null)) {
throw Message.getSQLException(ErrorCode.REFERENTIAL_INTEGRITY_VIOLATED_PARENT_MISSING_1, throw Message.getSQLException(ErrorCode.REFERENTIAL_INTEGRITY_VIOLATED_PARENT_MISSING_1,
...@@ -367,9 +367,8 @@ public class ConstraintReferential extends Constraint { ...@@ -367,9 +367,8 @@ public class ConstraintReferential extends Constraint {
Column refCol = refColumns[i].column; Column refCol = refColumns[i].column;
int refIdx = refCol.getColumnId(); int refIdx = refCol.getColumnId();
Column col = columns[i].column; Column col = columns[i].column;
int idx = col.getColumnId(); Value v = col.convert(oldRow.getValue(refIdx));
Value v = oldRow.getValue(refIdx).convertTo(col.getType()); check.setValue(col.getColumnId(), v);
check.setValue(idx, v);
} }
// exclude the row only for self-referencing constraints // exclude the row only for self-referencing constraints
Row excluding = (refTable == table) ? oldRow : null; Row excluding = (refTable == table) ? oldRow : null;
......
...@@ -110,7 +110,7 @@ public class TableFunction extends Function { ...@@ -110,7 +110,7 @@ public class TableFunction extends Function {
} else { } else {
Column c = columnList[j]; Column c = columnList[j];
v = l[row]; v = l[row];
v = v.convertTo(c.getType()); v = c.convert(v);
v = v.convertPrecision(c.getPrecision()); v = v.convertPrecision(c.getPrecision());
v = v.convertScale(true, c.getScale()); v = v.convertScale(true, c.getScale());
} }
......
...@@ -128,10 +128,9 @@ public class IndexCondition { ...@@ -128,10 +128,9 @@ public class IndexCondition {
*/ */
public Value[] getCurrentValueList(Session session) throws SQLException { public Value[] getCurrentValueList(Session session) throws SQLException {
HashSet<Value> valueSet = new HashSet<Value>(); HashSet<Value> valueSet = new HashSet<Value>();
int dataType = column.getType();
for (Expression e : expressionList) { for (Expression e : expressionList) {
Value v = e.getValue(session); Value v = e.getValue(session);
v = v.convertTo(dataType); v = column.convert(v);
valueSet.add(v); valueSet.add(v);
} }
Value[] array = new Value[valueSet.size()]; Value[] array = new Value[valueSet.size()];
......
...@@ -75,8 +75,6 @@ public class IndexCursor implements Cursor { ...@@ -75,8 +75,6 @@ public class IndexCursor implements Cursor {
break; break;
} }
Column column = condition.getColumn(); Column column = condition.getColumn();
int type = column.getType();
int id = column.getColumnId();
if (condition.getCompareType() == Comparison.IN_LIST) { if (condition.getCompareType() == Comparison.IN_LIST) {
this.inColumn = column; this.inColumn = column;
inList = condition.getCurrentValueList(session); inList = condition.getCurrentValueList(session);
...@@ -87,9 +85,10 @@ public class IndexCursor implements Cursor { ...@@ -87,9 +85,10 @@ public class IndexCursor implements Cursor {
inResult = condition.getCurrentResult(session); inResult = condition.getCurrentResult(session);
return; return;
} else { } else {
Value v = condition.getCurrentValue(session).convertTo(type); Value v = column.convert(condition.getCurrentValue(session));
boolean isStart = condition.isStart(); boolean isStart = condition.isStart();
boolean isEnd = condition.isEnd(); boolean isEnd = condition.isEnd();
int id = column.getColumnId();
IndexColumn idxCol = indexColumns[id]; IndexColumn idxCol = indexColumns[id];
if (idxCol != null && (idxCol.sortType & SortOrder.DESCENDING) != 0) { if (idxCol != null && (idxCol.sortType & SortOrder.DESCENDING) != 0) {
// if the index column is sorted the other way, we swap end and start // if the index column is sorted the other way, we swap end and start
...@@ -179,7 +178,7 @@ public class IndexCursor implements Cursor { ...@@ -179,7 +178,7 @@ public class IndexCursor implements Cursor {
} else if (inResult != null) { } else if (inResult != null) {
while (inResult.next()) { while (inResult.next()) {
Value v = inResult.currentRow()[0]; Value v = inResult.currentRow()[0];
v = v.convertTo(inColumn.getType()); v = inColumn.convert(v);
if (inResultTested.add(v)) { if (inResultTested.add(v)) {
find(v); find(v);
break; break;
...@@ -189,7 +188,7 @@ public class IndexCursor implements Cursor { ...@@ -189,7 +188,7 @@ public class IndexCursor implements Cursor {
} }
private void find(Value v) throws SQLException { private void find(Value v) throws SQLException {
v = v.convertTo(inColumn.getType()); v = inColumn.convert(v);
int id = inColumn.getColumnId(); int id = inColumn.getColumnId();
if (start == null) { if (start == null) {
start = table.getTemplateRow(); start = table.getTemplateRow();
......
...@@ -137,6 +137,23 @@ public class Column { ...@@ -137,6 +137,23 @@ public class Column {
return newColumn; return newColumn;
} }
/**
* Convert a value to this column's type.
*
* @param v the value
* @return the value
*/
public Value convert(Value v) throws SQLException {
try {
return v.convertTo(type);
} catch (SQLException e) {
if (e.getErrorCode() == ErrorCode.DATA_CONVERSION_ERROR_1) {
e = Message.getSQLException(ErrorCode.DATA_CONVERSION_ERROR_1, v.getSQL() + " (" + getCreateSQL() + ")");
}
throw e;
}
}
boolean getComputed() { boolean getComputed() {
return isComputed; return isComputed;
} }
......
...@@ -1686,7 +1686,7 @@ public class MetaTable extends Table { ...@@ -1686,7 +1686,7 @@ public class MetaTable extends Table {
String s = strings[i]; String s = strings[i];
Value v = (s == null) ? (Value) ValueNull.INSTANCE : ValueString.get(s); Value v = (s == null) ? (Value) ValueNull.INSTANCE : ValueString.get(s);
Column col = columns[i]; Column col = columns[i];
v = v.convertTo(col.getType()); v = col.convert(v);
values[i] = v; values[i] = v;
} }
Row row = new Row(values, 0); Row row = new Row(values, 0);
......
...@@ -961,8 +961,7 @@ public abstract class Table extends SchemaObjectBase { ...@@ -961,8 +961,7 @@ public abstract class Table extends SchemaObjectBase {
} else { } else {
v = defaultExpr.getValue(session); v = defaultExpr.getValue(session);
} }
int type = column.getType(); return column.convert(v);
return v.convertTo(type);
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论