selec implem
This commit is contained in:
parent
b100bf03d8
commit
d40cbb70df
91
solution.pl
91
solution.pl
|
@ -152,10 +152,27 @@ prompt, but which may include selectors. Selectors are terms of the form
|
||||||
*/
|
*/
|
||||||
delete(Table, Conds) :-
|
delete(Table, Conds) :-
|
||||||
(tabl(Table, _) ->
|
(tabl(Table, _) ->
|
||||||
(rows(tables, Row), \+ (member(Cond, Conds), \+ Cond) -> retract(Row); true);
|
row(Table, Row),
|
||||||
|
does_match(Conds, Table, Row)
|
||||||
|
;
|
||||||
throw("Table doesn't exist")
|
throw("Table doesn't exist")
|
||||||
).
|
).
|
||||||
|
|
||||||
|
|
||||||
|
does_match([], _, Row) :- !.
|
||||||
|
|
||||||
|
does_match([Cond|Rest], Table, Row) :-
|
||||||
|
% Extract the operator, field and value from the condition
|
||||||
|
Cond =.. [Operator | T],
|
||||||
|
[+F, S] = T,
|
||||||
|
|
||||||
|
tabl(Table, Cols),
|
||||||
|
member(F, Cols),
|
||||||
|
nth0(Index, Cols, F),
|
||||||
|
nth0(Index, Row, Value),
|
||||||
|
apply(Operator, [Value, S]),
|
||||||
|
does_match(Rest, Table, Row).
|
||||||
|
|
||||||
:- dynamic selec/4.
|
:- dynamic selec/4.
|
||||||
/*
|
/*
|
||||||
Note that the name of this predicate is selec (without t) for the simple
|
Note that the name of this predicate is selec (without t) for the simple
|
||||||
|
@ -186,86 +203,36 @@ 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, Selectors, Conds, Projection) :-
|
selec(Table, Selector, Cond, Projection) :-
|
||||||
selec_help(Table, Selectors,[], [], ColumnNames, ColumnValue),
|
tabl(Table, Cols),
|
||||||
%remove combinations that don't exist
|
row(Table, R),
|
||||||
row(Table, Row),
|
does_match(Cond, Table, R),
|
||||||
has_value(ColumnValue, Row),
|
selector(R,Cols, Selector, [],[],ColumnNames,ColumnValue),
|
||||||
Projection = ColumnNames/ColumnValue.
|
Projection = ColumnNames/ColumnValue.
|
||||||
|
|
||||||
%base case
|
%base case
|
||||||
selec_help(_, [], ColumnNames, ColumnValue, ColumnNames, ColumnValue).
|
selector(_,_,[],ColumnNames, ColumnValue, ColumnNames, ColumnValue):-!.
|
||||||
/**
|
/**
|
||||||
* Table: name of the table the selection is done one
|
* Row: A row from the table
|
||||||
|
* Cols: name of the column of the table
|
||||||
* [Selector|Selectors] name(s) of the column(s) for selection
|
* [Selector|Selectors] name(s) of the column(s) for selection
|
||||||
* AccCols: acumulator list for the column(s) name(s)
|
* AccCols: acumulator list for the column(s) name(s)
|
||||||
* 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
|
||||||
*/
|
*/
|
||||||
selec_help(Table, [Selector|Selectors], AccCols, AccVals, ColumnNames, ColumnValue) :-
|
selector(Row, Cols, [+Selector|Selectors], AccCols, AccVals, ColumnNames, ColumnValue):-
|
||||||
%check if table exist
|
(member(Selector, Cols) ->
|
||||||
(tabl(Table, Cols) ->
|
|
||||||
%check if column exist
|
|
||||||
member(Selector, Cols) ->
|
|
||||||
%find the value corresponding to the column in the row
|
%find the value corresponding to the column in the row
|
||||||
nth0(Index, Cols, Selector),
|
nth0(Index, Cols, Selector),
|
||||||
row(Table, Row),
|
|
||||||
nth0(Index, Row, Value),
|
nth0(Index, Row, Value),
|
||||||
%go to next column tho evaluate
|
%go to next column tho evaluate
|
||||||
selec_help(Table, Selectors, [Selector|AccCols], [Value|AccVals], ColumnNames, ColumnValue)
|
selector(Row, Cols, Selectors, [Selector|AccCols], [Value|AccVals], ColumnNames, ColumnValue)
|
||||||
;
|
;
|
||||||
string_concat(Selector, " isn't a column of table ", X),
|
string_concat(Selector, " isn't a column of table ", X),
|
||||||
string_concat(X, Table, Error),
|
string_concat(X, Table, Error),
|
||||||
throw(Error)
|
throw(Error)
|
||||||
;
|
|
||||||
string_concat(Table, " doesn't exist.", Error), throw(Error)
|
|
||||||
).
|
).
|
||||||
/*
|
|
||||||
Check if a list has all the values in [Value|Rest]
|
|
||||||
*/
|
|
||||||
has_value([], _) :- !.
|
|
||||||
has_value([Value|Rest], List) :-
|
|
||||||
memberchk(Value, List),
|
|
||||||
has_value(Rest, List).
|
|
||||||
|
|
||||||
evaluate_condition_string(ConditionString) :-
|
|
||||||
atom_codes(AtomCondition, ConditionString), % Convert string to atom
|
|
||||||
read_term_from_codes(AtomCondition, Term, []), % Parse the term
|
|
||||||
call(Term). % Evaluate the parsed term
|
|
||||||
|
|
||||||
selec_column(Table, Column, Proj) :-
|
|
||||||
tabl(Table, Cols),
|
|
||||||
row(Table, Row),
|
|
||||||
nth0(I, Cols, Column),
|
|
||||||
nth0(I, Row, Value),
|
|
||||||
Proj = Value.
|
|
||||||
|
|
||||||
apply_cond(Table, Cond, Proj) :-
|
|
||||||
tabl(Table, Cols),
|
|
||||||
row(Table, Row),
|
|
||||||
nth0(I, Cols, Column),
|
|
||||||
nth0(I, Row, Value),
|
|
||||||
split_string(Cond, " ", "", SubStrings),
|
|
||||||
Decomp = SubStrings,
|
|
||||||
Decomp = [Column, Op, Value],
|
|
||||||
check_condition(Column, Op, Value),
|
|
||||||
Proj = Row.
|
|
||||||
|
|
||||||
check_condition(Left, =, Right) :-
|
|
||||||
Left =:= Right.
|
|
||||||
check_condition(Left, <, Right) :-
|
|
||||||
Left < Right.
|
|
||||||
check_condition(Left, >, Right) :-
|
|
||||||
Left > Right.
|
|
||||||
check_condition(Left, <=, Right) :-
|
|
||||||
Left =< Right.
|
|
||||||
check_condition(Left, >=, Right) :-
|
|
||||||
Left >= Right.
|
|
||||||
check_condition(Left, \=, Right) :-
|
|
||||||
Left =\= Right.
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
:- dynamic selec/3.
|
:- dynamic selec/3.
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Reference in New Issue