Belle II Software development
DBObject Class Reference
Inheritance diagram for DBObject:
AbstractDBObject Serializable

Classes

struct  NameValue
 

Public Types

typedef std::vector< DBObject > DBObjectList
 
typedef std::map< std::string, void * > FieldValueList
 
typedef std::map< std::string, std::string > FieldTextList
 
typedef std::map< std::string, DBObjectList > FieldObjectList
 
typedef std::vector< NameValue > NameValueList
 

Public Member Functions

 DBObject (const std::string &path)
 
 DBObject (const DBObject &obj)
 
const DBObject & operator= (const DBObject &obj)
 
int getNObjects (const std::string &name) const
 
DBObjectList & getObjects (const std::string &name)
 
const DBObjectList & getObjects (const std::string &name) const
 
DBObject & getObject (const std::string &name, int i=0)
 
const DBObject & getObject (const std::string &name, int i=0) const
 
void addObjects (const std::string &name, const DBObjectList &obj)
 
void addObject (const std::string &name, const DBObject &obj)
 
DBObject & operator() (const std::string &name, int index=0)
 
const DBObject & operator() (const std::string &name, int index=0) const
 
StringList getNameList (bool isfull) const
 
void print (bool isfull=true) const
 
const std::string sprint (bool isfull) const
 
void printHTML (bool isfull=true) const
 
const std::string printSQL (const std::string &table, int id) const
 
void search (NameValueList &map, const std::string &name="", bool isfull=true) const
 
int getDate () const
 
void setDate (const Date &date)
 
void setDate (int date)
 
const void * getValue (const std::string &name) const override
 
const std::string & getText (const std::string &name) const override
 
void addText (const std::string &name, const std::string &value) override
 
void addValue (const std::string &name, const void *value, DBField::Type type, int length) override
 
void setValue (const std::string &name, const void *value, int length) override
 
void readObject (Reader &reader) override
 
void writeObject (Writer &writer) const override
 
const std::string & getPath () const
 
void setPath (const std::string &path)
 
int getId () const
 
void setId (int id)
 
const std::string & getName () const
 
void setName (const std::string &name)
 
int getIndex () const
 
void setIndex (int index)
 
DBField::NameList & getFieldNames ()
 
const DBField::NameList & getFieldNames () const
 
DBField::Property getProperty (const std::string &name) const
 
bool hasField (const std::string &name) const
 
bool hasValue (const std::string &name) const
 
bool hasText (const std::string &name) const
 
bool hasObject (const std::string &name) const
 
void add (const std::string &name, DBField::Property pro)
 
void setValue (const std::string &name, const std::string &value)
 
void setText (const std::string &name, const std::string &value)
 
bool getBool (const std::string &name, int index=0) const
 
char getChar (const std::string &name, int index=0) const
 
short getShort (const std::string &name, int index=0) const
 
int getInt (const std::string &name, int index=0) const
 
long long getLong (const std::string &name, int index=0) const
 
float getFloat (const std::string &name, int index=0) const
 
double getDouble (const std::string &name, int index=0) const
 
void addBool (const std::string &name, bool value)
 
void addChar (const std::string &name, char value)
 
void addShort (const std::string &name, short value)
 
void addInt (const std::string &name, int value)
 
void addLong (const std::string &name, long long value)
 
void addFloat (const std::string &name, float value)
 
void addDouble (const std::string &name, double value)
 
void setBool (const std::string &name, bool value, int index=0)
 
void setChar (const std::string &name, int value, int index=0)
 
void setShort (const std::string &name, int value, int index=0)
 
void setInt (const std::string &name, int value, int index=0)
 
void setLong (const std::string &name, long long value, int index=0)
 
void setFloat (const std::string &name, float value, int index=0)
 
void setDouble (const std::string &name, double value, int index=0)
 
const std::string getValueText (const std::string &name) const
 
virtual void setValueText (const std::string &name, const std::string &value)
 

Protected Member Functions

void reset () override
 

Private Member Functions

void copy (const DBObject &obj)
 
template<typename T>
T getD (const std::string &name, int index=0) const
 

Private Attributes

FieldValueList m_value_m
 
FieldTextList m_text_m
 
FieldObjectList m_obj_v_m
 
std::string m_empty
 
int m_date
 
int m_index
 
std::string m_path
 
int m_id
 
std::string m_name
 
DBField::NameList m_name_v
 
DBField::PropertyList m_pro_m
 

Detailed Description

Definition at line 21 of file DBObject.h.

