BOSS 7.0.1
BESIII Offline Software System
Loading...
Searching...
No Matches
rdbModel::Table Class Reference

#include <Table.h>

Public Member Functions

 Table ()
 
 ~Table ()
 
void setConnection (Connection *connect)
 
const std::string & getName () const
 
ColumngetColumnByName (const std::string &name) const
 
IndexgetIndexByName (const std::string &name) const
 
AssertiongetAssertionByName (const std::string &name) const
 
int insertLatest (Row &row, int *serial=0) const
 
int insertRow (Row &row, int *serial=0) const
 
int supersedeRow (Row &row, int oldKey, int *newKey=0) const
 
int updateRows (Row &row, Assertion *where) const
 
InsertNewgetInsertNew () const
 
SupersedegetSupersede () const
 
Visitor::VisitorState accept (Visitor *v)
 
void sortColumns ()
 
 Table ()
 
 ~Table ()
 
void setConnection (Connection *connect)
 
const std::string & getName () const
 
ColumngetColumnByName (const std::string &name) const
 
IndexgetIndexByName (const std::string &name) const
 
AssertiongetAssertionByName (const std::string &name) const
 
int insertLatest (Row &row, int *serial=0) const
 
int insertRow (Row &row, int *serial=0) const
 
int supersedeRow (Row &row, int oldKey, int *newKey=0) const
 
int updateRows (Row &row, Assertion *where) const
 
InsertNewgetInsertNew () const
 
SupersedegetSupersede () const
 
Visitor::VisitorState accept (Visitor *v)
 
void sortColumns ()
 

Friends

class rdbModel::XercesBuilder
 
class rdbModel::Rdb
 

Detailed Description

rdbModel representation of a(n SQL-like) table description

Definition at line 33 of file Calibration/rdbModel/rdbModel-00-01-01/rdbModel/Tables/Table.h.

Constructor & Destructor Documentation

◆ Table() [1/2]

rdbModel::Table::Table ( )

Definition at line 20 of file Table.cxx.

20 : m_primaryKeyCol(""), m_sorted(false), m_nEndUser(0),
21 m_validRow(0), m_iNew(0), m_sup(0), m_connect(0) {
22 m_sortedCols.clear(); m_programCols.clear();
23 m_userCols.clear(); m_mayDefault.clear();
24 m_out = &std::cout; m_err = &std::cerr;
25 }

◆ ~Table() [1/2]

rdbModel::Table::~Table ( )

Definition at line 28 of file Table.cxx.

28 {
29 while (m_cols.size() ) {
30 Column* c = m_cols.back();
31 m_cols.pop_back();
32 delete c;
33 }
34
35 while (m_asserts.size() ) {
36 Assertion* a = m_asserts.back();
37 m_asserts.pop_back();
38 delete a;
39 }
40
41 while (m_indices.size() ) {
42 Index* i = m_indices.back();
43 m_indices.pop_back();
44 delete i;
45 }
46 }

◆ Table() [2/2]

rdbModel::Table::Table ( )

◆ ~Table() [2/2]

rdbModel::Table::~Table ( )

Member Function Documentation

◆ accept() [1/2]

Visitor::VisitorState rdbModel::Table::accept ( Visitor v)

Definition at line 102 of file Table.cxx.

102 {
103
104 Visitor::VisitorState state = v->visitTable(this);
105 if (state == Visitor::VBRANCHDONE) return Visitor::VCONTINUE;
106 if (state != Visitor::VCONTINUE) return state;
107
108 unsigned n = m_cols.size();
109
110 for (unsigned i = 0; i < n; i++) {
111 state = m_cols[i]->accept(v);
112 if (state != Visitor::VCONTINUE) return state;
113 }
114
115 n = m_indices.size();
116 for (unsigned i = 0; i < n; i++) {
117 state = m_indices[i]->accept(v);
118 if (state != Visitor::VCONTINUE) return state;
119 }
120
121 n = m_asserts.size();
122 for (unsigned i = 0; i < n; i++) {
123 state = m_asserts[i]->accept(v);
124 if (state != Visitor::VCONTINUE) return state;
125 }
126 return state;
127 }
const Int_t n
**********Class see also m_nmax DOUBLE PRECISION m_amel DOUBLE PRECISION m_x2 DOUBLE PRECISION m_alfinv DOUBLE PRECISION m_Xenph INTEGER m_KeyWtm INTEGER m_idyfs DOUBLE PRECISION m_zini DOUBLE PRECISION m_q2 DOUBLE PRECISION m_Wt_KF DOUBLE PRECISION m_WtCut INTEGER m_KFfin *COMMON c_KarLud $ !Input CMS energy[GeV] $ !CMS energy after beam spread beam strahlung[GeV] $ !Beam energy spread[GeV] $ !z boost due to beam spread $ !electron beam mass *ff pair spectrum $ !minimum v
Definition: KarLud.h:35

