Fix select for non existing or empty tables

This commit is contained in:
Brieuc Dubois 2024-03-14 23:46:27 +01:00
parent 1e89e0f6b7
commit cacda5594a
1 changed files with 14 additions and 2 deletions

View File

@ -233,6 +233,17 @@ For example:
findall(Values, selec(persons,[+id,+first],[],Values), Projections) findall(Values, selec(persons,[+id,+first],[],Values), Projections)
returns: Projections = [[0, "Jeffrey"], [1, "Lorena"], [2, "Joseph"], ... 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) :- selec(Table, Selector, Cond, Projection) :-
tabl(Table, Cols), tabl(Table, Cols),
row(Table, R), row(Table, R),
@ -246,8 +257,6 @@ selec(Table, *, Cond, Projection) :-
does_match(Cond, Table, R), does_match(Cond, Table, R),
Projection = Cols/R. Projection = Cols/R.
%base case
selector(_,_,[],ColumnNames, ColumnValue, ColumnNames, ColumnValue):-!.
/** /**
* Row: A row from the table * Row: A row from the table
* Cols: name of the column of 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) * ColumnNames: List returning the column(s) name(s)
* ColumnValue: List returning the row values for each column * ColumnValue: List returning the row values for each column
*/ */
selector(_,_,[],ColumnNames, ColumnValue, ColumnNames, ColumnValue):-!.
selector(Row, Cols, [+Selector|Selectors], AccCols, AccVals, ColumnNames, ColumnValue):- selector(Row, Cols, [+Selector|Selectors], AccCols, AccVals, ColumnNames, ColumnValue):-
(member(Selector, Cols) -> (member(Selector, Cols) ->
%find the value corresponding to the column in the row %find the value corresponding to the column in the row