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_mysql.yap *
|
|
|
|
* Last rev: *
|
|
|
|
* mods: *
|
2005-12-19 13:41:15 +00:00
|
|
|
* comments: MySQL Predicates *
|
2005-11-22 11:25:59 +00:00
|
|
|
* *
|
|
|
|
*************************************************************************/
|
2006-04-30 11:11:25 +01:00
|
|
|
#ifdef MYDDAS_MYSQL
|
2005-11-22 11:25:59 +00:00
|
|
|
|
|
|
|
:- 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,
|
2007-05-20 22:33:29 +01:00
|
|
|
db_my_sql_mode/2,
|
|
|
|
db_sql_mode/1,
|
|
|
|
db_sql_mode/2
|
2005-12-19 13:41:15 +00:00
|
|
|
]).
|
2005-11-22 11:25:59 +00:00
|
|
|
|
2006-04-30 11:11:25 +01:00
|
|
|
:- use_module(myddas,[
|
|
|
|
db_sql/3
|
|
|
|
]).
|
2005-11-22 11:25:59 +00:00
|
|
|
|
2005-12-19 13:41:15 +00:00
|
|
|
:- use_module(myddas_errors,[
|
|
|
|
'$error_checks'/1
|
|
|
|
]).
|
2005-11-22 11:25:59 +00:00
|
|
|
|
2005-12-19 13:41:15 +00:00
|
|
|
:- use_module(myddas_util_predicates,[
|
|
|
|
'$get_value'/2,
|
|
|
|
'$make_atom'/2,
|
2006-04-30 11:11:25 +01:00
|
|
|
'$make_atom_args'/2,
|
|
|
|
'$make_a_list'/2,
|
2005-12-19 13:41:15 +00:00
|
|
|
'$write_or_not'/1
|
|
|
|
]).
|
2005-11-22 11:25:59 +00:00
|
|
|
|
|
|
|
|
2016-07-31 16:02:06 +01:00
|
|
|
:- load_foreign_files( ['Yapmysql'], [], init_mysql).
|
|
|
|
|
|
|
|
|
2005-11-22 11:25:59 +00:00
|
|
|
%--------------------------------------------------------
|
|
|
|
% Public Predicates
|
|
|
|
%--------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% 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
|
|
|
|
%
|
|
|
|
%
|
2006-03-03 14:14:54 +00:00
|
|
|
db_datalog_describe(Relation):-
|
|
|
|
db_datalog_describe(myddas,Relation).
|
|
|
|
db_datalog_describe(Connection,Relation) :-
|
2006-04-30 11:11:25 +01:00
|
|
|
'$error_checks'(db_datalog_describe(Relation,Connection)),
|
2005-11-22 11:25:59 +00:00
|
|
|
'$get_value'(Connection,Conn),
|
|
|
|
'$make_atom'(['DESCRIBE ',Relation],SQL),
|
|
|
|
db_my_result_set(Mode),
|
2006-04-30 11:11:25 +01:00
|
|
|
c_db_my_query(SQL,ResultSet,Conn,Mode,_),
|
2005-11-22 11:25:59 +00:00
|
|
|
c_db_my_table_write(ResultSet).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2006-03-03 14:14:54 +00:00
|
|
|
% db_describe/3
|
|
|
|
% db_describe/2
|
2005-11-22 11:25:59 +00:00
|
|
|
% gives the results of the DESCRIBE statement
|
|
|
|
% by backtracking
|
2006-03-03 14:14:54 +00:00
|
|
|
db_describe(Relation,TableInfo) :-
|
|
|
|
db_describe(myddas,Relation,TableInfo).
|
|
|
|
db_describe(Connection,Relation,tableinfo(A1,A2,A3,A4,A5,A6)) :-
|
2006-05-22 17:45:07 +01:00
|
|
|
'$error_checks'(db_describe(Relation,Connection,_)),
|
2005-11-22 11:25:59 +00:00
|
|
|
'$get_value'(Connection,Conn),
|
|
|
|
'$make_atom'(['DESCRIBE ',Relation],SQL),
|
|
|
|
db_my_result_set(Mode),
|
|
|
|
'$write_or_not'(SQL),
|
2006-04-30 11:11:25 +01:00
|
|
|
c_db_my_query(SQL,ResultSet,Conn,Mode,_),
|
2005-11-22 11:25:59 +00:00
|
|
|
!,c_db_my_row(ResultSet,6,[A1,A2,A3,A4,A5,A6]).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2006-03-03 14:14:54 +00:00
|
|
|
% db_datalog_show_tables/1
|
2005-11-22 11:25:59 +00:00
|
|
|
%
|
|
|
|
%
|
2006-03-03 14:14:54 +00:00
|
|
|
db_datalog_show_tables:-
|
|
|
|
db_datalog_show_tables(myddas).
|
|
|
|
db_datalog_show_tables(Connection) :-
|
2005-11-22 11:25:59 +00:00
|
|
|
'$error_checks'(db_my_show_tables(Connection)),
|
|
|
|
'$get_value'(Connection,Conn),
|
|
|
|
db_my_result_set(Mode),
|
|
|
|
'$write_or_not'('SHOW TABLES'),
|
2006-04-30 11:11:25 +01:00
|
|
|
c_db_my_query('SHOW TABLES',ResultSet,Conn,Mode,_),
|
2005-11-22 11:25:59 +00:00
|
|
|
c_db_my_table_write(ResultSet).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2006-03-03 14:14:54 +00:00
|
|
|
% db_show_tables/2
|
|
|
|
% db_show_tables/1
|
2005-11-22 11:25:59 +00:00
|
|
|
% gives the results of the SHOW TABLES statement
|
|
|
|
% by backtracking
|
2006-03-03 14:14:54 +00:00
|
|
|
db_show_tables(Table) :-
|
|
|
|
db_show_tables(myddas,Table).
|
|
|
|
db_show_tables(Connection,table(Table)) :-
|
2005-11-22 11:25:59 +00:00
|
|
|
'$error_checks'(db_my_show_tables(Connection)),
|
|
|
|
'$get_value'(Connection,Conn),
|
|
|
|
db_my_result_set(Mode),
|
|
|
|
'$write_or_not'('SHOW TABLES'),
|
2006-04-30 11:11:25 +01:00
|
|
|
c_db_my_query('SHOW TABLES',ResultSet,Conn,Mode,_),
|
2005-11-22 11:25:59 +00:00
|
|
|
!,c_db_my_row(ResultSet,1,[Table]).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
2006-02-05 02:19:15 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% db_show_database/2
|
|
|
|
%
|
|
|
|
%
|
|
|
|
db_show_database(Connection,Database) :-
|
|
|
|
'$error_checks'(db_show_database(Connection,Database)),
|
|
|
|
'$get_value'(Connection,Con),
|
|
|
|
c_db_my_get_database(Con,Database).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% db_show_databases/2
|
|
|
|
%
|
|
|
|
%
|
|
|
|
db_show_databases(Connection,database(Databases)) :-
|
|
|
|
%'$error_checks'(db_show_databases(Connection,Database)),
|
|
|
|
'$get_value'(Connection,Conn),
|
|
|
|
db_my_result_set(Mode),
|
|
|
|
'$write_or_not'('SHOW DATABASES'),
|
2006-04-30 11:11:25 +01:00
|
|
|
c_db_my_query('SHOW DATABASES',ResultSet,Conn,Mode,_),
|
2006-02-05 02:19:15 +00:00
|
|
|
!,c_db_my_row(ResultSet,1,[Databases]).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% db_show_databases/1
|
|
|
|
% TODO Error Checks
|
|
|
|
%
|
|
|
|
db_show_databases(Connection) :-
|
2006-04-30 11:11:25 +01:00
|
|
|
'$error_checks'(db_my_show_databases(Connection)),
|
2006-02-05 02:19:15 +00:00
|
|
|
'$get_value'(Connection,Conn),
|
|
|
|
db_my_result_set(Mode),
|
|
|
|
'$write_or_not'('SHOW DATABASES'),
|
2006-04-30 11:11:25 +01:00
|
|
|
c_db_my_query('SHOW DATABASES',ResultSet,Conn,Mode,_),
|
2006-02-05 02:19:15 +00:00
|
|
|
c_db_my_table_write(ResultSet).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% db_change_database/2
|
|
|
|
%
|
|
|
|
%
|
|
|
|
db_change_database(Connection,Database) :-
|
|
|
|
'$error_checks'(db_change_database(Connection,Database)),
|
|
|
|
'$get_value'(Connection,Con),
|
|
|
|
'$make_atom'(['USE ',Database],SQL),
|
|
|
|
'$write_or_not'(SQL),
|
|
|
|
c_db_my_change_database(Con,Database).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
2005-11-22 11:25:59 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
2006-04-30 11:11:25 +01:00
|
|
|
% db_call_procedure/4
|
|
|
|
% db_call_procedure/3
|
|
|
|
% db_call_procedure(+,+,+,?)
|
|
|
|
% Only support in MySQL 5.0 an above
|
|
|
|
% Called procedure must return results via MySQL result set
|
|
|
|
db_call_procedure(Procedure,Args,Result) :-
|
|
|
|
db_call_procedure(myddas,Procedure,Args,Result).
|
|
|
|
db_call_procedure(Connection,Procedure,Args,LA) :-
|
2006-06-09 19:32:01 +01:00
|
|
|
'$error_checks'(db_call_procedure(Connection,Procedure,Args,LA)),
|
2006-04-30 11:11:25 +01:00
|
|
|
'$make_atom_args'(Args,ArgsSQL),
|
|
|
|
'$make_atom'(['CALL ',Procedure,'(',ArgsSQL,')'],SQL),
|
|
|
|
db_sql(Connection,SQL,LA).
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
% db_my_sql_mode/1
|
|
|
|
% db_my_sql_mode/2
|
|
|
|
% Possible values : traditional,ansi,strict_trans_tables or '' (empty)
|
2007-05-20 22:33:29 +01:00
|
|
|
db_sql_mode(SQLMode):-
|
|
|
|
db_my_sql_mode(SQLMode).
|
|
|
|
db_sql_mode(Connection,SQLMode):-
|
|
|
|
db_my_sql_mode(Connection,SQLMode).
|
|
|
|
|
2006-04-30 11:11:25 +01:00
|
|
|
db_my_sql_mode(SQLMode):-
|
|
|
|
db_my_sql_mode(myddas,SQLMode).
|
|
|
|
db_my_sql_mode(Connection,SQLMode):-
|
|
|
|
var(SQLMode),!,
|
|
|
|
'$error_checks'(db_my_sql_mode(Connection,SQLMode)),
|
|
|
|
get_value(Connection,Con),
|
|
|
|
c_db_connection_type(Con,mysql), %must be a mysql connection
|
|
|
|
db_sql(Connection,'SELECT @@session.sql_mode',[SQLMode]).
|
|
|
|
db_my_sql_mode(Connection,SQLMode):-
|
|
|
|
'$error_checks'(db_my_sql_mode(Connection,SQLMode)),
|
|
|
|
get_value(Connection,Con),
|
|
|
|
c_db_connection_type(Con,mysql), %must be a mysql connection
|
|
|
|
'$make_atom'(['SET SESSION sql_mode=`',SQLMode,'`'],FinalSQL),
|
|
|
|
'$write_or_not'(FinalSQL),
|
|
|
|
db_my_result_set(Mode),
|
|
|
|
c_db_my_query(FinalSQL,_,Con,Mode,_).
|
2005-11-22 11:25:59 +00:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-04-30 11:11:25 +01:00
|
|
|
#endif /* MYDDAS_MYSQL*/
|