◆ accept() [2/2]

Visitor::VisitorState rdbModel::Table::accept ( Visitor v)

◆ getAssertionByName() [1/2]

Assertion * rdbModel::Table::getAssertionByName ( const std::string &  name) const

Definition at line 91 of file Table.cxx.

91 {
92 unsigned nAssert = m_asserts.size();
93 for (unsigned i = 0; i < nAssert; i++) {
94 Assertion* a = m_asserts[i];
95 if (a->getName() == name) return a;
96 }
97 return 0;
98
99 }

◆ getAssertionByName() [2/2]

Assertion * rdbModel::Table::getAssertionByName ( const std::string &  name) const

◆ getColumnByName() [1/2]

Column * rdbModel::Table::getColumnByName ( const std::string &  name) const

Definition at line 56 of file Table.cxx.

56 {
57 unsigned nCol = m_sortedCols.size();
58 unsigned minI = 0;
59 unsigned maxI = nCol;
60
61 unsigned guess = maxI/2;
62 unsigned oldGuess = nCol;
63
64 int cmp = name.compare(m_sortedCols[guess]->getName());
65
66 while (cmp != 0) {
67 if (guess == oldGuess) return 0; // not found
68
69 if (cmp < 0 ) { // thing we tried is > colName, so decrease maxI
70 maxI = guess;
71 } else { // thing we tried is > colName, so increase minI
72 minI = guess;
73 }
74 oldGuess = guess;
75 guess = (minI + maxI)/2;
76 cmp = name.compare(m_sortedCols[guess]->getName());
77 }
78 return m_sortedCols[guess];
79
80 }

Referenced by calibUtil::Metadata::checkNulls(), calibUtil::Metadata::checkValues(), rdbModel::Rdb::getColumn(), supersedeRow(), and rdbModel::Assertion::Operator::validCompareOp().

◆ getColumnByName() [2/2]

Column * rdbModel::Table::getColumnByName ( const std::string &  name) const

◆ getIndexByName() [1/2]

Index * rdbModel::Table::getIndexByName ( const std::string &  name) const

Definition at line 82 of file Table.cxx.

82 {
83 unsigned nIx = m_indices.size();
84 for (unsigned iIx = 0; iIx < nIx; iIx++) {
85 Index* index = m_indices[iIx];
86 if (index->getName() == name) return index;
87 }
88 return 0;
89 }

Referenced by rdbModel::Rdb::getIndex().

◆ getIndexByName() [2/2]

Index * rdbModel::Table::getIndexByName ( const std::string &  name) const

◆ getInsertNew() [1/2]

InsertNew * rdbModel::Table::getInsertNew ( ) const
inline

Definition at line 87 of file Calibration/rdbModel/rdbModel-00-01-01/rdbModel/Tables/Table.h.

87{return m_iNew;}

◆ getInsertNew() [2/2]

InsertNew * rdbModel::Table::getInsertNew ( ) const
inline

Definition at line 87 of file InstallArea/include/rdbModel/rdbModel/Tables/Table.h.

87{return m_iNew;}

◆ getName() [1/2]

const std::string & rdbModel::Table::getName ( ) const
inline

◆ getName() [2/2]

const std::string & rdbModel::Table::getName ( ) const
inline

Definition at line 40 of file InstallArea/include/rdbModel/rdbModel/Tables/Table.h.

40{ return m_name;}

