8#include "daq/slc/database/DBObject.h"
10#include <daq/slc/base/StringUtil.h>
11#include <daq/slc/base/Reader.h>
12#include <daq/slc/base/Writer.h>
26DBObject::DBObject(
const std::string& path)
32DBObject::DBObject(
const DBObject& obj)
44void DBObject::copy(
const DBObject& obj)
47 setDate(obj.getDate());
48 setIndex(obj.getIndex());
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();
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;
79int DBObject::getNObjects(
const std::string& name)
const
81 FieldObjectList::const_iterator it = m_obj_v_m.find(name);
82 if (it != m_obj_v_m.end())
return it->second.size();
86DBObjectList& DBObject::getObjects(
const std::string& name)
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__)));
95const DBObjectList& DBObject::getObjects(
const std::string& name)
const
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__)));
104DBObject& DBObject::getObject(
const std::string& name,
int i)
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__)));
113const DBObject& DBObject::getObject(
const std::string& name,
int i)
const
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__)));
122void DBObject::reset()
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();
132 for (FieldValueList::iterator it = m_value_m.begin();
133 it != m_value_m.end(); ++it) {
136 m_value_m = FieldValueList();
137 m_text_m = FieldTextList();
138 m_obj_v_m = FieldObjectList();
139 AbstractDBObject::reset();
142void DBObject::readObject(
Reader& reader)
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();
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: {
163 int nobj = reader.readInt();
164 for (
int n = 0; n < nobj; n++) {
166 obj.readObject(reader);
168 obj_v.push_back(obj);
170 addObjects(name, obj_v);
177void DBObject::writeObject(
Writer& writer)
const
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);
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);
212const void* DBObject::getValue(
const std::string& name)
const
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())));
220const std::string& DBObject::getText(
const std::string& name)
const
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())));
228void DBObject::addValue(
const std::string& name,
const void* value,
229 DBField::Type type,
int)
231 DBField::Property pro(type, 0, 0);
232 int size = pro.getTypeSize();
233 if (size <= 0)
return;
234 if (!hasValue(name)) {
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));
241 memcpy(m_value_m[name], value, size);
245void DBObject::setValue(
const std::string& name,
246 const void* value,
int)
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);
255void DBObject::addText(
const std::string& name,
256 const std::string& value)
258 if (!hasField(name)) {
259 add(name, DBField::Property(DBField::TEXT, 0));
260 m_text_m.insert(FieldTextList::value_type(name, value));
262 m_text_m[name] = value;
266void DBObject::addObject(
const std::string& name,
269 if (!hasField(name)) {
270 add(name, DBField::Property(DBField::OBJECT, 0));
271 m_obj_v_m.insert(FieldObjectList::value_type(name, DBObjectList()));
273 m_obj_v_m[name].push_back(obj);
274 getProperty(name).setLength(m_obj_v_m[name].size());
277void DBObject::addObjects(
const std::string& name,
278 const DBObjectList& obj_v)
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));
284 m_obj_v_m[name] = obj_v;
288void DBObject::print(
bool isfull)
const
290 const std::string& name_in =
"";
292 search(map, name_in, isfull);
294 for (NameValueList::iterator it = map.begin();
295 it != map.end(); ++it) {
296 if (it->name.size() > length) length = it->name.size();
299 printf(
"# DB object (confname = %s) stored at %s\n", getName().c_str(), Date(getDate()).toString());
302 StringList s = StringUtil::split(getName(),
'@');
304 printf(
"%*s : %s\n",
int(-length),
"nodename", s[0].c_str());
305 printf(
"%*s : %s\n",
int(-length),
"config", s[1].c_str());
307 printf(
"%*s : %s\n",
int(-length),
"config", getName().c_str());
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());
320const std::string DBObject::sprint(
bool isfull)
const
322 std::stringstream ss;
323 const std::string& name_in =
"";
325 search(map, name_in, isfull);
327 for (NameValueList::iterator it = map.begin();
328 it != map.end(); ++it) {
329 if (it->name.size() > length) length = it->name.size();
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(),
'@');
337 ss << StringUtil::form(
"%*s : %s\n", -length,
"nodename", s[0].c_str())
339 ss << StringUtil::form(
"%*s : %s\n", -length,
"config", s[1].c_str())
342 ss << StringUtil::form(
"%*s : %s\n", -length,
"config", getName().c_str())
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())
351 ss <<
"" << std::endl;
352 ss <<
"#" << std::endl;
353 ss <<
"#" << std::endl;
354 ss <<
"#" << std::endl;
358void DBObject::printHTML(
bool isfull)
const
360 const std::string& name_in =
"";
362 search(map, name_in, isfull);
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");
367 StringList s = StringUtil::split(getName(),
'@');
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());
372 printf(
"<tr><td>config</td><td>%s</td>\n", getName().c_str());
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) {
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());
388 printf(
"<tr><td>%s</td><td>%s</td>\n", it->name.c_str(), it->value.c_str());
391 printf(
"</tbody>\n");
392 printf(
"</table>\n");
395StringList DBObject::getNameList(
bool isfull)
const
397 const std::string& name_in =
"";
399 search(map, name_in, isfull);
401 for (NameValueList::iterator it = map.begin();
402 it != map.end(); ++it) {
403 str.push_back(it->name);
408void DBObject::search(NameValueList& map,
const std::string& name_in,
bool isfull)
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 +=
".";
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;
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 +
")";
435 nv.name = name_out.c_str();
437 nv.type = pro.getType();
440 for (
int i = 0; i < length; i++) {
441 std::string value = objs[i].getPath();
442 value =
"object(" + value +
")";
444 nv.name = StringUtil::form(
"%s[%d].%s", name.c_str(), i, name_out.c_str());
446 nv.type = pro.getType();
451 size_t lengthCur = getNObjects(name);
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);
458 const DBObject& obj(getObject(name));
459 obj.search(map, name_out, isfull);
463 std::string value = getValueText(name);
464 if (ptype.size() > 0) value = ptype +
"(" + value +
")";
468 nv.type = pro.getType();
469 if (pro.getType() != DBField::TEXT) {
470 nv.buf =
const_cast<void*
>(getValue(name));
472 nv.buf =
const_cast<void*
>(
static_cast<const void*
>(&getText(name)));
479const std::string DBObject::printSQL(
const std::string& table,
int id)
const
481 std::stringstream ss;
483 const std::string& name_in =
"";
486 search(map, name_in, isfull);
487 for (NameValueList::iterator it = map.begin();
488 it != map.end(); ++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),
")",
"");
509 ss <<
"insert into " << table <<
" (pid,name,value_b) values "
510 <<
"(" <<
id <<
",'" << nv.name <<
"'," << value <<
");" << std::endl;
513 ss <<
"insert into " << table <<
" (pid,name,value_i) values "
514 <<
"(" <<
id <<
",'" << nv.name <<
"'," << value <<
");" << std::endl;
517 case DBField::DOUBLE:
518 ss <<
"insert into " << table <<
" (pid,name,value_f) values "
519 <<
"(" <<
id <<
",'" << nv.name <<
"'," << value <<
");" << std::endl;
522 ss <<
"insert into " << table <<
" (pid,name,value_t) values "
523 <<
"(" <<
id <<
",'" << nv.name <<
"','" << value <<
"');" << std::endl;
Abstract base class for different kinds of events.