AST.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. * Copyright (c) 2021, Mahmoud Mandour <ma.mandourr@gmail.com>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/NonnullRefPtr.h>
  10. #include <AK/NonnullRefPtrVector.h>
  11. #include <AK/RefCounted.h>
  12. #include <AK/RefPtr.h>
  13. #include <AK/String.h>
  14. #include <LibSQL/AST/Token.h>
  15. #include <LibSQL/Forward.h>
  16. #include <LibSQL/Result.h>
  17. #include <LibSQL/ResultSet.h>
  18. #include <LibSQL/Type.h>
  19. namespace SQL::AST {
  20. template<class T, class... Args>
  21. static inline NonnullRefPtr<T>
  22. create_ast_node(Args&&... args)
  23. {
  24. return adopt_ref(*new T(forward<Args>(args)...));
  25. }
  26. class ASTNode : public RefCounted<ASTNode> {
  27. public:
  28. virtual ~ASTNode() = default;
  29. protected:
  30. ASTNode() = default;
  31. };
  32. //==================================================================================================
  33. // Language types
  34. //==================================================================================================
  35. class SignedNumber final : public ASTNode {
  36. public:
  37. explicit SignedNumber(double value)
  38. : m_value(value)
  39. {
  40. }
  41. double value() const { return m_value; }
  42. private:
  43. double m_value;
  44. };
  45. class TypeName : public ASTNode {
  46. public:
  47. TypeName(String name, NonnullRefPtrVector<SignedNumber> signed_numbers)
  48. : m_name(move(name))
  49. , m_signed_numbers(move(signed_numbers))
  50. {
  51. VERIFY(m_signed_numbers.size() <= 2);
  52. }
  53. const String& name() const { return m_name; }
  54. const NonnullRefPtrVector<SignedNumber>& signed_numbers() const { return m_signed_numbers; }
  55. private:
  56. String m_name;
  57. NonnullRefPtrVector<SignedNumber> m_signed_numbers;
  58. };
  59. class ColumnDefinition : public ASTNode {
  60. public:
  61. ColumnDefinition(String name, NonnullRefPtr<TypeName> type_name)
  62. : m_name(move(name))
  63. , m_type_name(move(type_name))
  64. {
  65. }
  66. const String& name() const { return m_name; }
  67. const NonnullRefPtr<TypeName>& type_name() const { return m_type_name; }
  68. private:
  69. String m_name;
  70. NonnullRefPtr<TypeName> m_type_name;
  71. };
  72. class CommonTableExpression : public ASTNode {
  73. public:
  74. CommonTableExpression(String table_name, Vector<String> column_names, NonnullRefPtr<Select> select_statement)
  75. : m_table_name(move(table_name))
  76. , m_column_names(move(column_names))
  77. , m_select_statement(move(select_statement))
  78. {
  79. }
  80. const String& table_name() const { return m_table_name; }
  81. const Vector<String>& column_names() const { return m_column_names; }
  82. const NonnullRefPtr<Select>& select_statement() const { return m_select_statement; }
  83. private:
  84. String m_table_name;
  85. Vector<String> m_column_names;
  86. NonnullRefPtr<Select> m_select_statement;
  87. };
  88. class CommonTableExpressionList : public ASTNode {
  89. public:
  90. CommonTableExpressionList(bool recursive, NonnullRefPtrVector<CommonTableExpression> common_table_expressions)
  91. : m_recursive(recursive)
  92. , m_common_table_expressions(move(common_table_expressions))
  93. {
  94. VERIFY(!m_common_table_expressions.is_empty());
  95. }
  96. bool recursive() const { return m_recursive; }
  97. const NonnullRefPtrVector<CommonTableExpression>& common_table_expressions() const { return m_common_table_expressions; }
  98. private:
  99. bool m_recursive;
  100. NonnullRefPtrVector<CommonTableExpression> m_common_table_expressions;
  101. };
  102. class QualifiedTableName : public ASTNode {
  103. public:
  104. QualifiedTableName(String schema_name, String table_name, String alias)
  105. : m_schema_name(move(schema_name))
  106. , m_table_name(move(table_name))
  107. , m_alias(move(alias))
  108. {
  109. }
  110. const String& schema_name() const { return m_schema_name; }
  111. const String& table_name() const { return m_table_name; }
  112. const String& alias() const { return m_alias; }
  113. private:
  114. String m_schema_name;
  115. String m_table_name;
  116. String m_alias;
  117. };
  118. class ReturningClause : public ASTNode {
  119. public:
  120. struct ColumnClause {
  121. NonnullRefPtr<Expression> expression;
  122. String column_alias;
  123. };
  124. ReturningClause() = default;
  125. explicit ReturningClause(Vector<ColumnClause> columns)
  126. : m_columns(move(columns))
  127. {
  128. }
  129. bool return_all_columns() const { return m_columns.is_empty(); };
  130. const Vector<ColumnClause>& columns() const { return m_columns; }
  131. private:
  132. Vector<ColumnClause> m_columns;
  133. };
  134. enum class ResultType {
  135. All,
  136. Table,
  137. Expression,
  138. };
  139. class ResultColumn : public ASTNode {
  140. public:
  141. ResultColumn() = default;
  142. explicit ResultColumn(String table_name)
  143. : m_type(ResultType::Table)
  144. , m_table_name(move(table_name))
  145. {
  146. }
  147. ResultColumn(NonnullRefPtr<Expression> expression, String column_alias)
  148. : m_type(ResultType::Expression)
  149. , m_expression(move(expression))
  150. , m_column_alias(move(column_alias))
  151. {
  152. }
  153. ResultType type() const { return m_type; }
  154. bool select_from_table() const { return !m_table_name.is_null(); }
  155. const String& table_name() const { return m_table_name; }
  156. bool select_from_expression() const { return !m_expression.is_null(); }
  157. const RefPtr<Expression>& expression() const { return m_expression; }
  158. const String& column_alias() const { return m_column_alias; }
  159. private:
  160. ResultType m_type { ResultType::All };
  161. String m_table_name {};
  162. RefPtr<Expression> m_expression {};
  163. String m_column_alias {};
  164. };
  165. class GroupByClause : public ASTNode {
  166. public:
  167. GroupByClause(NonnullRefPtrVector<Expression> group_by_list, RefPtr<Expression> having_clause)
  168. : m_group_by_list(move(group_by_list))
  169. , m_having_clause(move(having_clause))
  170. {
  171. VERIFY(!m_group_by_list.is_empty());
  172. }
  173. const NonnullRefPtrVector<Expression>& group_by_list() const { return m_group_by_list; }
  174. const RefPtr<Expression>& having_clause() const { return m_having_clause; }
  175. private:
  176. NonnullRefPtrVector<Expression> m_group_by_list;
  177. RefPtr<Expression> m_having_clause;
  178. };
  179. class TableOrSubquery : public ASTNode {
  180. public:
  181. TableOrSubquery() = default;
  182. TableOrSubquery(String schema_name, String table_name, String table_alias)
  183. : m_is_table(true)
  184. , m_schema_name(move(schema_name))
  185. , m_table_name(move(table_name))
  186. , m_table_alias(move(table_alias))
  187. {
  188. }
  189. explicit TableOrSubquery(NonnullRefPtrVector<TableOrSubquery> subqueries)
  190. : m_is_subquery(!subqueries.is_empty())
  191. , m_subqueries(move(subqueries))
  192. {
  193. }
  194. bool is_table() const { return m_is_table; }
  195. const String& schema_name() const { return m_schema_name; }
  196. const String& table_name() const { return m_table_name; }
  197. const String& table_alias() const { return m_table_alias; }
  198. bool is_subquery() const { return m_is_subquery; }
  199. const NonnullRefPtrVector<TableOrSubquery>& subqueries() const { return m_subqueries; }
  200. private:
  201. bool m_is_table { false };
  202. String m_schema_name {};
  203. String m_table_name {};
  204. String m_table_alias {};
  205. bool m_is_subquery { false };
  206. NonnullRefPtrVector<TableOrSubquery> m_subqueries {};
  207. };
  208. class OrderingTerm : public ASTNode {
  209. public:
  210. OrderingTerm(NonnullRefPtr<Expression> expression, String collation_name, Order order, Nulls nulls)
  211. : m_expression(move(expression))
  212. , m_collation_name(move(collation_name))
  213. , m_order(order)
  214. , m_nulls(nulls)
  215. {
  216. }
  217. const NonnullRefPtr<Expression>& expression() const { return m_expression; }
  218. const String& collation_name() const { return m_collation_name; }
  219. Order order() const { return m_order; }
  220. Nulls nulls() const { return m_nulls; }
  221. private:
  222. NonnullRefPtr<Expression> m_expression;
  223. String m_collation_name;
  224. Order m_order;
  225. Nulls m_nulls;
  226. };
  227. class LimitClause : public ASTNode {
  228. public:
  229. LimitClause(NonnullRefPtr<Expression> limit_expression, RefPtr<Expression> offset_expression)
  230. : m_limit_expression(move(limit_expression))
  231. , m_offset_expression(move(offset_expression))
  232. {
  233. }
  234. const NonnullRefPtr<Expression>& limit_expression() const { return m_limit_expression; }
  235. const RefPtr<Expression>& offset_expression() const { return m_offset_expression; }
  236. private:
  237. NonnullRefPtr<Expression> m_limit_expression;
  238. RefPtr<Expression> m_offset_expression;
  239. };
  240. //==================================================================================================
  241. // Expressions
  242. //==================================================================================================
  243. struct ExecutionContext {
  244. NonnullRefPtr<Database> database;
  245. class Statement const* statement;
  246. Tuple* current_row { nullptr };
  247. };
  248. class Expression : public ASTNode {
  249. public:
  250. virtual ResultOr<Value> evaluate(ExecutionContext&) const
  251. {
  252. return Result { SQLCommand::Unknown, SQLErrorCode::NotYetImplemented };
  253. }
  254. };
  255. class ErrorExpression final : public Expression {
  256. };
  257. class NumericLiteral : public Expression {
  258. public:
  259. explicit NumericLiteral(double value)
  260. : m_value(value)
  261. {
  262. }
  263. double value() const { return m_value; }
  264. virtual ResultOr<Value> evaluate(ExecutionContext&) const override;
  265. private:
  266. double m_value;
  267. };
  268. class StringLiteral : public Expression {
  269. public:
  270. explicit StringLiteral(String value)
  271. : m_value(move(value))
  272. {
  273. }
  274. const String& value() const { return m_value; }
  275. virtual ResultOr<Value> evaluate(ExecutionContext&) const override;
  276. private:
  277. String m_value;
  278. };
  279. class BlobLiteral : public Expression {
  280. public:
  281. explicit BlobLiteral(String value)
  282. : m_value(move(value))
  283. {
  284. }
  285. const String& value() const { return m_value; }
  286. private:
  287. String m_value;
  288. };
  289. class NullLiteral : public Expression {
  290. public:
  291. virtual ResultOr<Value> evaluate(ExecutionContext&) const override;
  292. };
  293. class NestedExpression : public Expression {
  294. public:
  295. const NonnullRefPtr<Expression>& expression() const { return m_expression; }
  296. virtual ResultOr<Value> evaluate(ExecutionContext&) const override;
  297. protected:
  298. explicit NestedExpression(NonnullRefPtr<Expression> expression)
  299. : m_expression(move(expression))
  300. {
  301. }
  302. private:
  303. NonnullRefPtr<Expression> m_expression;
  304. };
  305. class NestedDoubleExpression : public Expression {
  306. public:
  307. const NonnullRefPtr<Expression>& lhs() const { return m_lhs; }
  308. const NonnullRefPtr<Expression>& rhs() const { return m_rhs; }
  309. protected:
  310. NestedDoubleExpression(NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  311. : m_lhs(move(lhs))
  312. , m_rhs(move(rhs))
  313. {
  314. }
  315. private:
  316. NonnullRefPtr<Expression> m_lhs;
  317. NonnullRefPtr<Expression> m_rhs;
  318. };
  319. class InvertibleNestedExpression : public NestedExpression {
  320. public:
  321. bool invert_expression() const { return m_invert_expression; }
  322. protected:
  323. InvertibleNestedExpression(NonnullRefPtr<Expression> expression, bool invert_expression)
  324. : NestedExpression(move(expression))
  325. , m_invert_expression(invert_expression)
  326. {
  327. }
  328. private:
  329. bool m_invert_expression;
  330. };
  331. class InvertibleNestedDoubleExpression : public NestedDoubleExpression {
  332. public:
  333. bool invert_expression() const { return m_invert_expression; }
  334. protected:
  335. InvertibleNestedDoubleExpression(NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs, bool invert_expression)
  336. : NestedDoubleExpression(move(lhs), move(rhs))
  337. , m_invert_expression(invert_expression)
  338. {
  339. }
  340. private:
  341. bool m_invert_expression;
  342. };
  343. class ColumnNameExpression : public Expression {
  344. public:
  345. ColumnNameExpression(String schema_name, String table_name, String column_name)
  346. : m_schema_name(move(schema_name))
  347. , m_table_name(move(table_name))
  348. , m_column_name(move(column_name))
  349. {
  350. }
  351. const String& schema_name() const { return m_schema_name; }
  352. const String& table_name() const { return m_table_name; }
  353. const String& column_name() const { return m_column_name; }
  354. virtual ResultOr<Value> evaluate(ExecutionContext&) const override;
  355. private:
  356. String m_schema_name;
  357. String m_table_name;
  358. String m_column_name;
  359. };
  360. #define __enum_UnaryOperator(S) \
  361. S(Minus, "-") \
  362. S(Plus, "+") \
  363. S(BitwiseNot, "~") \
  364. S(Not, "NOT")
  365. enum class UnaryOperator {
  366. #undef __UnaryOperator
  367. #define __UnaryOperator(code, name) code,
  368. __enum_UnaryOperator(__UnaryOperator)
  369. #undef __UnaryOperator
  370. };
  371. constexpr char const* UnaryOperator_name(UnaryOperator op)
  372. {
  373. switch (op) {
  374. #undef __UnaryOperator
  375. #define __UnaryOperator(code, name) \
  376. case UnaryOperator::code: \
  377. return name;
  378. __enum_UnaryOperator(__UnaryOperator)
  379. #undef __UnaryOperator
  380. default : VERIFY_NOT_REACHED();
  381. }
  382. }
  383. class UnaryOperatorExpression : public NestedExpression {
  384. public:
  385. UnaryOperatorExpression(UnaryOperator type, NonnullRefPtr<Expression> expression)
  386. : NestedExpression(move(expression))
  387. , m_type(type)
  388. {
  389. }
  390. UnaryOperator type() const { return m_type; }
  391. virtual ResultOr<Value> evaluate(ExecutionContext&) const override;
  392. private:
  393. UnaryOperator m_type;
  394. };
  395. // Note: These are in order of highest-to-lowest operator precedence.
  396. #define __enum_BinaryOperator(S) \
  397. S(Concatenate, "||") \
  398. S(Multiplication, "*") \
  399. S(Division, "/") \
  400. S(Modulo, "%") \
  401. S(Plus, "+") \
  402. S(Minus, "-") \
  403. S(ShiftLeft, "<<") \
  404. S(ShiftRight, ">>") \
  405. S(BitwiseAnd, "&") \
  406. S(BitwiseOr, "|") \
  407. S(LessThan, "<") \
  408. S(LessThanEquals, "<=") \
  409. S(GreaterThan, ">") \
  410. S(GreaterThanEquals, ">=") \
  411. S(Equals, "=") \
  412. S(NotEquals, "!=") \
  413. S(And, "and") \
  414. S(Or, "or")
  415. enum class BinaryOperator {
  416. #undef __BinaryOperator
  417. #define __BinaryOperator(code, name) code,
  418. __enum_BinaryOperator(__BinaryOperator)
  419. #undef __BinaryOperator
  420. };
  421. constexpr char const* BinaryOperator_name(BinaryOperator op)
  422. {
  423. switch (op) {
  424. #undef __BinaryOperator
  425. #define __BinaryOperator(code, name) \
  426. case BinaryOperator::code: \
  427. return name;
  428. __enum_BinaryOperator(__BinaryOperator)
  429. #undef __BinaryOperator
  430. default : VERIFY_NOT_REACHED();
  431. }
  432. }
  433. class BinaryOperatorExpression : public NestedDoubleExpression {
  434. public:
  435. BinaryOperatorExpression(BinaryOperator type, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs)
  436. : NestedDoubleExpression(move(lhs), move(rhs))
  437. , m_type(type)
  438. {
  439. }
  440. BinaryOperator type() const { return m_type; }
  441. virtual ResultOr<Value> evaluate(ExecutionContext&) const override;
  442. private:
  443. BinaryOperator m_type;
  444. };
  445. class ChainedExpression : public Expression {
  446. public:
  447. explicit ChainedExpression(NonnullRefPtrVector<Expression> expressions)
  448. : m_expressions(move(expressions))
  449. {
  450. }
  451. const NonnullRefPtrVector<Expression>& expressions() const { return m_expressions; }
  452. virtual ResultOr<Value> evaluate(ExecutionContext&) const override;
  453. private:
  454. NonnullRefPtrVector<Expression> m_expressions;
  455. };
  456. class CastExpression : public NestedExpression {
  457. public:
  458. CastExpression(NonnullRefPtr<Expression> expression, NonnullRefPtr<TypeName> type_name)
  459. : NestedExpression(move(expression))
  460. , m_type_name(move(type_name))
  461. {
  462. }
  463. const NonnullRefPtr<TypeName>& type_name() const { return m_type_name; }
  464. private:
  465. NonnullRefPtr<TypeName> m_type_name;
  466. };
  467. class CaseExpression : public Expression {
  468. public:
  469. struct WhenThenClause {
  470. NonnullRefPtr<Expression> when;
  471. NonnullRefPtr<Expression> then;
  472. };
  473. CaseExpression(RefPtr<Expression> case_expression, Vector<WhenThenClause> when_then_clauses, RefPtr<Expression> else_expression)
  474. : m_case_expression(case_expression)
  475. , m_when_then_clauses(when_then_clauses)
  476. , m_else_expression(else_expression)
  477. {
  478. VERIFY(!m_when_then_clauses.is_empty());
  479. }
  480. const RefPtr<Expression>& case_expression() const { return m_case_expression; }
  481. const Vector<WhenThenClause>& when_then_clauses() const { return m_when_then_clauses; }
  482. const RefPtr<Expression>& else_expression() const { return m_else_expression; }
  483. private:
  484. RefPtr<Expression> m_case_expression;
  485. Vector<WhenThenClause> m_when_then_clauses;
  486. RefPtr<Expression> m_else_expression;
  487. };
  488. class ExistsExpression : public Expression {
  489. public:
  490. ExistsExpression(NonnullRefPtr<Select> select_statement, bool invert_expression)
  491. : m_select_statement(move(select_statement))
  492. , m_invert_expression(invert_expression)
  493. {
  494. }
  495. const NonnullRefPtr<Select>& select_statement() const { return m_select_statement; }
  496. bool invert_expression() const { return m_invert_expression; }
  497. private:
  498. NonnullRefPtr<Select> m_select_statement;
  499. bool m_invert_expression;
  500. };
  501. class CollateExpression : public NestedExpression {
  502. public:
  503. CollateExpression(NonnullRefPtr<Expression> expression, String collation_name)
  504. : NestedExpression(move(expression))
  505. , m_collation_name(move(collation_name))
  506. {
  507. }
  508. const String& collation_name() const { return m_collation_name; }
  509. private:
  510. String m_collation_name;
  511. };
  512. enum class MatchOperator {
  513. Like,
  514. Glob,
  515. Match,
  516. Regexp,
  517. };
  518. class MatchExpression : public InvertibleNestedDoubleExpression {
  519. public:
  520. MatchExpression(MatchOperator type, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs, RefPtr<Expression> escape, bool invert_expression)
  521. : InvertibleNestedDoubleExpression(move(lhs), move(rhs), invert_expression)
  522. , m_type(type)
  523. , m_escape(move(escape))
  524. {
  525. }
  526. MatchOperator type() const { return m_type; }
  527. const RefPtr<Expression>& escape() const { return m_escape; }
  528. virtual ResultOr<Value> evaluate(ExecutionContext&) const override;
  529. private:
  530. MatchOperator m_type;
  531. RefPtr<Expression> m_escape;
  532. };
  533. class NullExpression : public InvertibleNestedExpression {
  534. public:
  535. NullExpression(NonnullRefPtr<Expression> expression, bool invert_expression)
  536. : InvertibleNestedExpression(move(expression), invert_expression)
  537. {
  538. }
  539. };
  540. class IsExpression : public InvertibleNestedDoubleExpression {
  541. public:
  542. IsExpression(NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs, bool invert_expression)
  543. : InvertibleNestedDoubleExpression(move(lhs), move(rhs), invert_expression)
  544. {
  545. }
  546. };
  547. class BetweenExpression : public InvertibleNestedDoubleExpression {
  548. public:
  549. BetweenExpression(NonnullRefPtr<Expression> expression, NonnullRefPtr<Expression> lhs, NonnullRefPtr<Expression> rhs, bool invert_expression)
  550. : InvertibleNestedDoubleExpression(move(lhs), move(rhs), invert_expression)
  551. , m_expression(move(expression))
  552. {
  553. }
  554. const NonnullRefPtr<Expression>& expression() const { return m_expression; }
  555. private:
  556. NonnullRefPtr<Expression> m_expression;
  557. };
  558. class InSelectionExpression : public InvertibleNestedExpression {
  559. public:
  560. InSelectionExpression(NonnullRefPtr<Expression> expression, NonnullRefPtr<Select> select_statement, bool invert_expression)
  561. : InvertibleNestedExpression(move(expression), invert_expression)
  562. , m_select_statement(move(select_statement))
  563. {
  564. }
  565. const NonnullRefPtr<Select>& select_statement() const { return m_select_statement; }
  566. private:
  567. NonnullRefPtr<Select> m_select_statement;
  568. };
  569. class InChainedExpression : public InvertibleNestedExpression {
  570. public:
  571. InChainedExpression(NonnullRefPtr<Expression> expression, NonnullRefPtr<ChainedExpression> expression_chain, bool invert_expression)
  572. : InvertibleNestedExpression(move(expression), invert_expression)
  573. , m_expression_chain(move(expression_chain))
  574. {
  575. }
  576. const NonnullRefPtr<ChainedExpression>& expression_chain() const { return m_expression_chain; }
  577. private:
  578. NonnullRefPtr<ChainedExpression> m_expression_chain;
  579. };
  580. class InTableExpression : public InvertibleNestedExpression {
  581. public:
  582. InTableExpression(NonnullRefPtr<Expression> expression, String schema_name, String table_name, bool invert_expression)
  583. : InvertibleNestedExpression(move(expression), invert_expression)
  584. , m_schema_name(move(schema_name))
  585. , m_table_name(move(table_name))
  586. {
  587. }
  588. const String& schema_name() const { return m_schema_name; }
  589. const String& table_name() const { return m_table_name; }
  590. private:
  591. String m_schema_name;
  592. String m_table_name;
  593. };
  594. //==================================================================================================
  595. // Statements
  596. //==================================================================================================
  597. class Statement : public ASTNode {
  598. public:
  599. ResultOr<ResultSet> execute(AK::NonnullRefPtr<Database> database) const;
  600. virtual ResultOr<ResultSet> execute(ExecutionContext&) const
  601. {
  602. return Result { SQLCommand::Unknown, SQLErrorCode::NotYetImplemented };
  603. }
  604. };
  605. class ErrorStatement final : public Statement {
  606. };
  607. class CreateSchema : public Statement {
  608. public:
  609. CreateSchema(String schema_name, bool is_error_if_schema_exists)
  610. : m_schema_name(move(schema_name))
  611. , m_is_error_if_schema_exists(is_error_if_schema_exists)
  612. {
  613. }
  614. const String& schema_name() const { return m_schema_name; }
  615. bool is_error_if_schema_exists() const { return m_is_error_if_schema_exists; }
  616. ResultOr<ResultSet> execute(ExecutionContext&) const override;
  617. private:
  618. String m_schema_name;
  619. bool m_is_error_if_schema_exists;
  620. };
  621. class CreateTable : public Statement {
  622. public:
  623. CreateTable(String schema_name, String table_name, RefPtr<Select> select_statement, bool is_temporary, bool is_error_if_table_exists)
  624. : m_schema_name(move(schema_name))
  625. , m_table_name(move(table_name))
  626. , m_select_statement(move(select_statement))
  627. , m_is_temporary(is_temporary)
  628. , m_is_error_if_table_exists(is_error_if_table_exists)
  629. {
  630. }
  631. CreateTable(String schema_name, String table_name, NonnullRefPtrVector<ColumnDefinition> columns, bool is_temporary, bool is_error_if_table_exists)
  632. : m_schema_name(move(schema_name))
  633. , m_table_name(move(table_name))
  634. , m_columns(move(columns))
  635. , m_is_temporary(is_temporary)
  636. , m_is_error_if_table_exists(is_error_if_table_exists)
  637. {
  638. }
  639. const String& schema_name() const { return m_schema_name; }
  640. const String& table_name() const { return m_table_name; }
  641. bool has_selection() const { return !m_select_statement.is_null(); }
  642. const RefPtr<Select>& select_statement() const { return m_select_statement; }
  643. bool has_columns() const { return !m_columns.is_empty(); }
  644. const NonnullRefPtrVector<ColumnDefinition>& columns() const { return m_columns; }
  645. bool is_temporary() const { return m_is_temporary; }
  646. bool is_error_if_table_exists() const { return m_is_error_if_table_exists; }
  647. ResultOr<ResultSet> execute(ExecutionContext&) const override;
  648. private:
  649. String m_schema_name;
  650. String m_table_name;
  651. RefPtr<Select> m_select_statement;
  652. NonnullRefPtrVector<ColumnDefinition> m_columns;
  653. bool m_is_temporary;
  654. bool m_is_error_if_table_exists;
  655. };
  656. class AlterTable : public Statement {
  657. public:
  658. const String& schema_name() const { return m_schema_name; }
  659. const String& table_name() const { return m_table_name; }
  660. protected:
  661. AlterTable(String schema_name, String table_name)
  662. : m_schema_name(move(schema_name))
  663. , m_table_name(move(table_name))
  664. {
  665. }
  666. private:
  667. String m_schema_name;
  668. String m_table_name;
  669. };
  670. class RenameTable : public AlterTable {
  671. public:
  672. RenameTable(String schema_name, String table_name, String new_table_name)
  673. : AlterTable(move(schema_name), move(table_name))
  674. , m_new_table_name(move(new_table_name))
  675. {
  676. }
  677. const String& new_table_name() const { return m_new_table_name; }
  678. private:
  679. String m_new_table_name;
  680. };
  681. class RenameColumn : public AlterTable {
  682. public:
  683. RenameColumn(String schema_name, String table_name, String column_name, String new_column_name)
  684. : AlterTable(move(schema_name), move(table_name))
  685. , m_column_name(move(column_name))
  686. , m_new_column_name(move(new_column_name))
  687. {
  688. }
  689. const String& column_name() const { return m_column_name; }
  690. const String& new_column_name() const { return m_new_column_name; }
  691. private:
  692. String m_column_name;
  693. String m_new_column_name;
  694. };
  695. class AddColumn : public AlterTable {
  696. public:
  697. AddColumn(String schema_name, String table_name, NonnullRefPtr<ColumnDefinition> column)
  698. : AlterTable(move(schema_name), move(table_name))
  699. , m_column(move(column))
  700. {
  701. }
  702. const NonnullRefPtr<ColumnDefinition>& column() const { return m_column; }
  703. private:
  704. NonnullRefPtr<ColumnDefinition> m_column;
  705. };
  706. class DropColumn : public AlterTable {
  707. public:
  708. DropColumn(String schema_name, String table_name, String column_name)
  709. : AlterTable(move(schema_name), move(table_name))
  710. , m_column_name(move(column_name))
  711. {
  712. }
  713. const String& column_name() const { return m_column_name; }
  714. private:
  715. String m_column_name;
  716. };
  717. class DropTable : public Statement {
  718. public:
  719. DropTable(String schema_name, String table_name, bool is_error_if_table_does_not_exist)
  720. : m_schema_name(move(schema_name))
  721. , m_table_name(move(table_name))
  722. , m_is_error_if_table_does_not_exist(is_error_if_table_does_not_exist)
  723. {
  724. }
  725. const String& schema_name() const { return m_schema_name; }
  726. const String& table_name() const { return m_table_name; }
  727. bool is_error_if_table_does_not_exist() const { return m_is_error_if_table_does_not_exist; }
  728. private:
  729. String m_schema_name;
  730. String m_table_name;
  731. bool m_is_error_if_table_does_not_exist;
  732. };
  733. enum class ConflictResolution {
  734. Abort,
  735. Fail,
  736. Ignore,
  737. Replace,
  738. Rollback,
  739. };
  740. class Insert : public Statement {
  741. public:
  742. Insert(RefPtr<CommonTableExpressionList> common_table_expression_list, ConflictResolution conflict_resolution, String schema_name, String table_name, String alias, Vector<String> column_names, NonnullRefPtrVector<ChainedExpression> chained_expressions)
  743. : m_common_table_expression_list(move(common_table_expression_list))
  744. , m_conflict_resolution(conflict_resolution)
  745. , m_schema_name(move(schema_name))
  746. , m_table_name(move(table_name))
  747. , m_alias(move(alias))
  748. , m_column_names(move(column_names))
  749. , m_chained_expressions(move(chained_expressions))
  750. {
  751. }
  752. Insert(RefPtr<CommonTableExpressionList> common_table_expression_list, ConflictResolution conflict_resolution, String schema_name, String table_name, String alias, Vector<String> column_names, RefPtr<Select> select_statement)
  753. : m_common_table_expression_list(move(common_table_expression_list))
  754. , m_conflict_resolution(conflict_resolution)
  755. , m_schema_name(move(schema_name))
  756. , m_table_name(move(table_name))
  757. , m_alias(move(alias))
  758. , m_column_names(move(column_names))
  759. , m_select_statement(move(select_statement))
  760. {
  761. }
  762. Insert(RefPtr<CommonTableExpressionList> common_table_expression_list, ConflictResolution conflict_resolution, String schema_name, String table_name, String alias, Vector<String> column_names)
  763. : m_common_table_expression_list(move(common_table_expression_list))
  764. , m_conflict_resolution(conflict_resolution)
  765. , m_schema_name(move(schema_name))
  766. , m_table_name(move(table_name))
  767. , m_alias(move(alias))
  768. , m_column_names(move(column_names))
  769. {
  770. }
  771. const RefPtr<CommonTableExpressionList>& common_table_expression_list() const { return m_common_table_expression_list; }
  772. ConflictResolution conflict_resolution() const { return m_conflict_resolution; }
  773. const String& schema_name() const { return m_schema_name; }
  774. const String& table_name() const { return m_table_name; }
  775. const String& alias() const { return m_alias; }
  776. const Vector<String>& column_names() const { return m_column_names; }
  777. bool default_values() const { return !has_expressions() && !has_selection(); };
  778. bool has_expressions() const { return !m_chained_expressions.is_empty(); }
  779. const NonnullRefPtrVector<ChainedExpression>& chained_expressions() const { return m_chained_expressions; }
  780. bool has_selection() const { return !m_select_statement.is_null(); }
  781. const RefPtr<Select>& select_statement() const { return m_select_statement; }
  782. virtual ResultOr<ResultSet> execute(ExecutionContext&) const override;
  783. private:
  784. RefPtr<CommonTableExpressionList> m_common_table_expression_list;
  785. ConflictResolution m_conflict_resolution;
  786. String m_schema_name;
  787. String m_table_name;
  788. String m_alias;
  789. Vector<String> m_column_names;
  790. NonnullRefPtrVector<ChainedExpression> m_chained_expressions;
  791. RefPtr<Select> m_select_statement;
  792. };
  793. class Update : public Statement {
  794. public:
  795. struct UpdateColumns {
  796. Vector<String> column_names;
  797. NonnullRefPtr<Expression> expression;
  798. };
  799. Update(RefPtr<CommonTableExpressionList> common_table_expression_list, ConflictResolution conflict_resolution, NonnullRefPtr<QualifiedTableName> qualified_table_name, Vector<UpdateColumns> update_columns, NonnullRefPtrVector<TableOrSubquery> table_or_subquery_list, RefPtr<Expression> where_clause, RefPtr<ReturningClause> returning_clause)
  800. : m_common_table_expression_list(move(common_table_expression_list))
  801. , m_conflict_resolution(conflict_resolution)
  802. , m_qualified_table_name(move(qualified_table_name))
  803. , m_update_columns(move(update_columns))
  804. , m_table_or_subquery_list(move(table_or_subquery_list))
  805. , m_where_clause(move(where_clause))
  806. , m_returning_clause(move(returning_clause))
  807. {
  808. }
  809. const RefPtr<CommonTableExpressionList>& common_table_expression_list() const { return m_common_table_expression_list; }
  810. ConflictResolution conflict_resolution() const { return m_conflict_resolution; }
  811. const NonnullRefPtr<QualifiedTableName>& qualified_table_name() const { return m_qualified_table_name; }
  812. const Vector<UpdateColumns>& update_columns() const { return m_update_columns; }
  813. const NonnullRefPtrVector<TableOrSubquery>& table_or_subquery_list() const { return m_table_or_subquery_list; }
  814. const RefPtr<Expression>& where_clause() const { return m_where_clause; }
  815. const RefPtr<ReturningClause>& returning_clause() const { return m_returning_clause; }
  816. private:
  817. RefPtr<CommonTableExpressionList> m_common_table_expression_list;
  818. ConflictResolution m_conflict_resolution;
  819. NonnullRefPtr<QualifiedTableName> m_qualified_table_name;
  820. Vector<UpdateColumns> m_update_columns;
  821. NonnullRefPtrVector<TableOrSubquery> m_table_or_subquery_list;
  822. RefPtr<Expression> m_where_clause;
  823. RefPtr<ReturningClause> m_returning_clause;
  824. };
  825. class Delete : public Statement {
  826. public:
  827. Delete(RefPtr<CommonTableExpressionList> common_table_expression_list, NonnullRefPtr<QualifiedTableName> qualified_table_name, RefPtr<Expression> where_clause, RefPtr<ReturningClause> returning_clause)
  828. : m_common_table_expression_list(move(common_table_expression_list))
  829. , m_qualified_table_name(move(qualified_table_name))
  830. , m_where_clause(move(where_clause))
  831. , m_returning_clause(move(returning_clause))
  832. {
  833. }
  834. const RefPtr<CommonTableExpressionList>& common_table_expression_list() const { return m_common_table_expression_list; }
  835. const NonnullRefPtr<QualifiedTableName>& qualified_table_name() const { return m_qualified_table_name; }
  836. const RefPtr<Expression>& where_clause() const { return m_where_clause; }
  837. const RefPtr<ReturningClause>& returning_clause() const { return m_returning_clause; }
  838. private:
  839. RefPtr<CommonTableExpressionList> m_common_table_expression_list;
  840. NonnullRefPtr<QualifiedTableName> m_qualified_table_name;
  841. RefPtr<Expression> m_where_clause;
  842. RefPtr<ReturningClause> m_returning_clause;
  843. };
  844. class Select : public Statement {
  845. public:
  846. Select(RefPtr<CommonTableExpressionList> common_table_expression_list, bool select_all, NonnullRefPtrVector<ResultColumn> result_column_list, NonnullRefPtrVector<TableOrSubquery> table_or_subquery_list, RefPtr<Expression> where_clause, RefPtr<GroupByClause> group_by_clause, NonnullRefPtrVector<OrderingTerm> ordering_term_list, RefPtr<LimitClause> limit_clause)
  847. : m_common_table_expression_list(move(common_table_expression_list))
  848. , m_select_all(move(select_all))
  849. , m_result_column_list(move(result_column_list))
  850. , m_table_or_subquery_list(move(table_or_subquery_list))
  851. , m_where_clause(move(where_clause))
  852. , m_group_by_clause(move(group_by_clause))
  853. , m_ordering_term_list(move(ordering_term_list))
  854. , m_limit_clause(move(limit_clause))
  855. {
  856. }
  857. const RefPtr<CommonTableExpressionList>& common_table_expression_list() const { return m_common_table_expression_list; }
  858. bool select_all() const { return m_select_all; }
  859. const NonnullRefPtrVector<ResultColumn>& result_column_list() const { return m_result_column_list; }
  860. const NonnullRefPtrVector<TableOrSubquery>& table_or_subquery_list() const { return m_table_or_subquery_list; }
  861. const RefPtr<Expression>& where_clause() const { return m_where_clause; }
  862. const RefPtr<GroupByClause>& group_by_clause() const { return m_group_by_clause; }
  863. const NonnullRefPtrVector<OrderingTerm>& ordering_term_list() const { return m_ordering_term_list; }
  864. const RefPtr<LimitClause>& limit_clause() const { return m_limit_clause; }
  865. ResultOr<ResultSet> execute(ExecutionContext&) const override;
  866. private:
  867. RefPtr<CommonTableExpressionList> m_common_table_expression_list;
  868. bool m_select_all;
  869. NonnullRefPtrVector<ResultColumn> m_result_column_list;
  870. NonnullRefPtrVector<TableOrSubquery> m_table_or_subquery_list;
  871. RefPtr<Expression> m_where_clause;
  872. RefPtr<GroupByClause> m_group_by_clause;
  873. NonnullRefPtrVector<OrderingTerm> m_ordering_term_list;
  874. RefPtr<LimitClause> m_limit_clause;
  875. };
  876. class DescribeTable : public Statement {
  877. public:
  878. DescribeTable(NonnullRefPtr<QualifiedTableName> qualified_table_name)
  879. : m_qualified_table_name(move(qualified_table_name))
  880. {
  881. }
  882. NonnullRefPtr<QualifiedTableName> qualified_table_name() const { return m_qualified_table_name; }
  883. ResultOr<ResultSet> execute(ExecutionContext&) const override;
  884. private:
  885. NonnullRefPtr<QualifiedTableName> m_qualified_table_name;
  886. };
  887. }