Expression.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibSQL/AST/AST.h>
  7. #include <LibSQL/Database.h>
  8. namespace SQL::AST {
  9. Value Expression::evaluate(ExecutionContext&) const
  10. {
  11. return Value::null();
  12. }
  13. Value NumericLiteral::evaluate(ExecutionContext& context) const
  14. {
  15. if (context.result->has_error())
  16. return Value::null();
  17. Value ret(SQLType::Float);
  18. ret = value();
  19. return ret;
  20. }
  21. Value StringLiteral::evaluate(ExecutionContext& context) const
  22. {
  23. if (context.result->has_error())
  24. return Value::null();
  25. Value ret(SQLType::Text);
  26. ret = value();
  27. return ret;
  28. }
  29. Value NullLiteral::evaluate(ExecutionContext&) const
  30. {
  31. return Value::null();
  32. }
  33. Value NestedExpression::evaluate(ExecutionContext& context) const
  34. {
  35. if (context.result->has_error())
  36. return Value::null();
  37. return expression()->evaluate(context);
  38. }
  39. Value ChainedExpression::evaluate(ExecutionContext& context) const
  40. {
  41. if (context.result->has_error())
  42. return Value::null();
  43. Value ret(SQLType::Tuple);
  44. Vector<Value> values;
  45. for (auto& expression : expressions()) {
  46. values.append(expression.evaluate(context));
  47. }
  48. ret = values;
  49. return ret;
  50. }
  51. Value BinaryOperatorExpression::evaluate(ExecutionContext& context) const
  52. {
  53. if (context.result->has_error())
  54. return Value::null();
  55. Value lhs_value = lhs()->evaluate(context);
  56. Value rhs_value = rhs()->evaluate(context);
  57. switch (type()) {
  58. case BinaryOperator::Concatenate: {
  59. if (lhs_value.type() != SQLType::Text) {
  60. context.result->set_error(SQLErrorCode::BooleanOperatorTypeMismatch, BinaryOperator_name(type()));
  61. return Value::null();
  62. }
  63. AK::StringBuilder builder;
  64. builder.append(lhs_value.to_string());
  65. builder.append(rhs_value.to_string());
  66. return Value(builder.to_string());
  67. }
  68. case BinaryOperator::Multiplication:
  69. return lhs_value.multiply(rhs_value);
  70. case BinaryOperator::Division:
  71. return lhs_value.divide(rhs_value);
  72. case BinaryOperator::Modulo:
  73. return lhs_value.modulo(rhs_value);
  74. case BinaryOperator::Plus:
  75. return lhs_value.add(rhs_value);
  76. case BinaryOperator::Minus:
  77. return lhs_value.subtract(rhs_value);
  78. case BinaryOperator::ShiftLeft:
  79. return lhs_value.shift_left(rhs_value);
  80. case BinaryOperator::ShiftRight:
  81. return lhs_value.shift_right(rhs_value);
  82. case BinaryOperator::BitwiseAnd:
  83. return lhs_value.bitwise_and(rhs_value);
  84. case BinaryOperator::BitwiseOr:
  85. return lhs_value.bitwise_or(rhs_value);
  86. case BinaryOperator::LessThan:
  87. return Value(lhs_value.compare(rhs_value) < 0);
  88. case BinaryOperator::LessThanEquals:
  89. return Value(lhs_value.compare(rhs_value) <= 0);
  90. case BinaryOperator::GreaterThan:
  91. return Value(lhs_value.compare(rhs_value) > 0);
  92. case BinaryOperator::GreaterThanEquals:
  93. return Value(lhs_value.compare(rhs_value) >= 0);
  94. case BinaryOperator::Equals:
  95. return Value(lhs_value.compare(rhs_value) == 0);
  96. case BinaryOperator::NotEquals:
  97. return Value(lhs_value.compare(rhs_value) != 0);
  98. case BinaryOperator::And: {
  99. auto lhs_bool_maybe = lhs_value.to_bool();
  100. auto rhs_bool_maybe = rhs_value.to_bool();
  101. if (!lhs_bool_maybe.has_value() || !rhs_bool_maybe.has_value()) {
  102. context.result->set_error(SQLErrorCode::BooleanOperatorTypeMismatch, BinaryOperator_name(type()));
  103. return Value::null();
  104. }
  105. return Value(lhs_bool_maybe.release_value() && rhs_bool_maybe.release_value());
  106. }
  107. case BinaryOperator::Or: {
  108. auto lhs_bool_maybe = lhs_value.to_bool();
  109. auto rhs_bool_maybe = rhs_value.to_bool();
  110. if (!lhs_bool_maybe.has_value() || !rhs_bool_maybe.has_value()) {
  111. context.result->set_error(SQLErrorCode::BooleanOperatorTypeMismatch, BinaryOperator_name(type()));
  112. return Value::null();
  113. }
  114. return Value(lhs_bool_maybe.release_value() || rhs_bool_maybe.release_value());
  115. }
  116. default:
  117. VERIFY_NOT_REACHED();
  118. }
  119. }
  120. Value UnaryOperatorExpression::evaluate(ExecutionContext& context) const
  121. {
  122. if (context.result->has_error())
  123. return Value::null();
  124. Value expression_value = NestedExpression::evaluate(context);
  125. switch (type()) {
  126. case UnaryOperator::Plus:
  127. if (expression_value.type() == SQLType::Integer || expression_value.type() == SQLType::Float)
  128. return expression_value;
  129. context.result->set_error(SQLErrorCode::NumericOperatorTypeMismatch, UnaryOperator_name(type()));
  130. return Value::null();
  131. case UnaryOperator::Minus:
  132. if (expression_value.type() == SQLType::Integer) {
  133. expression_value = -int(expression_value);
  134. return expression_value;
  135. }
  136. if (expression_value.type() == SQLType::Float) {
  137. expression_value = -double(expression_value);
  138. return expression_value;
  139. }
  140. context.result->set_error(SQLErrorCode::NumericOperatorTypeMismatch, UnaryOperator_name(type()));
  141. return Value::null();
  142. case UnaryOperator::Not:
  143. if (expression_value.type() == SQLType::Boolean) {
  144. expression_value = !bool(expression_value);
  145. return expression_value;
  146. }
  147. context.result->set_error(SQLErrorCode::BooleanOperatorTypeMismatch, UnaryOperator_name(type()));
  148. return Value::null();
  149. case UnaryOperator::BitwiseNot:
  150. if (expression_value.type() == SQLType::Integer) {
  151. expression_value = ~u32(expression_value);
  152. return expression_value;
  153. }
  154. context.result->set_error(SQLErrorCode::IntegerOperatorTypeMismatch, UnaryOperator_name(type()));
  155. return Value::null();
  156. }
  157. VERIFY_NOT_REACHED();
  158. }
  159. Value ColumnNameExpression::evaluate(ExecutionContext& context) const
  160. {
  161. auto& descriptor = *context.current_row->descriptor();
  162. VERIFY(context.current_row->size() == descriptor.size());
  163. Optional<size_t> index_in_row;
  164. for (auto ix = 0u; ix < context.current_row->size(); ix++) {
  165. auto& column_descriptor = descriptor[ix];
  166. if (!table_name().is_empty() && column_descriptor.table != table_name())
  167. continue;
  168. if (column_descriptor.name == column_name()) {
  169. if (index_in_row.has_value()) {
  170. context.result->set_error(SQLErrorCode::AmbiguousColumnName, column_name());
  171. return Value::null();
  172. }
  173. index_in_row = ix;
  174. }
  175. }
  176. if (index_in_row.has_value())
  177. return (*context.current_row)[index_in_row.value()];
  178. context.result->set_error(SQLErrorCode::ColumnDoesNotExist, column_name());
  179. return Value::null();
  180. }
  181. }