libdsdb3  3.0
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups
datastream_config.c
Go to the documentation of this file.
1 /*******************************************************************************
2 *
3 * COPYRIGHT (C) 2010 Battelle Memorial Institute. All Rights Reserved.
4 *
5 ********************************************************************************
6 *
7 * Author:
8 * name: Brian Ermold
9 * phone: (509) 375-2277
10 * email: brian.ermold@pnl.gov
11 *
12 ********************************************************************************
13 *
14 * REPOSITORY INFORMATION:
15 * $Revision: 6726 $
16 * $Author: ermold $
17 * $Date: 2011-05-17 03:48:03 +0000 (Tue, 17 May 2011) $
18 *
19 ********************************************************************************
20 *
21 * NOTE: DOXYGEN is used to generate documentation for this file.
22 *
23 *******************************************************************************/
24 
25 /** @file datastream_config.c
26  * Datastream Config Functions.
27  */
28 
29 #include "dsdb3.h"
30 #include "dbog_dsdb.h"
31 
32 /**
33  * @defgroup DSDB_DSCONF Datastream Config
34  */
35 /*@{*/
36 
37 /*******************************************************************************
38  * Private Functions
39  */
40 /** @privatesection */
41 
42 static void _dsdb_destroy_ds_conf(DSConf *ds_conf)
43 {
44  if (ds_conf) {
45  if (ds_conf->site) free((void *)ds_conf->site);
46  if (ds_conf->facility) free((void *)ds_conf->facility);
47  if (ds_conf->name) free((void *)ds_conf->name);
48  if (ds_conf->level) free((void *)ds_conf->level);
49  if (ds_conf->key) free((void *)ds_conf->key);
50  if (ds_conf->value) free((void *)ds_conf->value);
51  free(ds_conf);
52  }
53 }
54 
55 static DSConf *_dsdb_create_ds_conf(
56  const char *name,
57  const char *level,
58  const char *site,
59  const char *facility,
60  const char *key,
61  const char *value)
62 {
63  DSConf *ds_conf = (DSConf *)calloc(1, sizeof(DSConf));
64 
65  if (!ds_conf) {
66  return((DSConf *)NULL);
67  }
68 
69  /* Site Name */
70 
71  if (site) {
72  ds_conf->site = msngr_copy_string(site);
73  if (!ds_conf->site) {
74  free(ds_conf);
75  return((DSConf *)NULL);
76  }
77  }
78  else {
79  ds_conf->site = (char *)NULL;
80  }
81 
82  /* Facility Name */
83 
84  if (facility) {
85  ds_conf->facility = msngr_copy_string(facility);
86  if (!ds_conf->facility) {
87  _dsdb_destroy_ds_conf(ds_conf);
88  return((DSConf *)NULL);
89  }
90  }
91  else {
92  ds_conf->facility = (char *)NULL;
93  }
94 
95  /* Datastream Name */
96 
97  if (name) {
98  ds_conf->name = msngr_copy_string(name);
99  if (!ds_conf->name) {
100  _dsdb_destroy_ds_conf(ds_conf);
101  return((DSConf *)NULL);
102  }
103  }
104  else {
105  ds_conf->name = (char *)NULL;
106  }
107 
108  /* Datastream Level */
109 
110  if (level) {
111  ds_conf->level = msngr_copy_string(level);
112  if (!ds_conf->level) {
113  _dsdb_destroy_ds_conf(ds_conf);
114  return((DSConf *)NULL);
115  }
116  }
117  else {
118  ds_conf->level = (char *)NULL;
119  }
120 
121  /* Config Key */
122 
123  if (key) {
124  ds_conf->key = msngr_copy_string(key);
125  if (!ds_conf->key) {
126  _dsdb_destroy_ds_conf(ds_conf);
127  return((DSConf *)NULL);
128  }
129  }
130  else {
131  ds_conf->key = (char *)NULL;
132  }
133 
134  /* Config Value */
135 
136  if (value) {
137  ds_conf->value = msngr_copy_string(value);
138  if (!ds_conf->value) {
139  _dsdb_destroy_ds_conf(ds_conf);
140  return((DSConf *)NULL);
141  }
142  }
143  else {
144  ds_conf->value = (char *)NULL;
145  }
146 
147  return(ds_conf);
148 }
149 
150 /*******************************************************************************
151  * Public Functions
152  */
153 /** @publicsection */
154 
155 /**
156  * Free all memory used by an array of DSConf structures.
157  *
158  * @param ds_conf - pointer to the array of DSConf structure pointers
159  */
161 {
162  int row;
163 
164  if (ds_conf) {
165  for (row = 0; ds_conf[row]; row++) {
166  _dsdb_destroy_ds_conf(ds_conf[row]);
167  }
168  free(ds_conf);
169  }
170 }
171 
172 /**
173  * Get datastream config values from the database.
174  *
175  * The nature of this function requires that NULL column values in the
176  * datastream_config table will match any argument value. A SQL reqular
177  * expression can be used for the key argument.
178  *
179  * The memory used by the output array is dynamically allocated.
180  * It is the responsibility of the calling process to free this memory
181  * when it is no longer needed (see dsdb_free_datastream_config_values()).
182  *
183  * Error messages from this function are sent to the message
184  * handler (see msngr_init_log() and msngr_init_mail()).
185  *
186  * Null results from the database are not reported as errors.
187  * It is the responsibility of the calling process to report
188  * these as errors if necessary.
189  *
190  * @param dsdb - pointer to the open database connection
191  * @param site - site name
192  * @param facility - facility name
193  * @param ds_name - datastream name
194  * @param ds_level - datastream level
195  * @param key - config key
196  * @param ds_conf - output: pointer to the NULL terminated array
197  * of DSConf structure pointers
198  *
199  * @return
200  * - 1 if successful
201  * - 0 if the database returned a NULL result
202  * - -1 if an error occurred
203  *
204  * @see dsdb_free_datastream_config_values()
205  */
207  DSDB *dsdb,
208  const char *site,
209  const char *facility,
210  const char *ds_name,
211  const char *ds_level,
212  const char *key,
213  DSConf ***ds_conf)
214 {
215  DBStatus status;
216  DBResult *dbres;
217  int row;
218 
219  *ds_conf = (DSConf **)NULL;
220 
221  status = dsdbog_get_datastream_config_values(
222  dsdb->dbconn, ds_name, ds_level, site, facility, key, &dbres);
223 
224  if (status == DB_NO_ERROR) {
225 
226  *ds_conf = (DSConf **)calloc(dbres->nrows + 1, sizeof(DSConf *));
227  if (!*ds_conf) {
228 
229  ERROR( DSDB_LIB_NAME,
230  "Could not get datastream config values\n"
231  " -> memory allocation error\n");
232 
233  dbres->free(dbres);
234  return(-1);
235  }
236 
237  for (row = 0; row < dbres->nrows; row++) {
238 
239  (*ds_conf)[row] = _dsdb_create_ds_conf(
240  DSConfName(dbres,row),
241  DSConfLevel(dbres,row),
242  DSConfSite(dbres,row),
243  DSConfFac(dbres,row),
244  DSConfKey(dbres,row),
245  DSConfValue(dbres,row));
246 
247  if (!(*ds_conf)[row]) {
248 
249  ERROR( DSDB_LIB_NAME,
250  "Could not get datastream config values\n"
251  " -> memory allocation error\n");
252 
254  *ds_conf = (DSConf **)NULL;
255 
256  dbres->free(dbres);
257  return(-1);
258  }
259  }
260 
261  (*ds_conf)[dbres->nrows] = (DSConf *)NULL;
262 
263  dbres->free(dbres);
264  return(1);
265  }
266  else if (status == DB_NULL_RESULT) {
267  return(0);
268  }
269 
270  return(-1);
271 }
272 
273 /*@}*/