StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StDbFieldI.cxx
1 /*
2  * StDbFieldI.cpp
3  *
4  * Created on Aug 20, 2007
5  *
6  * Author: Zhengqiang Liang (Wayne State University)
7  * Valeri Fine (Brookhaven National Laboratory)
8  * Jerome Lauret (Brookhaven National Laboratory)
9  *
10  *
11  * Copyright (c) 2007-2008 STAR Collaboration - Brookhaven National Laboratory
12  *
13  * @(#)cpp/api:$Id: StDbFieldI.cxx,v 1.5 2010/03/30 20:05:36 fine Exp $
14  *
15  *
16  *
17  * This is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 2 of the License, or
20  * (at your option) any later version.
21  *
22  * STAR Scheduler is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with STAR Scheduler; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include <string>
33 #include <iostream>
34 
35 #include "StDbFieldI.h"
36 using namespace std;
37 using namespace TxLogging;
38 // using namespace StDbField;
39 
40 std::map<StDbFieldI::EDataType,std::string> StDbFieldI::fTypeMap;
41 std::map<StDbFieldI::EDataType,std::string> StDbFieldI::fTypeMapName;
42 std::map<std::string,StDbFieldI::EDataType> StDbFieldI::fTypeMapInv;
43 namespace TxLogging {
45  Init_StDbFieldI() { StDbFieldI::MakeTypeMap();}
46 };
47 }
48 namespace {
50 }
51 //________________________________________________________________________________
52 StDbFieldI::StDbFieldI(const char* name, StDbFieldI::EDataType type, int length)
53  : fType(kINVALID),
54  fEncodedValue(""),
55  fNull(true),
56  fIgnore(true) {
57 
58  if (string(name).empty()) {
59  throw StDataException("Attempted to define field with empty name.",
60  StDataException::FIELD,
61  StUCMException::ERROR);
62  }
63 
64  if (type == kSTRING)
65  {
66  if (length < 0)
67  {
68  throw StDataException("String type specified with length < 0 in field '"
69  + string(name) + "'.",
70  StDataException::FIELD,
71  StUCMException::ERROR);
72  }
73  fMaxLength = length;
74  }
75  else
76  {
77  fMaxLength = 0;
78  }
79 
80  fName = name;
81  fType = type;
82  fIgnore = false;
83 }
84 
85 
86 // template<class T> StDbFieldI(const char* name, T value, int length=0)
87 // in header
88 
89 
90 //________________________________________________________________________________
91 StDbFieldI::StDbFieldI(const char* name, const char*value,
92  StDbFieldI::EDataType type, int length)
93  : fType(kINVALID),
94  fIgnore(true)
95 {
96  if (string(name).empty()) {
97  throw StDataException("Attempted to define field with empty name.",
98  StDataException::FIELD,
99  StUCMException::ERROR);
100  }
101 
102  if (type == kSTRING)
103  {
104  if (length <= 0)
105  {
106  throw StDataException("String type specified with length <= 0 in field '"
107  + string(name) + "'.",
108  StDataException::FIELD,
109  StUCMException::ERROR);
110  }
111  fMaxLength = length;
112  }
113  else
114  {
115  fMaxLength = 0;
116  }
117 
118  fName = name;
119  fType = type;
120  if (!value) value = " ";
121  setValueFromString(value);
122 #ifdef DEBUG
123  cout << __FUNCTION__
124  << ": name =" << name
125  << "; value =" << value
126  << " type =" << type
127  << "; lenght=" << length
128  << endl;
129 #endif
130 }
131 
132 
133 //________________________________________________________________________________
135  copy(f);
136 }
137 
138 
140 //________________________________________________________________________________
142 {} // EMPTY
143 
144 
145 //________________________________________________________________________________
146 const StDbFieldI& StDbFieldI::operator=(const StDbFieldI& f) {
147  if ( this != &f ) {
148  copy(f);
149  }
150  return *this;
151 }
152 
153 
154 //________________________________________________________________________________
155 void StDbFieldI::copy(const StDbFieldI &f) {
156  fName = f.getName();
157  fType = f.getType();
158  fEncodedValue = f.getValueAsString();
159  fNull = f.isNull();
160  fMaxLength = f.getMaxLength();
161  fIgnore = f.isIgnore();
162 }
163 
164 //________________________________________________________________________________
165 const char *
167  return fName.c_str();
168 }
169 
170 
171 //________________________________________________________________________________
172 const char *
174  static string tostring;
175  tostring = string(getName()) + "::"
176  + getValueAsString()
177  + "::"
178  + getTypeAsString();
179  return tostring.c_str();
180 }
181 
182 
183 //________________________________________________________________________________
184 void
185 StDbFieldI::setValueFromString(const char* strValue)
186 {
187  std::string value = strValue;
188  if (fType == kSTRING) {
189  if (value.length() > fMaxLength) {
190  value = string(strValue).substr(0,fMaxLength);
191  }
192  fNull = false;
193  }
194  else {
195  fNull =string(strValue).empty();
196  }
197 
198  fEncodedValue = value;
199  fIgnore = false;
200 }
201 
202 
203 //________________________________________________________________________________
206 {
207  return fType;
208 }
209 
210 
211 //________________________________________________________________________________
212 const char *
214 {
215  return fEncodedValue.c_str();
216 }
217 
218 //________________________________________________________________________________
219 bool
221 {
222  return fNull;
223 }
224 
225 
226 //________________________________________________________________________________
227 void
229 {
230  if (null) {
231  fEncodedValue = "";
232  }
233 
234  fIgnore = false;
235  fNull = null;
236 }
237 
238 
239 //________________________________________________________________________________
240 const char *
242 {
243  return fTypeMapName[getType()].c_str();
244 }
245 
246 
247 //________________________________________________________________________________
248 int
250 {
251  return fMaxLength;
252 }
253 
254 
255 //________________________________________________________________________________
256 bool StDbFieldI::isIgnore() const {
257  return fIgnore;
258 }
259 
260 
261 //________________________________________________________________________________
262 void StDbFieldI::setIgnore(bool ignore) {
263  fIgnore = ignore;
264 }
265 //________________________________________________________________________________
266 void StDbFieldI::MakeTypeMap() {
267 #ifdef TYPEtypeNAME
268 #error TYPEtypeNAME redefinitions
269 #else
270 #define TYPEtypeNAME(TYPENAME,datatype) \
271  case k##TYPENAME: { \
272  fTypeMap.insert(pair<EDataType,std::string>(k##TYPENAME,typeid(datatype).name())); \
273  fTypeMapName.insert(pair<EDataType,std::string>(k##TYPENAME,#TYPENAME)); \
274  fTypeMapInv.insert(pair<std::string,EDataType>(typeid(datatype).name(),k##TYPENAME)); \
275  break;}
276 #endif
277  for (int i=kBOOL;i<kEND;++i) {
278  switch (i) {
279  TYPEtypeNAME(BOOL,bool)
280  TYPEtypeNAME(INT,int)
281  TYPEtypeNAME(UINT,unsigned int)
282  TYPEtypeNAME(LONG,long)
283  TYPEtypeNAME(ULONG,unsigned long)
284  TYPEtypeNAME(DOUBLE,double)
285  TYPEtypeNAME(CHAR,char)
286  TYPEtypeNAME(UNIXTIME,unsigned int)
287  //TYPEtypeNAME(STRING,string)
288  default: {
289  fTypeMap.insert(pair<EDataType,std::string>(kINVALID,"invalid"));
290  fTypeMapName.insert(pair<EDataType,std::string>(kINVALID,"unkown"));
291  fTypeMapInv.insert(pair<std::string,EDataType>("unkown",kINVALID));
292  break;
293  }
294  }
295  }
296 }
297 #ifdef DBVALUECONV_
298 #error DBVALUECONV_ redefinitions
299 #else
300 #define DBVALUECONV_(DATATYPE,dataname) \
301  void StDbFieldI::setValue(const DATATYPE &value) {setValue<DATATYPE>(value);} \
302  DATATYPE StDbFieldI::to##dataname() const { return toValue<DATATYPE>(); }
303 #endif
304  DBVALUECONV_(char,Char)
305  DBVALUECONV_(int,Int)
306  DBVALUECONV_(unsigned int,UInt)
307  DBVALUECONV_(long,Long)
308  DBVALUECONV_(unsigned long,ULong)
309  DBVALUECONV_(double,Double)
310 // DBVALUECONV_(std::string,String)
311 
312 
313 
EDataType getType() const
Definition: StDbFieldI.cxx:205
virtual ~StDbFieldI()
Deconstructor.
Definition: StDbFieldI.cxx:141
StDbFieldI(const char *name, StDbFieldI::EDataType type, int length=0)
Definition: StDbFieldI.cxx:52
void setNull(bool Nil)
Definition: StDbFieldI.cxx:228
void setIgnore(bool ignore)
Definition: StDbFieldI.cxx:262
bool isIgnore() const
Definition: StDbFieldI.cxx:256
const char * getTypeAsString() const
Definition: StDbFieldI.cxx:241
const char * fieldAsString() const
Definition: StDbFieldI.cxx:173
const char * getName() const
Definition: StDbFieldI.cxx:166
const char * getValueAsString() const
Definition: StDbFieldI.cxx:213
void setValueFromString(const char *strValue)
Definition: StDbFieldI.cxx:185
bool isNull() const
Definition: StDbFieldI.cxx:220
int getMaxLength() const
Definition: StDbFieldI.cxx:249