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