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
% starts with Delim (a list), but does not consume
% any input.
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, []) }.
/**
* Authors:
* Dubois Brieuc
* Dubois Simon
*/
:- 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.
*/
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
table with the specified list of column names (order matters!).
A column name is always an atom.
If a table with the given name already exists, the predicate must throw a
descriptive exception (use throw/1).
All exceptions must have a descriptive error message.
If a table with the given name already exists, the predicate throws a
descriptive exception (using throw/1).
*/
create_table(Table, Cols) :-
(tabl(Table, _) -> throw("Table already exists"));
@ -81,8 +64,8 @@ row(Table, Row) :-
:- dynamic rows/1.
/*
Displays all rows in the given table, one per line (use writeln/1).
If the given table does not exist, the predicate must throw a descriptive
Displays all rows in the given table, one per line (using writeln/1).
If the given table does not exist, the predicate throws a descriptive
exception.
*/
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
corresponding columns in the table (in the order in which the columns
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.
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) :-
(tabl(Table, Cols) ->
@ -127,10 +110,7 @@ insert(Table, Row) :-
/*
When this predicate is executed, the effect will be the deletion of the given
table.
Do make sure that all of its rows are deleted as well, so that they dont
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
If the given table does not exist, the predicate throws a descriptive
exception.
*/
drop(Table) :-
@ -144,7 +124,7 @@ drop(Table) :-
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
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.
*/
delete(Table) :-
@ -157,13 +137,12 @@ delete(Table) :-
/*
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
must still exist after.
If the given table does not exist, the predicate must throw a descriptive
still exist after.
If the given table does not exist, the predicate throws a descriptive
exception.
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
+<column>where <column>should be replaced by a column name.
(See tests.pl for some concrete usage examples.)
*/
delete(Table, Conds) :-
(tabl(Table, _) ->
@ -205,13 +184,11 @@ does_match([Cond|Rest], Table, Row) :-
:- 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.
Selectors is either * or a list of selectors. These define the resulting
projection. * means: select all column names from the table. Other
selectors explicitly specify which columns to pick. (See above for what
selectors look like.) For example, +name would select the column named
selectors explicitly specify which columns to pick.
For example, +name would select the column named
name.
Conds has the same form as in delete/2 and works the same way: only
rows that match all conditions are selected.
@ -249,7 +226,10 @@ selec(Table, Selector, Cond, Projection) :-
row(Table, R),
does_match(Cond, Table, R),
selector(R,Cols, Selector, [],[],ColumnNames,ColumnValue),
Projection = ColumnNames/ColumnValue.
reverse(ColumnNames, Names),
reverse(ColumnValue, Values),
Projection = Names/Values.
selec(Table, *, Cond, Projection) :-
tabl(Table, Cols),
@ -264,7 +244,9 @@ selec(Table, *, Cond, Projection) :-
* AccCols: acumulator list for the column(s) name(s)
* AccVals: acumulator list for the row(s) values
* 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):-!.
@ -290,7 +272,10 @@ selec(TableOrTables, Selectors, Projection) :-
tabl(TableOrTables, Cols),
row(TableOrTables, R),
selector(R,Cols, Selectors, [],[],ColumnNames,ColumnValue),
Projection = ColumnNames/ColumnValue.
reverse(ColumnNames, Names),
reverse(ColumnValue, Values),
Projection = Names/Values.
selec(Table, *, Projection) :-
tabl(Table, Cols),
@ -396,7 +381,8 @@ parse_query(Query, insert, [AtomTable, Values]):-
split_string(Query, " ", '",();', SplitQuery),
append(["INSERT", "INTO"], L1, SplitQuery),
L1 = [Table | L2],
append(_, ["VALUES"|Values], L2),
append(_, ["VALUES"|Values1], L2),
maplist(is_number, Values, Values1),
atom_string(AtomTable, Table).
add_plus(+A, A).