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