StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StDbNode.cc
1 /***************************************************************************
2  *
3  * $Id: StDbNode.cc,v 1.11 2016/05/25 20:40:01 dmitry Exp $
4  *
5  * Author: R. Jeff Porter
6  ***************************************************************************
7  *
8  * Description: Base-class database entities
9  *
10  ***************************************************************************
11  *
12  * $Log: StDbNode.cc,v $
13  * Revision 1.11 2016/05/25 20:40:01 dmitry
14  * coverity - reverse_inull
15  *
16  * Revision 1.10 2004/01/15 00:02:25 fisyak
17  * Replace ostringstream => StString, add option for alpha
18  *
19  * Revision 1.9 2003/12/16 01:30:32 porter
20  * additional fixes for change from ostrstream to StString that were not exposed until
21  * running in online
22  *
23  * Revision 1.8 2003/09/16 22:44:17 porter
24  * got rid of all ostrstream objects; replaced with StString+string.
25  * modified rules.make and added file stdb_streams.h for standalone compilation
26  *
27  * Revision 1.7 2003/09/02 17:57:49 perev
28  * gcc 3.2 updates + WarnOff
29  *
30  * Revision 1.6 2001/02/09 23:06:25 porter
31  * replaced ostrstream into a buffer with ostrstream creating the
32  * buffer. The former somehow clashed on Solaris with CC5 iostream (current .dev)
33  *
34  * Revision 1.5 2001/01/22 18:37:57 porter
35  * Update of code needed in next year running. This update has little
36  * effect on the interface (only 1 method has been changed in the interface).
37  * Code also preserves backwards compatibility so that old versions of
38  * StDbLib can read new table structures.
39  * -Important features:
40  * a. more efficient low-level table structure (see StDbSql.cc)
41  * b. more flexible indexing for new systems (see StDbElememtIndex.cc)
42  * c. environment variable override KEYS for each database
43  * d. StMessage support & clock-time logging diagnostics
44  * -Cosmetic features
45  * e. hid stl behind interfaces (see new *Impl.* files) to again allow rootcint access
46  * f. removed codes that have been obsolete for awhile (e.g. db factories)
47  * & renamed some classes for clarity (e.g. tableQuery became StDataBaseI
48  * and mysqlAccessor became StDbSql)
49  *
50  * Revision 1.4 2000/04/25 18:26:03 porter
51  * added flavor & production time as settable query fields in
52  * table &/or node. Associated SQL updated in mysqlAccessor.
53  * Flavor key supports "+" as an OR symbol.
54  *
55  * Revision 1.3 2000/01/19 20:20:06 porter
56  * - finished transaction model needed by online
57  * - fixed CC5 compile problem in StDbNodeInfo.cc
58  * - replace TableIter class by StDbTableIter to prevent name problems
59  *
60  * Revision 1.2 2000/01/14 14:50:52 porter
61  * expanded use of verbose mode & fixed inconsistency in
62  * StDbNodeInfo::getElementID
63  *
64  * Revision 1.1 2000/01/10 20:37:54 porter
65  * expanded functionality based on planned additions or feedback from Online work.
66  * update includes:
67  * 1. basis for real transaction model with roll-back
68  * 2. limited SQL access via the manager for run-log & tagDb
69  * 3. balance obtained between enumerated & string access to databases
70  * 4. 3-levels of diagnostic output: Quiet, Normal, Verbose
71  * 5. restructured Node model for better XML support
72  *
73  *
74  ***************************************************************************/
75 
76 #include "StDbNode.hh"
77 #include "StDbDefaults.hh"
78 #include "stdb_streams.h"
79 #include <stdlib.h>
80 
81 #ifdef __ROOT__
82 ClassImp(StDbNode)
83 #endif
84 
86 
87 StDbNode::StDbNode(const char* name, const char* versionKey): mname(0), mversion(0), mdbName(0), mnodeID(0),mnodeType(0){
88 
89  setName(name);
90  setVersion(versionKey);
91  misConfigured = false;
92  mcanRollBack = false;
93 }
94 
96 StDbNode::StDbNode(const char* name ): mname(0), mversion(0), mdbName(0), mnodeID(0) ,mnodeType(0){
97  setName(name);
98  setVersion(StDbDefaults::Instance()->printVersion());
99  misConfigured = false;
100  mcanRollBack = false;
101 }
102 
104 StDbNode::StDbNode(StDbNode& node){
105 
106  mname = node.getName();
107  mversion = node.getVersion();
108  mdbName = node.getDbName();
109  mdbType = node.getDbType();
110  mdbDomain = node.getDbDomain();
111  mnodeID = node.getNodeID();
112  mnodeType = node.getNodeType();
113  misConfigured = node.IsConfigured();
114  mcanRollBack = node.canRollBack();
115 }
116 
118 StDbNode::~StDbNode(){
119  if(mname) delete [] mname;
120  if(mversion) delete [] mversion;
121  if(mdbName) delete [] mdbName;
122  if(mnodeType) delete [] mnodeType;
123 }
124 
126 
127 char* StDbNode::getName() { return mstrDup((const char*)mname); };
128 char* StDbNode::getVersion() { return mstrDup((const char*)mversion); }
129 char* StDbNode::getDbName() { return mstrDup((const char*)mdbName); }
130 char* StDbNode::getNodeType() { return mstrDup(mnodeType); }
131 
132 void StDbNode::setName(const char* nodeName) {
133  if(mname) delete [] mname;
134  mname=mstrDup(nodeName);
135 }
136 void StDbNode::setVersion(const char* version){
137  if(mversion) delete [] mversion;
138  mversion=mstrDup(version);
139 }
140 void StDbNode::setDbName(const char* dbName) {
141  if(mdbName)delete [] mdbName;
142  mdbName=mstrDup(dbName);
143 }
144 void StDbNode::setNodeType(const char* type) {
145  if(mnodeType) delete [] mnodeType;
146  mnodeType=mstrDup(type);
147 }
148 
150 int* StDbNode::decodeElementID(const char* elemID, int& numRows) {
151 
152 numRows=1;
153 int * retVal = 0;
154 char* id=strstr((char*)elemID,"None");
155 
156 if(id){
157  numRows=1;
158  int* e = new int[1]; *e=0;
159  return e;
160 }
161 
162 char* tmpName = new char[strlen(elemID)+1];
163 strcpy(tmpName,elemID);
164 
165 int numElements = 1;
166 id = strstr(tmpName,",");
167 char* id1;
168 char* id2;
169 
170 id2 = strstr(tmpName,"-");
171 StString sl;
172 
173 if(id2 && ( (id && id2<id) || !id)){
174  id=id2;
175  id[0]=',';
176  sl<<"r";
177 } else {
178  sl<<"l";
179 }
180 
181 int numEntries = 1;
182 if(id)id++;
183 while(id){
184  // cout << "id = " << id << endl;
185  numEntries++;
186  id1=strstr(id,",");
187  id2=strstr(id,"-");
188  id = id1;
189  if(id && id2 && id2<id){
190  id=id2;
191  id[0]=',';
192  sl<<"r";
193  } else {
194  sl<<"l";
195  }
196  if(id)id++;
197 }
198  char* islist=new char[strlen((sl.str()).c_str())+1];
199  strcpy(islist,(sl.str()).c_str());
200 
201  // cout << "My string list = " << islist << endl;
202 
203  int* tmpElements = new int[100000];
204  char* p1=&tmpName[0];
205  char* anID=0;
206  anID = getNextID(p1);
207  if ( anID ) {
208  tmpElements[0] = atoi(anID);
209  delete [] anID;
210  }
211  numElements = 1;
212  int iEnd, iStart, k;
213  for(int ient=1;ient<numEntries;ient++){
214  anID = getNextID(p1);
215  if ( anID ) {
216  if(islist[ient-1]=='r'){
217  iEnd = atoi(anID);
218  iStart = tmpElements[numElements-1];
219  int irange=iEnd-iStart;
220  for(int ir=1;ir<=irange;ir++){
221  numElements++;
222  tmpElements[numElements-1]=iStart+ir;
223  }
224  } else {
225  numElements++;
226  tmpElements[numElements-1]=atoi(anID);
227  }
228  delete [] anID;
229  }
230  }
231 
232  retVal = new int[numElements];
233  for(k=0;k<numElements;k++)retVal[k]=tmpElements[k];
234  numRows = numElements;
235 
236  delete [] islist;
237  delete [] tmpElements;
238  delete [] tmpName;
239 
240 return retVal;
241 }
242 
244 char*
245 StDbNode::getNextID(char*& currentElement) const {
246 
247 char* nextID = 0;
248 if(!currentElement)return nextID;
249 
250 char* id = strstr(currentElement,",");
251 
252 if(!id) {
253  nextID = new char[strlen(currentElement)+1];
254  strcpy(nextID,currentElement);
255  currentElement = 0;
256 } else {
257  int iloc = id-currentElement;
258  nextID = new char[iloc+1];
259  strncpy(nextID,currentElement,iloc);
260  nextID[iloc]='\0';
261  currentElement = id; currentElement++;
262 }
263 
264 return nextID;
265 }
266 
268 char* StDbNode::mstrDup(const char* s2) {
269 
270  char* s1=0;
271  if(!s2) return s1;
272  s1 = new char[strlen(s2)+1];
273  strcpy(s1,s2);
274 return s1;
275 }
276 
277 
278 
279 
280 
281 
282 
283 
284 
285 
286 
287 
288