libdsdb3  3.0
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups
ds_properties.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: 54301 $
16 * $Author: ermold $
17 * $Date: 2014-05-13 02:28:34 +0000 (Tue, 13 May 2014) $
18 *
19 ********************************************************************************
20 *
21 * NOTE: DOXYGEN is used to generate documentation for this file.
22 *
23 *******************************************************************************/
24 
25 /** @file ds_properties.c
26  * Datastream Config Functions.
27  */
28 
29 #include "dsdb3.h"
30 #include "dbog_dod.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_dsprop(DSProp *dsprop)
43 {
44  if (dsprop) {
45  if (dsprop->site) free((void *)dsprop->site);
46  if (dsprop->facility) free((void *)dsprop->facility);
47  if (dsprop->dsc_name) free((void *)dsprop->dsc_name);
48  if (dsprop->dsc_level) free((void *)dsprop->dsc_level);
49  if (dsprop->var_name) free((void *)dsprop->var_name);
50  if (dsprop->name) free((void *)dsprop->name);
51  if (dsprop->value) free((void *)dsprop->value);
52  free(dsprop);
53  }
54 }
55 
56 static DSProp *_dsdb_create_dsprop(
57  DSDB *dsdb,
58  const char *dsc_name,
59  const char *dcs_level,
60  const char *site,
61  const char *facility,
62  const char *var_name,
63  const char *name,
64  const char *time,
65  const char *value)
66 {
67  DSProp *dsprop = (DSProp *)calloc(1, sizeof(DSProp));
68  if (!dsprop) return((DSProp *)NULL);
69 
70  DSDB_STRDUP(dsc_name, dsprop->dsc_name, dsprop, (DSProp *)NULL);
71  DSDB_STRDUP(dcs_level, dsprop->dsc_level, dsprop, (DSProp *)NULL);
72  DSDB_STRDUP(site, dsprop->site, dsprop, (DSProp *)NULL);
73  DSDB_STRDUP(facility, dsprop->facility, dsprop, (DSProp *)NULL);
74  DSDB_STRDUP(var_name, dsprop->var_name, dsprop, (DSProp *)NULL);
75  DSDB_STRDUP(name, dsprop->name, dsprop, (DSProp *)NULL);
76  DSDB_STRDUP(value, dsprop->value, dsprop, (DSProp *)NULL);
77 
78  dsdb_text_to_time(dsdb, time, &(dsprop->time));
79 
80  return(dsprop);
81 }
82 
83 /*******************************************************************************
84  * Public Functions
85  */
86 /** @publicsection */
87 
88 /**
89  * Free all memory used by an array of DSProp structures.
90  *
91  * @param dsprops - pointer to the array of DSProp structure pointers
92  */
94 {
95  int row;
96 
97  if (dsprops) {
98  for (row = 0; dsprops[row]; row++) {
99  _dsdb_destroy_dsprop(dsprops[row]);
100  }
101  free(dsprops);
102  }
103 }
104 
105 /**
106  * Get datastream config values from the database.
107  *
108  * The nature of this function requires that NULL column values in the
109  * ds_properties table will match any argument value. A SQL reqular
110  * expression can be used for the key argument.
111  *
112  * The memory used by the output array is dynamically allocated.
113  * It is the responsibility of the calling process to free this memory
114  * when it is no longer needed (see dsdb_free_ds_properties()).
115  *
116  * Error messages from this function are sent to the message
117  * handler (see msngr_init_log() and msngr_init_mail()).
118  *
119  * Null results from the database are not reported as errors.
120  * It is the responsibility of the calling process to report
121  * these as errors if necessary.
122  *
123  * @param dsdb - pointer to the open database connection
124  * @param ds_name - datastream name
125  * @param ds_level - datastream level
126  * @param site - site name
127  * @param facility - facility name
128  * @param var_name - variable name
129  * @param prop_name - property name
130  * @param dsprops - output: pointer to the NULL terminated array
131  * of DSProp structure pointers
132  *
133  * @return
134  * - number of datastream properties
135  * - 0 if the database returned a NULL result
136  * - -1 if an error occurred
137  *
138  * @see dsdb_free_ds_properties()
139  */
141  DSDB *dsdb,
142  const char *ds_name,
143  const char *ds_level,
144  const char *site,
145  const char *facility,
146  const char *var_name,
147  const char *prop_name,
148  DSProp ***dsprops)
149 {
150  DBStatus status;
151  DBResult *dbres;
152  int ndsprops;
153  int row;
154 
155  ndsprops = 0;
156  *dsprops = (DSProp **)NULL;
157 
158  status = dodog_get_ds_properties(
159  dsdb->dbconn, ds_name, ds_level, site, facility,
160  var_name, prop_name, &dbres);
161 
162  if (status == DB_NO_ERROR) {
163 
164  *dsprops = (DSProp **)calloc(dbres->nrows + 1, sizeof(DSProp *));
165  if (!*dsprops) {
166 
167  ERROR( DSDB_LIB_NAME,
168  "Could not get datastream config values\n"
169  " -> memory allocation error\n");
170 
171  dbres->free(dbres);
172  return(-1);
173  }
174 
175  for (row = 0; row < dbres->nrows; row++) {
176 
177  (*dsprops)[row] = _dsdb_create_dsprop(dsdb,
178  DsPropDscName(dbres,row),
179  DsPropDscLevel(dbres,row),
180  DsPropSite(dbres,row),
181  DsPropFac(dbres,row),
182  DsPropVar(dbres,row),
183  DsPropName(dbres,row),
184  DsPropTime(dbres,row),
185  DsPropValue(dbres,row));
186 
187  if (!(*dsprops)[row]) {
188 
189  ERROR( DSDB_LIB_NAME,
190  "Could not get datastream properties\n"
191  " -> memory allocation error\n");
192 
193  dsdb_free_ds_properties(*dsprops);
194  *dsprops = (DSProp **)NULL;
195 
196  dbres->free(dbres);
197  return(-1);
198  }
199 
200  ndsprops++;
201  }
202 
203  (*dsprops)[dbres->nrows] = (DSProp *)NULL;
204 
205  dbres->free(dbres);
206  return(ndsprops);
207  }
208  else if (status == DB_NULL_RESULT) {
209  return(0);
210  }
211 
212  return(-1);
213 }
214 
215 /*@}*/