LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
nutools::dbi::Table Class Reference

#include "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::string > GetColNames ()
 
std::map< std::string, int > GetColNameToIndexMap ()
 
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 (double t)
 
void SetMaxTSVld (double t)
 
double GetMaxTSVld () const
 
double 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, double t)
 
std::vector< nutools::dbi::Row * > GetVldRows (uint64_t channel)
 
void SetRecordTime (double 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::string > fExcludeCol
 
std::vector< uint64_t > fChannelVec
 
std::unordered_map< uint64_t, std::vector< nutools::dbi::Row * > > fChanRowMap
 
PGconnfConnection
 
double fMaxTSVld
 
double fMinTSVld
 
double 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

nutools::dbi::Table::Table ( )

Definition at line 36 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, nutools::dbi::kGenericTable, nutools::dbi::kUnknownSource, and Reset().

36  : fHasRecordTime(false),
37  fFlushCache(false),
38  fDisableCache(false),
39  fMaxTSVld(0),fMinTSVld(0),
40  fRecordTime(0)
41  {
42  fTableName="";
43  fConnection=0;
44  fHasConnection=false;
45  fDetector="";
46  fDBHost="";
47  fDBName="";
48  fDBPort="";
49  fUser="";
50  fSchema="undef";
52  fDataTypeMask = 0;
54 
55  fIgnoreEnvVar = false;
56  fTestedExists = false;
57  fExistsInDB = false;
58  addInsertTime = addUpdateTime = false;
59  addInsertUser = addUpdateUser = false;
60  fIgnoreDB = false;
61  fTimeQueries = true;
62  fTimeParsing = true;
63  fMinChannel = 0;
64  fMaxChannel = 0;
65  fFolder = "";
66 
67  Reset();
68 
69  srandom(time(0));
70 
71  // default total time to attempt to connect to the db/web server will be
72  // ~4 minutes (some randomness is introduced by libwda)
73  fConnectionTimeout = 4*60;
74  // override default timeout if env. variable is set, but must be
75  // greater than 20 seconds
76  char* tmpStr = getenv("DBITIMEOUT");
77  if (tmpStr) {
78  int tmpTO = atoi(tmpStr);
79  if (tmpTO > 20)
80  fConnectionTimeout = tmpTO;
81  }
82 
83  fTag = "";
84  fWSURL = "";
85  const char* wsHost = getenv("DBIWSURL");
86  if (wsHost) fWSURL = std::string(wsHost);
87 
88  fUConDBURL = "";
89  const char* ucondbHost = getenv("DBIUCONDBURL");
90  if (ucondbHost) fUConDBURL = std::string(ucondbHost);
91 
92  fQEURL = "";
93  const char* qeHost = getenv("DBIQEURL");
94  if (qeHost) fQEURL = std::string(qeHost);
95 
96  fVerbosity=0;
97  tmpStr = getenv("DBIVERB");
98  if (tmpStr) {
99  fVerbosity = atoi(tmpStr);
100  }
101  }
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
double fMinTSVld
Definition: Table.h:406
int fConnectionTimeout
Definition: Table.h:361
bool fIgnoreEnvVar
Definition: Table.h:345
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
double fRecordTime
Definition: Table.h:407
double fMaxTSVld
Definition: Table.h:405
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
nutools::dbi::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 105 of file Table.cpp.

References CloseConnection(), ExistsInDB(), fCol, fConnection, fConnectionTimeout, fDataSource, fDBCacheFile, fExistsInDB, fFolder, fHasConnection, fIgnoreDB, fMaxChannel, fMinChannel, fSchema, fTimeParsing, fTimeQueries, fVerbosity, GetColsFromDB(), GetConnection(), nutools::dbi::kUnknownSource, Name(), Reset(), SetDBHost(), SetDBName(), SetDBPort(), SetTableName(), and SetUser().

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

Definition at line 237 of file Table.cpp.

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

238  {
239  this->Clear();
241  }
bool CloseConnection()
Definition: Table.cpp:837
bool fHasConnection
Definition: Table.h:351

Member Function Documentation

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

Definition at line 333 of file Table.cpp.

References addInsertTime, addInsertUser, addUpdateTime, addUpdateUser, fCol, and Name().

Referenced by ClearRows().

334  {
335  for (size_t i=0; i<fCol.size(); ++i) {
336  if (fCol[i].Name() == cname) {
337  std::cerr << "Table::AddCol: column \'" << cname << "\' already exists! Fatal, aborting..." << std::endl;
338  abort();
339  }
340  }
341 
342  ColumnDef cdef(cname,ctype);
343 
344  fCol.push_back(cdef);
345 
346  if (cname == "inserttime") addInsertTime = true;
347  if (cname == "insertuser") addInsertUser = true;
348  if (cname == "updatetime") addUpdateTime = true;
349  if (cname == "updateuser") addUpdateUser = true;
350 
351  return fCol.size()-1;
352  }
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
bool addInsertUser
Definition: Table.h:342
bool addUpdateTime
Definition: Table.h:343
bool nutools::dbi::Table::AddDistinctColumn ( unsigned int  i)

Definition at line 2465 of file Table.cpp.

References fCol, and fDistinctCol.

Referenced by AddDistinctColumn(), and WriteToCSV().

2466  {
2467  if (i >= fCol.size()) return false;
2468 
2469  const ColumnDef* c = &fCol[i];
2470  for (unsigned int j=0; j<fDistinctCol.size(); ++j)
2471  if (fDistinctCol[j] == c) return true;
2472 
2473  fDistinctCol.push_back(c);
2474 
2475  return true;
2476  }
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
std::vector< const nutools::dbi::ColumnDef * > fDistinctCol
Definition: Table.h:393
bool nutools::dbi::Table::AddDistinctColumn ( std::string  col)

Definition at line 2500 of file Table.cpp.

References fDistinctCol, and GetCol().

2501  {
2502  const ColumnDef* c = GetCol(cname);
2503 
2504  if (!c) return false;
2505 
2506  for (unsigned int j=0; j<fDistinctCol.size(); ++j)
2507  if (fDistinctCol[j] == c) return true;
2508 
2509  fDistinctCol.push_back(c);
2510 
2511  return true;
2512  }
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
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(), and RemoveDistinctColumn().

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

Definition at line 378 of file Table.cpp.

References fRow, and NewRow().

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

379  {
380  Row* row = this->NewRow();
381 
382  fRow.resize(fRow.size()+nrow,*row);
383  }
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 nutools::dbi::Table::AddOrderColumn ( unsigned int  i)

Definition at line 2537 of file Table.cpp.

References fCol, and fOrderCol.

Referenced by AddOrderColumn(), and RemoveDistinctColumn().

2538  {
2539  if (i >= fCol.size()) return false;
2540 
2541  const ColumnDef* c = &fCol[i];
2542  for (unsigned int j=0; j<fOrderCol.size(); ++j)
2543  if (fOrderCol[j] == c) return true;
2544 
2545  fOrderCol.push_back(c);
2546 
2547  return true;
2548  }
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
std::vector< const nutools::dbi::ColumnDef * > fOrderCol
Definition: Table.h:394
bool nutools::dbi::Table::AddOrderColumn ( std::string  col)

Definition at line 2572 of file Table.cpp.

References fOrderCol, and GetCol().

2573  {
2574  const ColumnDef* c = GetCol(cname);
2575 
2576  if (!c) return false;
2577 
2578  for (unsigned int j=0; j<fOrderCol.size(); ++j)
2579  if (fOrderCol[j] == c) return true;
2580 
2581  fOrderCol.push_back(c);
2582 
2583  return true;
2584  }
std::vector< const nutools::dbi::ColumnDef * > fOrderCol
Definition: Table.h:394
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
bool nutools::dbi::Table::AddOrderColumn ( const char *  col)
inline

Definition at line 167 of file Table.h.

References AddOrderColumn(), and RemoveOrderColumn().

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

Definition at line 356 of file Table.cpp.

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

Referenced by AddRow(), and ClearRows().

357  {
358  if (!row) return;
359 
360  Row r2(*row);
361 
362  for (unsigned int i=0; i<fCol.size(); ++i) {
363  if (fCol[i].Name() == "inserttime" ||
364  fCol[i].Name() == "insertuser" ||
365  fCol[i].Name() == "updatetime" ||
366  fCol[i].Name() == "updateuser" ) continue;
367  if (!fCol[i].CanBeNull())
368  if (r2.Col(i).IsNull())
369  fNullList.push_back(std::pair<int,int>(fRow.size(),i));
370  }
371 
372  fRow.push_back(r2);
373 
374  }
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 nutools::dbi::Table::AddRow ( const Row row)

Definition at line 386 of file Table.cpp.

References AddRow().

387  {
388  AddRow(&row);
389  }
void AddRow(const Row *row)
Definition: Table.cpp:356
void nutools::dbi::Table::CacheDBCommand ( std::string  cmd)

Definition at line 438 of file Table.cpp.

References fDBCacheFile.

Referenced by GetConnectionTimeout(), and WriteToDB().

439  {
440  std::ofstream fout;
441  fout.open(fDBCacheFile.c_str(),std::ios_base::app);
442 
443  fout << cmd << std::endl;
444 
445  fout.close();
446  }
std::string fDBCacheFile
Definition: Table.h:376
bool nutools::dbi::Table::CheckForNulls ( )
private

Definition at line 422 of file Table.cpp.

References fCol, fNullList, and fVerbosity.

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

423  {
424  bool isOk = fNullList.empty();
425 
426  if (!isOk) // print out list of null columns
427  for (unsigned int i=0; i<fNullList.size(); ++i)
428  if (fVerbosity>0)
429  std::cerr << fCol[fNullList[i].second].Name() << " is NULL in row "
430  << fNullList[i].first << std::endl;
431 
432  return isOk;
433 
434  }
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
short fVerbosity
Definition: Table.h:357
std::vector< std::pair< int, int > > fNullList
Definition: Table.h:395
void nutools::dbi::Table::Clear ( )
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, and GetCurrSeqVal().

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(), and RemoveRow().

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 nutools::dbi::Table::ClearValidity ( )

Definition at line 484 of file Table.cpp.

References fValidityChanged, fValidityEnd, fValiditySQL, and fValidityStart.

Referenced by Reset(), and WriteToCSV().

485  {
486  fValidityStart.clear();
487  fValidityEnd.clear();
488  fValiditySQL = "";
489  fValidityChanged=true;
490  }
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 nutools::dbi::Table::CloseConnection ( )

Definition at line 837 of file Table.cpp.

References fConnection, fHasConnection, and fVerbosity.

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

838  {
839  if (fConnection) {
840  PQfinish(fConnection);
841  if (fVerbosity > 0)
842  std::cout << "Closed connection" << std::endl;
843  }
844 
845  fConnection = 0;
846  fHasConnection = false;
847 
848  return true;
849  }
PGconn * fConnection
Definition: Table.h:401
bool fHasConnection
Definition: Table.h:351
short fVerbosity
Definition: Table.h:357
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

Definition at line 67 of file Table.h.

References fDataTypeMask, SetDataSource(), SetTableName(), and SetTableType().

67 { return fDataTypeMask; }
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 nutools::dbi::Table::ExecuteSQL ( std::string  cmd,
PGresult *&  res 
)

Definition at line 1030 of file Table.cpp.

References CloseConnection(), fConnection, fHasConnection, fIgnoreDB, fTimeQueries, fVerbosity, and GetConnection().

Referenced by GetCol().

1031  {
1032  if (fIgnoreDB) return false;
1033 
1034  bool hasConn = fHasConnection;
1035  if (! fHasConnection) {
1036  GetConnection();
1037  hasConn = false;
1038  }
1039 
1040  if (!fConnection) {
1041  std::cerr << "Table::ExecuteSQL: No connection to the database!" << std::endl;
1042  return false;
1043  }
1044 
1045  if (cmd == "") return false;
1046 
1047  if (fVerbosity)
1048  std::cerr << "Executing SQL query: " << cmd << std::endl;
1049 
1050  boost::posix_time::ptime ctt1;
1051  boost::posix_time::ptime ctt2;
1052 
1053  if (fTimeQueries) {
1054  ctt1 = boost::posix_time::microsec_clock::local_time();
1055  }
1056 
1057  res = PQexec(fConnection,cmd.c_str());
1058  if (fTimeQueries) {
1059  ctt2 = boost::posix_time::microsec_clock::local_time();
1060  boost::posix_time::time_duration tdiff = ctt2 - ctt1;
1061  std::cerr << "Table::ExecuteSQL(" << cmd << "): query took "
1062  << tdiff.total_milliseconds() << " ms" << std::endl;
1063  }
1064 
1065  // close connection to the dB if necessary
1066  if (! hasConn) CloseConnection();
1067 
1068  return (res != 0);
1069  }
bool GetConnection(int ntry=0)
Definition: Table.cpp:759
bool CloseConnection()
Definition: Table.cpp:837
PGconn * fConnection
Definition: Table.h:401
bool fHasConnection
Definition: Table.h:351
short fVerbosity
Definition: Table.h:357
bool nutools::dbi::Table::ExistsInDB ( )

Definition at line 893 of file Table.cpp.

References CloseConnection(), fConnection, fExistsInDB, fHasConnection, fIgnoreDB, fSchema, fTableType, fTestedExists, fVerbosity, GetConnection(), nutools::dbi::kConditionsTable, and Name().

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

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

Definition at line 1989 of file Table.cpp.

References util::begin(), nutools::dbi::Row::Channel(), util::empty(), fChannelVec, fChanRowMap, GetRow(), NRow(), and nutools::dbi::Row::VldTime().

Referenced by ClearChanRowMap().

1990  {
1991  nutools::dbi::Row* row;
1992  uint64_t chan;
1993  double tv;
1994  fChanRowMap.clear();
1995 
1996  for (int i=0; i<this->NRow(); ++i) {
1997  row = this->GetRow(i);
1998  chan = row->Channel();
1999  tv = row->VldTime();
2000  if (fChanRowMap[chan].empty())
2001  fChanRowMap[chan].push_back(row);
2002  else {
2003  bool wasInserted=false;
2004  for (unsigned int j=0; j<fChanRowMap[chan].size(); ++j) {
2005  if (tv < fChanRowMap[chan][j]->VldTime()) {
2006  fChanRowMap[chan].insert(fChanRowMap[chan].begin()+j,row);
2007  wasInserted=true;
2008  break;
2009  }
2010  }
2011  if (!wasInserted)
2012  fChanRowMap[chan].push_back(row);
2013  }
2014  }
2015 
2016  fChannelVec.clear();
2017  std::unordered_map<uint64_t,std::vector<nutools::dbi::Row*> >::iterator itr = fChanRowMap.begin();
2018  std::unordered_map<uint64_t,std::vector<nutools::dbi::Row*> >::iterator itrEnd = fChanRowMap.end();
2019 
2020  for (; itr != itrEnd; ++itr)
2021  fChannelVec.push_back(itr->first);
2022 
2023 
2024  }
uint64_t Channel()
Definition: Row.h:55
std::vector< uint64_t > fChannelVec
Definition: Table.h:398
nutools::dbi::Row *const GetRow(int i)
Definition: Table.cpp:413
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
double VldTime()
Definition: Row.h:56
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:109
std::unordered_map< uint64_t, std::vector< nutools::dbi::Row * > > fChanRowMap
Definition: Table.h:399
const nutools::dbi::ColumnDef* nutools::dbi::Table::GetCol ( int  i)
inline
const nutools::dbi::ColumnDef * nutools::dbi::Table::GetCol ( std::string &  cname)

Definition at line 577 of file Table.cpp.

References fCol, and Name().

578  {
579  unsigned int i=0;
580  for ( ; i < fCol.size(); ++i)
581  if (fCol[i].Name() == cname) break;
582 
583  if (i >= fCol.size()) return 0;
584 
585  return &fCol[i];
586  }
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 ExecuteSQL(), ExistsInDB(), GetCol(), LoadFromCSV(), SetTolerance(), and Tolerance().

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

Definition at line 589 of file Table.cpp.

References fCol, and Name().

Referenced by GetColName().

590  {
591  for (unsigned int i=0; i<fCol.size(); ++i)
592  if (fCol[i].Name() == cname) return (int)i;
593  std::cerr << "No such column \"" << cname << "\". Returning -1" << std::endl;
594  return -1;
595  }
std::string Name()
Definition: Table.h:59
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
std::string nutools::dbi::Table::GetColName ( int  i)
inline

Definition at line 130 of file Table.h.

References fCol, and GetColIndex().

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

Definition at line 608 of file Table.cpp.

References fCol.

Referenced by NewRow().

609  {
610  std::vector<std::string> nameList;
611 
612  for (unsigned int i=0; i<fCol.size(); ++i)
613  nameList.push_back(fCol[i].Name());
614 
615  return nameList;
616  }
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
std::map< std::string, int > nutools::dbi::Table::GetColNameToIndexMap ( )

Definition at line 598 of file Table.cpp.

References fCol.

Referenced by NewRow(), and WriteToDB().

599  {
600  std::map<std::string,int> tmap;
601  for (unsigned int i=0; i<fCol.size(); ++i) {
602  tmap[fCol[i].Name()] = int(i);
603  }
604  return tmap;
605  }
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
bool nutools::dbi::Table::GetColsFromDB ( std::vector< std::string >  pkeyList = {})

Definition at line 245 of file Table.cpp.

References addInsertTime, addInsertUser, addUpdateTime, addUpdateUser, CloseConnection(), fCol, fConnection, fExistsInDB, fHasConnection, fSchema, fTableName, fTableType, fVerbosity, GetConnection(), nutools::dbi::kConditionsTable, nutools::dbi::kUnstructuredConditionsTable, and nutools::dbi::ColumnDef::SetCanBeNull().

Referenced by main(), SetChannelRange(), and Table().

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

Definition at line 759 of file Table.cpp.

References CloseConnection(), e, fConnection, fConnectionTimeout, fDBHost, fDBName, fDBPort, fHasConnection, fIgnoreDB, fPassword, fUser, fVerbosity, GetConnectionInfo(), nutools::dbi::Util::RunningOnGrid(), SetPasswordFile(), and t1.

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

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

Definition at line 699 of file Table.cpp.

References fDBHost, fDBName, fDBPort, fIgnoreEnvVar, and fUser.

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

700  {
701  char* tmpStr;
702  char hname[256];
703 
704  if (!fIgnoreEnvVar) {
705  // now check environment variables, which will override any previous settings
706  if (ntry == 0) {
707  tmpStr = getenv("DBIHOST");
708  if (tmpStr)
709  fDBHost = tmpStr;
710  }
711  else {
712  sprintf(hname,"DBIHOST%d",ntry);
713  tmpStr = getenv(hname);
714  if (tmpStr) {
715  std::cerr << "Switching to " << tmpStr << std::endl;
716  fDBHost = tmpStr;
717  }
718  else
719  return false;
720  }
721 
722  tmpStr = getenv("DBINAME");
723  if (tmpStr)
724  fDBName = tmpStr;
725  tmpStr = getenv("DBIPORT");
726  if (tmpStr)
727  fDBPort = tmpStr;
728  tmpStr = getenv("DBIUSER");
729  if (tmpStr)
730  fUser = tmpStr;
731  }
732 
733  if (fUser == "") {
734  tmpStr = getenv("USER");
735  if (tmpStr) {
736  fUser = tmpStr;
737  std::cerr << "Table::GetConnectionInfo: DB User undefined. Setting to \""
738  << fUser << "\"" << std::endl;
739  }
740  else {
741  throw std::runtime_error("Table::GetConnectionInfo: DB USER undefined.");
742  }
743 
744  }
745  if (fDBHost == "") {
746  throw std::runtime_error("Table::GetConnectionInfo: DB HOST undefined.");
747  }
748  if (fDBName == "") {
749  throw std::runtime_error("Table::GetConnectionInfo: DB NAME undefined.");
750  }
751 
752  /* if (fDBPort == "")
753  if (fTable->hasDbPort())
754  fDBPort = fTable->getDbPort();
755  */
756  return true;
757  }
std::string fUser
Definition: Table.h:369
bool fIgnoreEnvVar
Definition: Table.h:345
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

Definition at line 264 of file Table.h.

References CacheDBCommand(), fConnectionTimeout, operator<<, and ResetConnectionInfo().

264 { return fConnectionTimeout; }
int fConnectionTimeout
Definition: Table.h:361
bool nutools::dbi::Table::GetCurrSeqVal ( std::string  col,
long &  iseq 
)

Definition at line 970 of file Table.cpp.

References CloseConnection(), fConnection, fHasConnection, fIgnoreDB, fVerbosity, GetConnection(), Name(), and Schema().

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

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

Definition at line 184 of file Table.h.

References GetCurrSeqVal().

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

Definition at line 1572 of file Table.cpp.

References AddEmptyRows(), fCol, fConnectionTimeout, fRow, fTimeParsing, fTimeQueries, fVerbosity, Name(), and t1.

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

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

Definition at line 515 of file Table.cpp.

References fDetector.

Referenced by SetValiditySQL().

516  {
517  if (fDetector == "") return false;
518  det = fDetector;
519 
520  return true;
521  }
std::string fDetector
Definition: Table.h:379
double nutools::dbi::Table::GetMaxTSVld ( ) const
inline

Definition at line 274 of file Table.h.

References fMaxTSVld.

274 {return fMaxTSVld; }
double fMaxTSVld
Definition: Table.h:405
double nutools::dbi::Table::GetMinTSVld ( ) const
inline

Definition at line 275 of file Table.h.

References fMinTSVld.

275 {return fMinTSVld; }
double fMinTSVld
Definition: Table.h:406
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 nutools::dbi::Table::GetPassword ( )
private

Definition at line 2606 of file Table.cpp.

References fin.

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

2607  {
2608  std::string pwd = "";
2609 
2610  char* pwdFile = getenv("DBIWSPWDFILE");
2611  if (pwdFile) {
2612  std::ifstream fin;
2613  fin.open(pwdFile);
2614  if (!fin.is_open() || !fin.good()) {
2615  std::cerr << "Could not open password file " << pwdFile
2616  << ". Canceling Table::Write()" << std::endl;
2617  return pwd;
2618  }
2619  else {
2620  fin >> pwd;
2621  fin.close();
2622  }
2623  }
2624 
2625  return pwd;
2626  }
TString fin
Definition: Style.C:24
const nutools::dbi::ColumnDef* nutools::dbi::Table::GetPKeyCol ( int  i)
inline

Definition at line 190 of file Table.h.

References fPKeyList, and PrintPQErrorMsg().

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

Definition at line 413 of file Table.cpp.

References fRow.

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

414  {
415  if (i >= 0 && i < (int)fRow.size())
416  return &fRow[i];
417  else
418  return 0;
419  }
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(), 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 * nutools::dbi::Table::GetVldRow ( uint64_t  channel,
double  t 
)

Definition at line 2035 of file Table.cpp.

References fChanRowMap.

Referenced by VldChannels().

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

Definition at line 2028 of file Table.cpp.

References fChanRowMap.

Referenced by VldChannels().

2029  {
2030  return fChanRowMap[channel];
2031  }
std::unordered_map< uint64_t, std::vector< nutools::dbi::Row * > > fChanRowMap
Definition: Table.h:399
bool nutools::dbi::Table::LoadConditionsTable ( )
private

Definition at line 1880 of file Table.cpp.

References fDataTypeMask, fDisableCache, fExcludeCol, fFlushCache, fHasRecordTime, fMaxChannel, fMaxTSVld, fMinChannel, fMinTSVld, fRecordTime, fTag, fValiditySQL, fWSURL, GetCol(), GetDataFromWebService(), nutools::dbi::kDataOnly, nutools::dbi::kMCOnly, nutools::dbi::kNone, nutools::dbi::ColumnDef::Name(), Name(), NCol(), nutools::dbi::Util::RunningOnGrid(), and Schema().

Referenced by Folder(), and Load().

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

Definition at line 1298 of file Table.cpp.

References AddEmptyRows(), fCol, fin, fRow, fTableType, nutools::dbi::kConditionsTable, Name(), NewRow(), r, and value.

Referenced by GetCol(), LoadFromCSV(), and main().

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

Definition at line 145 of file Table.h.

References LoadFromCSV(), LoadFromDB(), WriteToCSV(), and WriteToDB().

146  { return LoadFromCSV(std::string(fname)); }
bool LoadFromCSV(std::string fname)
Definition: Table.cpp:1298
bool nutools::dbi::Table::LoadFromDB ( )

Definition at line 1072 of file Table.cpp.

References AddEmptyRows(), CloseConnection(), ExistsInDB(), fCol, fConnection, fDescOrder, fDistinctCol, fHasConnection, fIgnoreDB, fOrderCol, fRow, fSchema, fSelectLimit, fSelectOffset, fTimeParsing, fTimeQueries, fValidityChanged, fValidityEnd, fValiditySQL, fValidityStart, fVerbosity, GetConnection(), Name(), and Schema().

Referenced by LoadFromCSV(), and LoadNonConditionsTable().

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

Definition at line 1758 of file Table.cpp.

References fDescOrder, fDisableCache, fExcludeCol, fFlushCache, fOrderCol, fQEURL, fSelectLimit, fValidityEnd, fValiditySQL, fValidityStart, GetCol(), GetDataFromWebService(), LoadFromDB(), nutools::dbi::ColumnDef::Name(), Name(), NCol(), and Schema().

Referenced by Folder(), and Load().

1759  {
1760  if (fQEURL == "") {
1761  std::cerr << "Table::LoadNonConditionsTable: Query Engine URL is not set! using Table::LoadFromDB() instead." << std::endl;
1762  return LoadFromDB();
1763  }
1764 
1765  if (fValiditySQL != "") {
1766  std::cerr << "Table::LoadNonConditionsTable: pure SQL statements are not supported, using Table::LoadFromDB() instead." << std::endl;
1767  return LoadFromDB();
1768  }
1769 
1770  std::stringstream myss;
1771 
1772  myss << fQEURL << "query?t=" << Schema() << "." << Name() << "&";
1773 
1774  int ncol = this->NCol();
1775 
1776  myss << "&c=";
1777  int nc=0;
1778  for (int i=0; i<ncol; ++i) {
1779  std::string cname = this->GetCol(i)->Name();
1780  bool skipCol = false;
1781  for (size_t j=0; j<fExcludeCol.size(); ++j) {
1782  if (fExcludeCol[j] == cname) {
1783  skipCol = true;
1784  break;
1785  }
1786  }
1787  if (skipCol) continue;
1788 
1789  if (nc>0)
1790  myss << ",";
1791  myss << cname;
1792  ++nc;
1793  }
1794 
1795  if (! fValidityStart.empty()) {
1796  for (unsigned int i=0; i<fValidityStart.size(); ++i) {
1797  if (fValidityStart[i].Type() == "string" ||
1798  fValidityStart[i].Type() == "text" ||
1799  fValidityStart[i].Type() == "timestamp" ||
1800  fValidityStart[i].Type() == "date") {
1801  std::cerr << "Table::LoadNonConditionsTable: validity strings are not supported, using Table::LoadFromDB() instead." << std::endl;
1802  return LoadFromDB();
1803  }
1804 
1805  myss << "&w=";
1806  bool isEqualTo = (fValidityStart[i].Value() == fValidityEnd[i].Value());
1807  if (isEqualTo) {
1808  myss << fValidityStart[i].Name() << ":"
1809  << fValidityStart[i].Value();
1810  }
1811  else {
1812  myss << fValidityStart[i].Name() << ":ge:"
1813  << fValidityStart[i].Value() << "&w="
1814  << fValidityEnd[i].Name() << ":le:"
1815  << fValidityEnd[i].Value();
1816  }
1817 
1818  }
1819  }
1820 
1821  if (!fOrderCol.empty()) {
1822  myss << "&o=";
1823  if (fDescOrder)
1824  myss << "-";
1825  for (unsigned int i=0; i<fOrderCol.size(); ++i) {
1826  myss << fOrderCol[i]->Name();
1827  if (i<(fOrderCol.size()-1)) myss << ", ";
1828  }
1829 
1830  }
1831 
1832  if (fSelectLimit>0)
1833  myss << "&l=" << fSelectLimit;
1834 
1835  if (fDisableCache) {
1836  if (fFlushCache)
1837  myss << "&x=clear";
1838  else
1839  myss << "&x=no";
1840  }
1841 
1842  Dataset ds;
1843 
1844  return GetDataFromWebService(ds,myss.str());
1845 
1846  }
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
std::string fValiditySQL
Definition: Table.h:378
std::string Name() const
Definition: ColumnDef.h:21
void * Dataset
Definition: DBFolder.h:12
std::vector< nutools::dbi::ColumnDef > fValidityEnd
Definition: Table.h:391
bool GetDataFromWebService(Dataset &, std::string)
Definition: Table.cpp:1572
bool nutools::dbi::Table::LoadUnstructuredConditionsTable ( )
private

Definition at line 1850 of file Table.cpp.

References fMaxTSVld, fMinTSVld, Folder(), fUConDBURL, Name(), and nutools::dbi::Util::RunningOnGrid().

Referenced by Folder(), and Load().

1851  {
1852  if (fMinTSVld == 0 || fMaxTSVld == 0) {
1853  std::cerr << "Table::LoadUnstructuredConditionsTable: No validity time is set!" << std::endl;
1854  return false;
1855  }
1856 
1857  if (fUConDBURL == "") {
1858  std::cerr << "Table::LoadConditionsTable: Web Service URL is not set!" << std::endl;
1859  return false;
1860  }
1861 
1862  if (!Util::RunningOnGrid()) {
1863  std::string interactiveURL = getenv("DBIUCONDBURLINT");
1864  if (!interactiveURL.empty())
1865  fUConDBURL = interactiveURL;
1866  }
1867 
1868  // int ncol = this->NCol();
1869 
1870  std::stringstream myss;
1871 
1872  myss << fUConDBURL << "get?folder=" << Folder() << "." << Name() << "&";
1873 
1874 
1875  return false;
1876  }
std::string Name()
Definition: Table.h:59
static bool RunningOnGrid()
Definition: Util.cpp:132
double fMinTSVld
Definition: Table.h:406
std::string Folder()
Definition: Table.h:321
double fMaxTSVld
Definition: Table.h:405
std::string fUConDBURL
Definition: Table.h:384
bool nutools::dbi::Table::MakeConditionsCSVString ( std::stringstream &  ss)
private

Definition at line 2305 of file Table.cpp.

References nutools::dbi::Row::Channel(), nutools::dbi::Row::Col(), GetCol(), GetRow(), nutools::dbi::ColumnDef::Name(), NCol(), NRow(), Tolerance(), nutools::dbi::ColumnDef::Type(), nutools::dbi::Row::VldTime(), and nutools::dbi::Row::VldTimeEnd().

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

2306  {
2307  int ncol = this->NCol();
2308  int nrow = this->NRow();
2309 
2310  ss << "channel,tv,";
2311  bool first = true;
2312  for (int i=0; i<ncol; ++i) {
2313  std::string cname = this->GetCol(i)->Name();
2314  if(!first) ss << ",";
2315  first = false;
2316  ss << cname;
2317  }
2318  ss << std::endl;
2319 
2320  ss << "tolerance,,";
2321  first = true;
2322  for (int j=0; j<ncol; ++j) {
2323  std::string cname = this->GetCol(j)->Name();
2324  std::string ctype = this->GetCol(j)->Type();
2325  if(!first) ss << ",";
2326  first = false;
2327  float tol = this->Tolerance(cname);
2328  if (tol == 0.) {
2329  if (ctype == "double")
2330  ss << "1.e-10";
2331  else if (ctype == "float")
2332  ss << "1.e-5";
2333  }
2334  else
2335  ss << this->Tolerance(cname);
2336  }
2337  ss << std::endl;
2338  for (int i=0; i<nrow; ++i) {
2339  ss << GetRow(i)->Channel() << ","
2340  << GetRow(i)->VldTime() << ",";
2341  if (GetRow(i)->VldTimeEnd() > GetRow(i)->VldTime())
2342  ss << GetRow(i)->VldTimeEnd() << ",";
2343  first = true;
2344  for (int j=0; j<ncol; ++j) {
2345  if(!first) ss << ",";
2346  first = false;
2347  ss << GetRow(i)->Col(j);
2348  }
2349  ss << std::endl;
2350  }
2351 
2352  return true;
2353  }
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
uint64_t Channel()
Definition: Row.h:55
double VldTimeEnd()
Definition: Row.h:57
float Tolerance(std::string &cname)
Definition: Table.cpp:632
std::string Type() const
Definition: ColumnDef.h:22
std::string Name() const
Definition: ColumnDef.h:21
nutools::dbi::Row *const GetRow(int i)
Definition: Table.cpp:413
double VldTime()
Definition: Row.h:56
Column & Col(int i)
Definition: Row.h:53
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;}
TRandom r
Definition: spectrum.C:23
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
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 nutools::dbi::Table::ParseSelfStatusLine ( char *  line)
private

