Merge delete and fixe row tests
This commit is contained in:
commit
84a3075bc8
66
solution.pl
66
solution.pl
|
@ -65,10 +65,10 @@ If the given table does not exist, the predicate should fail.
|
||||||
row(Table, Row) :-
|
row(Table, Row) :-
|
||||||
tabl(Table, Cols) ->
|
tabl(Table, Cols) ->
|
||||||
(
|
(
|
||||||
length(Cols, L),
|
length(Cols, L),
|
||||||
length(Row, L),
|
length(Row, L),
|
||||||
apply(Table, Row)
|
apply(Table, Row)
|
||||||
); fail.
|
); throw("Table doesn't exist").
|
||||||
|
|
||||||
:- dynamic rows/1.
|
:- dynamic rows/1.
|
||||||
/*
|
/*
|
||||||
|
@ -151,36 +151,42 @@ prompt, but which may include selectors. Selectors are terms of the form
|
||||||
(See tests.pl for some concrete usage examples.)
|
(See tests.pl for some concrete usage examples.)
|
||||||
*/
|
*/
|
||||||
delete(Table, Conds) :-
|
delete(Table, Conds) :-
|
||||||
(tabl(Table, _) ->
|
(tabl(Table, _) ->
|
||||||
row(Table, Row),
|
row(Table, Row),
|
||||||
does_match(Conds, Table, Row)
|
does_match(Conds, Table, Row),
|
||||||
;
|
Term =.. [Table | Row],
|
||||||
throw("Table doesn't exist")
|
retract(Term),
|
||||||
).
|
fail
|
||||||
|
;
|
||||||
|
throw("Table doesn't exist")
|
||||||
|
).
|
||||||
|
|
||||||
|
delete(_, _).
|
||||||
|
|
||||||
|
|
||||||
does_match([], _, _) :- !.
|
does_match([], _, _) :- !.
|
||||||
|
|
||||||
|
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).
|
||||||
|
|
||||||
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.
|
:- dynamic selec/4.
|
||||||
/*
|
/*
|
||||||
|
|
6
tests.pl
6
tests.pl
|
@ -52,16 +52,16 @@ test(drop_inexistant, throws(_)) :-
|
||||||
drop(foobar).
|
drop(foobar).
|
||||||
|
|
||||||
% Test if row/2 returns the different rows of a given table.
|
% 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]),
|
create_table(foo, [bar, baz]),
|
||||||
insert(foo, [1, 2]),
|
insert(foo, [1, 2]),
|
||||||
insert(foo, [3, 4]),
|
insert(foo, [3, 4]),
|
||||||
rows(foo, Rows),
|
row(foo, Rows),
|
||||||
assertion(length(Rows,2)).
|
assertion(length(Rows,2)).
|
||||||
|
|
||||||
% Test if row/2 fails on an unknown table name.
|
% Test if row/2 fails on an unknown table name.
|
||||||
test(row_inexistant, throws(_)) :-
|
test(row_inexistant, throws(_)) :-
|
||||||
rows(foo, _Row).
|
row(foo, _Row).
|
||||||
|
|
||||||
% Test if rows/1 works when called for a given table name.
|
% Test if rows/1 works when called for a given table name.
|
||||||
test(rows, cleanup(cdrop(foo))) :-
|
test(rows, cleanup(cdrop(foo))) :-
|
||||||
|
|
Loading…
Reference in New Issue