Expression.cpp 9.1 KB

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