Definition at line 1522 of file Table.cpp.

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

1523  {
1524  int i = strlen(line);
1525  while (*line < '0' || *line > '9') line++;
1526  line[i-3] = '\0';
1527  i = atoi(line);
1528  return i;
1529  }
void nutools::dbi::Table::PrintColumns ( )

Definition at line 645 of file Table.cpp.

References GetCol(), art::left(), nutools::dbi::ColumnDef::Name(), NCol(), and nutools::dbi::ColumnDef::Type().

Referenced by RemoveValidityRange().

646  {
647  std::cout << std::endl;
648  int i = 0;
649  std::vector<int> len(0);
650  int tlen;
651  //int sumlen = 0; // unused
652 
653  for ( ; i<this->NCol(); ++i) {
654  tlen = this->GetCol(i)->Name().length();
655  if ((int)this->GetCol(i)->Type().length() > tlen)
656  tlen = this->GetCol(i)->Type().length();
657  len.push_back(tlen);
658  //sumlen += tlen; // unused
659  }
660 
661  int nsp = 0;
662  i = 0;
663  int j = 0;
664  while (i < this->NCol()) {
665  for ( ; i<this->NCol() && nsp<78; ++i)
666  nsp += len[i] + 1;
667 
668  for (int k=0; k<nsp; ++k) {
669  std::cout << "_" ;
670  }
671  std::cout << std::endl;
672 
673  int j_save = j;
674  for (; j<i; ++j)
675  std::cout << "|" << std::setw(len[j]) << std::left << this->GetCol(j)->Name();
676  std::cout << "|" << std::endl;
677 
678  for (int k=0; k<nsp; ++k) {
679  std::cout << "-" ;
680  }
681  std::cout << std::endl;
682 
683  j = j_save;
684  for ( ; j<i; ++j)
685  std::cout << "|" << std::setw(len[j]) << std::left << this->GetCol(j)->Type();
686  std::cout << "|" << std::endl;
687 
688  for (int k=0; k<nsp; ++k) {
689  std::cout << "-" ;
690  }
691  std::cout << std::endl;
692 
693  nsp = 0;
694  }
695 
696  }
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
std::string Type() const
Definition: ColumnDef.h:22
std::string Name() const
Definition: ColumnDef.h:21
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:94
void nutools::dbi::Table::PrintPMUsed ( )

