Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
nutools::dbi::Table Class Reference

#include "/cvmfs/nova.opensciencegrid.org/externals/nuevdb/v1_00_02/source/nuevdb/IFDatabase/Table.h"

Public Member Functions

 Table ()
 
 Table (std::string schemaName, std::string tableName, int tableType=kGenericTable, std::string dbhost="", std::string dbname="", std::string dbport="", std::string dbuser="")
 
 ~Table ()
 
std::string Name ()
 
std::string DBName ()
 
std::string DBHost ()
 
std::string User ()
 
std::string Role ()
 
std::string DBPort ()
 
int TableType ()
 
int DataSource ()
 
int DataTypeMask ()
 
void SetTableName (std::string tname)
 
void SetTableName (const char *tname)
 
void SetDataSource (std::string ds)
 
void SetDataSource (int ids)
 
bool SetTableType (int t)
 
void SetDataTypeMask (int mask)
 
void SetIgnoreEnvVar (bool f)
 
void SetUser (std::string uname)
 
void SetUser (const char *uname)
 
bool SetRole (std::string role)
 
bool SetRole (const char *role)
 
void SetDBName (std::string dbname)
 
void SetDBName (const char *dbname)
 
void SetDBHost (std::string dbhost)
 
void SetDBHost (const char *dbhost)
 
void SetDBPort (std::string p)
 
void SetDBPort (const char *p)
 
void SetDBInfo (std::string name, std::string host, std::string port, std::string user)
 
void SetDBInfo (const char *name, const char *host, const char *port, const char *user)
 
bool SetPasswordFile (const char *fname=0)
 fname should be the name of the file More...
 
void ClearPassword ()
 
void DisablePasswordAccess ()
 
void ResetRole ()
 
void SetVerbosity (int i)
 
int NCol ()
 
int NRow ()
 
void Clear ()
 
void ClearRows ()
 
nutools::dbi::Row *const GetRow (int i)
 
void AddRow (const Row *row)
 
void AddRow (const Row &row)
 
int AddCol (std::string cname, std::string ctype)
 
void AddEmptyRows (unsigned int nrow)
 
bool RemoveRow (int i)
 
nutools::dbi::Row *const NewRow ()
 
std::vector< std::stringGetColNames ()
 
std::map< std::string, intGetColNameToIndexMap ()
 
std::string GetColName (int i)
 
int GetColIndex (std::string cname)
 
const nutools::dbi::ColumnDefGetCol (int i)
 
const nutools::dbi::ColumnDefGetCol (std::string &cname)
 
const nutools::dbi::ColumnDefGetCol (const char *cname)
 
void SetTolerance (std::string &cname, float t)
 
float Tolerance (std::string &cname)
 
bool ExistsInDB ()
 
bool ExecuteSQL (std::string cmd, PGresult *&res)
 
bool LoadFromCSV (std::string fname)
 
bool LoadFromCSV (const char *fname)
 
bool LoadFromDB ()
 
bool WriteToDB (bool commit=true)
 use commit=false if just testing More...
 
bool WriteToCSV (std::string fname, bool appendToFile=false, bool writeColNames=false)
 
bool WriteToCSV (const char *fname, bool appendToFile=false, bool writeColNames=false)
 
void ClearValidity ()
 
bool AddDistinctColumn (unsigned int i)
 
bool AddDistinctColumn (std::string col)
 
bool AddDistinctColumn (const char *col)
 
bool RemoveDistinctColumn (unsigned int i)
 
bool RemoveDistinctColumn (std::string col)
 
bool RemoveDistinctColumn (const char *col)
 
bool AddOrderColumn (unsigned int i)
 
bool AddOrderColumn (std::string col)
 
bool AddOrderColumn (const char *col)
 
bool RemoveOrderColumn (unsigned int i)
 
bool RemoveOrderColumn (std::string col)
 
bool RemoveOrderColumn (const char *col)
 
void SetSelectLimit (int limit)
 
void SetSelectOffset (int offset)
 
void SetOrderDesc ()
 
void SetOrderAsc ()
 
void AddExcludeCol (std::string col)
 
void ClearExcludedCols ()
 
bool GetCurrSeqVal (std::string col, long &iseq)
 
bool GetCurrSeqVal (const char *col, long &iseq)
 
int GetNPKeyCol ()
 
const nutools::dbi::ColumnDefGetPKeyCol (int i)
 
void PrintPQErrorMsg () const
 
std::string GetValiditySQL ()
 
void SetValiditySQL (std::string cmd)
 
bool SetDetector (std::string det)
 
bool GetDetector (std::string &det) const
 
void SetSchema (std::string s)
 
std::string Schema ()
 
template<class T >
bool SetValidityRange (std::string cname, T start, T end)
 
template<class T >
bool SetValidityRange (std::string cname, T start)
 
void RemoveValidityRange (std::string &cname)
 
void RemoveValidityRange (const char *cname)
 
void PrintColumns ()
 
bool GetConnection (int ntry=0)
 
bool CloseConnection ()
 
void SetConnectionTimeout (int n)
 
int GetConnectionTimeout ()
 
bool ResetConnectionInfo ()
 
void CacheDBCommand (std::string cmd)
 
void SetMinTSVld (float t)
 
void SetMaxTSVld (float t)
 
float GetMaxTSVld () const
 
float GetMinTSVld () const
 
void SetTag (std::string s)
 
std::string GetTag ()
 
bool Tag (std::string tn="", bool override=false)
 
bool Load ()
 
bool Write (bool commit=true)
 
void ClearChanRowMap ()
 
void FillChanRowMap ()
 
int NVldRows (uint64_t channel)
 
int NVldChannels ()
 
std::vector< uint64_t > VldChannels ()
 
nutools::dbi::RowGetVldRow (uint64_t channel, float t)
 
std::vector< nutools::dbi::Row * > GetVldRows (uint64_t channel)
 
void SetRecordTime (float t)
 
void ClearRecordTime ()
 
void EnableFlushCache ()
 
void DisableFlushCache ()
 
void DisableCache ()
 
void EnableCache ()
 
void SetWSURL (std::string url)
 
void SetQEURL (std::string url)
 
void SetTimeQueries (bool f)
 
void SetTimeParsing (bool f)
 
bool TimeQueries ()
 
bool TimeParsing ()
 
void SetMinChannel (uint64_t chan)
 
void SetMaxChannel (uint64_t chan)
 
void SetChannelRange (uint64_t chan1, uint64_t chan2)
 
void PrintVMUsed ()
 
void PrintPMUsed ()
 
bool GetColsFromDB (std::vector< std::string > pkeyList={})
 
void SetFolder (std::string f)
 
std::string Folder ()
 

Private Member Functions

bool LoadConditionsTable ()
 
bool LoadUnstructuredConditionsTable ()
 
bool LoadNonConditionsTable ()
 
bool GetDataFromWebService (Dataset &, std::string)
 
void Reset ()
 
bool GetConnectionInfo (int ntry=0)
 
bool CheckForNulls ()
 
bool MakeConditionsCSVString (std::stringstream &ss)
 
std::string GetPassword ()
 
int ParseSelfStatusLine (char *line)
 

Private Attributes

bool addInsertTime
 
bool addInsertUser
 
bool addUpdateTime
 
bool addUpdateUser
 
bool fIgnoreEnvVar
 
bool fValidityChanged
 
bool fDescOrder
 
bool fIgnoreDB
 
bool fTestedExists
 
bool fExistsInDB
 
bool fHasConnection
 
bool fHasRecordTime
 
bool fFlushCache
 
bool fDisableCache
 
bool fTimeQueries
 
bool fTimeParsing
 
short fVerbosity
 
int fSelectLimit
 
int fSelectOffset
 
int fConnectionTimeout
 
int fTableType
 
int fDataTypeMask
 
int fDataSource
 
uint64_t fMinChannel
 
uint64_t fMaxChannel
 
std::string fTableName
 
std::string fUser
 
std::string fRole
 
std::string fDBPort
 
std::string fDBHost
 
std::string fDBName
 
std::string fSchema
 
std::string fDBCacheFile
 
std::string fPassword
 
std::string fValiditySQL
 
std::string fDetector
 
std::string fFolder
 
std::string fTag
 
std::string fWSURL
 
std::string fUConDBURL
 
std::string fQEURL
 
std::vector< nutools::dbi::ColumnDeffCol
 
std::vector< nutools::dbi::RowfRow
 
std::vector< nutools::dbi::ColumnDeffValidityStart
 
std::vector< nutools::dbi::ColumnDeffValidityEnd
 
std::vector< const nutools::dbi::ColumnDef * > fPKeyList
 
std::vector< const nutools::dbi::ColumnDef * > fDistinctCol
 
std::vector< const nutools::dbi::ColumnDef * > fOrderCol
 
std::vector< std::pair< int, int > > fNullList
 
std::vector< std::stringfExcludeCol
 
std::vector< uint64_t > fChannelVec
 
std::unordered_map< uint64_t, std::vector< nutools::dbi::Row * > > fChanRowMap
 
PGconnfConnection
 
float fMaxTSVld
 
float fMinTSVld
 
float fRecordTime
 

Friends

std::ostream & operator<< (std::ostream &stream, const Table &t)
 

Detailed Description

Database Table Interface

Author
Jonathan Paley
Version
Id
Table.h,v 1.61 2013/02/11 20:48:41 jpaley Exp

Definition at line 47 of file Table.h.

Constructor & Destructor Documentation

Table::Table ( )

Definition at line 29 of file Table.cpp.

References addInsertTime, addInsertUser, addUpdateTime, addUpdateUser, fConnection, fConnectionTimeout, fDataSource, fDataTypeMask, fDBHost, fDBName, fDBPort, fDetector, fExistsInDB, fFolder, fHasConnection, fIgnoreDB, fIgnoreEnvVar, fMaxChannel, fMinChannel, fQEURL, fSchema, fTableName, fTableType, fTag, fTestedExists, fTimeParsing, fTimeQueries, fUConDBURL, fUser, fVerbosity, fWSURL, cet::getenv(), nutools::dbi::kGenericTable, nutools::dbi::kUnknownSource, Reset(), and string.

29  : fHasRecordTime(false),
30  fFlushCache(false),
31  fDisableCache(false),
32  fMaxTSVld(0),fMinTSVld(0),
33  fRecordTime(0)
34  {
35  fTableName="";
36  fConnection=0;
37  fHasConnection=false;
38  fDetector="";
39  fDBHost="";
40  fDBName="";
41  fDBPort="";
42  fUser="";
43  fSchema="undef";
45  fDataTypeMask = 0;
47 
48  fIgnoreEnvVar = false;
49  fTestedExists = false;
50  fExistsInDB = false;
51  addInsertTime = addUpdateTime = false;
52  addInsertUser = addUpdateUser = false;
53  fIgnoreDB = false;
54  fTimeQueries = true;
55  fTimeParsing = true;
56  fMinChannel = 0;
57  fMaxChannel = 0;
58  fFolder = "";
59 
60  Reset();
61 
62  srandom(time(0));
63 
64  // default total time to attempt to connect to the db/web server will be
65  // ~4 minutes (some randomness is introduced by libwda)
66  fConnectionTimeout = 4*60;
67  // override default timeout if env. variable is set, but must be
68  // greater than 20 seconds
69  char* tmpStr = getenv("DBITIMEOUT");
70  if (tmpStr) {
71  int tmpTO = atoi(tmpStr);
72  if (tmpTO > 20)
73  fConnectionTimeout = tmpTO;
74  }
75 
76  fTag = "";
77  fWSURL = "";
78  const char* wsHost = getenv("DBIWSURL");
79  if (wsHost) fWSURL = std::string(wsHost);
80 
81  fUConDBURL = "";
82  const char* ucondbHost = getenv("DBIUCONDBURL");
83  if (ucondbHost) fUConDBURL = std::string(ucondbHost);
84 
85  fQEURL = "";
86  const char* qeHost = getenv("DBIQEURL");
87  if (qeHost) fQEURL = std::string(qeHost);
88 
89  fVerbosity=0;
90  tmpStr = getenv("DBIVERB");
91  if (tmpStr) {
92  fVerbosity = atoi(tmpStr);
93  }
94  }
std::string fUser
Definition: Table.h:369
bool addUpdateUser
Definition: Table.h:344
uint64_t fMaxChannel
Definition: Table.h:366
bool fDisableCache
Definition: Table.h:354
bool addInsertTime
Definition: Table.h:341
std::string fQEURL
Definition: Table.h:385
int fConnectionTimeout
Definition: Table.h:361
bool fIgnoreEnvVar
Definition: Table.h:345
::xsd::cxx::tree::time< char, simple_type > time
Definition: Database.h:194
std::string fFolder
Definition: Table.h:380
PGconn * fConnection
Definition: Table.h:401
uint64_t fMinChannel
Definition: Table.h:365
bool fHasRecordTime
Definition: Table.h:352
bool fHasConnection
Definition: Table.h:351
std::string getenv(std::string const &name)
float fRecordTime
Definition: Table.h:407
short fVerbosity
Definition: Table.h:357
std::string fDBHost
Definition: Table.h:372
std::string fSchema
Definition: Table.h:374
std::string fWSURL
Definition: Table.h:383
bool addInsertUser
Definition: Table.h:342
bool addUpdateTime
Definition: Table.h:343
bool fTestedExists
Definition: Table.h:349
std::string fTag
Definition: Table.h:382
std::string fDBPort
Definition: Table.h:371
std::string fTableName
Definition: Table.h:368
std::string fDetector
Definition: Table.h:379
std::string fDBName
Definition: Table.h:373
std::string fUConDBURL
Definition: Table.h:384
enum BeamMode string
Table::Table ( std::string  schemaName,
std::string  tableName,
int  tableType = kGenericTable,
std::string  dbhost = "",
std::string  dbname = "",
std::string  dbport = "",
std::string  dbuser = "" 
)

Definition at line 98 of file Table.cpp.

References om::cerr, CloseConnection(), run_hadd::cmd, dirName, allTimeWatchdog::endl, ExistsInDB(), fCol, fConnection, fConnectionTimeout, fDataSource, fDBCacheFile, fExistsInDB, fFolder, fHasConnection, fIgnoreDB, fMaxChannel, fMinChannel, fSchema, fTimeParsing, fTimeQueries, fVerbosity, GetColsFromDB(), GetConnection(), cet::getenv(), MECModelEnuComparisons::i, findDuplicateFiles::key, nutools::dbi::kUnknownSource, Name(), Reset(), SetDBHost(), SetDBName(), SetDBPort(), SetTableName(), SetUser(), and string.

