MYDDAS: added new predicates to the MYDDAS interface. db_call_procedure/3 and db_sql_mode/2. Added also a sql_query_optmizer for translate/3
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1623 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
parent
7abbf8a1d8
commit
fd79ec1b9d
@ -20,7 +20,7 @@
|
||||
db_open/4,
|
||||
db_close/1,
|
||||
db_close/0,
|
||||
|
||||
|
||||
db_verbose/1,
|
||||
db_module/1,
|
||||
db_is_database_predicate/3,
|
||||
@ -29,6 +29,7 @@
|
||||
db_sql/2,
|
||||
db_sql/3,
|
||||
db_sql_select/3,
|
||||
db_prolog_select/2,
|
||||
db_prolog_select/3,
|
||||
db_prolog_select_multi/3,
|
||||
db_command/2,
|
||||
@ -45,12 +46,12 @@
|
||||
|
||||
db_multi_queries_number/2,
|
||||
|
||||
% myddas_top_level.yap
|
||||
% myddas_top_level.ypp
|
||||
#ifdef MYDDAS_TOP_LEVEL
|
||||
db_top_level/5,
|
||||
db_datalog_select/3,
|
||||
#endif
|
||||
% myddas_assert_predicates.yap
|
||||
% myddas_assert_predicates.ypp
|
||||
db_import/2,
|
||||
db_import/3,
|
||||
db_view/2,
|
||||
@ -59,15 +60,10 @@
|
||||
db_insert/3,
|
||||
db_abolish/2,
|
||||
db_listing/0,
|
||||
db_listing/1,
|
||||
|
||||
% myddas_mysql.yap
|
||||
db_my_open/5,
|
||||
db_my_close/1,
|
||||
db_my_import/3,
|
||||
db_my_view/3,
|
||||
db_my_insert/2,
|
||||
db_my_insert/3,
|
||||
db_listing/1
|
||||
#ifdef MYDDAS_MYSQL
|
||||
% myddas_mysql.ypp
|
||||
,
|
||||
db_my_result_set/1,
|
||||
db_datalog_describe/1,
|
||||
db_datalog_describe/2,
|
||||
@ -81,15 +77,19 @@
|
||||
db_show_databases/2,
|
||||
db_show_databases/1,
|
||||
db_change_database/2,
|
||||
db_my_sql_select/3,
|
||||
db_my_number_of_fields/3,
|
||||
db_my_get_attributes_types/3
|
||||
db_call_procedure/4,
|
||||
db_call_procedure/3,
|
||||
db_my_sql_mode/1,
|
||||
db_my_sql_mode/2
|
||||
#endif
|
||||
]).
|
||||
|
||||
#ifdef MYDDAS_MYSQL
|
||||
:- use_module(myddas_top_level,[
|
||||
db_top_level/5,
|
||||
db_datalog_select/3
|
||||
]).
|
||||
#endif
|
||||
|
||||
:- use_module(myddas_assert_predicates,[
|
||||
db_import/2,
|
||||
@ -103,13 +103,8 @@
|
||||
db_listing/1
|
||||
]).
|
||||
|
||||
#ifdef MYDDAS_MYSQL
|
||||
:- use_module(myddas_mysql,[
|
||||
db_my_open/5,
|
||||
db_my_close/1,
|
||||
db_my_import/3,
|
||||
db_my_view/3,
|
||||
db_my_insert/2,
|
||||
db_my_insert/3,
|
||||
db_my_result_set/1,
|
||||
db_datalog_describe/1,
|
||||
db_datalog_describe/2,
|
||||
@ -123,10 +118,12 @@
|
||||
db_show_databases/2,
|
||||
db_show_databases/1,
|
||||
db_change_database/2,
|
||||
db_my_sql_select/3,
|
||||
db_my_number_of_fields/3,
|
||||
db_my_get_attributes_types/3
|
||||
db_call_procedure/4,
|
||||
db_call_procedure/3,
|
||||
db_my_sql_mode/1,
|
||||
db_my_sql_mode/2
|
||||
]).
|
||||
#endif /* MYDDAS_MYSQL */
|
||||
|
||||
:- use_module(myddas_util_predicates,[
|
||||
'$prolog2sql'/3,
|
||||
@ -139,7 +136,6 @@
|
||||
'$write_or_not'/1,
|
||||
'$abolish_all'/1,
|
||||
'$make_a_list'/2,
|
||||
'$make_list_of_args'/4,
|
||||
'$get_table_name'/2,
|
||||
'$get_values_for_update'/4,
|
||||
'$extract_args'/4,
|
||||
@ -167,6 +163,7 @@
|
||||
db_open(Interface,HostDb,User,Password):-
|
||||
db_open(Interface,myddas,HostDb,User,Password).
|
||||
|
||||
#ifdef MYDDAS_MYSQL
|
||||
db_open(mysql,Connection,Host/Db/Port/Socket,User,Password) :- !,
|
||||
'$error_checks'(db_open(mysql,Connection,Host/Db/Port/Socket,User,Password)),
|
||||
c_db_my_connect(Host,User,Password,Db,Port,Socket,Con),
|
||||
@ -178,11 +175,14 @@ db_open(mysql,Connection,Host/Db/Socket,User,Password) :- !,
|
||||
db_open(mysql,Connection,Host/Db/0/Socket,User,Password). % 0 is default port
|
||||
db_open(mysql,Connection,Host/Db,User,Password) :-
|
||||
db_open(mysql,Connection,Host/Db/0/_,User,Password). % 0 is default port and Var to be NULL, the default socket
|
||||
#endif
|
||||
|
||||
#ifdef MYDDAS_ODBC
|
||||
db_open(odbc,Connection,ODBCEntry,User,Password) :-
|
||||
'$error_checks'(db_open(odbc,Connection,ODBCEntry,User,Password)),
|
||||
c_db_odbc_connect(ODBCEntry,User,Password,Con),
|
||||
set_value(Connection,Con).
|
||||
#endif
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
@ -210,6 +210,7 @@ db_close(Connection) :-
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_verbose/1
|
||||
%
|
||||
@ -271,9 +272,6 @@ db_stats(Connection,List):-
|
||||
|
||||
|
||||
|
||||
%db_sql_select(_,_,_):-
|
||||
%nl,write('Know use db_sql/3 insted of db_sql_select/3'),nl,
|
||||
%halt.
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_sql(+,+,-)
|
||||
%
|
||||
@ -289,17 +287,16 @@ db_sql(Connection,SQL,LA):-
|
||||
'$error_checks'(db_sql(Connection,SQL,LA)),
|
||||
get_value(Connection,Con),
|
||||
c_db_connection_type(Con,ConType),
|
||||
'$write_or_not'(SQL),
|
||||
( ConType == mysql ->
|
||||
c_db_my_number_of_fields_in_query(SQL,Con,Arity)
|
||||
db_my_result_set(Mode),
|
||||
c_db_my_query(SQL,ResultSet,Con,Mode,Arity)
|
||||
;
|
||||
c_db_odbc_number_of_fields(SQL,Con,Arity)
|
||||
),
|
||||
'$make_a_list'(Arity,LA),
|
||||
'$write_or_not'(SQL),
|
||||
( ConType == mysql ->
|
||||
db_my_result_set(Mode),
|
||||
c_db_my_query(SQL,ResultSet,Con,Mode),!,
|
||||
c_db_my_row(ResultSet,Arity,LA)
|
||||
!,c_db_my_row(ResultSet,Arity,LA)
|
||||
;
|
||||
'$make_a_list'(Arity,BindList),
|
||||
c_db_odbc_query(SQL,ResultSet,Arity,BindList,Con),!,
|
||||
@ -312,13 +309,16 @@ db_sql(Connection,SQL,LA):-
|
||||
% db_prolog_select(+,+,+)
|
||||
%
|
||||
%
|
||||
db_prolog_select(LA,DbGoal):-
|
||||
db_prolog_select(myddas,LA,DbGoal).
|
||||
db_prolog_select(Connection,LA,DbGoal):-
|
||||
|
||||
'$lenght'(LA,Arity),
|
||||
functor(ViewName,viewname,Arity),
|
||||
Name=viewname,
|
||||
functor(ViewName,Name,Arity),
|
||||
% build arg list for viewname/Arity
|
||||
'$make_list_of_args'(1,Arity,ViewName,LA),
|
||||
|
||||
ViewName=..[Name|LA],
|
||||
|
||||
'$prolog2sql'(ViewName,DbGoal,SQL),
|
||||
|
||||
get_value(Connection,Con),
|
||||
@ -326,7 +326,7 @@ db_prolog_select(Connection,LA,DbGoal):-
|
||||
'$write_or_not'(SQL),
|
||||
( ConType == mysql ->
|
||||
db_my_result_set(Mode),
|
||||
c_db_my_query(SQL,ResultSet,Con,Mode),
|
||||
c_db_my_query(SQL,ResultSet,Con,Mode,_),
|
||||
!,c_db_my_row(ResultSet,Arity,LA)
|
||||
;
|
||||
true
|
||||
@ -348,7 +348,7 @@ db_prolog_select_multi(Connection,DbGoalsList,ListOfResults) :-
|
||||
'$write_or_not'(SQL),
|
||||
( ConType == mysql ->
|
||||
db_my_result_set(Mode),
|
||||
c_db_my_query(SQL,ResultSet,Con,Mode)
|
||||
c_db_my_query(SQL,ResultSet,Con,Mode,_)
|
||||
;
|
||||
true
|
||||
),
|
||||
@ -368,7 +368,7 @@ db_command(Connection,SQL):-
|
||||
c_db_connection_type(Con,ConType),
|
||||
( ConType == mysql ->
|
||||
db_my_result_set(Mode),
|
||||
c_db_my_query(SQL,_,Con,Mode)
|
||||
c_db_my_query(SQL,_,Con,Mode,_)
|
||||
;
|
||||
true
|
||||
).
|
||||
@ -386,14 +386,14 @@ db_assert(Connection,PredName):-
|
||||
translate(PredName,PredName,Code),
|
||||
'$error_checks'(db_insert2(Connection,PredName,Code)),
|
||||
'$get_values_for_insert'(Code,ValuesList,RelName),
|
||||
'$make_atom'(['INSERT INTO `',RelName,'` VALUES'|ValuesList],SQL),
|
||||
'$make_atom'(['INSERT INTO `',RelName,'` VALUES '|ValuesList],SQL),
|
||||
|
||||
get_value(Connection,Con),
|
||||
c_db_connection_type(Con,ConType),
|
||||
'$write_or_not'(SQL),
|
||||
( ConType == mysql ->
|
||||
db_my_result_set(Mode),
|
||||
c_db_my_query(SQL,_,Con,Mode)
|
||||
c_db_my_query(SQL,_,Con,Mode,_)
|
||||
;
|
||||
c_db_odbc_query(SQL,_,_,_,Con)
|
||||
).
|
||||
@ -416,7 +416,7 @@ db_create_table(Connection,TableName,FieldsInf):-
|
||||
'$write_or_not'(FinalSQL),
|
||||
( ConType == mysql ->
|
||||
db_my_result_set(Mode),
|
||||
c_db_my_query(FinalSQL,_,Con,Mode)
|
||||
c_db_my_query(FinalSQL,_,Con,Mode,_)
|
||||
;
|
||||
c_db_odbc_query(FinalSQL,_,_,_,Con)
|
||||
).
|
||||
@ -446,7 +446,7 @@ db_export_view(Connection,TableViewName,SQLorDbGoal,FieldsInf):-
|
||||
'$write_or_not'(FinalSQL),
|
||||
( ConType == mysql ->
|
||||
db_my_result_set(Mode),
|
||||
c_db_my_query(FinalSQL,_,Con,Mode)
|
||||
c_db_my_query(FinalSQL,_,Con,Mode,_)
|
||||
;
|
||||
c_db_odbc_query(FinalSQL,_,_,_,Con)
|
||||
).
|
||||
@ -455,8 +455,9 @@ db_export_view(Connection,TableViewName,SQLorDbGoal,FieldsInf):-
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_update/2
|
||||
%
|
||||
%
|
||||
% BUB: db_update dosen't work for this case, just an example
|
||||
% db_update(my1,edge(1,3)-edge(99,99)).
|
||||
% The case where the set condition is "set" to change all the fields
|
||||
db_update(Connection,WherePred-SetPred):-
|
||||
%TODO: error_checks
|
||||
get_value(Connection,Conn),
|
||||
@ -471,16 +472,17 @@ db_update(Connection,WherePred-SetPred):-
|
||||
'$extract_args'(SetPred,1,Arity,SetArgs),
|
||||
|
||||
copy_term(WhereArgs,WhereArgsTemp),
|
||||
'$make_list_of_args'(1,Arity,NewRelation,WhereArgsTemp),
|
||||
NewRelation=..[PredName|WhereArgsTemp],
|
||||
translate(NewRelation,NewRelation,Code),
|
||||
|
||||
'$get_values_for_update'(Code,SetArgs,SetCondition,WhereCondition),
|
||||
|
||||
'$get_table_name'(Code,TableName),
|
||||
append(SetCondition,WhereCondition,Conditions),
|
||||
'$make_atom'(['UPDATE ',TableName,' '|Conditions],SQL),
|
||||
'$make_atom'(['UPDATE `',TableName,'` '|Conditions],SQL),
|
||||
'$write_or_not'(SQL),
|
||||
c_db_my_query_no_result(SQL,Conn).
|
||||
db_my_result_set(Mode),
|
||||
c_db_my_query(SQL,_,Conn,Mode,_).
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
@ -43,7 +43,6 @@
|
||||
'$copy_term_nv'/4,
|
||||
'$assert_attribute_information'/4,
|
||||
'$make_a_list'/2,
|
||||
'$make_list_of_args'/4,
|
||||
'$where_exists'/2,
|
||||
'$build_query'/5
|
||||
]).
|
||||
@ -95,7 +94,7 @@ db_import(Connection,RelationName,PredName) :-
|
||||
|
||||
% build PredName functor
|
||||
functor(P,PredName,Arity),
|
||||
'$make_list_of_args'(1,Arity,P,LA),
|
||||
P=..[PredName|LA],
|
||||
|
||||
M=myddas_assert_predicates,
|
||||
%build PredName clause
|
||||
@ -106,7 +105,7 @@ db_import(Connection,RelationName,PredName) :-
|
||||
','(M:queries_atom(Code,FinalSQL),
|
||||
','(M:db_my_result_set(Mode),
|
||||
','(M:'$write_or_not'(FinalSQL),
|
||||
','(M:c_db_my_query(FinalSQL,ResultSet,Con,Mode),
|
||||
','(M:c_db_my_query(FinalSQL,ResultSet,Con,Mode,_),
|
||||
','(!,M:c_db_my_row(ResultSet,Arity,LA))))))))]
|
||||
|
||||
;
|
||||
@ -144,7 +143,7 @@ db_view(Connection,PredName,DbGoal) :-
|
||||
% with top-level variables A='var('A')' error
|
||||
copy_term((PredName,DbGoal),(CopyView,CopyGoal)),
|
||||
|
||||
'$make_list_of_args'(1,Arity,CopyView,LA),
|
||||
CopyView=..[ViewName|LA],
|
||||
|
||||
M=myddas_assert_predicates,
|
||||
c_db_connection_type(Con,ConType),
|
||||
@ -158,7 +157,7 @@ db_view(Connection,PredName,DbGoal) :-
|
||||
','(M:queries_atom(Code,FinalSQL),
|
||||
','(M:db_my_result_set(Mode),
|
||||
','(M:'$write_or_not'(FinalSQL),
|
||||
','(M:c_db_my_query(FinalSQL,ResultSet,Con,Mode),
|
||||
','(M:c_db_my_query(FinalSQL,ResultSet,Con,Mode,_),
|
||||
','(!,M:c_db_my_row(ResultSet,Arity,LA)))))))))]
|
||||
;
|
||||
% Assert =..[':-',NewName,
|
||||
@ -203,8 +202,8 @@ db_insert(Connection,RelationName,PredName) :-
|
||||
|
||||
% build PredName functor
|
||||
functor(Predicate,PredName,Arity),
|
||||
'$make_list_of_args'(1,Arity,Predicate,LA),
|
||||
|
||||
Predicate=..[PredName|LA],
|
||||
|
||||
Size is 2*Arity,
|
||||
'$make_a_list'(Size,TypesList),
|
||||
|
||||
@ -216,7 +215,7 @@ db_insert(Connection,RelationName,PredName) :-
|
||||
','(myddas_assert_predicates:'$make_atom'(['INSERT INTO `',RelationName,'` VALUES ('|ValuesList],SQL),
|
||||
','(myddas_assert_predicates:db_my_result_set(Mode),
|
||||
','(myddas_assert_predicates:'$write_or_not'(SQL),
|
||||
myddas_assert_predicates:c_db_my_query(SQL,_,Con,Mode)))))]
|
||||
myddas_assert_predicates:c_db_my_query(SQL,_,Con,Mode,_)))))]
|
||||
;
|
||||
c_db_odbc_get_attributes_types(RelationName,Con,TypesList),
|
||||
Assert =..[':-',Predicate,','(myddas_assert_predicates:'$get_values_for_insert'(TypesList,LA,ValuesList),
|
||||
|
@ -38,8 +38,12 @@
|
||||
atom(ModulePredName)
|
||||
),
|
||||
number(Arity).
|
||||
'$error_checks'(db_show_databases(Connection)):- !,
|
||||
atom(Connection).
|
||||
'$error_checks'(db_show_database(Connection,_)):- !,
|
||||
atom(Connection).
|
||||
'$error_checks'(db_my_sql_mode(Connection,_)):- !,
|
||||
atom(Connection).
|
||||
'$error_checks'(db_change_database(Connection,Database)):- !,
|
||||
atom(Connection),
|
||||
atom(Database).
|
||||
@ -112,8 +116,8 @@
|
||||
nonvar(Pred),
|
||||
nonvar(DbGoal),
|
||||
Pred =.. [_|PredArgs],
|
||||
DbGoal =.. [_|DbGoalArgs],
|
||||
'$check_list_on_list'(PredArgs,DbGoalArgs).
|
||||
DbGoal =.. [_|DbGoalArgs].
|
||||
%'$check_list_on_list'(PredArgs,DbGoalArgs).
|
||||
'$error_checks'(db_import(Connection,RelationName,PredName)) :- !,
|
||||
atom(Connection),
|
||||
atom(RelationName),
|
||||
@ -121,6 +125,10 @@
|
||||
'$error_checks'(db_get_attributes_types(Connection,RelationName,_)) :- !,
|
||||
atom(Connection),
|
||||
nonvar(RelationName).
|
||||
'$error_checks'(db_call_procedure(_,Procedure,Args,LA)) :- !,
|
||||
nonvar(Procedure),
|
||||
ground(Args),
|
||||
not ground(LA).
|
||||
'$error_checks'(db_sql(Connection,SQL,LA)):- !,
|
||||
atom(Connection),
|
||||
nonvar(SQL),
|
||||
@ -131,7 +139,7 @@
|
||||
'$error_checks'(db_close(Connection)) :- !,
|
||||
atom(Connection).
|
||||
% must have only one relation
|
||||
'$error_checks'(db_my_describe(Relation,_)) :- !,
|
||||
'$error_checks'(db_datalog_describe(Relation,_)) :- !,
|
||||
nonvar(Relation).
|
||||
'$error_checks'(db_my_show_tables(_)):- !.
|
||||
'$error_checks'(db_is_database_predicate(PredName,Arity,Module)):-!,
|
||||
|
@ -14,14 +14,9 @@
|
||||
* comments: MySQL Predicates *
|
||||
* *
|
||||
*************************************************************************/
|
||||
#ifdef MYDDAS_MYSQL
|
||||
|
||||
:- module(myddas_mysql,[
|
||||
db_my_open/5,
|
||||
db_my_close/1,
|
||||
db_my_import/3,
|
||||
db_my_view/3,
|
||||
db_my_insert/2,
|
||||
db_my_insert/3,
|
||||
db_my_result_set/1,
|
||||
db_datalog_describe/1,
|
||||
db_datalog_describe/2,
|
||||
@ -35,11 +30,15 @@
|
||||
db_show_databases/2,
|
||||
db_show_databases/1,
|
||||
db_change_database/2,
|
||||
db_my_sql_select/3,
|
||||
db_my_number_of_fields/3,
|
||||
db_my_get_attributes_types/3
|
||||
db_call_procedure/4,
|
||||
db_call_procedure/3,
|
||||
db_my_sql_mode/1,
|
||||
db_my_sql_mode/2
|
||||
]).
|
||||
|
||||
:- use_module(myddas,[
|
||||
db_sql/3
|
||||
]).
|
||||
|
||||
:- use_module(myddas_errors,[
|
||||
'$error_checks'/1
|
||||
@ -48,6 +47,8 @@
|
||||
:- use_module(myddas_util_predicates,[
|
||||
'$get_value'/2,
|
||||
'$make_atom'/2,
|
||||
'$make_atom_args'/2,
|
||||
'$make_a_list'/2,
|
||||
'$write_or_not'/1
|
||||
]).
|
||||
|
||||
@ -56,78 +57,6 @@
|
||||
% Public Predicates
|
||||
%--------------------------------------------------------
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_my_open/5
|
||||
%
|
||||
%
|
||||
db_my_open(_,_,_,_,_) :-
|
||||
write('WARNING!! Now we use db_open/5'),nl,
|
||||
write('USAGE: db_open(ConType,Connection,Host/Db,User,Password).'),nl,
|
||||
halt.
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_my_close/1
|
||||
%
|
||||
%
|
||||
db_my_close(_):-
|
||||
write('WARNING!! Now we use db_close/1'),nl,
|
||||
write('USAGE: db_close(Connection).'),nl,
|
||||
halt.
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_my_import/3
|
||||
%
|
||||
%
|
||||
db_my_import(_,_,_) :-
|
||||
write('WARNING!! Now we use db_import/3'),nl,
|
||||
write('USAGE: db_import(Connection,RelationName,PredName).'),nl,
|
||||
halt.
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_my_view/3
|
||||
%
|
||||
%
|
||||
db_my_view(_,_,_) :-
|
||||
write('WARNING!! Now we use db_view/3'),nl,
|
||||
write('USAGE: db_import(Connection,RelationName,DBGoal).'),nl,
|
||||
halt.
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_my_insert/2
|
||||
%
|
||||
%
|
||||
db_my_insert(_,_):-
|
||||
write('WARNING!! Now we use db_insert/2'),nl,
|
||||
write('USAGE: db_insert(Connection,PredName).'),nl,
|
||||
halt.
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_my_insert/3
|
||||
%
|
||||
%
|
||||
db_my_insert(_,_,_) :-
|
||||
write('WARNING!! Now we use db_insert/3'),nl,
|
||||
write('USAGE: db_insert(Connection,RelationName,PredName).'),nl,
|
||||
halt.
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_my_result_set/1
|
||||
@ -153,11 +82,11 @@ db_my_result_set(store_result):-
|
||||
db_datalog_describe(Relation):-
|
||||
db_datalog_describe(myddas,Relation).
|
||||
db_datalog_describe(Connection,Relation) :-
|
||||
'$error_checks'(db_my_describe(Relation,Connection)),
|
||||
'$error_checks'(db_datalog_describe(Relation,Connection)),
|
||||
'$get_value'(Connection,Conn),
|
||||
'$make_atom'(['DESCRIBE ',Relation],SQL),
|
||||
db_my_result_set(Mode),
|
||||
c_db_my_query(SQL,ResultSet,Conn,Mode),
|
||||
c_db_my_query(SQL,ResultSet,Conn,Mode,_),
|
||||
c_db_my_table_write(ResultSet).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
@ -176,7 +105,7 @@ db_describe(Connection,Relation,tableinfo(A1,A2,A3,A4,A5,A6)) :-
|
||||
'$make_atom'(['DESCRIBE ',Relation],SQL),
|
||||
db_my_result_set(Mode),
|
||||
'$write_or_not'(SQL),
|
||||
c_db_my_query(SQL,ResultSet,Conn,Mode),
|
||||
c_db_my_query(SQL,ResultSet,Conn,Mode,_),
|
||||
!,c_db_my_row(ResultSet,6,[A1,A2,A3,A4,A5,A6]).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
@ -193,7 +122,7 @@ db_datalog_show_tables(Connection) :-
|
||||
'$get_value'(Connection,Conn),
|
||||
db_my_result_set(Mode),
|
||||
'$write_or_not'('SHOW TABLES'),
|
||||
c_db_my_query('SHOW TABLES',ResultSet,Conn,Mode),
|
||||
c_db_my_query('SHOW TABLES',ResultSet,Conn,Mode,_),
|
||||
c_db_my_table_write(ResultSet).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
@ -211,7 +140,7 @@ db_show_tables(Connection,table(Table)) :-
|
||||
'$get_value'(Connection,Conn),
|
||||
db_my_result_set(Mode),
|
||||
'$write_or_not'('SHOW TABLES'),
|
||||
c_db_my_query('SHOW TABLES',ResultSet,Conn,Mode),
|
||||
c_db_my_query('SHOW TABLES',ResultSet,Conn,Mode,_),
|
||||
!,c_db_my_row(ResultSet,1,[Table]).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
@ -236,7 +165,7 @@ db_show_databases(Connection,database(Databases)) :-
|
||||
'$get_value'(Connection,Conn),
|
||||
db_my_result_set(Mode),
|
||||
'$write_or_not'('SHOW DATABASES'),
|
||||
c_db_my_query('SHOW DATABASES',ResultSet,Conn,Mode),
|
||||
c_db_my_query('SHOW DATABASES',ResultSet,Conn,Mode,_),
|
||||
!,c_db_my_row(ResultSet,1,[Databases]).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
@ -247,11 +176,11 @@ db_show_databases(Connection,database(Databases)) :-
|
||||
% TODO Error Checks
|
||||
%
|
||||
db_show_databases(Connection) :-
|
||||
%'$error_checks'(db_my_show_databases(Connection)),
|
||||
'$error_checks'(db_my_show_databases(Connection)),
|
||||
'$get_value'(Connection,Conn),
|
||||
db_my_result_set(Mode),
|
||||
'$write_or_not'('SHOW DATABASES'),
|
||||
c_db_my_query('SHOW DATABASES',ResultSet,Conn,Mode),
|
||||
c_db_my_query('SHOW DATABASES',ResultSet,Conn,Mode,_),
|
||||
c_db_my_table_write(ResultSet).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
@ -269,37 +198,44 @@ db_change_database(Connection,Database) :-
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_call_procedure/4
|
||||
% db_call_procedure/3
|
||||
% db_call_procedure(+,+,+,?)
|
||||
% Only support in MySQL 5.0 an above
|
||||
% Called procedure must return results via MySQL result set
|
||||
db_call_procedure(Procedure,Args,Result) :-
|
||||
db_call_procedure(myddas,Procedure,Args,Result).
|
||||
db_call_procedure(Connection,Procedure,Args,LA) :-
|
||||
'$error_checks'(db_call_procedure(Connection,Procedure,Args,Result)),
|
||||
'$make_atom_args'(Args,ArgsSQL),
|
||||
'$make_atom'(['CALL ',Procedure,'(',ArgsSQL,')'],SQL),
|
||||
db_sql(Connection,SQL,LA).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_my_sql_select/3
|
||||
%
|
||||
%
|
||||
db_my_sql_select(_,_,_):-
|
||||
write('WARNING!! Now we use db_sql_select/3'),nl,
|
||||
write('USAGE: db_sql_select(Connection,SQL,ListaArgs).'),nl,
|
||||
halt.
|
||||
% db_my_sql_mode/1
|
||||
% db_my_sql_mode/2
|
||||
% Possible values : traditional,ansi,strict_trans_tables or '' (empty)
|
||||
db_my_sql_mode(SQLMode):-
|
||||
db_my_sql_mode(myddas,SQLMode).
|
||||
db_my_sql_mode(Connection,SQLMode):-
|
||||
var(SQLMode),!,
|
||||
'$error_checks'(db_my_sql_mode(Connection,SQLMode)),
|
||||
get_value(Connection,Con),
|
||||
c_db_connection_type(Con,mysql), %must be a mysql connection
|
||||
db_sql(Connection,'SELECT @@session.sql_mode',[SQLMode]).
|
||||
db_my_sql_mode(Connection,SQLMode):-
|
||||
'$error_checks'(db_my_sql_mode(Connection,SQLMode)),
|
||||
get_value(Connection,Con),
|
||||
c_db_connection_type(Con,mysql), %must be a mysql connection
|
||||
'$make_atom'(['SET SESSION sql_mode=`',SQLMode,'`'],FinalSQL),
|
||||
'$write_or_not'(FinalSQL),
|
||||
db_my_result_set(Mode),
|
||||
c_db_my_query(FinalSQL,_,Con,Mode,_).
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_my_number_of_fields/3
|
||||
%
|
||||
%
|
||||
db_my_number_of_fields(_,_,_) :-
|
||||
write('WARNING!! Now we use db_number_of_fields/3'),nl,
|
||||
write('USAGE: db_number_of_fields(Connection,RelationName,Arity).'),nl,
|
||||
halt.
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% db_my_get_attributes_types/3
|
||||
%
|
||||
%
|
||||
db_my_get_attributes_types(_,_,_) :-
|
||||
write('WARNING!! Now we use db_get_attributes_types/3'),nl,
|
||||
write('USAGE: db_get_attributes_types(Connection,RelationName,TypesList).'),nl,
|
||||
halt.
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
#endif /* MYDDAS_MYSQL*/
|
@ -42,10 +42,16 @@
|
||||
translate/3,
|
||||
queries_atom/2
|
||||
]).
|
||||
|
||||
|
||||
:- use_module(lists,[
|
||||
append/3,
|
||||
member/2
|
||||
]).
|
||||
|
||||
|
||||
|
||||
:- use_module(myddas_prolog2sql_optimizer,[
|
||||
optimize_sql/2
|
||||
]).
|
||||
|
||||
|
||||
% --------------------------------------------------------------------------------------
|
||||
@ -56,7 +62,7 @@
|
||||
% --------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
translate(ProjectionTerm,DatabaseGoal,SQLQueryTerm):-
|
||||
translate(ProjectionTerm,DatabaseGoal,SQLQueryTermOpt):-
|
||||
% --- initialize variable identifiers and range variables for relations -----
|
||||
init_gensym(var),
|
||||
init_gensym(rel),
|
||||
@ -69,8 +75,11 @@ translate(ProjectionTerm,DatabaseGoal,SQLQueryTerm):-
|
||||
disjunction(TokenDatabaseGoal,Disjunction),
|
||||
|
||||
% --- code generation ---------------------------------------------------------------
|
||||
query_generation(Disjunction,TokenProjectionTerm,SQLQueryTerm).
|
||||
query_generation(Disjunction,TokenProjectionTerm,SQLQueryTerm),
|
||||
|
||||
% --- optimize sql ------------------------------------------------------------------
|
||||
optimize_sql(SQLQueryTerm,SQLQueryTermOpt).
|
||||
|
||||
% --- disjunction(Goal,Disjunction) ----------------------------------------------------
|
||||
%
|
||||
% turns original goal into disjunctive normalized form by computing all conjunctions
|
||||
@ -225,18 +234,17 @@ tokenize_arguments([FirstArg|RestArgs],[TokFirstArg|TokRestArgs]):-
|
||||
query_generation([],_,[]).
|
||||
|
||||
query_generation([Conjunction|Conjunctions],ProjectionTerm,[Query|Queries]):-
|
||||
projection_term_variables(ProjectionTerm,InitDict),
|
||||
( Conjunction =.. [once|Arguments] ->
|
||||
[Args] = Arguments,
|
||||
translate_conjunction(Args,SQLFrom,SQLWhereTemp,InitDict,Dict),
|
||||
append(SQLWhereTemp,[once],SQLWhere)
|
||||
;
|
||||
translate_conjunction(Conjunction,SQLFrom,SQLWhere,InitDict,Dict)
|
||||
),
|
||||
translate_projection(ProjectionTerm,Dict,SQLSelect),
|
||||
Query = query(SQLSelect,SQLFrom,SQLWhere),
|
||||
query_generation(Conjunctions,ProjectionTerm,Queries).
|
||||
|
||||
projection_term_variables(ProjectionTerm,InitDict),
|
||||
( Conjunction =.. [once|Arguments] ->
|
||||
[Args] = Arguments,
|
||||
translate_conjunction(Args,SQLFrom,SQLWhereTemp,InitDict,Dict),
|
||||
append(SQLWhereTemp,[once],SQLWhere)
|
||||
;
|
||||
translate_conjunction(Conjunction,SQLFrom,SQLWhere,InitDict,Dict)
|
||||
),
|
||||
translate_projection(ProjectionTerm,Dict,SQLSelect),
|
||||
Query = query(SQLSelect,SQLFrom,SQLWhere),
|
||||
query_generation(Conjunctions,ProjectionTerm,Queries).
|
||||
|
||||
|
||||
% --- translate_goal(Goal,SQLFrom,SQLWhere,Dict,NewDict) -------------------------------
|
||||
@ -278,7 +286,16 @@ translate_goal(not ComparisonGoal,[],SQLCompOp,Dict,Dict):-
|
||||
negated_comparison(SQLOperator,SQLNegOperator),
|
||||
translate_comparison(LeftArg,RightArg,SQLNegOperator,Dict,SQLCompOp).
|
||||
|
||||
translate_goal(exists(ExistsGoals),[],SQLExistsSubquery,Dict,Dict):-
|
||||
%% EXAMPLE: db_prolog_select([Gene1],(once((eval(Gene1,_,_),exists(eval(Gene1),eval(Gene1,_,_)))))).
|
||||
translate_goal(exists(ProjectionTerm,ExistsGoals),SQLFrom,SQLExistsSubquery,Dict,Dict):-
|
||||
% --- exists goals do not bind variables - hence Dict is returned unchanged --------
|
||||
functor(ExistsGoals,Functor,_),
|
||||
not comparison(Functor,_),
|
||||
translate_projection(ProjectionTerm,Dict,SQLSelect),
|
||||
translate_conjunction(ExistsGoals,SQLFrom,SQLWhere,Dict,_),
|
||||
SQLExistsSubquery = [existential_subquery(SQLSelect,SQLFrom,SQLWhere)].
|
||||
|
||||
translate_goal(exists(ExistsGoals),SQLFrom,SQLExistsSubquery,Dict,Dict):-
|
||||
% --- exists goals do not bind variables - hence Dict is returned unchanged --------
|
||||
functor(ExistsGoals,Functor,_),
|
||||
not comparison(Functor,_),
|
||||
@ -312,19 +329,28 @@ add_distinct_statement(Dict,Dict):-
|
||||
% --------------------------------------------------------------------------------------
|
||||
|
||||
translate_conjunction('$var$'(VarId)^Goal,SQLFrom,SQLWhere,Dict,NewDict):-
|
||||
% --- add info on existentially quantified variables to dictionary here -------------
|
||||
#ifdef DEBUG_TRANSLATE
|
||||
write('translate_conjuntion clause 1'),nl,
|
||||
#endif
|
||||
% --- add info on existentially quantified variables to dictionary here -------------
|
||||
add_to_dictionary(VarId,_,_,_,existential,Dict,TmpDict),
|
||||
translate_conjunction(Goal,SQLFrom,SQLWhere,TmpDict,NewDict).
|
||||
|
||||
translate_conjunction(Goal,SQLFrom,SQLWhere,Dict,NewDict):-
|
||||
#ifdef DEBUG_TRANSLATE
|
||||
write('translate_conjuntion clause 2'),nl,
|
||||
#endif
|
||||
Goal \= (_,_),
|
||||
translate_goal(Goal,SQLFrom,SQLWhere,Dict,NewDict).
|
||||
|
||||
translate_conjunction((Goal,Conjunction),SQLFrom,SQLWhere,Dict,NewDict):-
|
||||
translate_goal(Goal,FromBegin,WhereBegin,Dict,TmpDict),
|
||||
translate_conjunction(Conjunction,FromRest,WhereRest,TmpDict,NewDict),
|
||||
append(FromBegin,FromRest,SQLFrom),
|
||||
append(WhereBegin,WhereRest,SQLWhere).
|
||||
#ifdef DEBUG_TRANSLATE
|
||||
write('translate_conjuntion clause 3'),nl,
|
||||
#endif
|
||||
translate_goal(Goal,FromBegin,WhereBegin,Dict,TmpDict),
|
||||
translate_conjunction(Conjunction,FromRest,WhereRest,TmpDict,NewDict),
|
||||
append(FromBegin,FromRest,SQLFrom),
|
||||
append(WhereBegin,WhereRest,SQLWhere).
|
||||
|
||||
|
||||
|
||||
@ -363,9 +389,9 @@ translate_arithmetic_function('$var$'(VarId),Expression,ArithComparison,Dict,Dic
|
||||
% test whether type of attribute is numeric - if not, there's no sense in
|
||||
% continuing the translation
|
||||
|
||||
type_compatible(PrevType,number),
|
||||
%type_compatible(PrevType,number),
|
||||
evaluable_expression(Expression,Dict,ArithExpression,ExprType),
|
||||
type_compatible(ExprType,number),
|
||||
%type_compatible(ExprType,number),
|
||||
ArithComparison = [comp(att(PrevRangeVar,PrevAtt),'=',ArithExpression)].
|
||||
|
||||
|
||||
@ -376,18 +402,18 @@ translate_arithmetic_function('$var$'(VarId),Expression,ArithComparison,Dict,Dic
|
||||
% of lookup returning is/2. Type information is implicit through the is/2 functor
|
||||
|
||||
lookup(VarId,Dict,is,LeftExpr,Type),
|
||||
type_compatible(Type,number),
|
||||
%type_compatible(Type,number),
|
||||
evaluable_expression(Expression,Dict,RightExpr,ExprType),
|
||||
type_compatible(ExprType,number),
|
||||
%type_compatible(ExprType,number),
|
||||
ArithComparison = [comp(LeftExpr,'=',RightExpr)].
|
||||
|
||||
|
||||
translate_arithmetic_function('$const$'(Constant),Expression,ArithComparison,Dict,Dict):-
|
||||
% --- is/2 used to test whether left side evaluates to right side -------------------
|
||||
get_type('$const$'(Constant),ConstantType),
|
||||
type_compatible(ConstantType,number),
|
||||
%type_compatible(ConstantType,number),
|
||||
evaluable_expression(Expression,Dict,ArithExpression,ExprType),
|
||||
type_compatible(ExprType,number),
|
||||
%type_compatible(ExprType,number),
|
||||
ArithComparison = [comp('$const$'(Constant),'=',ArithExpression)].
|
||||
|
||||
|
||||
@ -403,7 +429,7 @@ translate_arithmetic_function('$const$'(Constant),Expression,ArithComparison,Dic
|
||||
translate_comparison(LeftArg,RightArg,CompOp,Dict,Comparison):-
|
||||
evaluable_expression(LeftArg,Dict,LeftTerm,LeftArgType),
|
||||
evaluable_expression(RightArg,Dict,RightTerm,RightArgType),
|
||||
type_compatible(LeftArgType,RightArgType),
|
||||
%type_compatible(LeftArgType,RightArgType),
|
||||
Comparison = [comp(LeftTerm,CompOp,RightTerm)].
|
||||
|
||||
|
||||
@ -464,14 +490,14 @@ translate_argument('$var$'(VarId),rel(SQLTable,RangeVar),Position,AttComparison,
|
||||
% --- Variable occurred previously - retrieve first occurrence data from dictionary -
|
||||
lookup(VarId,Dict,PrevRangeVar,PrevAtt,PrevType),
|
||||
attribute(Position,SQLTable,Attribute,Type),
|
||||
type_compatible(PrevType,Type),
|
||||
% type_compatible(PrevType,Type),
|
||||
AttComparison = [comp(att(RangeVar,Attribute),=,att(PrevRangeVar,PrevAtt))].
|
||||
|
||||
translate_argument('$const$'(Constant),rel(SQLTable,RangeVar),Position,ConstComparison,Dict,Dict):-
|
||||
% --- Equality comparison of constant value and attribute in table ------------------
|
||||
attribute(Position,SQLTable,Attribute,Type),
|
||||
get_type('$const$'(Constant),ConstType),
|
||||
type_compatible(ConstType,Type),
|
||||
%get_type('$const$'(Constant),ConstType),
|
||||
%type_compatible(ConstType,Type),
|
||||
ConstComparison = [comp(att(RangeVar,Attribute),=,'$const$'(Constant))].
|
||||
|
||||
|
||||
@ -1202,18 +1228,6 @@ gensym(Atom,Var) :-
|
||||
|
||||
% --- auxiliary predicates (some of them may be built-in... --------------------
|
||||
|
||||
append([],L,L).
|
||||
append([H1|L1],L2,[H1|L3]):-
|
||||
append(L1,L2,L3).
|
||||
|
||||
|
||||
|
||||
member(X,[X|_]).
|
||||
member(X,[_|T]):-
|
||||
member(X,T).
|
||||
|
||||
|
||||
|
||||
repeat_n(N):-
|
||||
integer(N),
|
||||
N > 0,
|
||||
@ -1247,6 +1261,7 @@ set_difference([Element|RestSet],Set,RestDifference):-
|
||||
comparison(=,=).
|
||||
comparison(<,<).
|
||||
comparison(=<,'<=').
|
||||
comparison(>=,'>=').
|
||||
comparison(>,>).
|
||||
comparison(@<,<).
|
||||
comparison(@>,>).
|
||||
@ -1344,3 +1359,5 @@ get_type('$const$'(Constant),real):-
|
||||
|
||||
get_type('$const$'(Constant),string):-
|
||||
atom(Constant).
|
||||
|
||||
|
||||
|
110
library/MYDDAS/myddas_prolog2sql_optimizer.ypp
Normal file
110
library/MYDDAS/myddas_prolog2sql_optimizer.ypp
Normal file
@ -0,0 +1,110 @@
|
||||
:- module(myddas_prolog2sql_optimizer,[
|
||||
optimize_sql/2
|
||||
]).
|
||||
|
||||
:- use_module(lists,[
|
||||
append/3,
|
||||
member/2,
|
||||
delete/3,
|
||||
substitute/4
|
||||
]).
|
||||
|
||||
|
||||
|
||||
optimize_sql([],[]).
|
||||
|
||||
optimize_sql([query(Proj,From,Where)|Tail],[query(Proj1,From1,Where1)|SQLTerm]):-
|
||||
optimize_subquery(Proj,Proj1,From,From1,Where,Where1),
|
||||
optimize_sql(Tail,SQLTerm).
|
||||
|
||||
|
||||
|
||||
% --- optimize_subquery(SQLFrom,SQLWhere) -------------------------------
|
||||
%
|
||||
% If there is a subquery on the query,
|
||||
% uses the less number of table necessary in the
|
||||
% FROM token of the SQL Statement. see(EXPLAIN (SQL statemente with subquerie) )
|
||||
%
|
||||
% Optimization: * All variables on the projection term must be on the outer statement
|
||||
% * All variables on the outer query, must have their respective raltions on the outer query
|
||||
% * Try to maintain as more possibles relations on the inner statement
|
||||
% --------------------------------------------------------------------------------------
|
||||
|
||||
%TODO: BUG
|
||||
% BEWARE WHEN THE SUBQUERY HAS NO TABLES AT THE END OF THIS OPTIMIZATION
|
||||
|
||||
optimize_subquery(SQLSelect,OptSelectFinal,F,F,W,W):-
|
||||
once(member(agg_query(Agg,Select,From,Where,Extra),SQLSelect)),!,
|
||||
optimize_subquery(Select,OptSelect,From,OptFrom,Where,OptWhere),
|
||||
substitute(agg_query(Agg,Select,From,Where,Extra),SQLSelect,agg_query(Agg,OptSelect,OptFrom,OptWhere,Extra),OptSelectFinal).
|
||||
|
||||
optimize_subquery(SQLSelect,SQLSelect,SQLFrom,OptFrom,SQLWhere,OptWhere):-
|
||||
%SQLSelect = OptSelect, % In the future, if needed optimize projection term
|
||||
once(member(negated_existential_subquery(_,SubQueryRelations,_),SQLWhere))
|
||||
;
|
||||
once(member(existential_subquery(_,SubQueryRelations,_),SQLWhere))
|
||||
,!,
|
||||
add_relation(SubQueryRelations,[],RelTotalTemp),
|
||||
add_relation(SQLFrom,RelTotalTemp,RelTotal),
|
||||
projection_term_analysis(SQLSelect,From1,RelTotal,RelTotal1),
|
||||
comparasion_analysis(SQLWhere,From1,From2,RelTotal1,RelTotal2),
|
||||
delete_surplous_tables(SQLWhere,RelTotal2,OptWhere),
|
||||
OptFrom = From2.
|
||||
|
||||
optimize_subquery(ProjTerm,ProjTerm,From,From,Where,Where).
|
||||
|
||||
delete_surplous_tables([negated_existential_subquery(A,_,B)|T],Rel,[negated_existential_subquery(A,Rel,B)|T]):-!.
|
||||
delete_surplous_tables([existential_subquery(A,_,B)|T],Rel,[existential_subquery(A,Rel,B)|T]):-!.
|
||||
delete_surplous_tables([H|T],Rel,[H|Final]):-
|
||||
delete_surplous_tables(T,Rel,Final).
|
||||
|
||||
comparasion_analysis([],From,From,RelTotal,RelTotal).
|
||||
|
||||
comparasion_analysis([comp(att(Relation,_),_,att(Relation,_))|Tail],From1,[rel(Name,Relation)|FromFinal],RelTotal,RelTotalFinal):-
|
||||
member(rel(Name,Relation),RelTotal),!,
|
||||
delete(RelTotal,rel(Name,Relation),RelTotal1),
|
||||
comparasion_analysis(Tail,From1,FromFinal,RelTotal1,RelTotalFinal).
|
||||
|
||||
comparasion_analysis([comp(att(Relation1,_),_,att(Relation2,_))|Tail],From1,From4,RelTotal,RelTotal3):-
|
||||
comparasion_analysis(Tail,From1,From2,RelTotal,RelTotal1),
|
||||
!,
|
||||
(member(rel(Name,Relation1),RelTotal1) ->
|
||||
delete(RelTotal1,rel(Name,Relation1),RelTotal2),
|
||||
From3 = [rel(Name,Relation1)|From2]
|
||||
;
|
||||
RelTotal2 = RelTotal1,
|
||||
From3 = From2
|
||||
),
|
||||
(member(rel(Name,Relation2),RelTotal2) ->
|
||||
delete(RelTotal2,rel(Name,Relation1),RelTotal3),
|
||||
From4 = [rel(Name,Relation2)|From3]
|
||||
;
|
||||
RelTotal3 = RelTotal2,
|
||||
From4 = From3
|
||||
).
|
||||
|
||||
comparasion_analysis([_|Tail],From,FromFinal,RelTotal,RelTotalFinal):-
|
||||
comparasion_analysis(Tail,From,FromFinal,RelTotal,RelTotalFinal).
|
||||
|
||||
|
||||
projection_term_analysis([],[],Relation,Relation).
|
||||
|
||||
projection_term_analysis([att(Relation,_)|Tail],[rel(Name,Relation)|FromFinal],RelTotal,RelTotal1):-
|
||||
member(rel(Name,Relation),RelTotal),!,
|
||||
delete(RelTotal,rel(Name,Relation),Residue),
|
||||
projection_term_analysis(Tail,FromFinal,Residue,RelTotal1).
|
||||
|
||||
projection_term_analysis([_|Tail],FromFinal,RelTotal,RelTotal1):-
|
||||
projection_term_analysis(Tail,FromFinal,RelTotal,RelTotal1).
|
||||
|
||||
|
||||
|
||||
add_relation([],Final,Final).
|
||||
|
||||
add_relation([Rel|Tail],RelTotal,RelFinal):-
|
||||
not once(member(Rel,RelTotal)),!,
|
||||
append(RelTotal,[Rel],RelTemp),
|
||||
add_relation(Tail,RelTemp,RelFinal).
|
||||
|
||||
add_relation([_|Tail],RelTotal,RelFinal):-
|
||||
add_relation(Tail,RelTotal,RelFinal).
|
@ -35,7 +35,6 @@
|
||||
]).
|
||||
|
||||
:- use_module(myddas_util_predicates,[
|
||||
'$make_list_of_args'/4,
|
||||
'$prolog2sql'/3,
|
||||
'$write_or_not'/1,
|
||||
'$lenght'/2
|
||||
@ -84,18 +83,19 @@ db_top_level(datalog,Connection,_,_,_):-
|
||||
db_datalog_select(Connection,LA,DbGoal):-
|
||||
|
||||
'$lenght'(LA,Arity),
|
||||
functor(ViewName,viewname,Arity),
|
||||
Name=viewname,
|
||||
functor(ViewName,Name,Arity),
|
||||
% build arg list for viewname/Arity
|
||||
'$make_list_of_args'(1,Arity,ViewName,LA),
|
||||
ViewName=..[Name|LA],
|
||||
|
||||
'$prolog2sql'(ViewName,DbGoal,SQL),
|
||||
|
||||
get_value(Connection,Con),
|
||||
c_db_connection_type(Con,ConType),
|
||||
%c_db_connection_type(Con,ConType),
|
||||
'$write_or_not'(SQL),
|
||||
%( ConType == mysql ->
|
||||
db_my_result_set(Mode),
|
||||
c_db_my_query(SQL,ResultSet,Con,Mode),
|
||||
c_db_my_query(SQL,ResultSet,Con,Mode,_),
|
||||
c_db_my_table_write(ResultSet).
|
||||
|
||||
|
||||
|
@ -25,6 +25,7 @@
|
||||
'$get_value'/2,
|
||||
'$get_values_for_insert'/3,
|
||||
'$make_atom'/2,
|
||||
'$make_atom_args'/2,
|
||||
'$write_or_not'/1,
|
||||
'$abolish_all'/1,
|
||||
'$get_values_for_update'/4,
|
||||
@ -61,13 +62,15 @@
|
||||
|
||||
'$create_multi_query'([ProjTerm],[DbGoal],SQL):- !,
|
||||
'$lenght'(ProjTerm,Arity),
|
||||
functor(ViewName,viewname,Arity),
|
||||
'$make_list_of_args'(1,Arity,ViewName,ProjTerm),
|
||||
Name=viewname,
|
||||
functor(ViewName,Name,Arity),
|
||||
ViewName=..[Name|ProjTerm],
|
||||
'$prolog2sql'(ViewName,DbGoal,SQL).
|
||||
'$create_multi_query'([ProjTerm|TermList],[DbGoal|GoalList],SQL):-
|
||||
'$lenght'(ProjTerm,Arity),
|
||||
functor(ViewName,viewname,Arity),
|
||||
'$make_list_of_args'(1,Arity,ViewName,ProjTerm),
|
||||
Name=viewname,
|
||||
functor(ViewName,Name,Arity),
|
||||
ViewName=..[Name|ProjTerm],
|
||||
'$prolog2sql'(ViewName,DbGoal,SQLQuery),
|
||||
'$create_multi_query'(TermList,GoalList,SQLMulti),
|
||||
'$make_atom'([SQLQuery,' ; ',SQLMulti],SQL).
|
||||
@ -214,14 +217,6 @@
|
||||
%
|
||||
% End of Predicates for making the SQL query
|
||||
%
|
||||
|
||||
'$make_list_of_args'(N,N,F,[H]) :- !,
|
||||
arg(N,F,H).
|
||||
'$make_list_of_args'(N,M,F,[H|T]) :-
|
||||
arg(N,F,H),
|
||||
N1 is N+1,
|
||||
'$make_list_of_args'(N1,M,F,T).
|
||||
|
||||
'$make_a_list'(0,[]) :- !.
|
||||
'$make_a_list'(N,[_|T]) :-
|
||||
N1 is N-1,
|
||||
@ -339,6 +334,21 @@
|
||||
number_atom(Number,Atom),
|
||||
atom_concat(Atom,Result,Final).
|
||||
|
||||
'$make_atom_args'([Atom],Atom):-
|
||||
atom(Atom),!.
|
||||
'$make_atom_args'([Number],Atom):-
|
||||
number_atom(Number,Atom),!.
|
||||
'$make_atom_args'([Atom|T],Final) :-
|
||||
atom(Atom),!,
|
||||
'$make_atom_args'(T,Result),
|
||||
atom_concat(',',Result,X1),
|
||||
atom_concat(Atom,X1,Final).
|
||||
'$make_atom_args'([Number|T],Final) :-
|
||||
'$make_atom_args'(T,Result),
|
||||
number_atom(Number,Atom),
|
||||
atom_concat(',',Result,X1),
|
||||
atom_concat(Atom,X1,Final).
|
||||
|
||||
|
||||
|
||||
% for db_my_insert/3
|
||||
@ -418,4 +428,6 @@
|
||||
'$member_strick'(Element1, [Element2|_]) :-
|
||||
Element1 == Element2,!.
|
||||
'$member_strick'(Element, [_|Rest]) :-
|
||||
'$member_strick'(Element, Rest).
|
||||
'$member_strick'(Element, Rest).
|
||||
|
||||
|
||||
|
@ -61,7 +61,8 @@ MYDDAS_PROGRAMS= $(srcdir)/MYDDAS/myddas.ypp \
|
||||
$(srcdir)/MYDDAS/myddas_top_level.ypp \
|
||||
$(srcdir)/MYDDAS/myddas_errors.ypp \
|
||||
$(srcdir)/MYDDAS/myddas_prolog2sql.ypp \
|
||||
$(srcdir)/MYDDAS/myddas_util_predicates.ypp
|
||||
$(srcdir)/MYDDAS/myddas_util_predicates.ypp \
|
||||
$(srcdir)/MYDDAS/myddas_prolog2sql_optimizer.ypp
|
||||
|
||||
LOGTALK_PROGRAMS= \
|
||||
$(srcdir)/logtalk/logtalk.pl \
|
||||
|
Reference in New Issue
Block a user