MYDDAS: MySQL Top Level now available with db_top_level/4 or db_top_level/5 predicates

git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1659 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
tiagosoares 2006-05-31 14:35:05 +00:00
parent 326c64ff2c
commit 637cb7812c
4 changed files with 33 additions and 122 deletions

View File

@ -193,7 +193,7 @@ c_db_my_query(void) {
start = myddas_stats_walltime(); start = myddas_stats_walltime();
#endif #endif
/* executar a query SQL */ /* Send query to server and process it */
if (mysql_real_query(conn, sql, length) != 0) if (mysql_real_query(conn, sql, length) != 0)
{ {
#ifdef DEBUG #ifdef DEBUG

View File

@ -34,20 +34,14 @@
#include <sys/times.h> #include <sys/times.h>
STATIC_PROTO(Int c_db_tl_top_level_mysql,(void));
STATIC_PROTO(Int c_db_tl_readline,(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(void myddas_top_level_print_time,(MYDDAS_STATS_TIME));
void Yap_InitMYDDAS_TopLevelPreds(void) void Yap_InitMYDDAS_TopLevelPreds(void)
{ {
/* c_db_readline: +Prompt x -Line */ /* 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_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);
} }
@ -60,15 +54,6 @@ typedef struct {
} COMMANDS; } COMMANDS;
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 static Int
c_db_tl_readline(void) { c_db_tl_readline(void) {
Term arg_prompt = Deref(ARG1); Term arg_prompt = Deref(ARG1);
@ -91,91 +76,6 @@ c_db_tl_readline(void) {
} }
static Int
c_db_tl_top_level_mysql(void) {
Term arg_conn = Deref(ARG1);
Term arg_res_set_mode = Deref(ARG2);
MYSQL *con = (MYSQL *) IntegerOfTerm(arg_conn);
char *res_set_mode = AtomName(AtomOfTerm(arg_res_set_mode));
char *line;
Int quit;
Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_level_connection = myddas_util_search_connection(con);
printf ("\n");
for (;;) {
/* Ignore empty lines */
while (strlen(line = readline("mysql> ")) == 0) {
free(line);
}
add_history(line);
quit = myddas_top_level_command(line,res_set_mode);
free(line);
if (quit == -1)
return TRUE;
}
}
static Int
myddas_top_level_command(char *line,char *res_set_mode){
Int i;
MYSQL *conn;
MYSQL_RES *res_set;
if ( (line[0] == '#' ||
(line[0] == '-' && line[1] == '-') ||
line[0] == 0))
return 0; // Skip comment lines
for (i=0;commands[i].name!=NULL;i++)
if (!strcmp(commands[i].name,line))
{
Int quit = (*(commands[i].func))(NULL,NULL);
return quit;
}
Int length=strlen(line);
conn = (MYSQL *) (Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_level_connection->connection);
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);
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 ",mysql_num_rows(res_set));
myddas_top_level_print_time(diff);
printf ("\n");
mysql_free_result(res_set);
}
return 0;
}
Int com_quit(char *nill,char *null){
Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_level_connection = NULL;
printf ("Bye\n");
return -1;
}
static void static void
myddas_top_level_print_time(MYDDAS_STATS_TIME time){ myddas_top_level_print_time(MYDDAS_STATS_TIME time){

View File

@ -51,6 +51,7 @@
% myddas_top_level.ypp % myddas_top_level.ypp
#ifdef MYDDAS_TOP_LEVEL #ifdef MYDDAS_TOP_LEVEL
db_top_level/4,
db_top_level/5, db_top_level/5,
db_datalog_select/3, db_datalog_select/3,
#endif #endif
@ -87,8 +88,9 @@
#endif #endif
]). ]).
#ifdef MYDDAS_MYSQL #ifdef MYDDAS_TOP_LEVEL
:- use_module(myddas_top_level,[ :- use_module(myddas_top_level,[
db_top_level/4,
db_top_level/5, db_top_level/5,
db_datalog_select/3 db_datalog_select/3
]). ]).

View File

@ -18,6 +18,7 @@
#ifdef MYDDAS_TOP_LEVEL #ifdef MYDDAS_TOP_LEVEL
:- module(myddas_top_level,[ :- module(myddas_top_level,[
db_top_level/4,
db_top_level/5, db_top_level/5,
db_datalog_select/3 db_datalog_select/3
]). ]).
@ -26,6 +27,10 @@
db_my_result_set/1 db_my_result_set/1
]). ]).
:- use_module(system,[
system/2
]).
:- use_module(charsio,[ :- use_module(charsio,[
read_from_chars/2 read_from_chars/2
]). ]).
@ -35,18 +40,34 @@
]). ]).
:- use_module(myddas_util_predicates,[ :- use_module(myddas_util_predicates,[
'$make_atom'/2,
'$prolog2sql'/3, '$prolog2sql'/3,
'$write_or_not'/1, '$write_or_not'/1,
'$lenght'/2 '$lenght'/2
]). ]).
db_top_level(mysql,Connection,_,_,_):-
%'$error_checks'(db_open(mysql,Connection,Host/Db,User,Password)),
get_value(Connection,Con), %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Con \= [],!, % db_top_level/5
c_db_connection_type(Con,mysql), % db_top_level/4
db_my_result_set(Mode), %
c_db_tl_top_level_mysql(Con,Mode). db_top_level(mysql,HostDb,User,Password):-
db_top_level(mysql,myddas,HostDb,User,Password).
db_top_level(mysql,Connection,Host/Db/Port/Socket,User,Password) :- !,
'$make_atom'(['mysql',' -h ',Host,' -P ',Port,' -S ',Socket,' -u ',User,' --password=',Password,' ',Db],Command),
system(Command,_).
db_top_level(mysql,Connection,Host/Db/Port,User,Password) :-
integer(Port),!,
'$make_atom'(['mysql',' -h ',Host,' -P ',Port,' -u ',User,' --password=',Password,' ',Db],Command),
system(Command,_).
db_top_level(mysql,Connection,Host/Db/Socket,User,Password) :- !,
db_top_level(mysql,Connection,Host/Db/0/Socket,User,Password).
db_top_level(mysql,Connection,Host/Db,User,Password):-
db_top_level(mysql,Connection,Host/Db/0,User,Password).
db_top_level(datalog,Connection,_,_,_):- db_top_level(datalog,Connection,_,_,_):-
%'$error_checks'(db_open(mysql,Connection,Host/Db,User,Password)), %'$error_checks'(db_open(mysql,Connection,Host/Db,User,Password)),
@ -98,16 +119,4 @@ db_datalog_select(Connection,LA,DbGoal):-
c_db_my_query(SQL,ResultSet,Con,Mode,_), c_db_my_query(SQL,ResultSet,Con,Mode,_),
c_db_my_table_write(ResultSet). 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).
#endif MYDDAS_TOP_LEVEL #endif MYDDAS_TOP_LEVEL