102  {
103  fConnection=0;
104  fHasConnection=false;
105  std::string errStr;
106  fIgnoreDB = false;
107 
108  fTimeQueries = true;
109  fTimeParsing = true;
110 
111  fMinChannel = 0;
112  fMaxChannel = 0;
113 
114  fFolder = "";
115 
117 
118  fVerbosity=0;
119  char* tmpStr = getenv("DBIVERB");
120  if (tmpStr) {
121  fVerbosity = atoi(tmpStr);
122  }
123 
124  // default total time to attempt to connect to the db/web server will be
125  // ~4 minutes (some randomness is introduced by libwda)
126  fConnectionTimeout = 4*60;
127  // override default timeout if env. variable is set, but must be
128  // greater than 20 seconds
129  tmpStr = getenv("DBITIMEOUT");
130  if (tmpStr) {
131  int tmpTO = atoi(tmpStr);
132  if (tmpTO > 20)
133  fConnectionTimeout = tmpTO;
134  }
135 
136  if (!dbname.empty()) SetDBName(dbname);
137  /*
138  if (DBName() == "") {
139  errStr = "Table::Table(): missing database name!";
140  throw std::runtime_error(errStr);
141  }
142  */
143  if (!dbhost.empty()) SetDBHost(dbhost);
144  /*
145  if (DBHost() == "") {
146  errStr = "Table::Table(): missing database host!";
147  throw std::runtime_error(errStr);
148  }
149  */
150 
151  if (!dbport.empty()) SetDBPort(dbport);
152  if (!dbuser.empty()) SetUser(dbuser);
153 
154  this->SetTableName(tableName);
155  fSchema = std::string(schemaName);
156  boost::to_lower(fSchema);
157 
158  std::string stName = fSchema + std::string(".") + std::string(tableName);
159  // fIgnoreEnvVar = true;
160 
161  if (!ExistsInDB()) {
162  errStr = "Table::Table(): table \'" + stName + "\' not found in database!";
163  throw std::runtime_error(errStr);
164  }
165 
166  Reset();
167  fCol.clear();
168 
169  bool hasConn = fHasConnection;
170  if (! fHasConnection) {
171  GetConnection();
172  hasConn = false;
173  }
174 
175  std::vector<std::string> pkeyList;
176  // get list of rows that are primary keys
177  std::string cmd = "SELECT pg_attribute.attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) FROM pg_index, pg_class, pg_attribute WHERE indrelid = pg_class.oid AND pg_attribute.attrelid = pg_class.oid AND pg_attribute.attnum = any(pg_index.indkey) AND indisprimary AND pg_class.oid = '" + stName + "'::regclass";
178 
179  PGresult* res = PQexec(fConnection,cmd.c_str());
180 
181  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
182  errStr = "Table::Table(): command failed: " + std::string(PQerrorMessage(fConnection));
183  if (fVerbosity > 0)
184  std::cerr << errStr << std::endl;
185  PQclear(res);
186  CloseConnection();
187  throw std::runtime_error(errStr);
188  }
189 
190  int nRow = PQntuples(res);
191  if (nRow == 0) {
192  errStr = std::string("Table::Table(): no primary keys defined for table \'") + tableName + std::string("\', unable to proceed.");
193  PQclear(res);
194  fExistsInDB = false;
195  CloseConnection();
196  throw std::runtime_error(errStr);
197  }
198 
199  for (int i=0; i<nRow; ++i) {
200  std::string key = std::string(PQgetvalue(res,i,0));
201  pkeyList.push_back(key);
202  }
203 
204  PQclear(res);
205 
206  // now get names and types of all columns
207  this->GetColsFromDB(pkeyList);
208 
209  if (!hasConn) CloseConnection();
210 
211  // now set the dB command cache file name
213  tmpStr = getenv("DBICACHEDIR");
214  if (tmpStr)
215  dirName = tmpStr;
216  else {
217  tmpStr = getenv("PWD");
218  if (tmpStr)
219  dirName = tmpStr;
220  else
221  dirName = "/";
222  }
223 
224  fDBCacheFile = dirName + "/" + "." + Name() + ".cache";
225 
226  }
std::string Name()
Definition: Table.h:59
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
uint64_t fMaxChannel
Definition: Table.h:366
OStream cerr
Definition: OStream.cxx:7
int fConnectionTimeout
Definition: Table.h:361
std::string fFolder
Definition: Table.h:380
PGconn * fConnection
Definition: Table.h:401
pg_result PGresult
Definition: Table.h:25
uint64_t fMinChannel
Definition: Table.h:365
bool fHasConnection
Definition: Table.h:351
string cmd
Definition: run_hadd.py:52
bool CloseConnection()
Definition: Table.cpp:830
void SetDBName(std::string dbname)
Definition: Table.h:83
void SetDBHost(std::string dbhost)
Definition: Table.h:85
std::string getenv(std::string const &name)
short fVerbosity
Definition: Table.h:357
std::string fSchema
Definition: Table.h:374
bool GetConnection(int ntry=0)
Definition: Table.cpp:752
void SetDBPort(std::string p)
Definition: Table.h:87
std::string dirName
Definition: PlotSpectra.h:47
void SetTableName(std::string tname)
Definition: Table.cpp:517
std::string fDBCacheFile
Definition: Table.h:376
void SetUser(std::string uname)
Definition: Table.h:79
bool GetColsFromDB(std::vector< std::string > pkeyList={})
Definition: Table.cpp:238
enum BeamMode string
Table::~Table ( )

Definition at line 230 of file Table.cpp.

References Clear(), CloseConnection(), and fHasConnection.

231  {
232  this->Clear();
234  }
bool fHasConnection
Definition: Table.h:351
bool CloseConnection()
Definition: Table.cpp:830

Member Function Documentation

int Table::AddCol ( std::string  cname,
std::string  ctype 
)

Definition at line 326 of file Table.cpp.

References addInsertTime, addInsertUser, addUpdateTime, addUpdateUser, om::cerr, allTimeWatchdog::endl, fCol, MECModelEnuComparisons::i, and Name().

Referenced by ClearRows().

327  {
328  for (size_t i=0; i<fCol.size(); ++i) {
329  if (fCol[i].Name() == cname) {
330  std::cerr << "Table::AddCol: column \'" << cname << "\' already exists! Fatal, aborting..." << std::endl;
331  abort();
332  }
333  }
334 
335  ColumnDef cdef(cname,ctype);
336 
337  fCol.push_back(cdef);
338 
339  if (cname == "inserttime") addInsertTime = true;
340  if (cname == "insertuser") addInsertUser = true;
341  if (cname == "updatetime") addUpdateTime = true;
342  if (cname == "updateuser") addUpdateUser = true;
343 
344  return fCol.size()-1;
345  }
bool addUpdateUser
Definition: Table.h:344
std::string Name()
Definition: Table.h:59
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
bool addInsertTime
Definition: Table.h:341
OStream cerr
Definition: OStream.cxx:7
bool addInsertUser
Definition: Table.h:342
bool addUpdateTime
Definition: Table.h:343
bool Table::AddDistinctColumn ( unsigned int  i)

Definition at line 2458 of file Table.cpp.

References plot_validation_datamc::c, fCol, fDistinctCol, MECModelEnuComparisons::i, and calib::j.

Referenced by AddDistinctColumn(), and WriteToCSV().

2459  {
2460  if (i >= fCol.size()) return false;
2461 
2462  const ColumnDef* c = &fCol[i];
2463  for (unsigned int j=0; j<fDistinctCol.size(); ++j)
2464  if (fDistinctCol[j] == c) return true;
2465 
2466  fDistinctCol.push_back(c);
2467 
2468  return true;
2469  }
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
const double j
Definition: BetheBloch.cxx:29
std::vector< const nutools::dbi::ColumnDef * > fDistinctCol
Definition: Table.h:393
bool Table::AddDistinctColumn ( std::string  col)

Definition at line 2493 of file Table.cpp.

References plot_validation_datamc::c, fDistinctCol, GetCol(), and calib::j.

2494  {
2495  const ColumnDef* c = GetCol(cname);
2496 
2497  if (!c) return false;
2498 
2499  for (unsigned int j=0; j<fDistinctCol.size(); ++j)
2500  if (fDistinctCol[j] == c) return true;
2501 
2502  fDistinctCol.push_back(c);
2503 
2504  return true;
2505  }
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
const double j
Definition: BetheBloch.cxx:29
std::vector< const nutools::dbi::ColumnDef * > fDistinctCol
Definition: Table.h:393
bool nutools::dbi::Table::AddDistinctColumn ( const char *  col)
inline

Definition at line 158 of file Table.h.

References AddDistinctColumn(), RemoveDistinctColumn(), and string.

159  { return AddDistinctColumn(std::string(col)); }
bool AddDistinctColumn(unsigned int i)
Definition: Table.cpp:2458
Int_t col[ntarg]
Definition: Style.C:29
enum BeamMode string
void Table::AddEmptyRows ( unsigned int  nrow)

Definition at line 371 of file Table.cpp.

References fRow, NewRow(), and check_grl::row.

Referenced by ClearRows(), GetDataFromWebService(), LoadFromCSV(), and LoadFromDB().

372  {
373  Row* row = this->NewRow();
374 
375  fRow.resize(fRow.size()+nrow,*row);
376  }
nutools::dbi::Row *const NewRow()
Definition: Table.h:126
std::vector< nutools::dbi::Row > fRow
Definition: Table.h:388
void nutools::dbi::Table::AddExcludeCol ( std::string  col)
inline

Definition at line 180 of file Table.h.

References fExcludeCol.

180 {fExcludeCol.push_back(col); }
std::vector< std::string > fExcludeCol
Definition: Table.h:396
Int_t col[ntarg]
Definition: Style.C:29
bool Table::AddOrderColumn ( unsigned int  i)

Definition at line 2530 of file Table.cpp.

References plot_validation_datamc::c, fCol, fOrderCol, MECModelEnuComparisons::i, and calib::j.

Referenced by AddOrderColumn(), and RemoveDistinctColumn().

2531  {
2532  if (i >= fCol.size()) return false;
2533 
2534  const ColumnDef* c = &fCol[i];
2535  for (unsigned int j=0; j<fOrderCol.size(); ++j)
2536  if (fOrderCol[j] == c) return true;
2537 
2538  fOrderCol.push_back(c);
2539 
2540  return true;
2541  }
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
std::vector< const nutools::dbi::ColumnDef * > fOrderCol
Definition: Table.h:394
const double j
Definition: BetheBloch.cxx:29
bool Table::AddOrderColumn ( std::string  col)

Definition at line 2565 of file Table.cpp.

References plot_validation_datamc::c, fOrderCol, GetCol(), and calib::j.

2566  {
2567  const ColumnDef* c = GetCol(cname);
2568 
2569  if (!c) return false;
2570 
2571  for (unsigned int j=0; j<fOrderCol.size(); ++j)
2572  if (fOrderCol[j] == c) return true;
2573 
2574  fOrderCol.push_back(c);
2575 
2576  return true;
2577  }
std::vector< const nutools::dbi::ColumnDef * > fOrderCol
Definition: Table.h:394
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
const double j
Definition: BetheBloch.cxx:29
bool nutools::dbi::Table::AddOrderColumn ( const char *  col)
inline

Definition at line 167 of file Table.h.

References AddOrderColumn(), RemoveOrderColumn(), and string.

168  { return AddOrderColumn(std::string(col)); }
Int_t col[ntarg]
Definition: Style.C:29
bool AddOrderColumn(unsigned int i)
Definition: Table.cpp:2530
enum BeamMode string
void Table::AddRow ( const Row row)

Definition at line 349 of file Table.cpp.

References nutools::dbi::Row::Col(), fCol, fNullList, fRow, MECModelEnuComparisons::i, nutools::dbi::Column::IsNull(), and Name().

Referenced by AddRow(), and ClearRows().

350  {
351  if (!row) return;
352 
353  Row r2(*row);
354 
355  for (unsigned int i=0; i<fCol.size(); ++i) {
356  if (fCol[i].Name() == "inserttime" ||
357  fCol[i].Name() == "insertuser" ||
358  fCol[i].Name() == "updatetime" ||
359  fCol[i].Name() == "updateuser" ) continue;
360  if (!fCol[i].CanBeNull())
361  if (r2.Col(i).IsNull())
362  fNullList.push_back(std::pair<int,int>(fRow.size(),i));
363  }
364 
365  fRow.push_back(r2);
366 
367  }
std::string Name()
Definition: Table.h:59
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
std::vector< std::pair< int, int > > fNullList
Definition: Table.h:395
std::vector< nutools::dbi::Row > fRow
Definition: Table.h:388
void Table::AddRow ( const Row row)

Definition at line 379 of file Table.cpp.

References AddRow().

380  {
381  AddRow(&row);
382  }
void AddRow(const Row *row)
Definition: Table.cpp:349
void Table::CacheDBCommand ( std::string  cmd)

Definition at line 431 of file Table.cpp.

References demo::app, allTimeWatchdog::endl, fDBCacheFile, and submit_syst::fout.

Referenced by GetConnectionTimeout(), and WriteToDB().

432  {
433  std::ofstream fout;
434  fout.open(fDBCacheFile.c_str(),std::ios_base::app);
435 
436  fout << cmd << std::endl;
437 
438  fout.close();
439  }
string cmd
Definition: run_hadd.py:52
app
Definition: demo.py:189
std::string fDBCacheFile
Definition: Table.h:376
bool Table::CheckForNulls ( )
private

Definition at line 415 of file Table.cpp.

References om::cerr, allTimeWatchdog::endl, fCol, fNullList, fVerbosity, and MECModelEnuComparisons::i.

Referenced by Folder(), WriteToCSV(), and WriteToDB().

416  {
417  bool isOk = fNullList.empty();
418 
419  if (!isOk) // print out list of null columns
420  for (unsigned int i=0; i<fNullList.size(); ++i)
421  if (fVerbosity>0)
422  std::cerr << fCol[fNullList[i].second].Name() << " is NULL in row "
423  << fNullList[i].first << std::endl;
424 
425  return isOk;
426 
427  }
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
OStream cerr
Definition: OStream.cxx:7
short fVerbosity
Definition: Table.h:357
std::vector< std::pair< int, int > > fNullList
Definition: Table.h:395
void nutools::dbi::Table::Clear ( void  )
inline

Definition at line 105 of file Table.h.

References fDistinctCol, fNullList, fOrderCol, fRow, fValidityChanged, fValidityEnd, fValiditySQL, and fValidityStart.

Referenced by ~Table().

105  {
106  fRow.clear(); fValidityStart.clear(); fValidityEnd.clear();
107  fOrderCol.clear(); fDistinctCol.clear(); fNullList.clear();
108  fValiditySQL = "";
109  fValidityChanged = true;
110  }
std::vector< const nutools::dbi::ColumnDef * > fOrderCol
Definition: Table.h:394
std::vector< nutools::dbi::ColumnDef > fValidityStart
Definition: Table.h:390
std::string fValiditySQL
Definition: Table.h:378
bool fValidityChanged
Definition: Table.h:346
std::vector< std::pair< int, int > > fNullList
Definition: Table.h:395
std::vector< nutools::dbi::Row > fRow
Definition: Table.h:388
std::vector< nutools::dbi::ColumnDef > fValidityEnd
Definition: Table.h:391
std::vector< const nutools::dbi::ColumnDef * > fDistinctCol
Definition: Table.h:393
void nutools::dbi::Table::ClearChanRowMap ( )
inline

Definition at line 284 of file Table.h.

References fChanRowMap, and FillChanRowMap().

284 { fChanRowMap.clear(); }
std::unordered_map< uint64_t, std::vector< nutools::dbi::Row * > > fChanRowMap
Definition: Table.h:399
void nutools::dbi::Table::ClearExcludedCols ( )
inline

Definition at line 181 of file Table.h.

References fExcludeCol, GetCurrSeqVal(), and string.

181 { fExcludeCol.clear(); }
std::vector< std::string > fExcludeCol
Definition: Table.h:396
void nutools::dbi::Table::ClearPassword ( )
inline

Definition at line 95 of file Table.h.

References fPassword.

95 { fPassword=""; }
std::string fPassword
Definition: Table.h:377
void nutools::dbi::Table::ClearRecordTime ( )
inline

Definition at line 294 of file Table.h.

References fHasRecordTime.

294 { fHasRecordTime = false;}
bool fHasRecordTime
Definition: Table.h:352
void nutools::dbi::Table::ClearRows ( )
inline

Definition at line 112 of file Table.h.

References AddCol(), AddEmptyRows(), AddRow(), fNullList, fRow, fValidityChanged, GetRow(), MECModelEnuComparisons::i, RemoveRow(), check_grl::row, and string.

112 { fRow.clear(); fNullList.clear(); fValidityChanged=true;}
bool fValidityChanged
Definition: Table.h:346
std::vector< std::pair< int, int > > fNullList
Definition: Table.h:395
std::vector< nutools::dbi::Row > fRow
Definition: Table.h:388
void Table::ClearValidity ( )

Definition at line 477 of file Table.cpp.

References fValidityChanged, fValidityEnd, fValiditySQL, and fValidityStart.

Referenced by Reset(), and WriteToCSV().

478  {
479  fValidityStart.clear();
480  fValidityEnd.clear();
481  fValiditySQL = "";
482  fValidityChanged=true;
483  }
std::vector< nutools::dbi::ColumnDef > fValidityStart
Definition: Table.h:390
std::string fValiditySQL
Definition: Table.h:378
bool fValidityChanged
Definition: Table.h:346
std::vector< nutools::dbi::ColumnDef > fValidityEnd
Definition: Table.h:391
bool Table::CloseConnection ( )

Definition at line 830 of file Table.cpp.

References om::cout, allTimeWatchdog::endl, fConnection, fHasConnection, and fVerbosity.

Referenced by ExecuteSQL(), ExistsInDB(), GetColsFromDB(), GetConnection(), GetCurrSeqVal(), LoadFromDB(), RemoveValidityRange(), Table(), WriteToDB(), and ~Table().

831  {
832  if (fConnection) {
833  PQfinish(fConnection);
834  if (fVerbosity > 0)
835  std::cout << "Closed connection" << std::endl;
836  }
837 
838  fConnection = 0;
839  fHasConnection = false;
840 
841  return true;
842  }
PGconn * fConnection
Definition: Table.h:401
bool fHasConnection
Definition: Table.h:351
short fVerbosity
Definition: Table.h:357
OStream cout
Definition: OStream.cxx:6
int nutools::dbi::Table::DataSource ( )
inline

Definition at line 66 of file Table.h.

References fDataSource.

66 { return fDataSource; }
int nutools::dbi::Table::DataTypeMask ( )
inline
std::string nutools::dbi::Table::DBHost ( )
inline

Definition at line 61 of file Table.h.

References fDBHost.

61 { return fDBHost;}
std::string fDBHost
Definition: Table.h:372
std::string nutools::dbi::Table::DBName ( )
inline

Definition at line 60 of file Table.h.

References fDBName.

60 { return fDBName;}
std::string fDBName
Definition: Table.h:373
std::string nutools::dbi::Table::DBPort ( )
inline

Definition at line 64 of file Table.h.

References fDBPort.

64 { return fDBPort; }
std::string fDBPort
Definition: Table.h:371
void nutools::dbi::Table::DisableCache ( )
inline

Definition at line 299 of file Table.h.

References fDisableCache.

299 { fDisableCache = true; }
bool fDisableCache
Definition: Table.h:354
void nutools::dbi::Table::DisableFlushCache ( )
inline

Definition at line 297 of file Table.h.

References fFlushCache.

297 { fFlushCache = false; }
void nutools::dbi::Table::DisablePasswordAccess ( )
inline

Definition at line 96 of file Table.h.

References fPassword.

96 { fPassword = ""; }
std::string fPassword
Definition: Table.h:377
void nutools::dbi::Table::EnableCache ( )
inline

Definition at line 300 of file Table.h.

References fDisableCache.

300 { fDisableCache = false; }
bool fDisableCache
Definition: Table.h:354
void nutools::dbi::Table::EnableFlushCache ( )
inline

Definition at line 296 of file Table.h.

References fFlushCache.

296 { fFlushCache = true; }
bool Table::ExecuteSQL ( std::string  cmd,
PGresult *&  res 
)

Definition at line 1023 of file Table.cpp.

References om::cerr, CloseConnection(), allTimeWatchdog::endl, fConnection, fHasConnection, fIgnoreDB, fTimeQueries, fVerbosity, GetConnection(), and fixRunHistoryEndTimesViaNearline::tdiff.

Referenced by GetCol().

1024  {
1025  if (fIgnoreDB) return false;
1026 
1027  bool hasConn = fHasConnection;
1028  if (! fHasConnection) {
1029  GetConnection();
1030  hasConn = false;
1031  }
1032 
1033  if (!fConnection) {
1034  std::cerr << "Table::ExecuteSQL: No connection to the database!" << std::endl;
1035  return false;
1036  }
1037 
1038  if (cmd == "") return false;
1039 
1040  if (fVerbosity)
1041  std::cerr << "Executing SQL query: " << cmd << std::endl;
1042 
1043  boost::posix_time::ptime ctt1;
1044  boost::posix_time::ptime ctt2;
1045 
1046  if (fTimeQueries) {
1047  ctt1 = boost::posix_time::microsec_clock::local_time();
1048  }
1049 
1050  res = PQexec(fConnection,cmd.c_str());
1051  if (fTimeQueries) {
1052  ctt2 = boost::posix_time::microsec_clock::local_time();
1053  boost::posix_time::time_duration tdiff = ctt2 - ctt1;
1054  std::cerr << "Table::ExecuteSQL(" << cmd << "): query took "
1055  << tdiff.total_milliseconds() << " ms" << std::endl;
1056  }
1057 
1058  // close connection to the dB if necessary
1059  if (! hasConn) CloseConnection();
1060 
1061  return (res != 0);
1062  }
OStream cerr
Definition: OStream.cxx:7
PGconn * fConnection
Definition: Table.h:401
bool fHasConnection
Definition: Table.h:351
string cmd
Definition: run_hadd.py:52
bool CloseConnection()
Definition: Table.cpp:830
short fVerbosity
Definition: Table.h:357
bool GetConnection(int ntry=0)
Definition: Table.cpp:752
bool Table::ExistsInDB ( )

Definition at line 886 of file Table.cpp.

References om::cerr, CloseConnection(), run_hadd::cmd, allTimeWatchdog::endl, fConnection, fExistsInDB, fHasConnection, fIgnoreDB, fSchema, fTableType, fTestedExists, fVerbosity, GetConnection(), MECModelEnuComparisons::i, nutools::dbi::kConditionsTable, Name(), string, and getGoodRuns4SAM::tname.

Referenced by GetCol(), LoadFromDB(), Table(), and WriteToDB().

887  {
888  if (fIgnoreDB) return false;
889 
890  if (fTestedExists) return fExistsInDB;
891 
892  std::string tname = this->Name();
893 
894  fTestedExists = true;
895 
896  bool hasConn = fHasConnection;
897  if (! fHasConnection) {
898  GetConnection();
899  hasConn = false;
900  }
901 
902  std::string cmd = "SELECT tablename FROM pg_tables WHERE schemaname=\'" +
903  fSchema + "\'";
904  // std::cout << tname << ": " << cmd << std::endl;
905  PGresult* res = PQexec(fConnection,cmd.c_str());
906 
907  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
908  if (fVerbosity > 0)
909  std::cerr << "Table::ExistsInDB command failed: "
910  << PQerrorMessage(fConnection) << std::endl;
911  PQclear(res);
912  fExistsInDB = false; // set this to false just in case
913  CloseConnection();
914  return false;
915  }
916 
917  bool retVal = false;
918  int nRow = PQntuples(res);
919 
920  int tc=0;
921  std::vector<std::string> tList;
922  tList.push_back(tname+std::string("_snapshot"));
923  tList.push_back(tname+std::string("_snapshot_data"));
924  tList.push_back(tname+std::string("_tag"));
925  tList.push_back(tname+std::string("_tag_snapshot"));
926  tList.push_back(tname+std::string("_update"));
927 
928  for (int i=0; i<nRow; ++i) {
929  // std::cout << string(PQgetvalue(res,i,0)) << std::endl;
930  std::string tStr = std::string(PQgetvalue(res,i,0));
931 
932  if (fTableType != kConditionsTable) {
933  if (tStr == tname) {
934  retVal = true;
935  break;
936  }
937  }
938  else {
939  if (std::string(PQgetvalue(res,i,0)) == tList[0] ||
940  std::string(PQgetvalue(res,i,0)) == tList[1] ||
941  std::string(PQgetvalue(res,i,0)) == tList[2] ||
942  std::string(PQgetvalue(res,i,0)) == tList[3] ||
943  std::string(PQgetvalue(res,i,0)) == tList[4] )
944  ++tc;
945 
946  if (tc == 5) {
947  retVal = true;
948  break;
949  }
950  }
951  }
952 
953  PQclear(res);
954 
955  if (!hasConn) CloseConnection();
956 
957  fExistsInDB = true;
958  return retVal;
959 
960  }
std::string Name()
Definition: Table.h:59
OStream cerr
Definition: OStream.cxx:7
PGconn * fConnection
Definition: Table.h:401
pg_result PGresult
Definition: Table.h:25
bool fHasConnection
Definition: Table.h:351
string cmd
Definition: run_hadd.py:52
bool CloseConnection()
Definition: Table.cpp:830
short fVerbosity
Definition: Table.h:357
std::string fSchema
Definition: Table.h:374
bool GetConnection(int ntry=0)
Definition: Table.cpp:752
bool fTestedExists
Definition: Table.h:349
enum BeamMode string
void Table::FillChanRowMap ( )

Definition at line 1982 of file Table.cpp.

References febshutoff_auto::chan, nutools::dbi::Row::Channel(), fChannelVec, fChanRowMap, GetRow(), MECModelEnuComparisons::i, calib::j, NRow(), check_grl::row, and nutools::dbi::Row::VldTime().

Referenced by ClearChanRowMap().

1983  {
1985  uint64_t chan;
1986  float tv;
1987  fChanRowMap.clear();
1988 
1989  for (int i=0; i<this->NRow(); ++i) {
1990  row = this->GetRow(i);
1991  chan = row->Channel();
1992  tv = row->VldTime();
1993  if (fChanRowMap[chan].empty())
1994  fChanRowMap[chan].push_back(row);
1995  else {
1996  bool wasInserted=false;
1997  for (unsigned int j=0; j<fChanRowMap[chan].size(); ++j) {
1998  if (tv < fChanRowMap[chan][j]->VldTime()) {
1999  fChanRowMap[chan].insert(fChanRowMap[chan].begin()+j,row);
2000  wasInserted=true;
2001  break;
2002  }
2003  }
2004  if (!wasInserted)
2005  fChanRowMap[chan].push_back(row);
2006  }
2007  }
2008 
2009  fChannelVec.clear();
2010  std::unordered_map<uint64_t,std::vector<nutools::dbi::Row*> >::iterator itr = fChanRowMap.begin();
2011  std::unordered_map<uint64_t,std::vector<nutools::dbi::Row*> >::iterator itrEnd = fChanRowMap.end();
2012 
2013  for (; itr != itrEnd; ++itr)
2014  fChannelVec.push_back(itr->first);
2015 
2016 
2017  }
float VldTime()
Definition: Row.h:56
uint64_t Channel()
Definition: Row.h:55
std::vector< uint64_t > fChannelVec
Definition: Table.h:398
const double j
Definition: BetheBloch.cxx:29
nutools::dbi::Row *const GetRow(int i)
Definition: Table.cpp:406
std::unordered_map< uint64_t, std::vector< nutools::dbi::Row * > > fChanRowMap
Definition: Table.h:399
std::string nutools::dbi::Table::Folder ( )
inline
const nutools::dbi::ColumnDef* nutools::dbi::Table::GetCol ( int  i)
inline
const nutools::dbi::ColumnDef * Table::GetCol ( std::string cname)

Definition at line 570 of file Table.cpp.

References fCol, MECModelEnuComparisons::i, and Name().

571  {
572  unsigned int i=0;
573  for ( ; i < fCol.size(); ++i)
574  if (fCol[i].Name() == cname) break;
575 
576  if (i >= fCol.size()) return 0;
577 
578  return &fCol[i];
579  }
std::string Name()
Definition: Table.h:59
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
const nutools::dbi::ColumnDef* nutools::dbi::Table::GetCol ( const char *  cname)
inline

Definition at line 135 of file Table.h.

References run_hadd::cmd, ExecuteSQL(), ExistsInDB(), plot_validation_datamc::fname, GetCol(), LoadFromCSV(), SetTolerance(), string, and Tolerance().

136  { std::string cstr(cname); return GetCol(cstr); }
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
enum BeamMode string
int Table::GetColIndex ( std::string  cname)

Definition at line 582 of file Table.cpp.

References om::cerr, allTimeWatchdog::endl, fCol, MECModelEnuComparisons::i, and Name().

Referenced by GetColName().

583  {
584  for (unsigned int i=0; i<fCol.size(); ++i)
585  if (fCol[i].Name() == cname) return (int)i;
586  std::cerr << "No such column \"" << cname << "\". Returning -1" << std::endl;
587  return -1;
588  }
std::string Name()
Definition: Table.h:59
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
OStream cerr
Definition: OStream.cxx:7
std::string nutools::dbi::Table::GetColName ( int  i)
inline

Definition at line 130 of file Table.h.

References fCol, GetColIndex(), MECModelEnuComparisons::i, and string.

130 {return fCol[i].Name(); }
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
std::vector< std::string > Table::GetColNames ( )

Definition at line 601 of file Table.cpp.

References fCol, and MECModelEnuComparisons::i.

Referenced by NewRow().

602  {
603  std::vector<std::string> nameList;
604 
605  for (unsigned int i=0; i<fCol.size(); ++i)
606  nameList.push_back(fCol[i].Name());
607 
608  return nameList;
609  }
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
std::map< std::string, int > Table::GetColNameToIndexMap ( )

Definition at line 591 of file Table.cpp.

References fCol, MECModelEnuComparisons::i, and makeTrainCVSamples::int.

Referenced by NewRow(), and WriteToDB().

592  {
593  std::map<std::string,int> tmap;
594  for (unsigned int i=0; i<fCol.size(); ++i) {
595  tmap[fCol[i].Name()] = int(i);
596  }
597  return tmap;
598  }
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
bool Table::GetColsFromDB ( std::vector< std::string pkeyList = {})

Definition at line 238 of file Table.cpp.

References addInsertTime, addInsertUser, addUpdateTime, addUpdateUser, om::cerr, CloseConnection(), run_hadd::cmd, allTimeWatchdog::endl, fCol, fConnection, fExistsInDB, fHasConnection, fSchema, fTableName, fTableType, fVerbosity, GetConnection(), MECModelEnuComparisons::i, calib::j, nutools::dbi::kConditionsTable, nutools::dbi::kUnstructuredConditionsTable, nutools::dbi::ColumnDef::SetCanBeNull(), and string.

Referenced by SetChannelRange(), and Table().

239  {
241  std::cerr << "Table::GetColsFromDB() currently disabled for unstructured conditions tables." << std::endl;
242  abort();
243  }
244 
245  bool hasConn = fHasConnection;
246  if (! fHasConnection) {
247  GetConnection();
248  hasConn = false;
249  }
250 
251  // now get names and types of all columns
252  std::string cmd = "SELECT column_name, data_type from information_schema.columns where table_name = \'" + std::string(fTableName);
254  cmd += "_update";
255  cmd += "\' and table_schema=\'" + fSchema + "\'";
256 
257  PGresult* res = PQexec(fConnection,cmd.c_str());
258 
259  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
260  std::string errStr = "Table::Table() command failed: " + std::string(PQerrorMessage(fConnection));
261  if (fVerbosity > 0)
262  std::cerr << errStr << std::endl;
263  PQclear(res);
264  fExistsInDB = false; // set this to false just in case
265  CloseConnection();
266  throw std::runtime_error(errStr);
267  }
268 
269  int nRow = PQntuples(res);
270 
271  for (int i=0; i<nRow; ++i) {
272  std::string cname = std::string(PQgetvalue(res,i,0));
273  std::string ctype = std::string(PQgetvalue(res,i,1));
274 
275  if (fTableType == kConditionsTable) {
276  if (cname == "__snapshot_id") continue;
277  if (cname == "__tr") continue;
278  if (cname == "__channel") continue; //cname = "channel";
279  if (cname == "__tv") continue; //cname = "tv";
280  }
281 
282  if (ctype == "smallint") ctype="short";
283  else if (ctype == "double precision") ctype="double";
284  else if (ctype == "boolean") ctype="bool";
285  else if (ctype == "timestamp without time zone") ctype="timestamp";
286  else if (ctype.substr(0,7) == "varchar" || ctype == "text")
287  ctype = "text"; //varchar" + ctype.substr(8,ctype.find(')')-1);
288 
289  // check if this column is "auto_incr", only if !conditions table
290  if (fTableType != kConditionsTable && ctype == "integer") {
292  cmd = "SELECT pg_get_serial_sequence(\'" + stName +
293  "\',\'" + cname + "\')";
294  PGresult* res2 = PQexec(fConnection,cmd.c_str());
295  int nRow2 = PQntuples(res2);
296  for (int j=0; j<nRow2; ++j) {
297  std::string tStr = std::string(PQgetvalue(res2,j,0));
298  if (tStr != "") ctype = "auto_incr";
299  }
300  PQclear(res2);
301  }
302 
303  // now create Column based on this info
304  ColumnDef cdef(cname,ctype);
305 
306  if (find(pkeyList.begin(),pkeyList.end(),cname) != pkeyList.end()) {
307  cdef.SetCanBeNull(false);
308  }
309  fCol.insert(fCol.begin(),cdef);
310 
311  if (cname == "inserttime") addInsertTime = true;
312  if (cname == "insertuser") addInsertUser = true;
313  if (cname == "updatetime") addUpdateTime = true;
314  if (cname == "updateuser") addUpdateUser = true;
315  }
316 
317  PQclear(res);
318 
319  if (!hasConn) CloseConnection();
320 
321  return true;
322  }
bool addUpdateUser
Definition: Table.h:344
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
bool addInsertTime
Definition: Table.h:341
OStream cerr
Definition: OStream.cxx:7
PGconn * fConnection
Definition: Table.h:401
pg_result PGresult
Definition: Table.h:25
bool fHasConnection
Definition: Table.h:351
string cmd
Definition: run_hadd.py:52
bool CloseConnection()
Definition: Table.cpp:830
short fVerbosity
Definition: Table.h:357
const double j
Definition: BetheBloch.cxx:29
std::string fSchema
Definition: Table.h:374
bool GetConnection(int ntry=0)
Definition: Table.cpp:752
bool addInsertUser
Definition: Table.h:342
bool addUpdateTime
Definition: Table.h:343
std::string fTableName
Definition: Table.h:368
enum BeamMode string
bool Table::GetConnection ( int  ntry = 0)