◆ getSupersede() [1/2]

Supersede * rdbModel::Table::getSupersede ( ) const
inline

Definition at line 88 of file Calibration/rdbModel/rdbModel-00-01-01/rdbModel/Tables/Table.h.

88{return m_sup;}

◆ getSupersede() [2/2]

Supersede * rdbModel::Table::getSupersede ( ) const
inline

Definition at line 88 of file InstallArea/include/rdbModel/rdbModel/Tables/Table.h.

88{return m_sup;}

◆ insertLatest() [1/2]

int rdbModel::Table::insertLatest ( Row row,
int *  serial = 0 
) const

insertLatest is smart in the following respects: o Makes some checks to see if row is self-consistent o Fills in all fields which are to be supplied by "service" o If row satisfies conditions for being "official"

  • queries table to see if any pre-existing rows need adjustment; if so, they are adjusted o Finally inserts the row

If test is true, just output description of what would be done without actually doing it. Note row may be modified by the function. If a non-zero pointer is supplied for serial it will contain the primary key value (if there is one) of the new row on exit.

Definition at line 162 of file Table.cxx.

162 {
163
164 if (!m_connect) {
165 throw RdbException("Table::insertLatest Need matching connection");
166 }
167 row.rowSort();
168
169 // Fill in columns in m_programCols list
170 fillProgramCols(row, true);
171
172 // Check that all required columns are there and not null
173 for (unsigned i = 0; i < m_userCols.size(); i++) {
174 FieldVal* f = row.find(m_userCols[i]->getName());
175 if ((!f) || (f->m_null)) {
176 std::string
177 msg("Table::insertLatest Row to be inserted missing req'd field ");
178 msg = msg + m_userCols[i]->getName();
179 throw RdbException(msg);
180 }
181 }
182 // Fill in defaults
183 fillDefaults(row);
184
185 // Check against consistency conditions specified by <insertNew>
186 const Assertion* cond = m_iNew->getInternal();
187 bool satisfied;
188
189 try {
190 satisfied = cond->verify(row, row);
191 }
192 catch (RdbException ex) {
193 (*m_out) << ex.getMsg() << std::endl;
194 return -1;
195 }
196
197 if (!satisfied) return -1; // or maybe throw exception here?
198
199 // Check against official conditions specified by <insertNew>
200 const Assertion* condO = m_iNew->getOfficial();
201 Row empty;
202 std::vector<std::string> colNames;
203 std::vector<std::string> colValues;
204 std::vector<std::string> nullCols;
205
206 row.regroup(colNames, colValues, nullCols);
207
208 satisfied = condO->verify(empty, row);
209
210 // If not official, do insert and exit
211 if (!satisfied) {
212 bool ok = m_connect->insertRow(m_name, colNames, colValues,
213 serial, &nullCols);
214 return (ok) ? 0 : -1;
215 }
216
217 const std::vector<InterRow* >& inter = m_iNew->getInterRow();
218
219 unsigned nInter = inter.size();
220 Assertion* subsAssert;
221 for (unsigned iInter = 0; iInter < nInter; iInter++) {
222 const Query* q = inter[iInter]->getQuery();
223 const std::vector<std::string>& toSelect = q->getToSelect();
224
225 // Need to make a new Assertion which substitues our proposed row
226 // values for all "toBe" entries
227 subsAssert = new Assertion(q->getAssertion(), &row);
228
229 std::vector<std::string> orderCols;
230 orderCols.clear();
231 ResultHandle* r = m_connect->select(m_name, toSelect, orderCols,
232 subsAssert);
233
234 if (r->getNRows() == 0) { // no conflicts found
235 delete subsAssert;
236 continue; // go on to next InterRow, if any.
237 }
238
239 // otherwise
240 // if quit go ahead and quit
241 if (inter[iInter]->getQuit()) {
242 delete subsAssert;
243 (*m_out) << "insert latest not done because of irreconcilable "
244 << std::endl << "conflict with existing rows" << std::endl;
245 return -1;
246 }
247 // else modify conflicting rows as specified in <interRow>.
248 // (i.e., do an update with where clause = subsAssert
249
250 std::vector<Set> sets = inter[iInter]->getSets();
251
252 bool iu = doInterUpdate(sets, subsAssert, row);
253 if (!iu) return -1;
254 }
255 // Insert and exit
256 bool ok = m_connect->insertRow(m_name, colNames, colValues,
257 serial, &nullCols);
258 return (ok) ? 0 : -1;
259 }
****INTEGER imax DOUBLE PRECISION m_pi *DOUBLE PRECISION m_amfin DOUBLE PRECISION m_Chfin DOUBLE PRECISION m_Xenph DOUBLE PRECISION m_sinw2 DOUBLE PRECISION m_GFermi DOUBLE PRECISION m_MfinMin DOUBLE PRECISION m_ta2 INTEGER m_out INTEGER m_KeyFSR INTEGER m_KeyQCD *COMMON c_Semalib $ !copy of input $ !CMS energy $ !beam mass $ !final mass $ !beam charge $ !final charge $ !smallest final mass $ !Z mass $ !Z width $ !EW mixing angle $ !Gmu Fermi $ alphaQED at q
Definition: KKsem.h:33
virtual ResultHandle * select(const std::string &tableName, const StringVector &getCols, const StringVector &orderCols, const Assertion *where=0, int rowLimit=0, int rowOffset=0)=0
virtual bool insertRow(const std::string &tableName, const StringVector &colNames, const StringVector &values, int *auto_value=0, const StringVector *nullCols=0)=0

