MYDDAS: Myddas Top Level for MySQL and Datalog

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1537 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
tiagosoares 2006-02-08 17:29:55 +00:00
parent 7b527b676e
commit fbcfdb49a9
9 changed files with 271 additions and 41 deletions

View File

@ -10,8 +10,11 @@
* File: c_interface.c *
* comments: c_interface primitives definition *
* *
* Last rev: $Date: 2006-01-18 15:34:53 $,$Author: vsc $ *
* Last rev: $Date: 2006-02-08 17:29:54 $,$Author: tiagosoares $ *
* $Log: not supported by cvs2svn $
* Revision 1.82 2006/01/18 15:34:53 vsc
* avoid sideffects from MkBigInt
*
* Revision 1.81 2006/01/16 02:57:51 vsc
* fix bug with very large integers
* fix bug where indexing code was looking at code after a cut.
@ -1303,6 +1306,21 @@ YAP_Init(YAP_init_args *yap_init)
*/
yap_flags[HALT_AFTER_CONSULT_FLAG] = yap_init->HaltAfterConsult;
}
#ifdef MYDDAS_MYSQL
if (yap_init->myddas) {
Yap_PutValue(Yap_FullLookupAtom("$myddas_goal"),MkIntegerTerm(yap_init->myddas));
/* Mandatory Fields */
Yap_PutValue(Yap_FullLookupAtom("$myddas_user"),MkAtomTerm(Yap_LookupAtom(yap_init->myddas_user)));
Yap_PutValue(Yap_FullLookupAtom("$myddas_db"),MkAtomTerm(Yap_LookupAtom(yap_init->myddas_db)));
/* Non-Mandatory Fields */
if (yap_init->myddas_pass != NULL)
Yap_PutValue(Yap_FullLookupAtom("$myddas_pass"),MkAtomTerm(Yap_LookupAtom(yap_init->myddas_pass)));
if (yap_init->myddas_host != NULL)
Yap_PutValue(Yap_FullLookupAtom("$myddas_host"),MkAtomTerm(Yap_LookupAtom(yap_init->myddas_host)));
}
#endif
if (yap_init->YapPrologTopLevelGoal) {
Yap_PutValue(Yap_FullLookupAtom("$top_level_goal"), MkAtomTerm(Yap_LookupAtom(yap_init->YapPrologTopLevelGoal)));
}

View File