Definition at line 1553 of file Table.cpp.

References fclose(), file, Name(), ParseSelfStatusLine(), and Schema().

Referenced by SetChannelRange().

1553  { //Note: this value is in MB!
1554  FILE* file = fopen("/proc/self/status", "r");
1555  int result = -1;
1556  char line[128];
1557 
1558 
1559  while (fgets(line, 128, file) != NULL){
1560  if (strncmp(line, "VmRSS:", 6) == 0){
1561  result = this->ParseSelfStatusLine(line);
1562  break;
1563  }
1564  }
1565  fclose(file);
1566  std::cerr << Schema() << "." << Name() << ": this process using "
1567  << result/1024 << " MB of PhysicalMemory" << std::endl;
1568  }
std::string Name()
Definition: Table.h:59
std::string Schema()
Definition: Table.h:215
fclose(fg1)
TFile * file
int ParseSelfStatusLine(char *line)
Definition: Table.cpp:1522
void nutools::dbi::Table::PrintPQErrorMsg ( ) const

Definition at line 493 of file Table.cpp.

References fConnection.

Referenced by GetPKeyCol().

494  {
495  if (fConnection)
496  std::cerr << PQerrorMessage(fConnection) << std::endl;
497  }
PGconn * fConnection
Definition: Table.h:401
void nutools::dbi::Table::PrintVMUsed ( )

