mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:17:44 +00:00
LibSQL: Partially implement the DELETE command
This implements enough to delete rows filtered by a WHERE clause.
This commit is contained in:
parent
6d3f68cc11
commit
aba7f11a50
6 changed files with 190 additions and 0 deletions
|
@ -1017,6 +1017,8 @@ public:
|
|||
RefPtr<Expression> const& where_clause() const { return m_where_clause; }
|
||||
RefPtr<ReturningClause> const& returning_clause() const { return m_returning_clause; }
|
||||
|
||||
virtual ResultOr<ResultSet> execute(ExecutionContext&) const override;
|
||||
|
||||
private:
|
||||
RefPtr<CommonTableExpressionList> m_common_table_expression_list;
|
||||
NonnullRefPtr<QualifiedTableName> m_qualified_table_name;
|
||||
|
|
39
Userland/Libraries/LibSQL/AST/Delete.cpp
Normal file
39
Userland/Libraries/LibSQL/AST/Delete.cpp
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibSQL/AST/AST.h>
|
||||
#include <LibSQL/Database.h>
|
||||
#include <LibSQL/Meta.h>
|
||||
#include <LibSQL/Row.h>
|
||||
|
||||
namespace SQL::AST {
|
||||
|
||||
ResultOr<ResultSet> Delete::execute(ExecutionContext& context) const
|
||||
{
|
||||
auto const& schema_name = m_qualified_table_name->schema_name();
|
||||
auto const& table_name = m_qualified_table_name->table_name();
|
||||
auto table_def = TRY(context.database->get_table(schema_name, table_name));
|
||||
|
||||
ResultSet result { SQLCommand::Delete };
|
||||
|
||||
for (auto& table_row : TRY(context.database->select_all(*table_def))) {
|
||||
context.current_row = &table_row;
|
||||
|
||||
if (auto const& where_clause = this->where_clause()) {
|
||||
auto where_result = TRY(where_clause->evaluate(context)).to_bool();
|
||||
if (!where_result.has_value() || !where_result.value())
|
||||
continue;
|
||||
}
|
||||
|
||||
TRY(context.database->remove(table_row));
|
||||
|
||||
// FIXME: Implement the RETURNING clause.
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue