mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-11 10:18:15 +09:00
LibSQL: Add better error handling to evaluate
and execute
methods
There was a lot of `VERIFY_NOT_REACHED` error handling going on. Fixed most of those. A bit of a caveat is that after every `evaluate` call for expressions that are part of a statement the error status of the `SQLResult` return value must be called.
This commit is contained in:
parent
0cfb5eec32
commit
9022cf99ff
Notes:
sideshowbarker
2024-07-18 01:54:09 +09:00
Author: https://github.com/JanDeVisser
Commit: 9022cf99ff
Pull-request: https://github.com/SerenityOS/serenity/pull/10598
Reviewed-by: https://github.com/trflynn89 ✅
5 changed files with 125 additions and 55 deletions
|
@ -14,15 +14,19 @@ Value Expression::evaluate(ExecutionContext&) const
|
|||
return Value::null();
|
||||
}
|
||||
|
||||
Value NumericLiteral::evaluate(ExecutionContext&) const
|
||||
Value NumericLiteral::evaluate(ExecutionContext& context) const
|
||||
{
|
||||
if (context.result->has_error())
|
||||
return Value::null();
|
||||
Value ret(SQLType::Float);
|
||||
ret = value();
|
||||
return ret;
|
||||
}
|
||||
|
||||
Value StringLiteral::evaluate(ExecutionContext&) const
|
||||
Value StringLiteral::evaluate(ExecutionContext& context) const
|
||||
{
|
||||
if (context.result->has_error())
|
||||
return Value::null();
|
||||
Value ret(SQLType::Text);
|
||||
ret = value();
|
||||
return ret;
|
||||
|
@ -35,11 +39,15 @@ Value NullLiteral::evaluate(ExecutionContext&) const
|
|||
|
||||
Value NestedExpression::evaluate(ExecutionContext& context) const
|
||||
{
|
||||
if (context.result->has_error())
|
||||
return Value::null();
|
||||
return expression()->evaluate(context);
|
||||
}
|
||||
|
||||
Value ChainedExpression::evaluate(ExecutionContext& context) const
|
||||
{
|
||||
if (context.result->has_error())
|
||||
return Value::null();
|
||||
Value ret(SQLType::Tuple);
|
||||
Vector<Value> values;
|
||||
for (auto& expression : expressions()) {
|
||||
|
@ -51,6 +59,8 @@ Value ChainedExpression::evaluate(ExecutionContext& context) const
|
|||
|
||||
Value BinaryOperatorExpression::evaluate(ExecutionContext& context) const
|
||||
{
|
||||
if (context.result->has_error())
|
||||
return Value::null();
|
||||
Value lhs_value = lhs()->evaluate(context);
|
||||
Value rhs_value = rhs()->evaluate(context);
|
||||
switch (type()) {
|
||||
|
@ -97,8 +107,8 @@ Value BinaryOperatorExpression::evaluate(ExecutionContext& context) const
|
|||
auto lhs_bool_maybe = lhs_value.to_bool();
|
||||
auto rhs_bool_maybe = rhs_value.to_bool();
|
||||
if (!lhs_bool_maybe.has_value() || !rhs_bool_maybe.has_value()) {
|
||||
// TODO Error handling
|
||||
VERIFY_NOT_REACHED();
|
||||
context.result->set_error(SQLErrorCode::BooleanOperatorTypeMismatch, BinaryOperator_name(type()));
|
||||
return Value::null();
|
||||
}
|
||||
return Value(lhs_bool_maybe.value() && rhs_bool_maybe.value());
|
||||
}
|
||||
|
@ -106,24 +116,27 @@ Value BinaryOperatorExpression::evaluate(ExecutionContext& context) const
|
|||
auto lhs_bool_maybe = lhs_value.to_bool();
|
||||
auto rhs_bool_maybe = rhs_value.to_bool();
|
||||
if (!lhs_bool_maybe.has_value() || !rhs_bool_maybe.has_value()) {
|
||||
// TODO Error handling
|
||||
VERIFY_NOT_REACHED();
|
||||
context.result->set_error(SQLErrorCode::BooleanOperatorTypeMismatch, BinaryOperator_name(type()));
|
||||
return Value::null();
|
||||
}
|
||||
return Value(lhs_bool_maybe.value() || rhs_bool_maybe.value());
|
||||
}
|
||||
default:
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
Value UnaryOperatorExpression::evaluate(ExecutionContext& context) const
|
||||
{
|
||||
if (context.result->has_error())
|
||||
return Value::null();
|
||||
Value expression_value = NestedExpression::evaluate(context);
|
||||
switch (type()) {
|
||||
case UnaryOperator::Plus:
|
||||
if (expression_value.type() == SQLType::Integer || expression_value.type() == SQLType::Float)
|
||||
return expression_value;
|
||||
// TODO: Error handling.
|
||||
VERIFY_NOT_REACHED();
|
||||
context.result->set_error(SQLErrorCode::NumericOperatorTypeMismatch, UnaryOperator_name(type()));
|
||||
return Value::null();
|
||||
case UnaryOperator::Minus:
|
||||
if (expression_value.type() == SQLType::Integer) {
|
||||
expression_value = -int(expression_value);
|
||||
|
@ -133,22 +146,22 @@ Value UnaryOperatorExpression::evaluate(ExecutionContext& context) const
|
|||
expression_value = -double(expression_value);
|
||||
return expression_value;
|
||||
}
|
||||
// TODO: Error handling.
|
||||
VERIFY_NOT_REACHED();
|
||||
context.result->set_error(SQLErrorCode::NumericOperatorTypeMismatch, UnaryOperator_name(type()));
|
||||
return Value::null();
|
||||
case UnaryOperator::Not:
|
||||
if (expression_value.type() == SQLType::Boolean) {
|
||||
expression_value = !bool(expression_value);
|
||||
return expression_value;
|
||||
}
|
||||
// TODO: Error handling.
|
||||
VERIFY_NOT_REACHED();
|
||||
context.result->set_error(SQLErrorCode::BooleanOperatorTypeMismatch, UnaryOperator_name(type()));
|
||||
return Value::null();
|
||||
case UnaryOperator::BitwiseNot:
|
||||
if (expression_value.type() == SQLType::Integer) {
|
||||
expression_value = ~u32(expression_value);
|
||||
return expression_value;
|
||||
}
|
||||
// TODO: Error handling.
|
||||
VERIFY_NOT_REACHED();
|
||||
context.result->set_error(SQLErrorCode::IntegerOperatorTypeMismatch, UnaryOperator_name(type()));
|
||||
return Value::null();
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
@ -162,8 +175,8 @@ Value ColumnNameExpression::evaluate(ExecutionContext& context) const
|
|||
if (column_descriptor.name == column_name())
|
||||
return { (*context.current_row)[ix] };
|
||||
}
|
||||
// TODO: Error handling.
|
||||
VERIFY_NOT_REACHED();
|
||||
context.result->set_error(SQLErrorCode::ColumnDoesNotExist, column_name());
|
||||
return Value::null();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue