/************************************************************************* * * * 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: *show * comments: MySQL Predicates * * * *************************************************************************/ #ifdef sqlite3 #undef sqlite3 #define DBMS(x) sqlite3_##x #define c_DBMS(x) c_sqlite3_##x #define NAME() 'YAPsqlite3' #define MODULE() myddas_sqlite3 #define INIT() init_sqlite3 #elif defined( odbc ) && !defined(ANDROID) #undef odbc #define DBMS(x) odbc_##x #define c_DBMS(x) c_odbc_##x #define NAME() 'YAPodbc' #define MODULE() myddas_odbc #define INIT() init_odbc #elif defined( postgres ) && !defined(ANDROID) #undef postgres #define DBMS(x) postgres_##x #define c_DBMS(x) c_postgres_##x #define NAME() 'YAPpostgres' #define MODULE() myddas_postgres #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, DBMS(describe)/3, DBMS(result_set)/1, DBMS(show_tables)/2, DBMS(row)/3 ]). :- use_module(library(lists)). :- use_module(library(maplist)). :- use_module(myddas). :- 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 ]). :- 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). DBMS(result_set)(store_result):- set_value(DBMS(result_set),store_result). %default value :- DBMS(result_set)(use_result). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 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 %#if sqlite3 DBMS(describe)(Connection,Relation,tableinfo(FieldID,Type,Null,Primary,Default,'')) :- '$get_value'(Connection,Conn), '$make_atom'(['PRAGMA table_info(\'',Relation,'\')'],SQL), DBMS(result_set)(Mode), '$write_or_not'(SQL), c_DBMS(query)(SQL,ResultSet,Conn,Mode,_), !, 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 DBMS(show_tables)(Connection,table(Table)) :- '$get_value'(Connection,Conn), SQL = 'SELECT name FROM sqlite_master WHERE type=\'table\' ORDER BY name', DBMS(result_set)(Mode), '$write_or_not'(SQL), c_DBMS(query)(SQL,ResultSet,Conn,Mode,_), !, DBMS(row)(ResultSet,1,[Table]). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % DBMS(show_database)/2 % % 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 % % 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,_), !,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 % % 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 % 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). %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% DBMS(row)(ResultSet,Arity,Args) :- setup_call_catcher_cleanup( c_DBMS(row_initialise)(ResultSet,State), c_DBMS(row_get)(ResultSet,Arity,Args,State), Caught, c_DBMS(row_terminate)(State, Caught) ). #endif /* MYDDAS_DBMS */