3#include "rdbModel/Tables/Table.h"
4#include "rdbModel/Tables/Column.h"
5#include "rdbModel/Tables/Index.h"
6#include "rdbModel/Tables/Assertion.h"
7#include "rdbModel/Db/Connection.h"
8#include "rdbModel/Db/ResultHandle.h"
9#include "rdbModel/RdbException.h"
10#include "rdbModel/Tables/InsertNew.h"
11#include "rdbModel/Tables/InterRow.h"
12#include "rdbModel/Tables/Query.h"
13#include "rdbModel/Tables/Set.h"
14#include "rdbModel/Tables/Supersede.h"
15#include "facilities/Util.h"
16#include "facilities/Timestamp.h"
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;
29 while (m_cols.size() ) {
35 while (m_asserts.size() ) {
41 while (m_indices.size() ) {
42 Index* i = m_indices.back();
57 unsigned nCol = m_sortedCols.size();
61 unsigned guess = maxI/2;
62 unsigned oldGuess = nCol;
64 int cmp = name.compare(m_sortedCols[guess]->
getName());
67 if (guess == oldGuess)
return 0;
75 guess = (minI + maxI)/2;
76 cmp = name.compare(m_sortedCols[guess]->
getName());
78 return m_sortedCols[guess];
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;
92 unsigned nAssert = m_asserts.size();
93 for (
unsigned i = 0; i < nAssert; i++) {
95 if (a->
getName() == name)
return a;
108 unsigned n = m_cols.size();
110 for (
unsigned i = 0; i <
n; i++) {
111 state = m_cols[i]->accept(
v);
115 n = m_indices.size();
116 for (
unsigned i = 0; i <
n; i++) {
117 state = m_indices[i]->accept(
v);
121 n = m_asserts.size();
122 for (
unsigned i = 0; i <
n; i++) {
123 state = m_asserts[i]->accept(
v);
130 if (m_sorted)
return;
131 m_sortedCols = m_cols;
137 std::sort (m_sortedCols.begin(), m_sortedCols.end(), cmpObject);
142 void Table::addColumn(
Column* c) {
148 m_userCols.push_back(c);
151 m_mayDefault.push_back(c);
155 m_mayDefault.push_back(c);
158 m_programCols.push_back(c);
165 throw RdbException(
"Table::insertLatest Need matching connection");
170 fillProgramCols(row,
true);
173 for (
unsigned i = 0; i < m_userCols.size(); i++) {
175 if ((!f) || (f->
m_null)) {
177 msg(
"Table::insertLatest Row to be inserted missing req'd field ");
178 msg = msg + m_userCols[i]->getName();
190 satisfied = cond->
verify(row, row);
193 (*m_out) << ex.
getMsg() << std::endl;
197 if (!satisfied)
return -1;
202 std::vector<std::string> colNames;
203 std::vector<std::string> colValues;
204 std::vector<std::string> nullCols;
206 row.
regroup(colNames, colValues, nullCols);
208 satisfied = condO->
verify(empty, row);
212 bool ok = m_connect->
insertRow(m_name, colNames, colValues,
214 return (ok) ? 0 : -1;
217 const std::vector<InterRow* >& inter = m_iNew->
getInterRow();
219 unsigned nInter = inter.size();
221 for (
unsigned iInter = 0; iInter < nInter; iInter++) {
222 const Query*
q = inter[iInter]->getQuery();
223 const std::vector<std::string>& toSelect =
q->getToSelect();
227 subsAssert =
new Assertion(
q->getAssertion(), &row);
229 std::vector<std::string> orderCols;
241 if (inter[iInter]->getQuit()) {
243 (*m_out) <<
"insert latest not done because of irreconcilable "
244 << std::endl <<
"conflict with existing rows" << std::endl;
250 std::vector<Set> sets = inter[iInter]->getSets();
252 bool iu = doInterUpdate(sets, subsAssert, row);
256 bool ok = m_connect->
insertRow(m_name, colNames, colValues,
258 return (ok) ? 0 : -1;
264 throw RdbException(
"Table::insertRow Need matching connection");
269 fillProgramCols(row,
true);
273 for (
unsigned i = 0; i < m_userCols.size(); i++) {
275 if ((!f) || (f->
m_null)) {
277 msg(
"Table::insertRow Row to be inserted missing req'd field ");
278 msg = msg + m_userCols[i]->getName();
290 satisfied = m_validRow->
verify(row, row);
293 (*m_out) << ex.
getMsg() << std::endl;
298 (*m_out) <<
"Table::insertRow Row to be inserted is not valid "
305 std::vector<std::string> colNames;
306 std::vector<std::string> colValues;
307 std::vector<std::string> nullCols;
309 row.
regroup(colNames, colValues, nullCols);
310 bool ok = m_connect->
insertRow(m_name, colNames, colValues,
312 return (ok) ? 0 : -1;
316 std::string oldKeyStr;
319 if (m_primaryKeyCol.size() == 0) {
320 throw RdbException(
"Table::supersedeRow No primary key column!");
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])) {
331 (*m_out) <<
"Table::supersedeRow bad column in input row '"
332 << forced[i] <<
"'" << std::endl;
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)) {
343 (*m_out) <<
"Table::supersedeRow bad column in input row " <<
351 if (!isSupersedable(oldKeyStr)) {
352 *m_out <<
"Row " << oldKey <<
" is not supersedable" << std::endl;
358 const std::vector<std::string>& fixedInterp = m_sup->
getFixedInterp();
359 for (
unsigned i = 0; i < fixed.size(); i++) {
362 if (fixedInterp[i].size() > 0) {
376 std::vector<std::string> noCols;
377 const std::vector<std::string>& fromOld = m_sup->
getFromOld();
383 std::vector<std::string*> vals;
387 for (
unsigned i = 0; i < nDef; i++) {
388 if (!(row.
find(fromOld[i])) ) {
399 for (
unsigned i = nDef; i < fromOld.size(); i++) {
415 (*m_out) << ex.
getMsg() << std::endl;
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() ) {
433 c->
interpret(setOld[i]->getInterp(), src);
435 oldFields.push_back(
FieldVal(col, src));
437 Row updateArg(oldFields);
440 if (iUpdated == 1)
return 0;
444 (*m_out) << uEx.
getMsg() << std::endl;
452 throw RdbException(
"Table::insertLatest Need matching connection");
457 fillProgramCols(row,
false);
459 std::vector<std::string> colNames;
460 std::vector<std::string> colValues;
461 std::vector<std::string> nullCols;
463 row.
regroup(colNames, colValues, nullCols);
465 return m_connect->
update(m_name, colNames, colValues, where, &nullCols);
468 bool Table::fillProgramCols(
Row& row,
bool newRow)
const {
471 for (
unsigned i = 0; i < m_programCols.size(); i++) {
472 Column* col = m_programCols[i];
479 val = std::string(
"$(USER)");
481 val = std::string(
"$(USERNAME)");
492 if (!newRow)
continue;
508 FieldVal g(col->
getName(), val,
false);
516 void Table::fillDefaults(Row& row)
const {
517 for (
unsigned i = 0; i < m_mayDefault.size(); i++) {
518 Column* hasDef = m_mayDefault[i];
519 FieldVal* f = row.find(hasDef->getName());
522 if ((hasDef->getDefault()).size() > 0) {
523 FieldVal g(hasDef->getName(), hasDef->getDefault());
527 FieldVal g(hasDef->getName(),
"",
true);
538 bool Table::doInterUpdate(
const std::vector<Set>& sets,
539 Assertion* subsAssert, Row& toBe)
const {
540 unsigned nSets = sets.size();
541 std::vector<FieldVal> fields;
542 fields.reserve(nSets);
544 for (
unsigned iSet = 0; iSet < sets.size(); iSet++) {
545 std::string col = sets[iSet].getDestColName();
548 FIELDTYPE srcType = sets[iSet].getSrcType();
550 src = sets[iSet].getSrcValue();
552 if (sets[iSet].hasInterp()) {
554 c->interpret(sets[iSet].getInterp(), src);
560 std::string toBeCol = sets[iSet].getSrcValue();
561 FieldVal* f = toBe.find(toBeCol);
564 (*m_out) <<
"Table::InsertNew Row argument missing needed field"
565 << toBeCol << std::endl;
574 FieldVal g(col, src);
579 fillProgramCols(row,
false);
581 std::vector<std::string> updateCols;
582 std::vector<std::string> updateVals;
583 std::vector<std::string> nullCols;
585 row.regroup(updateCols, updateVals, nullCols);
588 m_connect->
update(m_name, updateCols, updateVals, subsAssert);
594 const std::string& Table::setPrimaryKeyCol() {
595 static const std::string empty(
"");
596 if (m_primaryKeyCol.size() > 0)
return m_primaryKeyCol;
598 for (
unsigned iCol = 0; iCol < m_cols.size(); iCol++) {
599 if (m_cols[iCol]->isPrimaryKey()) {
600 m_primaryKeyCol = m_cols[iCol]->getName();
601 return m_primaryKeyCol;
607 bool Table::isSupersedable(std::string oldKeyStr)
const {
610 Assertion::Operator* onlyIf =
611 new Assertion::Operator((m_sup->
getOnlyIf())->getOperator(),
614 Assertion::Operator* ser =
615 new Assertion::Operator(
OPTYPEequal, m_primaryKeyCol,
617 std::vector<Assertion::Operator*> children;
618 children.push_back(onlyIf);
619 children.push_back(ser);
620 Assertion::Operator* whereOp =
621 new Assertion::Operator(
OPTYPEand, children);
623 std::vector<std::string> queryCols;
624 queryCols.push_back(m_primaryKeyCol);
625 Assertion* where =
new Assertion(whereOp);
626 ResultHandle* results = m_connect->
select(m_name, queryCols, queryCols,
630 return (results->getNRows() > 0);
****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
**********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
std::string getString() const
Return string representation of time, not including nanoseconds;.
static int expandEnvVar(std::string *toExpand, const std::string &openDel=std::string("$("), const std::string &closeDel=std::string(")"))
static const char * itoa(int val, std::string &outStr)
bool verify(Row &old, Row &toBe) const
const std::string & getName() const
Function object used to sort columns by column name.
CONTENTS getContentsType() const
const std::string & getDefault() const
const std::string & getName() const
FROM getSourceType() const
bool nullAllowed() const
Returns true if column may take on value NULL.
bool interpret(const std::string &interpType, std::string &val)
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 unsigned int update(const std::string &tableName, const StringVector &colNames, const StringVector &values, const Assertion *where=0, const StringVector *nullCols=0)=0
virtual std::ostream * getOut() const =0
virtual bool insertRow(const std::string &tableName, const StringVector &colNames, const StringVector &values, int *auto_value=0, const StringVector *nullCols=0)=0
virtual std::ostream * getErrOut() const =0
const std::string & getName() const
const Assertion * getInternal() const
const Assertion * getOfficial() const
const std::vector< InterRow * > & getInterRow() const
virtual std::string getMsg()
static void cleanFieldPtrs(std::vector< std::string * > &fields)
virtual bool getRowPtrs(std::vector< std::string * > &fields, unsigned int i=0, bool clear=true)=0
virtual unsigned int getNRows() const =0
Return number of rows in results.
FieldVal * find(std::string colname)
void regroup(std::vector< std::string > &colNames, std::vector< std::string > &colVals, std::vector< std::string > &nullCols) const
Reorder information suitable for Connection::insert.
void addField(const FieldVal &f)
const std::vector< FieldVal > & getFixed() const
const std::vector< std::string > & getFixedInterp() const
const std::vector< std::string > & getFromOld() const
const std::vector< std::string > & getOldDefaulted() const
const std::vector< Set * > & getSetOld() const
const std::vector< std::string > & getForced() const
Visitor::VisitorState accept(Visitor *v)
int insertRow(Row &row, int *serial=0) const
Index * getIndexByName(const std::string &name) const
const std::string & getName() const
int insertLatest(Row &row, int *serial=0) const
int supersedeRow(Row &row, int oldKey, int *newKey=0) const
Assertion * getAssertionByName(const std::string &name) const
int updateRows(Row &row, Assertion *where) const
Column * getColumnByName(const std::string &name) const
void setConnection(Connection *connect)