Definition at line 752 of file Table.cpp.

References om::cerr, CloseConnection(), run_hadd::cmd, om::cout, e, allTimeWatchdog::endl, fConnection, fConnectionTimeout, fDBHost, fDBName, fDBPort, fHasConnection, fIgnoreDB, fPassword, fUser, fVerbosity, GetConnectionInfo(), cet::getenv(), nutools::dbi::Util::RunningOnGrid(), SetPasswordFile(), string, getGoodRuns4SAM::t0, and getGoodRuns4SAM::t1.

Referenced by ExecuteSQL(), ExistsInDB(), GetColsFromDB(), GetCurrSeqVal(), LoadFromDB(), RemoveValidityRange(), Table(), and WriteToDB().

753  {
754  if (fIgnoreDB) return false;
755 
756  bool gotConnInfo = false;
757  try {
758  gotConnInfo = GetConnectionInfo(ntry);
759  }
760  catch (std::runtime_error& e) {
761  std::cerr << e.what() << std::endl;
762  return false;
763  }
764 
765  if (!gotConnInfo) return false;
766 
767  // now get the password file name for read-only access
768 
769  if (Util::RunningOnGrid()) {
770  char* tmpStr = getenv("DBIGRIDPWDFILE");
771  if(tmpStr){
772  if(!this->SetPasswordFile(tmpStr)){
773  return false;
774  }
775  }
776  }
777  else {
778  char* tmpStr = getenv("DBIPWDFILE");
779  if(tmpStr){
780  if(!this->SetPasswordFile(tmpStr)) {
781  return false;
782  }
783  }
784  }
785 
786  if (!fConnection) {
787  std::string cmd = "dbname = " + fDBName + " host = " + fDBHost + " user = " + fUser;
788  if (fDBPort != "")
789  cmd += " port = " + fDBPort;
790 
791  if (fPassword != "")
792  cmd += " password = " + fPassword;
793 
794  fConnection = PQconnectdb(cmd.c_str());
795 
796  int nTry=0;
797  int sleepTime = 2;
798  time_t t0 = time(NULL);
799  time_t t1 = t0;
800 
801  while (PQstatus(fConnection) != CONNECTION_OK &&
802  ((t1-t0) < fConnectionTimeout) ) {
803  std::cerr << "Connection to " << fDBHost << ":"
804  << fDBName << " failed: "
805  << PQerrorMessage(fConnection) << std::endl;
806 
807  CloseConnection();
808  sleepTime = 1 + ((double)random()/(double)RAND_MAX)*(1 << nTry++);
809  sleep(sleepTime);
810  t1 = time(NULL);
811  fConnection = PQconnectdb(cmd.c_str());
812  }
813  if (PQstatus(fConnection) != CONNECTION_OK) {
814  CloseConnection();
815  if (! GetConnection(ntry+1)) {
816  std::cerr << "Too many attempts to connect to the database, "
817  << ", giving up." << std::endl;
818  CloseConnection();
819  return false;
820  }
821  }
822  fHasConnection = true;
823  if (fVerbosity > 0)
824  std::cout << "Got new connection" << std::endl;
825  }
826 
827  return true;
828  }
std::string fUser
Definition: Table.h:369
static bool RunningOnGrid()
Definition: Util.cpp:132
OStream cerr
Definition: OStream.cxx:7
int fConnectionTimeout
Definition: Table.h:361
::xsd::cxx::tree::time< char, simple_type > time
Definition: Database.h:194
PGconn * fConnection
Definition: Table.h:401
bool fHasConnection
Definition: Table.h:351
string cmd
Definition: run_hadd.py:52
bool CloseConnection()
Definition: Table.cpp:830
std::string fPassword
Definition: Table.h:377
bool GetConnectionInfo(int ntry=0)
Definition: Table.cpp:692
std::string getenv(std::string const &name)
short fVerbosity
Definition: Table.h:357
std::string fDBHost
Definition: Table.h:372
bool GetConnection(int ntry=0)
Definition: Table.cpp:752
OStream cout
Definition: OStream.cxx:6
bool SetPasswordFile(const char *fname=0)
fname should be the name of the file
Definition: Table.cpp:852
Float_t e
Definition: plot.C:35
std::string fDBPort
Definition: Table.h:371
std::string fDBName
Definition: Table.h:373
enum BeamMode string
bool Table::GetConnectionInfo ( int  ntry = 0)
private

Definition at line 692 of file Table.cpp.

References om::cerr, allTimeWatchdog::endl, fDBHost, fDBName, fDBPort, fIgnoreEnvVar, fUser, and cet::getenv().

Referenced by Folder(), GetConnection(), and WriteToDB().

693  {
694  char* tmpStr;
695  char hname[256];
696 
697  if (!fIgnoreEnvVar) {
698  // now check environment variables, which will override any previous settings
699  if (ntry == 0) {
700  tmpStr = getenv("DBIHOST");
701  if (tmpStr)
702  fDBHost = tmpStr;
703  }
704  else {
705  sprintf(hname,"DBIHOST%d",ntry);
706  tmpStr = getenv(hname);
707  if (tmpStr) {
708  std::cerr << "Switching to " << tmpStr << std::endl;
709  fDBHost = tmpStr;
710  }
711  else
712  return false;
713  }
714 
715  tmpStr = getenv("DBINAME");
716  if (tmpStr)
717  fDBName = tmpStr;
718  tmpStr = getenv("DBIPORT");
719  if (tmpStr)
720  fDBPort = tmpStr;
721  tmpStr = getenv("DBIUSER");
722  if (tmpStr)
723  fUser = tmpStr;
724  }
725 
726  if (fUser == "") {
727  tmpStr = getenv("USER");
728  if (tmpStr) {
729  fUser = tmpStr;
730  std::cerr << "Table::GetConnectionInfo: DB User undefined. Setting to \""
731  << fUser << "\"" << std::endl;
732  }
733  else {
734  throw std::runtime_error("Table::GetConnectionInfo: DB USER undefined.");
735  }
736 
737  }
738  if (fDBHost == "") {
739  throw std::runtime_error("Table::GetConnectionInfo: DB HOST undefined.");
740  }
741  if (fDBName == "") {
742  throw std::runtime_error("Table::GetConnectionInfo: DB NAME undefined.");
743  }
744 
745  /* if (fDBPort == "")
746  if (fTable->hasDbPort())
747  fDBPort = fTable->getDbPort();
748  */
749  return true;
750  }
std::string fUser
Definition: Table.h:369
OStream cerr
Definition: OStream.cxx:7
bool fIgnoreEnvVar
Definition: Table.h:345
std::string getenv(std::string const &name)
std::string fDBHost
Definition: Table.h:372
std::string fDBPort
Definition: Table.h:371
std::string fDBName
Definition: Table.h:373
int nutools::dbi::Table::GetConnectionTimeout ( )
inline
bool Table::GetCurrSeqVal ( std::string  col,
long &  iseq 
)

Definition at line 963 of file Table.cpp.

References om::cerr, CloseConnection(), run_hadd::cmd, allTimeWatchdog::endl, fConnection, fHasConnection, fIgnoreDB, fVerbosity, GetConnection(), Name(), Schema(), and string.

Referenced by ClearExcludedCols(), GetCurrSeqVal(), and WriteToDB().

964  {
965  if (fIgnoreDB) return false;
966 
967  bool hasConn = fHasConnection;
968  if (! fHasConnection) {
969  GetConnection();
970  hasConn = false;
971  }
972 
973  // now get current sequence value:
974 
975  std::string cmd = "SELECT last_value FROM ";
976  cmd += Schema() + "." + Name();
977  cmd += "_" + col + "_seq";
978 
979  if (fVerbosity > 0)
980  std::cerr << "Table::GetCurrSeqVal: Executing PGSQL command: \n\t"
981  << cmd << std::endl;
982 
983  PGresult* res = PQexec(fConnection, cmd.c_str());
984  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
985  if (fVerbosity > 0)
986  std::cerr << "SELECT failed: " << PQerrorMessage(fConnection) << std::endl;
987  PQclear(res);
988  return false;
989  }
990 
991  // check that the number of columns is consistent
992  if (PQnfields(res) != 1) {
993  PQclear(res);
994  return false;
995  }
996 
997  // now cache rows
998  int nRow = PQntuples(res);
999 
1000  if (nRow != 1) {
1001  PQclear(res);
1002  return false;
1003  }
1004 
1005  if (! PQgetisnull(res,0,0)) {
1006  std::string vstr = PQgetvalue(res,0,0);
1007  try {
1008  iseq = boost::lexical_cast<long>(vstr);
1009  }
1010  catch (boost::bad_lexical_cast &) {
1011  PQclear(res);
1012  return false;
1013  }
1014  }
1015 
1016  PQclear(res);
1017 
1018  if (!hasConn) CloseConnection();
1019 
1020  return true;
1021  }
std::string Name()
Definition: Table.h:59
std::string Schema()
Definition: Table.h:215
OStream cerr
Definition: OStream.cxx:7
PGconn * fConnection
Definition: Table.h:401
pg_result PGresult
Definition: Table.h:25
bool fHasConnection
Definition: Table.h:351
string cmd
Definition: run_hadd.py:52
bool CloseConnection()
Definition: Table.cpp:830
Int_t col[ntarg]
Definition: Style.C:29
short fVerbosity
Definition: Table.h:357
bool GetConnection(int ntry=0)
Definition: Table.cpp:752
enum BeamMode string
bool nutools::dbi::Table::GetCurrSeqVal ( const char *  col,
long &  iseq 
)
inline

Definition at line 184 of file Table.h.

References GetCurrSeqVal(), and string.

185  { return GetCurrSeqVal(std::string(col), iseq); }
bool GetCurrSeqVal(std::string col, long &iseq)
Definition: Table.cpp:963
Int_t col[ntarg]
Definition: Style.C:29
enum BeamMode string
bool Table::GetDataFromWebService ( Dataset ds,
std::string  myss 
)
private

Definition at line 1565 of file Table.cpp.

References AddEmptyRows(), om::cerr, febshutoff_auto::chan, om::cout, allTimeWatchdog::endl, fCol, fConnectionTimeout, fRow, fTimeParsing, fTimeQueries, fVerbosity, MECModelEnuComparisons::i, fillBadChanDBTables::irow, Name(), ss, string, getGoodRuns4SAM::t0, getGoodRuns4SAM::t1, and fixRunHistoryEndTimesViaNearline::tdiff.

Referenced by Folder(), LoadConditionsTable(), and LoadNonConditionsTable().

1566  {
1567  Tuple tu;
1568  char ss[1024];
1569  char ss2[1024];
1570  int wda_err, err;
1571  std::vector<int> colMap(fCol.size());
1572  std::vector<bool> isString(fCol.size());
1573  std::vector<bool> isKnownField(fCol.size());
1574 
1575  const char* uagent = NULL;
1576 
1577  if(fVerbosity > 0)
1578  std::cout << "DBWeb query: " << myss << std::endl;
1579 
1580  boost::posix_time::ptime ctt1;
1581  boost::posix_time::ptime ctt2;
1582 
1583  if (fTimeQueries) {
1584  ctt1 = boost::posix_time::microsec_clock::local_time();
1585  }
1586 
1587  ds = getDataWithTimeout(myss.c_str(), uagent,
1588  fConnectionTimeout, &wda_err);
1589 
1590  if (fTimeQueries) {
1591  ctt2 = boost::posix_time::microsec_clock::local_time();
1592  boost::posix_time::time_duration tdiff = ctt2 - ctt1;
1593  std::cerr << "Table::Load(" << Name() << "): query took "
1594  << tdiff.total_milliseconds() << " ms" << std::endl;
1595  }
1596 
1597  int httpStatus = getHTTPstatus(ds);
1598 
1599  if (httpStatus == 504) {
1600  int nTry=0;
1601  int sleepTime = 2;
1602  time_t t0 = time(NULL);
1603  time_t t1 = t0;
1604 
1605  while (httpStatus == 504 && ((t1-t0) < fConnectionTimeout) ) {
1606  sleepTime = 1 + ((double)random()/(double)RAND_MAX)*(1 << nTry++);
1607 
1608  std::cerr << "Table::Load() for " << Name()
1609  << " failed with error 504, retrying in " << sleepTime
1610  << " seconds." << std::endl;
1611 
1612  sleep(sleepTime);
1613  t1 = time(NULL);
1614  if (fTimeQueries)
1615  ctt1 = boost::posix_time::microsec_clock::local_time();
1616 
1617  ds = getDataWithTimeout(myss.c_str(), uagent,
1618  fConnectionTimeout, &wda_err);
1619 
1620  if (fTimeQueries) {
1621  ctt2 = boost::posix_time::microsec_clock::local_time();
1622  boost::posix_time::time_duration tdiff = ctt2 - ctt1;
1623  std::cerr << "Table::Load(" << Name() << "): query took "
1624  << tdiff.total_milliseconds() << " ms" << std::endl;
1625  }
1626  httpStatus = getHTTPstatus(ds);
1627  }
1628  }
1629 
1630  if (httpStatus != 200) {
1631  std::cerr << "Table::Load: Web Service returned HTTP status "
1632  << httpStatus << ": " << getHTTPmessage(ds) << std::endl;
1633  return false;
1634  }
1635 
1636  if (fTimeParsing)
1637  ctt1 = boost::posix_time::microsec_clock::local_time();
1638 
1639  int ntup = getNtuples(ds);
1640 
1641  // Getting no rows back can be legitimate
1642  if(ntup == 0){
1643  if(fVerbosity > 0)
1644  std::cout << "Got zero rows from database. Is that expected?" << std::endl;
1645 
1646  fRow.clear();
1647 
1648  return true;
1649  }
1650 
1651  if(fVerbosity > 0)
1652  std::cout << "Got " << ntup-1 << " rows from database" << std::endl;
1653 
1654  int ioff=fRow.size();
1655 
1656  AddEmptyRows(ntup);
1657 
1658  tu = getFirstTuple(ds);
1659  if (tu == NULL) {
1660  std::cerr << "Table::Load(" << Name() << ") has NULL first tuple!"
1661  << std::endl;
1662  return false;
1663  }
1664  int ncol2 = getNfields(tu);
1665  std::string chanStr = "channel";
1666  std::string tvStr = "tv";
1667  std::string tvEndStr = "tvend";
1668  int chanIdx=-1;
1669  int tvIdx=-1;
1670  int tvEndIdx=-1;
1671  for (int i=0; i<ncol2; ++i) {
1672  getStringValue(tu,i,ss,sizeof(ss),&err);
1673  if (chanStr == ss) { chanIdx=i; continue;}
1674  if (tvStr == ss) { tvIdx=i; continue;}
1675  if (tvEndStr == ss) { tvEndIdx=i; continue;}
1676 
1677  bool foundMatch=false;
1678  for (unsigned int icol=0; icol<fCol.size(); ++icol) {
1679  if (fCol[icol].Name() == ss) {
1680  colMap[i] = icol;
1681  isString[i] = false;
1682  if (fCol[icol].Type() == "string" || fCol[icol].Type() == "text")
1683  isString[i] = true;
1684  foundMatch=true;
1685  break;
1686  }
1687  }
1688  if (!foundMatch) // this means this field was unexpected, so
1689  // ignore it downstream
1690  isKnownField[i] = false;
1691  else
1692  isKnownField[i] = true;
1693  }
1694 
1695  releaseTuple(tu);
1696  tu = getNextTuple(ds);
1697  int irow=0;
1698  while (tu != NULL) {
1699  for (int i=0; i<ncol2; ++i) {
1700  getStringValue(tu,i,ss,sizeof(ss),&err);
1701  if (i == chanIdx) {
1702  uint64_t chan = strtoull(ss,NULL,10);
1703  fRow[ioff+irow].SetChannel(chan);
1704  continue;
1705  }
1706  else if (i == tvIdx) {
1707  float t1 = strtof(ss,NULL);
1708  fRow[ioff+irow].SetVldTime(t1);
1709  }
1710  else if (i == tvEndIdx) {
1711  float t1 = strtof(ss,NULL);
1712  fRow[ioff+irow].SetVldTimeEnd(t1);
1713  }
1714  else {
1715  if (isKnownField[i]) {
1716  if (isString[i] && (ss[0]=='\'' || ss[0]=='\"')) { // remove quotes
1717  int k = strlen(ss);
1718  strncpy(ss2,&ss[1],k-2);
1719  ss2[k-2] = '\0';
1720  fRow[ioff+irow].Col(colMap[i]).FastSet(ss2);
1721  }
1722  else
1723  fRow[ioff+irow].Col(colMap[i]).FastSet(ss);
1724  }
1725  }
1726  }
1727  releaseTuple(tu);
1728  tu = getNextTuple(ds);
1729  ++irow;
1730  };
1731 
1732  if (fTimeParsing) {
1733  ctt2 = boost::posix_time::microsec_clock::local_time();
1734  boost::posix_time::time_duration tdiff = ctt2 - ctt1;
1735  std::cerr << "Table::Load(" << Name() << "): parsing took "
1736  << tdiff.total_milliseconds() << " ms" << std::endl;
1737  }
1738 
1739  // Make sure that the rows list is no longer than what we actually
1740  // filled. This happens because ntup above included the header row that
1741  // gives the column names.
1742  while(int(fRow.size()) > irow) fRow.pop_back();
1743 
1744  releaseDataset(ds);
1745 
1746  return true;
1747  }
std::string Name()
Definition: Table.h:59
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
Float_t ss
Definition: plot.C:24
OStream cerr
Definition: OStream.cxx:7
int fConnectionTimeout
Definition: Table.h:361
::xsd::cxx::tree::time< char, simple_type > time
Definition: Database.h:194
void AddEmptyRows(unsigned int nrow)
Definition: Table.cpp:371
short fVerbosity
Definition: Table.h:357
OStream cout
Definition: OStream.cxx:6
std::vector< nutools::dbi::Row > fRow
Definition: Table.h:388
enum BeamMode string
bool Table::GetDetector ( std::string det) const

