2005-11-22 11:25:59 +00: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_shared.c *
|
|
|
|
|
* Last rev: 22/03/05 *
|
|
|
|
|
* mods: *
|
|
|
|
|
* comments: Predicates for maintaining MYDDAS *
|
|
|
|
|
* *
|
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
2006-05-03 19:02:29 +01:00
|
|
|
|
#if defined MYDDAS_MYSQL || defined MYDDAS_ODBC
|
2005-11-22 11:25:59 +00:00
|
|
|
|
|
|
|
|
|
#include "Yap.h"
|
|
|
|
|
#include "Yatom.h"
|
|
|
|
|
#include "cut_c.h"
|
|
|
|
|
#include "myddas_util.h"
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#include <stdlib.h>
|
2006-02-05 02:11:56 +00:00
|
|
|
|
#include "myddas_structs.h"
|
2006-02-26 15:53:31 +00:00
|
|
|
|
#ifdef MYDDAS_STATS
|
2006-01-17 22:54:02 +00:00
|
|
|
|
#include "myddas_statistics.h"
|
|
|
|
|
#endif
|
2005-11-22 11:25:59 +00:00
|
|
|
|
|
2006-03-09 15:52:06 +00:00
|
|
|
|
//STATIC_PROTO(Int c_db_get_new_table_name,(void));
|
|
|
|
|
STATIC_PROTO(Int c_db_connection_type,(void));
|
|
|
|
|
STATIC_PROTO(Int c_db_add_preds,(void));
|
|
|
|
|
STATIC_PROTO(Int c_db_preds_conn_start ,(void));
|
|
|
|
|
STATIC_PROTO(Int c_db_preds_conn_continue ,(void));
|
|
|
|
|
STATIC_PROTO(Int c_db_connection_start ,(void));
|
|
|
|
|
STATIC_PROTO(Int c_db_connection_continue ,(void));
|
|
|
|
|
STATIC_PROTO(Int c_db_check_if_exists_pred,(void));
|
|
|
|
|
STATIC_PROTO(Int c_db_delete_predicate,(void));
|
|
|
|
|
STATIC_PROTO(Int c_db_multi_queries_number,(void));
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#ifdef MYDDAS_STATS
|
2006-03-09 15:52:06 +00:00
|
|
|
|
STATIC_PROTO(Int c_db_stats,(void));
|
2006-05-03 19:02:29 +01:00
|
|
|
|
STATIC_PROTO(Int c_db_stats_walltime,(void));
|
|
|
|
|
STATIC_PROTO(Int c_db_stats_translate,(void));
|
|
|
|
|
STATIC_PROTO(Int c_db_stats_time,(void));
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#endif
|
2005-11-22 11:25:59 +00:00
|
|
|
|
#ifdef DEBUG
|
2006-03-09 15:52:06 +00:00
|
|
|
|
STATIC_PROTO(Int c_db_check,(void));
|
2005-11-22 11:25:59 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2006-02-10 23:42:19 +00:00
|
|
|
|
void Yap_InitMYDDAS_SharedPreds(void)
|
|
|
|
|
{
|
|
|
|
|
/* c_db_connection_type: Connection x Type */
|
|
|
|
|
Yap_InitCPred("c_db_connection_type",2,c_db_connection_type, 0);
|
|
|
|
|
|
|
|
|
|
/* CORRECT THIS: db_add_preds : PredName * Arity * Connection */
|
|
|
|
|
Yap_InitCPred("c_db_add_preds",4,c_db_add_preds, 0);
|
|
|
|
|
|
|
|
|
|
/* c_db_check_if_exists_pred : PredName * Arity * Connection */
|
|
|
|
|
Yap_InitCPred("c_db_check_if_exists_pred",3,c_db_check_if_exists_pred, 0);
|
2005-12-19 13:48:42 +00:00
|
|
|
|
|
2006-02-10 23:42:19 +00:00
|
|
|
|
/* c_db_delete_pred : Module * PredName * Arity */
|
|
|
|
|
Yap_InitCPred("c_db_delete_predicate",3,c_db_delete_predicate, 0);
|
2005-12-19 13:48:42 +00:00
|
|
|
|
|
2006-02-10 23:42:19 +00:00
|
|
|
|
/* c_db_delete_pred : Module * PredName * Arity */
|
|
|
|
|
Yap_InitCPred("c_db_multi_queries_number",2,c_db_multi_queries_number, 0);
|
2005-12-19 13:48:42 +00:00
|
|
|
|
|
2006-02-10 23:42:19 +00:00
|
|
|
|
#ifdef MYDDAS_STATS
|
2006-05-03 19:02:29 +01:00
|
|
|
|
/* c_db_stats: Connection * Stats */
|
2006-02-10 23:42:19 +00:00
|
|
|
|
Yap_InitCPred("c_db_stats",2, c_db_stats, 0);
|
2006-05-03 19:02:29 +01:00
|
|
|
|
|
|
|
|
|
/* c_db_stats_walltime */
|
|
|
|
|
Yap_InitCPred("c_db_stats_walltime",1, c_db_stats_walltime, 0);
|
|
|
|
|
|
|
|
|
|
/* c_db_stats_translate */
|
|
|
|
|
Yap_InitCPred("c_db_stats_translate",2,c_db_stats_translate, 0);
|
|
|
|
|
|
|
|
|
|
/* c_db_stats_time */
|
|
|
|
|
Yap_InitCPred("c_db_stats_time",2,c_db_stats_time, 0);
|
2006-02-10 23:42:19 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
Yap_InitCPred("c_db_check",0, c_db_check, 0);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Yap_InitBackMYDDAS_SharedPreds(void)
|
|
|
|
|
{
|
|
|
|
|
/* Gives all the predicates associated to a given connection */
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Yap_InitCPredBack("c_db_preds_conn", 4, sizeof(Int),
|
2006-02-10 23:42:19 +00:00
|
|
|
|
c_db_preds_conn_start,
|
|
|
|
|
c_db_preds_conn_continue, 0);
|
|
|
|
|
/* Gives all the connections stored on the MYDDAS Structure*/
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Yap_InitCPredBack("c_db_connection", 1, sizeof(Int),
|
2006-02-10 23:42:19 +00:00
|
|
|
|
c_db_connection_start,
|
|
|
|
|
c_db_connection_continue, 0);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2005-12-19 13:48:42 +00:00
|
|
|
|
|
|
|
|
|
/* Gives the type of a given connection,
|
|
|
|
|
in other words, type will be mysql or odbc
|
|
|
|
|
|
|
|
|
|
NOTE: In order to use this predicate, the connection*/
|
|
|
|
|
/* c_db_connection_type: +Connection * ?Type */
|
2006-03-09 15:52:06 +00:00
|
|
|
|
static Int
|
2005-12-19 13:48:42 +00:00
|
|
|
|
c_db_connection_type (void){
|
|
|
|
|
Term arg_con = Deref(ARG1);
|
|
|
|
|
Term arg_type = Deref(ARG2);
|
|
|
|
|
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Int *con = (Int *) IntegerOfTerm(arg_con);
|
|
|
|
|
Int type = myddas_util_connection_type(con);
|
2005-12-19 13:48:42 +00:00
|
|
|
|
|
|
|
|
|
if (type == 1) /* MYSQL Connection */
|
|
|
|
|
Yap_unify(arg_type, MkAtomTerm(Yap_LookupAtom("mysql")));
|
|
|
|
|
else if (type ==2) /* ODBC Connection */
|
|
|
|
|
Yap_unify(arg_type, MkAtomTerm(Yap_LookupAtom("odbc")));
|
|
|
|
|
else /* Not a valid connection*/
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
2005-11-22 11:25:59 +00:00
|
|
|
|
|
|
|
|
|
/* db_add_preds: PredName * Arity * Module * Connection*/
|
2006-03-09 15:52:06 +00:00
|
|
|
|
static Int
|
2005-11-22 11:25:59 +00:00
|
|
|
|
c_db_add_preds (void){
|
|
|
|
|
Term arg_nome = Deref(ARG1);
|
|
|
|
|
Term arg_aridade = Deref(ARG2);
|
|
|
|
|
Term arg_module = Deref(ARG3);
|
|
|
|
|
Term arg_conn = Deref(ARG4);
|
|
|
|
|
|
2006-04-30 11:07:44 +01:00
|
|
|
|
/* PredEntry *pe; */
|
|
|
|
|
/* pe = RepPredProp(PredPropByFunc(FunctorOfTerm(arg_pred),arg_module)); */
|
|
|
|
|
|
|
|
|
|
|
2005-11-22 11:25:59 +00:00
|
|
|
|
char *nome = AtomName(AtomOfTerm(arg_nome));
|
|
|
|
|
char *module = AtomName(AtomOfTerm(arg_module));
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Int aridade = IntegerOfTerm(arg_aridade);
|
|
|
|
|
Int *conn = (Int *) IntegerOfTerm(arg_conn);
|
2005-11-22 11:25:59 +00:00
|
|
|
|
|
|
|
|
|
if (myddas_util_add_predicate(nome,aridade,module,conn) == NULL)
|
|
|
|
|
{
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#ifdef DEBUG
|
2006-02-10 23:42:19 +00:00
|
|
|
|
printf ("ERROR : Could not add Predicate: Line: %d File: %s\n",__LINE__,__FILE__);
|
2006-04-30 11:07:44 +01:00
|
|
|
|
#endif
|
2005-11-22 11:25:59 +00:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-17 22:54:02 +00:00
|
|
|
|
|
2006-03-09 15:52:06 +00:00
|
|
|
|
static Int
|
2005-11-22 11:25:59 +00:00
|
|
|
|
c_db_check_if_exists_pred (void){
|
|
|
|
|
Term arg_nome = Deref(ARG1);
|
|
|
|
|
Term arg_aridade = Deref(ARG2);
|
|
|
|
|
Term arg_module = Deref(ARG3);
|
|
|
|
|
|
2006-04-30 11:07:44 +01:00
|
|
|
|
|
2005-11-22 11:25:59 +00:00
|
|
|
|
char *nome = AtomName(AtomOfTerm(arg_nome));
|
|
|
|
|
char *module = AtomName(AtomOfTerm(arg_module));
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Int aridade = IntegerOfTerm(arg_aridade);
|
2005-11-22 11:25:59 +00:00
|
|
|
|
|
|
|
|
|
if (myddas_util_search_predicate(nome,aridade,module) == NULL)
|
|
|
|
|
return FALSE;
|
|
|
|
|
else
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-03-09 15:52:06 +00:00
|
|
|
|
static Int
|
2006-01-17 22:54:02 +00:00
|
|
|
|
c_db_delete_predicate(void){
|
|
|
|
|
Term arg_module = Deref(ARG1);
|
|
|
|
|
Term arg_name = Deref(ARG2);
|
|
|
|
|
Term arg_arity = Deref(ARG3);
|
|
|
|
|
|
|
|
|
|
char *module = AtomName(AtomOfTerm(arg_module));
|
|
|
|
|
char *name = AtomName(AtomOfTerm(arg_name));
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Int arity = IntegerOfTerm(arg_arity);
|
2006-01-17 22:54:02 +00:00
|
|
|
|
|
|
|
|
|
MYDDAS_UTIL_PREDICATE predicate =
|
|
|
|
|
myddas_util_search_predicate(name,arity,module);
|
|
|
|
|
if (predicate == NULL)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
myddas_util_delete_predicate(predicate);
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-03-09 15:52:06 +00:00
|
|
|
|
static Int
|
2006-01-17 22:54:02 +00:00
|
|
|
|
c_db_multi_queries_number(void){
|
|
|
|
|
Term arg_conn = Deref(ARG1);
|
|
|
|
|
Term arg_number = Deref(ARG2);
|
|
|
|
|
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Int *conn = (Int *) IntegerOfTerm(arg_conn);
|
2006-01-17 22:54:02 +00:00
|
|
|
|
MYDDAS_UTIL_CONNECTION node =
|
|
|
|
|
myddas_util_search_connection(conn);
|
|
|
|
|
|
|
|
|
|
if (node == NULL)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
if (IsVarTerm(arg_number)){
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Yap_unify(arg_number,MkIntegerTerm(((Int)myddas_util_get_total_multi_queries_number(node))+1));
|
2006-01-17 22:54:02 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Int number = IntegerOfTerm(arg_number);
|
2006-01-17 22:54:02 +00:00
|
|
|
|
number--;
|
|
|
|
|
myddas_util_set_total_multi_queries_number(node,number);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
}
|
2006-02-10 23:42:19 +00:00
|
|
|
|
|
2006-03-09 15:52:06 +00:00
|
|
|
|
static Int
|
2006-02-10 23:42:19 +00:00
|
|
|
|
c_db_connection_start(void){
|
|
|
|
|
|
|
|
|
|
MYDDAS_UTIL_CONNECTION node =
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_connections;
|
2006-02-10 23:42:19 +00:00
|
|
|
|
|
2006-03-09 15:52:06 +00:00
|
|
|
|
EXTRA_CBACK_ARG(1,1)=(CELL) MkIntegerTerm((Int)node);
|
2006-02-10 23:42:19 +00:00
|
|
|
|
|
|
|
|
|
return (c_db_connection_continue());
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-09 15:52:06 +00:00
|
|
|
|
static Int
|
2006-02-10 23:42:19 +00:00
|
|
|
|
c_db_connection_continue(void){
|
|
|
|
|
Term arg_conn = Deref(ARG1);
|
|
|
|
|
|
|
|
|
|
MYDDAS_UTIL_CONNECTION node;
|
|
|
|
|
node = (MYDDAS_UTIL_CONNECTION) IntegerOfTerm(EXTRA_CBACK_ARG(1,1));
|
|
|
|
|
|
|
|
|
|
/* There is no connections */
|
|
|
|
|
if (node == NULL)
|
|
|
|
|
{
|
|
|
|
|
cut_fail();
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Yap_unify(arg_conn, MkIntegerTerm((Int)(node->connection)));
|
|
|
|
|
EXTRA_CBACK_ARG(1,1)=(CELL) MkIntegerTerm((Int)(node->next));
|
2006-02-10 23:42:19 +00:00
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-22 11:25:59 +00:00
|
|
|
|
/* db_preds_conn : Connection(+) * Pred_name(-) * Pred_arity */
|
2006-03-09 15:52:06 +00:00
|
|
|
|
static Int
|
2005-11-22 11:25:59 +00:00
|
|
|
|
c_db_preds_conn_start (void){
|
|
|
|
|
Term arg_conn = Deref(ARG1);
|
2006-02-10 23:42:19 +00:00
|
|
|
|
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Int *conn = (Int *) IntegerOfTerm(arg_conn);
|
2005-11-22 11:25:59 +00:00
|
|
|
|
MYDDAS_UTIL_CONNECTION node =
|
|
|
|
|
myddas_util_search_connection(conn);
|
|
|
|
|
|
|
|
|
|
/* Caso a ligacao j<> tenha sido apagada*/
|
|
|
|
|
if (node == NULL)
|
|
|
|
|
{
|
|
|
|
|
cut_fail();
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void *pointer = myddas_util_get_list_pred(node);
|
2006-03-09 15:52:06 +00:00
|
|
|
|
EXTRA_CBACK_ARG(4,1)=(CELL) MkIntegerTerm((Int)pointer);
|
2005-11-22 11:25:59 +00:00
|
|
|
|
|
2006-02-10 23:42:19 +00:00
|
|
|
|
return (c_db_preds_conn_continue());
|
2005-11-22 11:25:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* db_preds_conn : Connection(+) * Pred_name(-) * Pred_arity*/
|
2006-03-09 15:52:06 +00:00
|
|
|
|
static Int
|
2005-11-22 11:25:59 +00:00
|
|
|
|
c_db_preds_conn_continue (void){
|
2005-12-19 13:48:42 +00:00
|
|
|
|
Term module = Deref(ARG2);
|
2006-02-10 23:42:19 +00:00
|
|
|
|
Term name = Deref(ARG3);
|
|
|
|
|
Term arity = Deref(ARG4);
|
2005-11-22 11:25:59 +00:00
|
|
|
|
|
|
|
|
|
void *pointer;
|
2005-12-19 13:48:42 +00:00
|
|
|
|
pointer = (void *) IntegerOfTerm(EXTRA_CBACK_ARG(4,1));
|
2005-11-22 11:25:59 +00:00
|
|
|
|
|
|
|
|
|
if (pointer != NULL)
|
|
|
|
|
{
|
2006-03-09 15:52:06 +00:00
|
|
|
|
EXTRA_CBACK_ARG(4,1)=(CELL) MkIntegerTerm((Int)myddas_util_get_pred_next(pointer));
|
2006-02-10 23:42:19 +00:00
|
|
|
|
|
|
|
|
|
if (!Yap_unify(module, MkAtomTerm(Yap_LookupAtom(myddas_util_get_pred_module(pointer))))){
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
if (!Yap_unify(name,MkAtomTerm(Yap_LookupAtom(myddas_util_get_pred_name(pointer))))){
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2006-03-09 15:52:06 +00:00
|
|
|
|
if (!Yap_unify(arity, MkIntegerTerm((Int)myddas_util_get_pred_arity(pointer)))){
|
2006-02-10 23:42:19 +00:00
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2005-11-22 11:25:59 +00:00
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
cut_fail();
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2005-12-19 13:48:42 +00:00
|
|
|
|
|
|
|
|
|
|
2005-11-22 11:25:59 +00:00
|
|
|
|
#ifdef DEBUG
|
2006-03-09 15:52:06 +00:00
|
|
|
|
static Int
|
2005-11-22 11:25:59 +00:00
|
|
|
|
c_db_check(void){
|
|
|
|
|
check_int();
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
#endif /*DEBUG*/
|
|
|
|
|
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#ifdef MYDDAS_STATS
|
2006-05-03 19:02:29 +01:00
|
|
|
|
|
|
|
|
|
static Int
|
|
|
|
|
c_db_stats_walltime(void){
|
|
|
|
|
Term arg_time = Deref(ARG1);
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
if (IsVarTerm(arg_time)){
|
|
|
|
|
#endif
|
|
|
|
|
Yap_unify(arg_time,MkIntegerTerm((Int)myddas_stats_walltime()));
|
|
|
|
|
return TRUE;
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
printf ("ERROR: c_db_stats_walltime got a variable\n");
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
c_db_stats_translate(void){
|
|
|
|
|
Term arg_start = Deref(ARG1);
|
|
|
|
|
Term arg_end = Deref(ARG2);
|
|
|
|
|
|
|
|
|
|
MYDDAS_STATS_TIME start;
|
|
|
|
|
MYDDAS_STATS_TIME end;
|
|
|
|
|
|
|
|
|
|
MYDDAS_STATS_TIME total_time,diff;
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
//Both args must be instanciated
|
|
|
|
|
if (IsNonVarTerm(arg_start) && IsNonVarTerm(arg_end)){
|
|
|
|
|
#endif
|
|
|
|
|
start = (MYDDAS_STATS_TIME) IntegerOfTerm(arg_start);
|
|
|
|
|
end = (MYDDAS_STATS_TIME) IntegerOfTerm(arg_end);
|
|
|
|
|
|
|
|
|
|
MYDDAS_STATS_GET_TRANSLATE(total_time);
|
|
|
|
|
|
|
|
|
|
MYDDAS_STATS_INITIALIZE_TIME_STRUCT(diff,time_copy);
|
|
|
|
|
myddas_stats_subtract_time(diff,end,start);
|
|
|
|
|
|
|
|
|
|
diff = myddas_stats_time_copy_to_final(diff);
|
|
|
|
|
myddas_stats_add_time(total_time,diff,total_time);
|
|
|
|
|
|
|
|
|
|
free(diff);
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
printf ("ERROR: c_db_stats_translate got a variable\n");
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Int
|
|
|
|
|
c_db_stats_time(void){
|
|
|
|
|
Term arg_reference = Deref(ARG1);
|
|
|
|
|
Term arg_time = Deref(ARG2);
|
|
|
|
|
|
|
|
|
|
MYDDAS_STATS_TIME time = (MYDDAS_STATS_TIME )IntegerOfTerm(arg_reference);
|
|
|
|
|
|
|
|
|
|
Functor functor = Yap_MkFunctor(Yap_LookupAtom("myddas_time"),5);
|
|
|
|
|
Term time_numbers[5];
|
|
|
|
|
|
|
|
|
|
int time_number;
|
|
|
|
|
|
|
|
|
|
time_number = MYDDAS_STATS_TIME_HOURS(time);
|
|
|
|
|
time_numbers[0] = MkIntegerTerm(time_number);
|
|
|
|
|
time_number = MYDDAS_STATS_TIME_MINUTES(time);
|
|
|
|
|
time_numbers[1] = MkIntegerTerm(time_number);
|
|
|
|
|
time_number = MYDDAS_STATS_TIME_SECONDS(time);
|
|
|
|
|
time_numbers[2] = MkIntegerTerm(time_number);
|
|
|
|
|
time_number = MYDDAS_STATS_TIME_MILISECONDS(time);
|
|
|
|
|
time_numbers[3] = MkIntegerTerm(time_number);
|
|
|
|
|
time_number = MYDDAS_STATS_TIME_MICROSECONDS(time);
|
|
|
|
|
time_numbers[4] = MkIntegerTerm(time_number);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!Yap_unify(arg_time, Yap_MkApplTerm(functor,5,time_numbers))){
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2005-12-19 13:48:42 +00:00
|
|
|
|
//Returns the stats of this module in a list
|
2006-03-09 15:52:06 +00:00
|
|
|
|
static Int
|
2005-12-19 13:48:42 +00:00
|
|
|
|
c_db_stats(void) {
|
|
|
|
|
Term arg_conn = Deref(ARG1);
|
|
|
|
|
Term arg_list = Deref(ARG2);
|
|
|
|
|
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Int *conn = (Int *) (IntegerOfTerm(arg_conn));
|
2005-12-19 13:48:42 +00:00
|
|
|
|
|
2006-01-08 16:12:42 +00:00
|
|
|
|
// TODO
|
|
|
|
|
if (get_myddas_top() == 0 ){ /* We want all the statistics */
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2005-12-19 13:48:42 +00:00
|
|
|
|
|
|
|
|
|
MYDDAS_UTIL_CONNECTION
|
|
|
|
|
node = myddas_util_search_connection(conn);
|
|
|
|
|
Term head, list;
|
|
|
|
|
list = arg_list;
|
2006-02-05 02:11:56 +00:00
|
|
|
|
|
|
|
|
|
MYDDAS_STATS_TIME time;
|
2006-03-09 15:52:06 +00:00
|
|
|
|
UInt number;
|
2006-02-05 02:11:56 +00:00
|
|
|
|
|
2005-12-19 13:48:42 +00:00
|
|
|
|
|
|
|
|
|
//[Index 1] -> Total Number of Rows by connection
|
|
|
|
|
//Total number of Rows returned by the server
|
|
|
|
|
//WARNING: only works with store_result
|
|
|
|
|
head = HeadOfTerm(list);
|
|
|
|
|
list = TailOfTerm(list);
|
2006-02-05 02:11:56 +00:00
|
|
|
|
MYDDAS_STATS_CON_GET_TOTAL_ROWS(node,number);
|
|
|
|
|
Yap_unify(head, MkIntegerTerm(number));
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#ifdef DEBUG
|
2006-02-05 02:11:56 +00:00
|
|
|
|
printf ("Total Number of Rows returned from the Server\n");
|
2006-03-09 15:52:06 +00:00
|
|
|
|
printf ("%lu\n\n",(unsigned long)number);
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//[Index 2] -> Total of Time Spent by the DB Server
|
|
|
|
|
// processing all the SQL Querys
|
|
|
|
|
head = HeadOfTerm(list);
|
|
|
|
|
list = TailOfTerm(list);
|
2006-02-05 02:11:56 +00:00
|
|
|
|
MYDDAS_STATS_CON_GET_TOTAL_TIME_DBSERVER(node,time);
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Yap_unify(head, MkIntegerTerm((Int)time));
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#ifdef DEBUG
|
2006-02-05 02:11:56 +00:00
|
|
|
|
printf ("Reference to time Spent by the Server, on all the SQL Querys\n");
|
|
|
|
|
MYDDAS_STATS_PRINT_TIME_STRUCT(time);
|
|
|
|
|
printf ("\n\n");
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//[Index 3] -> Total of Time Spent by the DB Server
|
|
|
|
|
// processing a the last SQL Query
|
|
|
|
|
head = HeadOfTerm(list);
|
|
|
|
|
list = TailOfTerm(list);
|
2006-02-05 02:11:56 +00:00
|
|
|
|
MYDDAS_STATS_CON_GET_LAST_TIME_DBSERVER(node,time);
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Yap_unify(head, MkIntegerTerm((Int)time));
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#ifdef DEBUG
|
2006-02-05 02:11:56 +00:00
|
|
|
|
printf ("Reference to time Spent by the Server, on the last SQL Query\n");
|
|
|
|
|
MYDDAS_STATS_PRINT_TIME_STRUCT(time);
|
|
|
|
|
printf ("\n\n");
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//[Index 4] -> Total of Time Spent by the DB Server
|
|
|
|
|
// transfering all the results of the SQL Querys
|
|
|
|
|
head = HeadOfTerm(list);
|
|
|
|
|
list = TailOfTerm(list);
|
2006-02-05 02:11:56 +00:00
|
|
|
|
MYDDAS_STATS_CON_GET_TOTAL_TIME_TRANSFERING(node,time);
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Yap_unify(head, MkIntegerTerm((Int)time));
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#ifdef DEBUG
|
2006-02-05 02:11:56 +00:00
|
|
|
|
printf ("Refence to time Spent by the Server, transfering all the results SQL Query\n");
|
|
|
|
|
MYDDAS_STATS_PRINT_TIME_STRUCT(time);
|
|
|
|
|
printf ("\n\n");
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//[Index 5] -> Total of Time Spent by the DB Server
|
|
|
|
|
// transfering the result of the last SQL Query
|
|
|
|
|
head = HeadOfTerm(list);
|
|
|
|
|
list = TailOfTerm(list);
|
2006-02-05 02:11:56 +00:00
|
|
|
|
MYDDAS_STATS_CON_GET_LAST_TIME_TRANSFERING(node,time);
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Yap_unify(head, MkIntegerTerm((Int)time));
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#ifdef DEBUG
|
2006-02-05 02:11:56 +00:00
|
|
|
|
printf ("Reference to time Spent by the Server, transfering the result of the last SQL Query\n");
|
|
|
|
|
MYDDAS_STATS_PRINT_TIME_STRUCT(time);
|
|
|
|
|
printf ("\n\n");
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//[Index 6] -> Total of Time Spent by the
|
|
|
|
|
// db_row_function
|
|
|
|
|
head = HeadOfTerm(list);
|
|
|
|
|
list = TailOfTerm(list);
|
2006-02-05 02:11:56 +00:00
|
|
|
|
MYDDAS_STATS_GET_DB_ROW_FUNCTION(time);
|
2006-03-09 15:52:06 +00:00
|
|
|
|
Yap_unify(head, MkIntegerTerm((Int)time));
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#ifdef DEBUG
|
2006-02-05 02:11:56 +00:00
|
|
|
|
printf ("Reference to time Spent by the db_row_function\n");
|
|
|
|
|
MYDDAS_STATS_PRINT_TIME_STRUCT(time);
|
|
|
|
|
printf ("\n\n");
|
2005-12-19 13:48:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
2006-01-08 16:12:42 +00:00
|
|
|
|
//[Index 7] -> Total of Bytes Transfered by the
|
|
|
|
|
// DB Server on all SQL Querys
|
|
|
|
|
head = HeadOfTerm(list);
|
|
|
|
|
list = TailOfTerm(list);
|
2006-02-05 02:11:56 +00:00
|
|
|
|
MYDDAS_STATS_CON_GET_TOTAL_BYTES_TRANSFERING_FROM_DBSERVER(node,number);
|
|
|
|
|
Yap_unify(head, MkIntegerTerm(number));
|
2006-01-08 16:12:42 +00:00
|
|
|
|
#ifdef DEBUG
|
2006-02-05 02:11:56 +00:00
|
|
|
|
printf ("Bytes Transfered by the DB Server from all querys\n");
|
2006-03-09 15:52:06 +00:00
|
|
|
|
printf ("%lu\n\n",(unsigned long)number);
|
2006-01-08 16:12:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//[Index 8] -> Total of Bytes Transfered by the
|
|
|
|
|
// DB Server on the last SQL Query
|
|
|
|
|
head = HeadOfTerm(list);
|
|
|
|
|
list = TailOfTerm(list);
|
2006-02-05 02:11:56 +00:00
|
|
|
|
MYDDAS_STATS_CON_GET_LAST_BYTES_TRANSFERING_FROM_DBSERVER(node,number);
|
|
|
|
|
Yap_unify(head, MkIntegerTerm(number));
|
2006-01-08 16:12:42 +00:00
|
|
|
|
#ifdef DEBUG
|
2006-02-05 02:11:56 +00:00
|
|
|
|
printf ("Bytes Transfered by the DB Server on the last query\n");
|
2006-03-09 15:52:06 +00:00
|
|
|
|
printf ("%lu\n\n",(unsigned long)number);
|
2006-01-08 16:12:42 +00:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
//[Index 9] -> Number of querys made to the DBserver
|
|
|
|
|
head = HeadOfTerm(list);
|
|
|
|
|
list = TailOfTerm(list);
|
2006-02-05 02:11:56 +00:00
|
|
|
|
MYDDAS_STATS_CON_GET_NUMBER_QUERIES_MADE(node,number);
|
|
|
|
|
Yap_unify(head, MkIntegerTerm(number));
|
2006-01-08 16:12:42 +00:00
|
|
|
|
#ifdef DEBUG
|
2006-02-05 02:11:56 +00:00
|
|
|
|
printf ("Number of Querys made to the server\n");
|
2006-03-09 15:52:06 +00:00
|
|
|
|
printf ("%lu\n\n",(unsigned long)number);
|
2006-01-08 16:12:42 +00:00
|
|
|
|
#endif
|
2006-05-03 19:02:29 +01:00
|
|
|
|
|
|
|
|
|
//[Index 10] -> Total of Time Spent by the
|
|
|
|
|
// translate predicate
|
|
|
|
|
head = HeadOfTerm(list);
|
|
|
|
|
list = TailOfTerm(list);
|
|
|
|
|
MYDDAS_STATS_GET_TRANSLATE(time);
|
|
|
|
|
Yap_unify(head, MkIntegerTerm((Int)time));
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
printf ("Reference to time Spent by the translate predicate\n");
|
|
|
|
|
MYDDAS_STATS_PRINT_TIME_STRUCT(time);
|
|
|
|
|
printf ("\n\n");
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-01-08 16:12:42 +00:00
|
|
|
|
|
2005-12-19 13:48:42 +00:00
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* MYDDAS_STATS */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Function to delete all the temporary tables */
|
|
|
|
|
/* from the mysql server */
|
|
|
|
|
void Yap_MyDDAS_delete_all_myddas_structs(void)
|
|
|
|
|
{
|
|
|
|
|
//char *table_name;
|
|
|
|
|
//char query[500];
|
|
|
|
|
|
|
|
|
|
/* NAO ESQUECER DE FAZER ISTO TB PARA O DB_CLOSE*/
|
|
|
|
|
|
|
|
|
|
/* for(;(table_name = myddas_util_delete_all_temp_table()) != NULL ;) { */
|
|
|
|
|
/* printf ("%s\n",table_name); */
|
|
|
|
|
/* sprintf (query,"DROP TABLE IF EXISTS %s",table_name); */
|
|
|
|
|
/* printf ("%s\n",query); */
|
|
|
|
|
/* free(table_name); */
|
|
|
|
|
/* query[0]=0; */
|
|
|
|
|
/* } */
|
|
|
|
|
}
|
2005-11-22 11:25:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif /*CUT_C && (MYDDAS_MYSQL || MYDDAS_ODBC)*/
|