Definition at line 1534 of file Table.cpp.

References fclose(), file, Name(), ParseSelfStatusLine(), and Schema().

Referenced by SetChannelRange().

1534  { //Note: this value is in MB!
1535  FILE* file = fopen("/proc/self/status", "r");
1536  int result = -1;
1537  char line[128];
1538 
1539 
1540  while (fgets(line, 128, file) != NULL){
1541  if (strncmp(line, "VmSize:", 7) == 0){
1542  result = this->ParseSelfStatusLine(line);
1543  break;
1544  }
1545  }
1546  fclose(file);
1547  std::cerr << Schema() << "." << Name() << ": this process using "
1548  << result/1024 << " MB of VirtualMemory" << std::endl;
1549  }
std::string Name()
Definition: Table.h:59
std::string Schema()
Definition: Table.h:215
fclose(fg1)
TFile * file
int ParseSelfStatusLine(char *line)
Definition: Table.cpp:1522
bool nutools::dbi::Table::RemoveDistinctColumn ( unsigned int  i)

Definition at line 2482 of file Table.cpp.

References fCol, and fDistinctCol.

Referenced by AddDistinctColumn(), and RemoveDistinctColumn().

2483  {
2484  if (i >= fCol.size()) return false;
2485 
2486  const ColumnDef* c = &fCol[i];
2487  for (unsigned int j=0; j<fDistinctCol.size(); ++j)
2488  if (fDistinctCol[j] == c) {
2489  fDistinctCol.erase(fDistinctCol.begin() + j);
2490  return true;
2491  }
2492 
2493  return false;
2494  }
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
std::vector< const nutools::dbi::ColumnDef * > fDistinctCol
Definition: Table.h:393
bool nutools::dbi::Table::RemoveDistinctColumn ( std::string  col)

Definition at line 2518 of file Table.cpp.

References fDistinctCol, and GetCol().

2519  {
2520  const ColumnDef* c = GetCol(cname);
2521 
2522  if (!c) return false;
2523 
2524  for (unsigned int j=0; j<fDistinctCol.size(); ++j)
2525  if (fDistinctCol[j] == c) {
2526  fDistinctCol.erase(fDistinctCol.begin() + j);
2527  return true;
2528  }
2529 
2530  return false;
2531  }
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
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(), and RemoveDistinctColumn().

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

Definition at line 2554 of file Table.cpp.

References fCol, and fOrderCol.

Referenced by AddOrderColumn(), and RemoveOrderColumn().

2555  {
2556  if (i >= fCol.size()) return false;
2557 
2558  const ColumnDef* c = &fCol[i];
2559  for (unsigned int j=0; j<fOrderCol.size(); ++j)
2560  if (fOrderCol[j] == c) {
2561  fOrderCol.erase(fOrderCol.begin() + j);
2562  return true;
2563  }
2564 
2565  return false;
2566  }
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
std::vector< const nutools::dbi::ColumnDef * > fOrderCol
Definition: Table.h:394
bool nutools::dbi::Table::RemoveOrderColumn ( std::string  col)

Definition at line 2590 of file Table.cpp.

References fOrderCol, and GetCol().

2591  {
2592  const ColumnDef* c = GetCol(cname);
2593 
2594  if (!c) return false;
2595 
2596  for (unsigned int j=0; j<fOrderCol.size(); ++j)
2597  if (fOrderCol[j] == c) {
2598  fOrderCol.erase(fOrderCol.begin() + j);
2599  return true;
2600  }
2601 
2602  return false;
2603  }
std::vector< const nutools::dbi::ColumnDef * > fOrderCol
Definition: Table.h:394
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
bool nutools::dbi::Table::RemoveOrderColumn ( const char *  col)
inline