Member Typedef Documentation

◆ DBObjectList

typedef std::vector<DBObject> DBObjectList

Definition at line 24 of file DBObject.h.

◆ FieldObjectList

typedef std::map<std::string, DBObjectList> FieldObjectList

Definition at line 27 of file DBObject.h.

◆ FieldTextList

typedef std::map<std::string, std::string> FieldTextList

Definition at line 26 of file DBObject.h.

◆ FieldValueList

typedef std::map<std::string, void*> FieldValueList

Definition at line 25 of file DBObject.h.

◆ NameValueList

typedef std::vector<NameValue> NameValueList

Definition at line 36 of file DBObject.h.

Constructor & Destructor Documentation

◆ DBObject() [1/3]

DBObject ( )

Definition at line 21 of file DBObject.cc.

22{
23 setDate(0);
24}

◆ DBObject() [2/3]

DBObject ( const std::string & path)
explicit

Definition at line 26 of file DBObject.cc.

27{
28 setDate(0);
29 setPath(path);
30}

◆ DBObject() [3/3]

DBObject ( const DBObject & obj)

Definition at line 32 of file DBObject.cc.

33 : AbstractDBObject(obj)
34{
35 copy(obj);
36}

◆ ~DBObject()

~DBObject ( )
overridevirtual

Definition at line 71 of file DBObject.cc.

72{
73 try {
74 DBObject::reset();
75 } catch (...) {
76 }
77}

Member Function Documentation

◆ add()

void add ( const std::string & name,
DBField::Property pro )
inherited

Definition at line 88 of file AbstractDBObject.cc.

89{
90 if (!hasField(name)) {
91 m_name_v.push_back(name);
92 m_pro_m.insert(DBField::PropertyList::value_type(name, pro));
93 }
94}

◆ addObject()

void addObject ( const std::string & name,
const DBObject & obj )

Definition at line 266 of file DBObject.cc.

268{
269 if (!hasField(name)) {
270 add(name, DBField::Property(DBField::OBJECT, 0));
271 m_obj_v_m.insert(FieldObjectList::value_type(name, DBObjectList()));
272 }
273 m_obj_v_m[name].push_back(obj);
274 getProperty(name).setLength(m_obj_v_m[name].size());
275}

◆ addObjects()

void addObjects ( const std::string & name,
const DBObjectList & obj )

Definition at line 277 of file DBObject.cc.

279{
280 if (!hasField(name)) {
281 add(name, DBField::Property(DBField::OBJECT, obj_v.size()));
282 m_obj_v_m.insert(FieldObjectList::value_type(name, obj_v));
283 } else {
284 m_obj_v_m[name] = obj_v;
285 }
286}

◆ addText()

void addText ( const std::string & name,
const std::string & value )
overridevirtual

Implements AbstractDBObject.

Definition at line 255 of file DBObject.cc.

257{
258 if (!hasField(name)) {
259 add(name, DBField::Property(DBField::TEXT, 0));
260 m_text_m.insert(FieldTextList::value_type(name, value));
261 } else {
262 m_text_m[name] = value;
263 }
264}

◆ addValue()

void addValue ( const std::string & name,
const void * value,
DBField::Type type,
int length )
overridevirtual

Implements AbstractDBObject.

Definition at line 228 of file DBObject.cc.

230{
231 DBField::Property pro(type, 0, 0);
232 int size = pro.getTypeSize();
233 if (size <= 0) return;
234 if (!hasValue(name)) {
235 add(name, pro);
236 void* v = malloc(size);
237 if (v == NULL) return;
238 memcpy(v, value, size);
239 m_value_m.insert(FieldValueList::value_type(name, v));
240 } else {
241 memcpy(m_value_m[name], value, size);
242 }
243}

◆ copy()

void copy ( const DBObject & obj)
private

Definition at line 44 of file DBObject.cc.

45{
46 DBObject::reset();
47 setDate(obj.getDate());
48 setIndex(obj.getIndex());
49 setId(obj.getId());
50 setPath(obj.getPath());
51 setName(obj.getName());
52 for (DBField::NameList::const_iterator it = obj.getFieldNames().begin();
53 it != obj.getFieldNames().end(); ++it) {
54 const std::string& name(*it);
55 DBField::Type type = obj.getProperty(name).getType();
56 switch (type) {
57 case DBField::BOOL: addBool(name, obj.getBool(name)); break;
58 case DBField::CHAR: addChar(name, obj.getChar(name)); break;
59 case DBField::SHORT: addShort(name, obj.getShort(name)); break;
60 case DBField::INT: addInt(name, obj.getInt(name)); break;
61 case DBField::LONG: addLong(name, obj.getLong(name)); break;
62 case DBField::FLOAT: addFloat(name, obj.getFloat(name)); break;
63 case DBField::DOUBLE: addDouble(name, obj.getDouble(name)); break;
64 case DBField::TEXT: addText(name, obj.getText(name)); break;
65 case DBField::OBJECT: addObjects(name, obj.getObjects(name)); break;
66 default: break;
67 }
68 }
69}

