support for the MyDDAS interface library

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1465 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
tiagosoares
2005-11-22 11:25:59 +00:00
parent 0e6cb326da
commit 253f5e7f9a
18 changed files with 5550 additions and 8 deletions

80
library/MYDDAS/myddas.yap Normal file
View File

@@ -0,0 +1,80 @@
/*************************************************************************
* *
* 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.yap *
* Last rev: *
* mods: *
* comments: Global predicates for the MyDDAS Interface *
* *
*************************************************************************/
:- module(myddas,[
db_verbose/1,
db_is_database_predicate/3,
db_module/1,
db_stats/2
]).
:- use_module(myddas_util_predicates).
:- use_module(myddas_errors).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% db_verbose/1
%
%
db_verbose(X):-
var(X),!,
get_value(db_verbose,X).
db_verbose(1):-!,
set_value(db_verbose,1).
db_verbose(_):-
set_value(db_verbose,0).
%default value
:- db_verbose(0).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% db_is_database_predicate/3
%
%
db_is_database_predicate(PredName,Arity,Module):-
'$error_checks'(db_is_database_predicate(PredName,Arity,Module)),
c_db_check_if_exists_pred(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
%
%
db_stats(Connection,List):-
'$get_value'(Connection,Conn),
NumberOfStats = 2,
'$make_a_list'(NumberOfStats,List),
c_db_my_stats(Conn,List).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

View File

@@ -0,0 +1,124 @@
/*************************************************************************
* *
* 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_errors.yap *
* Last rev: *
* mods: *
* comments: MYDDAS errors checks and errors Messages *
* *
*************************************************************************/
:- module(myddas_errors,[
'$error_checks'/1,
'$db_my_error'/2
]).
:- use_module(myddas_util_predicates).
% must have only one relation
'$error_checks'(db_my_insert3(_,_,_)):-!.
'$error_checks'(db_my_insert2(_,Conn,[query(Att,[rel(Relation,_)],_)])) :- !,
% Number of fields of the Relation, must be
% equal to the number of attributes
c_db_my_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_my_get_fields_properties(Relation,Conn,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)) :- !,
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),
nonvar(PredName).
'$error_checks'(db_my_view(PredName,DbGoal,_)) :- !,
nonvar(DbGoal),
nonvar(PredName).
'$error_checks'(db_my_number_of_fields(RelationName,_,_)) :- !,
nonvar(RelationName).
'$error_checks'(db_my_get_attributes_types(RelationName,_,_)) :- !,
nonvar(RelationName).
'$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)) :- !,
% This also prevents the case of giving the number of the connection
% as an argument
atom(Conn),
var(Connection),
get_value(Conn,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
% as an argument
atom(Conn),
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.

350
library/MYDDAS/myddas_mysql.yap Executable file
View File

@@ -0,0 +1,350 @@
/*************************************************************************
* *
* 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_mysql.yap *
* Last rev: *
* mods: *
* comments: MySQL Server communication library *
* *
*************************************************************************/
:- 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).
:- use_module(myddas_errors).
:- use_module(myddas_prolog2sql,[translate/3,queries_atom/2]).
:- use_module(myddas_util_predicates).
:- use_module(lists,[append/3]).
%--------------------------------------------------------
% Public Predicates
%--------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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_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_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_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_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/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_result_set/1
%
%
db_my_result_set(X):-
var(X),!,
get_value(db_my_result_set,X).
db_my_result_set(use_result):-
set_value(db_my_result_set,use_result).
db_my_result_set(store_result):-
set_value(db_my_result_set,store_result).
%default value
:- db_my_result_set(store_result).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% db_my_describe/2
%
%
db_my_describe(Relation,Connection) :-
'$error_checks'(db_my_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_table_write(ResultSet).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% db_my_describe/3
% gives the results of the DESCRIBE statement
% by backtracking
db_my_describe(Relation,Connection,tableinfo(A1,A2,A3,A4,A5,A6)) :-
'$error_checks'(db_my_describe(Relation,Connection)),
'$get_value'(Connection,Conn),
'$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_row(ResultSet,6,[A1,A2,A3,A4,A5,A6]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% db_my_show_tables/1
%
%
db_my_show_tables(Connection) :-
'$error_checks'(db_my_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_table_write(ResultSet).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% db_my_show_tables/2
% gives the results of the SHOW TABLES statement
% by backtracking
db_my_show_tables(Connection,table(Table)) :-
'$error_checks'(db_my_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_row(ResultSet,1,[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_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_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).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%--------------------------------------------------------
% 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).

260
library/MYDDAS/myddas_odbc.yap Executable file
View File

@@ -0,0 +1,260 @@
/*************************************************************************
* *
* 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).

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,317 @@
:- module(myddas_test_predicates,[
% Tests or Debug Predicates
%db_my_delete/2,
db_my_insert_test/2,
db_my_update/3,
db_my_import_query_normal/3,
db_view/3, % DEBUG ONLY
db_my_ilpview/4
]).
:- use_module(myddas).
:- use_module(myddas_mysql).
:- use_module(myddas_util_predicates).
:- use_module(myddas_prolog2sql,[translate/3,queries_atom/2]).
:- use_module(myddas_errors).
:- use_module(lists).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% db_my_insert/2
%
%
db_my_insert_test(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),
c_db_my_query_no_result(SQL,Conn).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% db_my_update/3
% UpdaList = [X,1,Y,2,T,0]
%
db_my_update(UpdateList,Relation,Connection):-
'$get_value'(Connection,Conn),
%TODO: error_checks
functor(Relation,PredName,Arity),
functor(NewRelation,PredName,Arity),
'$extract_args'(Relation,1,Arity,ArgsList1),
copy_term(ArgsList1,ArgsList2),
'$make_list_of_args'(1,Arity,NewRelation,ArgsList2),
translate(NewRelation,NewRelation,Code),
'$get_table_name'(Code,TableName),
'$get_values_for_update'(Code,SetCondition,ArgsList1,UpdateList,WhereCondition),
append(SetCondition,WhereCondition,Conditions),
'$make_atom'(['UPDATE ',TableName,' '|Conditions],SQL),
'$write_or_not'(SQL),
c_db_my_query_no_result(SQL,Conn).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% db_my_delete/2
%
%
% db_my_delete(PredName,Connection):-
% '$get_value'(Connection,Conn),
% translate(PredName,PredName,Code),
% %'$error_checks'(db_my_delete(PredName,Conn,Code)),
% queries_atom(Code,SQL),
% c_db_my_query('SELECT 46 , "ola" , "Adeus" FROM estrada A WHERE A.id_estrada = 46 , "ola" , "Adeus" FROM estrada A WHERE A.id_estrada = 46 AND A.nome = "ola" AND A.nome_alternativo = "Adeus"',_,Conn).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% db_my_import_michel/3
%
%
db_my_import_query_normal(RelationName,PredName,Connection) :-
'$error_checks'(db_my_import(RelationName,PredName,Connection)),
% get connection id based on given atom
'$get_value'(Connection,Conn),
% assert information needed for translate/3 and PredName clause
'$assert_relation_info_and_pred_clause_michel_query'(RelationName,PredName,Conn).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- '$assert_relation_info_and_pred_clause'(RelationName,PredName,Connection)
% Asserts information needed for translate/3 and the clause of the
% PredName predicate
% ---
'$assert_relation_info_and_pred_clause_michel_query'(RelationName,PredName,Connection) :-
% get relation arity
% C Predicate
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_facts'(myddas_prolog2sql,R),
%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),
% build PredName clause
Assert =..[':-',P,','(myddas_test_predicates:'$generate_optimized_SQL'(P,SQL,LA,ArityProj),
','(myddas_test_predicates:db_my_result_set(Mode),
','(myddas_test_predicates:'$write_or_not'(SQL),
','(myddas_test_predicates:c_db_my_query(SQL,ResultSet,Connection,Mode),
','(!,myddas_test_predicates:c_db_my_row_unify(ResultSet,ArityProj,LA))))))],
% we are assuming that all the predicates will be inserted in
% the user module
assert(Module:Assert),
% ALTERACAO
% Adds PredName and Arity to this Connection List
% C Predicate
c_db_add_preds(PredName,Arity,Module,Connection).
% Beginning of new predicates for optimized translate
'$generate_optimized_SQL'(Pred,SQL,DbRowList,Arity) :-
Pred =..[F|Args],
functor(Pred,_,A),
myddas_prolog2sql:relation(F,A,Relation),
'$divide_args_in_proj_and_where'(1,Relation,Args,[],Proj,Where),
'$generate_SQL'(Relation,Proj,Where,SQL),
'$generate_dbrow_list'(Proj,DbRowList,Arity).
'$divide_args_in_proj_and_where'(_, _, [], _, [], []) :- !.
'$divide_args_in_proj_and_where'(I, Relation, [H|T], Dict, Proj, [v(Att,Att1)|Where]) :-
var(H),
'$member_var'(H,Dict,Att),!,
myddas_prolog2sql:attribute(I,Relation,Att1,_),
I1 is I+1,
'$divide_args_in_proj_and_where'(I1, Relation, T, Dict, Proj, Where).
'$divide_args_in_proj_and_where'(I, Relation, [H|T], Dict, [(Att,H)|Proj], Where) :-
var(H),!,
myddas_prolog2sql:attribute(I,Relation,Att,_),
I1 is I+1,
'$divide_args_in_proj_and_where'( I1, Relation, T, [(H,Att)|Dict], Proj, Where).
'$divide_args_in_proj_and_where'(I, Relation, [H|T], Dict, Proj, [g(Att,H)|Where]) :-
myddas_prolog2sql:attribute(I,Relation,Att,_),
I1 is I+1,
'$divide_args_in_proj_and_where'(I1,Relation,T,Dict,Proj,Where).
'$generate_SQL'(Relation,_,[],SQL) :-
!,
atom_concat('SELECT * FROM ',Relation, SQL).
'$generate_SQL'(Relation,Proj,Where,SQL) :-
'$make_proj_atom'(Proj,Proj_Atom),
atom_concat('SELECT ',Proj_Atom,R),
atom_concat(R, ' FROM ',R1),
atom_concat(R1, Relation, R2),
atom_concat(R2, ' WHERE ', R3),
'$make_where_atom'(Where,Where_Atom),
atom_concat(R3,Where_Atom,SQL).
'$generate_dbrow_list'([],[_],0) :- !. % important for empty projection terms.
'$generate_dbrow_list'([(_,V)],[V],1) :- !.
'$generate_dbrow_list'([(_,V)|T],[V|R],I1) :-
'$generate_dbrow_list'(T,R,I), I1 is I+1.
'$make_proj_atom'([],'1') :- !. % important for empty projection terms.
'$make_proj_atom'([(A,_)],A) :- !.
'$make_proj_atom'([(A,_)|T],Atom) :-
'$make_proj_atom'(T,Atom1),
atom_concat(A,',',Atom2),
atom_concat(Atom2,Atom1,Atom).
'$make_where_atom'([v(Att,H)],Atom) :-
!,
atom_concat(Att,' = ',R),
atom_concat(R,H,Atom).
'$make_where_atom'([v(Att,H)|T],Atom) :-
'$make_where_atom'(T,Atom1),
atom_concat(Att,' = ',R),
atom_concat(R,H,Atom2),
atom_concat(Atom2, ' AND ', Atom3),
atom_concat(Atom3,Atom1,Atom).
'$make_where_atom'([g(Att,H)],Atom) :-
number(H),
!,
number_atom(H,H1),
atom_concat(Att,' = \'',R),
atom_concat(R,H1,R1),
atom_concat(R1,'\'',Atom).
'$make_where_atom'([g(Att,H)],Atom) :-
!,
atom_concat(Att,' = \'',R),
atom_concat(R,H,R1),
atom_concat(R1,'\'',Atom).
'$make_where_atom'([g(Att,H)|T],Atom) :-
number(H),
!,
number_atom(H,H1),
'$make_where_atom'(T,Atom1),
atom_concat(Att,' = \'',R),
atom_concat(R,H1,R1),
atom_concat(R1,'\'',Atom2),
atom_concat(Atom2, ' AND ', Atom3),
atom_concat(Atom3,Atom1,Atom).
'$make_where_atom'([g(Att,H)|T],Atom) :-
'$make_where_atom'(T,Atom1),
atom_concat(Att,' = \'',R),
atom_concat(R,H,R1),
atom_concat(R1,'\'',Atom2),
atom_concat(Atom2, ' AND ', Atom3),
atom_concat(Atom3,Atom1,Atom).
'$member_var'(H,[(V,Att)|_],Att) :-
H == V,!.
'$member_var'(H,[_|T],Att) :-
'$member_var'(H,T,Att).
% End of predicates for optimized translate
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% db_my_ilpview/4
%
%
db_my_ilpview(LA,ViewName,DbGoal,Connection):-
'$get_value'(Connection,Conn),
functor(ViewName,PredName,Arity),
functor(NewName,PredName,Arity),
translate(ViewName,DbGoal,Code),
queries_atom(Code,SQL),
% build arg list for db_my_row/2
'$make_list_of_args'(1,Arity,NewName,LA),
% checks if the WHERE commend of SQL exists in the string
'$where_exists'(SQL,Flag),
'$build_query'(Flag,SQL,Code,LA,FinalSQL),
db_my_result_set(Mode),
'$write_or_not'(FinalSQL),
c_db_my_query(FinalSQL,ResultSet,Conn,Mode),
!,c_db_my_row(ResultSet,Arity,LA).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% db_view/3
%
%
db_view(PredName,DbGoal,Connection) :-
'$error_checks'(db_my_view(PredName,DbGoal,Connection)),
'$get_value'(Connection,Conn),
'$assert_view_clause2'(PredName,DbGoal,Conn).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'$assert_view_clause2'(ViewName,DbGoal,Connection) :-
% here we can add some error control, like checking DBgoals include
% only DB relations
% get arity of projection term
% PredName necessary for c_db_add_preds
functor(ViewName,PredName,Arity),
functor(NewName,PredName,Arity),
db_module(Module),
not c_db_my_check_if_exists_pred(PredName,Arity,Module),
%'$copy_term_nv'(NewName,[],G,_),
%translate(ViewName,DbGoal,Code),
%queries_atom(Code,SQL),
% build arg list for db_my_row/2
'$make_list_of_args'(1,Arity,NewName,LA),
% checks if the WHERE commend of SQL exists in the string
%'$where_exists'(SQL,Flag),
% build view clause
Assert =..[':-',NewName,
','(myddas_test_predicates:translate(ViewName,DbGoal,Code),
','(myddas_test_predicates:queries_atom(Code,FinalSQL),
','(myddas_test_predicates:db_my_result_set(Mode),
','(myddas_test_predicates:c_db_my_query(FinalSQL,ResultSet,Connection,Mode),
','(myddas_test_predicates:'$write_or_not'(FinalSQL),
','(!,myddas_test_predicates:c_db_my_row(ResultSet,Arity,LA)))))))],
assert(Module:Assert),
% ALTERACAO
% Adds PredName and Arity to this Connection List
% C Predicate
c_db_add_preds(PredName,Arity,Module,Connection).

View File

@@ -0,0 +1,308 @@
/*************************************************************************
* *
* 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_util_predicates.yap *
* Last rev: *
* mods: *
* comments: Auxiliary for the MyDDAS MySQL and ODBC library *
* *
*************************************************************************/
:- module(myddas_util_predicates,[
'$check_fields'/2,
'$get_value'/2,
'$get_values_for_insert'/3,
'$make_atom'/2,
'$write_or_not'/1,
'$abolish_all'/1,
'$get_values_for_update'/5,
'$get_table_name'/2,
'$extract_args'/4,
'$copy_term_nv'/4,
'$assert_attribute_information'/4,
'$make_a_list'/2,
'$make_list_of_args'/4,
'$where_exists'/2,
'$build_query'/5,
'$assert_facts'/2
]).
:- use_module(myddas).
:- use_module(myddas_errors).
:- use_module(lists,[append/3]).
%
% Predicate's used to determine if the command 'WHERE' exists in the
% query
%
'$where_exists'(SQL,1):-
atom_codes(SQL,ListSQL),
% Code for ' WHERE ', the spaces garantee that is the WHERE
% command, and not a value of a field
'$where_exists_aux'(ListSQL,[32,87,72,69,82,69,32]),!.
'$where_exists'(_,0).
'$where_exists_aux'([W|TCodes],[W|TWhere]):-
'$where_found'(TCodes,TWhere),!.
'$where_exists_aux'([_|TCodes],Where):-
'$where_exists_aux'(TCodes,Where).
'$where_found'(_,[]).
'$where_found'([Letter|TCodes],[Letter|TWhere]):-
'$where_found'(TCodes,TWhere).
%
% Predicates used to build the new string SQL
%
'$build_query'(0,SQL,[query(CodeArgs,_,_)],LA,FinalSQL):-
'$build_query_aux'(0,SQL,CodeArgs,LA,FinalSQL).
'$build_query'(1,SQL,[query(CodeArgs,_,_)],LA,FinalSQL):-
'$build_query_aux'(1,SQL,CodeArgs,LA,FinalSQL).
%Flag it necessary for knowing if it is the first argument
%added to where, and if so we do not add 'and'
'$build_query_aux'(_,SQL,[],[],SQL).
'$build_query_aux'(Flag,SQL,[CodeArg|CodeT],[LArg|LT],FinalSQL):-
nonvar(LArg),!,
'$concatSQL'(Flag,SQL,CodeArg,LArg,ConcatSQL),
'$build_query_aux'(1,ConcatSQL,CodeT,LT,FinalSQL).
'$build_query_aux'(Flag,SQL,[_|CodeT],[_|LT],FinalSQL):-
'$build_query_aux'(Flag,SQL,CodeT,LT,FinalSQL).
%This Predicate will concat the SQL query generated to the
% moment with the field and it's value
'$concatSQL'(Flag,SQL,att(Rel,Field),Value,ConcatSQL) :-
number(Value),!,
number_atom(Value,Number),
'$and_or_where'(Flag,SQL,Temp0),
atom_concat(Temp0,Rel,Temp1),
atom_concat(Temp1,'.',Temp2),
atom_concat(Temp2,Field,Temp3),
atom_concat(Temp3,'=',Temp4),
atom_concat(Temp4,Number,Temp5),
atom_concat(Temp5,' ',ConcatSQL).
'$concatSQL'(Flag,SQL,att(Rel,Field),Value,ConcatSQL) :-
'$and_or_where'(Flag,SQL,Temp0),
atom_concat(Temp0,Rel,Temp1),
atom_concat(Temp1,'.',Temp2),
atom_concat(Temp2,Field,Temp3),
atom_concat(Temp3,'=',Temp4),
atom_concat(Temp4,'"',Temp5), %"
atom_concat(Temp5,Value,Temp6),
atom_concat(Temp6,'" ',ConcatSQL). %"
% This predicate will determin if we should use AND or WHERE
'$and_or_where'(1,SQL,ConcatSQL):-
atom_concat(SQL,'AND ',ConcatSQL).
'$and_or_where'(0,SQL,ConcatSQL):-
atom_concat(SQL,' WHERE ',ConcatSQL).
%
% 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,
'$make_a_list'(N1,T).
'$assert_attribute_information'(N,N,_,_) :- !.
'$assert_attribute_information'(N,M,Relation,[FieldName,HeadType|TailTypes]) :-
functor(Attrib,attribute,4),
N1 is N+1,
arg(1,Attrib,N1),
arg(2,Attrib,Relation),
arg(3,Attrib,FieldName),
arg(4,Attrib,HeadType),
'$assert_facts'(myddas_prolog2sql,Attrib),
%assert(myddas_prolog2sql:Attrib),
'$assert_attribute_information'(N1,M,Relation,TailTypes).
'$copy_term_nv'(T,Dic,NT,[(T,NT)|Dic]) :-
var(T),!,
'$v_member'(T,Dic,(T,NT)).
'$copy_term_nv'(T,Dic,T,Dic) :-
functor(T,_,0),!.
'$copy_term_nv'(T,Dic,NT,NDic) :-
functor(T,F,N),
functor(NT,F,N),
'$iterate_on_args'(N,T,NT,Dic,NDic).
'$iterate_on_args'(0,_,_,Dic,Dic) :- !.
'$iterate_on_args'(N,T,NT,Dic,NDic2) :-
arg(N,T,A),
'$copy_term_nv'(A,Dic,NA,NDic),
arg(N,NT,NA),
N1 is N-1,
'$iterate_on_args'(N1,T,NT,NDic,NDic2).
'$v_member'(T,[],(T,_)).
'$v_member'(T,[(V,V1)|_],(T,V1)) :-
T == V, !.
'$v_member'(T,[_|R],V) :-
'$v_member'(T,R,V).
% '$extract_args(+Predicate,+FirstArg,+Arity,-ArgList).
% extracts args from predicate, to a list
'$extract_args'(Predicate,Arity,Arity,[Arg]):-
arg(Arity,Predicate,Arg).
'$extract_args'(Predicate,ArgNumber,Arity,[Arg|ArgList]):-
arg(ArgNumber,Predicate,Arg),
NextArg is ArgNumber+1,
'$extract_args'(Predicate,NextArg,Arity,ArgList).
% '$get_table_name'(+SQLQueryTerm,?TableName).
% Gets the Table name from the SQLQueryTerm of translate/3
'$get_table_name'([query(_,[rel(TableName,_)],_)],TableName).
% '$get_values_for_update'(+SQLQueryTerm,-SetFields,+ArgList,+Updatelist,-WhereCondition)
% It will unify with the first clause
% only on the first call of the predicate
'$get_values_for_update'([query(Fields,_,Comp)],[' SET '|SQLSet],ArgList,UpdateList,[' WHERE '|Where]):-!,
'$get_values_for_set'(Fields,ArgList,UpdateList,Set),
'$build_set_condition'(Set,SQLSet),
'$get_values_for_where'(Comp,Where).
'$get_values_for_where'([comp(att(_,Field),'=','$const$'(Atom))],[' ',Field,' = "',Atom,'" ']).
'$get_values_for_where'([comp(att(_,Field),'=','$const$'(Atom))|Comp],[' ',Field,' = "',Atom,'" '|Rest]):-
'$get_values_for_where'(Comp,Rest).
'$get_values_for_set'([],[],_,[]).
'$get_values_for_set'([att(_,Field)|FieldList],[Var|ArgList],UpdateList,[Field,Value|ValueList]):-!,
'$lookup_variable_value'(Var,UpdateList,Value),
'$get_values_for_set'(FieldList,ArgList,UpdateList,ValueList).
'$get_values_for_set'([_|FieldList],[_|ArgList],UpdateList,ValueList):-
'$get_values_for_set'(FieldList,ArgList,UpdateList,ValueList).
'$lookup_variable_value'(Var,[TestVar,Value|_],Value):-
Var==TestVar,!.
'$lookup_variable_value'(Var,[_,_|List],Value):-
'$lookup_variable_value'(Var,List,Value).
'$build_set_condition'([Field,Value|FieldValues],[SQLFirst|SQLRest]):-
'$make_atom'([' ',Field,' = "',Value,'" '],SQLFirst),
'$build_set_condition_with_comma'(FieldValues,SQLRest).
'$build_set_condition_with_comma'([],[]).
'$build_set_condition_with_comma'([Field,Value|FieldValues],[SQL|SQLRest]):-
'$make_atom'([' , ',Field,' = "',Value,'" '],SQL),
'$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),
fail.
'$write_or_not'(X) :-
get_value(db_verbose,1),!,
write(X),nl.
'$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).
% for db_my_insert/3
% integer,real, string, came from the myddas_mysql.c get_attributes_types function
'$get_values_for_insert'([_,_],[Value],['NULL',')']):-var(Value),!.
'$get_values_for_insert'([_,integer],[Value],[Value,')']):-!.
'$get_values_for_insert'([_,real],[Value],[Value,')']):-!.
'$get_values_for_insert'([_,string],[Value],['"',Value,'")']):-!.
'$get_values_for_insert'([_,_|TTypesList],[Value|TValues],['NULL',','|RestValues]):-
var(Value),!,
'$get_values_for_insert'(TTypesList,TValues,RestValues).
'$get_values_for_insert'([_,integer|TTypesList],[Value|TValues],[Value,','|RestValues]):-!,
'$get_values_for_insert'(TTypesList,TValues,RestValues).
'$get_values_for_insert'([_,real|TTypesList],[Value|TValues],[Value,','|RestValues]):-!,
'$get_values_for_insert'(TTypesList,TValues,RestValues).
'$get_values_for_insert'([_,string|TTypesList],[Value|TValues],['"',Value,'",'|RestValues]):-!,
'$get_values_for_insert'(TTypesList,TValues,RestValues).
% for db_my_insert/2
'$get_values_for_insert'([query(Att,[rel(Relation,_)],_)],['('|ValuesList],Relation):-
'$get_values_for_insert_make_list'(Att,ValuesList).
'$get_values_for_insert_make_list'([att(_,_)],['NULL',')']):-!.
'$get_values_for_insert_make_list'(['$const$'(Value)],[Value,')']):-
number(Value),!.
'$get_values_for_insert_make_list'(['$const$'(Value)],['"',Value,'")']):-!.
'$get_values_for_insert_make_list'([att(_,_)|TAtt],['NULL',','|TList]):-!,
'$get_values_for_insert_make_list'(TAtt,TList).
'$get_values_for_insert_make_list'(['$const$'(Value)|TAtt],[Value,','|TList]):-
number(Value),!,
'$get_values_for_insert_make_list'(TAtt,TList).
'$get_values_for_insert_make_list'(['$const$'(Value)|TAtt],['"',Value,'"',','|TList]):-
'$get_values_for_insert_make_list'(TAtt,TList).
% 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).
'$check_fields'([],[]).
'$check_fields'(['$const$'(_)|TAtt],[_|TFields]):-
'$check_fields'(TAtt,TFields).
% um campo auto_incrementavel, <20> sempre parte da chave, e como <20> auto
% pode-se dar valores NULOS
'$check_fields'([att(_,Name)|TAtt],[property(Name,_,1,1)|TFields]):-!,
'$check_fields'(TAtt,TFields).
'$check_fields'([att(_,Name)|TAtt],[property(Name,0,_,_)|TFields]):-!,
'$check_fields'(TAtt,TFields).
%
% This predicate asserts facts in a Module, but if that
% fact already exists, it dosen't assert it
%
'$assert_facts'(Module,Fact):-
Module:Fact,!.
'$assert_facts'(Module,Fact):-
assert(Module:Fact).

View File

@@ -48,7 +48,14 @@ PROGRAMS= $(srcdir)/apply_macros.yap \
$(srcdir)/timeout.yap \
$(srcdir)/trees.yap \
$(srcdir)/ugraphs.yap \
$(srcdir)/ypp.yap
$(srcdir)/ypp.yap \
$(srcdir)/MYDDAS/myddas.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 \
$(srcdir)/MYDDAS/myddas_test_predicates.yap
LOGTALK_PROGRAMS= \
$(srcdir)/logtalk/logtalk.pl \