Definition at line 171 of file Table.h.

References RemoveOrderColumn().

172  { return RemoveOrderColumn(std::string(col)); }
bool RemoveOrderColumn(unsigned int i)
Definition: Table.cpp:2554
Int_t col[ntarg]
Definition: Style.C:29
bool nutools::dbi::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 392 of file Table.cpp.

References fNullList, and fRow.

Referenced by ClearRows().

393  {
394  if (i < 0) return false;
395 
396  unsigned int j = i;
397 
398  if (j >= fRow.size()) return false;
399 
400  unsigned int kEnd = fNullList.size();
401  for (unsigned int k=0; k<kEnd; ++k)
402  if (fNullList[k].first == i) {
403  fNullList.erase(fNullList.begin()+k);
404  kEnd = fNullList.size();
405  }
406 
407  fRow.erase(fRow.begin()+j);
408 
409  return true;
410  }
std::vector< std::pair< int, int > > fNullList
Definition: Table.h:395
std::vector< nutools::dbi::Row > fRow
Definition: Table.h:388
void nutools::dbi::Table::RemoveValidityRange ( std::string &  cname)

Definition at line 2451 of file Table.cpp.

References fValidityEnd, fValidityStart, and Name().

Referenced by RemoveValidityRange(), and SetValidityRange().

2452  {
2453  unsigned int i=0;
2454  for (; i<fValidityStart.size(); ++i)
2455  if (fValidityStart[i].Name() == cname) {
2456  fValidityStart.erase(fValidityStart.begin()+i);
2457  fValidityEnd.erase(fValidityEnd.begin()+i);
2458  }
2459  }
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(), and RemoveValidityRange().

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

Definition at line 467 of file Table.cpp.

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

Referenced by Folder(), and Table().

468  {
469  fConnection = 0;
470  fHasConnection = 0;
471  fPKeyList.clear();
472  fDistinctCol.clear();
473  fVerbosity = 0;
474  fDescOrder = true;
475  fSelectLimit = 0;
476  fSelectOffset = 0;
477  ClearValidity();
478  fMinChannel = 0;
479  fMaxChannel = 0;
480  fExcludeCol.clear();
481  }
uint64_t fMaxChannel
Definition: Table.h:366
void ClearValidity()
Definition: Table.cpp:484
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
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 n.

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

Definition at line 537 of file Table.cpp.

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

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

537  {
538  if (ds == std::string("DAQ"))
540  else if (ds == std::string("DCS"))
542  else if (ds == std::string("Offline"))
544  else
546  }
void SetDataSource(std::string ds)
Definition: Table.cpp:537
void nutools::dbi::Table::SetDataSource ( int  ids)

Definition at line 549 of file Table.cpp.

References fDataSource, nutools::dbi::kNDataSources, and nutools::dbi::kUnknownSource.

549  {
550  if (ids >= 0 && ids < nutools::dbi::kNDataSources)
551  fDataSource = ids;
552  else
554  }
void nutools::dbi::Table::SetDataTypeMask ( int  mask)
inline

Definition at line 76 of file Table.h.

References fDataTypeMask.

Referenced by main().

76 { fDataTypeMask = 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 nutools::dbi::Table::SetDBInfo ( std::string  name,
std::string  host,
std::string  port,
std::string  user 
)

Definition at line 557 of file Table.cpp.

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

Referenced by SetDBPort().

559  {
560  SetDBName(name);
561  SetDBHost(host);
562  SetDBPort(port);
563  SetUser(user);
564  }
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
void nutools::dbi::Table::SetDBInfo ( const char *  name,
const char *  host,
const char *  port,
const char *  user 
)

Definition at line 567 of file Table.cpp.

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

569  {
570  SetDBName(name);
571  SetDBHost(host);
572  SetDBPort(port);
573  SetUser(user);
574  }
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
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; }
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, SetDBInfo(), and SetPasswordFile().

88 { fDBPort = p; }
std::string fDBPort
Definition: Table.h:371
bool nutools::dbi::Table::SetDetector ( std::string  det)

Definition at line 500 of file Table.cpp.

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

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

501  {
502  fDetector = det;
503 
504  if (fTableType != kHardwareTable)
505  fSchema = det;
506  else
507  fSchema = "public";
508 
509  boost::to_lower(fSchema);
510 
511  return true;
512  }
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 f, and fFolder.

320 { fFolder = f; }
std::string fFolder
Definition: Table.h:380
TFile f
Definition: plotHisto.C:6
void nutools::dbi::Table::SetIgnoreEnvVar ( bool  f)
inline

Definition at line 78 of file Table.h.

References f, and fIgnoreEnvVar.

78 { fIgnoreEnvVar = f;}
bool fIgnoreEnvVar
Definition: Table.h:345
TFile f
Definition: plotHisto.C:6
void nutools::dbi::Table::SetMaxChannel ( uint64_t  chan)
inline

Definition at line 311 of file Table.h.

References fMaxChannel.

311 {fMaxChannel = chan;}
uint64_t fMaxChannel
Definition: Table.h:366
void nutools::dbi::Table::SetMaxTSVld ( double  t)
inline

Definition at line 272 of file Table.h.

References fMaxTSVld.

Referenced by main().

272 { fMaxTSVld = t;}
double fMaxTSVld
Definition: Table.h:405
void nutools::dbi::Table::SetMinChannel ( uint64_t  chan)
inline

Definition at line 310 of file Table.h.

References fMinChannel.

310 {fMinChannel = chan;}
uint64_t fMinChannel
Definition: Table.h:365
void nutools::dbi::Table::SetMinTSVld ( double  t)
inline

Definition at line 271 of file Table.h.

References fMinTSVld.

Referenced by main().

271 { fMinTSVld = t;}
double fMinTSVld
Definition: Table.h:406
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 nutools::dbi::Table::SetPasswordFile ( const char *  fname = 0)

fname should be the name of the file

Definition at line 859 of file Table.cpp.

References fin, and fPassword.

Referenced by GetConnection(), and SetDBPort().

860  {
861  std::string fNameStr = "";
862 
863  if (fname == 0) {
864  char* tmpStr = getenv("DBIPWDFILE");
865  if (tmpStr)
866  fNameStr = tmpStr;
867  else {
868  std::cerr << "DBIPWDFILE env. variable is not set, disabling "
869  << "password-access to the dB." << std::endl;
870  fPassword = "";
871  return false;
872  }
873  }
874  else
875  fNameStr = fname;
876 
877  std::ifstream fin;
878  fin.open(fNameStr.c_str());
879  if (!fin.is_open() || !fin.good()) {
880  std::cerr << "Could not open password file " << fNameStr
881  << ". Disabling password-access to the dB." << std::endl;
882  return false;
883  }
884  else {
885  fin >> fPassword;
886  fin.close();
887  }
888 
889  return true;
890  }
TString fin
Definition: Style.C:24
std::string fPassword
Definition: Table.h:377
void nutools::dbi::Table::SetQEURL ( std::string  url)
inline

Definition at line 303 of file Table.h.

References fQEURL.

Referenced by nutools::dbi::DBIService::CreateTable().

303 { fQEURL = url;}
std::string fQEURL
Definition: Table.h:385
void nutools::dbi::Table::SetRecordTime ( double  t)

Definition at line 450 of file Table.cpp.

References fHasRecordTime, and fRecordTime.

Referenced by VldChannels().

451  {
452  fRecordTime = t;
453  fHasRecordTime = true;
454  }
bool fHasRecordTime
Definition: Table.h:352
double fRecordTime
Definition: Table.h:407
bool nutools::dbi::Table::SetRole ( std::string  role)

Definition at line 852 of file Table.cpp.

References fRole.

Referenced by SetUser().

853  {
854  fRole = role;
855  return true;
856  }
std::string fRole
Definition: Table.h:370
bool nutools::dbi::Table::SetRole ( const char *  role)
void nutools::dbi::Table::SetSchema ( std::string  s)
inline

Definition at line 214 of file Table.h.

References fSchema.

214 { fSchema = s; }
std::string fSchema
Definition: Table.h:374
void nutools::dbi::Table::SetSelectLimit ( int  limit)
inline

Definition at line 174 of file Table.h.

References fSelectLimit.

174 { fSelectLimit=limit; }
void nutools::dbi::Table::SetSelectOffset ( int  offset)
inline

Definition at line 175 of file Table.h.

References fSelectOffset.

175 { fSelectOffset=offset; }
void nutools::dbi::Table::SetTableName ( std::string  tname)

Definition at line 524 of file Table.cpp.

References fTableName.

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

524  {
525  boost::to_lower(tname);
526  fTableName = tname;
527  }
std::string fTableName
Definition: Table.h:368
void nutools::dbi::Table::SetTableName ( const char *  tname)

Definition at line 530 of file Table.cpp.

References fTableName.

530  {
531  std::string tnameStr = tname;
532  boost::to_lower(tnameStr);
533  fTableName = tnameStr;
534  }
std::string fTableName
Definition: Table.h:368
bool nutools::dbi::Table::SetTableType ( int  t)

Definition at line 458 of file Table.cpp.

References fTableType, and nutools::dbi::kNTableType.

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

459  {
460  if (t < 0 || t >= kNTableType) return false;
461  fTableType = t;
462 
463  return true;
464  }
void nutools::dbi::Table::SetTag ( std::string  s)
inline

Definition at line 277 of file Table.h.

References fTag.

277 { fTag = s; }
std::string fTag
Definition: Table.h:382
void nutools::dbi::Table::SetTimeParsing ( bool  f)
inline

Definition at line 306 of file Table.h.

References f, and fTimeParsing.

Referenced by nutools::dbi::DBIService::CreateTable().

306 {fTimeParsing = f; }
TFile f
Definition: plotHisto.C:6
void nutools::dbi::Table::SetTimeQueries ( bool  f)
inline

Definition at line 305 of file Table.h.

References f, and fTimeQueries.

Referenced by nutools::dbi::DBIService::CreateTable().

305 {fTimeQueries = f; }
TFile f
Definition: plotHisto.C:6
void nutools::dbi::Table::SetTolerance ( std::string &  cname,
float  t 
)

Definition at line 619 of file Table.cpp.

References fCol, and Name().

Referenced by GetCol().

620  {
621  unsigned int i=0;
622  for ( ; i < fCol.size(); ++i)
623  if (fCol[i].Name() == cname) break;
624 
625  if (i >= fCol.size()) return;
626 
627  fCol[i].SetTolerance(t);
628 
629  }
std::string Name()
Definition: Table.h:59
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
void nutools::dbi::Table::SetUser ( std::string  uname)
inline

Definition at line 79 of file Table.h.

References fUser.

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

79 { fUser = uname; }
std::string fUser
Definition: Table.h:369
void nutools::dbi::Table::SetUser ( const char *  uname)
inline

Definition at line 80 of file Table.h.

References fUser, and SetRole().

80 { fUser = uname; }
std::string fUser
Definition: Table.h:369
template<class T >
bool nutools::dbi::Table::SetValidityRange ( std::string  cname,
start,
end 
)
inline

Definition at line 218 of file Table.h.

References util::end(), fValidityChanged, fValidityEnd, fValidityStart, GetCol(), nutools::dbi::ColumnDef::Name(), Name(), and nutools::dbi::ColumnDef::Type().

Referenced by SetValidityRange().

219  {
220  const ColumnDef* c = this->GetCol(cname);
221  if (c) {
222  // check to see if this makes sense for a boolean column.
223  if (c->Type() == "bool")
224  if (start != end) return false;
225 
226  // check to see that this column wasn't already added to valid.
227  // list if it is, overwrite it
228 
229  unsigned int i=0;
230  for (; i<fValidityStart.size(); ++i)
231  if (fValidityStart[i].Name() == c->Name()) break;
232 
233  if (i == fValidityStart.size()) {
234  fValidityStart.push_back(ColumnDef(*c));
235  fValidityEnd.push_back(ColumnDef(*c));
236  }
237  std::stringstream startSS;
238  startSS << start;
239  std::stringstream endSS;
240  endSS << end;
241  fValidityStart[i].SetValue(startSS.str());
242  fValidityEnd[i].SetValue(endSS.str());
243  fValidityChanged=true;
244  return true;
245  }
246 
247  return false;
248  }
std::string Name()
Definition: Table.h:59
const nutools::dbi::ColumnDef * GetCol(int i)
Definition: Table.h:133
std::vector< nutools::dbi::ColumnDef > fValidityStart
Definition: Table.h:390
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
bool fValidityChanged
Definition: Table.h:346
std::vector< nutools::dbi::ColumnDef > fValidityEnd
Definition: Table.h:391
template<class T >
bool nutools::dbi::Table::SetValidityRange ( std::string  cname,
start 
)
inline

Definition at line 251 of file Table.h.

References RemoveValidityRange(), and SetValidityRange().

251  {
252  return (this->SetValidityRange(cname,start,start));
253  }
bool SetValidityRange(std::string cname, T start, T end)
Definition: Table.h:218
void nutools::dbi::Table::SetValiditySQL ( std::string  cmd)
inline

Definition at line 209 of file Table.h.

References fValidityChanged, fValiditySQL, GetDetector(), and SetDetector().

209 { fValiditySQL = cmd; fValidityChanged=true;}
std::string fValiditySQL
Definition: Table.h:378
bool fValidityChanged
Definition: Table.h:346
void nutools::dbi::Table::SetVerbosity ( int  i)
inline

Definition at line 100 of file Table.h.

References fVerbosity.

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

100 { fVerbosity = i;}
short fVerbosity
Definition: Table.h:357
void nutools::dbi::Table::SetWSURL ( std::string  url)
inline

Definition at line 302 of file Table.h.

References fWSURL.

Referenced by nutools::dbi::DBIService::CreateTable().

302 { fWSURL = url;}
std::string fWSURL
Definition: Table.h:383
int nutools::dbi::Table::TableType ( )
inline

Definition at line 65 of file Table.h.

References fTableType.

65 { return fTableType; }
bool nutools::dbi::Table::Tag ( std::string  tn = "",
bool  override = false 
)

Definition at line 2053 of file Table.cpp.

References fTag, fWSURL, GetPassword(), Name(), and Schema().

Referenced by GetTag(), and main().

2054  {
2055  if (tn != "") fTag = tn;
2056 
2057  if (fTag == "") return false;
2058 
2059  std::stringstream myss;
2060 
2061  myss << fWSURL << "tag?table=" << Schema() << "." << Name() << "&";
2062  myss << "tag=" << fTag;
2063  if (override)
2064  myss << "&override=yes";
2065 
2066  int status;
2067  std::string pwd = GetPassword();
2068 
2069  postHTTPsigned(myss.str().c_str(), pwd.c_str(), NULL, 0, NULL, 0, &status);
2070 
2071  return (status==0);
2072  }
std::string Name()
Definition: Table.h:59
std::string Schema()
Definition: Table.h:215
std::string fWSURL
Definition: Table.h:383
std::string fTag
Definition: Table.h:382
std::string GetPassword()
Definition: Table.cpp:2606
bool nutools::dbi::Table::TimeParsing ( )
inline

Definition at line 308 of file Table.h.

References fTimeParsing.

308 {return fTimeParsing; }
bool nutools::dbi::Table::TimeQueries ( )
inline

Definition at line 307 of file Table.h.

References fTimeQueries.

307 {return fTimeQueries; }
float nutools::dbi::Table::Tolerance ( std::string &  cname)

Definition at line 632 of file Table.cpp.

References fCol, and Name().

Referenced by GetCol(), and MakeConditionsCSVString().

633  {
634  unsigned int i=0;
635  for ( ; i < fCol.size(); ++i)
636  if (fCol[i].Name() == cname) break;
637 
638  if (i >= fCol.size()) return 0.;
639 
640  return fCol[i].Tolerance();
641 
642  }
std::string Name()
Definition: Table.h:59
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
std::string nutools::dbi::Table::User ( )
inline

Definition at line 62 of file Table.h.

References fUser.

62 { return fUser; }
std::string fUser
Definition: Table.h:369
std::vector<uint64_t> nutools::dbi::Table::VldChannels ( )
inline

Definition at line 288 of file Table.h.

References fChannelVec, GetVldRow(), GetVldRows(), and SetRecordTime().

288 { return fChannelVec; }
std::vector< uint64_t > fChannelVec
Definition: Table.h:398
bool nutools::dbi::Table::Write ( bool  commit = true)

Definition at line 2356 of file Table.cpp.

References fDataTypeMask, fTimeQueries, fVerbosity, fWSURL, GetPassword(), nutools::dbi::kDataOnly, nutools::dbi::kMCOnly, MakeConditionsCSVString(), Name(), nutools::dbi::Util::RunningOnGrid(), and Schema().

Referenced by GetTag(), and main().

2357  {
2358  if (fDataTypeMask == 0){
2359  std::cerr << "Table::Write: Data type mask is not set!" << std::endl;
2360  return false;
2361  }
2362 
2363  if (fWSURL == "") {
2364  std::cerr << "Table::Write: Web Service URL is not set!" << std::endl;
2365  return false;
2366  }
2367 
2368  if (!Util::RunningOnGrid()) {
2369  std::string putURL = getenv("DBIWSURLPUT");
2370  if (!putURL.empty())
2371  fWSURL = putURL;
2372  }
2373 
2374  std::stringstream ss;
2375 
2377 
2378  int status;
2379  std::string url = fWSURL + "put?table=" + Schema() + "." + Name();
2380 
2381  std::stringstream typeStr;
2382  typeStr << "&type=";
2383  if ((fDataTypeMask & kMCOnly)) typeStr << "mc";
2384  if ((fDataTypeMask & kDataOnly)) typeStr << "data";
2385 
2386  url += typeStr.str();
2387 
2388  // get web service password
2389  std::string pwd = GetPassword();
2390 
2391  boost::posix_time::ptime ctt1;
2392  boost::posix_time::ptime ctt2;
2393 
2394  if (fTimeQueries)
2395  ctt1 = boost::posix_time::microsec_clock::local_time();
2396 
2397  if (fVerbosity>0)
2398  std::cout << "Posting data to: " << url << std::endl;
2399 
2400  postHTTPsigned(url.c_str(), pwd.c_str(), NULL, 0,
2401  ss.str().c_str(), ss.str().length(), &status);
2402  if (fTimeQueries) {
2403  ctt2 = boost::posix_time::microsec_clock::local_time();
2404  boost::posix_time::time_duration tdiff = ctt2 - ctt1;
2405  std::cerr << "Table::Write(" << Name() << "): query took "
2406  << tdiff.total_milliseconds() << " ms" << std::endl;
2407  }
2408  return (status == 0);
2409  }
std::string Name()
Definition: Table.h:59
bool MakeConditionsCSVString(std::stringstream &ss)
Definition: Table.cpp:2305
static bool RunningOnGrid()
Definition: Util.cpp:132
std::string Schema()
Definition: Table.h:215
short fVerbosity
Definition: Table.h:357
std::string fWSURL
Definition: Table.h:383
std::string GetPassword()
Definition: Table.cpp:2606
bool nutools::dbi::Table::WriteToCSV ( std::string  fname,
bool  appendToFile = false,
bool  writeColNames = false 
)

Definition at line 2412 of file Table.cpp.

References CheckForNulls(), fCol, fRow, fTableType, nutools::dbi::kConditionsTable, and MakeConditionsCSVString().

Referenced by LoadFromCSV(), main(), and WriteToCSV().

2414  {
2415  if (! CheckForNulls()) return false;
2416 
2417  std::ofstream fout;
2418  if (!appendToFile)
2419  fout.open(fname.c_str());
2420  else
2421  fout.open(fname.c_str(),std::ios_base::app);
2422 
2423  if (fTableType==kConditionsTable) {
2424  std::stringstream ss;
2426  fout << ss.str();
2427  }
2428  else {
2429  if (writeColNames) {
2430  for (unsigned int j=0; j<fCol.size(); ++j) {
2431  fout << fCol[j].Name();
2432  if (j<fCol.size()-1) fout << ",";
2433  }
2434  }
2435 
2436  for (unsigned int i=0; i<fRow.size(); ++i) {
2437  for (unsigned int j=0; j<fCol.size(); ++j) {
2438  fout << fRow[i].Col(j);
2439  if (j<fCol.size()-1) fout << ",";
2440  }
2441  fout << std::endl;
2442  }
2443  }
2444 
2445  fout.close();
2446 
2447  return true;
2448  }
bool MakeConditionsCSVString(std::stringstream &ss)
Definition: Table.cpp:2305
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
bool CheckForNulls()
Definition: Table.cpp:422
std::vector< nutools::dbi::Row > fRow
Definition: Table.h:388
bool nutools::dbi::Table::WriteToCSV ( const char *  fname,
bool  appendToFile = false,
bool  writeColNames = false 
)
inline