Definition at line 508 of file Table.cpp.

References fDetector.

Referenced by SetValiditySQL().

509  {
510  if (fDetector == "") return false;
511  det = fDetector;
512 
513  return true;
514  }
std::string fDetector
Definition: Table.h:379
float nutools::dbi::Table::GetMaxTSVld ( ) const
inline

Definition at line 274 of file Table.h.

References fMaxTSVld.

274 {return fMaxTSVld; }
float nutools::dbi::Table::GetMinTSVld ( ) const
inline

Definition at line 275 of file Table.h.

References fMinTSVld.

275 {return fMinTSVld; }
int nutools::dbi::Table::GetNPKeyCol ( )
inline

Definition at line 189 of file Table.h.

References fPKeyList.

189 { return fPKeyList.size(); }
std::vector< const nutools::dbi::ColumnDef * > fPKeyList
Definition: Table.h:392
std::string Table::GetPassword ( )
private

Definition at line 2599 of file Table.cpp.

References om::cerr, allTimeWatchdog::endl, fin, cet::getenv(), g4zmq::pwd(), and string.

Referenced by Folder(), Tag(), and Write().

2600  {
2601  std::string pwd = "";
2602 
2603  char* pwdFile = getenv("DBIWSPWDFILE");
2604  if (pwdFile) {
2605  std::ifstream fin;
2606  fin.open(pwdFile);
2607  if (!fin.is_open() || !fin.good()) {
2608  std::cerr << "Could not open password file " << pwdFile
2609  << ". Canceling Table::Write()" << std::endl;
2610  return pwd;
2611  }
2612  else {
2613  fin >> pwd;
2614  fin.close();
2615  }
2616  }
2617 
2618  return pwd;
2619  }
TString fin
Definition: Style.C:24
OStream cerr
Definition: OStream.cxx:7
def pwd()
Definition: g4zmq.py:47
std::string getenv(std::string const &name)
enum BeamMode string
const nutools::dbi::ColumnDef* nutools::dbi::Table::GetPKeyCol ( int  i)
inline

Definition at line 190 of file Table.h.

References fPKeyList, MECModelEnuComparisons::i, and PrintPQErrorMsg().

190 { return fPKeyList[i]; }
std::vector< const nutools::dbi::ColumnDef * > fPKeyList
Definition: Table.h:392
Row *const Table::GetRow ( int  i)

Definition at line 406 of file Table.cpp.

References fRow.

Referenced by ClearRows(), FillChanRowMap(), and MakeConditionsCSVString().

407  {
408  if (i >= 0 && i < (int)fRow.size())
409  return &fRow[i];
410  else
411  return 0;
412  }
std::vector< nutools::dbi::Row > fRow
Definition: Table.h:388
std::string nutools::dbi::Table::GetTag ( )
inline

Definition at line 278 of file Table.h.

References fTag, Load(), string, Tag(), and Write().

278 { return fTag; }
std::string fTag
Definition: Table.h:382
std::string nutools::dbi::Table::GetValiditySQL ( )
inline

Definition at line 208 of file Table.h.

References fValiditySQL.

208 { return fValiditySQL; }
std::string fValiditySQL
Definition: Table.h:378
nutools::dbi::Row * Table::GetVldRow ( uint64_t  channel,
float  t 
)

Definition at line 2028 of file Table.cpp.

References fChanRowMap, MECModelEnuComparisons::i, and fillBadChanDBTables::irow.

Referenced by VldChannels().

2029  {
2030  std::vector<nutools::dbi::Row*>& rlist = fChanRowMap[channel];
2031  if (rlist.empty()) return 0;
2032  int irow=-1;
2033  float tv;
2034  // fChanRowMap is time-ordered, so this simplifies things
2035  unsigned int i=0;
2036  for ( ; i<rlist.size(); ++i) {
2037  tv = rlist[i]->VldTime();
2038  if (t >= tv) irow=i;
2039  else break;
2040  }
2041  if (irow>=0) return rlist[irow];
2042  return 0;
2043  }
std::unordered_map< uint64_t, std::vector< nutools::dbi::Row * > > fChanRowMap
Definition: Table.h:399
std::vector< nutools::dbi::Row * > Table::GetVldRows ( uint64_t  channel)

Definition at line 2021 of file Table.cpp.

References fChanRowMap.

Referenced by VldChannels().

2022  {
2023  return fChanRowMap[channel];
2024  }
std::unordered_map< uint64_t, std::vector< nutools::dbi::Row * > > fChanRowMap
Definition: Table.h:399
bool Table::Load ( void  )
bool Table::LoadConditionsTable ( )
private

Definition at line 1873 of file Table.cpp.

References om::cerr, allTimeWatchdog::endl, fDataTypeMask, fDisableCache, fExcludeCol, fFlushCache, fHasRecordTime, fMaxChannel, fMaxTSVld, fMinChannel, fMinTSVld, fRecordTime, fTag, fValiditySQL, fWSURL, GetCol(), GetDataFromWebService(), cet::getenv(), MECModelEnuComparisons::i, calib::j, nutools::dbi::kDataOnly, nutools::dbi::kMCOnly, nutools::dbi::kNone, nutools::dbi::ColumnDef::Name(), Name(), NCol(), nutools::dbi::Util::RunningOnGrid(), Schema(), and string.

Referenced by Folder(), and Load().

1874  {
1875  if (fDataTypeMask == 0) {
1876  std::cerr << "Table::LoadConditionsTable: Data type mask is not set!" << std::endl;
1877  return false;
1878  }
1879 
1880  if (fMinTSVld == 0 || fMaxTSVld == 0) {
1881  std::cerr << "Table::LoadConditionsTable: No validity time is set!" << std::endl;
1882  return false;
1883  }
1884 
1885  if (fWSURL == "") {
1886  std::cerr << "Table::LoadConditionsTable: Web Service URL is not set!" << std::endl;
1887  return false;
1888  }
1889 
1890  if (!Util::RunningOnGrid()) {
1891  std::string interactiveURL = getenv("DBIWSURLINT");
1892  if (!interactiveURL.empty())
1893  fWSURL = interactiveURL;
1894  }
1895 
1896  int ncol = this->NCol();
1897 
1898  std::stringstream myss;
1899 
1900  myss << fWSURL << "get?table=" << Schema() << "." << Name() << "&";
1901 
1902  if (fDataTypeMask > kNone) {
1903  myss << "type=";
1904 
1905  if ((fDataTypeMask & kMCOnly)) myss << "mc";
1906  if ((fDataTypeMask & kDataOnly)) myss << "data";
1907 
1908  myss << "&";
1909  }
1910 
1911  if (fMaxChannel > fMinChannel) {
1912  myss << "cr=" << fMinChannel << "-" << fMaxChannel << "&";
1913  }
1914 
1915  if (fValiditySQL != "") {
1916  myss << "where=" << fValiditySQL << "&";
1917  }
1918 
1919  if (fTag != "") myss << "tag=" << fTag << "&";
1920 
1921  // char ts[256];
1922 
1923  if (fMinTSVld == fMaxTSVld) {
1924  // sprintf(ts,"t=%" PRIu64,fMinTSVld);
1925  // myss << ts; //"t=" << fMinTSVld;
1926  myss << "t=" << std::setprecision(12) << fMinTSVld;
1927  }
1928  else {
1929  // sprintf(ts,"t0=%" PRIu64 "&t1=" PRIu64,fMinTSVld,fMaxTSVld);
1930  // myss << ts; //"t0=" << fMinTSVld << "&t1=" << fMaxTSVld;
1931  myss << "t0=" << std::setprecision(12) << fMinTSVld << "&t1=" << std::setprecision(12) << fMaxTSVld;
1932  }
1933 
1934  if (fHasRecordTime) myss << "&rtime=" << fRecordTime;
1935 
1936  if (fFlushCache) myss << "&cache=flush";
1937  if (fDisableCache) myss << "&cache=no";
1938 
1939  myss << "&columns=";
1940  bool firstCol = true;
1941  for (int i=0; i<ncol; ++i) {
1942  std::string cName = this->GetCol(i)->Name();
1943  bool skipCol = false;
1944  for (size_t j=0; j<fExcludeCol.size(); ++j)
1945  if (fExcludeCol[j] == cName) {
1946  skipCol = true;
1947  break;
1948  }
1949  if (skipCol) continue;
1950  if(!firstCol) myss << ",";
1951  myss << this->GetCol(i)->Name();
1952  firstCol = false;
1953  }
1954 
1955  // std::cout << myss.str() << std::endl;
1956  Dataset ds;
1957 
1958  return GetDataFromWebService(ds,myss.str());
1959 
1960  }
std::string Name()
Definition: Table.h:59
uint64_t fMaxChannel
Definition: Table.h:366
bool fDisableCache
Definition: Table.h:354
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
static bool RunningOnGrid()
Definition: Util.cpp:132
std::vector< std::string > fExcludeCol
Definition: Table.h:396
std::string Schema()
Definition: Table.h:215
OStream cerr
Definition: OStream.cxx:7
std::string fValiditySQL
Definition: Table.h:378
uint64_t fMinChannel
Definition: Table.h:365
bool fHasRecordTime
Definition: Table.h:352
std::string getenv(std::string const &name)
float fRecordTime
Definition: Table.h:407
bool GetDataFromWebService(Dataset &, std::string)
Definition: Table.cpp:1565
const double j
Definition: BetheBloch.cxx:29
std::string Name() const
Definition: ColumnDef.h:21
A very simple service to remember what detector we&#39;re working in.
std::string fWSURL
Definition: Table.h:383
std::string fTag
Definition: Table.h:382
enum BeamMode string
bool Table::LoadFromCSV ( std::string  fname)

Definition at line 1291 of file Table.cpp.

References AddEmptyRows(), om::cerr, om::cout, allTimeWatchdog::endl, fCol, fin, fRow, fTableType, MECModelEnuComparisons::i, makeTrainCVSamples::int, fillBadChanDBTables::irow, calib::j, nutools::dbi::kConditionsTable, Name(), NewRow(), r(), string, and trim().

Referenced by GetCol(), and LoadFromCSV().