◆ insertLatest() [2/2]

int rdbModel::Table::insertLatest ( Row row,
int *  serial = 0 
) const

insertLatest is smart in the following respects: o Makes some checks to see if row is self-consistent o Fills in all fields which are to be supplied by "service" o If row satisfies conditions for being "official"

  • queries table to see if any pre-existing rows need adjustment; if so, they are adjusted o Finally inserts the row

If test is true, just output description of what would be done without actually doing it. Note row may be modified by the function. If a non-zero pointer is supplied for serial it will contain the primary key value (if there is one) of the new row on exit.

◆ insertRow() [1/2]

int rdbModel::Table::insertRow ( Row row,
int *  serial = 0 
) const

The less smart insertRow function fills in all fields which are to be supplied by "service" and inserts the row, without any further checks. Good return value is 0

Definition at line 261 of file Table.cxx.

261 {
262
263 if (!m_connect) {
264 throw RdbException("Table::insertRow Need matching connection");
265 }
266 row.rowSort();
267
268 // Fill in columns in m_programCols list
269 fillProgramCols(row, true);
270
271
272 // Check that all required columns are there and not null
273 for (unsigned i = 0; i < m_userCols.size(); i++) {
274 FieldVal* f = row.find(m_userCols[i]->getName());
275 if ((!f) || (f->m_null)) {
276 std::string
277 msg("Table::insertRow Row to be inserted missing req'd field ");
278 msg = msg + m_userCols[i]->getName();
279 throw RdbException(msg);
280 }
281 }
282
283 // Fill in defaults
284 fillDefaults(row);
285
286 // Check standard valid row condition if there is one
287 if (m_validRow) {
288 bool satisfied;
289 try {
290 satisfied = m_validRow->verify(row, row);
291 }
292 catch (RdbException ex) {
293 (*m_out) << ex.getMsg() << std::endl;
294 return -1;
295 }
296 if (!satisfied) {
297 // throw RdbException("Table::insertRow Row to be inserted is not valid");
298 (*m_out) << "Table::insertRow Row to be inserted is not valid "
299 << std::endl;
300 m_out->flush();
301 return -1;
302 }
303 }
304 // Insert and exit
305 std::vector<std::string> colNames;
306 std::vector<std::string> colValues;
307 std::vector<std::string> nullCols;
308
309 row.regroup(colNames, colValues, nullCols);
310 bool ok = m_connect->insertRow(m_name, colNames, colValues,
311 serial, &nullCols);
312 return (ok) ? 0 : -1;
313 }

Referenced by supersedeRow().

◆ insertRow() [2/2]

int rdbModel::Table::insertRow ( Row row,
int *  serial = 0 
) const

