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_driver.ypp

243 lines
6.8 KiB
Plaintext
Raw Normal View History

/*************************************************************************
* *
* 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: *
2019-03-31 11:46:17 +01:00
* mods: *show
* comments: MySQL Predicates *
* *
*************************************************************************/
#ifdef sqlite3
#undef sqlite3
#define DBMS(x) sqlite3_##x
#define c_DBMS(x) c_sqlite3_##x
2018-06-28 12:48:29 +01:00
#define NAME() 'YAPsqlite3'
#define MODULE() myddas_sqlite3
#define INIT() init_sqlite3
2019-04-05 23:52:34 +01:00
#elif defined( odbc ) && !defined(ANDROID)
#undef odbc
#define DBMS(x) odbc_##x
#define c_DBMS(x) c_odbc_##x
2018-06-28 12:48:29 +01:00
#define NAME() 'YAPodbc'
#define MODULE() myddas_odbc
#define INIT() init_odbc
2019-04-05 23:52:34 +01:00
#elif defined( postgres ) && !defined(ANDROID)
2017-11-18 00:04:42 +00:00
#undef postgres
#define DBMS(x) postgres_##x
#define c_DBMS(x) c_postgres_##x
2018-06-28 12:48:29 +01:00
#define NAME() 'YAPpostgres'
#define MODULE() myddas_postgres
2018-06-28 12:48:29 +01:00
#define INIT() init_postgres
#endif
#if defined(DBMS)
:- module(MODULE(),[
c_DBMS(change_database)/2,
c_DBMS(connect)/4,
c_DBMS(disconnect)/1,
c_DBMS(get_attributes_types)/3,
c_DBMS(get_database)/2,
c_DBMS(get_fields_properties)/3,
c_DBMS(get_next_result_set)/3,
c_DBMS(query)/5,
c_DBMS(number_of_fields)/3,
2017-11-18 00:04:42 +00:00
DBMS(describe)/3,
DBMS(result_set)/1,
2017-11-18 00:04:42 +00:00
DBMS(show_tables)/2,
DBMS(row)/3
]).
2018-07-02 16:48:00 +01:00
:- use_module(library(lists)).
:- use_module(library(maplist)).
2019-04-07 00:25:10 +01:00
:- use_module(myddas_core).
:- 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
]).
2018-06-28 12:48:29 +01:00
:- load_foreign_files( [NAME()], [], INIT()).
%--------------------------------------------------------
% Public Predicates
%--------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DBMS(result_set)/1
%
%
DBMS(result_set)(X):-
var(X),!,
get_value(DBMS(result_set),X).
DBMS(result_set)(use_result):-
set_value(DBMS(result_set),use_result).
2017-11-18 00:04:42 +00:00
DBMS(result_set)(store_result):-
set_value(DBMS(result_set),store_result).
%default value
:- DBMS(result_set)(use_result).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017-11-18 00:04:42 +00:00
% DBMS(db_datalog_describe)/2
%
%
DBMS(datalog_describe)(Relation):-
DBMS(datalog_describe)(myddas,Relation).
DBMS(datalog_describe)(Connection,Relation) :-
'$error_checks'(DBMS(datalog_describe)(Relation,Connection)),
'$get_value'(Connection,Conn),
'$make_atom'(['DESCRIBE ',Relation],SQL),
DBMS(result_set)(Mode),
c_DBMS(query)(SQL,ResultSet,Conn,Mode,_),
c_DBMS(table_write)(ResultSet).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DBMS(describe)/3
% gives the results of the DESCRIBE statement
% by backtracking
2017-11-18 00:04:42 +00:00
%#if sqlite3
DBMS(describe)(Connection,Relation,tableinfo(FieldID,Type,Null,Primary,Default,'')) :-
'$get_value'(Connection,Conn),
2017-11-18 00:04:42 +00:00
'$make_atom'(['PRAGMA table_info(\'',Relation,'\')'],SQL),
DBMS(result_set)(Mode),
'$write_or_not'(SQL),
c_DBMS(query)(SQL,ResultSet,Conn,Mode,_),
!,
2017-11-18 00:04:42 +00:00
DBMS(row)(ResultSet,6,[_A1,FieldID,Type,Null,Default,Primary]).
%#endif
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DBMS(datalog_show_tables)/1
%
%
DBMS(datalog_show_tables):-
DBMS(datalog_show_tables)(myddas).
DBMS(datalog_show_tables)(Connection) :-
'$get_value'(Connection,Conn),
DBMS(result_set)(Mode),
'$write_or_not'('.tables'),
c_DBMS(query)('.tables',ResultSet,Conn,Mode,_),
c_DBMS(table_write)(ResultSet).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DBMS(show_tables)/2
% gives the results of the SHOW TABLES statement
% by backtracking
2019-03-31 11:46:17 +01:00
DBMS(show_tables)(Connection,table(Table)) :-
'$get_value'(Connection,Conn),
2017-11-18 00:04:42 +00:00
SQL = 'SELECT name FROM sqlite_master WHERE type=\'table\' ORDER BY name',
DBMS(result_set)(Mode),
2017-11-18 00:04:42 +00:00
'$write_or_not'(SQL),
c_DBMS(query)(SQL,ResultSet,Conn,Mode,_),
!,
DBMS(row)(ResultSet,1,[Table]).
2018-11-26 09:16:52 +00:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DBMS(show_database)/2
2017-11-18 00:04:42 +00:00
%
%
DBMS(show_database)(Connection,Database) :-
'$error_checks'(DBMS(show_database)(Connection,Database)),
'$get_value'(Connection,Con),
c_DBMS(get_database)(Con,Database).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DBMS(show_databases)/2
2017-11-18 00:04:42 +00:00
%
%
DBMS(show_databases)(Connection,database(Databases)) :-
%'$error_checks'(DBMS(show_databases)(Connection,Database)),
'$get_value'(Connection,Conn),
DBMS(result_set)(Mode),
'$write_or_not'('SHOW DATABASES'),
c_DBMS(query)('SHOW DATABASES',ResultSet,Conn,Mode,_),
2018-03-26 23:12:48 +01:00
!,DBMS(row)(ResultSet,1,[Databases]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DBMS(show_databases)/1
% TODO Error Checks
%
DBMS(show_databases)(Connection) :-
'$error_checks'(DBMS(show_databases)(Connection)),
'$get_value'(Connection,Conn),
DBMS(result_set)(Mode),
'$write_or_not'('SHOW DATABASES'),
c_DBMS(query)('SHOW DATABASES',ResultSet,Conn,Mode,_),
c_DBMS(table_write)(ResultSet).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DBMS(change_database)/2
2017-11-18 00:04:42 +00:00
%
%
DBMS(change_database)(Connection,Database) :-
'$error_checks'(DBMS(change_database)(Connection,Database)),
'$get_value'(Connection,Con),
'$make_atom'(['USE ',Database],SQL),
'$write_or_not'(SQL),
c_DBMS(change_database)(Con,Database).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DBMS(call_procedure)/4
% DBMS(call_procedure)/3
2017-11-18 00:04:42 +00:00
% DBMS(call_procedure)(+,+,+,?)
% Only support in MySQL 5.0 an above
% Called procedure must return results via MySQL result set
DBMS(call_procedure)(Procedure,Args,Result) :-
DBMS(call_procedure)(myddas,Procedure,Args,Result).
DBMS(call_procedure)(Connection,Procedure,Args,LA) :-
'$error_checks'(DBMS(call_procedure)(Connection,Procedure,Args,LA)),
'$make_atom_args'(Args,ArgsSQL),
'$make_atom'(['CALL ',Procedure,'(',ArgsSQL,')'],SQL),
DBMS(sql)(Connection,SQL,LA).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2017-08-21 12:41:15 +01:00
DBMS(row)(ResultSet,Arity,Args) :-
2017-11-18 00:04:42 +00:00
setup_call_catcher_cleanup(
c_DBMS(row_initialise)(ResultSet,State),
2017-08-21 12:41:15 +01:00
c_DBMS(row_get)(ResultSet,Arity,Args,State),
2017-11-18 00:04:42 +00:00
Caught,
c_DBMS(row_terminate)(State, Caught)
2017-08-21 12:41:15 +01:00
).
#endif /* MYDDAS_DBMS */