1292  {
1293  std::cout << "Reading " << fname << std::endl;
1294 
1295  std::ifstream fin;
1296  fin.open(fname.c_str());
1297  if (!fin.is_open()) {
1298  std::cerr << "Could not open " << fname << std::endl;
1299  return false;
1300  }
1301  if (!fin.good()) {
1302  std::cerr << "Stream not good " << fname << std::endl;
1303  return false;
1304  }
1305 
1306  std::string s;
1307 
1308  char buff[256];
1310 
1311  std::vector<int> colMap(fCol.size());
1312  for (unsigned int i=0; i<fCol.size(); ++i) {
1313  colMap[i] = int(i);
1314  }
1315 
1316  bool hasColNames = true;
1317  bool hasTols = true;
1318 
1319  int chanIdx=-1;
1320  int tvIdx=-1;
1321  int tvEndIdx=-1;
1322 
1323  // check first line to see if it is column names. Should begin with a '#'
1324  std::getline(fin,s);
1325  if (s[0] == '#' || fTableType == kConditionsTable) {
1326  unsigned int ic=1;
1327  if (fTableType == kConditionsTable && s[0] != '#') ic=0;
1328  int k;
1329 
1330  int joff=0;
1331  for (int j=0; ic<s.length(); ++j) {
1332  k=0;
1333 
1334  while (s[ic++] != ',' && ic<s.length())
1335  buff[k++] = s[ic-1];
1336 
1337  if (ic==s.length()) buff[k++] = s[s.length()-1];
1338  buff[k] = '\0';
1339  value = buff;
1340 
1341  boost::algorithm::trim(value);
1342  if (value == "channel") { chanIdx=j; ++joff;}
1343  else if (value == "tv") { tvIdx=j; ++joff;}
1344  else if (value == "tvend") { tvEndIdx=j; ++joff;}
1345  else {
1346  for (unsigned int jc=0; jc<fCol.size(); ++jc)
1347  if (fCol[jc].Name() == value) {
1348  colMap[j-joff] = jc;
1349  break;
1350  }
1351  }
1352  }
1353 
1354  }
1355  else {
1356  hasColNames = false;
1357  fin.clear();
1358  fin.seekg(0);
1359  }
1360 
1361  // now check for tolerances
1362  std::getline(fin,s);
1363  if (fTableType == kConditionsTable && s.substr(0,10) == "tolerance,") {
1364  unsigned int ic=11;
1365  int k;
1366 
1367  int joff=0;
1368  for (int j=0; ic<s.length(); ++j) {
1369  k=0;
1370  while (s[ic] != ',' && ic<s.length())
1371  buff[k++] = s[ic++];
1372  // if (ic==s.length()) buff[k++] = s[s.length()-1];
1373  ++ic;
1374  if (ic < s.length())
1375  buff[k] = '\0';
1376  else {
1377  buff[k] = '\0';
1378  }
1379  value = buff;
1380  if (value.length() > 0) {
1381  if (j==chanIdx || j==tvIdx || j==tvEndIdx)
1382  ++joff;
1383  else
1384  fCol[colMap[j-joff]].SetTolerance(atof(buff));
1385  }
1386  }
1387  }
1388  else {
1389  hasTols = false;
1390  fin.clear();
1391  fin.seekg(0);
1392  }
1393 
1394  Row* r = NewRow();
1395 
1396  int nRow=0;
1397  unsigned int ioff=fRow.size();
1398  unsigned int irow=0;
1399  fin.clear();
1400  fin.seekg(0);
1401  while (std::getline(fin,s))
1402  ++nRow;
1403  fin.clear();
1404  fin.seekg(0);
1405  if (hasColNames) {
1406  --nRow;
1407  std::getline(fin,s);
1408  }
1409  if (hasTols) {
1410  --nRow;
1411  std::getline(fin,s);
1412  }
1413 
1414  if (nRow<=0) {
1415  std::cout << "Table::LoadFromCSV() found no rows in "
1416  << fname << std::endl;
1417  return false;
1418  }
1419 
1420  AddEmptyRows(nRow);
1421  std::cout << "Added " << nRow << " empty rows" << std::endl;
1422 
1423  for (int jrow=0; jrow<nRow; ++jrow) {
1424  std::getline(fin,s);
1425 
1426  unsigned int ic=0;
1427  int k;
1428  bool hasX;
1429  int joff=0;
1430  for (int j=0; ic<s.length(); ++j) {
1431  k=0;
1432  hasX=false;
1433  while (s[ic++] != ',' && ic<s.length()) {
1434  buff[k++] = s[ic-1];
1435  if (buff[k-1] == 'x') hasX=true;
1436  }
1437  if (ic==s.length()) buff[k++] = s[s.length()-1];
1438  buff[k] = '\0';
1439  value = buff;
1440 
1441  if (j==chanIdx) {
1442  fRow[ioff+irow].SetChannel(strtoull(buff,NULL,10));
1443  ++joff;
1444  }
1445  else if (j==tvIdx) {
1446  fRow[ioff+irow].SetVldTime(strtoull(buff,NULL,10));
1447  ++joff;
1448  }
1449  else if (j==tvEndIdx) {
1450  fRow[ioff+irow].SetVldTimeEnd(strtoull(buff,NULL,10));
1451  ++joff;
1452  }
1453  else {
1454  if (hasX) {
1455  if (fCol[j-joff].Type() == "bigint" ||
1456  fCol[j-joff].Type() == "long") {
1457  try {
1458  std::istringstream iss(value);
1459  uint64_t ulongValue;
1460  iss >> std::hex >> ulongValue;
1461  int64_t longValue = (int64_t) ulongValue;
1462  value = boost::lexical_cast<std::string>(longValue);
1463  }
1464  catch (...) {
1465  // simply let "value" remain unchanged
1466  }
1467  }
1468  else if (fCol[j-joff].Type() == "int") {
1469  try {
1470  std::istringstream iss(value);
1471  uint32_t uintValue;
1472  iss >> std::hex >> uintValue;
1473  int32_t intValue = (int32_t) uintValue;
1474  value = boost::lexical_cast<std::string>(intValue);
1475  }
1476  catch (...) {
1477  // simply let "value" remain unchanged
1478  }
1479  }
1480  else if (fCol[j-joff].Type() == "short") {
1481  try {
1482  std::istringstream iss(value);
1483  uint16_t ushortValue;
1484  iss >> std::hex >> ushortValue;
1485  int16_t shortValue = (int16_t) ushortValue;
1486  value = boost::lexical_cast<std::string>(shortValue);
1487  }
1488  catch (...) {
1489  // simply let "value" remain unchanged
1490  }
1491  }
1492  } // if (hasX)
1493  if (fCol[j-joff].Type() == "text") {
1494  boost::algorithm::trim(value);
1495  if ((value[0] == '"' && value[value.length()-1] == '"') ||
1496  (value[0] == '\'' && value[value.length()-1] == '\''))
1497  value = value.substr(1,value.length()-2);
1498  }
1499  fRow[ioff+irow].Col(colMap[j-joff]).FastSet(value);
1500  } // else not a validity channel or time
1501  }
1502 
1503  fRow[ioff+irow].SetInDB();
1504  ++irow;
1505  }
1506  delete r;
1507 
1508  fin.close();
1509 
1510  return true;
1511  }
std::string Name()
Definition: Table.h:59
TString fin
Definition: Style.C:24
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
OStream cerr
Definition: OStream.cxx:7
string trim(string in)
Definition: rootgINukeVal.C:65
void AddEmptyRows(unsigned int nrow)
Definition: Table.cpp:371
const XML_Char * s
Definition: expat.h:262
nutools::dbi::Row *const NewRow()
Definition: Table.h:126
const XML_Char int const XML_Char * value
Definition: expat.h:331
const double j
Definition: BetheBloch.cxx:29
OStream cout
Definition: OStream.cxx:6
TRandom3 r(0)
std::vector< nutools::dbi::Row > fRow
Definition: Table.h:388
enum BeamMode string
bool nutools::dbi::Table::LoadFromCSV ( const char *  fname)
inline

Definition at line 145 of file Table.h.

References lemmon_tools::appendToFile(), LoadFromCSV(), LoadFromDB(), string, WriteToCSV(), and WriteToDB().

146  { return LoadFromCSV(std::string(fname)); }
bool LoadFromCSV(std::string fname)
Definition: Table.cpp:1291
enum BeamMode string
bool Table::LoadFromDB ( )

Definition at line 1065 of file Table.cpp.

References AddEmptyRows(), om::cerr, CloseConnection(), run_hadd::cmd, allTimeWatchdog::endl, ExistsInDB(), fCol, fConnection, fDescOrder, fDistinctCol, fHasConnection, fIgnoreDB, fOrderCol, fRow, fSchema, fSelectLimit, fSelectOffset, fTimeParsing, fTimeQueries, fValidityChanged, fValidityEnd, fValiditySQL, fValidityStart, fVerbosity, GetConnection(), MECModelEnuComparisons::i, calib::j, Name(), Schema(), string, and fixRunHistoryEndTimesViaNearline::tdiff.

Referenced by LoadFromCSV(), and LoadNonConditionsTable().

1066  {
1067  if (fIgnoreDB) return false;
1068 
1069  if (fSchema == "undef") {
1070  std::cerr << "Table::LoadFromDB: Detector not set! Table::SetDetector()"
1071  << " must be called first!" << std::endl;
1072  return false;
1073  }
1074 
1075  if (!fValidityChanged) return true;
1076 
1077  // make a connection to the dB if there isn't one already
1078  bool hasConn = fHasConnection;
1079  if (! fHasConnection) {
1080  GetConnection();
1081  hasConn = false;
1082  }
1083 
1084  if (!fConnection) {
1085  std::cerr << "Table::LoadFromDB: No connection to the database!" << std::endl;
1086  return false;
1087  }
1088 
1089  if (!ExistsInDB()) {
1090  std::cerr << "Table::LoadFromDB: Table \"" << Name()
1091  << "\" not found in database!" << std::endl;
1092  CloseConnection();
1093  return false;
1094  }
1095 
1096  std::string cmd;
1097  cmd.clear();
1098  PGresult* res;
1099 
1100  std::ostringstream outs;
1101  outs << "BEGIN";
1102  res = PQexec(fConnection, outs.str().c_str());
1103  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
1104  std::cerr << "BEGIN command failed: " << PQerrorMessage(fConnection) << std::endl;
1105  PQclear(res);
1106  CloseConnection();
1107  return false;
1108  }
1109 
1110  PQclear(res);
1111 
1112  outs.str("");
1113  outs << "DECLARE myportal CURSOR FOR SELECT ";
1114  if (!fDistinctCol.empty()) {
1115  outs << "DISTINCT ON (";
1116  if (! fDistinctCol.empty()) {
1117  for (unsigned int i=0; i<fDistinctCol.size(); ++i) {
1118  outs << fDistinctCol[i]->Name();
1119  if (i<(fDistinctCol.size()-1)) outs << ", ";
1120  }
1121  }
1122  outs << ") ";
1123  }
1124 
1125  outs << "* from ";
1126  outs << Schema() << "." << Name();
1127 
1128  if (! fValidityStart.empty() || fValiditySQL != "" ) {
1129  outs << " WHERE " << fValiditySQL;
1130  if (fValiditySQL != "" && !fValidityStart.empty()) outs << " and ";
1131 
1132  for (unsigned int i=0; i<fValidityStart.size(); ++i) {
1133  bool isEqualTo = (fValidityStart[i].Value() == fValidityEnd[i].Value());
1134  bool needsQuotes=false;
1135  if (fValidityStart[i].Type() == "string" ||
1136  fValidityStart[i].Type() == "text" ||
1137  fValidityStart[i].Type() == "timestamp" ||
1138  fValidityStart[i].Type() == "date") needsQuotes=true;
1139 
1140  outs << fValidityStart[i].Name();
1141  if (!isEqualTo)
1142  outs << ">=";
1143  else
1144  outs << "=";
1145 
1146  if (needsQuotes) outs << "'";
1147  outs << fValidityStart[i].Value();
1148  if (needsQuotes) outs << "'";
1149 
1150  if (!isEqualTo) {
1151  outs << " and ";
1152  outs << fValidityEnd[i].Name() + "<=";
1153  if (needsQuotes) outs << "'";
1154  outs << fValidityEnd[i].Value();
1155  if (needsQuotes) outs << "'";
1156  }
1157 
1158  if (i < (fValidityStart.size()-1)) outs << " and ";
1159  }
1160  }
1161 
1162  if (!fDistinctCol.empty() || !fOrderCol.empty()) {
1163  outs << " ORDER BY ";
1164 
1165  if (!fDistinctCol.empty()) {
1166  for (unsigned int i=0; i<fDistinctCol.size(); ++i) {
1167  outs << fDistinctCol[i]->Name();
1168  if (i<(fDistinctCol.size()-1)) outs << ", ";
1169  }
1170  }
1171 
1172  if (!fOrderCol.empty()) {
1173  for (unsigned int i=0; i<fOrderCol.size(); ++i) {
1174  outs << fOrderCol[i]->Name();
1175  if (i<(fOrderCol.size()-1)) outs << ", ";
1176  }
1177  }
1178 
1179  if (fDescOrder)
1180  outs << " DESC";
1181  else
1182  outs << " ASC";
1183  }
1184 
1185  if (fSelectLimit>0) {
1186  outs << " LIMIT " << boost::lexical_cast<std::string>(fSelectLimit);
1187  }
1188 
1189  if (fSelectOffset>0) {
1190  outs << " OFFSET " << boost::lexical_cast<std::string>(fSelectOffset);
1191  }
1192 
1193  if (fVerbosity > 0)
1194  std::cerr << "Table::LoadFromDB: Executing PGSQL command: \n\t" << outs.str() << std::endl;
1195  res = PQexec(fConnection,outs.str().c_str());
1196 
1197  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
1198  std::cerr << "DECLARE CURSOR failed: " << PQerrorMessage(fConnection) << std::endl;
1199  PQclear(res);
1200  CloseConnection();
1201  return false;
1202  }
1203  PQclear(res);
1204 
1205 
1206  boost::posix_time::ptime ctt1;
1207  boost::posix_time::ptime ctt2;
1208 
1209  if (fTimeQueries) {
1210  ctt1 = boost::posix_time::microsec_clock::local_time();
1211  }
1212 
1213  res = PQexec(fConnection, "FETCH ALL in myportal");
1214  if (fTimeQueries) {
1215  ctt2 = boost::posix_time::microsec_clock::local_time();
1216  boost::posix_time::time_duration tdiff = ctt2 - ctt1;
1217  std::cerr << "Table::LoadFromDB(" << Name() << "): query took "
1218  << tdiff.total_milliseconds() << " ms" << std::endl;
1219  }
1220 
1221  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
1222  std::cerr << "FETCH ALL failed: %" << PQerrorMessage(fConnection) << std::endl;
1223  PQclear(res);
1224  CloseConnection();
1225  return false;
1226  }
1227 
1228  // now cache rows
1229  int nRow = PQntuples(res);
1230  if (fVerbosity>0)
1231  std::cerr << "Table::LoadFromDB(" << Name() << "): got " << nRow
1232  << " rows of data." << std::endl;
1233 
1234  if (fTimeParsing)
1235  ctt1 = boost::posix_time::microsec_clock::local_time();
1236 
1237  if (nRow > 0) {
1238  std::vector<int> colMap(fCol.size());
1239 
1240  for (unsigned int i=0; i<fCol.size(); ++i) {
1241  colMap[i] = PQfnumber(res,fCol[i].Name().c_str());
1242  }
1243 
1244  int k;
1245 
1246  unsigned int ioff = fRow.size();
1247  AddEmptyRows(nRow);
1248 
1249  for (int i=0; i < nRow; i++) {
1250  for (unsigned int j=0; j < fCol.size(); j++) {
1251  k = colMap[j];
1252  if (k >= 0) {
1253  if (! PQgetisnull(res,i,k)) {
1254  std::string vstr = PQgetvalue(res,i,k);
1255  fRow[ioff+i].Col(j).FastSet(vstr);
1256  }
1257  // else
1258  // fRow[ioff+i].Col(j).FastSet("");
1259  }
1260  }
1261  fRow[ioff+i].SetInDB();
1262  }
1263  }
1264 
1265  if (fTimeParsing) {
1266  ctt2 = boost::posix_time::microsec_clock::local_time();
1267  boost::posix_time::time_duration tdiff = ctt2 - ctt1;
1268  std::cerr << "Table::LoadFromDB(" << Name() << "): parsing took "
1269  << tdiff.total_milliseconds() << " ms" << std::endl;
1270  }
1271 
1272  PQclear(res);
1273 
1274  /* close the portal ... we don't bother to check for errors ... */
1275  res = PQexec(fConnection, "CLOSE myportal");
1276  PQclear(res);
1277 
1278  /* end the transaction */
1279  res = PQexec(fConnection, "END");
1280  PQclear(res);
1281 
1282  // close connection to the dB if necessary
1283  if (! hasConn) CloseConnection();
1284 
1285  fValidityChanged = false;
1286 
1287  return true;
1288  }
std::string Name()
Definition: Table.h:59
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
std::vector< const nutools::dbi::ColumnDef * > fOrderCol
Definition: Table.h:394
std::string Schema()
Definition: Table.h:215
std::vector< nutools::dbi::ColumnDef > fValidityStart
Definition: Table.h:390
OStream cerr
Definition: OStream.cxx:7
PGconn * fConnection
Definition: Table.h:401
pg_result PGresult
Definition: Table.h:25
void AddEmptyRows(unsigned int nrow)
Definition: Table.cpp:371
std::string fValiditySQL
Definition: Table.h:378
bool fHasConnection
Definition: Table.h:351
string cmd
Definition: run_hadd.py:52
bool CloseConnection()
Definition: Table.cpp:830
short fVerbosity
Definition: Table.h:357
const double j
Definition: BetheBloch.cxx:29
std::string fSchema
Definition: Table.h:374
bool GetConnection(int ntry=0)
Definition: Table.cpp:752
bool fValidityChanged
Definition: Table.h:346
std::vector< nutools::dbi::Row > fRow
Definition: Table.h:388
std::vector< nutools::dbi::ColumnDef > fValidityEnd
Definition: Table.h:391
std::vector< const nutools::dbi::ColumnDef * > fDistinctCol
Definition: Table.h:393
enum BeamMode string
bool Table::LoadNonConditionsTable ( )
private

Definition at line 1751 of file Table.cpp.

References om::cerr, allTimeWatchdog::endl, fDescOrder, fDisableCache, fExcludeCol, fFlushCache, fOrderCol, fQEURL, fSelectLimit, fValidityEnd, fValiditySQL, fValidityStart, GetCol(), GetDataFromWebService(), MECModelEnuComparisons::i, calib::j, LoadFromDB(), nutools::dbi::ColumnDef::Name(), Name(), nc, NCol(), Schema(), and string.

Referenced by Folder(), and Load().

1752  {
1753  if (fQEURL == "") {
1754  std::cerr << "Table::LoadNonConditionsTable: Query Engine URL is not set! using Table::LoadFromDB() instead." << std::endl;
1755  return LoadFromDB();
1756  }
1757 
1758  if (fValiditySQL != "") {
1759  std::cerr << "Table::LoadNonConditionsTable: pure SQL statements are not supported, using Table::LoadFromDB() instead." << std::endl;
1760  return LoadFromDB();
1761  }
1762 
1763  std::stringstream myss;
1764 
1765  myss << fQEURL << "query?t=" << Schema() << "." << Name() << "&";
1766 
1767  int ncol = this->NCol();
1768 
1769  myss << "&c=";
1770  int nc=0;
1771  for (int i=0; i<ncol; ++i) {
1772  std::string cname = this->GetCol(i)->Name();
1773  bool skipCol = false;
1774  for (size_t j=0; j<fExcludeCol.size(); ++j) {
1775  if (fExcludeCol[j] == cname) {
1776  skipCol = true;
1777  break;
1778  }
1779  }
1780  if (skipCol) continue;
1781 
1782  if (nc>0)
1783  myss << ",";
1784  myss << cname;
1785  ++nc;
1786  }
1787 
1788  if (! fValidityStart.empty()) {
1789  for (unsigned int i=0; i<fValidityStart.size(); ++i) {
1790  if (fValidityStart[i].Type() == "string" ||
1791  fValidityStart[i].Type() == "text" ||
1792  fValidityStart[i].Type() == "timestamp" ||
1793  fValidityStart[i].Type() == "date") {
1794  std::cerr << "Table::LoadNonConditionsTable: validity strings are not supported, using Table::LoadFromDB() instead." << std::endl;
1795  return LoadFromDB();
1796  }
1797 
1798  myss << "&w=";
1799  bool isEqualTo = (fValidityStart[i].Value() == fValidityEnd[i].Value());
1800  if (isEqualTo) {
1801  myss << fValidityStart[i].Name() << ":"
1802  << fValidityStart[i].Value();
1803  }
1804  else {
1805  myss << fValidityStart[i].Name() << ":ge:"
1806  << fValidityStart[i].Value() << "&w="
1807  << fValidityEnd[i].Name() << ":le:"
1808  << fValidityEnd[i].Value();
1809  }
1810 
1811  }
1812  }
1813 
1814  if (!fOrderCol.empty()) {
1815  myss << "&o=";
1816  if (fDescOrder)
1817  myss << "-";
1818  for (unsigned int i=0; i<fOrderCol.size(); ++i) {
1819  myss << fOrderCol[i]->Name();
1820  if (i<(fOrderCol.size()-1)) myss << ", ";
1821  }
1822 
1823  }
1824 
1825  if (fSelectLimit>0)
1826  myss << "&l=" << fSelectLimit;
1827 
1828  if (fDisableCache) {
1829  if (fFlushCache)
1830  myss << "&x=clear";
1831  else
1832  myss << "&x=no";
1833  }
1834 
1835  Dataset ds;
1836 
1837  return GetDataFromWebService(ds,myss.str());
1838 
1839  }
std::string Name()
Definition: Table.h:59
std::vector< const nutools::dbi::ColumnDef * > fOrderCol
Definition: Table.h:394
bool fDisableCache
Definition: Table.h:354
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
std::string fQEURL
Definition: Table.h:385
std::vector< std::string > fExcludeCol
Definition: Table.h:396
std::string Schema()
Definition: Table.h:215
std::vector< nutools::dbi::ColumnDef > fValidityStart
Definition: Table.h:390
OStream cerr
Definition: OStream.cxx:7
std::string fValiditySQL
Definition: Table.h:378
bool GetDataFromWebService(Dataset &, std::string)
Definition: Table.cpp:1565
const double j
Definition: BetheBloch.cxx:29
std::string Name() const
Definition: ColumnDef.h:21
A very simple service to remember what detector we&#39;re working in.
enum BeamMode nc
std::vector< nutools::dbi::ColumnDef > fValidityEnd
Definition: Table.h:391
enum BeamMode string
bool Table::LoadUnstructuredConditionsTable ( )
private

Definition at line 1843 of file Table.cpp.

References om::cerr, allTimeWatchdog::endl, fMaxTSVld, fMinTSVld, Folder(), fUConDBURL, cet::getenv(), Name(), nutools::dbi::Util::RunningOnGrid(), and string.

Referenced by Folder(), and Load().

1844  {
1845  if (fMinTSVld == 0 || fMaxTSVld == 0) {
1846  std::cerr << "Table::LoadUnstructuredConditionsTable: No validity time is set!" << std::endl;
1847  return false;
1848  }
1849 
1850  if (fUConDBURL == "") {
1851  std::cerr << "Table::LoadConditionsTable: Web Service URL is not set!" << std::endl;
1852  return false;
1853  }
1854 
1855  if (!Util::RunningOnGrid()) {
1856  std::string interactiveURL = getenv("DBIUCONDBURLINT");
1857  if (!interactiveURL.empty())
1858  fUConDBURL = interactiveURL;
1859  }
1860 
1861  // int ncol = this->NCol();
1862 
1863  std::stringstream myss;
1864 
1865  myss << fUConDBURL << "get?folder=" << Folder() << "." << Name() << "&";
1866 
1867 
1868  return false;
1869  }
std::string Name()
Definition: Table.h:59
static bool RunningOnGrid()
Definition: Util.cpp:132
OStream cerr
Definition: OStream.cxx:7
std::string Folder()
Definition: Table.h:321
std::string getenv(std::string const &name)
std::string fUConDBURL
Definition: Table.h:384
enum BeamMode string
bool Table::MakeConditionsCSVString ( std::stringstream &  ss)
private

Definition at line 2298 of file Table.cpp.

References nutools::dbi::Row::Channel(), nutools::dbi::Row::Col(), allTimeWatchdog::endl, GetCol(), GetRow(), MECModelEnuComparisons::i, calib::j, nutools::dbi::ColumnDef::Name(), NCol(), NRow(), string, Tolerance(), nutools::dbi::ColumnDef::Type(), nutools::dbi::Row::VldTime(), and nutools::dbi::Row::VldTimeEnd().

Referenced by Folder(), Write(), and WriteToCSV().

2299  {
2300  int ncol = this->NCol();
2301  int nrow = this->NRow();
2302 
2303  ss << "channel,tv,";
2304  bool first = true;
2305  for (int i=0; i<ncol; ++i) {
2306  std::string cname = this->GetCol(i)->Name();
2307  if(!first) ss << ",";
2308  first = false;
2309  ss << cname;
2310  }
2311  ss << std::endl;
2312 
2313  ss << "tolerance,,";
2314  first = true;
2315  for (int j=0; j<ncol; ++j) {
2316  std::string cname = this->GetCol(j)->Name();
2317  std::string ctype = this->GetCol(j)->Type();
2318  if(!first) ss << ",";
2319  first = false;
2320  float tol = this->Tolerance(cname);
2321  if (tol == 0.) {
2322  if (ctype == "double")
2323  ss << "1.e-10";
2324  else if (ctype == "float")
2325  ss << "1.e-5";
2326  }
2327  else
2328  ss << this->Tolerance(cname);
2329  }
2330  ss << std::endl;
2331  for (int i=0; i<nrow; ++i) {
2332  ss << GetRow(i)->Channel() << ","
2333  << GetRow(i)->VldTime() << ",";
2334  if (GetRow(i)->VldTimeEnd() > GetRow(i)->VldTime())
2335  ss << GetRow(i)->VldTimeEnd() << ",";
2336  first = true;
2337  for (int j=0; j<ncol; ++j) {
2338  if(!first) ss << ",";
2339  first = false;
2340  ss << GetRow(i)->Col(j);
2341  }
2342  ss << std::endl;
2343  }
2344 
2345  return true;
2346  }
float VldTime()
Definition: Row.h:56
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
Float_t ss
Definition: plot.C:24
uint64_t Channel()
Definition: Row.h:55
float VldTimeEnd()
Definition: Row.h:57
std::string Type() const
Definition: ColumnDef.h:22
const double j
Definition: BetheBloch.cxx:29
std::string Name() const
Definition: ColumnDef.h:21
nutools::dbi::Row *const GetRow(int i)
Definition: Table.cpp:406
Column & Col(int i)
Definition: Row.h:53
float Tolerance(std::string &cname)
Definition: Table.cpp:625
enum BeamMode string
std::string nutools::dbi::Table::Name ( void  )
inline
int nutools::dbi::Table::NCol ( )
inline

Definition at line 102 of file Table.h.

References fCol.

Referenced by LoadConditionsTable(), LoadNonConditionsTable(), MakeConditionsCSVString(), and PrintColumns().

102 {return fCol.size();}
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
nutools::dbi::Row* const nutools::dbi::Table::NewRow ( )
inline

Definition at line 126 of file Table.h.

References fCol, GetColNames(), GetColNameToIndexMap(), and r().

Referenced by AddEmptyRows(), and LoadFromCSV().

126 { Row* r = new Row(fCol); return r;}
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
TRandom3 r(0)
int nutools::dbi::Table::NRow ( )
inline

Definition at line 103 of file Table.h.

References fRow.

Referenced by FillChanRowMap(), and MakeConditionsCSVString().

103 {return fRow.size();}
std::vector< nutools::dbi::Row > fRow
Definition: Table.h:388
int nutools::dbi::Table::NVldChannels ( )
inline

Definition at line 287 of file Table.h.

References fChanRowMap.

287 { return fChanRowMap.size(); }
std::unordered_map< uint64_t, std::vector< nutools::dbi::Row * > > fChanRowMap
Definition: Table.h:399
int nutools::dbi::Table::NVldRows ( uint64_t  channel)
inline

Definition at line 286 of file Table.h.

References fChanRowMap.

286 { return fChanRowMap[channel].size(); }
std::unordered_map< uint64_t, std::vector< nutools::dbi::Row * > > fChanRowMap
Definition: Table.h:399
int Table::ParseSelfStatusLine ( char *  line)
private

Definition at line 1515 of file Table.cpp.

References MECModelEnuComparisons::i.

Referenced by Folder(), PrintPMUsed(), and PrintVMUsed().

1516  {
1517  int i = strlen(line);
1518  while (*line < '0' || *line > '9') line++;
1519  line[i-3] = '\0';
1520  i = atoi(line);
1521  return i;
1522  }
void Table::PrintColumns ( )

Definition at line 638 of file Table.cpp.

References om::cout, allTimeWatchdog::endl, GetCol(), MECModelEnuComparisons::i, calib::j, art::left(), demo0::length, nutools::dbi::ColumnDef::Name(), NCol(), and nutools::dbi::ColumnDef::Type().

Referenced by RemoveValidityRange().

639  {
640  std::cout << std::endl;
641  int i = 0;
642  std::vector<int> len(0);
643  int tlen;
644  int sumlen = 0;
645 
646  for ( ; i<this->NCol(); ++i) {
647  tlen = this->GetCol(i)->Name().length();
648  if ((int)this->GetCol(i)->Type().length() > tlen)
649  tlen = this->GetCol(i)->Type().length();
650  len.push_back(tlen);
651  sumlen += tlen;
652  }
653 
654  int nsp = 0;
655  i = 0;
656  int j = 0;
657  while (i < this->NCol()) {
658  for ( ; i<this->NCol() && nsp<78; ++i)
659  nsp += len[i] + 1;
660 
661  for (int k=0; k<nsp; ++k) {
662  std::cout << "_" ;
663  }
664  std::cout << std::endl;
665 
666  int j_save = j;
667  for (; j<i; ++j)
668  std::cout << "|" << std::setw(len[j]) << std::left << this->GetCol(j)->Name();
669  std::cout << "|" << std::endl;
670 
671  for (int k=0; k<nsp; ++k) {
672  std::cout << "-" ;
673  }
674  std::cout << std::endl;
675 
676  j = j_save;
677  for ( ; j<i; ++j)
678  std::cout << "|" << std::setw(len[j]) << std::left << this->GetCol(j)->Type();
679  std::cout << "|" << std::endl;
680 
681  for (int k=0; k<nsp; ++k) {
682  std::cout << "-" ;
683  }
684  std::cout << std::endl;
685 
686  nsp = 0;
687  }
688 
689  }
const XML_Char int len
Definition: expat.h:262
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
length
Definition: demo0.py:21
std::string Type() const
Definition: ColumnDef.h:22
const double j
Definition: BetheBloch.cxx:29
std::string Name() const
Definition: ColumnDef.h:21
OStream cout
Definition: OStream.cxx:6
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:104
void Table::PrintPMUsed ( )

Definition at line 1546 of file Table.cpp.

References om::cerr, allTimeWatchdog::endl, fclose(), file, make_syst_table_plots::line, Name(), ParseSelfStatusLine(), fillBadChanDBTables::result, and Schema().

Referenced by SetChannelRange().

1546  { //Note: this value is in MB!
1547  FILE* file = fopen("/proc/self/status", "r");
1548  int result = -1;
1549  char line[128];
1550 
1551 
1552  while (fgets(line, 128, file) != NULL){
1553  if (strncmp(line, "VmRSS:", 6) == 0){
1554  result = this->ParseSelfStatusLine(line);
1555  break;
1556  }
1557  }
1558  fclose(file);
1559  std::cerr << Schema() << "." << Name() << ": this process using "
1560  << result/1024 << " MB of PhysicalMemory" << std::endl;
1561  }
std::string Name()
Definition: Table.h:59
std::string Schema()
Definition: Table.h:215
OStream cerr
Definition: OStream.cxx:7
fclose(fg1)
int ParseSelfStatusLine(char *line)
Definition: Table.cpp:1515
TFile * file
Definition: cellShifts.C:17
void Table::PrintPQErrorMsg ( ) const

Definition at line 486 of file Table.cpp.

References om::cerr, allTimeWatchdog::endl, and fConnection.

Referenced by GetPKeyCol().

487  {
488  if (fConnection)
489  std::cerr << PQerrorMessage(fConnection) << std::endl;
490  }
OStream cerr
Definition: OStream.cxx:7
PGconn * fConnection
Definition: Table.h:401
void Table::PrintVMUsed ( )

Definition at line 1527 of file Table.cpp.

References om::cerr, allTimeWatchdog::endl, fclose(), file, make_syst_table_plots::line, Name(), ParseSelfStatusLine(), fillBadChanDBTables::result, and Schema().

Referenced by SetChannelRange().

1527  { //Note: this value is in MB!
1528  FILE* file = fopen("/proc/self/status", "r");
1529  int result = -1;
1530  char line[128];
1531 
1532 
1533  while (fgets(line, 128, file) != NULL){
1534  if (strncmp(line, "VmSize:", 7) == 0){
1535  result = this->ParseSelfStatusLine(line);
1536  break;
1537  }
1538  }
1539  fclose(file);
1540  std::cerr << Schema() << "." << Name() << ": this process using "
1541  << result/1024 << " MB of VirtualMemory" << std::endl;
1542  }
std::string Name()
Definition: Table.h:59
std::string Schema()
Definition: Table.h:215
OStream cerr
Definition: OStream.cxx:7
fclose(fg1)
int ParseSelfStatusLine(char *line)
Definition: Table.cpp:1515
TFile * file
Definition: cellShifts.C:17
bool Table::RemoveDistinctColumn ( unsigned int  i)

Definition at line 2475 of file Table.cpp.

References plot_validation_datamc::c, fCol, fDistinctCol, MECModelEnuComparisons::i, and calib::j.

Referenced by AddDistinctColumn(), and RemoveDistinctColumn().