The less smart insertRow function fills in all fields which are to be supplied by "service" and inserts the row, without any further checks. Good return value is 0

◆ setConnection() [1/2]

void rdbModel::Table::setConnection ( Connection connect)

Definition at line 49 of file Table.cxx.

49 {
50 m_connect = connect;
51 m_out = connect->getOut();
52 m_err = connect->getErrOut();
53 }

◆ setConnection() [2/2]

void rdbModel::Table::setConnection ( Connection connect)

◆ sortColumns() [1/2]

void rdbModel::Table::sortColumns ( )

Definition at line 129 of file Table.cxx.

129 {
130 if (m_sorted) return;
131 m_sortedCols = m_cols;
132
133 ColCompare cmpObject;
134
135 // Keep around original set so that rdbGUI will display them in
136 // this order
137 std::sort (m_sortedCols.begin(), m_sortedCols.end(), cmpObject);
138 m_sorted = true;
139
140 }

◆ sortColumns() [2/2]

void rdbModel::Table::sortColumns ( )

◆ supersedeRow() [1/2]

int rdbModel::Table::supersedeRow ( Row row,
int  oldKey,
int *  newKey = 0 
) const

Given some input values and primary key of an existing row, insert a new row which will supersede the old, according to prescription laid down in <supersede> element of the xml description of the dbs.
Good return value is 0

Definition at line 315 of file Table.cxx.

315 {
316 std::string oldKeyStr;
317 facilities::Util::itoa(oldKey, oldKeyStr);
318
319 if (m_primaryKeyCol.size() == 0) {
320 throw RdbException("Table::supersedeRow No primary key column!");
321 }
322
323 // Confirm that row does not contain any of the fixed or oldForced columns
324 row.rowSort();
325 const std::vector<std::string>& forced = m_sup->getForced();
326 for (unsigned i = 0; i < forced.size(); i++) {
327 if (row.find(forced[i])) {
328 // throw RdbException
329 // (std::string("Table::supersedeRow bad column in input row ") +
330 // forced[i]);
331 (*m_out) << "Table::supersedeRow bad column in input row '"
332 << forced[i] << "'" << std::endl;
333 m_out->flush();
334 return -1;
335 }
336 }
337 const std::vector<FieldVal>& fixed = m_sup->getFixed();
338 for (unsigned i = 0; i < fixed.size(); i++) {
339 if (row.find(fixed[i].m_colname)) {
340 // throw RdbException
341 // (std::string("Table::supersedeRow bad column in input row ") +
342 // fixed[i].m_colname);
343 (*m_out) << "Table::supersedeRow bad column in input row " <<
344 fixed[i].m_colname;
345 m_out->flush();
346 return -1;
347 }
348 }
349
350 // Check that old row is supersedable
351 if (!isSupersedable(oldKeyStr)) {
352 *m_out << "Row " << oldKey << " is not supersedable" << std::endl;
353 m_out->flush();
354 return -1;
355 }
356
357 // Fill in fixed fields
358 const std::vector<std::string>& fixedInterp = m_sup->getFixedInterp();
359 for (unsigned i = 0; i < fixed.size(); i++) {
360
361 FieldVal fv = fixed[i];
362 if (fixedInterp[i].size() > 0) {
363 Column* c = getColumnByName(fixed[i].m_colname);
364 c->interpret(fixedInterp[i], fv.m_val);
365 }
366 row.addField(fv);
367 }
368 row.rowSort();
369
370 // Fetch needed fields from row to be superseded (oldKey)
371 Assertion::Operator* whereOp =
372 new Assertion::Operator(OPTYPEequal, m_primaryKeyCol,
373 oldKeyStr, FIELDTYPEold, FIELDTYPElit);
374 Assertion* where = new Assertion(whereOp);
375
376 std::vector<std::string> noCols;
377 const std::vector<std::string>& fromOld = m_sup->getFromOld();
378 noCols.clear();
379 ResultHandle* results = m_connect->select(m_name, fromOld,
380 noCols, where);
381 // std::vector<std::string> vals;
382 // results->getRow(vals);
383 std::vector<std::string*> vals;
384 results->getRowPtrs(vals);
385 // Merge as needed into our row. First bunch may already be in the row
386 unsigned nDef = m_sup->getOldDefaulted().size();
387 for (unsigned i = 0; i < nDef; i++) {
388 if (!(row.find(fromOld[i])) ) {
389 if (vals[i] == 0) {
390 row.addField(FieldVal(fromOld[i], "", true));
391 }
392 else {
393 row.addField(FieldVal(fromOld[i], *vals[i]));
394 }
395 row.rowSort();
396 }
397 }
398 // Remainder definitely have to be merged in
399 for (unsigned i = nDef; i < fromOld.size(); i++) {
400 if (vals[i] == 0) {
401 row.addField(FieldVal(fromOld[i], "", true));
402 }
403 else {
404 row.addField(FieldVal(fromOld[i], *vals[i]));
405 }
406 }
407 results->cleanFieldPtrs(vals);
408
409 // Insert the row and update old row
410 int insRet;
411 try {
412 insRet = insertRow(row, serial);
413 }
414 catch (RdbException ex) {
415 (*m_out) << ex.getMsg() << std::endl;
416 insRet = -1;
417 }
418
419 if (insRet) { // something went wrong
420 delete where;
421 return insRet;
422 }
423
424 // Do the update of old row
425 const std::vector<Set*>& setOld = m_sup->getSetOld();
426 std::vector<FieldVal> oldFields;
427 oldFields.reserve(setOld.size());
428 for (unsigned i = 0; i < setOld.size(); i++) {
429 std::string src = setOld[i]->getSrcValue();
430 std::string col = setOld[i]->getDestColName();
431 if (setOld[i]->hasInterp() ) {
432 Column* c = getColumnByName(col);
433 c->interpret(setOld[i]->getInterp(), src);
434 }
435 oldFields.push_back(FieldVal(col, src));
436 }
437 Row updateArg(oldFields);
438 try {
439 int iUpdated = updateRows(updateArg, where);
440 if (iUpdated == 1) return 0;
441 return -1;
442 }
443 catch (RdbException uEx) {
444 (*m_out) << uEx.getMsg() << std::endl;
445 return -1;
446 }
447 }
static const char * itoa(int val, std::string &outStr)
virtual bool getRowPtrs(std::vector< std::string * > &fields, unsigned int i=0, bool clear=true)=0
const std::vector< std::string > & getOldDefaulted() const
int insertRow(Row &row, int *serial=0) const
Definition: Table.cxx:261
int updateRows(Row &row, Assertion *where) const
Definition: Table.cxx:449
Column * getColumnByName(const std::string &name) const
Definition: Table.cxx:56