◆ getD()

template<typename T>
T getD ( const std::string & name,
int index = 0 ) const
inlineprivateinherited

Definition at line 103 of file AbstractDBObject.h.

104 {
105 const void* value = getValue(name);
106 if (value == NULL/* || index >= getProperty(name).getLength()*/) {
107 throw (std::out_of_range(name + " not found"));
108 }
109 DBField::Type type(getProperty(name).getType());
110 if (type == DBField::INT) return (T)((static_cast<const int*>(value))[index]);
111 if (type == DBField::FLOAT) return (T)((static_cast<const float*>(value))[index]);
112 if (type == DBField::DOUBLE) return (T)((static_cast<const double*>(value))[index]);
113 if (type == DBField::SHORT) return (T)((static_cast<const short*>(value))[index]);
114 if (type == DBField::CHAR) return (T)((static_cast<const char*>(value))[index]);
115 if (type == DBField::LONG) return (T)((static_cast<const long*>(value))[index]);
116 return (static_cast<const T*>(value))[index];
117 }

◆ getDate()

int getDate ( ) const
inline

Definition at line 71 of file DBObject.h.

71{ return m_date; }

◆ getFieldNames() [1/2]

DBField::NameList & getFieldNames ( )
inlineinherited

Definition at line 46 of file AbstractDBObject.h.

46{ return m_name_v; }

◆ getFieldNames() [2/2]

const DBField::NameList & getFieldNames ( ) const
inlineinherited

Definition at line 47 of file AbstractDBObject.h.

47{ return m_name_v; }

◆ getId()

int getId ( ) const
inlineinherited

Definition at line 40 of file AbstractDBObject.h.

40{ return m_id; }

◆ getIndex()

int getIndex ( ) const
inlineinherited

Definition at line 44 of file AbstractDBObject.h.

44{ return m_index; }

◆ getName()

const std::string & getName ( ) const
inlineinherited

Definition at line 42 of file AbstractDBObject.h.

42{ return m_name; }

◆ getNameList()

StringList getNameList ( bool isfull) const

Definition at line 395 of file DBObject.cc.

396{
397 const std::string& name_in = "";
398 NameValueList map;
399 search(map, name_in, isfull);
400 StringList str;
401 for (NameValueList::iterator it = map.begin();
402 it != map.end(); ++it) {
403 str.push_back(it->name);
404 }
405 return str;
406}

◆ getNObjects()

int getNObjects ( const std::string & name) const

Definition at line 79 of file DBObject.cc.

80{
81 FieldObjectList::const_iterator it = m_obj_v_m.find(name);
82 if (it != m_obj_v_m.end()) return it->second.size();
83 return 0;
84}

◆ getObject() [1/2]

DBObject & getObject ( const std::string & name,
int i = 0 )

Definition at line 104 of file DBObject.cc.