@ -8,8 +8,6 @@ static MYDDAS_UTIL_PREDICATE
myddas_util_initialize_predicate(char *, int,char *,
MYDDAS_UTIL_PREDICATE);
MYDDAS_GLOBAL
myddas_util_initialize_myddas(){
MYDDAS_GLOBAL global = NULL;

View File

@ -21,6 +21,7 @@
#include "Yatom.h"
#include "myddas_util.h"
#include "myddas_structs.h"
#include "myddas_statistics.h"
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
@ -29,11 +30,23 @@
#include <readline/history.h>
#include <sys/times.h>
//void myddas_top_level_preprocess(char *);
STATIC_PROTO(ulong start_timer,(void));
STATIC_PROTO(int c_db_tl_top_level_mysql,(void));
STATIC_PROTO(int c_db_tl_readline,(void));
STATIC_PROTO(int com_quit,(char *,char*));
STATIC_PROTO(int myddas_top_level_command,(char *,char *));
STATIC_PROTO(int c_db_top_level,(void));
STATIC_PROTO(void myddas_top_level_print_time,(MYDDAS_STATS_TIME));
void Yap_InitMYDDAS_TopLevelPreds(void)
{
/* c_db_readline: +Prompt x -Line */
Yap_InitCPred("c_db_tl_readline", 2, c_db_tl_readline, SafePredFlag|SyncPredFlag|HiddenPredFlag);
Yap_InitCPred("c_db_tl_top_level_mysql", 2, c_db_tl_top_level_mysql, SafePredFlag|SyncPredFlag|HiddenPredFlag);
}
typedef struct {
const char *name; /* User printable name of the function. */
@ -46,16 +59,37 @@ typedef struct {
static COMMANDS commands[] = {
// { "?", '?', com_help, 1, "Synonym for `help'." },
// { "use", 'u', com_use, "Use another database. Takes database name as argument."},
{ "exit", 'q', com_quit, "Exit MYDDAS Top Level. Same as quit."},
{ "quit", 'q', com_quit, "Quit MYDDAS Top Level." },
// End of the vector
{ (char *)NULL, 0, 0, ""}
};
static int
c_db_tl_readline(void) {
Term arg_prompt = Deref(ARG1);
Term arg_line = Deref(ARG2);
char *prompt = AtomName(AtomOfTerm(arg_prompt));
char *line;
while (strlen(line = readline(prompt)) == 0) {
free(line);
}
add_history(line);
Term line_read = MkAtomTerm(Yap_LookupAtom(line));
free(line);
if (!Yap_unify(arg_line,line_read))
return FALSE;
return TRUE;
}
static int
c_db_top_level(void) {
c_db_tl_top_level_mysql(void) {
Term arg_conn = Deref(ARG1);
Term arg_res_set_mode = Deref(ARG2);
@ -72,8 +106,6 @@ c_db_top_level(void) {
while (strlen(line = readline("mysql> ")) == 0) {
free(line);
}
//myddas_top_level_preprocess(line);
add_history(line);
quit = myddas_top_level_command(line,res_set_mode);
free(line);
@ -83,17 +115,6 @@ c_db_top_level(void) {
}
static
ulong start_timer(void){
struct tms tms_tmp;
return times(&tms_tmp);
}
/* void */
/* myddas_top_level_preprocess(char *line); */
static int
myddas_top_level_command(char *line,char *res_set_mode){
@ -111,27 +132,36 @@ myddas_top_level_command(char *line,char *res_set_mode){
if (!strcmp(commands[i].name,line))
{
int quit = (*(commands[i].func))(NULL,NULL);
if (quit == -1)
return -1;
return quit;
}
int length=strlen(line);
conn = (MYSQL *) (Yap_regp->MYDDAS_GLOBAL_POINTER->myddas_top_level_connection->connection);
ulong start = start_timer();
MYDDAS_STATS_TIME start = myddas_stats_walltime();
if (mysql_real_query(conn, line, length) != 0){
printf ("ERROR %d (%s): %s\n",mysql_errno(conn),mysql_sqlstate(conn),mysql_error(conn));
}
else{
if (strcmp(res_set_mode,"store_result")==0) //True
res_set = mysql_store_result(conn);
else
res_set = mysql_use_result(conn);
ulong end = start_timer();
MYDDAS_STATS_TIME end = myddas_stats_walltime();
MYDDAS_STATS_TIME diff;
MYDDAS_STATS_INITIALIZE_TIME_STRUCT(diff,time_copy);
myddas_stats_subtract_time(diff,end,start);
diff = myddas_stats_time_copy_to_final(diff);
myddas_util_table_write(res_set);
printf ("%lld rows in set (%ld sec)\n",mysql_num_rows(res_set),end-start);
printf ("%lld rows in set ",mysql_num_rows(res_set));
myddas_top_level_print_time(diff);
printf ("\n");
mysql_free_result(res_set);
}
return 0;
@ -143,10 +173,16 @@ int com_quit(char *nill,char *null){
return -1;
}
void Yap_InitMYDDAS_TopLevelPreds(void)
{
Yap_InitCPred("c_db_top_level", 2, c_db_top_level, SafePredFlag|SyncPredFlag|HiddenPredFlag);
static void
myddas_top_level_print_time(MYDDAS_STATS_TIME time){
//TODO test for big queries, and see the output of mysql
printf("(");
printf("%d",time->u.time_final.seconds);
//MiliSeconds 2 decimal points
printf(".%d",time->u.time_final.miliseconds/10);
printf (" sec)");
}
#endif

View File

@ -257,6 +257,12 @@ myddas_util_table_write(MYSQL_RES *res_set){
MYSQL_FIELD *fields;
int i,f;
if (mysql_num_rows(res_set) == 0)
{
printf ("Empty Set\n");
return;
}
f = mysql_num_fields(res_set);
fields = mysql_fetch_field(res_set);

View File

@ -277,9 +277,11 @@ parse_yap_arguments(int argc, char *argv[], YAP_init_args *iap)
int BootMode = YAP_FULL_BOOT_FROM_PROLOG;
#else
int BootMode = YAP_BOOT_FROM_SAVED_CODE;
#endif
#ifdef MYDDAS_MYSQL
char *myddas_temp;
#endif
int *ssize;
while (--argc > 0)
{
p = *++argv;
@ -345,6 +347,36 @@ parse_yap_arguments(int argc, char *argv[], YAP_init_args *iap)
case 'f':
iap->FastBoot = TRUE;
break;
#ifdef MYDDAS_MYSQL
case 'm':
if (strncmp(p,"myddas_",7) == 0)
{
iap->myddas = 1;
if ((*argv)[0] == '\0')
myddas_temp = *argv;
else {
argc--;
if (argc == 0) {
fprintf(stderr," [ YAP unrecoverable error: missing file name with option 'l' ]\n");
exit(EXIT_FAILURE);
}
argv++;
myddas_temp = *argv;
}
if (strstr(p,"user") != NULL)
iap->myddas_user = myddas_temp;
else if (strstr(p,"pass") != NULL)
iap->myddas_pass = myddas_temp;
else if (strstr(p,"db") != NULL)
iap->myddas_db = myddas_temp;
else if (strstr(p,"host") != NULL)
iap->myddas_host = myddas_temp;
else
goto myddas_error_print;
break;
}
#endif
#ifdef MPWSHELL
case 'm':
if (*++p == 'p' && *++p == 'w' && *++p == '\0')
@ -510,7 +542,13 @@ parse_yap_arguments(int argc, char *argv[], YAP_init_args *iap)
break;
default:
{
#ifdef MYDDAS_MYSQL
myddas_error_print :
#endif
fprintf(stderr,"[ YAP unrecoverable error: unknown switch -%c ]\n", *p);
#ifdef MYDDAS_MYSQL
myddas_error :
#endif
print_usage();
exit(EXIT_FAILURE);
}
@ -519,6 +557,15 @@ parse_yap_arguments(int argc, char *argv[], YAP_init_args *iap)
iap->SavedState = p;
}
}
#ifdef MYDDAS_MYSQL
/* Check MYDDAS Arguments */
if (iap->myddas_user != NULL || iap->myddas_pass != NULL
|| iap->myddas_db != NULL || iap->myddas_host != NULL)
if (iap->myddas_user == NULL || iap->myddas_db == NULL){
fprintf(stderr,"[ YAP unrecoverable error: Missing Mandatory Arguments for MYDDAS ]\n");
goto myddas_error;
}
#endif
return(BootMode);
}
@ -549,6 +596,13 @@ init_standard_system(int argc, char *argv[], YAP_init_args *iap)
iap->PrologShouldHandleInterrupts = TRUE;
iap->Argc = argc;
iap->Argv = argv;
#ifdef MYDDAS_MYSQL
iap->myddas = 0;
iap->myddas_user = NULL;
iap->myddas_pass = NULL;
iap->myddas_db = NULL;
iap->myddas_host = NULL;
#endif
iap->ErrorNo = 0;
iap->ErrorCause = NULL;

View File

@ -112,6 +112,15 @@ typedef struct {
int Argc;
/* array of arguments as seen by Prolog */
char **Argv;
#ifdef MYDDAS_MYSQL
/* If any myddas option was given */
short myddas;
/* MYDDAS Fields */
char *myddas_user;
char *myddas_pass;
char *myddas_db;
char *myddas_host;
#endif
/* errornumber */
int ErrorNo;
/* errorstring */

View File

@ -39,7 +39,8 @@
db_multi_queries_number/2,
% myddas_top_level.yap
db_top_level/4,
db_top_level/5,
db_datalog_select/3,
% myddas_assert_predicates.yap
db_import/3,
@ -69,7 +70,8 @@
]).
:- use_module(myddas_top_level,[
db_top_level/4
db_top_level/5,
db_datalog_select/3
]).
:- use_module(myddas_assert_predicates,[
@ -140,7 +142,7 @@ db_open(mysql,Connection,Host/Db,User,Password) :-
'$error_checks'(db_open(mysql,Connection,Host/Db,User,Password)),
c_db_my_connect(Host,User,Password,Db,Con),
set_value(Connection,Con).
db_open(odbc,Connection,ODBCEntry,User,Password) :-
'$error_checks'(db_open(odbc,Connection,ODBCEntry,User,Password)),
c_db_odbc_connect(ODBCEntry,User,Password,Con),

View File

@ -16,24 +16,94 @@
*************************************************************************/
:- module(myddas_top_level,[
db_top_level/4
]).
db_top_level/5,
db_datalog_select/3
]).
:- use_module(myddas_mysql,[
db_my_result_set/1
]).
db_top_level(Connection,_,_,_):-
:- use_module(charsio,[
read_from_chars/2
]).
:- use_module(terms,[
term_variables/2
]).
:- use_module(myddas_util_predicates,[
'$make_list_of_args'/4,
'$prolog2sql'/3,
'$write_or_not'/1,
'$lenght'/2
]).
db_top_level(mysql,Connection,_,_,_):-
%'$error_checks'(db_open(mysql,Connection,Host/Db,User,Password)),
get_value(Connection,Con),
Con \= [],!,
c_db_connection_type(Con,mysql),
db_my_result_set(Mode),
c_db_top_level(Con,Mode).
c_db_tl_top_level_mysql(Con,Mode).
db_top_level(Connection,Host/Db,User,Password):-
db_top_level(datalog,Connection,_,_,_):-
%'$error_checks'(db_open(mysql,Connection,Host/Db,User,Password)),
c_db_my_connect(Host,User,Password,Db,Con),
set_value(Connection,Con),
get_value(Connection,Con),
Con \= [],!,
c_db_connection_type(Con,mysql),
Prompt = ' datalog> ',
nl,
'$top_level_datalog_cicle'(Connection,Prompt).
% c_db_tl_readline(Prompt,Line),
% name(Line,CharsLine),
% read_from_chars(CharsLine,Query),
% term_variables(Query,VarList),
% db_datalog_select(Connection,VarList,Query).
'$top_level_datalog_cicle'(Connection,Prompt):-
c_db_tl_readline(Prompt,Line),
name(Line,CharsLine),
catch(read_from_chars(CharsLine,Query),_,'$top_level_datalog_cicle'(Connection,Prompt)),
!,'$top_level_datalog'(Connection,Prompt,Query).
'$top_level_datalog'(_,_,halt):-!.
'$top_level_datalog'(Connection,Prompt,Query):-
term_variables(Query,[]),!,
Query =..[_|Args],
db_datalog_select(Connection,Args,Query),
'$top_level_datalog_cicle'(Connection,Prompt).
'$top_level_datalog'(Connection,Prompt,Query):-
term_variables(Query,VarList),
db_datalog_select(Connection,VarList,Query),
!,'$top_level_datalog_cicle'(Connection,Prompt).
db_datalog_select(Connection,LA,DbGoal):-
'$lenght'(LA,Arity),
functor(ViewName,viewname,Arity),
% build arg list for viewname/Arity
'$make_list_of_args'(1,Arity,ViewName,LA),
'$prolog2sql'(ViewName,DbGoal,SQL),
get_value(Connection,Con),
c_db_connection_type(Con,ConType),
'$write_or_not'(SQL),
%( ConType == mysql ->
db_my_result_set(Mode),
c_db_top_level(Con,Mode).
c_db_my_query(SQL,ResultSet,Con,Mode),
c_db_my_table_write(ResultSet).
% db_top_level(mysqlConnection,Host/Db,User,Password):-
% %'$error_checks'(db_open(mysql,Connection,Host/Db,User,Password)),
% c_db_my_connect(Host,User,Password,Db,Con),
% set_value(Connection,Con),
% db_my_result_set(Mode),
% c_db_top_level(Con,Mode).

View File

@ -164,6 +164,31 @@ true :- true.
set_value('$init_goal',[]),
'$run_atom_goal'(GA),
fail.
'$startup_goals' :-
get_value('$myddas_goal',GA), GA \= [],
set_value('$myddas_goal',[]),
get_value('$myddas_user',User), User \= [],
set_value('$myddas_user',[]),
get_value('$myddas_db',Db), Db \= [],
set_value('$myddas_db',[]),
get_value('$myddas_host',HostT),
( HostT \= [] ->
Host = HostT,
set_value('$myddas_host',[])
;
Host = localhost
),
get_value('$myddas_pass',PassT),
( PassT \= [] ->
Pass = PassT,
set_value('$myddas_pass',[])
;
Pass = ''
),
use_module(library(myddas)),
call(db_open(mysql,myddas,Host/Db,User,Pass)),
'$myddas_import_all',
fail.
'$startup_goals'.
'$startup_reconsult' :-
@ -172,6 +197,18 @@ true :- true.
'$do_startup_reconsult'(X).
'$startup_reconsult'.
%
% MYDDAS: Import all the tables from one database
%
'$myddas_import_all':-
call(db_my_show_tables(myddas,table(Table))),
call(db_import(myddas,Table,Table)),
fail.
'$myddas_import_all'.
%
% remove any debugging info after an abort.
%