00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef STDBFIELD_H
00034 #define STDBFIELD_H
00035
00036 #include <string>
00037 #include <sstream>
00038 #include <map>
00039 #include <typeinfo>
00040
00041 #include "StDataException.h"
00042 #include "FieldList.h"
00043
00047 namespace TxLogging {
00048 class Init_StDbFieldI;
00049
00050 class StDbFieldI {
00051 friend class TxLogging::Init_StDbFieldI;
00052 public:
00060 enum EDataType {
00061 kINVALID=0,
00062 kBOOL,
00063 kINT,
00064 kUINT,
00065 kLONG,
00066 kULONG,
00067 kDOUBLE,
00068 kCHAR,
00069 kUNIXTIME,
00070 kSTRING,
00071 kEND
00072 };
00073
00074 public:
00075
00083 StDbFieldI(const char* name, StDbFieldI::EDataType type, int length=0);
00084
00102
00103
00113 StDbFieldI(const char* name, const char* value,
00114 StDbFieldI::EDataType type, int length=0);
00115
00119 StDbFieldI(const StDbFieldI& );
00120 const StDbFieldI& operator=(const StDbFieldI& );
00121
00125 virtual ~StDbFieldI();
00126
00130 const char* getName() const;
00131
00136 const char* fieldAsString() const;
00137
00146 void setValue(const int &value);
00147 void setValue(const unsigned int &value);
00148 void setValue(const long &value);
00149 void setValue(const unsigned long &value);
00150 void setValue(const double &value);
00151 void setValue(const char &value);
00152
00153
00154 int toInt() const;
00155 unsigned toUInt() const;
00156 long toLong() const;
00157 unsigned long toULong() const;
00158 double toDouble() const;
00159 char toChar() const;
00160
00168 void setValueFromString(const char* strValue);
00169
00174 EDataType getType() const;
00175
00181 int getMaxLength() const;
00182
00187 const char*getTypeAsString() const;
00188
00195 const char*getValueAsString() const;
00196
00200 bool isNull() const;
00201
00207 void setNull(bool Nil);
00208
00216 void setIgnore(bool ignore);
00217
00222 bool isIgnore() const;
00223
00224 protected:
00225 template<class T>
00226 StDbFieldI(const char* name, const T &value, int length);
00227
00228
00229 template <class T>
00230 void setValue(const T &value);
00231
00238 template <class T>
00239 T toValue() const;
00240
00241 private:
00246 void copy(const StDbFieldI &f);
00254 template<class T> void typeMatches() const;
00255
00256 private:
00257 static void MakeTypeMap();
00258 std::string fName;
00259 EDataType fType;
00260 std::string fEncodedValue;
00261 size_t fMaxLength;
00262 bool fNull;
00263 bool fIgnore;
00264 static std::map<EDataType,std::string> fTypeMap;
00265 static std::map<EDataType,std::string> fTypeMapName;
00266 static std::map<std::string,EDataType> fTypeMapInv;
00267 };
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 template<class T> StDbFieldI::StDbFieldI(const char* name, const T &value, int length)
00278 : fType(kINVALID),
00279 fNull(true),
00280 fEncodedValue(""),
00281 fMaxLength(length),
00282 fIgnore(true),
00283 fName(name)
00284 {
00285 if (std::string(name).empty()) {
00286 throw StDataException("Attempted to define field with empty name.",
00287 StDataException::FIELD,
00288 StUCMException::ERROR);
00289 }
00290 setValue(value);
00291 }
00292
00293
00294
00295 template <class T> void
00296 StDbFieldI::setValue(const T &value)
00297 {
00298 typeMatches<T>();
00299
00300
00301 std::ostringstream encodingStream;
00302 encodingStream << value;
00303
00304
00305 if ( fType == kSTRING ) {
00306 if ( encodingStream.str().length() > fMaxLength ) {
00307 std::ostringstream lenStr;
00308 lenStr << fMaxLength;
00309
00310
00311 throw StDataException("Invalid value for field '" + fName
00312 + "': Length of string '" + encodingStream.str()
00313 + "' is longer than variant length '"
00314 + lenStr.str() + "'.",
00315 StDataException::FIELD,
00316 StUCMException::ERROR);
00317 }
00318 }
00319
00320 fEncodedValue = encodingStream.str();
00321 fNull = false;
00322 fIgnore = false;
00323 }
00324
00325
00326
00327 template<class T>
00328 T StDbFieldI::toValue() const
00329 {
00330 typeMatches<T>();
00331
00332 std::istringstream decodingStream(fEncodedValue);
00333 T realValue;
00334
00335 decodingStream >> realValue;
00336 return realValue;
00337 }
00338
00339
00340 template<class T> void
00341 StDbFieldI::typeMatches() const
00342 {
00343
00344
00345 std::map<EDataType,std::string>::const_iterator t = fTypeMap.find(getType());
00346
00347 if (t == fTypeMap.end()) {
00348 throw StDataException( "Type mismatch in field '" + fName + "': Actual type "
00349 + getTypeAsString() + std::string(", requested type ")
00350 + typeid(T).name(),
00351 StDataException::FIELD,
00352 StUCMException::ERROR);
00353 }
00354 }
00355 }
00356 #endif