31/35 test passed version. inverted list for selec

This commit is contained in:
duboissim 2024-03-15 22:28:33 +01:00
parent a1682eaef5
commit 1ce0b1db2f
2 changed files with 36 additions and 45 deletions

5
note.txt Normal file
View File

@ -0,0 +1,5 @@
When inserting a row we don't put a + sign before the name
When we return row of an empty table instead of having [] we have [_] same thing with drop
Currently a condtion in a query as to contain no space in order to function properly
Ex: the condtion +name="Dakota" will work but the conditions +name = "Dakota" or
+name="North Dakota" will not.

View File

@ -1,28 +1,12 @@
:- use_module(library(dcg/basics)). /**
% DCG rule that checks if the remainder of the input * Authors:
% starts with Delim (a list), but does not consume * Dubois Brieuc
% any input. * Dubois Simon
lookahead(Delim , L, L) :- */
prefix(Delim , L).
% Checks if the remainder of the input starts with one
% of the items in the supplied list (as per lookahead /3).
lookaheads([H|T]) -->
lookahead(H), ! ; lookaheads(T).
% Reads text until one of the delimiters in Delims is
% encountered , then unifies R with the Prolog term parsed
% from the text.
% ! Will crash the parse if a delimiter is encountered
% but the intervening text is not a Prolog term (you
% dont have this handle this case).
prolog_term(R, Delims) -->
string(S), lookaheads(Delims), !, {
read_term_from_atom(S, R, []) }.
:- dynamic tables/0. :- dynamic tables/0.
/* /*
Prints the names of all existing tables, one per line (use writeln/1). Prints the names of all existing tables, one per line (using writeln/1).
A table name is always an atom. A table name is always an atom.
*/ */
tables :- tabl(X, _), writeln(X). tables :- tabl(X, _), writeln(X).
@ -38,9 +22,8 @@ tables(Tables) :- findall(X, tabl(X, _), Tables).
When this predicate is executed, the effect will be the creation of a new When this predicate is executed, the effect will be the creation of a new
table with the specified list of column names (order matters!). table with the specified list of column names (order matters!).
A column name is always an atom. A column name is always an atom.
If a table with the given name already exists, the predicate must throw a If a table with the given name already exists, the predicate throws a
descriptive exception (use throw/1). descriptive exception (using throw/1).
All exceptions must have a descriptive error message.
*/ */
create_table(Table, Cols) :- create_table(Table, Cols) :-
(tabl(Table, _) -> throw("Table already exists")); (tabl(Table, _) -> throw("Table already exists"));
@ -81,8 +64,8 @@ row(Table, Row) :-
:- dynamic rows/1. :- dynamic rows/1.
/* /*
Displays all rows in the given table, one per line (use writeln/1). Displays all rows in the given table, one per line (using writeln/1).
If the given table does not exist, the predicate must throw a descriptive If the given table does not exist, the predicate throws a descriptive
exception. exception.
*/ */
rows(Table) :- rows(Table) :-
@ -108,10 +91,10 @@ When this predicate is executed, the effect will be the addition of a given
row in the given table. The given row is a list of values for each of the row in the given table. The given row is a list of values for each of the
corresponding columns in the table (in the order in which the columns corresponding columns in the table (in the order in which the columns
were supplied to create table/2). were supplied to create table/2).
If the given table does not exist, the predicate must throw a descriptive If the given table does not exist, the predicate throws a descriptive
exception. exception.
If the row does not have as many elements as the number of columns in If the row does not have as many elements as the number of columns in
the table, the predicate must throw a descriptive exception. the table, the predicate throws a descriptive exception.
*/ */
insert(Table, Row) :- insert(Table, Row) :-
(tabl(Table, Cols) -> (tabl(Table, Cols) ->
@ -127,10 +110,7 @@ insert(Table, Row) :-
/* /*
When this predicate is executed, the effect will be the deletion of the given When this predicate is executed, the effect will be the deletion of the given
table. table.
Do make sure that all of its rows are deleted as well, so that they dont If the given table does not exist, the predicate throws a descriptive
magically reappear again if you would recreate a table with the same name
and signature later on.
If the given table does not exist, the predicate must throw a descriptive
exception. exception.
*/ */
drop(Table) :- drop(Table) :-
@ -144,7 +124,7 @@ drop(Table) :-
When this predicate is executed, the effect will be the deletion of all rows When this predicate is executed, the effect will be the deletion of all rows
in the given table. The table itself should still exist after, but with no in the given table. The table itself should still exist after, but with no
more rows. more rows.
If the given table does not exist, the predicate must throw a descriptive If the given table does not exist, the predicate throws a descriptive
exception. exception.
*/ */
delete(Table) :- delete(Table) :-
@ -157,13 +137,12 @@ delete(Table) :-
/* /*
When this predicate is executed, the effect will be the deletion of all rows When this predicate is executed, the effect will be the deletion of all rows
from the given table that match all of the given conditions. The table from the given table that match all of the given conditions. The table
must still exist after. still exist after.
If the given table does not exist, the predicate must throw a descriptive If the given table does not exist, the predicate throws a descriptive
exception. exception.
A condition is any Prolog predicate that could have been typed at the A condition is any Prolog predicate that could have been typed at the
prompt, but which may include selectors. Selectors are terms of the form prompt, but which may include selectors. Selectors are terms of the form
+<column>where <column>should be replaced by a column name. +<column>where <column>should be replaced by a column name.
(See tests.pl for some concrete usage examples.)
*/ */
delete(Table, Conds) :- delete(Table, Conds) :-
(tabl(Table, _) -> (tabl(Table, _) ->
@ -205,13 +184,11 @@ does_match([Cond|Rest], Table, Row) :-
:- dynamic selec/4. :- dynamic selec/4.
/* /*
Note that the name of this predicate is selec (without t) for the simple
reason that select/4 is already a built-in Prolog predicate.
Table is the name of a single table. Table is the name of a single table.
Selectors is either * or a list of selectors. These define the resulting Selectors is either * or a list of selectors. These define the resulting
projection. * means: select all column names from the table. Other projection. * means: select all column names from the table. Other
selectors explicitly specify which columns to pick. (See above for what selectors explicitly specify which columns to pick.
selectors look like.) For example, +name would select the column named For example, +name would select the column named
name. name.
Conds has the same form as in delete/2 and works the same way: only Conds has the same form as in delete/2 and works the same way: only
rows that match all conditions are selected. rows that match all conditions are selected.
@ -249,7 +226,10 @@ selec(Table, Selector, Cond, Projection) :-
row(Table, R), row(Table, R),
does_match(Cond, Table, R), does_match(Cond, Table, R),
selector(R,Cols, Selector, [],[],ColumnNames,ColumnValue), selector(R,Cols, Selector, [],[],ColumnNames,ColumnValue),
Projection = ColumnNames/ColumnValue. reverse(ColumnNames, Names),
reverse(ColumnValue, Values),
Projection = Names/Values.
selec(Table, *, Cond, Projection) :- selec(Table, *, Cond, Projection) :-
tabl(Table, Cols), tabl(Table, Cols),
@ -265,6 +245,8 @@ selec(Table, *, Cond, Projection) :-
* AccVals: acumulator list for the row(s) values * AccVals: acumulator list for the row(s) values
* 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
*
* This predicate return
*/ */
selector(_,_,[],ColumnNames, ColumnValue, ColumnNames, ColumnValue):-!. selector(_,_,[],ColumnNames, ColumnValue, ColumnNames, ColumnValue):-!.
@ -290,7 +272,10 @@ selec(TableOrTables, Selectors, Projection) :-
tabl(TableOrTables, Cols), tabl(TableOrTables, Cols),
row(TableOrTables, R), row(TableOrTables, R),
selector(R,Cols, Selectors, [],[],ColumnNames,ColumnValue), selector(R,Cols, Selectors, [],[],ColumnNames,ColumnValue),
Projection = ColumnNames/ColumnValue. reverse(ColumnNames, Names),
reverse(ColumnValue, Values),
Projection = Names/Values.
selec(Table, *, Projection) :- selec(Table, *, Projection) :-
tabl(Table, Cols), tabl(Table, Cols),
@ -396,7 +381,8 @@ parse_query(Query, insert, [AtomTable, Values]):-
split_string(Query, " ", '",();', SplitQuery), split_string(Query, " ", '",();', SplitQuery),
append(["INSERT", "INTO"], L1, SplitQuery), append(["INSERT", "INTO"], L1, SplitQuery),
L1 = [Table | L2], L1 = [Table | L2],
append(_, ["VALUES"|Values], L2), append(_, ["VALUES"|Values1], L2),
maplist(is_number, Values, Values1),
atom_string(AtomTable, Table). atom_string(AtomTable, Table).
add_plus(+A, A). add_plus(+A, A).