From d5351f3dee13963331639796d9e2ec7e27590e54 Mon Sep 17 00:00:00 2001 From: Brieuc Dubois Date: Thu, 14 Mar 2024 18:06:34 +0100 Subject: [PATCH 1/2] delete/2 --- solution.pl | 40 +++++++++++++++++++++++++++++++++++----- tests.pl | 6 +++--- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/solution.pl b/solution.pl index 76b7af9..7a98b6a 100644 --- a/solution.pl +++ b/solution.pl @@ -43,10 +43,10 @@ If the given table does not exist, the predicate should fail. row(Table, Row) :- tabl(Table, Cols) -> ( - length(Cols, L), - length(Row, L), + length(Cols, L), + length(Row, L), apply(Table, Row) - ); fail. + ); throw("Table doesn't exist"). :- dynamic rows/1. /* @@ -130,9 +130,39 @@ prompt, but which may include selectors. Selectors are terms of the form */ delete(Table, Conds) :- (tabl(Table, _) -> - (rows(tables, Row), \+ (member(Cond, Conds), \+ Cond) -> retract(Row); true); + row(Table, Row), + does_match(Conds, Table, Row) + ; throw("Table doesn't exist") - ). + ); true. + + +does_match([], Table, Row) :- + Term =.. [Table | Row], + retract(Term), + fail. + +does_match([Cond|Rest], Table, Row) :- + % Extract the operator, field and value from the condition + Cond =.. [Operator | T], + [+F, S] = T, + + % Get the columns of the table + tabl(Table, Cols), + + % Get the index of the field in the columns + member(F, Cols), + nth0(Index, Cols, F), + + % Get the value of the field in the row + nth0(Index, Row, Value), + + % Apply the operator to the value and the selector + apply(Operator, [Value, S]), + + % Check the next condition + does_match(Rest, Table, Row). + :- dynamic selec/4. /* diff --git a/tests.pl b/tests.pl index 7be71b6..e3446cc 100644 --- a/tests.pl +++ b/tests.pl @@ -52,16 +52,16 @@ test(drop_inexistant, throws(_)) :- drop(foobar). % Test if row/2 returns the different rows of a given table. -test(rows, cleanup(cdrop(foo))) :- +test(row, cleanup(cdrop(foo))) :- create_table(foo, [bar, baz]), insert(foo, [1, 2]), insert(foo, [3, 4]), - rows(foo, Rows), + row(foo, Rows), assertion(length(Rows,2)). % Test if row/2 fails on an unknown table name. test(row_inexistant, throws(_)) :- - rows(foo, _Row). + row(foo, _Row). % Test if rows/1 works when called for a given table name. test(rows, cleanup(cdrop(foo))) :- From eeaba5ee465417cebbe7a40b77aa5017b62acd4f Mon Sep 17 00:00:00 2001 From: Brieuc Dubois Date: Thu, 14 Mar 2024 18:11:33 +0100 Subject: [PATCH 2/2] Move deletion logic to delete/2 --- solution.pl | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/solution.pl b/solution.pl index 7a98b6a..509fdaa 100644 --- a/solution.pl +++ b/solution.pl @@ -131,16 +131,18 @@ prompt, but which may include selectors. Selectors are terms of the form delete(Table, Conds) :- (tabl(Table, _) -> row(Table, Row), - does_match(Conds, Table, Row) + does_match(Conds, Table, Row), + Term =.. [Table | Row], + retract(Term), + fail ; throw("Table doesn't exist") - ); true. + ). + +delete(_, _). -does_match([], Table, Row) :- - Term =.. [Table | Row], - retract(Term), - fail. +does_match([], _, _) :- !. does_match([Cond|Rest], Table, Row) :- % Extract the operator, field and value from the condition