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/myddas_util.c

268 lines
7.5 KiB
C
Raw Normal View History

#include <string.h>
#include <stdlib.h>
2015-02-09 10:19:15 +00:00
#include "Yap.h"
#include "cut_c.h"
2016-01-03 02:06:09 +00:00
#include "myddas.h"
#include "myddas_util.h"
2016-01-03 02:06:09 +00:00
/* Type: MYSQL->1 ODBC->2*/
Short myddas_util_connection_type(void *con) {
2016-01-03 02:06:09 +00:00
MYDDAS_UTIL_CONNECTION con_node = myddas_util_search_connection(con);
if (con_node == NULL)
return 0;
2015-02-14 11:35:07 +00:00
return con_node->api;
2016-01-03 02:06:09 +00:00
// if (con_node->odbc_enviromment != NULL) /* ODBC */
2015-02-14 11:35:07 +00:00
// return 2;
2016-01-03 02:06:09 +00:00
// else
2015-02-14 11:35:07 +00:00
// return 1;
}
MYDDAS_UTIL_PREDICATE
2015-06-19 01:19:48 +01:00
myddas_util_search_predicate(const char *pred_name, Int pred_arity,
2016-01-03 02:06:09 +00:00
const char *pred_module) {
CACHE_REGS
2016-01-03 02:06:09 +00:00
MYDDAS_UTIL_PREDICATE pred = NULL;
MYDDAS_UTIL_CONNECTION top =
Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_connections;
2016-01-03 02:06:09 +00:00
for (; top != NULL; top = top->next) {
if ((pred = myddas_util_find_predicate(pred_name, pred_arity, pred_module,
top->predicates)))
return pred;
}
return NULL;
}
/* When using this function, we must guarante that this predicate
it's unique */
2016-01-03 02:06:09 +00:00
MYDDAS_UTIL_CONNECTION
2015-06-19 01:19:48 +01:00
myddas_util_add_predicate(const char *pred_name, Int pred_arity,
2016-01-03 02:06:09 +00:00
const char *pred_module, void *con) {
MYDDAS_UTIL_CONNECTION node_con = myddas_util_search_connection(con);
MYDDAS_UTIL_PREDICATE new = myddas_init_initialize_predicate(
pred_name, pred_arity, pred_module, node_con->predicates);
if (new == NULL) {
myddas_util_error_message("Could not initialize predicate node", __LINE__,
__FILE__);
return NULL;
}
node_con->predicates = new;
return node_con;
2016-01-03 02:06:09 +00:00
}
2016-01-03 02:06:09 +00:00
void myddas_util_delete_predicate(MYDDAS_UTIL_PREDICATE to_delete) {
CACHE_REGS
if (to_delete->next != NULL)
to_delete->next->previous = to_delete->previous;
if (to_delete->previous != NULL)
to_delete->previous->next = to_delete->next;
2016-01-03 02:06:09 +00:00
else // First predicate of the predicate list
{
MYDDAS_UTIL_CONNECTION con_node =
Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_connections;
for (; con_node != NULL; con_node = con_node->next)
if (con_node->predicates == to_delete)
break;
con_node->predicates = to_delete->next;
}
MYDDAS_FREE(to_delete, struct myddas_list_preds);
}
2016-01-03 02:06:09 +00:00
void myddas_util_delete_connection(void *conn) {
CACHE_REGS
MYDDAS_UTIL_CONNECTION to_delete = myddas_util_search_connection(conn);
2016-01-03 02:06:09 +00:00
if (to_delete == NULL)
return;
2016-01-03 02:06:09 +00:00
else {
/* Removes the predicates list */
myddas_util_delete_predicate_list(to_delete->predicates);
#ifdef MYDDAS_STATS
2016-01-03 02:06:09 +00:00
/* Removes the stats list */
myddas_stats_delete_stats_list(to_delete->stats);
#endif
2016-01-03 02:06:09 +00:00
/* List Integrety */
/* Is the last element of the list */
if ((to_delete->next) != NULL)
to_delete->next->previous = to_delete->previous;
/* Is the first element of the list */
if (to_delete == (Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_connections))
Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_connections = to_delete->next;
else
to_delete->previous->next = to_delete->next;
MYDDAS_FREE(to_delete, struct myddas_list_connection);
return;
}
}
2016-01-03 02:06:09 +00:00
MYDDAS_UTIL_CONNECTION
myddas_util_search_connection(void *conn) {
CACHE_REGS
2016-01-03 02:06:09 +00:00
MYDDAS_UTIL_CONNECTION list =
Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_connections;
#ifdef MYDDAS_STATS
if (conn == 0) { /* We want all the statistics */
return list;
}
#endif
2016-01-03 02:06:09 +00:00
for (; list != NULL; list = list->next)
if (list->connection == conn)
return list;
return NULL;
}
2016-01-03 02:06:09 +00:00
MYDDAS_UTIL_CONNECTION
myddas_util_add_connection(void *conn, void *enviromment, MYDDAS_API api) {
CACHE_REGS
2016-01-03 02:06:09 +00:00
MYDDAS_UTIL_CONNECTION node = NULL;
MYDDAS_UTIL_CONNECTION temp = NULL;
2016-01-03 02:06:09 +00:00
if ((node = myddas_util_search_connection(conn)) != NULL) {
return node;
}
// put the new connection node on the top of the list
temp = myddas_init_initialize_connection(
conn, enviromment, api,
Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_connections);
if (temp == NULL) {
#ifdef DEBUG
2016-01-03 02:06:09 +00:00
myddas_util_error_message("Could not initialize connection node", __LINE__,
__FILE__);
#endif
return NULL;
}
Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_connections = temp;
return Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_connections;
}
2016-01-03 02:06:09 +00:00
UInt myddas_util_get_total_multi_queries_number(MYDDAS_UTIL_CONNECTION con) {
return con->total_number_queries;
}
2016-01-03 02:06:09 +00:00
void myddas_util_set_total_multi_queries_number(MYDDAS_UTIL_CONNECTION con,
UInt number) {
con->total_number_queries = number;
}
#ifdef MYDDAS_MYSQL
/* Auxilary function to table_write*/
2016-01-03 02:06:09 +00:00
static void n_print(Int n, char c) {
for (; n > 0; n--)
printf("%c", c);
}
#endif
2016-01-03 02:06:09 +00:00
void myddas_util_error_message(char *message, Int line, char *file) {
#ifdef DEBUG
2016-01-03 02:06:09 +00:00
printf("ERROR: %s at line %d in file %s\n", message, (int)line, file);
#else
2016-01-03 02:06:09 +00:00
printf("ERROR: %s\n", message);
#endif
}
MYDDAS_UTIL_PREDICATE
2015-06-19 01:19:48 +01:00
myddas_util_find_predicate(const char *pred_name, Int pred_arity,
2016-01-03 02:06:09 +00:00
const char *pred_module,
MYDDAS_UTIL_PREDICATE list) {
2016-01-03 02:06:09 +00:00
for (; list != NULL; list = list->next)
if (pred_arity == list->pred_arity && !strcmp(pred_name, list->pred_name) &&
!strcmp(pred_module, list->pred_module))
return list;
2016-01-03 02:06:09 +00:00
return NULL;
}
2016-01-03 02:06:09 +00:00
void myddas_util_delete_predicate_list(MYDDAS_UTIL_PREDICATE preds_list) {
CACHE_REGS
MYDDAS_UTIL_PREDICATE to_delete = NULL;
2016-01-03 02:06:09 +00:00
for (; preds_list != NULL;) {
to_delete = preds_list;
preds_list = preds_list->next;
2016-01-03 02:06:09 +00:00
MYDDAS_FREE(to_delete, struct myddas_list_preds);
}
2016-01-03 02:06:09 +00:00
return;
}
2016-01-03 02:06:09 +00:00
// DELETE THIS WHEN DB_STATS IS COMPLETED
MyddasInt get_myddas_top(void) {
CACHE_REGS
if (Yap_REGS.MYDDAS_GLOBAL_POINTER == NULL)
return 0;
return (Int)Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_connections;
}
2016-01-03 02:06:09 +00:00
void *myddas_util_get_pred_next(void *pointer) {
MYDDAS_UTIL_PREDICATE temp = (MYDDAS_UTIL_PREDICATE)pointer;
return (void *)(temp->next);
}
2016-01-03 02:06:09 +00:00
MyddasInt myddas_util_get_pred_arity(void *pointer) {
MYDDAS_UTIL_PREDICATE temp = (MYDDAS_UTIL_PREDICATE)pointer;
return temp->pred_arity;
}
2016-01-03 02:06:09 +00:00
const char *myddas_util_get_pred_name(void *pointer) {
MYDDAS_UTIL_PREDICATE temp = (MYDDAS_UTIL_PREDICATE)pointer;
return temp->pred_name;
}
2016-01-03 02:06:09 +00:00
const char *myddas_util_get_pred_module(void *pointer) {
MYDDAS_UTIL_PREDICATE temp = (MYDDAS_UTIL_PREDICATE)pointer;
return temp->pred_module;
}
2016-01-03 02:06:09 +00:00
void *myddas_util_get_list_pred(MYDDAS_UTIL_CONNECTION node) {
return (void *)(node->predicates);
}
#ifdef DEBUG
2016-01-03 02:06:09 +00:00
void check_int(void) {
CACHE_REGS
2014-01-30 18:00:37 +00:00
MYDDAS_UTIL_PREDICATE pred = NULL;
2016-01-03 02:06:09 +00:00
MYDDAS_UTIL_CONNECTION top =
Yap_REGS.MYDDAS_GLOBAL_POINTER->myddas_top_connections;
for (; top != NULL; top = top->next) {
printf("***************\n");
printf("===== top =====\n");
printf("======= %p =====\n", top);
printf("CONN: = %p =====\n", top->connection);
printf("ENV : = %p =====\n", top->odbc_enviromment);
printf("PRED: = %p =====\n", top->predicates);
printf("======= %p =====\n", top->previous);
printf("======= %p =====\n", top->next);
if (top->predicates != NULL) {
printf("\t******\n");
printf("\t===== PREDICATES =====\n");
for (pred = top->predicates; pred != NULL; pred = pred->next) {
printf("\t--------------\n");
printf("\t===== %p =====\n", pred);
printf("\t===== %s =====\n", pred->pred_name);
printf("\t===== %d =====\n", pred->pred_arity);
printf("\t===== %s =====\n", pred->pred_module);
printf("\t===== %p =====\n", pred->previous);
printf("\t===== %p =====\n", pred->next);
}
}
2016-01-03 02:06:09 +00:00
}
return;
}
#endif