Expression.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/StringView.h>
  8. #include <LibRegex/Regex.h>
  9. #include <LibSQL/AST/AST.h>
  10. #include <LibSQL/Database.h>
  11. namespace SQL::AST {
  12. static constexpr auto s_posix_basic_metacharacters = ".^$*[]+\\"sv;
  13. ResultOr<Value> NumericLiteral::evaluate(ExecutionContext&) const
  14. {
  15. return Value { value() };
  16. }
  17. ResultOr<Value> StringLiteral::evaluate(ExecutionContext&) const
  18. {
  19. return Value { value() };
  20. }
  21. ResultOr<Value> NullLiteral::evaluate(ExecutionContext&) const
  22. {
  23. return Value {};
  24. }
  25. ResultOr<Value> Placeholder::evaluate(ExecutionContext& context) const
  26. {
  27. if (parameter_index() >= context.placeholder_values.size())
  28. return Result { SQLCommand::Unknown, SQLErrorCode::InvalidNumberOfPlaceholderValues };
  29. return context.placeholder_values[parameter_index()];
  30. }
  31. ResultOr<Value> NestedExpression::evaluate(ExecutionContext& context) const
  32. {
  33. return expression()->evaluate(context);
  34. }
  35. ResultOr<Value> ChainedExpression::evaluate(ExecutionContext& context) const
  36. {
  37. Vector<Value> values;
  38. TRY(values.try_ensure_capacity(expressions().size()));
  39. for (auto& expression : expressions())
  40. values.unchecked_append(TRY(expression.evaluate(context)));
  41. return Value::create_tuple(move(values));
  42. }
  43. ResultOr<Value> BinaryOperatorExpression::evaluate(ExecutionContext& context) const
  44. {
  45. Value lhs_value = TRY(lhs()->evaluate(context));
  46. Value rhs_value = TRY(rhs()->evaluate(context));
  47. switch (type()) {
  48. case BinaryOperator::Concatenate: {
  49. if (lhs_value.type() != SQLType::Text)
  50. return Result { SQLCommand::Unknown, SQLErrorCode::BooleanOperatorTypeMismatch, BinaryOperator_name(type()) };
  51. AK::StringBuilder builder;
  52. builder.append(lhs_value.to_deprecated_string());
  53. builder.append(rhs_value.to_deprecated_string());
  54. return Value(builder.to_deprecated_string());
  55. }
  56. case BinaryOperator::Multiplication:
  57. return lhs_value.multiply(rhs_value);
  58. case BinaryOperator::Division:
  59. return lhs_value.divide(rhs_value);
  60. case BinaryOperator::Modulo:
  61. return lhs_value.modulo(rhs_value);
  62. case BinaryOperator::Plus:
  63. return lhs_value.add(rhs_value);
  64. case BinaryOperator::Minus:
  65. return lhs_value.subtract(rhs_value);
  66. case BinaryOperator::ShiftLeft:
  67. return lhs_value.shift_left(rhs_value);
  68. case BinaryOperator::ShiftRight:
  69. return lhs_value.shift_right(rhs_value);
  70. case BinaryOperator::BitwiseAnd:
  71. return lhs_value.bitwise_and(rhs_value);
  72. case BinaryOperator::BitwiseOr:
  73. return lhs_value.bitwise_or(rhs_value);
  74. case BinaryOperator::LessThan:
  75. return Value(lhs_value.compare(rhs_value) < 0);
  76. case BinaryOperator::LessThanEquals:
  77. return Value(lhs_value.compare(rhs_value) <= 0);
  78. case BinaryOperator::GreaterThan:
  79. return Value(lhs_value.compare(rhs_value) > 0);
  80. case BinaryOperator::GreaterThanEquals:
  81. return Value(lhs_value.compare(rhs_value) >= 0);
  82. case BinaryOperator::Equals:
  83. return Value(lhs_value.compare(rhs_value) == 0);
  84. case BinaryOperator::NotEquals:
  85. return Value(lhs_value.compare(rhs_value) != 0);
  86. case BinaryOperator::And: {
  87. auto lhs_bool_maybe = lhs_value.to_bool();
  88. auto rhs_bool_maybe = rhs_value.to_bool();
  89. if (!lhs_bool_maybe.has_value() || !rhs_bool_maybe.has_value())
  90. return Result { SQLCommand::Unknown, SQLErrorCode::BooleanOperatorTypeMismatch, BinaryOperator_name(type()) };
  91. return Value(lhs_bool_maybe.release_value() && rhs_bool_maybe.release_value());
  92. }
  93. case BinaryOperator::Or: {
  94. auto lhs_bool_maybe = lhs_value.to_bool();
  95. auto rhs_bool_maybe = rhs_value.to_bool();
  96. if (!lhs_bool_maybe.has_value() || !rhs_bool_maybe.has_value())
  97. return Result { SQLCommand::Unknown, SQLErrorCode::BooleanOperatorTypeMismatch, BinaryOperator_name(type()) };
  98. return Value(lhs_bool_maybe.release_value() || rhs_bool_maybe.release_value());
  99. }
  100. default:
  101. VERIFY_NOT_REACHED();
  102. }
  103. }
  104. ResultOr<Value> UnaryOperatorExpression::evaluate(ExecutionContext& context) const
  105. {
  106. Value expression_value = TRY(NestedExpression::evaluate(context));
  107. switch (type()) {
  108. case UnaryOperator::Plus:
  109. if (expression_value.type() == SQLType::Integer || expression_value.type() == SQLType::Float)
  110. return expression_value;
  111. return Result { SQLCommand::Unknown, SQLErrorCode::NumericOperatorTypeMismatch, UnaryOperator_name(type()) };
  112. case UnaryOperator::Minus:
  113. if (expression_value.type() == SQLType::Integer) {
  114. expression_value = -expression_value.to_int().value();
  115. return expression_value;
  116. }
  117. if (expression_value.type() == SQLType::Float) {
  118. expression_value = -expression_value.to_double().value();
  119. return expression_value;
  120. }
  121. return Result { SQLCommand::Unknown, SQLErrorCode::NumericOperatorTypeMismatch, UnaryOperator_name(type()) };
  122. case UnaryOperator::Not:
  123. if (expression_value.type() == SQLType::Boolean) {
  124. expression_value = !expression_value.to_bool().value();
  125. return expression_value;
  126. }
  127. return Result { SQLCommand::Unknown, SQLErrorCode::BooleanOperatorTypeMismatch, UnaryOperator_name(type()) };
  128. case UnaryOperator::BitwiseNot:
  129. if (expression_value.type() == SQLType::Integer) {
  130. expression_value = ~expression_value.to_u32().value();
  131. return expression_value;
  132. }
  133. return Result { SQLCommand::Unknown, SQLErrorCode::IntegerOperatorTypeMismatch, UnaryOperator_name(type()) };
  134. default:
  135. VERIFY_NOT_REACHED();
  136. }
  137. }
  138. ResultOr<Value> ColumnNameExpression::evaluate(ExecutionContext& context) const
  139. {
  140. if (!context.current_row)
  141. return Result { SQLCommand::Unknown, SQLErrorCode::SyntaxError, column_name() };
  142. auto& descriptor = *context.current_row->descriptor();
  143. VERIFY(context.current_row->size() == descriptor.size());
  144. Optional<size_t> index_in_row;
  145. for (auto ix = 0u; ix < context.current_row->size(); ix++) {
  146. auto& column_descriptor = descriptor[ix];
  147. if (!table_name().is_empty() && column_descriptor.table != table_name())
  148. continue;
  149. if (column_descriptor.name == column_name()) {
  150. if (index_in_row.has_value())
  151. return Result { SQLCommand::Unknown, SQLErrorCode::AmbiguousColumnName, column_name() };
  152. index_in_row = ix;
  153. }
  154. }
  155. if (index_in_row.has_value())
  156. return (*context.current_row)[index_in_row.value()];
  157. return Result { SQLCommand::Unknown, SQLErrorCode::ColumnDoesNotExist, column_name() };
  158. }
  159. ResultOr<Value> MatchExpression::evaluate(ExecutionContext& context) const
  160. {
  161. switch (type()) {
  162. case MatchOperator::Like: {
  163. Value lhs_value = TRY(lhs()->evaluate(context));
  164. Value rhs_value = TRY(rhs()->evaluate(context));
  165. char escape_char = '\0';
  166. if (escape()) {
  167. auto escape_str = TRY(escape()->evaluate(context)).to_deprecated_string();
  168. if (escape_str.length() != 1)
  169. return Result { SQLCommand::Unknown, SQLErrorCode::SyntaxError, "ESCAPE should be a single character" };
  170. escape_char = escape_str[0];
  171. }
  172. // Compile the pattern into a simple regex.
  173. // https://sqlite.org/lang_expr.html#the_like_glob_regexp_and_match_operators
  174. bool escaped = false;
  175. AK::StringBuilder builder;
  176. builder.append('^');
  177. for (auto c : rhs_value.to_deprecated_string()) {
  178. if (escape() && c == escape_char && !escaped) {
  179. escaped = true;
  180. } else if (s_posix_basic_metacharacters.contains(c)) {
  181. escaped = false;
  182. builder.append('\\');
  183. builder.append(c);
  184. } else if (c == '_' && !escaped) {
  185. builder.append('.');
  186. } else if (c == '%' && !escaped) {
  187. builder.append(".*"sv);
  188. } else {
  189. escaped = false;
  190. builder.append(c);
  191. }
  192. }
  193. builder.append('$');
  194. // FIXME: We should probably cache this regex.
  195. auto regex = Regex<PosixBasic>(builder.build());
  196. auto result = regex.match(lhs_value.to_deprecated_string(), PosixFlags::Insensitive | PosixFlags::Unicode);
  197. return Value(invert_expression() ? !result.success : result.success);
  198. }
  199. case MatchOperator::Regexp: {
  200. Value lhs_value = TRY(lhs()->evaluate(context));
  201. Value rhs_value = TRY(rhs()->evaluate(context));
  202. auto regex = Regex<PosixExtended>(rhs_value.to_deprecated_string());
  203. auto err = regex.parser_result.error;
  204. if (err != regex::Error::NoError) {
  205. StringBuilder builder;
  206. builder.append("Regular expression: "sv);
  207. builder.append(get_error_string(err));
  208. return Result { SQLCommand::Unknown, SQLErrorCode::SyntaxError, builder.build() };
  209. }
  210. auto result = regex.match(lhs_value.to_deprecated_string(), PosixFlags::Insensitive | PosixFlags::Unicode);
  211. return Value(invert_expression() ? !result.success : result.success);
  212. }
  213. case MatchOperator::Glob:
  214. return Result { SQLCommand::Unknown, SQLErrorCode::NotYetImplemented, "GLOB expression is not yet implemented"sv };
  215. case MatchOperator::Match:
  216. return Result { SQLCommand::Unknown, SQLErrorCode::NotYetImplemented, "MATCH expression is not yet implemented"sv };
  217. default:
  218. VERIFY_NOT_REACHED();
  219. }
  220. }
  221. }