query implem
This commit is contained in:
parent
84a3075bc8
commit
7498c9214c
119
solution.pl
119
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 <table> WHERE <conds>
|
||||
*/
|
||||
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 <cols> FROM <table> WHERE <conds>
|
||||
*/
|
||||
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 <table>
|
||||
*/
|
||||
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 <cols> FROM <table>
|
||||
*/
|
||||
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<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=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(_).
|
Loading…
Reference in New Issue