Definition at line 151 of file Table.h.

References AddDistinctColumn(), ClearValidity(), col, and WriteToCSV().

152  { return WriteToCSV(std::string(fname),appendToFile,writeColNames); }
bool WriteToCSV(std::string fname, bool appendToFile=false, bool writeColNames=false)
Definition: Table.cpp:2412
bool nutools::dbi::Table::WriteToDB ( bool  commit = true)

use commit=false if just testing

Definition at line 2075 of file Table.cpp.

References addInsertTime, addInsertUser, addUpdateTime, addUpdateUser, CacheDBCommand(), CheckForNulls(), CloseConnection(), nutools::dbi::Row::Col(), e, ExistsInDB(), fCol, fConnection, fHasConnection, fIgnoreDB, fPKeyList, fRow, fSchema, fTimeQueries, fUser, fVerbosity, GetColNameToIndexMap(), GetConnection(), GetConnectionInfo(), nutools::dbi::Util::GetCurrentTimeAsString(), GetCurrSeqVal(), nutools::dbi::Column::Modified(), Name(), nutools::dbi::Row::NModified(), r, Schema(), nutools::dbi::Row::Set(), and nutools::dbi::Row::Update().

Referenced by LoadFromCSV().

2076  {
2077  if (! CheckForNulls()) return false;
2078 
2079  bool doWrite = ! fIgnoreDB;
2080  bool hasConn = fHasConnection;
2081 
2082  try {
2084  }
2085  catch (std::runtime_error& e) {
2086  std::cerr << e.what() << std::endl;
2087  return false;
2088  }
2089 
2090  // make a connection to the dB if one doesn't already exist
2091  if (doWrite) {
2092  if (! fHasConnection) {
2093  GetConnection();
2094  hasConn = false;
2095  }
2096 
2097  if (!fConnection) {
2098  std::cerr << "Table::WriteToDB: No connection to the database!" << std::endl;
2099  doWrite = false;
2100  }
2101  else {
2102  // now check that the table actually exists...
2103  if (!ExistsInDB()) {
2104  std::cerr << "Table::WriteToDB: Table does not exist in database!"
2105  << std::endl;
2106  doWrite = false;
2107  }
2108  }
2109  }
2110 
2111  bool retVal = true;
2112 
2113  // get current timestamp:
2114  std::string ts = Util::GetCurrentTimeAsString();
2115 
2116  PGresult* res = PQexec(fConnection, "BEGIN");
2117  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2118  std::cerr << "BEGIN command failed: " << PQerrorMessage(fConnection) << std::endl;
2119  PQclear(res);
2120  CloseConnection();
2121  return false;
2122  }
2123 
2124  PQclear(res);
2125 
2126  std::string cmd = "SET search_path TO " + fSchema;
2127  res = PQexec(fConnection, cmd.c_str());
2128  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2129  std::cerr << "\'" << cmd << "\' command failed" << std::endl;
2130  PQclear(res);
2131  CloseConnection();
2132  return false;
2133  }
2134  PQclear(res);
2135  cmd.clear();
2136 
2137  std::map<std::string,int> colMap = GetColNameToIndexMap();
2138  int insertTimeIdx = colMap["inserttime"];
2139  int insertUserIdx = colMap["insertuser"];
2140  int updateTimeIdx = colMap["updatetime"];
2141  int updateUserIdx = colMap["updateuser"];
2142  // now create the INSERT command
2143  for (unsigned int i=0; i<fRow.size(); ++i) {
2144  // do an INSERT only if this entry does not already exists in the dB
2145  if (! fRow[i].InDB()) {
2146  Row r(fRow[i]);
2147  if (addInsertTime) r.Set(insertTimeIdx,ts);
2148  if (addInsertUser) r.Set(insertUserIdx,fUser);
2149 
2150  int nrowInsert = fCol.size();
2151  for (unsigned int j=0; j<fCol.size(); ++j) {
2152  if (fCol[j].Name() == "updatetime")
2153  nrowInsert--;
2154  else if (fCol[j].Name() == "updateuser")
2155  nrowInsert--;
2156  else if (fCol[j].Type() == "autoincr")
2157  nrowInsert--;
2158  }
2159 
2160  std::ostringstream outs;
2161 
2162  int ic=0;
2163  outs << "INSERT INTO " << Schema() << "." << Name() << " (";
2164  for (unsigned int j=0; j<fCol.size(); ++j) {
2165  if (fCol[j].Name() == "updatetime") continue;
2166  if (fCol[j].Name() == "updateuser") continue;
2167  if (fCol[j].Type() == "autoincr") continue;
2168 
2169  outs << fCol[j].Name();
2170  if (ic < nrowInsert-1) outs << ",";
2171  ++ic;
2172  }
2173  outs << ") VALUES (";
2174 
2175  ic = 0;
2176  for (unsigned int j=0; j<fCol.size(); ++j) {
2177  if (fCol[j].Name() == "updatetime") continue;
2178  if (fCol[j].Name() == "updateuser") continue;
2179  if (fCol[j].Type() == "autoincr") continue;
2180 
2181  outs << r.Col(j);
2182 
2183  if (ic < nrowInsert-1) outs << ",";
2184  ++ic;
2185  }
2186 
2187  outs << ")";
2188 
2189  if (fVerbosity > 0)
2190  std::cerr << "Table::WriteToDB: Executing PGSQL command: \n\t"
2191  << outs.str() << std::endl;
2192 
2193  if (!commit)
2194  std::cout << outs.str() << std::endl;
2195  else {
2196  if (doWrite) {
2197  boost::posix_time::ptime ctt1;
2198  boost::posix_time::ptime ctt2;
2199 
2200  if (fTimeQueries)
2201  ctt1 = boost::posix_time::microsec_clock::local_time();
2202 
2203  res = PQexec(fConnection, outs.str().c_str());
2204 
2205  if (fTimeQueries) {
2206  ctt2 = boost::posix_time::microsec_clock::local_time();
2207  boost::posix_time::time_duration tdiff = ctt2 - ctt1;
2208  std::cerr << "Table::WriteToDB(" << Name() << "): query took "
2209  << tdiff.total_milliseconds() << " ms" << std::endl;
2210  }
2211 
2212  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2213  CacheDBCommand(outs.str());
2214  std::cerr << "INSERT failed: " << PQerrorMessage(fConnection)
2215  << std::endl;
2216  retVal = false;
2217  }
2218  else {
2219  fRow[i].SetInDB();
2220  // set insert columns
2221  if (addInsertTime) fRow[i].Col(insertTimeIdx).Set(ts);
2222  if (addInsertUser) fRow[i].Col(insertUserIdx).Set(fUser);
2223  // set autoincr columns
2224  long iseq;
2225  std::string seqstr;
2226  for (unsigned int j=0; j<fCol.size(); ++j) {
2227  if (fCol[j].Type() == "autoincr") {
2228  if (this->GetCurrSeqVal(fCol[j].Name(),iseq)) {
2229  seqstr = boost::lexical_cast<std::string>(iseq);
2230  fRow[i].Col(j).Set(seqstr,true);
2231  }
2232  }
2233  }
2234  }
2235  PQclear(res);
2236  }
2237  else
2238  CacheDBCommand(outs.str());
2239  }
2240  }
2241  else {
2242  if ( fRow[i].NModified() > 0 ) {
2243  Row r(fRow[i]);
2244  if (addUpdateTime) r.Update(updateTimeIdx,ts);
2245  if (addUpdateUser) r.Update(updateUserIdx,fUser);
2246  std::ostringstream outs;
2247  outs << "UPDATE " << Schema() << "." << Name() << " SET ";
2248  int im = 0;
2249  for (unsigned int j=0; j<fCol.size() && im < r.NModified(); ++j) {
2250  if (r.Col(j).Modified()) {
2251  outs << fCol[j].Name() + "=";
2252  outs << r.Col(j);
2253  ++im;
2254  if (im < r.NModified()) outs << ",";
2255  }
2256  }
2257  outs << " WHERE ";
2258  // now print out all pkey values
2259  int nkey = fPKeyList.size();
2260  for (int j=0; j<nkey; ++j) {
2261  std::string pkey = fPKeyList[j]->Name();
2262  int pkeyIdx = colMap[pkey];
2263  outs << pkey << "=" << r.Col(pkeyIdx);
2264  if (j < (nkey-1)) outs << " and ";
2265  }
2266 
2267  if (fVerbosity > 0)
2268  std::cerr << "Table::WriteToDB: Executing PGSQL command: \n\t"
2269  << outs.str() << std::endl;
2270 
2271  if (!commit)
2272  std::cout << outs.str() << std::endl;
2273  else {
2274  if (doWrite) {
2275  res = PQexec(fConnection, outs.str().c_str());
2276  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2277  CacheDBCommand(outs.str());
2278  std::cerr << "UPDATE failed: " << PQerrorMessage(fConnection) << std::endl;
2279  retVal = false;
2280  }
2281  else {
2282  // set update columns
2283  if (addUpdateTime) fRow[i].Col(updateTimeIdx).Set(ts);
2284  if (addUpdateUser) fRow[i].Col(updateUserIdx).Set(fUser);
2285  }
2286  PQclear(res);
2287  }
2288  else
2289  CacheDBCommand(outs.str());
2290  }
2291  }
2292  }
2293  }
2294 
2295  res = PQexec(fConnection, "END");
2296  PQclear(res);
2297 
2298  // close connection to the dB if necessary
2299  if (! hasConn) CloseConnection();
2300 
2301  return retVal;
2302  }
std::string fUser
Definition: Table.h:369
TRandom r
Definition: spectrum.C:23
bool addUpdateUser
Definition: Table.h:344
std::string Name()
Definition: Table.h:59
bool GetConnection(int ntry=0)
Definition: Table.cpp:759
void CacheDBCommand(std::string cmd)
Definition: Table.cpp:438
std::vector< nutools::dbi::ColumnDef > fCol
Definition: Table.h:387
bool addInsertTime
Definition: Table.h:341
std::map< std::string, int > GetColNameToIndexMap()
Definition: Table.cpp:598
static std::string GetCurrentTimeAsString()
Definition: Util.cpp:42
std::string Schema()
Definition: Table.h:215
bool CloseConnection()
Definition: Table.cpp:837
PGconn * fConnection
Definition: Table.h:401
bool GetConnectionInfo(int ntry=0)
Definition: Table.cpp:699
std::vector< const nutools::dbi::ColumnDef * > fPKeyList
Definition: Table.h:392
bool CheckForNulls()
Definition: Table.cpp:422
bool fHasConnection
Definition: Table.h:351
short fVerbosity
Definition: Table.h:357
pg_result PGresult
Definition: Table.h:19
std::string fSchema
Definition: Table.h:374
bool GetCurrSeqVal(std::string col, long &iseq)
Definition: Table.cpp:970
bool addInsertUser
Definition: Table.h:342
bool addUpdateTime
Definition: Table.h:343
Float_t e
Definition: plot.C:35
std::vector< nutools::dbi::Row > fRow
Definition: Table.h:388

