From cacda5594af2d1bfc78019a897af92549f3de157 Mon Sep 17 00:00:00 2001 From: Brieuc Dubois Date: Thu, 14 Mar 2024 23:46:27 +0100 Subject: [PATCH] Fix select for non existing or empty tables --- solution.pl | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/solution.pl b/solution.pl index a70ba03..06af34b 100644 --- a/solution.pl +++ b/solution.pl @@ -233,6 +233,17 @@ For example: findall(Values, selec(persons,[+id,+first],[],Values), Projections) returns: Projections = [[0, "Jeffrey"], [1, "Lorena"], [2, "Joseph"], ... */ +selec(Table, _, _, _) :- + \+ tabl(Table, _), + throw("Table doesn't exist"), + !. + +selec(Table, _, _, _) :- + tabl(Table, Cols), + length(Cols, L), + \+ current_predicate(Table/L), + !. + selec(Table, Selector, Cond, Projection) :- tabl(Table, Cols), row(Table, R), @@ -246,8 +257,6 @@ selec(Table, *, Cond, Projection) :- does_match(Cond, Table, R), Projection = Cols/R. -%base case -selector(_,_,[],ColumnNames, ColumnValue, ColumnNames, ColumnValue):-!. /** * Row: A row from the table * Cols: name of the column of the table @@ -257,6 +266,9 @@ selector(_,_,[],ColumnNames, ColumnValue, ColumnNames, ColumnValue):-!. * ColumnNames: List returning the column(s) name(s) * ColumnValue: List returning the row values for each column */ + +selector(_,_,[],ColumnNames, ColumnValue, ColumnNames, ColumnValue):-!. + selector(Row, Cols, [+Selector|Selectors], AccCols, AccVals, ColumnNames, ColumnValue):- (member(Selector, Cols) -> %find the value corresponding to the column in the row