2005-11-22 11:25:59 +00:00
|
|
|
/*************************************************************************
|
|
|
|
* *
|
|
|
|
* 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,[
|
2005-12-19 13:41:15 +00:00
|
|
|
db_open/5,
|
2006-02-10 23:42:19 +00:00
|
|
|
db_open/4,
|
2005-12-19 13:41:15 +00:00
|
|
|
db_close/1,
|
2006-02-10 23:42:19 +00:00
|
|
|
db_close/0,
|
2006-04-30 11:11:25 +01:00
|
|
|
|
2005-11-22 11:25:59 +00:00
|
|
|
db_verbose/1,
|
|
|
|
db_module/1,
|
2005-12-19 13:41:15 +00:00
|
|
|
db_is_database_predicate/3,
|
2006-05-03 19:02:29 +01:00
|
|
|
#ifdef MYDDAS_STATS
|
|
|
|
db_stats/1,
|
2005-12-19 13:41:15 +00:00
|
|
|
db_stats/2,
|
2006-05-03 19:02:29 +01:00
|
|
|
db_stats_time/2,
|
|
|
|
#endif
|
2006-02-10 23:42:19 +00:00
|
|
|
db_sql/2,
|
|
|
|
db_sql/3,
|
2005-12-19 13:41:15 +00:00
|
|
|
db_sql_select/3,
|
2006-04-30 11:11:25 +01:00
|
|
|
db_prolog_select/2,
|
2006-01-11 23:15:52 +00:00
|
|
|
db_prolog_select/3,
|
2006-01-25 20:40:21 +00:00
|
|
|
db_prolog_select_multi/3,
|
2006-01-09 13:38:02 +00:00
|
|
|
db_command/2,
|
2006-03-03 14:14:54 +00:00
|
|
|
db_assert/2,
|
|
|
|
db_assert/1,
|
2005-12-19 13:41:15 +00:00
|
|
|
db_create_table/3,
|
|
|
|
db_export_view/4,
|
2006-01-09 19:07:26 +00:00
|
|
|
db_update/2,
|
2006-01-09 13:38:02 +00:00
|
|
|
|
2006-03-03 14:14:54 +00:00
|
|
|
db_get_attributes_types/2,
|
2005-12-19 13:41:15 +00:00
|
|
|
db_get_attributes_types/3,
|
2006-03-03 14:14:54 +00:00
|
|
|
db_number_of_fields/2,
|
2005-12-19 13:41:15 +00:00
|
|
|
db_number_of_fields/3,
|
2006-01-17 22:54:02 +00:00
|
|
|
|
|
|
|
db_multi_queries_number/2,
|
2006-02-05 02:19:15 +00:00
|
|
|
|
2006-04-30 11:11:25 +01:00
|
|
|
% myddas_top_level.ypp
|
2006-03-07 17:34:29 +00:00
|
|
|
#ifdef MYDDAS_TOP_LEVEL
|
2006-02-08 17:29:55 +00:00
|
|
|
db_top_level/5,
|
|
|
|
db_datalog_select/3,
|
2006-03-07 17:34:29 +00:00
|
|
|
#endif
|
2006-04-30 11:11:25 +01:00
|
|
|
% myddas_assert_predicates.ypp
|
2006-02-10 23:42:19 +00:00
|
|
|
db_import/2,
|
2005-12-19 13:41:15 +00:00
|
|
|
db_import/3,
|
2006-02-10 23:42:19 +00:00
|
|
|
db_view/2,
|
2005-12-19 13:41:15 +00:00
|
|
|
db_view/3,
|
2006-03-03 14:14:54 +00:00
|
|
|
db_insert/2,
|
2005-12-19 13:41:15 +00:00
|
|
|
db_insert/3,
|
2006-01-17 22:54:02 +00:00
|
|
|
db_abolish/2,
|
2006-02-10 23:42:19 +00:00
|
|
|
db_listing/0,
|
2006-04-30 11:11:25 +01:00
|
|
|
db_listing/1
|
|
|
|
#ifdef MYDDAS_MYSQL
|
|
|
|
% myddas_mysql.ypp
|
|
|
|
,
|
2005-12-19 13:41:15 +00:00
|
|
|
db_my_result_set/1,
|
2006-03-03 14:14:54 +00:00
|
|
|
db_datalog_describe/1,
|
|
|
|
db_datalog_describe/2,
|
|
|
|
db_describe/3,
|
|
|
|
db_describe/2,
|
|
|
|
db_datalog_show_tables/1,
|
|
|
|
db_datalog_show_tables/0,
|
|
|
|
db_show_tables/2,
|
|
|
|
db_show_tables/1,
|
2006-02-05 02:19:15 +00:00
|
|
|
db_show_database/2,
|
|
|
|
db_show_databases/2,
|
|
|
|
db_show_databases/1,
|
|
|
|
db_change_database/2,
|
2006-04-30 11:11:25 +01:00
|
|
|
db_call_procedure/4,
|
|
|
|
db_call_procedure/3,
|
|
|
|
db_my_sql_mode/1,
|
|
|
|
db_my_sql_mode/2
|
|
|
|
#endif
|
2005-11-22 11:25:59 +00:00
|
|
|
]).
|
|
|
|
|
2006-04-30 11:11:25 +01:00
|
|
|
#ifdef MYDDAS_MYSQL
|
2006-02-05 02:19:15 +00:00
|
|
|
:- use_module(myddas_top_level,[
|
2006-02-08 17:29:55 +00:00
|
|
|
db_top_level/5,
|
|
|
|
db_datalog_select/3
|
2006-02-05 02:19:15 +00:00
|
|
|
]).
|
2006-04-30 11:11:25 +01:00
|
|
|
#endif
|
2006-02-05 02:19:15 +00:00
|
|
|
|
2005-12-19 13:41:15 +00:00
|
|
|
:- use_module(myddas_assert_predicates,[
|
2006-02-10 23:42:19 +00:00
|
|
|
db_import/2,
|
2005-12-19 13:41:15 +00:00
|
|
|
db_import/3,
|
2006-02-10 23:42:19 +00:00
|
|
|
db_view/2,
|
2005-12-19 13:41:15 +00:00
|
|
|
db_view/3,
|
2006-03-03 14:14:54 +00:00
|
|
|
db_insert/2,
|
2006-01-17 22:54:02 +00:00
|
|
|
db_insert/3,
|
2006-02-10 23:42:19 +00:00
|
|
|
db_abolish/2,
|
|
|
|
db_listing/0,
|
|
|
|
db_listing/1
|
2005-12-19 13:41:15 +00:00
|
|
|
]).
|
|
|
|
|
2006-04-30 11:11:25 +01:00
|
|
|
#ifdef MYDDAS_MYSQL
|
2005-12-19 13:41:15 +00:00
|
|
|
:- use_module(myddas_mysql,[
|
|
|
|
db_my_result_set/1,
|
2006-03-03 14:14:54 +00:00
|
|
|
db_datalog_describe/1,
|
|
|
|
db_datalog_describe/2,
|
|
|
|
db_describe/3,
|
|
|
|
db_describe/2,
|
|
|
|
db_datalog_show_tables/1,
|
|
|
|
db_datalog_show_tables/0,
|
|
|
|
db_show_tables/2,
|
|
|
|
db_show_tables/1,
|
2006-02-05 02:19:15 +00:00
|
|
|
db_show_database/2,
|
|
|
|
db_show_databases/2,
|
|
|
|
db_show_databases/1,
|
|
|
|
db_change_database/2,
|
2006-04-30 11:11:25 +01:00
|
|
|
db_call_procedure/4,
|
|
|
|
db_call_procedure/3,
|
|
|
|
db_my_sql_mode/1,
|
|
|
|
db_my_sql_mode/2
|
2005-12-19 13:41:15 +00:00
|
|
|
]).
|
2006-04-30 11:11:25 +01:00
|
|
|
#endif /* MYDDAS_MYSQL */
|
2005-12-19 13:41:15 +00:00
|
|
|
|
|
|
|
:- use_module(myddas_util_predicates,[
|
2006-01-25 20:40:21 +00:00
|
|
|
'$prolog2sql'/3,
|
|
|
|
'$create_multi_query'/3,
|
|
|
|
'$get_multi_results'/4,
|
2005-12-19 13:41:15 +00:00
|
|
|
'$process_sql_goal'/4,
|
|
|
|
'$process_fields'/3,
|
|
|
|
'$get_values_for_insert'/3,
|
|
|
|
'$make_atom'/2,
|
|
|
|
'$write_or_not'/1,
|
|
|
|
'$abolish_all'/1,
|
2006-01-09 13:38:02 +00:00
|
|
|
'$make_a_list'/2,
|
|
|
|
'$get_table_name'/2,
|
2006-01-09 19:07:26 +00:00
|
|
|
'$get_values_for_update'/4,
|
2006-01-11 23:15:52 +00:00
|
|
|
'$extract_args'/4,
|
2006-05-22 17:45:07 +01:00
|
|
|
#ifdef MYDDAS_STATS
|
|
|
|
'$make_stats_list'/2,
|
|
|
|
#endif
|
2006-01-11 23:15:52 +00:00
|
|
|
'$lenght'/2
|
2005-12-19 13:41:15 +00:00
|
|
|
]).
|
|
|
|
|
|
|
|
:- use_module(myddas_errors,[
|
|
|
|
'$error_checks'/1
|
|
|
|
]).
|
|
|
|
|
|
|
|
:- use_module(myddas_prolog2sql,[
|
2006-01-11 23:15:52 +00:00
|
|
|
translate/3,
|
|
|
|
queries_atom/2
|
2005-12-19 13:41:15 +00:00
|
|
|
]).
|
|
|
|
|
2006-01-09 13:38:02 +00:00
|
|
|
:- use_module(lists,[
|
|
|
|
append/3
|
2006-01-11 23:15:52 +00:00
|
|
|
]).
|
2006-01-09 13:38:02 +00:00
|
|
|
|
2005-12-19 13:41:15 +00:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2006-02-10 23:42:19 +00:00
|
|
|
% db_open/5
|
|
|
|
% db_open/4
|
2005-12-19 13:41:15 +00:00
|
|
|
%
|
2006-02-10 23:42:19 +00:00
|
|
|
db_open(Interface,HostDb,User,Password):-
|
|
|
|
db_open(Interface,myddas,HostDb,User,Password).
|
2006-03-17 10:41:44 +00:00
|
|
|
|
2006-04-30 11:11:25 +01:00
|
|
|
#ifdef MYDDAS_MYSQL
|
2006-03-17 10:41:44 +00:00
|
|
|
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),
|
2005-12-19 13:41:15 +00:00
|
|
|
set_value(Connection,Con).
|
2006-03-17 10:41:44 +00:00
|
|
|
db_open(mysql,Connection,Host/Db/Port,User,Password) :-
|
|
|
|
integer(Port),!,
|
|
|
|
db_open(mysql,Connection,Host/Db/Port/_,User,Password). % Var to be NULL, the default socket
|
|
|
|
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
|
2006-04-30 11:11:25 +01:00
|
|
|
#endif
|
2006-02-08 17:29:55 +00:00
|
|
|
|
2006-04-30 11:11:25 +01:00
|
|
|
#ifdef MYDDAS_ODBC
|
2006-02-05 02:19:15 +00:00
|
|
|
db_open(odbc,Connection,ODBCEntry,User,Password) :-
|
2005-12-19 13:41:15 +00:00
|
|
|
'$error_checks'(db_open(odbc,Connection,ODBCEntry,User,Password)),
|
|
|
|
c_db_odbc_connect(ODBCEntry,User,Password,Con),
|
|
|
|
set_value(Connection,Con).
|
2006-04-30 11:11:25 +01:00
|
|
|
#endif
|
2005-12-19 13:41:15 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% db_close/1
|
2006-02-10 23:42:19 +00:00
|
|
|
% db_close/0
|
2005-12-19 13:41:15 +00:00
|
|
|
%
|
2006-02-10 23:42:19 +00:00
|
|
|
db_close:-
|
|
|
|
db_close(myddas).
|
2005-12-19 13:41:15 +00:00
|
|
|
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
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2005-11-22 11:25:59 +00:00
|
|
|
|
2006-04-30 11:11:25 +01:00
|
|
|
|
2005-11-22 11:25:59 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% db_verbose/1
|
|
|
|
%
|
|
|
|
%
|
|
|
|
db_verbose(X):-
|
|
|
|
var(X),!,
|
|
|
|
get_value(db_verbose,X).
|
2006-01-25 20:40:21 +00:00
|
|
|
db_verbose(N):-!,
|
|
|
|
set_value(db_verbose,N).
|
2005-11-22 11:25:59 +00:00
|
|
|
%default value
|
2006-01-25 20:40:21 +00:00
|
|
|
:- set_value(db_verbose,0).
|
|
|
|
:- set_value(db_verbose_filename,myddas_queries).
|
2005-11-22 11:25:59 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
2005-12-19 13:41:15 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% 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).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2005-11-22 11:25:59 +00:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2006-01-10 15:37:21 +00:00
|
|
|
% db_is_database_predicate(+,+,+)
|
2005-11-22 11:25:59 +00:00
|
|
|
%
|
|
|
|
%
|
2006-03-03 14:14:54 +00:00
|
|
|
db_is_database_predicate(Module,PredName,Arity):-
|
2005-11-22 11:25:59 +00:00
|
|
|
'$error_checks'(db_is_database_predicate(PredName,Arity,Module)),
|
|
|
|
c_db_check_if_exists_pred(PredName,Arity,Module).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2006-05-03 19:02:29 +01:00
|
|
|
#ifdef MYDDAS_STATS
|
2006-01-10 15:37:21 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% db_stats(+,-)
|
2005-12-19 13:41:15 +00:00
|
|
|
%
|
|
|
|
%
|
2006-05-03 19:02:29 +01:00
|
|
|
db_stats(List):-
|
|
|
|
db_stats(myddas,List).
|
|
|
|
|
2005-12-19 13:41:15 +00:00
|
|
|
db_stats(Connection,List):-
|
2006-01-08 16:30:00 +00:00
|
|
|
'$error_checks'(db_stats(Connection,List)),
|
2006-05-03 19:02:29 +01:00
|
|
|
NumberOfStats = 10,
|
2006-05-22 17:45:07 +01:00
|
|
|
'$make_a_list'(NumberOfStats,ListX1),
|
2005-12-19 13:41:15 +00:00
|
|
|
( var(Connection) ->
|
2006-05-22 17:45:07 +01:00
|
|
|
c_db_stats(0,ListX1)
|
2005-12-19 13:41:15 +00:00
|
|
|
;
|
|
|
|
get_value(Connection,Conn),
|
2006-05-22 17:45:07 +01:00
|
|
|
c_db_stats(Conn,ListX1)
|
|
|
|
),
|
|
|
|
'$make_stats_list'(ListX1,List).
|
2006-05-03 19:02:29 +01:00
|
|
|
|
2006-05-22 17:45:07 +01:00
|
|
|
#ifdef DEBUG
|
2006-05-03 19:02:29 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% db_stats_time(+,-)
|
|
|
|
% Reference is C pointer (memory reference)
|
|
|
|
%
|
|
|
|
db_stats_time(Reference,Time):-
|
|
|
|
'$error_checks'(db_stats_time(Reference,Time)),
|
|
|
|
c_db_stats_time(Reference,Time).
|
|
|
|
|
2005-12-19 13:41:15 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2006-05-22 17:45:07 +01:00
|
|
|
#endif /* DEBUG */
|
2005-12-19 13:41:15 +00:00
|
|
|
|
2006-05-22 17:45:07 +01:00
|
|
|
#endif /* MYDDAS_STATS */
|
2005-12-19 13:41:15 +00:00
|
|
|
|
2006-02-10 23:42:19 +00:00
|
|
|
|
2005-12-19 13:41:15 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2006-02-10 23:42:19 +00:00
|
|
|
% db_sql(+,+,-)
|
2005-12-19 13:41:15 +00:00
|
|
|
%
|
|
|
|
%
|
2006-03-03 14:14:54 +00:00
|
|
|
|
|
|
|
%compatibility
|
|
|
|
db_sql_select(Connection,SQL,LA):-
|
2006-03-09 15:52:06 +00:00
|
|
|
db_sql(Connection,SQL,LA).
|
2006-03-03 14:14:54 +00:00
|
|
|
|
2006-02-10 23:42:19 +00:00
|
|
|
db_sql(SQL,LA):-
|
|
|
|
db_sql(myddas,SQL,LA).
|
|
|
|
db_sql(Connection,SQL,LA):-
|
|
|
|
'$error_checks'(db_sql(Connection,SQL,LA)),
|
2005-12-19 13:41:15 +00:00
|
|
|
get_value(Connection,Con),
|
|
|
|
c_db_connection_type(Con,ConType),
|
2006-04-30 11:11:25 +01:00
|
|
|
'$write_or_not'(SQL),
|
2005-12-19 13:41:15 +00:00
|
|
|
( ConType == mysql ->
|
2006-04-30 11:11:25 +01:00
|
|
|
db_my_result_set(Mode),
|
|
|
|
c_db_my_query(SQL,ResultSet,Con,Mode,Arity)
|
2005-12-19 13:41:15 +00:00
|
|
|
;
|
|
|
|
c_db_odbc_number_of_fields(SQL,Con,Arity)
|
|
|
|
),
|
|
|
|
'$make_a_list'(Arity,LA),
|
|
|
|
( ConType == mysql ->
|
2006-04-30 11:11:25 +01:00
|
|
|
!,c_db_my_row(ResultSet,Arity,LA)
|
2005-12-19 13:41:15 +00:00
|
|
|
;
|
|
|
|
'$make_a_list'(Arity,BindList),
|
|
|
|
c_db_odbc_query(SQL,ResultSet,Arity,BindList,Con),!,
|
|
|
|
c_db_odbc_row(ResultSet,BindList,LA)
|
|
|
|
).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2006-01-10 15:37:21 +00:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2006-01-11 23:15:52 +00:00
|
|
|
% db_prolog_select(+,+,+)
|
2006-01-10 15:37:21 +00:00
|
|
|
%
|
|
|
|
%
|
2006-04-30 11:11:25 +01:00
|
|
|
db_prolog_select(LA,DbGoal):-
|
|
|
|
db_prolog_select(myddas,LA,DbGoal).
|
2006-01-11 23:15:52 +00:00
|
|
|
db_prolog_select(Connection,LA,DbGoal):-
|
2006-01-10 15:37:21 +00:00
|
|
|
|
2006-01-11 23:15:52 +00:00
|
|
|
'$lenght'(LA,Arity),
|
2006-04-30 11:11:25 +01:00
|
|
|
Name=viewname,
|
|
|
|
functor(ViewName,Name,Arity),
|
2006-01-11 23:15:52 +00:00
|
|
|
% build arg list for viewname/Arity
|
2006-04-30 11:11:25 +01:00
|
|
|
ViewName=..[Name|LA],
|
|
|
|
|
2006-01-25 20:40:21 +00:00
|
|
|
'$prolog2sql'(ViewName,DbGoal,SQL),
|
2006-01-10 15:37:21 +00:00
|
|
|
|
|
|
|
get_value(Connection,Con),
|
|
|
|
c_db_connection_type(Con,ConType),
|
|
|
|
'$write_or_not'(SQL),
|
|
|
|
( ConType == mysql ->
|
|
|
|
db_my_result_set(Mode),
|
2006-04-30 11:11:25 +01:00
|
|
|
c_db_my_query(SQL,ResultSet,Con,Mode,_),
|
2006-01-10 15:37:21 +00:00
|
|
|
!,c_db_my_row(ResultSet,Arity,LA)
|
|
|
|
;
|
|
|
|
true
|
|
|
|
).
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
2006-01-25 20:40:21 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% db_prolog_select_multi(+,+,-)
|
|
|
|
% db_prolog_select_multi(guest,[(ramos(A,C),A=C),(ramos(D,B),B=10)],[[A],[D,B]]).
|
|
|
|
%
|
|
|
|
db_prolog_select_multi(Connection,DbGoalsList,ListOfResults) :-
|
|
|
|
'$error_checks'(db_prolog_select_multi(Connection,DbGoalsList,ListOfResults)),
|
|
|
|
'$create_multi_query'(ListOfResults,DbGoalsList,SQL),
|
|
|
|
|
|
|
|
get_value(Connection,Con),
|
|
|
|
c_db_connection_type(Con,ConType),
|
|
|
|
'$write_or_not'(SQL),
|
|
|
|
( ConType == mysql ->
|
|
|
|
db_my_result_set(Mode),
|
2006-04-30 11:11:25 +01:00
|
|
|
c_db_my_query(SQL,ResultSet,Con,Mode,_)
|
2006-01-25 20:40:21 +00:00
|
|
|
;
|
|
|
|
true
|
|
|
|
),
|
|
|
|
'$get_multi_results'(Con,ConType,ResultSet,ListOfResults).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-01-09 13:38:02 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% db_command/2
|
|
|
|
%
|
|
|
|
%
|
|
|
|
db_command(Connection,SQL):-
|
|
|
|
'$error_checks'(db_command(Connection,SQL)),
|
|
|
|
get_value(Connection,Con),
|
|
|
|
'$write_or_not'(SQL),
|
|
|
|
c_db_connection_type(Con,ConType),
|
|
|
|
( ConType == mysql ->
|
|
|
|
db_my_result_set(Mode),
|
2006-04-30 11:11:25 +01:00
|
|
|
c_db_my_query(SQL,_,Con,Mode,_)
|
2006-01-09 13:38:02 +00:00
|
|
|
;
|
|
|
|
true
|
|
|
|
).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
2005-11-22 11:25:59 +00:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2006-03-03 14:14:54 +00:00
|
|
|
% db_assert/2
|
|
|
|
% db_assert/1
|
2005-11-22 11:25:59 +00:00
|
|
|
%
|
2006-03-03 14:14:54 +00:00
|
|
|
db_assert(PredName):-
|
|
|
|
db_assert(myddas,PredName).
|
|
|
|
|
|
|
|
db_assert(Connection,PredName):-
|
2005-12-19 13:41:15 +00:00
|
|
|
translate(PredName,PredName,Code),
|
|
|
|
'$error_checks'(db_insert2(Connection,PredName,Code)),
|
|
|
|
'$get_values_for_insert'(Code,ValuesList,RelName),
|
2006-04-30 11:11:25 +01:00
|
|
|
'$make_atom'(['INSERT INTO `',RelName,'` VALUES '|ValuesList],SQL),
|
2005-12-19 13:41:15 +00:00
|
|
|
|
|
|
|
get_value(Connection,Con),
|
|
|
|
c_db_connection_type(Con,ConType),
|
|
|
|
'$write_or_not'(SQL),
|
|
|
|
( ConType == mysql ->
|
|
|
|
db_my_result_set(Mode),
|
2006-04-30 11:11:25 +01:00
|
|
|
c_db_my_query(SQL,_,Con,Mode,_)
|
2005-12-19 13:41:15 +00:00
|
|
|
;
|
|
|
|
c_db_odbc_query(SQL,_,_,_,Con)
|
|
|
|
).
|
2005-11-22 11:25:59 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
2005-12-19 13:41:15 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% 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),
|
2005-11-22 11:25:59 +00:00
|
|
|
|
2005-12-19 13:41:15 +00:00
|
|
|
'$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),
|
2006-04-30 11:11:25 +01:00
|
|
|
c_db_my_query(FinalSQL,_,Con,Mode,_)
|
2005-12-19 13:41:15 +00:00
|
|
|
;
|
|
|
|
c_db_odbc_query(FinalSQL,_,_,_,Con)
|
|
|
|
).
|
2005-11-22 11:25:59 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2005-12-19 13:41:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% db_export_view/4
|
|
|
|
% TODO Test with ODBC
|
2006-01-08 16:30:00 +00:00
|
|
|
%
|
2005-12-19 13:41:15 +00:00
|
|
|
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),
|
2006-04-30 11:11:25 +01:00
|
|
|
c_db_my_query(FinalSQL,_,Con,Mode,_)
|
2005-12-19 13:41:15 +00:00
|
|
|
;
|
|
|
|
c_db_odbc_query(FinalSQL,_,_,_,Con)
|
|
|
|
).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2006-01-09 13:38:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2006-01-09 19:07:26 +00:00
|
|
|
% db_update/2
|
2006-04-30 11:11:25 +01:00
|
|
|
% 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
|
2006-01-09 19:07:26 +00:00
|
|
|
db_update(Connection,WherePred-SetPred):-
|
2006-01-09 13:38:02 +00:00
|
|
|
%TODO: error_checks
|
2006-01-09 19:07:26 +00:00
|
|
|
get_value(Connection,Conn),
|
|
|
|
|
|
|
|
% Match and Values must be "unifiable"
|
|
|
|
functor(WherePred,PredName,Arity),
|
|
|
|
functor(SetPred,PredName,Arity),
|
|
|
|
|
2006-01-09 13:38:02 +00:00
|
|
|
functor(NewRelation,PredName,Arity),
|
2006-01-09 19:07:26 +00:00
|
|
|
|
|
|
|
'$extract_args'(WherePred,1,Arity,WhereArgs),
|
|
|
|
'$extract_args'(SetPred,1,Arity,SetArgs),
|
|
|
|
|
|
|
|
copy_term(WhereArgs,WhereArgsTemp),
|
2006-04-30 11:11:25 +01:00
|
|
|
NewRelation=..[PredName|WhereArgsTemp],
|
2006-01-09 13:38:02 +00:00
|
|
|
translate(NewRelation,NewRelation,Code),
|
2006-01-09 19:07:26 +00:00
|
|
|
|
|
|
|
'$get_values_for_update'(Code,SetArgs,SetCondition,WhereCondition),
|
|
|
|
|
2006-01-09 13:38:02 +00:00
|
|
|
'$get_table_name'(Code,TableName),
|
|
|
|
append(SetCondition,WhereCondition,Conditions),
|
2006-04-30 11:11:25 +01:00
|
|
|
'$make_atom'(['UPDATE `',TableName,'` '|Conditions],SQL),
|
2006-01-09 13:38:02 +00:00
|
|
|
'$write_or_not'(SQL),
|
2006-04-30 11:11:25 +01:00
|
|
|
db_my_result_set(Mode),
|
|
|
|
c_db_my_query(SQL,_,Conn,Mode,_).
|
2006-01-09 13:38:02 +00:00
|
|
|
|
2005-12-19 13:41:15 +00:00
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% db_get_attributes_types/3
|
2006-03-03 14:14:54 +00:00
|
|
|
% db_get_attributes_types/2
|
2005-11-22 11:25:59 +00:00
|
|
|
%
|
2006-03-03 14:14:54 +00:00
|
|
|
db_get_attributes_types(RelationName,TypesList) :-
|
|
|
|
db_get_attributes_types(myddas,RelationName,TypesList).
|
2005-12-19 13:41:15 +00:00
|
|
|
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
|
|
|
|
%
|
|
|
|
%
|
2006-03-03 14:14:54 +00:00
|
|
|
db_number_of_fields(RelationName,Arity) :-
|
|
|
|
db_number_of_fields(myddas,RelationName,Arity).
|
2005-12-19 13:41:15 +00:00
|
|
|
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)
|
|
|
|
).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2006-01-17 22:54:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% db_multi_queries_number(+,+)
|
2006-01-25 20:40:21 +00:00
|
|
|
% TODO: EVERITHING
|
2006-01-17 22:54:02 +00:00
|
|
|
%
|
|
|
|
db_multi_queries_number(Connection,Number) :-
|
|
|
|
'$error_checks'(db_multi_queries_number(Connection,Number)),
|
|
|
|
get_value(Connection,Con),
|
|
|
|
c_db_multi_queries_number(Con,Number).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
2006-01-25 20:40:21 +00:00
|
|
|
|