Friends And Related Function Documentation

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

Definition at line 414 of file Table.h.

Referenced by GetConnectionTimeout().

414  {
415  for (unsigned int j=0; j<t.fRow.size(); ++j) {
416  stream << t.fRow[j] << std::endl;
417  }
418  return stream;
419  }

Member Data Documentation

bool nutools::dbi::Table::addInsertTime
private

Definition at line 341 of file Table.h.

Referenced by AddCol(), GetColsFromDB(), Table(), and WriteToDB().

bool nutools::dbi::Table::addInsertUser
private

Definition at line 342 of file Table.h.

Referenced by AddCol(), GetColsFromDB(), Table(), and WriteToDB().

bool nutools::dbi::Table::addUpdateTime
private

Definition at line 343 of file Table.h.

Referenced by AddCol(), GetColsFromDB(), Table(), and WriteToDB().

bool nutools::dbi::Table::addUpdateUser
private

Definition at line 344 of file Table.h.

Referenced by AddCol(), GetColsFromDB(), Table(), and WriteToDB().

std::vector<uint64_t> nutools::dbi::Table::fChannelVec
private

Definition at line 398 of file Table.h.

Referenced by FillChanRowMap(), and VldChannels().

std::unordered_map<uint64_t,std::vector<nutools::dbi::Row*> > nutools::dbi::Table::fChanRowMap
private

Definition at line 399 of file Table.h.

Referenced by ClearChanRowMap(), FillChanRowMap(), GetVldRow(), GetVldRows(), NVldChannels(), and NVldRows().

PGconn* nutools::dbi::Table::fConnection
private
int nutools::dbi::Table::fConnectionTimeout
private
int nutools::dbi::Table::fDataSource
private

Definition at line 364 of file Table.h.

Referenced by DataSource(), SetDataSource(), and Table().

int nutools::dbi::Table::fDataTypeMask
private

Definition at line 363 of file Table.h.

Referenced by DataTypeMask(), LoadConditionsTable(), SetDataTypeMask(), Table(), and Write().

std::string nutools::dbi::Table::fDBCacheFile
private

Definition at line 376 of file Table.h.

Referenced by CacheDBCommand(), and Table().

std::string nutools::dbi::Table::fDBHost
private

Definition at line 372 of file Table.h.

Referenced by DBHost(), GetConnection(), GetConnectionInfo(), SetDBHost(), and Table().

std::string nutools::dbi::Table::fDBName
private

Definition at line 373 of file Table.h.

Referenced by DBName(), GetConnection(), GetConnectionInfo(), SetDBName(), and Table().

std::string nutools::dbi::Table::fDBPort
private

Definition at line 371 of file Table.h.

Referenced by DBPort(), GetConnection(), GetConnectionInfo(), SetDBPort(), and Table().

bool nutools::dbi::Table::fDescOrder
private

Definition at line 347 of file Table.h.

Referenced by LoadFromDB(), LoadNonConditionsTable(), Reset(), SetOrderAsc(), and SetOrderDesc().

std::string nutools::dbi::Table::fDetector
private

Definition at line 379 of file Table.h.

Referenced by GetDetector(), SetDetector(), and Table().

bool nutools::dbi::Table::fDisableCache
private

Definition at line 354 of file Table.h.

Referenced by DisableCache(), EnableCache(), LoadConditionsTable(), and LoadNonConditionsTable().

std::vector<const nutools::dbi::ColumnDef*> nutools::dbi::Table::fDistinctCol
private

Definition at line 393 of file Table.h.

Referenced by AddDistinctColumn(), Clear(), LoadFromDB(), RemoveDistinctColumn(), and Reset().

std::vector<std::string> nutools::dbi::Table::fExcludeCol
private
bool nutools::dbi::Table::fExistsInDB
private

Definition at line 350 of file Table.h.

Referenced by ExistsInDB(), GetColsFromDB(), and Table().

bool nutools::dbi::Table::fFlushCache
private
std::string nutools::dbi::Table::fFolder
private

Definition at line 380 of file Table.h.

Referenced by Folder(), SetFolder(), and Table().

bool nutools::dbi::Table::fHasConnection
private
bool nutools::dbi::Table::fHasRecordTime
private

Definition at line 352 of file Table.h.

Referenced by ClearRecordTime(), LoadConditionsTable(), and SetRecordTime().

bool nutools::dbi::Table::fIgnoreDB
private

Definition at line 348 of file Table.h.

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

bool nutools::dbi::Table::fIgnoreEnvVar
private

Definition at line 345 of file Table.h.

Referenced by GetConnectionInfo(), SetIgnoreEnvVar(), and Table().

uint64_t nutools::dbi::Table::fMaxChannel
private

Definition at line 366 of file Table.h.

Referenced by LoadConditionsTable(), Reset(), SetChannelRange(), SetMaxChannel(), and Table().

double nutools::dbi::Table::fMaxTSVld
private
uint64_t nutools::dbi::Table::fMinChannel
private

Definition at line 365 of file Table.h.

Referenced by LoadConditionsTable(), Reset(), SetChannelRange(), SetMinChannel(), and Table().

double nutools::dbi::Table::fMinTSVld
private
std::vector<std::pair<int,int> > nutools::dbi::Table::fNullList
private

Definition at line 395 of file Table.h.

Referenced by AddRow(), CheckForNulls(), Clear(), ClearRows(), and RemoveRow().

std::vector<const nutools::dbi::ColumnDef*> nutools::dbi::Table::fOrderCol
private

Definition at line 394 of file Table.h.

Referenced by AddOrderColumn(), Clear(), LoadFromDB(), LoadNonConditionsTable(), and RemoveOrderColumn().

std::string nutools::dbi::Table::fPassword
private

Definition at line 377 of file Table.h.

Referenced by ClearPassword(), DisablePasswordAccess(), GetConnection(), and SetPasswordFile().

std::vector<const nutools::dbi::ColumnDef*> nutools::dbi::Table::fPKeyList
private

Definition at line 392 of file Table.h.

Referenced by GetNPKeyCol(), GetPKeyCol(), Reset(), and WriteToDB().

std::string nutools::dbi::Table::fQEURL
private

Definition at line 385 of file Table.h.

Referenced by LoadNonConditionsTable(), SetQEURL(), and Table().

double nutools::dbi::Table::fRecordTime
private

Definition at line 407 of file Table.h.

Referenced by LoadConditionsTable(), and SetRecordTime().

std::string nutools::dbi::Table::fRole
private

Definition at line 370 of file Table.h.

Referenced by ResetRole(), Role(), and SetRole().

std::string nutools::dbi::Table::fSchema
private
int nutools::dbi::Table::fSelectLimit
private

Definition at line 359 of file Table.h.

Referenced by LoadFromDB(), LoadNonConditionsTable(), Reset(), and SetSelectLimit().

int nutools::dbi::Table::fSelectOffset
private

Definition at line 360 of file Table.h.

Referenced by LoadFromDB(), Reset(), and SetSelectOffset().

std::string nutools::dbi::Table::fTableName
private

Definition at line 368 of file Table.h.

Referenced by GetColsFromDB(), Name(), SetTableName(), and Table().

int nutools::dbi::Table::fTableType
private
std::string nutools::dbi::Table::fTag
private

Definition at line 382 of file Table.h.

Referenced by GetTag(), LoadConditionsTable(), SetTag(), Table(), and Tag().

bool nutools::dbi::Table::fTestedExists
private

Definition at line 349 of file Table.h.

Referenced by ExistsInDB(), and Table().

bool nutools::dbi::Table::fTimeParsing
private

Definition at line 356 of file Table.h.

Referenced by GetDataFromWebService(), LoadFromDB(), SetTimeParsing(), Table(), and TimeParsing().

bool nutools::dbi::Table::fTimeQueries
private
std::string nutools::dbi::Table::fUConDBURL
private

Definition at line 384 of file Table.h.

Referenced by LoadUnstructuredConditionsTable(), and Table().

std::string nutools::dbi::Table::fUser
private

Definition at line 369 of file Table.h.

Referenced by GetConnection(), GetConnectionInfo(), ResetRole(), SetUser(), Table(), User(), and WriteToDB().

bool nutools::dbi::Table::fValidityChanged
private
std::vector<nutools::dbi::ColumnDef> nutools::dbi::Table::fValidityEnd
private
std::string nutools::dbi::Table::fValiditySQL
private
std::vector<nutools::dbi::ColumnDef> nutools::dbi::Table::fValidityStart
private
std::string nutools::dbi::Table::fWSURL
private

Definition at line 383 of file Table.h.

Referenced by LoadConditionsTable(), SetWSURL(), Table(), Tag(), and Write().


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