Merge pull request #1830 from rubenk/move-collectd-header
[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
50 #include "common.h"
51 #include "plugin.h"
52 #include "configfile.h"
53 #include "utils_db_query.h"
54
55 #include <oci.h>
56
57 /*
58  * Data types
59  */
60 struct o_database_s
61 {
62   char *name;
63   char *host;
64   char *connect_id;
65   char *username;
66   char *password;
67
68   udb_query_preparation_area_t **q_prep_areas;
69   udb_query_t **queries;
70   size_t        queries_num;
71
72   OCISvcCtx *oci_service_context;
73 };
74 typedef struct o_database_s o_database_t;
75
76 /*
77  * Global variables
78  */
79 static udb_query_t  **queries       = NULL;
80 static size_t         queries_num   = 0;
81 static o_database_t **databases     = NULL;
82 static size_t         databases_num = 0;
83
84 OCIEnv   *oci_env = NULL;
85 OCIError *oci_error = NULL;
86
87 /*
88  * Functions
89  */
90 static void o_report_error (const char *where, /* {{{ */
91     const char *db_name, const char *query_name,
92     const char *what, OCIError *eh)
93 {
94   char buffer[2048];
95   sb4 error_code;
96   int status;
97   unsigned int record_number;
98
99   if (db_name == NULL)
100     db_name = "(none)";
101   if (query_name == NULL)
102     query_name = "(none)";
103
104   /* An operation may cause / return multiple errors. Loop until we have
105    * handled all errors available (with a fail-save limit of 16). */
106   for (record_number = 1; record_number <= 16; record_number++)
107   {
108     memset (buffer, 0, sizeof (buffer));
109     error_code = -1;
110
111     status = OCIErrorGet (eh, (ub4) record_number,
112         /* sqlstate = */ NULL,
113         &error_code,
114         (text *) &buffer[0],
115         (ub4) sizeof (buffer),
116         OCI_HTYPE_ERROR);
117     buffer[sizeof (buffer) - 1] = 0;
118
119     if (status == OCI_NO_DATA)
120       return;
121
122     if (status == OCI_SUCCESS)
123     {
124       size_t buffer_length;
125
126       buffer_length = strlen (buffer);
127       while ((buffer_length > 0) && (buffer[buffer_length - 1] < 32))
128       {
129         buffer_length--;
130         buffer[buffer_length] = 0;
131       }
132
133       ERROR ("oracle plugin: %s (db = %s, query = %s): %s failed: %s",
134           where, db_name, query_name, what, buffer);
135     }
136     else
137     {
138       ERROR ("oracle plugin: %s (db = %s, query = %s): %s failed. "
139           "Additionally, OCIErrorGet failed with status %i.",
140           where, db_name, query_name, what, status);
141       return;
142     }
143   }
144 } /* }}} void o_report_error */
145
146 static void o_database_free (o_database_t *db) /* {{{ */
147 {
148   size_t i;
149
150   if (db == NULL)
151     return;
152
153   sfree (db->name);
154   sfree (db->connect_id);
155   sfree (db->username);
156   sfree (db->password);
157   sfree (db->queries);
158
159   if (db->q_prep_areas != NULL)
160     for (i = 0; i < db->queries_num; ++i)
161       udb_query_delete_preparation_area (db->q_prep_areas[i]);
162   free (db->q_prep_areas);
163
164   sfree (db);
165 } /* }}} void o_database_free */
166
167 /* Configuration handling functions {{{
168  *
169  * <Plugin oracle>
170  *   <Query "plugin_instance0">
171  *     Statement "SELECT name, value FROM table"
172  *     <Result>
173  *       Type "gauge"
174  *       InstancesFrom "name"
175  *       ValuesFrom "value"
176  *     </Result>
177  *   </Query>
178  *
179  *   <Database "plugin_instance1">
180  *     ConnectID "db01"
181  *     Username "oracle"
182  *     Password "secret"
183  *     Query "plugin_instance0"
184  *   </Database>
185  * </Plugin>
186  */
187
188 static int o_config_add_database (oconfig_item_t *ci) /* {{{ */
189 {
190   o_database_t *db;
191   int status;
192   int i;
193
194   if ((ci->values_num != 1)
195       || (ci->values[0].type != OCONFIG_TYPE_STRING))
196   {
197     WARNING ("oracle plugin: The `Database' block "
198         "needs exactly one string argument.");
199     return (-1);
200   }
201
202   db = calloc (1, sizeof (*db));
203   if (db == NULL)
204   {
205     ERROR ("oracle plugin: calloc failed.");
206     return (-1);
207   }
208   db->name = NULL;
209   db->host = NULL;
210   db->connect_id = NULL;
211   db->username = NULL;
212   db->password = NULL;
213
214   status = cf_util_get_string (ci, &db->name);
215   if (status != 0)
216   {
217     sfree (db);
218     return (status);
219   }
220
221   /* Fill the `o_database_t' structure.. */
222   for (i = 0; i < ci->children_num; i++)
223   {
224     oconfig_item_t *child = ci->children + i;
225
226     if (strcasecmp ("ConnectID", child->key) == 0)
227       status = cf_util_get_string (child, &db->connect_id);
228     else if (strcasecmp ("Host", child->key) == 0)
229       status = cf_util_get_string (child, &db->host);
230     else if (strcasecmp ("Username", child->key) == 0)
231       status = cf_util_get_string (child, &db->username);
232     else if (strcasecmp ("Password", child->key) == 0)
233       status = cf_util_get_string (child, &db->password);
234     else if (strcasecmp ("Query", child->key) == 0)
235       status = udb_query_pick_from_list (child, queries, queries_num,
236           &db->queries, &db->queries_num);
237     else
238     {
239       WARNING ("oracle plugin: Option `%s' not allowed here.", child->key);
240       status = -1;
241     }
242
243     if (status != 0)
244       break;
245   }
246
247   /* Check that all necessary options have been given. */
248   while (status == 0)
249   {
250     if (db->connect_id == NULL)
251     {
252       WARNING ("oracle plugin: `ConnectID' not given for query `%s'", db->name);
253       status = -1;
254     }
255     if (db->username == NULL)
256     {
257       WARNING ("oracle plugin: `Username' not given for query `%s'", db->name);
258       status = -1;
259     }
260     if (db->password == NULL)
261     {
262       WARNING ("oracle plugin: `Password' not given for query `%s'", db->name);
263       status = -1;
264     }
265
266     break;
267   } /* while (status == 0) */
268
269   while ((status == 0) && (db->queries_num > 0))
270   {
271     db->q_prep_areas = (udb_query_preparation_area_t **) calloc (
272         db->queries_num, sizeof (*db->q_prep_areas));
273
274     if (db->q_prep_areas == NULL)
275     {
276       WARNING ("oracle plugin: calloc failed");
277       status = -1;
278       break;
279     }
280
281     for (i = 0; i < db->queries_num; ++i)
282     {
283       db->q_prep_areas[i]
284         = udb_query_allocate_preparation_area (db->queries[i]);
285
286       if (db->q_prep_areas[i] == NULL)
287       {
288         WARNING ("oracle plugin: udb_query_allocate_preparation_area failed");
289         status = -1;
290         break;
291       }
292     }
293
294     break;
295   }
296
297   /* If all went well, add this query to the list of queries within the
298    * database structure. */
299   if (status == 0)
300   {
301     o_database_t **temp;
302
303     temp = realloc (databases,
304         sizeof (*databases) * (databases_num + 1));
305     if (temp == NULL)
306     {
307       ERROR ("oracle plugin: realloc failed");
308       status = -1;
309     }
310     else
311     {
312       databases = temp;
313       databases[databases_num] = db;
314       databases_num++;
315     }
316   }
317
318   if (status != 0)
319   {
320     o_database_free (db);
321     return (-1);
322   }
323
324   return (0);
325 } /* }}} int o_config_add_database */
326
327 static int o_config (oconfig_item_t *ci) /* {{{ */
328 {
329   int i;
330
331   for (i = 0; i < ci->children_num; i++)
332   {
333     oconfig_item_t *child = ci->children + i;
334     if (strcasecmp ("Query", child->key) == 0)
335       udb_query_create (&queries, &queries_num, child,
336           /* callback = */ NULL);
337     else if (strcasecmp ("Database", child->key) == 0)
338       o_config_add_database (child);
339     else
340     {
341       WARNING ("oracle plugin: Ignoring unknown config option `%s'.", child->key);
342     }
343
344     if (queries_num > 0)
345     {
346       DEBUG ("oracle plugin: o_config: queries_num = %zu; queries[0] = %p; udb_query_get_user_data (queries[0]) = %p;",
347           queries_num, (void *) queries[0], udb_query_get_user_data (queries[0]));
348     }
349   } /* for (ci->children) */
350
351   return (0);
352 } /* }}} int o_config */
353
354 /* }}} End of configuration handling functions */
355
356 static int o_init (void) /* {{{ */
357 {
358   int status;
359
360   if (oci_env != NULL)
361     return (0);
362
363   status = OCIEnvCreate (&oci_env,
364       /* mode = */ OCI_THREADED,
365       /* context        = */ NULL,
366       /* malloc         = */ NULL,
367       /* realloc        = */ NULL,
368       /* free           = */ NULL,
369       /* user_data_size = */ 0,
370       /* user_data_ptr  = */ NULL);
371   if (status != 0)
372   {
373     ERROR ("oracle plugin: OCIEnvCreate failed with status %i.", status);
374     return (-1);
375   }
376
377   status = OCIHandleAlloc (oci_env, (void *) &oci_error, OCI_HTYPE_ERROR,
378       /* user_data_size = */ 0, /* user_data = */ NULL);
379   if (status != OCI_SUCCESS)
380   {
381     ERROR ("oracle plugin: OCIHandleAlloc (OCI_HTYPE_ERROR) failed "
382         "with status %i.", status);
383     return (-1);
384   }
385
386   return (0);
387 } /* }}} int o_init */
388
389 static int o_read_database_query (o_database_t *db, /* {{{ */
390     udb_query_t *q, udb_query_preparation_area_t *prep_area)
391 {
392   char **column_names;
393   char **column_values;
394   size_t column_num;
395
396   OCIStmt *oci_statement;
397
398   /* List of `OCIDefine' pointers. These defines map columns to the buffer
399    * space declared above. */
400   OCIDefine **oci_defines;
401
402   int status;
403   size_t i;
404
405   oci_statement = udb_query_get_user_data (q);
406
407   /* Prepare the statement */
408   if (oci_statement == NULL) /* {{{ */
409   {
410     const char *statement;
411
412     statement = udb_query_get_statement (q);
413     assert (statement != NULL);
414
415     status = OCIHandleAlloc (oci_env, (void *) &oci_statement,
416         OCI_HTYPE_STMT, /* user_data_size = */ 0, /* user_data = */ NULL);
417     if (status != OCI_SUCCESS)
418     {
419       o_report_error ("o_read_database_query", db->name,
420           udb_query_get_name (q), "OCIHandleAlloc", oci_error);
421       oci_statement = NULL;
422       return (-1);
423     }
424
425     status = OCIStmtPrepare (oci_statement, oci_error,
426         (text *) statement, (ub4) strlen (statement),
427         /* language = */ OCI_NTV_SYNTAX,
428         /* mode     = */ OCI_DEFAULT);
429     if (status != OCI_SUCCESS)
430     {
431       o_report_error ("o_read_database_query", db->name,
432           udb_query_get_name (q), "OCIStmtPrepare", oci_error);
433       OCIHandleFree (oci_statement, OCI_HTYPE_STMT);
434       oci_statement = NULL;
435       return (-1);
436     }
437     udb_query_set_user_data (q, oci_statement);
438
439     DEBUG ("oracle plugin: o_read_database_query (%s, %s): "
440         "Successfully allocated statement handle.",
441         db->name, udb_query_get_name (q));
442   } /* }}} */
443
444   assert (oci_statement != NULL);
445
446   /* Execute the statement */
447   status = OCIStmtExecute (db->oci_service_context, /* {{{ */
448       oci_statement,
449       oci_error,
450       /* iters = */ 0,
451       /* rowoff = */ 0,
452       /* snap_in = */ NULL, /* snap_out = */ NULL,
453       /* mode = */ OCI_DEFAULT);
454   if (status != OCI_SUCCESS)
455   {
456     o_report_error ("o_read_database_query", db->name, udb_query_get_name (q),
457         "OCIStmtExecute", oci_error);
458     return (-1);
459   } /* }}} */
460
461   /* Acquire the number of columns returned. */
462   do /* {{{ */
463   {
464     ub4 param_counter = 0;
465     status = OCIAttrGet (oci_statement, OCI_HTYPE_STMT, /* {{{ */
466         &param_counter, /* size pointer = */ NULL,
467         OCI_ATTR_PARAM_COUNT, oci_error);
468     if (status != OCI_SUCCESS)
469     {
470       o_report_error ("o_read_database_query", db->name,
471           udb_query_get_name (q), "OCIAttrGet", oci_error);
472       return (-1);
473     } /* }}} */
474
475     column_num = (size_t) param_counter;
476   } while (0); /* }}} */
477
478   /* Allocate the following buffers:
479    *
480    *  +---------------+-----------------------------------+
481    *  ! Name          ! Size                              !
482    *  +---------------+-----------------------------------+
483    *  ! column_names  ! column_num x DATA_MAX_NAME_LEN    !
484    *  ! column_values ! column_num x DATA_MAX_NAME_LEN    !
485    *  ! oci_defines   ! column_num x sizeof (OCIDefine *) !
486    *  +---------------+-----------------------------------+
487    *
488    * {{{ */
489 #define NUMBER_BUFFER_SIZE 64
490
491 #define FREE_ALL \
492   if (column_names != NULL) { \
493     sfree (column_names[0]); \
494     sfree (column_names); \
495   } \
496   if (column_values != NULL) { \
497     sfree (column_values[0]); \
498     sfree (column_values); \
499   } \
500   sfree (oci_defines)
501
502 #define ALLOC_OR_FAIL(ptr, ptr_size) \
503   do { \
504     size_t alloc_size = (size_t) ((ptr_size)); \
505     (ptr) = calloc (1, alloc_size); \
506     if ((ptr) == NULL) { \
507       FREE_ALL; \
508       ERROR ("oracle plugin: o_read_database_query: calloc failed."); \
509       return (-1); \
510     } \
511   } while (0)
512
513   /* Initialize everything to NULL so the above works. */
514   column_names  = NULL;
515   column_values = NULL;
516   oci_defines   = NULL;
517
518   ALLOC_OR_FAIL (column_names, column_num * sizeof (char *));
519   ALLOC_OR_FAIL (column_names[0], column_num * DATA_MAX_NAME_LEN
520       * sizeof (char));
521   for (i = 1; i < column_num; i++)
522     column_names[i] = column_names[i - 1] + DATA_MAX_NAME_LEN;
523
524   ALLOC_OR_FAIL (column_values, column_num * sizeof (char *));
525   ALLOC_OR_FAIL (column_values[0], column_num * DATA_MAX_NAME_LEN
526       * sizeof (char));
527   for (i = 1; i < column_num; i++)
528     column_values[i] = column_values[i - 1] + DATA_MAX_NAME_LEN;
529
530   ALLOC_OR_FAIL (oci_defines, column_num * sizeof (OCIDefine *));
531   /* }}} End of buffer allocations. */
532
533   /* ``Define'' the returned data, i. e. bind the columns to the buffers
534    * allocated above. */
535   for (i = 0; i < column_num; i++) /* {{{ */
536   {
537     char *column_name;
538     ub4 column_name_length;
539     OCIParam *oci_param;
540
541     oci_param = NULL;
542
543     status = OCIParamGet (oci_statement, OCI_HTYPE_STMT, oci_error,
544         (void *) &oci_param, (ub4) (i + 1));
545     if (status != OCI_SUCCESS)
546     {
547       /* This is probably alright */
548       DEBUG ("oracle plugin: o_read_database_query: status = %#x (= %i);",
549           status, status);
550       o_report_error ("o_read_database_query", db->name,
551           udb_query_get_name (q), "OCIParamGet", oci_error);
552       status = OCI_SUCCESS;
553       break;
554     }
555
556     column_name = NULL;
557     column_name_length = 0;
558     status = OCIAttrGet (oci_param, OCI_DTYPE_PARAM,
559         &column_name, &column_name_length, OCI_ATTR_NAME, oci_error);
560     if (status != OCI_SUCCESS)
561     {
562       OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
563       o_report_error ("o_read_database_query", db->name,
564           udb_query_get_name (q), "OCIAttrGet (OCI_ATTR_NAME)", oci_error);
565       continue;
566     }
567
568     OCIDescriptorFree (oci_param, OCI_DTYPE_PARAM);
569     oci_param = NULL;
570
571     /* Copy the name to column_names. Warning: The ``string'' returned by OCI
572      * may not be null terminated! */
573     memset (column_names[i], 0, DATA_MAX_NAME_LEN);
574     if (column_name_length >= DATA_MAX_NAME_LEN)
575       column_name_length = DATA_MAX_NAME_LEN - 1;
576     memcpy (column_names[i], column_name, column_name_length);
577     column_names[i][column_name_length] = 0;
578
579     DEBUG ("oracle plugin: o_read_database_query: column_names[%zu] = %s; "
580         "column_name_length = %"PRIu32";",
581         i, column_names[i], (uint32_t) column_name_length);
582
583     status = OCIDefineByPos (oci_statement,
584         &oci_defines[i], oci_error, (ub4) (i + 1),
585         column_values[i], DATA_MAX_NAME_LEN, SQLT_STR,
586         NULL, NULL, NULL, OCI_DEFAULT);
587     if (status != OCI_SUCCESS)
588     {
589       o_report_error ("o_read_database_query", db->name,
590           udb_query_get_name (q), "OCIDefineByPos", oci_error);
591       continue;
592     }
593   } /* for (j = 1; j <= param_counter; j++) */
594   /* }}} End of the ``define'' stuff. */
595
596   status = udb_query_prepare_result (q, prep_area,
597       (db->host != NULL) ? db->host : hostname_g,
598       /* plugin = */ "oracle", db->name, column_names, column_num,
599       /* interval = */ 0);
600   if (status != 0)
601   {
602     ERROR ("oracle plugin: o_read_database_query (%s, %s): "
603         "udb_query_prepare_result failed.",
604         db->name, udb_query_get_name (q));
605     FREE_ALL;
606     return (-1);
607   }
608
609   /* Fetch and handle all the rows that matched the query. */
610   while (42) /* {{{ */
611   {
612     status = OCIStmtFetch2 (oci_statement, oci_error,
613         /* nrows = */ 1, /* orientation = */ OCI_FETCH_NEXT,
614         /* fetch offset = */ 0, /* mode = */ OCI_DEFAULT);
615     if (status == OCI_NO_DATA)
616     {
617       status = OCI_SUCCESS;
618       break;
619     }
620     else if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
621     {
622       o_report_error ("o_read_database_query", db->name,
623           udb_query_get_name (q), "OCIStmtFetch2", oci_error);
624       break;
625     }
626
627     status = udb_query_handle_result (q, prep_area, column_values);
628     if (status != 0)
629     {
630       WARNING ("oracle plugin: o_read_database_query (%s, %s): "
631           "udb_query_handle_result failed.",
632           db->name, udb_query_get_name (q));
633     }
634   } /* }}} while (42) */
635
636   /* DEBUG ("oracle plugin: o_read_database_query: This statement succeeded: %s", q->statement); */
637   FREE_ALL;
638
639   return (0);
640 #undef FREE_ALL
641 #undef ALLOC_OR_FAIL
642 } /* }}} int o_read_database_query */
643
644 static int o_read_database (o_database_t *db) /* {{{ */
645 {
646   size_t i;
647   int status;
648
649   if (db->oci_service_context != NULL)
650   {
651     OCIServer *server_handle;
652     ub4 connection_status;
653
654     server_handle = NULL;
655     status = OCIAttrGet ((void *) db->oci_service_context, OCI_HTYPE_SVCCTX,
656         (void *) &server_handle, /* size pointer = */ NULL,
657         OCI_ATTR_SERVER, oci_error);
658     if (status != OCI_SUCCESS)
659     {
660       o_report_error ("o_read_database", db->name, NULL, "OCIAttrGet",
661           oci_error);
662       return (-1);
663     }
664
665     if (server_handle == NULL)
666     {
667       connection_status = OCI_SERVER_NOT_CONNECTED;
668     }
669     else /* if (server_handle != NULL) */
670     {
671       connection_status = 0;
672       status = OCIAttrGet ((void *) server_handle, OCI_HTYPE_SERVER,
673           (void *) &connection_status, /* size pointer = */ NULL,
674           OCI_ATTR_SERVER_STATUS, oci_error);
675       if (status != OCI_SUCCESS)
676       {
677         o_report_error ("o_read_database", db->name, NULL, "OCIAttrGet",
678             oci_error);
679         return (-1);
680       }
681     }
682
683     if (connection_status != OCI_SERVER_NORMAL)
684     {
685       INFO ("oracle plugin: Connection to %s lost. Trying to reconnect.",
686           db->name);
687       OCIHandleFree (db->oci_service_context, OCI_HTYPE_SVCCTX);
688       db->oci_service_context = NULL;
689     }
690   } /* if (db->oci_service_context != NULL) */
691
692   if (db->oci_service_context == NULL)
693   {
694     status = OCILogon (oci_env, oci_error,
695         &db->oci_service_context,
696         (OraText *) db->username, (ub4) strlen (db->username),
697         (OraText *) db->password, (ub4) strlen (db->password),
698         (OraText *) db->connect_id, (ub4) strlen (db->connect_id));
699     if ((status != OCI_SUCCESS) && (status != OCI_SUCCESS_WITH_INFO))
700     {
701       char errfunc[256];
702
703       ssnprintf (errfunc, sizeof (errfunc), "OCILogon(\"%s\")", db->connect_id);
704
705       o_report_error ("o_read_database", db->name, NULL, errfunc, 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  */