diff --git a/solution.pl b/solution.pl index d91821c..8c2342f 100644 --- a/solution.pl +++ b/solution.pl @@ -316,10 +316,125 @@ selec/4 predicate. For “INSERT”, nothing needs to be printed by query/1 or in case of success, and if query/2 is used the Result parameter can be ignored. */ -/* query(Query, Result). */ +query(Query, Result):- + parse_query(Query, Fun, Args), + apply(Fun, Args), + last(Args, Result). + +/** + * Parse SELECT * FROM WHERE +*/ +parse_query(Query, selec, [AtomTable, *, AtomsConds, R]):- + split_string(Query, " ", ",;+", SplitQuery), + append(["SELECT"], L1, SplitQuery), + append(["*"], ["FROM"|L2], L1), + append([Table], ["WHERE"|Conds],L2), + atom_string(AtomTable, Table), + maplist(parse_cond, AtomsConds, Conds),!. +/** + * Parse SELECT FROM
WHERE +*/ +parse_query(Query, selec, [AtomTable, AtomsSelectors, AtomsConds, R]):- + split_string(Query, " ", ",;+", SplitQuery), + append(["SELECT"], L1, SplitQuery), + append(Selectors, ["FROM"|L2], L1), + append([Table], ["WHERE"|Conds],L2), + atom_string(AtomTable, Table), + maplist(atom_string, AtomsSelectors1, Selectors), + maplist(add_plus, AtomsSelectors, AtomsSelectors1), + maplist(parse_cond, AtomsConds, Conds),!. + +/** + * Parse SELECT * FROM
+*/ +parse_query(Query, selec, [AtomTable, *, R]):- + split_string(Query, " ", ",;+", SplitQuery), + append(["SELECT"], L1, SplitQuery), + append(["*"], ["FROM",Table], L1), + atom_string(AtomTable, Table), + maplist(parse_cond, AtomsConds, Conds),!. + +/** + * Parse SELECT FROM
+*/ +parse_query(Query, selec, [AtomTable, AtomsSelectors, R]):- + split_string(Query, " ", ",;+", SplitQuery), + append(["SELECT"], L1, SplitQuery), + append(Selectors, ["FROM",Table], L1), + atom_string(AtomTable, Table), + maplist(atom_string, AtomsSelectors1, Selectors), + maplist(add_plus, AtomsSelectors, AtomsSelectors1). + +parse_query(Query, insert, [AtomTable, Values]):- + split_string(Query, " ", '",();', SplitQuery), + append(["INSERT", "INTO"], L1, SplitQuery), + L1 = [Table | L2], + append(_, ["VALUES"|Values], L2), + atom_string(AtomTable, Table). + +add_plus(+A, A). + +% parse >= conditon +parse_cond(+AtomLeft>=AtomRight, Cond):- + string_chars(Cond, Code), + append(LeftMemb, [>,=|RightMemb], Code), + string_chars(Left, LeftMemb), + string_chars(Right1, RightMemb), + split_string(Right1,"",'"',[Right]), + atom_string(AtomLeft, Left), + is_number(AtomRight, Right),!. + +% parse <= conditon +parse_cond(+AtomLeft<=AtomRight, Cond):- + string_chars(Cond, Code), + append(LeftMemb, [<,=|RightMemb], Code), + string_chars(Left, LeftMemb), + string_chars(Right1, RightMemb), + split_string(Right1,"",'"',[Right]), + atom_string(AtomLeft, Left), + is_number(AtomRight, Right),!. + +% parse < conditon +parse_cond(+AtomLeft conditon +parse_cond(+AtomLeft>AtomRight, Cond):- + string_chars(Cond, Code), + append(LeftMemb, [>|RightMemb], Code), + string_chars(Left, LeftMemb), + string_chars(Right1, RightMemb), + split_string(Right1,"",'"',[Right]), + atom_string(AtomLeft, Left), + is_number(AtomRight, Right),!. + +% parse = conditon +parse_cond( +AtomLeft=AtomRight, Cond):- + string_chars(Cond, Code), + append(LeftMemb, [=|RightMemb], Code), + string_chars(Left, LeftMemb), + string_chars(Right1, RightMemb), + split_string(Right1,"",'"',[Right]), + atom_string(AtomLeft, Left), + is_number(AtomRight, Right),!. + +is_number(R, N):- + atom_number(N, R); R = N. + :- dynamic query/1. /* cf. query/2 */ -/* query(Query). */ + +query(Query) :- + query(Query, R), + writeln(R), + fail. +query(_). \ No newline at end of file