2476  {
2477  if (i >= fCol.size()) return false;
2478 
2479  const ColumnDef* c = &fCol[i];
2480  for (unsigned int j=0; j<fDistinctCol.size(); ++j)
2481  if (fDistinctCol[j] == c) {
2482  fDistinctCol.erase(fDistinctCol.begin() + j);
2483  return true;
2484  }
2485 
2486  return false;
2487  }
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
const double j
Definition: BetheBloch.cxx:29
std::vector< const nutools::dbi::ColumnDef * > fDistinctCol
Definition: Table.h:393
bool Table::RemoveDistinctColumn ( std::string  col)

Definition at line 2511 of file Table.cpp.

References plot_validation_datamc::c, fDistinctCol, GetCol(), and calib::j.

2512  {
2513  const ColumnDef* c = GetCol(cname);
2514 
2515  if (!c) return false;
2516 
2517  for (unsigned int j=0; j<fDistinctCol.size(); ++j)
2518  if (fDistinctCol[j] == c) {
2519  fDistinctCol.erase(fDistinctCol.begin() + j);
2520  return true;
2521  }
2522 
2523  return false;
2524  }
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
const double j
Definition: BetheBloch.cxx:29
std::vector< const nutools::dbi::ColumnDef * > fDistinctCol
Definition: Table.h:393
bool nutools::dbi::Table::RemoveDistinctColumn ( const char *  col)
inline

Definition at line 162 of file Table.h.

References AddOrderColumn(), RemoveDistinctColumn(), and string.

163  { return RemoveDistinctColumn(std::string(col)); }
bool RemoveDistinctColumn(unsigned int i)
Definition: Table.cpp:2475
Int_t col[ntarg]
Definition: Style.C:29
enum BeamMode string
bool Table::RemoveOrderColumn ( unsigned int  i)

Definition at line 2547 of file Table.cpp.

References plot_validation_datamc::c, fCol, fOrderCol, MECModelEnuComparisons::i, and calib::j.

Referenced by AddOrderColumn(), and RemoveOrderColumn().

2548  {
2549  if (i >= fCol.size()) return false;
2550 
2551  const ColumnDef* c = &fCol[i];
2552  for (unsigned int j=0; j<fOrderCol.size(); ++j)
2553  if (fOrderCol[j] == c) {
2554  fOrderCol.erase(fOrderCol.begin() + j);
2555  return true;
2556  }
2557 
2558  return false;
2559  }
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
std::vector< const nutools::dbi::ColumnDef * > fOrderCol
Definition: Table.h:394
const double j
Definition: BetheBloch.cxx:29
bool Table::RemoveOrderColumn ( std::string  col)

Definition at line 2583 of file Table.cpp.

References plot_validation_datamc::c, fOrderCol, GetCol(), and calib::j.

2584  {
2585  const ColumnDef* c = GetCol(cname);
2586 
2587  if (!c) return false;
2588 
2589  for (unsigned int j=0; j<fOrderCol.size(); ++j)
2590  if (fOrderCol[j] == c) {
2591  fOrderCol.erase(fOrderCol.begin() + j);
2592  return true;
2593  }
2594 
2595  return false;
2596  }
std::vector< const nutools::dbi::ColumnDef * > fOrderCol
Definition: Table.h:394
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
const double j
Definition: BetheBloch.cxx:29
bool nutools::dbi::Table::RemoveOrderColumn ( const char *  col)
inline

Definition at line 171 of file Table.h.

References RemoveOrderColumn(), and string.

172  { return RemoveOrderColumn(std::string(col)); }
Int_t col[ntarg]
Definition: Style.C:29
bool RemoveOrderColumn(unsigned int i)
Definition: Table.cpp:2547
enum BeamMode string
bool Table::RemoveRow ( int  i)

note, this will only delete a row from memory, it will not delete an existing row in a dB!

Definition at line 385 of file Table.cpp.

References fNullList, fRow, MECModelEnuComparisons::i, calib::j, and cmf::kEnd.

Referenced by ClearRows().

386  {
387  if (i < 0) return false;
388 
389  unsigned int j = i;
390 
391  if (j >= fRow.size()) return false;
392 
393  unsigned int kEnd = fNullList.size();
394  for (unsigned int k=0; k<kEnd; ++k)
395  if (fNullList[k].first == i) {
396  fNullList.erase(fNullList.begin()+k);
397  kEnd = fNullList.size();
398  }
399 
400  fRow.erase(fRow.begin()+j);
401 
402  return true;
403  }
const double j
Definition: BetheBloch.cxx:29
std::vector< std::pair< int, int > > fNullList
Definition: Table.h:395
std::vector< nutools::dbi::Row > fRow
Definition: Table.h:388
void Table::RemoveValidityRange ( std::string cname)

Definition at line 2444 of file Table.cpp.

References fValidityEnd, fValidityStart, MECModelEnuComparisons::i, and Name().

Referenced by RemoveValidityRange(), and SetValidityRange().

2445  {
2446  unsigned int i=0;
2447  for (; i<fValidityStart.size(); ++i)
2448  if (fValidityStart[i].Name() == cname) {
2449  fValidityStart.erase(fValidityStart.begin()+i);
2450  fValidityEnd.erase(fValidityEnd.begin()+i);
2451  }
2452  }
std::string Name()
Definition: Table.h:59
std::vector< nutools::dbi::ColumnDef > fValidityStart
Definition: Table.h:390
std::vector< nutools::dbi::ColumnDef > fValidityEnd
Definition: Table.h:391
void nutools::dbi::Table::RemoveValidityRange ( const char *  cname)
inline

Definition at line 256 of file Table.h.

References CloseConnection(), fValidityChanged, GetConnection(), PrintColumns(), RemoveValidityRange(), and string.

257  { std::string cstr(cname); return RemoveValidityRange(cstr); fValidityChanged=true;}
void RemoveValidityRange(std::string &cname)
Definition: Table.cpp:2444
bool fValidityChanged
Definition: Table.h:346
enum BeamMode string
void Table::Reset ( void  )
private

Definition at line 460 of file Table.cpp.

References ClearValidity(), fConnection, fDescOrder, fDistinctCol, fExcludeCol, fHasConnection, fMaxChannel, fMinChannel, fPKeyList, fSelectLimit, fSelectOffset, and fVerbosity.

Referenced by Folder(), and Table().

461  {
462  fConnection = 0;
463  fHasConnection = 0;
464  fPKeyList.clear();
465  fDistinctCol.clear();
466  fVerbosity = 0;
467  fDescOrder = true;
468  fSelectLimit = 0;
469  fSelectOffset = 0;
470  ClearValidity();
471  fMinChannel = 0;
472  fMaxChannel = 0;
473  fExcludeCol.clear();
474  }
uint64_t fMaxChannel
Definition: Table.h:366
std::vector< std::string > fExcludeCol
Definition: Table.h:396
PGconn * fConnection
Definition: Table.h:401
std::vector< const nutools::dbi::ColumnDef * > fPKeyList
Definition: Table.h:392
uint64_t fMinChannel
Definition: Table.h:365
bool fHasConnection
Definition: Table.h:351
short fVerbosity
Definition: Table.h:357
void ClearValidity()
Definition: Table.cpp:477
std::vector< const nutools::dbi::ColumnDef * > fDistinctCol
Definition: Table.h:393
bool nutools::dbi::Table::ResetConnectionInfo ( )

Referenced by GetConnectionTimeout().

void nutools::dbi::Table::ResetRole ( )
inline

Definition at line 98 of file Table.h.

References fRole, and fUser.

98 { fRole = fUser; }
std::string fUser
Definition: Table.h:369
std::string fRole
Definition: Table.h:370
std::string nutools::dbi::Table::Role ( )
inline

Definition at line 63 of file Table.h.

References fRole.

63 { return fRole; }
std::string fRole
Definition: Table.h:370
std::string nutools::dbi::Table::Schema ( )
inline

Definition at line 215 of file Table.h.

References fSchema.

Referenced by GetCurrSeqVal(), LoadConditionsTable(), LoadFromDB(), LoadNonConditionsTable(), PrintPMUsed(), PrintVMUsed(), Tag(), Write(), and WriteToDB().

215 { return fSchema; }
std::string fSchema
Definition: Table.h:374
void nutools::dbi::Table::SetChannelRange ( uint64_t  chan1,
uint64_t  chan2 
)
inline

Definition at line 312 of file Table.h.

References fMaxChannel, fMinChannel, GetColsFromDB(), PrintPMUsed(), and PrintVMUsed().

313  { fMinChannel=chan1; fMaxChannel=chan2;}
uint64_t fMaxChannel
Definition: Table.h:366
uint64_t fMinChannel
Definition: Table.h:365
void nutools::dbi::Table::SetConnectionTimeout ( int  n)
inline

Definition at line 263 of file Table.h.

References fConnectionTimeout, and getGoodRuns4SAM::n.

263 { fConnectionTimeout=n;} // units in sec
int fConnectionTimeout
Definition: Table.h:361
void Table::SetDataSource ( std::string  ds)

Definition at line 530 of file Table.cpp.

References nutools::dbi::kDAQ, nutools::dbi::kDCS, nutools::dbi::kOffline, nutools::dbi::kUnknownSource, and string.

Referenced by nutools::dbi::DBIService::CreateTable(), and DataTypeMask().

530  {
531  if (ds == std::string("DAQ"))
533  else if (ds == std::string("DCS"))
535  else if (ds == std::string("Offline"))
537  else
539  }
A very simple service to remember what detector we&#39;re working in.
void SetDataSource(std::string ds)
Definition: Table.cpp:530
enum BeamMode string
void Table::SetDataSource ( int  ids)
void nutools::dbi::Table::SetDataTypeMask ( int  mask)
inline

Definition at line 76 of file Table.h.

References fDataTypeMask, and lem_server::mask.

void nutools::dbi::Table::SetDBHost ( std::string  dbhost)
inline

Definition at line 85 of file Table.h.

References fDBHost.

Referenced by SetDBInfo(), and Table().

85 { fDBHost = dbhost; }
std::string fDBHost
Definition: Table.h:372
void nutools::dbi::Table::SetDBHost ( const char *  dbhost)
inline

Definition at line 86 of file Table.h.

References fDBHost.

86 { fDBHost = dbhost; }
std::string fDBHost
Definition: Table.h:372
void Table::SetDBInfo ( std::string  name,
std::string  host,
std::string  port,
std::string  user 
)

Definition at line 550 of file Table.cpp.

References SetDBHost(), SetDBName(), SetDBPort(), and SetUser().

Referenced by SetDBPort().

552  {
553  SetDBName(name);
554  SetDBHost(host);
555  SetDBPort(port);
556  SetUser(user);
557  }
const XML_Char * name
Definition: expat.h:151
void SetDBName(std::string dbname)
Definition: Table.h:83
void SetDBHost(std::string dbhost)
Definition: Table.h:85
void SetDBPort(std::string p)
Definition: Table.h:87
void SetUser(std::string uname)
Definition: Table.h:79
int port
Definition: client_test.C:9
void Table::SetDBInfo ( const char *  name,
const char *  host,
const char *  port,
const char *  user 
)

Definition at line 560 of file Table.cpp.

References SetDBHost(), SetDBName(), SetDBPort(), and SetUser().

562  {
563  SetDBName(name);
564  SetDBHost(host);
565  SetDBPort(port);
566  SetUser(user);
567  }
const XML_Char * name
Definition: expat.h:151
void SetDBName(std::string dbname)
Definition: Table.h:83
void SetDBHost(std::string dbhost)
Definition: Table.h:85
void SetDBPort(std::string p)
Definition: Table.h:87
void SetUser(std::string uname)
Definition: Table.h:79
int port
Definition: client_test.C:9
void nutools::dbi::Table::SetDBName ( std::string  dbname)
inline

Definition at line 83 of file Table.h.

References fDBName.

Referenced by SetDBInfo(), and Table().

83 { fDBName = dbname; }
std::string fDBName
Definition: Table.h:373
void nutools::dbi::Table::SetDBName ( const char *  dbname)
inline

Definition at line 84 of file Table.h.

References fDBName.

84 { fDBName = dbname; }
std::string fDBName
Definition: Table.h:373
void nutools::dbi::Table::SetDBPort ( std::string  p)
inline

Definition at line 87 of file Table.h.

References fDBPort.

Referenced by SetDBInfo(), and Table().

87 { fDBPort = p; }
const char * p
Definition: xmltok.h:285
std::string fDBPort
Definition: Table.h:371
void nutools::dbi::Table::SetDBPort ( const char *  p)
inline

Definition at line 88 of file Table.h.

References fDBPort, plot_validation_datamc::fname, drop_table::host, port, SetDBInfo(), SetPasswordFile(), string, and drop_table::user.

88 { fDBPort = p; }
const char * p
Definition: xmltok.h:285
std::string fDBPort
Definition: Table.h:371
bool Table::SetDetector ( std::string  det)

Definition at line 493 of file Table.cpp.

References fillBadChanDBTables::det, fDetector, fSchema, fTableType, and nutools::dbi::kHardwareTable.

Referenced by nutools::dbi::DBIService::CreateTable(), and SetValiditySQL().

494  {
495  fDetector = det;
496 
497  if (fTableType != kHardwareTable)
498  fSchema = det;
499  else
500  fSchema = "public";
501 
502  boost::to_lower(fSchema);
503 
504  return true;
505  }
std::string fSchema
Definition: Table.h:374
std::string fDetector
Definition: Table.h:379
void nutools::dbi::Table::SetFolder ( std::string  f)
inline

Definition at line 320 of file Table.h.

References MakeMiniprodValidationCuts::f, and fFolder.

void nutools::dbi::Table::SetIgnoreEnvVar ( bool  f)
inline
void nutools::dbi::Table::SetMaxChannel ( uint64_t  chan)
inline

Definition at line 311 of file Table.h.

References febshutoff_auto::chan, and fMaxChannel.

void nutools::dbi::Table::SetMaxTSVld ( float  t)
inline

Definition at line 272 of file Table.h.

References fMaxTSVld, and confusionMatrixTree::t.

void nutools::dbi::Table::SetMinChannel ( uint64_t  chan)
inline

Definition at line 310 of file Table.h.

References febshutoff_auto::chan, and fMinChannel.

void nutools::dbi::Table::SetMinTSVld ( float  t)
inline

Definition at line 271 of file Table.h.

References fMinTSVld, and confusionMatrixTree::t.

void nutools::dbi::Table::SetOrderAsc ( )
inline

Definition at line 178 of file Table.h.

References fDescOrder.

178 { fDescOrder = false; }
void nutools::dbi::Table::SetOrderDesc ( )
inline

Definition at line 177 of file Table.h.

References fDescOrder.

177 { fDescOrder = true; }
bool Table::SetPasswordFile ( const char *  fname = 0)

fname should be the name of the file

Definition at line 852 of file Table.cpp.

References om::cerr, allTimeWatchdog::endl, fin, plot_validation_datamc::fname, fPassword, cet::getenv(), and string.

Referenced by GetConnection(), and SetDBPort().

853  {
854  std::string fNameStr = "";
855 
856  if (fname == 0) {
857  char* tmpStr = getenv("DBIPWDFILE");
858  if (tmpStr)
859  fNameStr = tmpStr;
860  else {
861  std::cerr << "DBIPWDFILE env. variable is not set, disabling "
862  << "password-access to the dB." << std::endl;
863  fPassword = "";
864  return false;
865  }
866  }
867  else
868  fNameStr = fname;
869 
870  std::ifstream fin;
871  fin.open(fNameStr.c_str());
872  if (!fin.is_open() || !fin.good()) {
873  std::cerr << "Could not open password file " << fNameStr
874  << ". Disabling password-access to the dB." << std::endl;
875  return false;
876  }
877  else {
878  fin >> fPassword;
879  fin.close();
880  }
881 
882  return true;
883  }
TString fin
Definition: Style.C:24
OStream cerr
Definition: OStream.cxx:7
std::string fPassword
Definition: Table.h:377
std::string getenv(std::string const &name)
enum BeamMode string
void nutools::dbi::Ta