◆ supersedeRow() [2/2]

int rdbModel::Table::supersedeRow ( Row row,
int  oldKey,
int *  newKey = 0 
) const

Given some input values and primary key of an existing row, insert a new row which will supersede the old, according to prescription laid down in <supersede> element of the xml description of the dbs.
Good return value is 0

◆ updateRows() [1/2]

int rdbModel::Table::updateRows ( Row row,
Assertion where 
) const

Silently fills in "service" fields as well as requested updates Good return value is 0

Definition at line 449 of file Table.cxx.

449 {
450
451 if (!m_connect) {
452 throw RdbException("Table::insertLatest Need matching connection");
453 }
454 row.rowSort();
455
456 // Fill in columns in m_programCols list
457 fillProgramCols(row, false);
458
459 std::vector<std::string> colNames;
460 std::vector<std::string> colValues;
461 std::vector<std::string> nullCols;
462
463 row.regroup(colNames, colValues, nullCols);
464
465 return m_connect->update(m_name, colNames, colValues, where, &nullCols);
466 }
virtual unsigned int update(const std::string &tableName, const StringVector &colNames, const StringVector &values, const Assertion *where=0, const StringVector *nullCols=0)=0

Referenced by supersedeRow().

◆ updateRows() [2/2]

int rdbModel::Table::updateRows ( Row row,
Assertion where 
) const

Silently fills in "service" fields as well as requested updates Good return value is 0

Friends And Related Function Documentation

◆ rdbModel::Rdb

◆ rdbModel::XercesBuilder


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