diff --git a/Makefile.in b/Makefile.in index 113c58119..c80c6d9c6 100644 --- a/Makefile.in +++ b/Makefile.in @@ -124,7 +124,7 @@ HEADERS = \ $(srcdir)/OPTYap/alpha_locks_funcs.h \ $(srcdir)/OPTYap/pthread_locks.h \ $(srcdir)/H/cut_c.h \ - $(srcdir)/MYDDAS/myddas_util.h + $(srcdir)/MYDDAS/myddas_util.h $(srcdir)/MYDDAS/myddas_structs.h C_SOURCES= \ $(srcdir)/C/absmi.c $(srcdir)/C/adtdefs.c \ diff --git a/library/MYDDAS/myddas.yap b/library/MYDDAS/myddas.yap index 9a5525891..ab985dab2 100644 --- a/library/MYDDAS/myddas.yap +++ b/library/MYDDAS/myddas.yap @@ -16,14 +16,123 @@ *************************************************************************/ :- module(myddas,[ + db_open/5, + db_close/1, + db_verbose/1, - db_is_database_predicate/3, db_module/1, - db_stats/2 + db_is_database_predicate/3, + db_stats/2, + + db_sql_select/3, + db_insert/2, + db_create_table/3, + db_export_view/4, + + db_get_attributes_types/3, + db_number_of_fields/3, + + % myddas_assert_predicates.yap + db_import/3, + db_view/3, + db_insert/3, + + % 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_my_result_set/1, + db_my_describe/2, + db_my_describe/3, + db_my_show_tables/1, + db_my_show_tables/2, + db_my_sql_select/3, + db_my_number_of_fields/3, + db_my_get_attributes_types/3 + ]). -:- use_module(myddas_util_predicates). -:- use_module(myddas_errors). +:- use_module(myddas_assert_predicates,[ + db_import/3, + db_view/3, + db_insert/3 + ]). + +:- 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_my_describe/2, + db_my_describe/3, + db_my_show_tables/1, + db_my_show_tables/2, + db_my_sql_select/3, + db_my_number_of_fields/3, + db_my_get_attributes_types/3 + ]). + +:- use_module(myddas_util_predicates,[ + '$process_sql_goal'/4, + '$process_fields'/3, + '$get_values_for_insert'/3, + '$make_atom'/2, + '$write_or_not'/1, + '$abolish_all'/1, + '$make_a_list'/2 + ]). + +:- use_module(myddas_errors,[ + '$error_checks'/1 + ]). + +:- use_module(myddas_prolog2sql,[ + translate/3 + ]). + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% db_open/5 +% +% +db_open(mysql,Connection,Host/Db,User,Password) :-!, + '$error_checks'(db_open(mysql,Connection,Host/Db,User,Password)), + c_db_my_connect(Host,User,Password,Db,Con), + set_value(Connection,Con). +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). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% db_close/1 +% +% +db_close(Connection):- + '$error_checks'(db_close(Connection)), + get_value(Connection,Con), + '$abolish_all'(Con). +db_close(Connection) :- + '$error_checks'(db_close(Connection)), + get_value(Connection,Con), + c_db_connection_type(Con,ConType), + ( ConType == mysql -> + c_db_my_disconnect(Con) + ; + c_db_odbc_disconnect(Con) + ), + set_value(Connection,[]). % "deletes" atom +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % db_verbose/1 @@ -41,6 +150,19 @@ db_verbose(_):- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% db_module/1 +% +% +db_module(X):- + var(X),!, + get_value(db_module,X). +db_module(ModuleName):- + set_value(db_module,ModuleName). +% default value +:- db_module(user). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % db_is_database_predicate/3 @@ -52,29 +174,163 @@ db_is_database_predicate(PredName,Arity,Module):- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% db_module/1 -% -% -db_module(X):- - var(X),!, - get_value(db_module,X). -db_module(ModuleName):- - set_value(db_module,ModuleName). -%default value -:- db_module(user). -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - - %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % db_stats/2 % % +:- set_value(db_myddas_stats_count,0). db_stats(Connection,List):- - '$get_value'(Connection,Conn), - NumberOfStats = 2, + NumberOfStats = 6, '$make_a_list'(NumberOfStats,List), - c_db_my_stats(Conn,List). -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \ No newline at end of file + ( var(Connection) -> + c_db_stats(0,List) + ; + get_value(Connection,Conn), + c_db_stats(Conn,List) + ), + write('MYDDAS'),nl,write(List),nl, + get_value(db_myddas_stats_count,Value), + write(Value),nl. +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% db_sql_select/3 +% +% +db_sql_select(Connection,SQL,LA):- + '$error_checks'(db_sql_select(Connection,SQL,LA)), + get_value(Connection,Con), + c_db_connection_type(Con,ConType), + ( ConType == mysql -> + c_db_my_number_of_fields_in_query(SQL,Con,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) + ; + '$make_a_list'(Arity,BindList), + c_db_odbc_query(SQL,ResultSet,Arity,BindList,Con),!, + c_db_odbc_row(ResultSet,BindList,LA) + ). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% db_insert/2 +% +% +db_insert(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), + + 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_odbc_query(SQL,_,_,_,Con) + ). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% db_create_table/3 +% FieldsList = [field(Name,Type,Null,Key,DefaultValue)] +% Example [field(campo1,'char(12)',y,y,a),field(campo2,int,y,y,0)] +% TODO Test with ODBC & Type Checks +db_create_table(Connection,TableName,FieldsInf):- + '$error_checks'(db_create_table(Connection,TableName,FieldsInf)), + get_value(Connection,Con), + + '$process_fields'(FieldsInf,FieldString,KeysSQL), + '$make_atom'(['CREATE TABLE `',TableName,'` ( ',FieldString,KeysSQL,' )'],FinalSQL), + + c_db_connection_type(Con,ConType), + '$write_or_not'(FinalSQL), + ( ConType == mysql -> + db_my_result_set(Mode), + c_db_my_query(FinalSQL,_,Con,Mode) + ; + c_db_odbc_query(FinalSQL,_,_,_,Con) + ). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% db_export_view/4 +% TODO Test with ODBC +% +db_export_view(Connection,TableViewName,SQLorDbGoal,FieldsInf):- + '$error_checks'(db_export_view(Connection,TableViewName,SQLorDbGoal,FieldsInf)), + get_value(Connection,Con), + '$process_sql_goal'(TableViewName,SQLorDbGoal,TableName,SQL), + + % Case there's some information about the + % attribute fields of the relation given + % by the user + ( FieldsInf == [] -> + '$make_atom'(['CREATE TABLE ',TableName,' AS ',SQL],FinalSQL) + ; + '$process_fields'(FieldsInf,FieldString,KeysSQL), + '$make_atom'(['CREATE TABLE ',TableName,' (',FieldString,KeysSQL,') AS ',SQL],FinalSQL) + ), + + c_db_connection_type(Con,ConType), + '$write_or_not'(FinalSQL), + ( ConType == mysql -> + db_my_result_set(Mode), + c_db_my_query(FinalSQL,_,Con,Mode) + ; + c_db_odbc_query(FinalSQL,_,_,_,Con) + ). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% db_get_attributes_types/3 +% +% +db_get_attributes_types(Connection,RelationName,TypesList) :- + '$error_checks'(db_get_attributes_types(Connection,RelationName,TypesList)), + get_value(Connection,Con), + c_db_connection_type(Con,ConType), + ( ConType == mysql -> + c_db_my_number_of_fields(RelationName,Con,Arity) + ; + c_db_odbc_number_of_fields(RelationName,Con,Arity) + ), + Size is 2*Arity, + '$make_a_list'(Size,TypesList), + c_db_connection_type(Con,ConType), + ( ConType == mysql -> + c_db_my_get_attributes_types(RelationName,Con,TypesList) + ; + c_db_odbc_get_attributes_types(RelationName,Con,TypesList) + ). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% db_number_of_fields/3 +% +% +db_number_of_fields(Connection,RelationName,Arity) :- + '$error_checks'(db_number_of_fields(Connection,RelationName,Arity)), + get_value(Connection,Con), + c_db_connection_type(Con,ConType), + ( ConType == mysql -> + c_db_my_number_of_fields(RelationName,Con,Arity) + ; + c_db_odbc_number_of_fields(RelationName,Con,Arity) + ). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/library/MYDDAS/myddas_assert_predicates.yap b/library/MYDDAS/myddas_assert_predicates.yap new file mode 100644 index 000000000..13c908a93 --- /dev/null +++ b/library/MYDDAS/myddas_assert_predicates.yap @@ -0,0 +1,230 @@ +/************************************************************************* +* * +* YAP Prolog * +* * +* Yap Prolog was developed at NCCUP - Universidade do Porto * +* * +* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 * +* * +************************************************************************** +* * +* File: myddas_assert_predicates.yap * +* Last rev: * +* mods: * +* comments: Predicates that assert other for the MyDDAS Interface * +* * +*************************************************************************/ + +:- module(myddas_assert_predicates,[ + db_import/3, + db_view/3, + db_insert/3 + ]). + + +:- use_module(myddas,[ + db_module/1 + ]). + +:- use_module(myddas_errors,[ + '$error_checks'/1 + ]). + +:- use_module(myddas_util_predicates,[ + '$get_values_for_insert'/3, + '$make_atom'/2, + '$write_or_not'/1, + '$copy_term_nv'/4, + '$assert_attribute_information'/4, + '$make_a_list'/2, + '$make_list_of_args'/4, + '$where_exists'/2, + '$build_query'/5 + ]). + +:- use_module(myddas_prolog2sql,[ + translate/3, + queries_atom/2 + ]). +:- use_module(myddas_mysql,[ + db_my_result_set/1 + ]). + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% db_import/3 +% +% +db_import(Connection,RelationName,PredName) :- + '$error_checks'(db_import(Connection,RelationName,PredName)), + get_value(Connection,Con), + c_db_connection_type(Con,ConType), + + % get relation arity + ( ConType == mysql -> + c_db_my_number_of_fields(RelationName,Con,Arity) + ; + c_db_odbc_number_of_fields(RelationName,Con,Arity) + ), + db_module(Module), + not c_db_check_if_exists_pred(PredName,Arity,Module), + + R=..[relation,PredName,Arity,RelationName], + % assert relation fact + assert(myddas_prolog2sql:R), + + Size is 2*Arity, + '$make_a_list'(Size,TypesList), + % get attributes types in TypesList [field0,type0,field1,type1...] + ( ConType == mysql -> + c_db_my_get_attributes_types(RelationName,Con,TypesList) + ; + c_db_odbc_get_attributes_types(RelationName,Con,TypesList) + ), + + % assert attributes facts + '$assert_attribute_information'(0,Arity,RelationName,TypesList), + + % build PredName functor + functor(P,PredName,Arity), + '$make_list_of_args'(1,Arity,P,LA), + + %Optimization + '$copy_term_nv'(P,[],G,_), + + %generate the SQL query + translate(G,G,Code), + queries_atom(Code,SQL), + + %build PredName clause + ( ConType == mysql -> + Assert =..[':-',P,','(myddas_assert_predicates:'$build_query'(0,SQL,Code,LA,FinalSQL), + ','(myddas_assert_predicates:db_my_result_set(Mode), + ','(myddas_assert_predicates:'$write_or_not'(FinalSQL), + ','(myddas_assert_predicates:c_db_my_query(FinalSQL,ResultSet,Con,Mode), + ','(!,myddas_assert_predicates:c_db_my_row(ResultSet,Arity,LA))))))] + +% Assert =..[':-',P,','(get_value(db_myddas_stats_count,Number), +% ','(statistics(cputime,TimeI), +% ','(myddas_assert_predicates:'$build_query'(0,SQL,Code,LA,FinalSQL), +% ','(myddas_assert_predicates:db_my_result_set(Mode), +% ','(myddas_assert_predicates:'$write_or_not'(FinalSQL), +% ','(myddas_assert_predicates:c_db_my_query(FinalSQL,ResultSet,Con,Mode), +% ','(statistics(cputime,TimeF), +% ','(Temp is TimeF - TimeI, +% ','(Temp2 is Temp + Number, +% ','(set_value(db_myddas_stats_count,Temp2), +% ','(!,myddas_assert_predicates:c_db_my_row(ResultSet,Arity,LA))))))))))))] + ; + + Assert =..[':-',P,','(myddas_assert_predicates:'$build_query'(0,SQL,Code,LA,FinalSQL), + ','(myddas_assert_predicates:'$make_a_list'(Arity,BindList), + ','(myddas_assert_predicates:c_db_odbc_query(FinalSQL,ResultSet,Arity,BindList,Connection), + ','(myddas_assert_predicates:'$write_or_not'(FinalSQL), + ','(!,myddas_assert_predicates:c_db_odbc_row(ResultSet,BindList,LA))))))] + ), + + assert(Module:Assert), + c_db_add_preds(PredName,Arity,Module,Con). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% db_view/3 +% +% +db_view(Connection,PredName,DbGoal) :- + '$error_checks'(db_view(Connection,PredName,DbGoal)), + get_value(Connection,Con), + + % get arity of projection term + functor(PredName,ViewName,Arity), + functor(NewName,ViewName,Arity), + db_module(Module), + not c_db_check_if_exists_pred(ViewName,Arity,Module), + + % This copy_term is done to prevent the unification + % with top-level variables A='var('A')' error + copy_term((PredName,DbGoal),(CopyView,CopyGoal)), + translate(CopyView,CopyGoal,Code), + queries_atom(Code,SQL), + + % checks if the WHERE commend of SQL exists in the string + '$where_exists'(SQL,Flag), + + '$make_list_of_args'(1,Arity,NewName,LA), + + c_db_connection_type(Con,ConType), + % build view clause + ( ConType == mysql -> + Assert =..[':-',NewName, + ','(myddas_assert_predicates:'$build_query'(Flag,SQL,Code,LA,FinalSQL), + ','(myddas_assert_predicates:db_my_result_set(Mode), + ','(myddas_assert_predicates:'$write_or_not'(FinalSQL), + ','(myddas_assert_predicates:c_db_my_query(FinalSQL,ResultSet,Con,Mode), + ','(!,myddas_assert_predicates:c_db_my_row(ResultSet,Arity,LA))))))] + ; + Assert =..[':-',NewName, + ','(myddas_assert_predicates:'$build_query'(Flag,SQL,Code,LA,FinalSQL), + ','(myddas_assert_predicates:'$make_a_list'(Arity,BindList), + ','(myddas_assert_predicates:'$write_or_not'(FinalSQL), + ','(myddas_assert_predicates:c_db_odbc_query(FinalSQL,ResultSet,Arity,BindList,Con), + ','(!,myddas_assert_predicates:c_db_odbc_row(ResultSet,BindList,LA))))))] + + ), + assert(Module:Assert), + c_db_add_preds(ViewName,Arity,Module,Con). + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% db_insert/3 +% +% +db_insert(Connection,RelationName,PredName) :- + '$error_checks'(db_insert3(Connection,RelationName,PredName)), + get_value(Connection,Con), + c_db_connection_type(Con,ConType), + + % get relation arity + ( ConType == mysql -> + c_db_my_number_of_fields(RelationName,Con,Arity) + ; + c_db_odbc_number_of_fields(RelationName,Con,Arity) + ), + db_module(Module), + not c_db_check_if_exists_pred(PredName,Arity,Module), + + R=..[relation,PredName,Arity,RelationName], + % assert relation fact + assert(myddas_prolog2sql:R), + + % build PredName functor + functor(Predicate,PredName,Arity), + '$make_list_of_args'(1,Arity,Predicate,LA), + + Size is 2*Arity, + '$make_a_list'(Size,TypesList), + + % get attributes types in TypesList [field0,type0,field1,type1...] + % and build PredName clause + ( ConType == mysql -> + c_db_my_get_attributes_types(RelationName,Con,TypesList), + Assert =..[':-',Predicate,','(myddas_assert_predicates:'$get_values_for_insert'(TypesList,LA,ValuesList), + ','(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)))))] + ; + c_db_odbc_get_attributes_types(RelationName,Con,TypesList), + Assert =..[':-',Predicate,','(myddas_assert_predicates:'$get_values_for_insert'(TypesList,LA,ValuesList), + ','(myddas_assert_predicates:'$make_atom'(['INSERT INTO ',RelationName,' VALUES ('|ValuesList],SQL), + ','(myddas_assert_predicates:'$write_or_not'(SQL), + myddas_assert_predicates:c_db_odbc_query(SQL,_,_,_,Con))))] + ), + assert(Module:Assert), + c_db_add_preds(PredName,Arity,Module,Con). +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/library/MYDDAS/myddas_errors.yap b/library/MYDDAS/myddas_errors.yap index a73fb633f..a47fb4946 100644 --- a/library/MYDDAS/myddas_errors.yap +++ b/library/MYDDAS/myddas_errors.yap @@ -16,98 +16,101 @@ *************************************************************************/ :- module(myddas_errors,[ - '$error_checks'/1, - '$db_my_error'/2 + '$error_checks'/1 ]). -:- use_module(myddas_util_predicates). +:- use_module(myddas_util_predicates,[ + '$make_a_list'/2, + '$check_fields'/2 + ]). -% must have only one relation -'$error_checks'(db_my_insert3(_,_,_)):-!. -'$error_checks'(db_my_insert2(_,Conn,[query(Att,[rel(Relation,_)],_)])) :- !, +:- use_module(lists,[ + is_list/1 + ]). + +'$error_checks'(db_export_view(Connection,TableViewName,SQLorDbGoal,FieldsInf)):-!, + atom(Connection), + ( atom(TableViewName) -> atom(SQLorDbGoal) ; true ), + ( atom(SQLorDbGoal) -> atom(TableViewName) ; true ), + is_list(FieldsInf). +'$error_checks'(db_create_table(Connection,TableName,FieldsInf)):-!, + atom(Connection), + atom(TableName), + FieldsInf = [_|_]. +'$error_checks'(db_insert3(Connection,RelationName,PredName)):-!, + atom(Connection), + atom(RelationName), + atom(PredName). +'$error_checks'(db_insert2(Connection,_,[query(Att,[rel(Relation,_)],_)])) :- !, + atom(Connection), + get_value(Connection,Con), % Number of fields of the Relation, must be % equal to the number of attributes - c_db_my_number_of_fields(Relation,Conn,Arity), + c_db_connection_type(Con,ConType), + ( ConType == mysql -> + c_db_my_number_of_fields(Relation,Con,Arity) + ; + c_db_odbc_number_of_fields(Relation,Con,Arity) + ), length(Att,Arity), % All fields must be Instanciated ( FALTA POR O NULL ) '$make_a_list'(Arity,FieldsProperties), - c_db_my_get_fields_properties(Relation,Conn,FieldsProperties), + ( ConType == mysql -> + c_db_my_get_fields_properties(Relation,Con,FieldsProperties) + ; + c_db_odbc_get_fields_properties(Relation,Con,FieldsProperties) + ), '$check_fields'(Att,FieldsProperties). -%'$error_checks'(Preddb_Call):- - %'$do_error'(db_my_error(incompatible_db_predicate,PredCall)). - -'$error_checks'(db_my_open(Host,User,Password,Db,Conn)) :- !, +'$error_checks'(db_open(mysql,Connection,Host/Db,User,Password)) :- !, nonvar(Host), % == \+var(Host) nonvar(User), nonvar(Password), nonvar(Db), - atom(Conn), - get_value(Conn,[]). % Nao pode ter nenhum valor atribuido -'$error_checks'(db_my_close(_)) :- !. -'$error_checks'(db_my_import(RelationName,PredName,_)) :- !, - nonvar(RelationName), + atom(Connection), + get_value(Connection,[]). % Nao pode ter nenhum valor atribuido +'$error_checks'(db_open(odbc,Connection,ODBCEntry,User,Password)) :- !, + nonvar(ODBCEntry), % == \+var(ODBCEntry) + nonvar(User), + nonvar(Password), + atom(Connection), + get_value(Connection,[]). % Nao pode ter nenhum valor atribuido +'$error_checks'(db_view(Connection,PredName,DbGoal)) :- !, + atom(Connection), + nonvar(DbGoal), nonvar(PredName). -'$error_checks'(db_my_view(PredName,DbGoal,_)) :- !, - nonvar(DbGoal), - nonvar(PredName). -'$error_checks'(db_my_number_of_fields(RelationName,_,_)) :- !, +'$error_checks'(db_import(Connection,RelationName,PredName)) :- !, + atom(Connection), + atom(RelationName), + atom(PredName). +'$error_checks'(db_get_attributes_types(Connection,RelationName,_)) :- !, + atom(Connection), nonvar(RelationName). -'$error_checks'(db_my_get_attributes_types(RelationName,_,_)) :- !, +'$error_checks'(db_sql_select(Connection,SQL,LA)):- !, + atom(Connection), + nonvar(SQL), + var(LA). +'$error_checks'(db_number_of_fields(Connection,RelationName,_)) :- !, + atom(Connection), nonvar(RelationName). +'$error_checks'(db_close(Connection)) :- !, + atom(Connection). +% must have only one relation '$error_checks'(db_my_describe(Relation,_)) :- !, nonvar(Relation). '$error_checks'(db_my_show_tables(_)):- !. -'$error_checks'(db_my_sql_select(_,SQL,LA)):- !, - nonvar(SQL), - var(LA). '$error_checks'(db_is_database_predicate(PredName,Arity,Module)):-!, nonvar(PredName), nonvar(Arity), nonvar(Module). % Prevent the error of given an atom that has no value -'$error_checks'(get_value(Conn,Connection)) :- !, +'$error_checks'(get_value(Connection,Con)) :- !, % This also prevents the case of giving the number of the connection % as an argument - atom(Conn), - var(Connection), - get_value(Conn,Value), + atom(Connection), + var(Con), + get_value(Connection,Value), Value \== []. -% must have only one relation -%'$error_checks'(db_insert(_,_,_)):-!. -'$error_checks'(db_odbc_insert3(_,_,_)):-!. -'$error_checks'(db_odbc_insert2(PredName,Conn,[query(Att,[rel(Relation,_)],_)])) :- !, - % Number of fields of the Relation, must be - % equal to the number of attributes - c_db_odbc_number_of_fields(Relation,Conn,Arity), - length(Att,Arity), - % All fields must be Instanciated ( FALTA POR O NULL ) - '$make_a_list'(Arity,FieldsProperties), - c_db_odbc_get_fields_properties(Relation,Conn,FieldsProperties), - '$check_fields'(Att,FieldsProperties). -%'$error_checks'(PredCall):- - %'$do_error'(db_error(incompatible_db_predicate,PredCall)). - -'$error_checks'(db_odbc_open(Host,User,Password,Conn)) :- !, - nonvar(Host), % == \+var(Host) - nonvar(User), - nonvar(Password), - atom(Conn), - get_value(Conn,[]). % Nao pode ter nenhum valor atribuido -'$error_checks'(db_odbc_close(_)) :- !. -'$error_checks'(db_odbc_import(RelationName,PredName,_)) :- !, - nonvar(RelationName), - nonvar(PredName). -'$error_checks'(db_odbc_view(PredName,DbGoal,Connection)) :- !, - nonvar(DbGoal), - nonvar(PredName). -'$error_checks'(db_odbc_number_of_fields(RelationName,Connection,Arity)) :- !, - nonvar(RelationName). -'$error_checks'(db_odbc_get_attributes_types(RelationName,Connection,TypesList)) :- !, - nonvar(RelationName). -'$error_checks'(db_odbc_sql_select(Connection,SQL,LA)):- !, - nonvar(SQL), - var(LA). % Prevent the error of given an atom that has no value '$error_checks'(get_value(Conn,Connection)) :- !, % This also prevents the case of giving the number of the connection @@ -116,9 +119,3 @@ var(Connection), get_value(Conn,Value), Value \== []. - - - -'$db_my_error'(ERROR,_):-var(ERROR),!. -'$db_my_error'(2005,c_db_my_connect(Host,User,Password,Db,Connection)):-!, - write(Host),nl. \ No newline at end of file diff --git a/library/MYDDAS/myddas_mysql.yap b/library/MYDDAS/myddas_mysql.yap index d78e3c4dd..3574a3add 100755 --- a/library/MYDDAS/myddas_mysql.yap +++ b/library/MYDDAS/myddas_mysql.yap @@ -11,7 +11,7 @@ * File: myddas_mysql.yap * * Last rev: * * mods: * -* comments: MySQL Server communication library * +* comments: MySQL Predicates * * * *************************************************************************/ @@ -30,16 +30,19 @@ db_my_sql_select/3, db_my_number_of_fields/3, db_my_get_attributes_types/3 - ]). + ]). +:- use_module(myddas_errors,[ + '$error_checks'/1 + ]). -:- use_module(myddas). -:- use_module(myddas_errors). -:- use_module(myddas_prolog2sql,[translate/3,queries_atom/2]). -:- use_module(myddas_util_predicates). +:- use_module(myddas_util_predicates,[ + '$get_value'/2, + '$make_atom'/2, + '$write_or_not'/1 + ]). -:- use_module(lists,[append/3]). %-------------------------------------------------------- % Public Predicates @@ -49,11 +52,10 @@ % db_my_open/5 % % -db_my_open(Host,User,Password,Db,Conn) :- - '$error_checks'(db_my_open(Host,User,Password,Db,Conn)), - c_db_my_connect(Host,User,Password,Db,Connection), - %'$db_my_error'(ERROR,c_db_my_connect(Host,User,Password,Db,Connection)), - set_value(Conn,Connection). +db_my_open(_,_,_,_,_) :- + write('WARNING!! Now we use db_open/5'),nl, + write('USAGE: db_open(ConType,Connection,Host/Db,User,Password).'),nl, + halt. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -62,14 +64,10 @@ db_my_open(Host,User,Password,Db,Conn) :- % db_my_close/1 % % -db_my_close(Conn):- - '$error_checks'(db_my_close(Conn)), - '$abolish_all'(Conn). -db_my_close(Conn) :- - '$error_checks'(db_my_close(Conn)), - '$get_value'(Conn,Connection), - c_db_my_disconnect(Connection), - set_value(Conn,[]). % "deletes" atom +db_my_close(_):- + write('WARNING!! Now we use db_close/1'),nl, + write('USAGE: db_close(Connection).'),nl, + halt. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -79,10 +77,10 @@ db_my_close(Conn) :- % db_my_import/3 % % -db_my_import(RelationName,PredName,Connection) :- - '$error_checks'(db_my_import(RelationName,PredName,Connection)), - '$get_value'(Connection,Conn), - '$assert_import_clause'(RelationName,PredName,Conn). +db_my_import(_,_,_) :- + write('WARNING!! Now we use db_import/3'),nl, + write('USAGE: db_import(Connection,RelationName,PredName).'),nl, + halt. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -91,10 +89,10 @@ db_my_import(RelationName,PredName,Connection) :- % db_my_view/3 % % -db_my_view(PredName,DbGoal,Connection) :- - '$error_checks'(db_my_view(PredName,DbGoal,Connection)), - '$get_value'(Connection,Conn), - '$assert_view_clause'(PredName,DbGoal,Conn). +db_my_view(_,_,_) :- + write('WARNING!! Now we use db_view/3'),nl, + write('USAGE: db_import(Connection,RelationName,DBGoal).'),nl, + halt. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -103,14 +101,10 @@ db_my_view(PredName,DbGoal,Connection) :- % db_my_insert/2 % % -db_my_insert(PredName,Connection):- - '$get_value'(Connection,Conn), - translate(PredName,PredName,Code), - '$error_checks'(db_my_insert2(PredName,Conn,Code)), - '$get_values_for_insert'(Code,ValuesList,RelName), - '$make_atom'(['INSERT INTO ',RelName,' VALUES'|ValuesList],SQL), - db_my_result_set(Mode), - c_db_my_query(SQL,_,Conn,Mode). +db_my_insert(_,_):- + write('WARNING!! Now we use db_insert/2'),nl, + write('USAGE: db_insert(Connection,PredName).'),nl, + halt. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -119,10 +113,10 @@ db_my_insert(PredName,Connection):- % db_my_insert/3 % % -db_my_insert(RelationName,PredName,Connection) :- - '$get_value'(Connection,Conn), - '$error_checks'(db_my_insert3(RelationName,PredName,Connection)), - '$assert_relation_insert'(RelationName,PredName,Conn). +db_my_insert(_,_,_) :- + write('WARNING!! Now we use db_insert/3'),nl, + write('USAGE: db_insert(Connection,RelationName,PredName).'),nl, + halt. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -209,15 +203,10 @@ db_my_show_tables(Connection,table(Table)) :- % db_my_sql_select/3 % % -db_my_sql_select(Connection,SQL,LA):- - '$error_checks'(db_my_sql_select(Connection,SQL,LA)), - '$get_value'(Connection,Conn), - c_db_my_number_of_fields_in_query(SQL,Conn,Arity), - '$make_a_list'(Arity,LA), - db_my_result_set(Mode), - '$write_or_not'(SQL),!, - c_db_my_query(SQL,ResultSet,Conn,Mode), - c_db_my_row(ResultSet,Arity,LA). +db_my_sql_select(_,_,_):- + write('WARNING!! Now we use db_sql_select/3'),nl, + write('USAGE: db_sql_select(Connection,SQL,ListaArgs).'),nl, + halt. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -226,10 +215,10 @@ db_my_sql_select(Connection,SQL,LA):- % db_my_number_of_fields/3 % % -db_my_number_of_fields(RelationName,Connection,Arity) :- - '$error_checks'(db_my_number_of_fields(RelationName,Connection,Arity)), - '$get_value'(Connection,Conn), - c_db_my_number_of_fields(RelationName,Conn,Arity). +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. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -238,113 +227,8 @@ db_my_number_of_fields(RelationName,Connection,Arity) :- % db_my_get_attributes_types/3 % % -db_my_get_attributes_types(RelationName,Connection,TypesList) :- - '$error_checks'(db_my_get_attributes_types(RelationName,Connection,TypesList)), - '$get_value'(Connection,Conn), - c_db_my_number_of_fields(RelationName,Conn,Arity), - Size is 2*Arity, - '$make_a_list'(Size,TypesList), - c_db_my_get_attributes_types(RelationName,Conn,TypesList). +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. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - - - - -%-------------------------------------------------------- -% Private Predicates -%-------------------------------------------------------- -'$assert_view_clause'(ViewName,DbGoal,Connection) :- - % here we can add some error control, like checking DBgoals include - % only DB relations - % get arity of projection term - functor(ViewName,PredName,Arity), - functor(NewName,PredName,Arity), - db_module(Module), - not c_db_my_check_if_exists_pred(PredName,Arity,Module), - - % This copy_term is done to prevent the unification - % with top-level variables A='var('A')' error - copy_term((ViewName,DbGoal),(CopyView,CopyGoal)), - translate(CopyView,CopyGoal,Code), - queries_atom(Code,SQL), - % checks if the WHERE commend of SQL exists in the string - '$where_exists'(SQL,Flag), - - '$make_list_of_args'(1,Arity,NewName,LA), - % build view clause - Assert =..[':-',NewName, - ','(myddas_mysql:'$build_query'(Flag,SQL,Code,LA,FinalSQL), - ','(myddas_mysql:db_my_result_set(Mode), - ','(myddas_mysql:'$write_or_not'(FinalSQL), - ','(myddas_mysql:c_db_my_query(FinalSQL,ResultSet,Connection,Mode), - ','(!,myddas_mysql:c_db_my_row(ResultSet,Arity,LA))))))], - assert(Module:Assert), - c_db_add_preds(PredName,Arity,Module,Connection). - - -'$assert_relation_insert'(RelationName,PredName,Connection) :- - % get relation arity - c_db_my_number_of_fields(RelationName,Connection,Arity), - db_module(Module), - not c_db_my_check_if_exists_pred(PredName,Arity,Module), - - R=..[relation,PredName,Arity,RelationName], - % assert relation fact - assert(myddas_prolog2sql:R), - - Size is 2*Arity, - '$make_a_list'(Size,TypesList), - % get attributes types in TypesList [field0,type0,field1,type1...] - c_db_my_get_attributes_types(RelationName,Connection,TypesList), - - % build PredName functor - functor(P,PredName,Arity), - '$make_list_of_args'(1,Arity,P,LA), - - % build PredName clause - Assert =..[':-',P,','(myddas_mysql:'$get_values_for_insert'(TypesList,LA,ValuesList), - ','(myddas_mysql:'$make_atom'(['INSERT INTO ',RelationName,' VALUES ('|ValuesList],SQL), - ','(myddas_mysql:db_my_result_set(Mode), - ','(myddas_mysql:'$write_or_not'(SQL), - myddas_mysql:c_db_my_query(SQL,_,Connection,Mode)))))], - assert(Module:Assert), - c_db_add_preds(PredName,Arity,Module,Connection). - - -'$assert_import_clause'(RelationName,PredName,Connection) :- - % get relation arity - c_db_my_number_of_fields(RelationName,Connection,Arity), - db_module(Module), - not c_db_my_check_if_exists_pred(PredName,Arity,Module), - - R=..[relation,PredName,Arity,RelationName], - % assert relation fact - assert(myddas_prolog2sql:R), - - Size is 2*Arity, - '$make_a_list'(Size,TypesList), - % get attributes types in TypesList [field0,type0,field1,type1...] - c_db_my_get_attributes_types(RelationName,Connection,TypesList), - % assert attributes facts - '$assert_attribute_information'(0,Arity,RelationName,TypesList), - - % build PredName functor - functor(P,PredName,Arity), - '$make_list_of_args'(1,Arity,P,LA), - - %Optimization - '$copy_term_nv'(P,[],G,_), - - %generate the SQL query - translate(G,G,Code), - queries_atom(Code,SQL), - - % build PredName clause - Assert =..[':-',P,','(myddas_mysql:'$build_query'(0,SQL,Code,LA,FinalSQL), - ','(myddas_mysql:db_my_result_set(Mode), - ','(myddas_mysql:'$write_or_not'(FinalSQL), - ','(myddas_mysql:c_db_my_query(FinalSQL,ResultSet,Connection,Mode), - ','(!,myddas_mysql:c_db_my_row(ResultSet,Arity,LA))))))], - assert(Module:Assert), - c_db_add_preds(PredName,Arity,Module,Connection). diff --git a/library/MYDDAS/myddas_odbc.yap b/library/MYDDAS/myddas_odbc.yap deleted file mode 100755 index 6615aa76d..000000000 --- a/library/MYDDAS/myddas_odbc.yap +++ /dev/null @@ -1,260 +0,0 @@ -/************************************************************************* -* * -* YAP Prolog * -* * -* Yap Prolog was developed at NCCUP - Universidade do Porto * -* * -* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 * -* * -************************************************************************** -* * -* File: myddas_odbc.yap * -* Last rev: * -* mods: * -* comments: ODBC Driver communication library * -* * -*************************************************************************/ - -:- module(myddas_odbc,[ - db_odbc_open/4, - db_odbc_close/1, - db_odbc_import/3, - db_odbc_view/3, - db_odbc_insert/2, - db_odbc_insert/3, - db_odbc_sql_select/3, - db_odbc_number_of_fields/3, - db_odbc_get_attributes_types/3 - ]). - -:- use_module(myddas). -:- use_module(myddas_errors). -:- use_module(myddas_prolog2sql,[translate/3,queries_atom/2]). -:- use_module(myddas_util_predicates). - -:- use_module(lists,[append/3]). - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% db_odbc_open/4 -% -% -db_odbc_open(ODBCEntry,User,Password,Conn) :- - '$error_checks'(db_odbc_open(ODBCEntry,User,Password,Conn)), - c_db_odbc_connect(ODBCEntry,User,Password,Connection), - set_value(Conn,Connection). -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% db_odbc_close/1 -% -% -db_odbc_close(Conn):- - '$error_checks'(db_odbc_close(Conn)), - '$abolish_all'(Conn). -db_odbc_close(Conn) :- - '$error_checks'(db_odbc_close(Conn)), - '$get_value'(Conn,Connection), - c_db_odbc_disconnect(Connection), - set_value(Conn,[]). % "deletes" atom -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% db_odbc_import/3 -% -% -db_odbc_import(RelationName,PredName,Connection) :- - '$error_checks'(db_odbc_import(RelationName,PredName,Connection)), - '$get_value'(Connection,Conn), - '$assert_import_clause'(RelationName,PredName,Conn). -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% db_odbc_view/3 -% -% -db_odbc_view(PredName,DbGoal,Connection) :- - '$error_checks'(db_odbc_view(PredName,DbGoal,Connection)), - '$get_value'(Connection,Conn), - '$assert_view_clause'(PredName,DbGoal,Conn). -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% db_odbc_insert/2 -% -% -db_odbc_insert(PredName,Connection):- - '$get_value'(Connection,Conn), - translate(PredName,PredName,Code), - '$error_checks'(db_odbc_insert2(PredName,Conn,Code)), - '$get_values_for_insert'(Code,ValuesList,RelName), - '$make_atom'(['INSERT INTO ',RelName,' VALUES'|ValuesList],SQL), - c_db_odbc_query(SQL,_,_,_,Conn). -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% db_odbc_insert/3 -% -% -db_odbc_insert(RelationName,PredName,Connection) :- - '$error_checks'(db_odbc_insert3(RelationName,PredName,Connection)), - '$get_value'(Connection,Conn), - '$assert_relation_insert'(RelationName,PredName,Conn). -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% db_odbc_sql_select/3 -% -% -db_odbc_sql_select(Connection,SQL,LA):- - '$error_checks'(db_odbc_sql_select(Connection,SQL,LA)), - '$get_value'(Connection,Conn), - c_db_odbc_number_of_fields_in_query(SQL,Conn,Arity), - '$make_a_list'(Arity,LA), - '$make_a_list'(Arity,BindList), - '$write_or_not'(SQL), - c_db_odbc_query(SQL,ResultSet,Arity,BindList,Conn),!, - c_db_odbc_row(ResultSet,BindList,LA). -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% db_odbc_number_of_fields/3 -% -% -db_odbc_number_of_fields(RelationName,Connection,Arity) :- - '$error_checks'(db_odbc_number_of_fields(RelationName,Connection,Arity)), - '$get_value'(Connection,Conn), - c_db_odbc_number_of_fields(RelationName,Conn,Arity). -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% db_odbc_get_attributes_types/3 -% -% -db_odbc_get_attributes_types(RelationName,Connection,TypesList) :- - '$error_checks'(db_odbc_get_attributes_types(RelationName,Connection,TypesList)), - '$get_value'(Connection,Conn), - c_db_odbc_number_of_fields(RelationName,Conn,Arity), - Size is 2*Arity, - '$make_a_list'(Size,TypesList), - c_db_odbc_get_attributes_types(RelationName,Conn,TypesList). -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% - - - -%-------------------------------------------------------- -% Private Predicates -%-------------------------------------------------------- -'$assert_view_clause'(ViewName,DbGoal,Connection) :- - % here we can add some error control, like checking DBgoals include - % only DB relations - % get arity of projection term - functor(ViewName,PredName,Arity), - functor(NewName,PredName,Arity), - db_module(Module), - not c_db_check_if_exists_pred(PredName,Arity,Module), - - % This copy_term is done to prevent the unification - % with top-level variables A='var('A')' error - copy_term((ViewName,DbGoal),(CopyView,CopyGoal)), - translate(ViewName,DbGoal,Code), - queries_atom(Code,SQL), - % checks if the WHERE commend of SQL exists in the string - '$where_exists'(SQL,Flag), - - '$make_list_of_args'(1,Arity,NewName,LA), - % build view clause - Assert =..[':-',NewName, - ','(myddas_odbc:'$build_query'(Flag,SQL,Code,LA,FinalSQL), - ','(myddas_odbc:'$make_a_list'(Arity,BindList), - ','(myddas_odbc:c_db_odbc_query(FinalSQL,ResultSet,Arity,BindList,Connection), - ','(myddas_odbc:'$write_or_not'(FinalSQL), - ','(!,myddas_odbc:c_db_odbc_row(ResultSet,BindList,LA))))))], - assert(Module:Assert), - c_db_add_preds(PredName,Arity,Module,Connection). - - - -'$assert_relation_insert'(RelationName,PredName,Connection) :- - % get relation arity - c_db_odbc_number_of_fields(RelationName,Connection,Arity), - db_module(Module), - not c_db_check_if_exists_pred(PredName,Arity,Module), - - R=..[relation,PredName,Arity,RelationName], - % assert relation fact - assert(myddas_prolog2sql:R), - - Size is 2*Arity, - '$make_a_list'(Size,TypesList), - % get attributes types in TypesList [field0,type0,field1,type1...] - c_db_odbc_get_attributes_types(RelationName,Connection,TypesList), - - % build PredName functor - functor(P,PredName,Arity), - '$make_list_of_args'(1,Arity,P,LA), - - % build PredName clause - Assert =..[':-',P,','(myddas_odbc:'$get_values_for_insert'(TypesList,LA,ValuesList), - ','(myddas_odbc:'$make_atom'(['INSERT INTO ',RelationName,' VALUES ('|ValuesList],SQL), - myddas_odbc:c_db_odbc_query(SQL,_,_,_,Connection)))], - assert(Module:Assert), - c_db_add_preds(PredName,Arity,Module,Connection). - - - - -'$assert_import_clause'(RelationName,PredName,Connection) :- - % get relation arity - c_db_odbc_number_of_fields(RelationName,Connection,Arity), - db_module(Module), - not c_db_check_if_exists_pred(PredName,Arity,Module), - - R=..[relation,PredName,Arity,RelationName], - % assert relation fact - assert(myddas_prolog2sql:R), - - Size is 2*Arity, - '$make_a_list'(Size,TypesList), - % get attributes types in TypesList [field0,type0,field1,type1...] - c_db_odbc_get_attributes_types(RelationName,Connection,TypesList), - % assert attributes facts - '$assert_attribute_information'(0,Arity,RelationName,TypesList), - - % build PredName functor - functor(P,PredName,Arity), - % build arg list for db_row/2 - '$make_list_of_args'(1,Arity,P,LA), - - %Optimization - '$copy_term_nv'(P,[],G,_), - - %generate the SQL query - translate(G,G,Code), - queries_atom(Code,SQL), - - % build PredName clause - Assert =..[':-',P,','(myddas_odbc:'$build_query'(0,SQL,Code,LA,FinalSQL), - ','(myddas_odbc:'$make_a_list'(Arity,BindList), - ','(myddas_odbc:c_db_odbc_query(FinalSQL,ResultSet,Arity,BindList,Connection), - ','(myddas_odbc:'$write_or_not'(FinalSQL), - ','(!,myddas_odbc:c_db_odbc_row(ResultSet,BindList,LA))))))], - assert(Module:Assert), - c_db_add_preds(PredName,Arity,Module,Connection). - - diff --git a/library/MYDDAS/myddas_prolog2sql.yap b/library/MYDDAS/myddas_prolog2sql.yap index a71a6fd78..59c188ab5 100644 --- a/library/MYDDAS/myddas_prolog2sql.yap +++ b/library/MYDDAS/myddas_prolog2sql.yap @@ -287,7 +287,7 @@ translate_goal(distinct(Goal),List,SQL,Dict,DistinctDict):-!, %DEBUG add_distinct_statement(Dict,Dict):- - append([A],[1,2],_). + append([_],[1,2],_). diff --git a/library/MYDDAS/myddas_test_predicates.yap b/library/MYDDAS/myddas_test_predicates.yap index e161943f0..5736db2a3 100644 --- a/library/MYDDAS/myddas_test_predicates.yap +++ b/library/MYDDAS/myddas_test_predicates.yap @@ -1,10 +1,13 @@ :- module(myddas_test_predicates,[ % Tests or Debug Predicates %db_my_delete/2, + db_create_table/3, + db_export_view/4, + db_assert_view/4, db_my_insert_test/2, db_my_update/3, db_my_import_query_normal/3, - db_view/3, % DEBUG ONLY + db_view_original/3, % DEBUG ONLY db_my_ilpview/4 ]). @@ -17,6 +20,36 @@ :- use_module(lists). + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% db_assert_view/4 +% TODO Test with ODBC +% TODO error checks +db_assert_view(ViewName,SQLorDbGoal,FieldsInf,Connection):- + '$get_value'(Connection,Con), + %'$error_checks'(), + ( var(ViewName) -> + c_db_get_new_table_name(Con,ViewName), + TableName = ViewName + ), + '$process_sql_goal'(ViewName,SQLorDbGoal,TableName,SQL), + + % Case there's some information about the + % attribute fields of the relation given + % by the user + '$generate_final_sql'(FieldsInf,TableName,SQL,FinalSQL), + '$run_query'(Con,FinalSQL), + + % TODO: Optimize this + db_my_import(TableName,TableName,Connection). + + + + + + + %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % db_my_insert/2 % @@ -271,10 +304,10 @@ db_my_ilpview(LA,ViewName,DbGoal,Connection):- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -% db_view/3 +% db_view_original/3 % % -db_view(PredName,DbGoal,Connection) :- +db_view_original(PredName,DbGoal,Connection) :- '$error_checks'(db_my_view(PredName,DbGoal,Connection)), '$get_value'(Connection,Conn), '$assert_view_clause2'(PredName,DbGoal,Conn). diff --git a/library/MYDDAS/myddas_util_predicates.yap b/library/MYDDAS/myddas_util_predicates.yap index ad6fb8aa4..793614cb2 100644 --- a/library/MYDDAS/myddas_util_predicates.yap +++ b/library/MYDDAS/myddas_util_predicates.yap @@ -16,6 +16,8 @@ *************************************************************************/ :- module(myddas_util_predicates,[ + '$process_sql_goal'/4, + '$process_fields'/3, '$check_fields'/2, '$get_value'/2, '$get_values_for_insert'/3, @@ -34,9 +36,75 @@ '$assert_facts'/2 ]). -:- use_module(myddas). -:- use_module(myddas_errors). -:- use_module(lists,[append/3]). +:- use_module(myddas,[ + db_verbose/1 + ]). + +:- use_module(myddas_errors,[ + '$error_checks'/1 + ]). + +:- use_module(myddas_prolog2sql,[ + translate/3, + queries_atom/2 + ]). + +'$process_sql_goal'(TableViewName,SQLorDbGoal,TableName,SQL):- + (atom(SQLorDbGoal) -> + SQL = SQLorDbGoal, + TableName = TableViewName + ; + % This copy_term is done to prevent the unification + % with top-level variables A='var('A')' error + copy_term((TableViewName,SQLorDbGoal),(CopyView,CopyGoal)), + translate(CopyView,CopyGoal,Code), + queries_atom(Code,SQL), + functor(TableViewName,TableName,_) + ). + +'$process_fields'(FieldsInf,FieldString,KeysSQL):- + '$create_field_list'(FieldsInf,FieldString,PrimaryKeys), + '$process_primary_keys'(PrimaryKeys,KeysSQL). + + +'$process_primary_keys'([],''). +'$process_primary_keys'([FieldName|Fields],KeysSQL):- + '$process_primary_keys_put_comma'(Fields,CommaSQL), + '$make_atom'([' , PRIMARY KEY ( ',FieldName,CommaSQL,' )'],KeysSQL). + +'$process_primary_keys_put_comma'([],''):-!. +'$process_primary_keys_put_comma'([FieldName|Fields],CommaSQL):-!, + '$process_primary_keys_put_comma'(Fields,TempSQL), + '$make_atom'([' , `',FieldName,'` ',TempSQL],CommaSQL). + + +'$create_field_list'([field(Name,Type,Null,Key,DefaultValue)],FinalSQL,PrimaryKeys):-!, + '$field_extra_options'(Name,Null,Key,[],DefaultValue,TempSQL,PrimaryKeys), + '$make_atom'([' `',Name,'` ',Type,TempSQL],FinalSQL). +'$create_field_list'([field(Name,Type,Null,Key,DefaultValue)|T],FinalSQL,PrimaryKeys):- + %'$check_field_type' + '$create_field_list'(T,Result,KeyInfo), + '$field_extra_options'(Name,Null,Key,KeyInfo,DefaultValue,TempSQL1,PrimaryKeys), + '$make_atom'([' `',Name,'` ',Type,TempSQL1,' , ',Result],FinalSQL). + + +'$field_extra_options'(Name,Null,Key,KeyInfo,DefaultValue,Result,PrimaryKeys):- + ( Null == 'y' -> + '$make_atom'([' NOT NULL '],TempSQL1) + ; + TempSQL1 = '' + ), + (var(DefaultValue) -> + Result = TempSQL1 + ; + '$make_atom'([TempSQL1,' DEFAULT \'',DefaultValue,'\' '],Result) + ), + ( Key == 'y' -> + PrimaryKeys = [Name|KeyInfo] + ; + PrimaryKeys = KeyInfo + ). + % % Predicate's used to determine if the command 'WHERE' exists in the @@ -212,12 +280,10 @@ '$build_set_condition_with_comma'(FieldValues,SQLRest). -% Este predicado vai sempre falhar -'$abolish_all'(Conn):- - '$get_value'(Conn,Connection),!, - % C Predicate - p_db_preds_conn(Connection,Pred_Name,Pred_Arity), - abolish(user:Pred_Name,Pred_Arity), +% This predicate will always fail +'$abolish_all'(Con):- + c_db_preds_conn(Con,Pred_Module,Pred_Name,Pred_Arity), + abolish(Pred_Module:Pred_Name,Pred_Arity), fail. '$write_or_not'(X) :- @@ -226,20 +292,15 @@ '$write_or_not'(_). -'$make_atom'(L,A) :- - '$make_atom_list'(L,L1), - atom_codes(A,L1). - -'$make_atom_list'([],[]). -'$make_atom_list'([H|T],L2) :- - atom(H),!, - atom_codes(H,L), - '$make_atom_list'(T,L1), - append(L,L1,L2). -'$make_atom_list'([H|T],L2) :- - number_chars(H,L), - '$make_atom_list'(T,L1), - append(L,L1,L2). +'$make_atom'([],''). +'$make_atom'([Atom|T],Final) :- + atom(Atom),!, + '$make_atom'(T,Result), + atom_concat(Atom,Result,Final). +'$make_atom'([Number|T],Final) :- + '$make_atom'(T,Result), + number_atom(Number,Atom), + atom_concat(Atom,Result,Final). @@ -281,9 +342,9 @@ % Only for making the error tests in all of the calls to % get_value/2 -'$get_value'(Conn,Connection) :- - '$error_checks'(get_value(Conn,Connection)), - get_value(Conn,Connection). +'$get_value'(Connection,Con) :- + '$error_checks'(get_value(Connection,Con)), + get_value(Connection,Con). '$check_fields'([],[]). diff --git a/library/Makefile.in b/library/Makefile.in index 7f8c41ae6..c42dfb04b 100644 --- a/library/Makefile.in +++ b/library/Makefile.in @@ -50,8 +50,8 @@ PROGRAMS= $(srcdir)/apply_macros.yap \ $(srcdir)/ugraphs.yap \ $(srcdir)/ypp.yap \ $(srcdir)/MYDDAS/myddas.yap \ + $(srcdir)/MYDDAS/myddas_assert_predicates.yap \ $(srcdir)/MYDDAS/myddas_mysql.yap \ - $(srcdir)/MYDDAS/myddas_odbc.yap \ $(srcdir)/MYDDAS/myddas_errors.yap \ $(srcdir)/MYDDAS/myddas_prolog2sql.yap \ $(srcdir)/MYDDAS/myddas_util_predicates.yap \