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 #include <string>
00033 #include <iostream>
00034
00035 #include "StDbFieldI.h"
00036 using namespace std;
00037 using namespace TxLogging;
00038
00039
00040 std::map<StDbFieldI::EDataType,std::string> StDbFieldI::fTypeMap;
00041 std::map<StDbFieldI::EDataType,std::string> StDbFieldI::fTypeMapName;
00042 std::map<std::string,StDbFieldI::EDataType> StDbFieldI::fTypeMapInv;
00043 namespace TxLogging {
00044 struct Init_StDbFieldI {
00045 Init_StDbFieldI() { StDbFieldI::MakeTypeMap();}
00046 };
00047 }
00048 namespace {
00049 Init_StDbFieldI a;
00050 }
00051
00052 StDbFieldI::StDbFieldI(const char* name, StDbFieldI::EDataType type, int length)
00053 : fType(kINVALID),
00054 fEncodedValue(""),
00055 fNull(true),
00056 fIgnore(true) {
00057
00058 if (string(name).empty()) {
00059 throw StDataException("Attempted to define field with empty name.",
00060 StDataException::FIELD,
00061 StUCMException::ERROR);
00062 }
00063
00064 if (type == kSTRING)
00065 {
00066 if (length < 0)
00067 {
00068 throw StDataException("String type specified with length < 0 in field '"
00069 + string(name) + "'.",
00070 StDataException::FIELD,
00071 StUCMException::ERROR);
00072 }
00073 fMaxLength = length;
00074 }
00075 else
00076 {
00077 fMaxLength = 0;
00078 }
00079
00080 fName = name;
00081 fType = type;
00082 fIgnore = false;
00083 }
00084
00085
00086
00087
00088
00089
00090
00091 StDbFieldI::StDbFieldI(const char* name, const char*value,
00092 StDbFieldI::EDataType type, int length)
00093 : fType(kINVALID),
00094 fIgnore(true)
00095 {
00096 if (string(name).empty()) {
00097 throw StDataException("Attempted to define field with empty name.",
00098 StDataException::FIELD,
00099 StUCMException::ERROR);
00100 }
00101
00102 if (type == kSTRING)
00103 {
00104 if (length <= 0)
00105 {
00106 throw StDataException("String type specified with length <= 0 in field '"
00107 + string(name) + "'.",
00108 StDataException::FIELD,
00109 StUCMException::ERROR);
00110 }
00111 fMaxLength = length;
00112 }
00113 else
00114 {
00115 fMaxLength = 0;
00116 }
00117
00118 fName = name;
00119 fType = type;
00120 if (!value) value = " ";
00121 setValueFromString(value);
00122 #ifdef DEBUG
00123 cout << __FUNCTION__
00124 << ": name =" << name
00125 << "; value =" << value
00126 << " type =" << type
00127 << "; lenght=" << length
00128 << endl;
00129 #endif
00130 }
00131
00132
00133
00134 StDbFieldI::StDbFieldI(const StDbFieldI &f) {
00135 copy(f);
00136 }
00137
00138
00140
00141 StDbFieldI::~StDbFieldI()
00142 {}
00143
00144
00145
00146 const StDbFieldI& StDbFieldI::operator=(const StDbFieldI& f) {
00147 if ( this != &f ) {
00148 copy(f);
00149 }
00150 return *this;
00151 }
00152
00153
00154
00155 void StDbFieldI::copy(const StDbFieldI &f) {
00156 fName = f.getName();
00157 fType = f.getType();
00158 fEncodedValue = f.getValueAsString();
00159 fNull = f.isNull();
00160 fMaxLength = f.getMaxLength();
00161 fIgnore = f.isIgnore();
00162 }
00163
00164
00165 const char *
00166 StDbFieldI::getName() const {
00167 return fName.c_str();
00168 }
00169
00170
00171
00172 const char *
00173 StDbFieldI::fieldAsString() const {
00174 static string tostring;
00175 tostring = string(getName()) + "::"
00176 + getValueAsString()
00177 + "::"
00178 + getTypeAsString();
00179 return tostring.c_str();
00180 }
00181
00182
00183
00184 void
00185 StDbFieldI::setValueFromString(const char* strValue)
00186 {
00187 std::string value = strValue;
00188 if (fType == kSTRING) {
00189 if (value.length() > fMaxLength) {
00190 value = string(strValue).substr(0,fMaxLength);
00191 }
00192 fNull = false;
00193 }
00194 else {
00195 fNull =string(strValue).empty();
00196 }
00197
00198 fEncodedValue = value;
00199 fIgnore = false;
00200 }
00201
00202
00203
00204 StDbFieldI::EDataType
00205 StDbFieldI::getType() const
00206 {
00207 return fType;
00208 }
00209
00210
00211
00212 const char *
00213 StDbFieldI::getValueAsString() const
00214 {
00215 return fEncodedValue.c_str();
00216 }
00217
00218
00219 bool
00220 StDbFieldI::isNull() const
00221 {
00222 return fNull;
00223 }
00224
00225
00226
00227 void
00228 StDbFieldI::setNull(bool null)
00229 {
00230 if (null) {
00231 fEncodedValue = "";
00232 }
00233
00234 fIgnore = false;
00235 fNull = null;
00236 }
00237
00238
00239
00240 const char *
00241 StDbFieldI::getTypeAsString() const
00242 {
00243 return fTypeMapName[getType()].c_str();
00244 }
00245
00246
00247
00248 int
00249 StDbFieldI::getMaxLength() const
00250 {
00251 return fMaxLength;
00252 }
00253
00254
00255
00256 bool StDbFieldI::isIgnore() const {
00257 return fIgnore;
00258 }
00259
00260
00261
00262 void StDbFieldI::setIgnore(bool ignore) {
00263 fIgnore = ignore;
00264 }
00265
00266 void StDbFieldI::MakeTypeMap() {
00267 #ifdef TYPEtypeNAME
00268 #error TYPEtypeNAME redefinitions
00269 #else
00270 #define TYPEtypeNAME(TYPENAME,datatype) \
00271 case k##TYPENAME: { \
00272 fTypeMap.insert(pair<EDataType,std::string>(k##TYPENAME,typeid(datatype).name())); \
00273 fTypeMapName.insert(pair<EDataType,std::string>(k##TYPENAME,#TYPENAME)); \
00274 fTypeMapInv.insert(pair<std::string,EDataType>(typeid(datatype).name(),k##TYPENAME)); \
00275 break;}
00276 #endif
00277 for (int i=kBOOL;i<kEND;++i) {
00278 switch (i) {
00279 TYPEtypeNAME(BOOL,bool)
00280 TYPEtypeNAME(INT,int)
00281 TYPEtypeNAME(UINT,unsigned int)
00282 TYPEtypeNAME(LONG,long)
00283 TYPEtypeNAME(ULONG,unsigned long)
00284 TYPEtypeNAME(DOUBLE,double)
00285 TYPEtypeNAME(CHAR,char)
00286 TYPEtypeNAME(UNIXTIME,unsigned int)
00287
00288 default: {
00289 fTypeMap.insert(pair<EDataType,std::string>(kINVALID,"invalid"));
00290 fTypeMapName.insert(pair<EDataType,std::string>(kINVALID,"unkown"));
00291 fTypeMapInv.insert(pair<std::string,EDataType>("unkown",kINVALID));
00292 break;
00293 }
00294 }
00295 }
00296 }
00297 #ifdef DBVALUECONV_
00298 #error DBVALUECONV_ redefinitions
00299 #else
00300 #define DBVALUECONV_(DATATYPE,dataname) \
00301 void StDbFieldI::setValue(const DATATYPE &value) {setValue<DATATYPE>(value);} \
00302 DATATYPE StDbFieldI::to##dataname() const { return toValue<DATATYPE>(); }
00303 #endif
00304 DBVALUECONV_(char,Char)
00305 DBVALUECONV_(int,Int)
00306 DBVALUECONV_(unsigned int,UInt)
00307 DBVALUECONV_(long,Long)
00308 DBVALUECONV_(unsigned long,ULong)
00309 DBVALUECONV_(double,Double)
00310
00311
00312
00313