Merge branch 'collectd-4.10' into collectd-5.0
[collectd.git] / src / oracle.c
1 /**
2  * collectd - src/oracle.c
3  * Copyright (C) 2008,2009  noris network AG
4  * Copyright (C) 2012       Florian octo Forster
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the License is applicable.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Linking src/oracle.c ("the oracle plugin") statically or dynamically with
20  * other modules is making a combined work based on the oracle plugin. Thus,
21  * the terms and conditions of the GNU General Public License cover the whole
22  * combination.
23  *
24  * In addition, as a special exception, the copyright holders of the oracle
25  * plugin give you permission to combine the oracle plugin with free software
26  * programs or libraries that are released under the GNU LGPL and with code
27  * included in the standard release of the Oracle® Call Interface (OCI) under
28  * the Oracle® Technology Network (OTN) License (or modified versions of such
29  * code, with unchanged license). You may copy and distribute such a system
30  * following the terms of the GNU GPL for the oracle plugin and the licenses of
31  * the other code concerned.
32  *
33  * Note that people who make modified versions of the oracle plugin are not
34  * obligated to grant this special exception for their modified versions; it is
35  * their choice whether to do so. The GNU General Public License gives
36  * permission to release a modified version without this exception; this
37  * exception also makes it possible to release a modified version which carries
38  * forward this exception. However, without this exception the OTN License does
39  * not allow linking with code licensed under the GNU General Public License.
40  *
41  * Oracle® is a registered trademark of Oracle Corporation and/or its
42  * affiliates. Other names may be trademarks of their respective owners.
43  *
44  * Authors:
45  *   Florian octo Forster <octo at collectd.org>
46  **/
47
48 #include "collectd.h"
49 #include "common.h"
50 #include "plugin.h"
51 #include "configfile.h"
52 #include "utils_db_query.h"
53
54 #include <oci.h>
55
56 /*
57  * Data types
58  */
59 struct o_database_s
60 {
61   char *name;
62   char *connect_id;
63   char *username;
64   char *password;
65
66   udb_query_preparation_area_t **q_prep_areas;
67   udb_query_t **queries;
68   size_t        queries_num;
69
70   OCISvcCtx *oci_service_context;
71 };
72 typedef struct o_database_s o_database_t;
73
74 /*
75  * Global variables
76  */
77 static udb_query_t  **queries       = NULL;
78 static size_t         queries_num   = 0;
79 static o_database_t **databases     = NULL;
80 static size_t         databases_num = 0;
81
82 OCIEnv   *oci_env = NULL;
83 OCIError *oci_error = NULL;
84
85 /*
86  * Functions
87  */
88 static void o_report_error (const char *where, /* {{{ */
89     const char *what, OCIError *eh)
90 {
91   char buffer[2048];
92   sb4 error_code;
93   int status;
94   unsigned int record_number;
95
96   /* An operation may cause / return multiple errors. Loop until we have
97    * handled all errors available (with a fail-save limit of 16). */
98   for (record_number = 1; record_number <= 16; record_number++)
99   {
100     memset (buffer, 0, sizeof (buffer));
101     error_code = -1;
102
103     status = OCIErrorGet (eh, (ub4) record_number,
104         /* sqlstate = */ NULL,
105         &error_code,
106         (text *) &buffer[0],
107         (ub4) sizeof (buffer),
108         OCI_HTYPE_ERROR);
109     buffer[sizeof (buffer) - 1] = 0;
110
111     if (status == OCI_NO_DATA)
112       return;
113
114     if (status == OCI_SUCCESS)
115     {
116       size_t buffer_length;
117
118       buffer_length = strlen (buffer);
119       while ((buffer_length > 0) && (buffer[buffer_length - 1] < 32))
120       {
121         buffer_length--;
122         buffer[buffer_length] = 0;
123       }
124
125       ERROR ("oracle plugin: %s: %s failed: %s", where, what, buffer);
126     }
127     else
128     {
129       ERROR ("oracle plugin: %s: %s failed. Additionally, OCIErrorGet failed with status %i.",
130           where, what, status);
131       return;
132     }
133   }
134 } /* }}} void o_report_error */
135
136 static void o_database_free (o_database_t *db) /* {{{ */
137 {
138   size_t i;
139
140   if (db == NULL)
141     return;
142
143   sfree (db->name);
144   sfree (db->connect_id);
145   sfree (db->username);
146   sfree (db->password);
147   sfree (db->queries);
148
149   if (db->q_prep_areas != NULL)
150     for (i = 0; i < db->queries_num; ++i)
151       udb_query_delete_preparation_area (db->q_prep_areas[i]);
152   free (db->q_prep_areas);
153
154   sfree (db);
155 } /* }}} void o_database_free */
156
157 /* Configuration handling functions {{{
158  *
159  * <Plugin oracle>
160  *   <Query "plugin_instance0">
161  *     Statement "SELECT name, value FROM table"
162  *     <Result>
163  *       Type "gauge"
164  *       InstancesFrom "name"
165  *       ValuesFrom "value"
166  *     </Result>
167  *   </Query>
168  *     
169  *   <Database "plugin_instance1">
170  *     ConnectID "db01"
171  *     Username "oracle"
172  *     Password "secret"
173  *     Query "plugin_instance0"
174  *   </Database>
175  * </Plugin>
176  */
177
178 static int o_config_set_string (char **ret_string, /* {{{ */
179     oconfig_item_t *ci)
180 {
181   char *string;
182
183   if ((ci->values_num != 1)
184       || (ci->values[0].type != OCONFIG_TYPE_STRING))
185   {
186     WARNING ("oracle plugin: The `%s' config option "
187         "needs exactly one string argument.", ci->key);
188     return (-1);
189   }
190
191   string = strdup (ci->values[0].value.string);
192   if (string == NULL)
193   {
194     ERROR ("oracle plugin: strdup failed.");
195     return (-1);
196   }
197
198   if (*ret_string != NULL)
199     free (*ret_string);
200   *ret_string = string;
201
202   return (0);
203 } /* }}} int o_config_set_string */
204
205 static int o_config_add_database (oconfig_item_t *ci) /* {{{ */
206 {
207   o_database_t *db;
208   int status;
209   int i;
210
211   if ((ci->values_num != 1)
212       || (ci->values[0].type != OCONFIG_TYPE_STRING))
213   {
214     WARNING ("oracle plugin: The `Database' block "
215         "needs exactly one string argument.");
216     return (-1);
217   }
218
219   db = (o_database_t *) malloc (sizeof (*db));
220   if (db == NULL)
221   {
222     ERROR ("oracle plugin: malloc failed.");
223     return (-1);
224   }
225   memset (db, 0, sizeof (*db));
226
227   status = o_config_set_string (&db->name, ci);
228   if (status != 0)
229   {
230     sfree (db);
231     return (status);
232   }
233
234   /* Fill the `o_database_t' structure.. */
235   for (i = 0; i < ci->children_num; i++)
236   {
237     oconfig_item_t *child = ci->children + i;
238
239     if (strcasecmp ("ConnectID", child->key) == 0)
240       status = o_config_set_string (&db->connect_id, child);
241     else if (strcasecmp ("Username", child->key) == 0)
242       status = o_config_set_string (&db->username, child);
243     else if (strcasecmp ("Password", child->key) == 0)
244       status = o_config_set_string (&db->password, child);
245     else if (strcasecmp ("Query", child->key) == 0)
246       status = udb_query_pick_from_list (child, queries, queries_num,
247           &db->queries, &db->queries_num);
248     else
249     {
250       WARNING ("oracle plugin: Option `%s' not allowed here.", child->key);
251       status = -1;
252     }
253
254     if (status != 0)
255       break;
256   }
257
258   /* Check that all necessary options have been given. */
259   while (status == 0)
260   {
261     if (db->connect_id == NULL)
262     {
263       WARNING ("oracle plugin: `ConnectID' not given for query `%s'", db->name);
264       status = -1;
265     }
266     if (db->username == NULL)
267     {
268       WARNING ("oracle plugin: `Username' not given for query `%s'", db->name);
269       status = -1;
270     }
271     if (db->password == NULL)
272     {
273       WARNING ("oracle plugin: `Password' not given for query `%s'", db->name);
274       status = -1;
275     }
276
277     break;
278   } /* while (status == 0) */
279
280   while ((status == 0) && (db->queries_num > 0))
281   {
282     db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
283         db->queries_num, sizeof (*db->q_prep_areas));
284
285     if (db->q_prep_areas == NULL)
286     {
287       WARNING ("oracle plugin: malloc failed");
288       status = -1;
289       break;
290     }
291
292     for (i = 0; i < db->queries_num; ++i)
293     {
294       db->q_prep_areas[i]
295         = udb_query_allocate_preparation_area (db->queries[i]);
296
297       if (db->q_prep_areas[i] == NULL)
298       {
299         WARNING ("oracle plugin: udb_query_allocate_preparation_area failed");
300         status = -1;
301         break;
302       }
303     }
304
305     break;
306   }
307
308   /* If all went well, add this query to the list of queries within the
309    * database structure. */
310   if (status == 0)
311   {
312     o_database_t **temp;
313
314     temp = (o_database_t **) realloc (databases,
315         sizeof (*databases) * (databases_num + 1));
316     if (temp == NULL)
317     {
318       ERROR ("oracle plugin: realloc failed");
319       status = -1;
320     }
321     else
322     {
323       databases = temp;
324       databases[databases_num] = db;
325       databases_num++;
326     }
327   }
328
329   if (status != 0)
330   {
331     o_database_free (db);
332     return (-1);
333   }
334
335   return (0);
336 } /* }}} int o_config_add_database */
337
338 static int o_config (oconfig_item_t *ci) /* {{{ */
339 {
340   int i;
341
342   for (i = 0; i < ci->children_num; i++)
343   {
344     oconfig_item_t *child = ci->children + i;
345     if (strcasecmp ("Query", child->key) == 0)
346       udb_query_create (&queries, &queries_num, child,
347           /* callback = */ NULL);
348     else if (strcasecmp ("Database", child->key) == 0)
349       o_config_add_database (child);
350     else
351     {
352       WARNING ("oracle plugin: Ignoring unknown config option `%s'.", child->key);
353     }
354
355     if (queries_num > 0)
356     {
357       DEBUG ("oracle plugin: o_config: queries_num = %zu; queries[0] = %p; udb_query_get_user_data (queries[0]) = %p;",
358           queries_num, (void *) queries[0], udb_query_get_user_data (queries[0]));
359     }
360   } /* for (ci->children) */
361
362   return (0);
363 } /* }}} int o_config */
364
365 /* }}} End of configuration handling functions */
366
367 static int o_init (void) /* {{{ */
368 {
369   int status;
370
371   if (oci_env != NULL)
372     return (0);
373
374   status = OCIEnvCreate (&oci_env,
375       /* mode = */ OCI_THREADED,
376       /* context        = */ NULL,
377       /* malloc         = */ NULL,
378       /* realloc        = */ NULL,
379       /* free           = */ NULL,
380       /* user_data_size = */ 0,
381       /* user_data_ptr  = */ NULL);
382   if (status != 0)
383   {
384     ERROR ("oracle plugin: OCIEnvCreate failed with status %i.", status);
385     return (-1);
386   }
387
388   status = OCIHandleAlloc (oci_env, (void *) &oci_error, OCI_HTYPE_ERROR,
389       /* user_data_size = */ 0, /* user_data = */ NULL);
390   if (status != OCI_SUCCESS)
391   {
392     ERROR ("oracle plugin: OCIHandleAlloc (OCI_HTYPE_ERROR) failed "
393         "with status %i.", status);
394     return (-1);
395   }
396
397   return (0);
398 } /* }}} int o_init */
399
400 static int o_read_database_query (o_database_t *db, /* {{{ */
401     udb_query_t *q, udb_query_preparation_area_t *prep_area)
402 {
403   char **column_names;
404   char **column_values;
405   size_t column_num;
406
407   OCIStmt *oci_statement;
408
409   /* List of `OCIDefine' pointers. These defines map columns to the buffer
410    * space declared above. */
411   OCIDefine **oci_defines;
412
413   int status;
414   size_t i;
415
416   oci_statement = udb_query_get_user_data (q);
417
418   /* Prepare the statement */
419   if (oci_statement == NULL) /* {{{ */
420   {
421     const char *statement;
422
423     statement = udb_query_get_statement (q);
424     assert (statement != NULL);
425
426     status = OCIHandleAlloc (oci_env, (void *) &oci_statement,
427         OCI_HTYPE_STMT, /* user_data_size = */ 0, /* user_data = */ NULL);
428     if (status != OCI_SUCCESS)
429     {
430       o_report_error ("o_read_database_query", "OCIHandleAlloc", oci_error);
431       oci_statement = NULL;
432       return (-1);
433     }
434
435     status = OCIStmtPrepare (oci_statement, oci_error,
436         (text *) statement, (ub4) strlen (statement),
437         /* language = */ OCI_NTV_SYNTAX,
438         /* mode     = */ OCI_DEFAULT);
439     if (status != OCI_SUCCESS)
440     {
441       o_report_error ("o_read_database_query", "OCIStmtPrepare", oci_error);
442       OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
443       oci_statement = NULL;
444       return (-1);
445     }
446     udb_query_set_user_data (q, oci_statement);
447
448     DEBUG ("oracle plugin: o_read_database_query (%s, %s): "
449         "Successfully allocated statement handle.",
450         db->name, udb_query_get_name (q));
451   } /* }}} */
452
453   assert (oci_statement != NULL);
454
455   /* Execute the statement */
456   status = OCIStmtExecute (db->oci_service_context, /* {{{ */
457       oci_statement,
458       oci_error,
459       /* iters = */ 0,
460       /* rowoff = */ 0,
461       /* snap_in = */ NULL, /* snap_out = */ NULL,
462       /* mode = */ OCI_DEFAULT);
463   if (status != OCI_SUCCESS)
464   {
465     DEBUG ("oracle plugin: o_read_database_query: status = %i (%#x)", status, status);
466     o_report_error ("o_read_database_query", "OCIStmtExecute", oci_error);
467     ERROR ("oracle plugin: o_read_database_query: "
468         "Failing statement was: %s", udb_query_get_statement (q));
469     return (-1);
470   } /* }}} */
471
472   /* Acquire the number of columns returned. */
473   do /* {{{ */
474   {
475     ub4 param_counter = 0;
476     status = OCIAttrGet (oci_statement, OCI_HTYPE_STMT, /* {{{ */
477         &param_counter, /* size pointer = */ NULL, 
478         OCI_ATTR_PARAM_COUNT, oci_error);
479     if (status != OCI_SUCCESS)
480     {
481       o_report_error ("o_read_database_query", "OCIAttrGet", oci_error);
482       return (-1);
483     } /* }}} */
484
485     column_num = (size_t) param_counter;
486   } while (0); /* }}} */
487
488   /* Allocate the following buffers:
489    * 
490    *  +---------------+-----------------------------------+
491    *  ! Name          ! Size                              !
492    *  +---------------+-----------------------------------+
493    *  ! column_names  ! column_num x DATA_MAX_NAME_LEN    !
494    *  ! column_values ! column_num x DATA_MAX_NAME_LEN    !
495    *  ! oci_defines   ! column_num x sizeof (OCIDefine *) !
496    *  +---------------+-----------------------------------+
497    *
498    * {{{ */
499 #define NUMBER_BUFFER_SIZE 64
500
501 #define FREE_ALL \
502   if (column_names != NULL) { \
503     sfree (column_names[0]); \
504     sfree (column_names); \
505   } \
506   if (column_values != NULL) { \
507     sfree (column_values[0]); \
508     sfree (column_values); \
509   } \
510   sfree (oci_defines)
511
512 #define ALLOC_OR_FAIL(ptr, ptr_size) \
513   do { \
514     size_t alloc_size = (size_t) ((ptr_size)); \
515     (ptr) = malloc (alloc_size); \
516     if ((ptr) == NULL) { \
517       FREE_ALL; \
518       ERROR ("oracle plugin: o_read_database_query: malloc failed."); \
519       return (-1); \
520     } \
521     memset ((ptr), 0, alloc_size); \
522   } while (0)
523
524   /* Initialize everything to NULL so the above works. */
525   column_names  = NULL;
526   column_values = NULL;
527   oci_defines   = NULL;
528
529   ALLOC_OR_FAIL (column_names, column_num * sizeof (char *));
530   ALLOC_OR_FAIL (column_names[0], column_num * DATA_MAX_NAME_LEN
531       * sizeof (char));
532   for (i = 1; i < column_num; i++)
533     column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
534
535   ALLOC_OR_FAIL (column_values, column_num * sizeof (char *));
536   ALLOC_OR_FAIL (column_values[0], column_num * DATA_MAX_NAME_LEN
537       * sizeof (char));
538   for (i = 1; i < column_num; i++)
539     column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
540
541   ALLOC_OR_FAIL (oci_defines, column_num * sizeof (OCIDefine *));
542   /* }}} End of buffer allocations. */
543
544   /* ``Define'' the returned data, i. e. bind the columns to the buffers
545    * allocated above. */
546   for (i = 0; i < column_num; i++) /* {{{ */
547   {
548     char *column_name;
549     ub4 column_name_length;
550     OCIParam *oci_param;
551
552     oci_param = NULL;
553
554     status = OCIParamGet (oci_statement, OCI_HTYPE_STMT, oci_error,
555         (void *) &oci_param, (ub4) (i + 1));
556     if (status != OCI_SUCCESS)
557     {
558       /* This is probably alright */
559       DEBUG ("oracle plugin: o_read_database_query: status = %#x (= %i);", status, status);
560       o_report_error ("o_read_database_query", "OCIParamGet", oci_error);
561       status = OCI_SUCCESS;
562       break;
563     }
564
565     column_name = NULL;
566     column_name_length = 0;
567     status = OCIAttrGet (oci_param, OCI_DTYPE_PARAM,
568         &column_name, &column_name_length, OCI_ATTR_NAME, oci_error);
569     if (status != OCI_SUCCESS)
570     {
571       OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
572       o_report_error ("o_read_database_query", "OCIAttrGet (OCI_ATTR_NAME)",
573           oci_error);
574       continue;
575     }
576
577     OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
578     oci_param = NULL;
579
580     /* Copy the name to column_names. Warning: The ``string'' returned by OCI
581      * may not be null terminated! */
582     memset (column_names[i], 0, DATA_MAX_NAME_LEN);
583     if (column_name_length >= DATA_MAX_NAME_LEN)
584       column_name_length = DATA_MAX_NAME_LEN - 1;
585     memcpy (column_names[i], column_name, column_name_length);
586     column_names[i][column_name_length] = 0;
587
588     DEBUG ("oracle plugin: o_read_database_query: column_names[%zu] = %s; "
589         "column_name_length = %"PRIu32";",
590         i, column_names[i], (uint32_t) column_name_length);
591
592     status = OCIDefineByPos (oci_statement,
593         &oci_defines[i], oci_error, (ub4) (i + 1),
594         column_values[i], DATA_MAX_NAME_LEN, SQLT_STR,
595         NULL, NULL, NULL, OCI_DEFAULT);
596     if (status != OCI_SUCCESS)
597     {
598       o_report_error ("o_read_database_query", "OCIDefineByPos", oci_error);
599       continue;
600     }
601   } /* for (j = 1; j <= param_counter; j++) */
602   /* }}} End of the ``define'' stuff. */
603
604   status = udb_query_prepare_result (q, prep_area, hostname_g,
605       /* plugin = */ "oracle", db->name, column_names, column_num,
606       /* interval = */ 0);
607   if (status != 0)
608   {
609     ERROR ("oracle plugin: o_read_database_query (%s, %s): "
610         "udb_query_prepare_result failed.",
611         db->name, udb_query_get_name (q));
612     FREE_ALL;
613     return (-1);
614   }
615
616   /* Fetch and handle all the rows that matched the query. */
617   while (42) /* {{{ */
618   {
619     status = OCIStmtFetch2 (oci_statement, oci_error,
620         /* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT,
621         /* fetch offset = */ 0, /* mode = */ OCI_DEFAULT);
622     if (status == OCI_NO_DATA)
623     {
624       status = OCI_SUCCESS;
625       break;
626     }
627     else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
628     {
629       o_report_error ("o_read_database_query", "OCIStmtFetch2", oci_error);
630       break;
631     }
632
633     status = udb_query_handle_result (q, prep_area, column_values);
634     if (status != 0)
635     {
636       WARNING ("oracle plugin: o_read_database_query (%s, %s): "
637           "udb_query_handle_result failed.",
638           db->name, udb_query_get_name (q));
639     }
640   } /* }}} while (42) */
641
642   /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded: %s", q->statement); */
643   FREE_ALL;
644
645   return (0);
646 #undef FREE_ALL
647 #undef ALLOC_OR_FAIL
648 } /* }}} int o_read_database_query */
649
650 static int o_read_database (o_database_t *db) /* {{{ */
651 {
652   size_t i;
653   int status;
654
655   if (db->oci_service_context != NULL)
656   {
657     OCIServer *server_handle;
658     ub4 connection_status;
659
660     server_handle = NULL;
661     status = OCIAttrGet ((void *) db->oci_service_context, OCI_HTYPE_SVCCTX, 
662         (void *) &server_handle, /* size pointer = */ NULL,
663         OCI_ATTR_SERVER, oci_error);
664     if (status != OCI_SUCCESS)
665     {
666       o_report_error ("o_read_database", "OCIAttrGet", oci_error);
667       return (-1);
668     }
669
670     if (server_handle == NULL)
671     {
672       connection_status = OCI_SERVER_NOT_CONNECTED;
673     }
674     else /* if (server_handle != NULL) */
675     {
676       connection_status = 0;
677       status = OCIAttrGet ((void *) server_handle, OCI_HTYPE_SERVER,
678           (void *) &connection_status, /* size pointer = */ NULL,
679           OCI_ATTR_SERVER_STATUS, oci_error);
680       if (status != OCI_SUCCESS)
681       {
682         o_report_error ("o_read_database", "OCIAttrGet", oci_error);
683         return (-1);
684       }
685     }
686
687     if (connection_status != OCI_SERVER_NORMAL)
688     {
689       INFO ("oracle plugin: Connection to %s lost. Trying to reconnect.",
690           db->name);
691       OCIHandleFree (db->oci_service_context, OCI_HTYPE_SVCCTX);
692       db->oci_service_context = NULL;
693     }
694   } /* if (db->oci_service_context != NULL) */
695
696   if (db->oci_service_context == NULL)
697   {
698     status = OCILogon (oci_env, oci_error,
699         &db->oci_service_context,
700         (OraText *) db->username, (ub4) strlen (db->username),
701         (OraText *) db->password, (ub4) strlen (db->password),
702         (OraText *) db->connect_id, (ub4) strlen (db->connect_id));
703     if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
704     {
705       o_report_error ("o_read_database", "OCILogon", oci_error);
706       DEBUG ("oracle plugin: OCILogon (%s): db->oci_service_context = %p;",
707           db->connect_id, db->oci_service_context);
708       db->oci_service_context = NULL;
709       return (-1);
710     }
711     else if (status == OCI_SUCCESS_WITH_INFO)
712     {
713       /* TODO: Print NOTIFY message. */
714     }
715     assert (db->oci_service_context != NULL);
716   }
717
718   DEBUG ("oracle plugin: o_read_database: db->connect_id = %s; db->oci_service_context = %p;",
719       db->connect_id, db->oci_service_context);
720
721   for (i = 0; i < db->queries_num; i++)
722     o_read_database_query (db, db->queries[i], db->q_prep_areas[i]);
723
724   return (0);
725 } /* }}} int o_read_database */
726
727 static int o_read (void) /* {{{ */
728 {
729   size_t i;
730
731   for (i = 0; i < databases_num; i++)
732     o_read_database (databases[i]);
733
734   return (0);
735 } /* }}} int o_read */
736
737 static int o_shutdown (void) /* {{{ */
738 {
739   size_t i;
740
741   for (i = 0; i < databases_num; i++)
742     if (databases[i]->oci_service_context != NULL)
743     {
744       OCIHandleFree (databases[i]->oci_service_context, OCI_HTYPE_SVCCTX);
745       databases[i]->oci_service_context = NULL;
746     }
747   
748   for (i = 0; i < queries_num; i++)
749   {
750     OCIStmt *oci_statement;
751
752     oci_statement = udb_query_get_user_data (queries[i]);
753     if (oci_statement != NULL)
754     {
755       OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
756       udb_query_set_user_data (queries[i], NULL);
757     }
758   }
759   
760   OCIHandleFree (oci_env, OCI_HTYPE_ENV);
761   oci_env = NULL;
762
763   udb_query_free (queries, queries_num);
764   queries = NULL;
765   queries_num = 0;
766
767   return (0);
768 } /* }}} int o_shutdown */
769
770 void module_register (void) /* {{{ */
771 {
772   plugin_register_complex_config ("oracle", o_config);
773   plugin_register_init ("oracle", o_init);
774   plugin_register_read ("oracle", o_read);
775   plugin_register_shutdown ("oracle", o_shutdown);
776 } /* }}} void module_register */
777
778 /*
779  * vim: shiftwidth=2 softtabstop=2 et fdm=marker
780  */