This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/myddas/pl/myddas_odbc.ypp

227 lines
6.3 KiB
Plaintext
Raw Normal View History

2016-08-01 12:54:28 +01: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: *
* comments: MySQL Predicates *
* *
*************************************************************************/
2017-10-18 17:02:27 +01:00
#ifdef MYDDAS_ODBC
:- module(myddas_odbc,[
odbc_result_set/1,
odbc_datalog_describe/1,
odbc_datalog_describe/2,
odbc_describe/3,
odbc_describe/2,
odbc_datalog_show_tables/1,
odbc_datalog_show_tables/0,
odbc_show_tables/2,
odbc_show_tables/1,
odbc_show_database/2,
odbc_show_databases/2,
odbc_show_databases/1,
odbc_change_database/2,
odbc_call_procedure/4,
odbc_call_procedure/3,
c_odbc_change_database/2,
c_odbc_connect/4,
c_odbc_disconnect/1,
c_odbc_get_attributes_types/3,
c_odbc_get_database/2,
c_odbc_get_fields_properties/3,
c_odbc_get_next_result_set/3,
c_odbc_query/5,
c_odbc_number_of_fields/3,
c_odbc_row/3
2016-08-01 12:54:28 +01:00
]).
:- use_module(myddas,[
db_sql/3
]).
:- use_module(myddas_errors,[
'$error_checks'/1
]).
:- use_module(myddas_util_predicates,[
'$get_value'/2,
'$make_atom'/2,
'$make_atom_args'/2,
'$make_a_list'/2,
'$write_or_not'/1
]).
2017-10-18 17:02:27 +01:00
:- load_foreign_files( ['Yapodbc'], [], init_odbc).
2016-08-01 12:54:28 +01:00
%--------------------------------------------------------
% Public Predicates
%--------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017-10-18 17:02:27 +01:00
% odbc_result_set/1
2016-08-01 12:54:28 +01:00
%
%
2017-10-18 17:02:27 +01:00
odbc_result_set(X):-
2016-08-01 12:54:28 +01:00
var(X),!,
2017-10-18 17:02:27 +01:00
get_value(odbc_result_set,X).
odbc_result_set(use_result):-
set_value(odbc_result_set,use_result).
odbc_result_set(store_result):-
set_value(odbc_result_set,store_result).
2016-08-01 12:54:28 +01:00
%default value
2017-10-18 17:02:27 +01:00
:- odbc_result_set(use_result).
2016-08-01 12:54:28 +01:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017-10-18 17:02:27 +01:00
% odbc_describe/2
2016-08-01 12:54:28 +01:00
%
%
2017-10-18 17:02:27 +01:00
odbc_datalog_describe(Relation):-
odbc_datalog_describe(myddas,Relation).
odbc_datalog_describe(Connection,Relation) :-
'$error_checks'(odbc_datalog_describe(Relation,Connection)),
2016-08-01 12:54:28 +01:00
'$get_value'(Connection,Conn),
'$make_atom'(['DESCRIBE ',Relation],SQL),
2017-10-18 17:02:27 +01:00
odbc_result_set(Mode),
c_odbc_query(SQL,ResultSet,Conn,Mode,_),
c_odbc_table_write(ResultSet).
2016-08-01 12:54:28 +01:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017-10-18 17:02:27 +01:00
% odbc_describe/3
% odbc_describe/2
2016-08-01 12:54:28 +01:00
% gives the results of the DESCRIBE statement
% by backtracking
2017-10-18 17:02:27 +01:00
odbc_describe(Relation,TableInfo) :-
odbc_describe(myddas,Relation,TableInfo).
odbc_describe(Connection,Relation,tableinfo(A1,A2,A3,A4,A5,A6)) :-
'$error_checks'(odbc_describe(Relation,Connection,_)),
2016-08-01 12:54:28 +01:00
'$get_value'(Connection,Conn),
'$make_atom'(['DESCRIBE ',Relation],SQL),
2017-10-18 17:02:27 +01:00
odbc_result_set(Mode),
2016-08-01 12:54:28 +01:00
'$write_or_not'(SQL),
2017-10-18 17:02:27 +01:00
c_odbc_query(SQL,ResultSet,Conn,Mode,_),
2016-08-01 12:54:28 +01:00
!,
2017-10-18 17:02:27 +01:00
c_odbc_row(ResultSet,6,[A1,A2,A3,A4,A5,A6]).
2016-08-01 12:54:28 +01:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017-10-18 17:02:27 +01:00
% odbc_datalog_show_tables/1
2016-08-01 12:54:28 +01:00
%
%
2017-10-18 17:02:27 +01:00
odbc_datalog_show_tables:-
odbc_datalog_show_tables(myddas).
odbc_datalog_show_tables(Connection) :-
'$error_checks'(odbc_show_tables(Connection)),
2016-08-01 12:54:28 +01:00
'$get_value'(Connection,Conn),
2017-10-18 17:02:27 +01:00
odbc_result_set(Mode),
2016-08-01 12:54:28 +01:00
'$write_or_not'('.tables'),
2017-10-18 17:02:27 +01:00
c_odbc_query('.tables',ResultSet,Conn,Mode,_),
c_odbc_table_write(ResultSet).
2016-08-01 12:54:28 +01:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017-10-18 17:02:27 +01:00
% odbc_show_tables/2
% odbc_show_tables/1
2016-08-01 12:54:28 +01:00
% gives the results of the SHOW TABLES statement
% by backtracking
2017-10-18 17:02:27 +01:00
odbc_show_tables(Table) :-
odbc_show_tables(myddas,Table).
odbc_show_tables(Connection,table(Table)) :-
'$error_checks'(odbc_show_tables(Connection)),
2016-08-01 12:54:28 +01:00
'$get_value'(Connection,Conn),
2017-10-18 17:02:27 +01:00
odbc_result_set(Mode),
2016-08-01 12:54:28 +01:00
'$write_or_not'('.tables'),
2017-10-18 17:02:27 +01:00
c_odbc_query('.tables',ResultSet,Conn,Mode,_),
!,c_odbc_row(ResultSet,1,[Table]).
2016-08-01 12:54:28 +01:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017-10-18 17:02:27 +01:00
% odbc_show_database/2
2016-08-01 12:54:28 +01:00
%
%
2017-10-18 17:02:27 +01:00
odbc_show_database(Connection,Database) :-
'$error_checks'(odbc_show_database(Connection,Database)),
2016-08-01 12:54:28 +01:00
'$get_value'(Connection,Con),
2017-10-18 17:02:27 +01:00
c_odbc_get_database(Con,Database).
2016-08-01 12:54:28 +01:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017-10-18 17:02:27 +01:00
% odbc_show_databases/2
2016-08-01 12:54:28 +01:00
%
%
2017-10-18 17:02:27 +01:00
odbc_show_databases(Connection,database(Databases)) :-
%'$error_checks'(odbc_show_databases(Connection,Database)),
2016-08-01 12:54:28 +01:00
'$get_value'(Connection,Conn),
2017-10-18 17:02:27 +01:00
odbc_result_set(Mode),
2016-08-01 12:54:28 +01:00
'$write_or_not'('SHOW DATABASES'),
2017-10-18 17:02:27 +01:00
c_odbc_query('SHOW DATABASES',ResultSet,Conn,Mode,_),
!,c_odbc_row(ResultSet,1,[Databases]).
2016-08-01 12:54:28 +01:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017-10-18 17:02:27 +01:00
% odbc_show_databases/1
2016-08-01 12:54:28 +01:00
% TODO Error Checks
%
2017-10-18 17:02:27 +01:00
odbc_show_databases(Connection) :-
'$error_checks'(odbc_show_databases(Connection)),
2016-08-01 12:54:28 +01:00
'$get_value'(Connection,Conn),
2017-10-18 17:02:27 +01:00
odbc_result_set(Mode),
2016-08-01 12:54:28 +01:00
'$write_or_not'('SHOW DATABASES'),
2017-10-18 17:02:27 +01:00
c_odbc_query('SHOW DATABASES',ResultSet,Conn,Mode,_),
c_odbc_table_write(ResultSet).
2016-08-01 12:54:28 +01:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017-10-18 17:02:27 +01:00
% odbc_change_database/2
2016-08-01 12:54:28 +01:00
%
%
2017-10-18 17:02:27 +01:00
odbc_change_database(Connection,Database) :-
'$error_checks'(odbc_change_database(Connection,Database)),
2016-08-01 12:54:28 +01:00
'$get_value'(Connection,Con),
'$make_atom'(['USE ',Database],SQL),
'$write_or_not'(SQL),
2017-10-18 17:02:27 +01:00
c_odbc_change_database(Con,Database).
2016-08-01 12:54:28 +01:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017-10-18 17:02:27 +01:00
% odbc_call_procedure/4
% odbc_call_procedure/3
% odbc_call_procedure(+,+,+,?)
2016-08-01 12:54:28 +01:00
% Only support in MySQL 5.0 an above
% Called procedure must return results via MySQL result set
2017-10-18 17:02:27 +01:00
odbc_call_procedure(Procedure,Args,Result) :-
odbc_call_procedure(myddas,Procedure,Args,Result).
odbc_call_procedure(Connection,Procedure,Args,LA) :-
'$error_checks'(odbc_call_procedure(Connection,Procedure,Args,LA)),
2016-08-01 12:54:28 +01:00
'$make_atom_args'(Args,ArgsSQL),
'$make_atom'(['CALL ',Procedure,'(',ArgsSQL,')'],SQL),
2017-10-18 17:02:27 +01:00
odbc_sql(Connection,SQL,LA).
2016-08-01 12:54:28 +01:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017-10-18 17:02:27 +01:00
#endif /* MYDDAS_ODBC */