105{
106 FieldObjectList::iterator it = m_obj_v_m.find(name);
107 if (it != m_obj_v_m.end()) return it->second[i];
108 else throw (std::out_of_range(StringUtil::form("%s not found in %s (%s:%d)",
109 name.c_str(), getName().c_str(),
110 __FILE__, __LINE__)));
111}
TString getName(const TObject *obj)
human-readable name (e.g.
Definition ObjectInfo.cc:45

◆ getObject() [2/2]

const DBObject & getObject ( const std::string & name,
int i = 0 ) const

Definition at line 113 of file DBObject.cc.

114{
115 FieldObjectList::const_iterator it = m_obj_v_m.find(name);
116 if (it != m_obj_v_m.end()) return it->second[i];
117 else throw (std::out_of_range(StringUtil::form("%s not found in %s (%s:%d)",
118 name.c_str(), getName().c_str(),
119 __FILE__, __LINE__)));
120}

◆ getObjects() [1/2]

DBObjectList & getObjects ( const std::string & name)

Definition at line 86 of file DBObject.cc.

87{
88 FieldObjectList::iterator it = m_obj_v_m.find(name);
89 if (it != m_obj_v_m.end()) return it->second;
90 else throw (std::out_of_range(StringUtil::form("%s not found in %s (%s:%d)",
91 name.c_str(), getName().c_str(),
92 __FILE__, __LINE__)));
93}

◆ getObjects() [2/2]

const DBObjectList & getObjects ( const std::string & name) const

Definition at line 95 of file DBObject.cc.

96{
97 FieldObjectList::const_iterator it = m_obj_v_m.find(name);
98 if (it != m_obj_v_m.end()) return it->second;
99 else throw (std::out_of_range(StringUtil::form("%s not found in %s (%s:%d)",
100 name.c_str(), getName().c_str(),
101 __FILE__, __LINE__)));
102}

◆ getPath()

const std::string & getPath ( ) const
inlineinherited

Definition at line 38 of file AbstractDBObject.h.

38{ return m_path; }

◆ getProperty()

DBField::Property getProperty ( const std::string & name) const
inherited

Definition at line 54 of file AbstractDBObject.cc.

55{
56 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
57 if (it != m_pro_m.end()) return it->second;
58 return DBField::Property();
59}

◆ getText()

const std::string & getText ( const std::string & name) const
overridevirtual

Implements AbstractDBObject.

Definition at line 220 of file DBObject.cc.

221{
222 if (!hasText(name)) return m_empty;
223 FieldTextList::const_iterator it = m_text_m.find(name);
224 if (it != m_text_m.end()) return it->second;
225 else throw (std::out_of_range(StringUtil::form("text %s not found", name.c_str())));
226}

◆ getValue()

const void * getValue ( const std::string & name) const
overridevirtual

Implements AbstractDBObject.

Definition at line 212 of file DBObject.cc.

213{
214 if (!hasValue(name)) return NULL;
215 FieldValueList::const_iterator it = m_value_m.find(name);
216 if (it != m_value_m.end()) return it->second;
217 else throw (std::out_of_range(StringUtil::form("value %s not found", name.c_str())));
218}

◆ getValueText()

const std::string getValueText ( const std::string & name) const
inherited

Definition at line 96 of file AbstractDBObject.cc.

98{
99 const static int double_precision = std::numeric_limits<double>::digits10 + 2;
100
101 if (hasField(name)) {
102 switch (getProperty(name).getType()) {
103 case DBField::BOOL: return getBool(name) ? "true" : "false";
104 case DBField::CHAR: return StringUtil::form("%d", (int)getChar(name));
105 case DBField::SHORT: return StringUtil::form("%d", (int)getShort(name));
106 case DBField::INT: return StringUtil::form("%d", (int)getInt(name));
107 case DBField::LONG: return StringUtil::form("%ld", getLong(name));
108 case DBField::FLOAT: return StringUtil::form("%f", getFloat(name));
109 case DBField::DOUBLE: return StringUtil::form("%.*e", double_precision, getDouble(name));
110 case DBField::TEXT: return getText(name);
111 default: break;
112 }
113 }
114 throw (std::out_of_range(name + " not found"));
115}

◆ hasField()

bool hasField ( const std::string & name) const
inherited

Definition at line 61 of file AbstractDBObject.cc.

62{
63 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
64 return (it != m_pro_m.end());
65}

◆ hasObject()

bool hasObject ( const std::string & name) const
inherited

Definition at line 81 of file AbstractDBObject.cc.

82{
83 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
84 return hasField(name) &&
85 it->second.getType() == DBField::OBJECT;
86}

◆ hasText()

bool hasText ( const std::string & name) const
inherited

Definition at line 75 of file AbstractDBObject.cc.

76{
77 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
78 return hasField(name) && it->second.getType() == DBField::TEXT;
79}

◆ hasValue()

bool hasValue ( const std::string & name) const
inherited

Definition at line 67 of file AbstractDBObject.cc.

68{
69 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
70 return hasField(name) &&
71 it->second.getType() != DBField::TEXT &&
72 it->second.getType() != DBField::OBJECT;
73}

◆ operator()() [1/2]

DBObject & operator() ( const std::string & name,
int index = 0 )
inline

Definition at line 55 of file DBObject.h.

56 {
57 return getObject(name, index);
58 }

◆ operator()() [2/2]

const DBObject & operator() ( const std::string & name,
int index = 0 ) const
inline

Definition at line 59 of file DBObject.h.

60 {
61 return getObject(name, index);
62 }

◆ operator=()

const DBObject & operator= ( const DBObject & obj)

Definition at line 38 of file DBObject.cc.

39{
40 copy(obj);
41 return *this;
42}

◆ print()

void print ( bool isfull = true) const

Definition at line 288 of file DBObject.cc.

289{
290 const std::string& name_in = "";
291 NameValueList map;
292 search(map, name_in, isfull);
293 size_t length = 0;
294 for (NameValueList::iterator it = map.begin();
295 it != map.end(); ++it) {
296 if (it->name.size() > length) length = it->name.size();
297 }
298 printf("#\n");
299 printf("# DB object (confname = %s) stored at %s\n", getName().c_str(), Date(getDate()).toString());
300 printf("#\n");
301 printf("\n");
302 StringList s = StringUtil::split(getName(), '@');
303 if (s.size() > 1) {
304 printf("%*s : %s\n", int(-length), "nodename", s[0].c_str());
305 printf("%*s : %s\n", int(-length), "config", s[1].c_str());
306 } else {
307 printf("%*s : %s\n", int(-length), "config", getName().c_str());
308 }
309 printf("\n");
310 for (NameValueList::iterator it = map.begin();
311 it != map.end(); ++it) {
312 printf("%*s : %s\n", int(-length), it->name.c_str(), it->value.c_str());
313 }
314 printf("\n");
315 printf("#\n");
316 printf("#\n");
317 printf("#\n");
318}

◆ printHTML()

void printHTML ( bool isfull = true) const

Definition at line 358 of file DBObject.cc.

359{
360 const std::string& name_in = "";
361 NameValueList map;
362 search(map, name_in, isfull);
363 printf("<table>\n");
364 printf("<caption><strong>%s</strong></caption>\n", getName().c_str());
365 printf("<thead><tr><th>Name</th><th>Value</th>\n</tr></thead>\n");
366 printf("<tbody>\n");
367 StringList s = StringUtil::split(getName(), '@');
368 if (s.size() > 1) {
369 printf("<tr><td>nodename</td><td>%s</td>\n", s[0].c_str());
370 printf("<tr><td>config</td><td>%s</td>\n", s[1].c_str());
371 } else {
372 printf("<tr><td>config</td><td>%s</td>\n", getName().c_str());
373 }
374 std::string rcconfig, dbtable, nodename;
375 for (NameValueList::iterator it = map.begin();
376 it != map.end(); ++it) {
377 if (it->name.find("name") != std::string::npos) {
378 nodename = it->value;
379 printf("<tr><td>%s</td><td>%s</td>\n", it->name.c_str(), it->value.c_str());
380 } else if (it->name.find("rcconfig") != std::string::npos) {
381 rcconfig = it->value;
382 } else if (it->name.find("dbtable") != std::string::npos) {
383 dbtable = it->value;
384 printf("<tr><td>%s</td><td><a href=\"./daqconfig.php?db=%s&config=%s@%s\" >%s/%s</a></td>\n",
385 it->name.c_str(), dbtable.c_str(), nodename.c_str(), rcconfig.c_str(),
386 dbtable.c_str(), rcconfig.c_str());
387 } else {
388 printf("<tr><td>%s</td><td>%s</td>\n", it->name.c_str(), it->value.c_str());
389 }
390 }
391 printf("</tbody>\n");
392 printf("</table>\n");
393}

◆ printSQL()

const std::string printSQL ( const std::string & table,
int id ) const

Definition at line 479 of file DBObject.cc.

480{
481 std::stringstream ss;
482
483 const std::string& name_in = "";
484 NameValueList map;
485 bool isfull = false;
486 search(map, name_in, isfull);
487 for (NameValueList::iterator it = map.begin();
488 it != map.end(); ++it) {
489 const NameValue& nv(*it);
490 std::string value = nv.value;
491 std::string::size_type pos = value.find_first_of("(");
492 DBField::Type type = DBField::TEXT;
493 if (pos != std::string::npos) {
494 std::string type_s = value.substr(0, pos);
495 if (type_s == "bool") type = DBField::BOOL;
496 else if (type_s == "char") type = DBField::CHAR;
497 else if (type_s == "short") type = DBField::SHORT;
498 else if (type_s == "int") type = DBField::INT;
499 else if (type_s == "float") type = DBField::FLOAT;
500 else if (type_s == "double") type = DBField::DOUBLE;
501 else if (type_s == "object") type = DBField::OBJECT;
502 if (type != DBField::TEXT) {
503 value = StringUtil::replace(value.substr(pos + 1), ")", "");
504 }
505 }
506
507 switch (nv.type) {
508 case DBField::BOOL:
509 ss << "insert into " << table << " (pid,name,value_b) values "
510 << "(" << id << ",'" << nv.name << "'," << value << ");" << std::endl;
511 break;
512 case DBField::INT:
513 ss << "insert into " << table << " (pid,name,value_i) values "
514 << "(" << id << ",'" << nv.name << "'," << value << ");" << std::endl;
515 break;
516 case DBField::FLOAT:
517 case DBField::DOUBLE:
518 ss << "insert into " << table << " (pid,name,value_f) values "
519 << "(" << id << ",'" << nv.name << "'," << value << ");" << std::endl;
520 break;
521 case DBField::TEXT:
522 ss << "insert into " << table << " (pid,name,value_t) values "
523 << "(" << id << ",'" << nv.name << "','" << value << "');" << std::endl;
524 break;
525 default : break;
526 }
527 }
528 return ss.str();
529}

◆ readObject()

void readObject ( Reader & reader)
overridevirtual

Implements Serializable.

Definition at line 142 of file DBObject.cc.

143{
144 reset();
145 setPath(reader.readString());
146 setName(reader.readString());
147 setDate(reader.readInt());
148 int npar = reader.readInt();
149 for (int i = 0; i < npar; i++) {
150 std::string name = reader.readString();
151 DBField::Type type = (DBField::Type)reader.readInt();
152 switch (type) {
153 case DBField::BOOL: addBool(name, reader.readBool()); break;
154 case DBField::CHAR: addChar(name, reader.readChar()); break;
155 case DBField::SHORT: addShort(name, reader.readShort()); break;
156 case DBField::INT: addInt(name, reader.readInt()); break;
157 case DBField::LONG: addLong(name, reader.readLong()); break;
158 case DBField::FLOAT: addFloat(name, reader.readFloat()); break;
159 case DBField::DOUBLE: addDouble(name, reader.readDouble()); break;
160 case DBField::TEXT: addText(name, reader.readString()); break;
161 case DBField::OBJECT: {
162 DBObjectList obj_v;
163 int nobj = reader.readInt();
164 for (int n = 0; n < nobj; n++) {
165 DBObject obj;
166 obj.readObject(reader);
167 obj.setIndex(n);
168 obj_v.push_back(obj);
169 }
170 addObjects(name, obj_v);
171 }; break;
172 default: break;
173 }
174 }
175}

◆ reset()

void reset ( )
overrideprotectedvirtual

Reimplemented from AbstractDBObject.

Definition at line 122 of file DBObject.cc.

123{
124 const DBField::NameList& name_v(getFieldNames());
125 for (size_t ii = 0; ii < name_v.size(); ii++) {
126 const std::string& name(name_v[ii]);
127 if (hasObject(name)) {
128 size_t nobj = getNObjects(name);
129 for (size_t i = 0; i < nobj; i++) getObject(name, i).reset();
130 }
131 }
132 for (FieldValueList::iterator it = m_value_m.begin();
133 it != m_value_m.end(); ++it) {
134 free(it->second);
135 }
136 m_value_m = FieldValueList();
137 m_text_m = FieldTextList();
138 m_obj_v_m = FieldObjectList();
139 AbstractDBObject::reset();
140}

◆ search()

void search ( NameValueList & map,
const std::string & name = "",
bool isfull = true ) const

Definition at line 408 of file DBObject.cc.

410{
411 const DBField::NameList& name_v(getFieldNames());
412 for (DBField::NameList::const_iterator it = name_v.begin();
413 it != name_v.end(); ++it) {
414 const std::string& name(*it);
415 const DBField::Property& pro(getProperty(name));
416 std::string name_out = name_in;
417 if (name_in.size() > 0) name_out += ".";
418 name_out += name;
419 std::string ptype;
420 switch (pro.getType()) {
421 case DBField::BOOL: ptype = "bool"; break;
422 case DBField::INT: ptype = "int"; break;
423 case DBField::FLOAT: ptype = "float"; break;
424 case DBField::DOUBLE: ptype = "double"; break;
425 default : break;
426 }
427 if (pro.getType() == DBField::OBJECT) {
428 int length = getNObjects(name);
429 if (!isfull && getObject(name).getPath().size() > 0) {
430 const DBObjectList& objs(getObjects(name));
431 if (length == 1 || objs[1].getPath().size() == 0 || objs[0].getPath() == objs[1].getPath()) {
432 std::string value = objs[0].getPath();
433 value = "object(" + value + ")";
434 NameValue nv;
435 nv.name = name_out.c_str();
436 nv.value = value;
437 nv.type = pro.getType();
438 map.push_back(nv);
439 } else {
440 for (int i = 0; i < length; i++) {
441 std::string value = objs[i].getPath();
442 value = "object(" + value + ")";
443 NameValue nv;
444 nv.name = StringUtil::form("%s[%d].%s", name.c_str(), i, name_out.c_str());
445 nv.value = value;
446 nv.type = pro.getType();
447 map.push_back(nv);
448 }
449 }
450 } else {
451 size_t lengthCur = getNObjects(name);
452 if (lengthCur > 1) {
453 const DBObjectList& objs(getObjects(name));
454 for (size_t i = 0; i < lengthCur; i++) {
455 objs[i].search(map, StringUtil::form("%s[%d]", name_out.c_str(), i), isfull);
456 }
457 } else {
458 const DBObject& obj(getObject(name));
459 obj.search(map, name_out, isfull);
460 }
461 }
462 } else {
463 std::string value = getValueText(name);
464 if (ptype.size() > 0) value = ptype + "(" + value + ")";
465 NameValue nv;
466 nv.name = name_out;
467 nv.value = value;
468 nv.type = pro.getType();
469 if (pro.getType() != DBField::TEXT) {
470 nv.buf = const_cast<void*>(getValue(name));
471 } else {
472 nv.buf = const_cast<void*>(static_cast<const void*>(&getText(name)));
473 }
474 map.push_back(nv);
475 }
476 }
477}

◆ setDate() [1/2]

void setDate ( const Date & date)
inline

Definition at line 72 of file DBObject.h.

72{ m_date = date.get(); }

◆ setDate() [2/2]

void setDate ( int date)
inline

Definition at line 73 of file DBObject.h.

73{ m_date = date; }

◆ setId()

void setId ( int id)
inlineinherited

Definition at line 41 of file AbstractDBObject.h.

41{ m_id = id; }

◆ setIndex()

void setIndex ( int index)
inlineinherited

Definition at line 45 of file AbstractDBObject.h.

45{ m_index = index; }

◆ setName()

void setName ( const std::string & name)
inlineinherited

Definition at line 43 of file AbstractDBObject.h.

43{ m_name = name; }

◆ setPath()

void setPath ( const std::string & path)
inlineinherited

Definition at line 39 of file AbstractDBObject.h.

39{ m_path = path; }

◆ setText()

void setText ( const std::string & name,
const std::string & value )
inlineinherited

Definition at line 55 of file AbstractDBObject.h.

55{ addText(name, value); }

◆ setValue()

void setValue ( const std::string & name,
const void * value,
int length )
overridevirtual

Implements AbstractDBObject.

Definition at line 245 of file DBObject.cc.

247{
248 const DBField::Property& pro(getProperty(name));
249 int size = pro.getTypeSize();
250 if (hasField(name) && size > 0) {
251 memcpy(m_value_m[name], value, size);
252 }
253}

◆ setValueText()

void setValueText ( const std::string & name,
const std::string & value )
virtualinherited

Definition at line 117 of file AbstractDBObject.cc.

119{
120 if (hasField(name)) {
121 switch (getProperty(name).getType()) {
122 case DBField::BOOL: setBool(name, value == "true" || value == "t"); break;
123 case DBField::CHAR: {
124 if (StringUtil::find(value, "0x"))
125 setChar(name, (char)strtol(value.c_str(), NULL, 0));
126 else
127 setChar(name, (char)atoi(value.c_str()));
128 } break;
129 case DBField::SHORT: {
130 if (StringUtil::find(value, "0x"))
131 setShort(name, (short)strtol(value.c_str(), NULL, 0));
132 else
133 setShort(name, (short)atoi(value.c_str()));
134 } break;
135 case DBField::INT: {
136 if (StringUtil::find(value, "0x"))
137 setInt(name, (int)strtol(value.c_str(), NULL, 0));
138 else
139 setInt(name, (int)atoi(value.c_str()));
140 } break;
141 case DBField::LONG: setLong(name, (long long)atoi(value.c_str())); break;
142 case DBField::FLOAT: setFloat(name, atof(value.c_str())); break;
143 case DBField::DOUBLE: setDouble(name, atof(value.c_str())); break;
144 case DBField::TEXT: setText(name, value); break;
145 default: break;
146 }
147 return;
148 }
149 throw (std::out_of_range(name + " not found"));
150}

◆ sprint()

const std::string sprint ( bool isfull) const

Definition at line 320 of file DBObject.cc.

321{
322 std::stringstream ss;
323 const std::string& name_in = "";
324 NameValueList map;
325 search(map, name_in, isfull);
326 size_t length = 0;
327 for (NameValueList::iterator it = map.begin();
328 it != map.end(); ++it) {
329 if (it->name.size() > length) length = it->name.size();
330 }
331 ss << "#" << std::endl;
332 ss << "# DB object (confname = " << getName() << ")" << std::endl;
333 ss << "#" << std::endl;
334 ss << "" << std::endl;
335 StringList s = StringUtil::split(getName(), '@');
336 if (s.size() > 1) {
337 ss << StringUtil::form("%*s : %s\n", -length, "nodename", s[0].c_str())
338 << std:: endl;
339 ss << StringUtil::form("%*s : %s\n", -length, "config", s[1].c_str())
340 << std:: endl;
341 } else {
342 ss << StringUtil::form("%*s : %s\n", -length, "config", getName().c_str())
343 << std:: endl;
344 }
345 ss << "" << std::endl;
346 for (NameValueList::iterator it = map.begin();
347 it != map.end(); ++it) {
348 ss << StringUtil::form("%*s : %s\n", -length, it->name.c_str(), it->value.c_str())
349 << std::endl;
350 }
351 ss << "" << std::endl;
352 ss << "#" << std::endl;
353 ss << "#" << std::endl;
354 ss << "#" << std::endl;
355 return ss.str();
356}

◆ writeObject()

void writeObject ( Writer & writer) const
overridevirtual

Implements Serializable.

Definition at line 177 of file DBObject.cc.

178{
179 writer.writeString(getPath());
180 writer.writeString(getName());
181 writer.writeInt(getDate());
182 const DBField::NameList& name_v(getFieldNames());
183 writer.writeInt(name_v.size());
184 for (DBField::NameList::const_iterator iname = name_v.begin();
185 iname != name_v.end(); ++iname) {
186 const std::string name = *iname;
187 DBField::Type type = getProperty(name).getType();
188 writer.writeString(name);
189 writer.writeInt(type);
190 switch (type) {
191 case DBField::BOOL: writer.writeBool(getBool(name)); break;
192 case DBField::CHAR: writer.writeChar(getChar(name)); break;
193 case DBField::SHORT: writer.writeShort(getShort(name)); break;
194 case DBField::INT: writer.writeInt(getInt(name)); break;
195 case DBField::LONG: writer.writeLong(getLong(name)); break;
196 case DBField::FLOAT: writer.writeFloat(getFloat(name)); break;
197 case DBField::DOUBLE: writer.writeDouble(getDouble(name)); break;
198 case DBField::TEXT: writer.writeString(getText(name)); break;
199 case DBField::OBJECT: {
200 const DBObjectList& obj_v(getObjects(name));
201 writer.writeInt(obj_v.size());
202 for (DBObjectList::const_iterator iobj = obj_v.begin();
203 iobj != obj_v.end(); ++iobj) {
204 iobj->writeObject(writer);
205 }
206 }; break;
207 default: break;
208 }
209 }
210}

Member Data Documentation

◆ m_date

int m_date
private

Definition at line 92 of file DBObject.h.

◆ m_empty

std::string m_empty
private

Definition at line 91 of file DBObject.h.

◆ m_id

int m_id
privateinherited

Definition at line 96 of file AbstractDBObject.h.

◆ m_index

int m_index
privateinherited

Definition at line 94 of file AbstractDBObject.h.

◆ m_name

std::string m_name
privateinherited

Definition at line 97 of file AbstractDBObject.h.

◆ m_name_v

DBField::NameList m_name_v
privateinherited

Definition at line 98 of file AbstractDBObject.h.

◆ m_obj_v_m

FieldObjectList m_obj_v_m
private

Definition at line 90 of file DBObject.h.

◆ m_path

std::string m_path
privateinherited

Definition at line 95 of file AbstractDBObject.h.

◆ m_pro_m

DBField::PropertyList m_pro_m
privateinherited

Definition at line 99 of file AbstractDBObject.h.

◆ m_text_m

FieldTextList m_text_m
private

Definition at line 89 of file DBObject.h.

◆ m_value_m

FieldValueList m_value_m
private

Definition at line 88 of file DBObject.h.


The documentation for this class was generated from the following files: