>From 87377b9da721aaa36b0b5ae594b68c55bef85888 Mon Sep 17 00:00:00 2001
From: Daniel Hilst <danielhilst@gmail.com>
Date: Thu, 19 Apr 2012 15:24:00 +0000
Subject: [PATCH] oracle.c: Added support for interval/database

---
 src/oracle.c |  112 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 97 insertions(+), 15 deletions(-)

diff --git a/src/oracle.c b/src/oracle.c
index 4415b48..0a63706 100644
--- a/src/oracle.c
+++ b/src/oracle.c
@@ -63,6 +63,8 @@ struct o_database_s
   char *username;
   char *password;
 
+  struct timespec *interval;
+
   udb_query_preparation_area_t **q_prep_areas;
   udb_query_t **queries;
   size_t        queries_num;
@@ -83,6 +85,13 @@ OCIEnv   *oci_env = NULL;
 OCIError *oci_error = NULL;
 
 /*
+ * Prototypes
+ */
+static int o_read_database (o_database_t *db);
+static int o_read_database_query (o_database_t *db,
+                                  udb_query_t *q, udb_query_preparation_area_t *prep_are);
+
+/*
  * Functions
  */
 static void o_report_error (const char *where, /* {{{ */
@@ -179,7 +188,6 @@ static int o_config_set_string (char **ret_string, /* {{{ */
     oconfig_item_t *ci)
 {
   char *string;
-
   if ((ci->values_num != 1)
       || (ci->values[0].type != OCONFIG_TYPE_STRING))
   {
@@ -189,6 +197,7 @@ static int o_config_set_string (char **ret_string, /* {{{ */
   }
 
   string = strdup (ci->values[0].value.string);
+
   if (string == NULL)
   {
     ERROR ("oracle plugin: strdup failed.");
@@ -201,6 +210,14 @@ static int o_config_set_string (char **ret_string, /* {{{ */
 
   return (0);
 } /* }}} int o_config_set_string */
+    
+
+
+static int o_complex_read_database (user_data_t *ud) 
+{
+  DEBUG("oracle plugin: o_complex_read_database() -- index = %d -- db = %p", *(int *)ud->data, databases[*(int *)ud->data]);
+  return o_read_database(databases[*(int*)ud->data]);
+}
 
 static int o_config_add_database (oconfig_item_t *ci) /* {{{ */
 {
@@ -245,6 +262,30 @@ static int o_config_add_database (oconfig_item_t *ci) /* {{{ */
     else if (strcasecmp ("Query", child->key) == 0)
       status = udb_query_pick_from_list (child, queries, queries_num,
           &db->queries, &db->queries_num);
+    else if (strcasecmp ("Interval", child->key) == 0) {
+      if (child->values_num < 1) {
+        status = -1;
+        ERROR ("oracle plugin: o_config_add_database() -- Interval expects one value, no value passed");
+      }
+      else if (!child->values[0].type & OCONFIG_TYPE_NUMBER)
+      {
+        status = -1;
+        ERROR ("oracle plugin: o_config_add_database() -- Interval expects one number, wrong type passed");
+      }
+      else
+      {
+        db->interval = malloc(sizeof(struct timespec));
+        if (!db->interval) {
+          ERROR ("oracle plugin: o_config_add_database() -- malloc() failed at line: %d",  __LINE__);
+          status = -1;
+          break;
+        }
+    
+        db->interval->tv_nsec = 0;
+        db->interval->tv_sec = child->values[0].value.number;
+        DEBUG("oracle plugin: o_config_add_database() -- Interval = %d secs", (int)db->interval->tv_sec);
+      }
+    }
     else
     {
       WARNING ("oracle plugin: Option `%s' not allowed here.", child->key);
@@ -320,12 +361,47 @@ static int o_config_add_database (oconfig_item_t *ci) /* {{{ */
     }
     else
     {
-      databases = temp;
-      databases[databases_num] = db;
-      databases_num++;
+      user_data_t *ud = malloc(sizeof (user_data_t));
+      
+      if (ud) 
+      {
+        ud->free_func = NULL;
+        ud->data = malloc(sizeof(int));
+        if (ud->data) 
+        {
+          *(int *)ud->data = databases_num;
+          char addrs[20];
+          snprintf(addrs, 20, "oracle%d", (int)databases_num);
+          plugin_register_complex_read("oracle",
+                                       addrs, 
+                                       o_complex_read_database, 
+                                       (db->interval ? db->interval : NULL), 
+                                       ud);        
+
+          databases = temp;
+          databases[databases_num] = db;
+          databases_num++;
+          DEBUG("oracle plugin: o_config_add_database() -- db = %p added to databases", db);
+        }
+        else 
+        {
+          ERROR("oracle plugin: o_config_add_database() -- malloc() failed at %d", __LINE__);
+          status = -1;
+          free(ud);
+        }
+      }
+      else 
+      {
+        ERROR("oracle plugin: o_config_add_database() -- malloc() failed at %d", __LINE__);          
+        status = -1;
+      }
+      
+
+      
     }
   }
 
+
   if (status != 0)
   {
     o_database_free (db);
@@ -649,9 +725,12 @@ static int o_read_database_query (o_database_t *db, /* {{{ */
 
 static int o_read_database (o_database_t *db) /* {{{ */
 {
+
   size_t i;
   int status;
 
+  DEBUG("oracle plugin: o_read_database() -- reading db = %p", db);
+  
   if (db->oci_service_context != NULL)
   {
     OCIServer *server_handle;
@@ -724,16 +803,19 @@ static int o_read_database (o_database_t *db) /* {{{ */
   return (0);
 } /* }}} int o_read_database */
 
-static int o_read (void) /* {{{ */
-{
-  size_t i;
-
-  for (i = 0; i < databases_num; i++)
-    o_read_database (databases[i]);
-
-  return (0);
-} /* }}} int o_read */
-
+/*
+ * static int o_read (void) 
+ * {
+ *   size_t i;
+ * 
+ *   for (i = 0; i < databases_num; i++)
+ *     o_read_database (databases[i]);
+ * 
+ *   return (0);
+ * }
+ *     
+ */
+    
 static int o_shutdown (void) /* {{{ */
 {
   size_t i;
@@ -771,7 +853,7 @@ void module_register (void) /* {{{ */
 {
   plugin_register_complex_config ("oracle", o_config);
   plugin_register_init ("oracle", o_init);
-  plugin_register_read ("oracle", o_read);
+  /* plugin_register_read ("oracle", o_read); */
   plugin_register_shutdown ("oracle", o_shutdown);
 } /* }}} void module_register */
 
-- 
1.7.3.4

