Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
nova::database::Table Class Reference

#include "/cvmfs/nova-development.opensciencegrid.org/novasoft/releases/N21-01-18/NovaDatabase/cxx/include/Table.h"

Public Member Functions

 Table ()
 
 Table (std::string tname)
 
 Table (const char *tname)
 
 ~Table ()
 
std::string Name ()
 
std::string DBName ()
 
std::string DBHost ()
 
std::string User ()
 
std::string Role ()
 
std::string DBPort ()
 
int TableType ()
 
bool SetTableType (int t)
 
bool CreateFromDB (const char *schemaName, const char *tableName, const char *dbhost=0, const char *dbname=0, const char *dbport=0, const char *dbuser=0)
 
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 ()
 
RowGetRow (int i)
 
RowGetRowByPKey (std::vector< std::string > &pkeyval)
 
std::vector< Row * > const GetRowByColSet (std::vector< Column > &cols)
 
void AddRow (const Row *row)
 
void AddRow (const Row &row)
 
bool RemoveRow (int i)
 
RowNewRow ()
 
std::vector< std::stringGetColNames ()
 
std::string GetColName (int i)
 
const ColumnGetCol (int i)
 
const ColumnGetCol (std::string &cname)
 
const ColumnGetCol (const char *cname)
 
void SetTolerance (std::string &cname, float t)
 
void SetToleranceType (std::string &cname, int t)
 
float Tolerance (std::string &cname)
 
float ToleranceType (std::string &cname)
 
bool ExistsInDB ()
 
bool ExecuteSQL (std::string cmd, PGresult *&res)
 
bool LoadFromCSV (std::string fname, bool update=false)
 
bool LoadFromCSV (const char *fname, bool update=false)
 
bool UpdateFromCSV (std::string fname)
 
bool UpdateFromCSV (const char *fname)
 
bool LoadFromSSV (std::string fname, bool update=false)
 
bool LoadFromSSV (const char *fname, bool update=false)
 
bool UpdateFromSSV (std::string fname)
 
bool UpdateFromSSV (const char *fname)
 
bool LoadFromDB ()
 
bool WriteToDB (bool commit=true)
 use commit=false if just testing More...
 
bool BulkInsertInDB (int rowsPerGroup=100)
 
bool WriteToCSV (std::string fname, bool appendToFile=false)
 
bool WriteToCSV (const char *fname, bool appendToFile=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 ()
 
bool GetCurrSeqVal (std::string col, int64_t &iseq)
 
bool GetCurrSeqVal (const char *col, int64_t &iseq)
 
bool HasPKey ()
 
int GetNPKeyCol ()
 
const ColumnGetPKeyCol (int i)
 
int GetNUnique ()
 
const dBRow_tGetUniqueRow (int i)
 
int GetNCheck ()
 
std::string GetCheckConstraint (int i)
 
void PrintPQErrorMsg ()
 
std::string GetValiditySQL ()
 
void SetValiditySQL (std::string cmd)
 
bool SetDetector (std::string det)
 
bool GetDetector (std::string &det) const
 
bool SetDetector (int idet)
 
bool GetDetector (int &idet) 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 ()
 
bool CloseConnection ()
 
void SetMaxConnAttempts (int n)
 
int GetMaxConnAttempts ()
 
bool CheckValidXMLDef ()
 
bool ResetConnectionInfo ()
 
void CacheDBCommand (std::string cmd)
 

Public Attributes

std::map< std::string, dBColumn_t * > XMLCol
 

Private Member Functions

void CreateFromXML (const char *fname)
 
void Reset ()
 
bool GetConnectionInfo ()
 
bool CheckForNulls ()
 

Private Attributes

std::vector< ColumnfCol
 
std::vector< RowfRow
 
::std::unique_ptr< dBTablefTable
 
bool fXMLOverDB
 
bool addInsertTime
 
bool addInsertUser
 
bool addUpdateTime
 
bool addUpdateUser
 
bool fIgnoreEnvVar
 
bool fValidityChanged
 
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::vector< ColumnfValidityStart
 
std::vector< ColumnfValidityEnd
 
std::vector< const Column * > fPKeyList
 
std::vector< const Column * > fDistinctCol
 
std::vector< const Column * > fOrderCol
 
std::vector< std::pair< int, int > > fNullList
 
short fVerbosity
 
bool fDescOrder
 
bool fIgnoreDB
 
bool fHasValidXML
 
bool fTestedXML
 
bool fTestedExists
 
bool fExistsInDB
 
int fSelectLimit
 
int fSelectOffset
 
int fMaxConnAttempts
 
int fTableType
 
bool fHasConnection
 
PGconnfConnection
 
std::string fDetector
 

Static Private Attributes

static boost::mutex _xsdLock
 

Friends

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

Detailed Description

Generalized Database Table Interface

Author
Jonathan Paley
Version
Id
Table.h,v 1.64.12.1 2019/09/27 00:07:24 slin Exp

Definition at line 33 of file Table.h.

Constructor & Destructor Documentation

Table::Table ( )

Definition at line 28 of file Table.cpp.

References addInsertTime, addInsertUser, addUpdateTime, addUpdateUser, CreateFromXML(), fDBHost, fDBName, fDBPort, fDetector, fExistsInDB, fHasValidXML, fIgnoreEnvVar, fMaxConnAttempts, fSchema, fTableType, fTestedExists, fTestedXML, fUser, cet::getenv(), nova::database::kGenericTable, Reset(), and getGoodRuns4SAM::tname.

28  : fMaxConnAttempts(10), fDetector("")
29 {
30  fDBHost="";
31  fDBName="";
32  fDBPort="";
33  fUser="";
34  fSchema="undef";
36 
37  fIgnoreEnvVar = false;
38  fHasValidXML = false;
39  fTestedXML = false;
40  fTestedExists = false;
41  fExistsInDB = false;
42  addInsertTime = addUpdateTime = false;
43  addInsertUser = addUpdateUser = false;
44 
45  Reset();
46 
47  char* tmpStr = getenv("NOVADBNCNXTRIES");
48  if (tmpStr) {
49  int nTry = atoi(tmpStr);
50  if (nTry > 0)
51  fMaxConnAttempts = nTry;
52  }
53 
54 }
std::string fDBPort
Definition: Table.h:281
std::string fDBHost
Definition: Table.h:282
std::string getenv(std::string const &name)
std::string fUser
Definition: Table.h:279
std::string fDetector
Definition: Table.h:311
std::string fDBName
Definition: Table.h:283
std::string fSchema
Definition: Table.h:284
nova::database::Table::Table ( std::string  tname)
Table::Table ( const char *  tname)

Definition at line 77 of file Table.cpp.

References CreateFromXML(), fDBHost, fDBName, fDBPort, fExistsInDB, fHasValidXML, fIgnoreEnvVar, fSchema, fTableType, fTestedExists, fTestedXML, fUser, nova::database::kGenericTable, and Reset().

77  : fMaxConnAttempts(10), fDetector("")
78 {
79  fDBHost="";
80  fDBName="";
81  fDBPort="";
82  fUser="";
83  fSchema="undef";
85  fIgnoreEnvVar = false;
86  fHasValidXML = false;
87  fTestedXML = false;
88  fTestedExists = false;
89  fExistsInDB = false;
90  Reset();
92 }
std::string fDBPort
Definition: Table.h:281
std::string fDBHost
Definition: Table.h:282
std::string fUser
Definition: Table.h:279
std::string fDetector
Definition: Table.h:311
void CreateFromXML(const char *fname)
Definition: Table.cpp:277
std::string fDBName
Definition: Table.h:283
std::string fSchema
Definition: Table.h:284
Table::~Table ( )

Definition at line 96 of file Table.cpp.

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

97 {
98  this->Clear();
100 }

Member Function Documentation

bool Table::AddDistinctColumn ( unsigned int  i)

Definition at line 2260 of file Table.cpp.

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

Referenced by AddDistinctColumn(), RemoveDistinctColumn(), and WriteToCSV().

2261 {
2262  if (i >= fCol.size()) return false;
2263 
2264  const Column* c = &fCol[i];
2265  for (unsigned int j=0; j<fDistinctCol.size(); ++j)
2266  if (fDistinctCol[j] == c) return true;
2267 
2268  fDistinctCol.push_back(c);
2269 
2270  return true;
2271 }
std::vector< Column > fCol
Definition: Table.h:268
std::vector< const Column * > fDistinctCol
Definition: Table.h:293
const double j
Definition: BetheBloch.cxx:29
bool nova::database::Table::AddDistinctColumn ( std::string  col)
bool nova::database::Table::AddDistinctColumn ( const char *  col)
inline

Definition at line 146 of file Table.h.

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

147  { return AddDistinctColumn(std::string(col)); }
Int_t col[ntarg]
Definition: Style.C:29
bool AddDistinctColumn(unsigned int i)
Definition: Table.cpp:2260
enum BeamMode string
bool Table::AddOrderColumn ( unsigned int  i)

Definition at line 2332 of file Table.cpp.

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

Referenced by AddOrderColumn(), RemoveDistinctColumn(), and RemoveOrderColumn().

2333 {
2334  if (i >= fCol.size()) return false;
2335 
2336  const Column* c = &fCol[i];
2337  for (unsigned int j=0; j<fOrderCol.size(); ++j)
2338  if (fOrderCol[j] == c) return true;
2339 
2340  fOrderCol.push_back(c);
2341 
2342  return true;
2343 }
std::vector< Column > fCol
Definition: Table.h:268
const double j
Definition: BetheBloch.cxx:29
std::vector< const Column * > fOrderCol
Definition: Table.h:294
bool nova::database::Table::AddOrderColumn ( std::string  col)
bool nova::database::Table::AddOrderColumn ( const char *  col)
inline

Definition at line 155 of file Table.h.

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

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

Definition at line 727 of file Table.cpp.

References nova::database::Column::CanBeNull(), nova::database::Row::Col(), fCol, fNullList, fRow, MECModelEnuComparisons::i, nova::database::Column::IsNull(), and nova::database::Column::Name().

Referenced by AddRow(), ClearRows(), comi::CosmicMetrics::endJob(), comi::DataCheck::endJob(), LoadFromCSV(), LoadFromDB(), and LoadFromSSV().

728 {
729  if (!row) return;
730 
731  Row r2(*row);
732 
733  for (unsigned int i=0; i<fCol.size(); ++i) {
734  if (r2.Col(i).Name() == "inserttime" ||
735  r2.Col(i).Name() == "insertuser" ||
736  r2.Col(i).Name() == "updatetime" ||
737  r2.Col(i).Name() == "updateuser" ) continue;
738  /*
739  if (TableType() == "runsubrun") {
740  r2.Col("minrun")->Set(fRunVld->Value());
741  r2.Col("minsubrun")->Set(fSubrunVld->Value());
742  r2.Col("maxrun")->Set(fRunVld->Value());
743  r2.Col("maxsubrun")->Set(fSubrunVld->Value());
744  }
745  if (TableType() == "timestamp") {
746  r2.Col("mintime")->Set(fTSVld->Value());
747  r2.Col("maxtime")->Set(fTSVld->Value());
748  }
749  */
750  if (!r2.Col(i).CanBeNull())
751  if (r2.Col(i).IsNull())
752  fNullList.push_back(pair<int,int>(fRow.size(),i));
753  }
754 
755  fRow.push_back(r2);
756 
757 }
std::vector< Column > fCol
Definition: Table.h:268
std::vector< Row > fRow
Definition: Table.h:269
std::vector< std::pair< int, int > > fNullList
Definition: Table.h:295
void Table::AddRow ( const Row row)

Definition at line 760 of file Table.cpp.

References AddRow().

761 {
762  AddRow(&row);
763 }
void AddRow(const Row *row)
Definition: Table.cpp:727
bool Table::BulkInsertInDB ( int  rowsPerGroup = 100)

Inserts the new Rows in this Table into the database.

Please Note that this method does not provide the same level of functionality as WriteToDB(). That method handles updates as well as inserts, whereas this method only handles inserts. Also, this method does not update the Table Rows in local memory with the the correct ID (and other fields) after the Rows are stored in the database.

The expected advantage of this method is faster execution than WriteToDB() for the same number of new Rows. However, users should be careful when using this method (maybe call table->Clear() after calling this method to reduce the danger that the Table Rows will be accessed when they have data that does not fully match what is in the database).

Definition at line 2075 of file Table.cpp.

References addInsertTime, addInsertUser, demo::app, lemmon_tools::appendToFile(), CacheDBCommand(), om::cerr, CheckForNulls(), CheckValidXMLDef(), CloseConnection(), nova::database::Row::Col(), allTimeWatchdog::endl, ExistsInDB(), fCol, fConnection, fHasConnection, fIgnoreDB, plot_validation_datamc::fname, submit_syst::fout, fRow, fUser, fVerbosity, GetConnection(), GetConnectionInfo(), nova::database::Util::GetCurrentTimeAsString(), MECModelEnuComparisons::i, calib::j, nova::database::Column::Name(), Name(), r(), Schema(), nova::database::Row::Set(), nova::database::Column::Type(), and WriteToCSV().

Referenced by UpdateFromSSV().

2076 {
2077  if (! CheckForNulls()) return false;
2078 
2080 
2081  bool doWrite = ! fIgnoreDB;
2082  bool hasConn = fHasConnection;
2083 
2084  // make a connection to the dB if one doesn't already exist
2085  if (doWrite) {
2086  if (! fHasConnection) {
2087  GetConnection();
2088  hasConn = false;
2089  }
2090 
2091  if (!fConnection) {
2092  std::cerr << "Table::BulkWriteToDB: No connection to the database!" << std::endl;
2093  doWrite = false;
2094  }
2095  else {
2096  // now check that the table actually exists...
2097  if (!ExistsInDB()) {
2098  std::cerr << "Table::BulkWriteToDB: Table does not exist in database!" << std::endl;
2099  doWrite = false;
2100  }
2101  else {
2102  if (! CheckValidXMLDef()) {
2103  std::cerr << "Table::BulkWriteToDB: Mismatch between XML table definition file ";
2104  std::cerr << "and table defintion from the database! Aborting write...";
2105  doWrite = false;
2106  }
2107  }
2108  }
2109  }
2110 
2111  bool retVal = true;
2112 
2113  // get current timestamp:
2114  string ts = Util::GetCurrentTimeAsString();
2115 
2116  // now create the INSERT command
2117  int bulkInsertCount = 0;
2118  ostringstream outs;
2119  outs.str("");
2120  for (unsigned int i=0; i<fRow.size(); ++i) {
2121  // do an INSERT only if this entry does not already exists in the dB
2122  if (! fRow[i].InDB()) {
2123  Row r(fRow[i]);
2124  if (addInsertTime) r.Set("inserttime",ts);
2125  if (addInsertUser) r.Set("insertuser",fUser);
2126 
2127  int nrowInsert = fCol.size();
2128  for (unsigned int j=0; j<fCol.size(); ++j) {
2129  if (r.Col(j).Name() == "updatetime")
2130  nrowInsert--;
2131  else if (r.Col(j).Name() == "updateuser")
2132  nrowInsert--;
2133  else if (r.Col(j).Type() == "autoincr")
2134  nrowInsert--;
2135  }
2136 
2137  int ic;
2138  if (bulkInsertCount == 0) {
2139  ic = 0;
2140  outs << "INSERT INTO " << Schema() << "." << Name() << " (";
2141  for (unsigned int j=0; j<fCol.size(); ++j) {
2142  if (r.Col(j).Name() == "updatetime") continue;
2143  if (r.Col(j).Name() == "updateuser") continue;
2144  if (r.Col(j).Type() == "autoincr") continue;
2145 
2146  outs << r.Col(j).Name();
2147  if (ic < nrowInsert-1) outs << ",";
2148  ++ic;
2149  }
2150  outs << ") VALUES (";
2151  }
2152  else {
2153  outs << ", (";
2154  }
2155 
2156  ic = 0;
2157  for (unsigned int j=0; j<fCol.size(); ++j) {
2158  if (r.Col(j).Name() == "updatetime") continue;
2159  if (r.Col(j).Name() == "updateuser") continue;
2160  if (r.Col(j).Type() == "autoincr") continue;
2161 
2162  outs << r.Col(j);
2163 
2164  if (ic < nrowInsert-1) outs << ",";
2165  ++ic;
2166  }
2167 
2168  outs << ")";
2169 
2170  ++bulkInsertCount;
2171  if (bulkInsertCount >= rowsPerGroup) {
2172  if (fVerbosity > 0)
2173  std::cerr << "Table::BulkWriteToDB: Executing PGSQL command: \n\t"
2174  << outs.str() << std::endl;
2175 
2176  if (doWrite) {
2177  PGresult* res = PQexec(fConnection, outs.str().c_str());
2178  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2179  CacheDBCommand(outs.str());
2180  std::cerr << "INSERT failed: " << PQerrorMessage(fConnection) << std::endl;
2181  retVal = false;
2182  }
2183  PQclear(res);
2184  }
2185  else {
2186  CacheDBCommand(outs.str());
2187  }
2188 
2189  bulkInsertCount = 0;
2190  outs.str("");
2191  }
2192  }
2193  }
2194 
2195  if (bulkInsertCount > 0) {
2196  if (fVerbosity > 0)
2197  std::cerr << "Table::WriteToDB: Executing PGSQL command: \n\t"
2198  << outs.str() << std::endl;
2199 
2200  if (doWrite) {
2201  PGresult* res = PQexec(fConnection, outs.str().c_str());
2202  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2203  CacheDBCommand(outs.str());
2204  std::cerr << "INSERT failed: " << PQerrorMessage(fConnection) << std::endl;
2205  retVal = false;
2206  }
2207  PQclear(res);
2208  }
2209  else {
2210  CacheDBCommand(outs.str());
2211  }
2212  }
2213 
2214  // close connection to the dB if necessary
2215  if (! hasConn) CloseConnection();
2216 
2217  return retVal;
2218 }
PGconn * fConnection
Definition: Table.h:309
std::vector< Column > fCol
Definition: Table.h:268
static std::string GetCurrentTimeAsString()
Definition: Util.cpp:34
std::vector< Row > fRow
Definition: Table.h:269
OStream cerr
Definition: OStream.cxx:7
pg_result PGresult
Definition: Table.h:25
std::string Schema()
Definition: Table.h:203
bool CheckValidXMLDef()
Definition: Table.cpp:889
std::string fUser
Definition: Table.h:279
const double j
Definition: BetheBloch.cxx:29
bool GetConnectionInfo()
Definition: Table.cpp:1446
void CacheDBCommand(std::string cmd)
TRandom3 r(0)
std::string Name()
Definition: Table.h:42
void nova::database::Table::CacheDBCommand ( std::string  cmd)
bool Table::CheckForNulls ( )
private

Definition at line 787 of file Table.cpp.

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

Referenced by BulkInsertInDB(), and WriteToDB().

788 {
789  // if (TableType() == "runsubrun" || TableType() == "timestamp") return true;
790 
791  bool isOk = fNullList.empty();
792 
793  if (!isOk) // print out list of null columns
794  for (unsigned int i=0; i<fNullList.size(); ++i)
795  if (fVerbosity>0)
796  std::cerr << fCol[fNullList[i].second].Name() << " is NULL in row "
797  << fNullList[i].first << std::endl;
798 
799  return isOk;
800 
801 }
std::vector< Column > fCol
Definition: Table.h:268
OStream cerr
Definition: OStream.cxx:7
std::vector< std::pair< int, int > > fNullList
Definition: Table.h:295
bool Table::CheckValidXMLDef ( )

Definition at line 889 of file Table.cpp.

References om::cerr, CloseConnection(), run_hadd::cmd, allTimeWatchdog::endl, ExistsInDB(), fCol, fConnection, fDetector, fHasConnection, fHasValidXML, fIgnoreDB, fTableType, fTestedXML, GetConnection(), MECModelEnuComparisons::i, calib::j, nova::database::kHardwareTable, Name(), and Schema().

Referenced by BulkInsertInDB(), GetMaxConnAttempts(), LoadFromDB(), and WriteToDB().

890 {
891  if (fIgnoreDB) return false;
892 
893  if (fTestedXML) return fHasValidXML;
894 
895  if (fTableType != kHardwareTable)
896  if (fDetector == "") {
897  std::cerr << "Table::CheckValidXMLDef: Detector Validity not set!" << std::endl;
898  return false;
899  }
900 
901  // make a connection to the dB if there isn't one already
902  bool hasConn = fHasConnection;
903  if (! fHasConnection) {
904  if (! GetConnection()) {
905  std::cerr << "Table::CheckValidXMLDef: No connection to the database!"
906  << std::endl;
907  return false;
908  }
909  hasConn = false;
910  }
911 
912  if (!ExistsInDB()) {
913  std::cerr << "Table::CheckValidXMLDef: Table not found in database!"
914  << std::endl;
915  CloseConnection();
916  return false;
917  }
918 
919  string cmd;
920  PGresult* res;
921 
922  cmd = "SELECT column_name from information_schema.columns where table_schema=\'"
923  + Schema() + "\' and table_name=\'" + Name() + "\'";
924 
925  res = PQexec(fConnection, cmd.c_str());
926  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
927  std::cerr << "Table::CheckValidXML: Select failed: %"
928  << PQerrorMessage(fConnection) << std::endl;
929  PQclear(res);
930  CloseConnection();
931  return false;
932  }
933 
934  // now cache rows
935  // int nCol = PQnfields(res);
936  int nRow = PQntuples(res);
937 
938  bool isOk = true;
939 
940  for (unsigned int i=0; i<fCol.size(); ++i) {
941  string cName = fCol[i].Name();
942 
943  int j=0;
944  for (; j<nRow; ++j) {
945  string cName2 = PQgetvalue(res,j,0);
946  if (cName2 == cName) break;
947  }
948 
949  if (j == nRow) {
950  isOk = false;
951  break;
952  }
953  }
954 
955  PQclear(res);
956 
957  // close connection to the dB if necessary
958  if (! hasConn) CloseConnection();
959 
960  fTestedXML = true;
961  fHasValidXML = isOk;
962 
963  return fHasValidXML;
964 
965 }
PGconn * fConnection
Definition: Table.h:309
std::vector< Column > fCol
Definition: Table.h:268
OStream cerr
Definition: OStream.cxx:7
pg_result PGresult
Definition: Table.h:25
string cmd
Definition: run_hadd.py:52
std::string Schema()
Definition: Table.h:203
const double j
Definition: BetheBloch.cxx:29
std::string fDetector
Definition: Table.h:311
std::string Name()
Definition: Table.h:42
void nova::database::Table::Clear ( )
inline

Definition at line 83 of file Table.h.

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

Referenced by ~Table().

83  {
84  fRow.clear(); fValidityStart.clear(); fValidityEnd.clear();
85  fOrderCol.clear(); fDistinctCol.clear(); fNullList.clear();
86  fValiditySQL = "";
87  fValidityChanged = true;
88  }
std::vector< Row > fRow
Definition: Table.h:269
std::vector< const Column * > fDistinctCol
Definition: Table.h:293
std::vector< std::pair< int, int > > fNullList
Definition: Table.h:295
std::vector< Column > fValidityEnd
Definition: Table.h:291
std::vector< const Column * > fOrderCol
Definition: Table.h:294
std::string fValiditySQL
Definition: Table.h:288
std::vector< Column > fValidityStart
Definition: Table.h:290
void nova::database::Table::ClearPassword ( )
inline

Definition at line 73 of file Table.h.

References fPassword.

73 { fPassword=""; }
std::string fPassword
Definition: Table.h:287
void nova::database::Table::ClearRows ( )
inline

Definition at line 89 of file Table.h.

References AddRow(), cols, fNullList, fRow, fValidityChanged, GetRow(), GetRowByColSet(), GetRowByPKey(), MECModelEnuComparisons::i, RemoveRow(), and check_grl::row.

89 { fRow.clear(); fNullList.clear(); fValidityChanged=true;}
std::vector< Row > fRow
Definition: Table.h:269
std::vector< std::pair< int, int > > fNullList
Definition: Table.h:295
void Table::ClearValidity ( )

Definition at line 504 of file Table.cpp.

References fValidityChanged, fValidityEnd, fValiditySQL, and fValidityStart.

Referenced by Reset(), and WriteToCSV().

505 {
506  fValidityStart.clear();
507  fValidityEnd.clear();
508  fValiditySQL = "";
509  fValidityChanged=true;
510 }
std::vector< Column > fValidityEnd
Definition: Table.h:291
std::string fValiditySQL
Definition: Table.h:288
std::vector< Column > fValidityStart
Definition: Table.h:290
bool Table::CloseConnection ( )

Definition at line 1611 of file Table.cpp.

References om::cout, allTimeWatchdog::endl, fConnection, fHasConnection, fRole, fVerbosity, retire-remove-files::role, and SetRole().

Referenced by BulkInsertInDB(), CheckValidXMLDef(), CreateFromDB(), ExistsInDB(), GetConnection(), GetCurrSeqVal(), LoadFromDB(), RemoveValidityRange(), WriteToDB(), and ~Table().

1612 {
1613  if (fConnection) {
1614  PQfinish(fConnection);
1615  if (fVerbosity > 0)
1616  std::cout << "Closed connection" << std::endl;
1617  }
1618 
1619  fConnection = 0;
1620  fHasConnection = false;
1621 
1622  return true;
1623 }
PGconn * fConnection
Definition: Table.h:309
OStream cout
Definition: OStream.cxx:6
bool Table::CreateFromDB ( const char *  schemaName,
const char *  tableName,
const char *  dbhost = 0,
const char *  dbname = 0,
const char *  dbport = 0,
const char *  dbuser = 0 
)

Definition at line 104 of file Table.cpp.

References nova::database::dBRow_t::addDBCol(), addInsertTime, addInsertUser, addUpdateTime, addUpdateUser, plot_validation_datamc::c, om::cerr, CloseConnection(), run_hadd::cmd, DBHost(), DBName(), dirName, allTimeWatchdog::endl, ExistsInDB(), fCol, fConnection, fDBCacheFile, fExistsInDB, fHasConnection, fHasValidXML, fIgnoreEnvVar, fSchema, fTable, fTestedXML, fVerbosity, fXMLOverDB, GetConnection(), cet::getenv(), MECModelEnuComparisons::i, calib::j, findDuplicateFiles::key, Name(), Reset(), nova::database::dBColumn_t::setColType(), SetDBHost(), SetDBName(), SetDBPort(), nova::database::dBColumn_t::setName(), nova::database::dBColumn_t::setNull(), SetUser(), string, and XMLCol.

Referenced by dbutils::hw::Util::PositionTable(), and TableType().

107 {
108  std::string errStr;
109 
110  if (!tableName) {
111  errStr = "Table::CreateFromDB: missing table name!";
112  throw std::runtime_error(errStr);
113  }
114 
115  if (!schemaName) {
116  errStr = "Table::CreateFromDB: missing table schema (eg, detector name)!";
117  throw std::runtime_error(errStr);
118  }
119 
120  if (dbname) SetDBName(dbname);
121  if (DBName() == "") {
122  errStr = "Table::CreateFromDB: missing database name!";
123  throw std::runtime_error(errStr);
124  }
125 
126  if (dbhost) SetDBHost(dbhost);
127  if (DBHost() == "") {
128  errStr = "Table::CreateFromDB: missing database host!";
129  throw std::runtime_error(errStr);
130  }
131 
132  if (dbport) SetDBPort(dbport);
133  if (dbuser) SetUser(dbuser);
134 
135  fTable.reset(new dBTable());
136  fTable->setTableName(tableName);
137  fSchema = std::string(schemaName);
138  boost::to_lower(fSchema);
139 
140  std::string stName = fSchema + std::string(".") + std::string(tableName);
141  fIgnoreEnvVar = true;
142 
143  if (!ExistsInDB()) {
144  errStr = "Table::CreateFromDB: table \'" + stName + "\' not found in database!";
145  throw std::runtime_error(errStr);
146  }
147 
148  Reset();
149  fCol.clear();
150  fXMLOverDB = false;
151 
152  bool hasConn = fHasConnection;
153  if (! fHasConnection) {
154  GetConnection();
155  hasConn = false;
156  }
157 
158  std::vector<std::string> pkeyList;
160  // get list of rows that are primary keys
161  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";
162 
163  PGresult* res = PQexec(fConnection,cmd.c_str());
164 
165  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
166  if (fVerbosity > 0)
167  std::cerr << "Table::CreateFromDB command failed: "
168  << PQerrorMessage(fConnection) << std::endl;
169  PQclear(res);
170  fExistsInDB = false; // set this to false just in case
171  CloseConnection();
172  return false;
173  }
174 
175  int nRow = PQntuples(res);
176  if (nRow == 0) {
177  std::cerr << "Table::CreateFromDB: no primary keys defined for table \'" << tableName << "\', unable to proceed." << std::endl;
178  PQclear(res);
179  fExistsInDB = false;
180  CloseConnection();
181  return false;
182  }
183 
184  for (int i=0; i<nRow; ++i) {
185  std::string key = string(PQgetvalue(res,i,0));
186  pkeyList.push_back(key);
187  }
188 
189  PQclear(res);
190 
191  // now get names and types of all columns
192  cmd = "SELECT column_name, data_type from information_schema.columns where table_name = \'" + std::string(tableName) + "\' and table_schema=\'" + fSchema + "\'";
193  res = PQexec(fConnection,cmd.c_str());
194 
195  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
196  if (fVerbosity > 0)
197  std::cerr << "Table::CreateFromDB command failed: "
198  << PQerrorMessage(fConnection) << std::endl;
199  PQclear(res);
200  fExistsInDB = false; // set this to false just in case
201  CloseConnection();
202  return false;
203  }
204 
205  nRow = PQntuples(res);
206 
207  for (int i=0; i<nRow; ++i) {
208  std::string cname = std::string(PQgetvalue(res,i,0));
209  std::string ctype = std::string(PQgetvalue(res,i,1));
210  if (ctype == "smallint") ctype="short";
211  else if (ctype == "double precision") ctype="double";
212  else if (ctype == "boolean") ctype="bool";
213  else if (ctype == "timestamp without time zone") ctype="timestamp";
214  else if (ctype.substr(0,7) == "varchar")
215  ctype = "varchar" + ctype.substr(8,ctype.find(')')-1);
216 
217  // check if this column is "auto_incr"
218  if (ctype == "integer") {
219  cmd = "SELECT pg_get_serial_sequence(\'" + stName +
220  "\',\'" + cname + "\')";
221  PGresult* res2 = PQexec(fConnection,cmd.c_str());
222  int nRow2 = PQntuples(res2);
223  for (int j=0; j<nRow2; ++j) {
224  std::string tStr = std::string(PQgetvalue(res2,j,0));
225  if (tStr != "") ctype = "auto_incr";
226  }
227  PQclear(res2);
228  }
229 
230  // now create Column based on this info
231  dBColumn_t c;
232  c.setName(cname);
233  c.setColType(ctype);
234 
235  if (find(pkeyList.begin(),pkeyList.end(),cname) != pkeyList.end()) {
236  c.setNull(false);
237  pkey.addDBCol(dBColumn_t(c));
238  }
239  fTable->addDBColumn(dBColumn_t(c));
240  fCol.insert(fCol.begin(),Column(c));
241 
242  XMLCol[cname] = &(fTable->getDBColumnReference(i));
243 
244  if (cname == "inserttime") addInsertTime = true;
245  if (cname == "insertuser") addInsertUser = true;
246  if (cname == "updatetime") addUpdateTime = true;
247  if (cname == "updateuser") addUpdateUser = true;
248  }
249 
250  PQclear(res);
251 
252  if (!hasConn) CloseConnection();
253 
254  // now set the dB command cache file name
255  string dirName;
256  char* tmpStr = getenv("NOVADBCACHEDIR");
257  if (tmpStr)
258  dirName = tmpStr;
259  else {
260  tmpStr = getenv("PWD");
261  if (tmpStr)
262  dirName = tmpStr;
263  else
264  dirName = "/";
265  }
266 
267  fDBCacheFile = dirName + "/" + "." + Name() + ".cache";
268 
269  fTestedXML=true;
270  fHasValidXML=true;
271 
272  return true;
273 }
PGconn * fConnection
Definition: Table.h:309
void setColType(const colType_type &x)
std::vector< Column > fCol
Definition: Table.h:268
void SetDBName(std::string dbname)
Definition: Table.h:61
void addDBCol(const dBCol_type &newDBCol)
OStream cerr
Definition: OStream.cxx:7
void SetDBPort(std::string p)
Definition: Table.h:65
pg_result PGresult
Definition: Table.h:25
std::string fDBCacheFile
Definition: Table.h:286
string cmd
Definition: run_hadd.py:52
std::string getenv(std::string const &name)
const double j
Definition: BetheBloch.cxx:29
void SetUser(std::string uname)
Definition: Table.h:57
std::string DBHost()
Definition: Table.h:44
std::map< std::string, dBColumn_t * > XMLCol
Definition: Table.h:256
void setName(const name_type &x)
::std::unique_ptr< dBTable > fTable
Definition: Table.h:270
std::string dirName
Definition: PlotSpectra.h:47
void SetDBHost(std::string dbhost)
Definition: Table.h:63
std::string Name()
Definition: Table.h:42
std::string DBName()
Definition: Table.h:43
std::string fSchema
Definition: Table.h:284
void setNull(const null_type &x)
enum BeamMode string
void Table::CreateFromXML ( const char *  fname)
private

Definition at line 277 of file Table.cpp.

References _xsdLock, addInsertTime, addInsertUser, addUpdateTime, addUpdateUser, plot_validation_datamc::c, col, nova::database::dBTable_(), dirName, fCol, fDBCacheFile, fDBHost, fDBName, fIgnoreDB, novadaq::LocationUtils::findFile(), fMaxConnAttempts, plot_validation_datamc::fname, fPassword, fPKeyList, fRow, fSchema, fTable, fTableType, fXMLOverDB, GetCol(), cet::getenv(), MECModelEnuComparisons::i, calib::j, nova::database::kGenericTable, nova::database::kHardwareTable, nova::database::kNTableType, nova::database::kValidityTable, Name(), r(), nova::database::dBColumn_t::setColType(), nova::database::dBColumn_t::setName(), nova::database::dBColumn_t::setNull(), SetTableType(), datagram_client::sl, string, TableType(), and XMLCol.

Referenced by Table().

278 {
280  std::string errStr;
281  fPassword="";
282 
283  if (! novadaq::LocationUtils::findFile(tname, fname)) {
284  errStr = "Table::CreateFromXML: unable to find the file " + string(tname);
285  throw std::runtime_error(errStr);
286  }
287 
288  try {
289  // 10-Aug-2011, KAB - added class-wide locking of this section
290  // of code to prevent multiple threads from reading in a table
291  // definition file at the same time. Without this, it's possible
292  // for the code to crash since the Xerces-C library (which is
293  // used by the XSD code) is not thread safe.
294  boost::mutex::scoped_lock sl(_xsdLock);
295 
296  fTable = dBTable_(fname.c_str(),::xml_schema::flags::dont_validate);
297  }
298  catch (xsd::cxx::tree::parsing<char>& excpt) {
299  errStr = "Table::CreateFromXML: " + string(excpt.what());
300  throw std::runtime_error(errStr);
301  }
302  catch (xsd::cxx::tree::expected_element<char>& excpt) {
303  errStr = "Table::CreateFromXML: " + string(excpt.what());
304  throw std::runtime_error(errStr);
305  }
306 
307  // convert table name to all lower case for convenience
308  std::string tName = this->Name();
309  boost::to_lower(tName);
310  fTable->setTableName(tName);
311 
312  // set the DB host and name
313  fDBHost = std::string(fTable->getDbHost());
314  fDBName = std::string(fTable->getDbName());
315 
316  // set the table type. By default, a table is of type "generic",
317  // eg. no default validity context
318  if (fTable->hasTableType()) {
319  std::string ttype = fTable->getTableType();
320  boost::to_lower(ttype);
321  int ittype = kNTableType;
322  if (ttype == "generic") ittype = kGenericTable;
323  if (ttype == "hardware") ittype = kHardwareTable;
324  if (ttype == "validity") ittype = kValidityTable;
325  if (! this->SetTableType(ittype)) {
326  errStr = "Table::CreateFromXML: " + fTable->getTableName() + " unrecognized table type " + ttype;
327  throw std::runtime_error(errStr);
328  }
329  }
330 
331  if (fTable->hasSchema()) {
332  fSchema = std::string(fTable->getSchema());
333  boost::to_lower(fSchema);
334  }
335 
336  if (! fTable->hasDBPKey() && fTableType != kValidityTable ) {
337  errStr = "Table::CreateFromXML: " + fTable->getTableName() + " has no primary key defined";
338  throw std::runtime_error(errStr);
339  }
340 
341  // now add columns where necessary
342  dBColumn_t c;
343 
345  if (fTable->hasDBPKey())
346  pkey = dBTable::dBPKey_type(fTable->getDBPKey());
347 
348  fTable->setDBPKey(pkey);
349 
350  addInsertTime = false;
351  if (fTable->hasAddInsertTime())
352  addInsertTime = fTable->getAddInsertTime();
353 
354  if (addInsertTime) {
355  c.setNull(false);
356  c.setName("inserttime");
357  c.setColType("timestamp");
358  fTable->addDBColumn(dBColumn_t(c));
359  addInsertTime = true;
360  }
361 
362  addInsertUser = false;
363  if (fTable->hasAddInsertUser())
364  addInsertUser = fTable->getAddInsertUser();
365  if (addInsertUser) {
366  c.setNull(false);
367  c.setName("insertuser");
368  c.setColType("string");
369  fTable->addDBColumn(dBColumn_t(c));
370  addInsertUser = true;
371  }
372 
373  addUpdateTime = false;
374  if (fTable->hasAddUpdateTime())
375  addUpdateTime = fTable->getAddUpdateTime();
376  if (addUpdateTime) {
377  c.setNull(true);
378  c.setName("updatetime");
379  c.setColType("timestamp");
380  fTable->addDBColumn(dBColumn_t(c));
381  addUpdateTime = true;
382  }
383 
384  addUpdateUser = false;
385  if (fTable->hasAddUpdateUser())
386  addUpdateUser = fTable->getAddUpdateUser();
387  if (addUpdateUser) {
388  c.setNull(true);
389  c.setName("updateuser");
390  c.setColType("string");
391  fTable->addDBColumn(dBColumn_t(c));
392  addUpdateUser = true;
393  }
394 
395  // add necessary columns for Validity type tables (channel and timestamp)
396  if (TableType() == kValidityTable) {
397  c.setName("channel");
398  c.setColType("int");
399  fTable->addDBColumn(dBColumn_t(c));
400  c.setName("tv");
401  // c.setColType("timestamp");
402  c.setColType("long");
403  fTable->addDBColumn(dBColumn_t(c));
404  }
405 
406  fXMLOverDB = false;
407  if (fTable->hasXmlOverDB())
408  fXMLOverDB = fTable->getXmlOverDB();
409 
410  fCol.clear();
411 
412  for (int i=0; i<fTable->getDBColumnCount(); ++i) {
413  Column col(fTable->getDBColumn(i));
414  string cName = col.Name(); // convert name to all lower case
415  boost::to_lower(cName);
416  col.SetName(cName);
417  fCol.push_back(col);
418  XMLCol[cName] = &(fTable->getDBColumnReference(i));
419  }
420 
421  fPKeyList.clear();
422  if (fTable->hasDBPKey()) {
423  int npkey = fTable->getDBPKey().getDBColCount();
424  for (int j=0; j<npkey; ++j) {
425  string pkey = fTable->getDBPKey().getDBCol(j).getName();
426  if (this->GetCol(pkey))
427  fPKeyList.push_back(this->GetCol(pkey));
428  }
429  }
430 
431  fRow.clear();
432  for (int i=0; i<fTable->getDBRowCount(); ++i) {
433  Row r(fCol, fTable->getDBRow(i));
434  fRow.push_back(r);
435  }
436 
437  fIgnoreDB = false;
438  if (fTable->hasIgnoreDB())
439  fIgnoreDB = fTable->getIgnoreDB();
440 
441  // GetConnectionInfo();
442 
443  char* tmpStr = getenv("NOVADBNCNXTRIES");
444  if (tmpStr) {
445  int nTry = atoi(tmpStr);
446  if (nTry > 0)
447  fMaxConnAttempts = nTry;
448  }
449 
450  // now set the dB command cache file name
451  string dirName;
452  tmpStr = getenv("NOVADBCACHEDIR");
453  if (tmpStr)
454  dirName = tmpStr;
455  else {
456  tmpStr = getenv("PWD");
457  if (tmpStr)
458  dirName = tmpStr;
459  else
460  dirName = "/";
461  }
462 
463  fDBCacheFile = dirName + "/" + "." + Name() + ".cache";
464 
465 }
const Column * GetCol(int i)
Definition: Table.h:108
void setColType(const colType_type &x)
std::vector< Column > fCol
Definition: Table.h:268
static boost::mutex _xsdLock
Definition: Table.h:313
std::vector< Row > fRow
Definition: Table.h:269
static bool findFile(const std::string &filePath, std::string &locationOfFoundInstance)
std::string fDBHost
Definition: Table.h:282
std::string fDBCacheFile
Definition: Table.h:286
bool SetTableType(int t)
Definition: Table.cpp:469
Int_t col[ntarg]
Definition: Style.C:29
std::string getenv(std::string const &name)
const double j
Definition: BetheBloch.cxx:29
std::map< std::string, dBColumn_t * > XMLCol
Definition: Table.h:256
void setName(const name_type &x)
std::string fPassword
Definition: Table.h:287
::std::unique_ptr< dBTable > fTable
Definition: Table.h:270
std::string dirName
Definition: PlotSpectra.h:47
::std::unique_ptr< ::nova::database::dBTable > dBTable_(const ::std::string &uri,::xml_schema::flags f=0, const ::xml_schema::properties &p=::xml_schema::properties())
TRandom3 r(0)
std::vector< const Column * > fPKeyList
Definition: Table.h:292
std::string fDBName
Definition: Table.h:283
std::string Name()
Definition: Table.h:42
std::string fSchema
Definition: Table.h:284
void setNull(const null_type &x)
enum BeamMode string
std::string nova::database::Table::DBHost ( )
inline

Definition at line 44 of file Table.h.

References fDBHost.

Referenced by CreateFromDB().

44 { return fDBHost;}
std::string fDBHost
Definition: Table.h:282
std::string nova::database::Table::DBName ( )
inline

Definition at line 43 of file Table.h.

References fDBName.

Referenced by CreateFromDB().

43 { return fDBName;}
std::string fDBName
Definition: Table.h:283
std::string nova::database::Table::DBPort ( )
inline

Definition at line 47 of file Table.h.

References fDBPort.

47 { return fDBPort; }
std::string fDBPort
Definition: Table.h:281
void nova::database::Table::DisablePasswordAccess ( )
inline

Definition at line 74 of file Table.h.

References fPassword.

74 { fPassword = ""; }
std::string fPassword
Definition: Table.h:287
bool nova::database::Table::ExecuteSQL ( std::string  cmd,
PGresult *&  res 
)

Referenced by GetCol(), and GetCurrSeqVal().

bool Table::ExistsInDB ( )

Definition at line 1667 of file Table.cpp.

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

Referenced by BulkInsertInDB(), CheckValidXMLDef(), CreateFromDB(), GetCol(), LoadFromDB(), and WriteToDB().

1668 {
1669  if (fIgnoreDB) return false;
1670 
1671  if (fTestedExists) return fExistsInDB;
1672 
1673  std::string tname = this->Name();
1674 
1675  fTestedExists = true;
1676 
1677  bool hasConn = fHasConnection;
1678  if (! fHasConnection) {
1679  GetConnection();
1680  hasConn = false;
1681  }
1682 
1683  std::string cmd = "SELECT tablename FROM pg_tables WHERE schemaname=\'" +
1684  fSchema + "\'";
1685  // std::cout << tname << ": " << cmd << std::endl;
1686  PGresult* res = PQexec(fConnection,cmd.c_str());
1687 
1688  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
1689  if (fVerbosity > 0)
1690  std::cerr << "Table::ExistsInDB command failed: "
1691  << PQerrorMessage(fConnection) << std::endl;
1692  PQclear(res);
1693  fExistsInDB = false; // set this to false just in case
1694  CloseConnection();
1695  return false;
1696  }
1697 
1698  bool retVal = false;
1699  int nRow = PQntuples(res);
1700 
1701  for (int i=0; i<nRow; ++i) {
1702  // std::cout << string(PQgetvalue(res,i,0)) << std::endl;
1703 
1704  if (string(PQgetvalue(res, i, 0)) == tname) {
1705  retVal = true;
1706  break;
1707  }
1708  }
1709 
1710  PQclear(res);
1711 
1712  if (!hasConn) CloseConnection();
1713 
1714  fExistsInDB = true;
1715  return retVal;
1716 
1717 }
PGconn * fConnection
Definition: Table.h:309
OStream cerr
Definition: OStream.cxx:7
pg_result PGresult
Definition: Table.h:25
string cmd
Definition: run_hadd.py:52
std::string Name()
Definition: Table.h:42
std::string fSchema
Definition: Table.h:284
enum BeamMode string
std::string nova::database::Table::GetCheckConstraint ( int  i)
inline

Definition at line 185 of file Table.h.

References fTable, and string.

185  {
186  return std::string(fTable->getDBCheck(i).getConstraint());
187  }
::std::unique_ptr< dBTable > fTable
Definition: Table.h:270
enum BeamMode string
const Column* nova::database::Table::GetCol ( int  i)
inline

Definition at line 108 of file Table.h.

References fCol, MECModelEnuComparisons::i, and string.

Referenced by CreateFromXML(), GetCol(), PrintColumns(), RemoveDistinctColumn(), RemoveOrderColumn(), and SetDBInfo().

108 {return &fCol[i]; }
std::vector< Column > fCol
Definition: Table.h:268
const Column* nova::database::Table::GetCol ( std::string cname)
const Column* nova::database::Table::GetCol ( const char *  cname)
inline

Definition at line 110 of file Table.h.

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

111  { std::string cstr(cname); return GetCol(cstr); }
const Column * GetCol(int i)
Definition: Table.h:108
enum BeamMode string
std::string nova::database::Table::GetColName ( int  i)
inline

Definition at line 106 of file Table.h.

References fCol, and MECModelEnuComparisons::i.

106 {return fCol[i].Name(); }
std::vector< Column > fCol
Definition: Table.h:268
vector< string > Table::GetColNames ( )

Definition at line 611 of file Table.cpp.

References fCol, and MECModelEnuComparisons::i.

Referenced by NewRow().

612 {
613  vector<string> nameList;
614 
615  for (unsigned int i=0; i<fCol.size(); ++i)
616  nameList.push_back(fCol[i].Name());
617 
618  return nameList;
619 }
std::vector< Column > fCol
Definition: Table.h:268
bool Table::GetConnection ( )

Definition at line 1550 of file Table.cpp.

References om::cerr, CloseConnection(), run_hadd::cmd, om::cout, e, allTimeWatchdog::endl, stan::math::exp(), fConnection, fDBHost, fDBName, fDBPort, fHasConnection, fIgnoreDB, fMaxConnAttempts, fPassword, fUser, fVerbosity, GetConnectionInfo(), cet::getenv(), makeTrainCVSamples::int, and SetPasswordFile().

Referenced by BulkInsertInDB(), CheckValidXMLDef(), CreateFromDB(), ExistsInDB(), GetCurrSeqVal(), LoadFromDB(), RemoveValidityRange(), and WriteToDB().

1551 {
1552  if (fIgnoreDB) return false;
1553 
1554  try {
1556  }
1557  catch (std::runtime_error& e) {
1558  std::cerr << e.what() << std::endl;
1559  return false;
1560  }
1561 
1562  // now get the password file name for read-only access
1563  // (nova_reader acct only)
1564  if (fUser == "nova_reader" || fUser == "nova_grid")
1565  if (fPassword == "") {
1566  char* tmpStr = getenv("NOVADBPWDFILE");
1567  if (tmpStr)
1568  if (! this->SetPasswordFile(tmpStr)) {
1569  return false;
1570  }
1571  }
1572 
1573  if (!fConnection) {
1574  string cmd = "dbname = " + fDBName + " host = " + fDBHost + " user = " + fUser;
1575  if (fDBPort != "")
1576  cmd += " port = " + fDBPort;
1577 
1578  if (fPassword != "")
1579  cmd += " password = " + fPassword;
1580 
1581  fConnection = PQconnectdb(cmd.c_str());
1582 
1583  int nTry=0;
1584  int sleepTime = 2;
1585  while (PQstatus(fConnection) != CONNECTION_OK &&
1586  ++nTry<fMaxConnAttempts) {
1587  std::cerr << "Connection to " << fDBHost << ":"
1588  << fDBName << " failed: "
1589  << PQerrorMessage(fConnection) << std::endl;
1590 
1591  CloseConnection();
1592  fConnection = PQconnectdb(cmd.c_str());
1593  sleep(sleepTime);
1594  sleepTime = int(60./(1+29*exp(-0.5*nTry)));
1595  }
1596  if (nTry == fMaxConnAttempts) {
1597  std::cerr << "Too many attempts to connect to " << fDBHost
1598  << ", giving up." << std::endl;
1599  CloseConnection();
1600  return false;
1601  }
1602  fHasConnection = true;
1603  if (fVerbosity > 0)
1604  std::cout << "Got new connection" << std::endl;
1605  }
1606 
1607  return true;
1608 }
bool SetPasswordFile(const char *fname=0)
fname should be the name of the file
Definition: Table.cpp:1633
PGconn * fConnection
Definition: Table.h:309
OStream cerr
Definition: OStream.cxx:7
std::string fDBPort
Definition: Table.h:281
std::string fDBHost
Definition: Table.h:282
string cmd
Definition: run_hadd.py:52
std::string getenv(std::string const &name)
std::string fUser
Definition: Table.h:279
fvar< T > exp(const fvar< T > &x)
Definition: exp.hpp:10
OStream cout
Definition: OStream.cxx:6
std::string fPassword
Definition: Table.h:287
bool GetConnectionInfo()
Definition: Table.cpp:1446
std::string fDBName
Definition: Table.h:283
Float_t e
Definition: plot.C:35
bool Table::GetConnectionInfo ( )
private

Definition at line 1446 of file Table.cpp.

References om::cerr, om::cout, allTimeWatchdog::endl, fDBHost, fDBName, fDBPort, fDetector, fIgnoreEnvVar, fTable, fTableType, fUser, cet::getenv(), novadaq::cnv::DetInfo::GetId(), nova::database::kDAQTable, nova::database::kDCSTable, novadaq::cnv::kFARDET, nova::database::kHardwareTable, novadaq::cnv::kNEARDET, and string.

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

1447 {
1448  char* tmpStr;
1449 
1450  if (!fIgnoreEnvVar) {
1451  // now check environment variables, which will override any previous settings
1452  if (fTableType == kHardwareTable) {
1453  tmpStr = getenv("NOVAHWDBHOST");
1454  if (tmpStr)
1455  fDBHost = tmpStr;
1456  tmpStr = getenv("NOVAHWDBNAME");
1457  if (tmpStr)
1458  fDBName = tmpStr;
1459  tmpStr = getenv("NOVAHWDBPORT");
1460  if (tmpStr)
1461  fDBPort = tmpStr;
1462  tmpStr = getenv("NOVAHWDBUSER");
1463  if (tmpStr)
1464  fUser = tmpStr;
1465  }
1466  else {
1467  tmpStr = getenv("NOVADBHOST");
1468  if (tmpStr)
1469  fDBHost = tmpStr;
1470  tmpStr = getenv("NOVADBNAME");
1471  if (tmpStr)
1472  fDBName = tmpStr;
1473  tmpStr = getenv("NOVADBPORT");
1474  if (tmpStr)
1475  fDBPort = tmpStr;
1476  tmpStr = getenv("NOVADBUSER");
1477  if (tmpStr)
1478  fUser = tmpStr;
1479  }
1480  }
1481 
1482  if (fTableType == kDAQTable || fTableType == kDCSTable) {
1483  std::string bVar = "NOVAXXXX";
1486  if (fTableType == kDAQTable)
1487  bVar = "NOVAFARDAQDB";
1488  else
1489  bVar = "NOVAFARDCSDB";
1490  }
1493  if (fTableType == kDAQTable)
1494  bVar = "NOVANEARDAQDB";
1495  else
1496  bVar = "NOVANEARDCSDB";
1497  }
1498  std::string envVar = bVar + std::string("HOST");
1499  tmpStr = getenv(envVar.c_str());
1500  if (tmpStr)
1501  fDBHost = tmpStr;
1502 
1503  envVar = bVar + std::string("NAME");
1504  tmpStr = getenv(envVar.c_str());
1505  if (tmpStr)
1506  fDBName = tmpStr;
1507  envVar = bVar + std::string("PORT");
1508  tmpStr = getenv(envVar.c_str());
1509  if (tmpStr)
1510  fDBPort = tmpStr;
1511 
1512  envVar = bVar + std::string("USER");
1513  tmpStr = getenv(envVar.c_str());
1514  if (tmpStr)
1515  fUser = tmpStr;
1516 
1517  }
1518 
1519  std::cout << "Connecting with: " << fDBHost << ":" << fDBPort
1520  << ":" << fDBName << ":" << fUser << std::endl;
1521 
1522  if (fUser == "") {
1523  tmpStr = getenv("USER");
1524  if (tmpStr) {
1525  fUser = tmpStr;
1526  std::cerr << "Table::GetConnectionInfo: DB User undefined. Setting to \""
1527  << fUser << "\"" << std::endl;
1528  }
1529  else {
1530  fUser = "nova_reader";
1531  std::cerr << "Table::GetConnectionInfo: DB User undefined. "
1532  << "Setting to \"nova_reader\"" << std::endl;
1533  }
1534  }
1535  if (fDBHost == "") {
1536  throw std::runtime_error("Table::GetConnectionInfo: DB HOST undefined.");
1537  }
1538  if (fDBName == "") {
1539  throw std::runtime_error("Table::GetConnectionInfo: DB NAME undefined.");
1540  }
1541 
1542  if (fDBPort == "")
1543  if (fTable->hasDbPort())
1544  fDBPort = fTable->getDbPort();
1545 
1546  return true;
1547 }
OStream cerr
Definition: OStream.cxx:7
std::string fDBPort
Definition: Table.h:281
std::string fDBHost
Definition: Table.h:282
Far Detector at Ash River, MN.
std::string getenv(std::string const &name)
std::string fUser
Definition: Table.h:279
Near Detector in the NuMI cavern.
OStream cout
Definition: OStream.cxx:6
static int GetId(std::string name)
std::string fDetector
Definition: Table.h:311
::std::unique_ptr< dBTable > fTable
Definition: Table.h:270
std::string fDBName
Definition: Table.h:283
enum BeamMode string
bool Table::GetCurrSeqVal ( std::string  col,
int64_t &  iseq 
)

Definition at line 1720 of file Table.cpp.

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

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

1721 {
1722  if (fIgnoreDB) return false;
1723 
1724  bool hasConn = fHasConnection;
1725  if (! fHasConnection) {
1726  GetConnection();
1727  hasConn = false;
1728  }
1729 
1730  // now get current sequence value:
1731 
1732  string cmd = "SELECT last_value FROM ";
1733  cmd += Schema() + "." + Name();
1734  cmd += "_" + col + "_seq";
1735 
1736  if (fVerbosity > 0)
1737  std::cerr << "Table::GetCurrSeqVal: Executing PGSQL command: \n\t"
1738  << cmd << std::endl;
1739 
1740  PGresult* res = PQexec(fConnection, cmd.c_str());
1741  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
1742  if (fVerbosity > 0)
1743  std::cerr << "SELECT failed: " << PQerrorMessage(fConnection) << std::endl;
1744  PQclear(res);
1745  return false;
1746  }
1747 
1748  // check that the number of columns is consistent
1749  if (PQnfields(res) != 1) {
1750  PQclear(res);
1751  return false;
1752  }
1753 
1754  // now cache rows
1755  int nRow = PQntuples(res);
1756 
1757  if (nRow != 1) {
1758  PQclear(res);
1759  return false;
1760  }
1761 
1762  if (! PQgetisnull(res,0,0)) {
1763  string vstr = PQgetvalue(res,0,0);
1764  try {
1765  iseq = boost::lexical_cast<long>(vstr);
1766  }
1767  catch (boost::bad_lexical_cast &) {
1768  PQclear(res);
1769  return false;
1770  }
1771  }
1772 
1773  PQclear(res);
1774 
1775  if (!hasConn) CloseConnection();
1776 
1777  return true;
1778 }
PGconn * fConnection
Definition: Table.h:309
OStream cerr
Definition: OStream.cxx:7
pg_result PGresult
Definition: Table.h:25
string cmd
Definition: run_hadd.py:52
std::string Schema()
Definition: Table.h:203
Int_t col[ntarg]
Definition: Style.C:29
std::string Name()
Definition: Table.h:42
bool nova::database::Table::GetCurrSeqVal ( const char *  col,
int64_t &  iseq 
)
inline

Definition at line 169 of file Table.h.

References GetCurrSeqVal(), and string.

170  { return GetCurrSeqVal(std::string(col), iseq); }
Int_t col[ntarg]
Definition: Style.C:29
bool GetCurrSeqVal(std::string col, int64_t &iseq)
Definition: Table.cpp:1720
enum BeamMode string
bool Table::GetDetector ( std::string det) const

Definition at line 557 of file Table.cpp.

References fDetector.

Referenced by SetValiditySQL().

558 {
559  if (fDetector == "") return false;
560  det = fDetector;
561 
562  return true;
563 }
std::string fDetector
Definition: Table.h:311
bool Table::GetDetector ( int idet) const

Definition at line 566 of file Table.cpp.

References om::cerr, allTimeWatchdog::endl, fDetector, novadaq::cnv::DetInfo::GetId(), and novadaq::cnv::kNDetector.

567 {
568  if (idet <= novadaq::cnv::kUNKNOWN_DET || idet >= novadaq::cnv::kNDetector) {
569  std::cerr << "Error in Table::GetDetector: Unknown detector id" << idet
570  << std::endl;
571  return false;
572  }
574 
575  return true;
576 }
OStream cerr
Definition: OStream.cxx:7
static int GetId(std::string name)
std::string fDetector
Definition: Table.h:311
int nova::database::Table::GetMaxConnAttempts ( )
inline

Definition at line 251 of file Table.h.

References CacheDBCommand(), CheckValidXMLDef(), fMaxConnAttempts, ResetConnectionInfo(), and string.

251 { return fMaxConnAttempts; }
int nova::database::Table::GetNCheck ( )
inline

Definition at line 184 of file Table.h.

References fTable.

184 { return fTable->getDBCheckCount(); }
::std::unique_ptr< dBTable > fTable
Definition: Table.h:270
int nova::database::Table::GetNPKeyCol ( )
inline

Definition at line 173 of file Table.h.

References fPKeyList.

173 { return fPKeyList.size(); }
std::vector< const Column * > fPKeyList
Definition: Table.h:292
int nova::database::Table::GetNUnique ( )
inline

Definition at line 176 of file Table.h.

References fTable.

Referenced by GetUniqueRow().

176 { return fTable->getDBUniqueCount(); }
::std::unique_ptr< dBTable > fTable
Definition: Table.h:270
const Column* nova::database::Table::GetPKeyCol ( int  i)
inline

Definition at line 174 of file Table.h.

References fPKeyList, and MECModelEnuComparisons::i.

Referenced by GetRowByPKey().

174 { return fPKeyList[i]; }
std::vector< const Column * > fPKeyList
Definition: Table.h:292
Row * Table::GetRow ( int  i)

Definition at line 804 of file Table.cpp.

References fRow.

Referenced by ClearRows(), dbutils::hw::FEB::GetAllInstalled(), dbutils::hw::APD::GetAllInstalled(), dbutils::hw::FEB::Installed(), and dbutils::hw::APD::Installed().

805 {
806  if (i >= 0 && i < (int)fRow.size())
807  return &fRow[i];
808  else
809  return 0;
810 }
std::vector< Row > fRow
Definition: Table.h:269
vector< Row * > const Table::GetRowByColSet ( std::vector< Column > &  cols)

Definition at line 838 of file Table.cpp.

References demo::app, CacheDBCommand(), run_hadd::cmd, allTimeWatchdog::endl, fCol, fDBCacheFile, submit_syst::fout, fRow, MECModelEnuComparisons::i, calib::j, Name(), and fillBadChanDBTables::rows.

Referenced by ClearRows().

839 {
840  vector<Row*> rows(0);
841 
842  vector<unsigned int> colidx(cols.size());
843  vector<string> colval(cols.size());
844 
845  // create index to cols
846  for (unsigned int i=0; i<cols.size(); ++i) {
847  string colStr = cols[i].Name();
848  for (unsigned int j=0; j<fCol.size(); ++j) {
849  if (fCol[j].Name() == colStr) {
850  colidx[i] = j;
851  cols[i].Get(colval[i]);
852  break;
853  }
854  }
855  }
856 
857  string vStr;
858  for (unsigned int i=0; i<fRow.size(); ++i) {
859  bool isMatch = true;
860  for (unsigned int j=0; j<cols.size(); ++j) {
861  unsigned int k=colidx[j];
862  if (fRow[i].Col(k).Value() != colval[j]) {
863  isMatch = false;
864  break;
865  }
866  }
867  if (isMatch) rows.push_back(&fRow[i]);
868  }
869 
870  return rows;
871 
872 }
std::vector< Column > fCol
Definition: Table.h:268
std::vector< Row > fRow
Definition: Table.h:269
const int cols[3]
const double j
Definition: BetheBloch.cxx:29
std::string Name()
Definition: Table.h:42
Row * Table::GetRowByPKey ( std::vector< std::string > &  pkeyval)

Definition at line 813 of file Table.cpp.

References om::cerr, allTimeWatchdog::endl, fPKeyList, fRow, GetPKeyCol(), MECModelEnuComparisons::i, calib::j, and Name().

Referenced by ClearRows().

814 {
815  if (pkeyval.size() != fPKeyList.size()) {
816  std::cerr << "Table::GetRowByPKey: too few pkeys provided!" << std::endl;
817  return 0;
818  }
819 
820  vector<string> pkeyname;
821  for (unsigned int i=0; i<fPKeyList.size(); ++i)
822  pkeyname.push_back(GetPKeyCol(i)->Name());
823 
824  for (unsigned int i=0; i<fRow.size(); ++i) {
825  unsigned int j=0;
826  for (; j<pkeyval.size(); ++j) {
827  if (fRow[i].Col(pkeyname[j])->Value() != pkeyval[j]) break;
828  }
829 
830  if (j == pkeyval.size()) {
831  return &fRow[i];
832  }
833  }
834  return 0;
835 }
const Column * GetPKeyCol(int i)
Definition: Table.h:174
std::vector< Row > fRow
Definition: Table.h:269
OStream cerr
Definition: OStream.cxx:7
const double j
Definition: BetheBloch.cxx:29
std::vector< const Column * > fPKeyList
Definition: Table.h:292
std::string Name()
Definition: Table.h:42
const dBRow_t* nova::database::Table::GetUniqueRow ( int  i)
inline

Definition at line 177 of file Table.h.

References fTable, and GetNUnique().

177  {
178  if (fTable->hasDBUnique() &&
179  i < this->GetNUnique())
180  return &fTable->getDBUnique(i);
181  else return 0;
182  }
::std::unique_ptr< dBTable > fTable
Definition: Table.h:270
std::string nova::database::Table::GetValiditySQL ( )
inline

Definition at line 194 of file Table.h.

References fValiditySQL.

194 { return fValiditySQL; }
std::string fValiditySQL
Definition: Table.h:288
bool nova::database::Table::HasPKey ( )
inline

Definition at line 172 of file Table.h.

References fTable.

172 { return fTable->hasDBPKey(); }
::std::unique_ptr< dBTable > fTable
Definition: Table.h:270
bool Table::LoadFromCSV ( std::string  fname,
bool  update = false 
)

Definition at line 1177 of file Table.cpp.

References AddRow(), plot_validation_datamc::c, om::cerr, nova::database::Row::Col(), om::cout, allTimeWatchdog::endl, fCol, fin, novadaq::LocationUtils::findFile(), fValidityEnd, fValidityStart, MECModelEnuComparisons::i, calib::j, nova::database::Column::Name(), Name(), NewRow(), r(), nova::database::Column::Set(), nova::database::Column::SetNull(), getGoodRuns4SAM::tname, trim(), nova::database::Column::Type(), nova::database::Row::Update(), and nova::database::Column::Value().

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

1178 {
1179  string tname;
1180  if (! novadaq::LocationUtils::findFile(fname, tname)) {
1181  std::cerr << "Cannot find " << fname << std::endl;
1182  return false;
1183  }
1184 
1185  std::cout << "Reading " << tname << std::endl;
1186 
1187  ifstream fin;
1188  fin.open(tname.c_str());
1189  if (!fin.is_open() || !fin.good()) {
1190  std::cerr << "Could not open " << tname << std::endl;
1191  return false;
1192  }
1193 
1194  unsigned int nReqCol = 0;
1195  for (unsigned int j=0; j<fCol.size(); ++j)
1196  if (fCol[j].Name() == "inserttime" ||
1197  fCol[j].Name() == "insertuser" ||
1198  fCol[j].Name() == "updatetime" ||
1199  fCol[j].Name() == "updateuser" ) continue;
1200  else
1201  nReqCol++;
1202 
1203  using namespace boost;
1204 
1205  string s;
1206  while (!fin.eof()) {
1207  getline(fin,s);
1208  if (fin.eof()) break;
1209 
1210  tokenizer<escaped_list_separator<char> > tok(s);
1211  tokenizer<escaped_list_separator<char> >::iterator itr=tok.begin();
1212  tokenizer<escaped_list_separator<char> >::iterator itrEnd=tok.end();
1213 
1214  unsigned int sz = 0;
1215  for (; itr != itrEnd; ++itr) ++sz;
1216 
1217  itr = tok.begin();
1218 
1219  // require the correct number of commas in the line if we are to
1220  // insert it into the table
1221 
1222  if (sz >= nReqCol && sz <= fCol.size()) {
1223  Row* r = NewRow();
1224  for (int j=0; itr != itrEnd; ++itr,++j) {
1225  string value = *itr;
1226  boost::trim(value);
1227  if (value.find("x") != std::string::npos) {
1228  if (r->Col(j).Type() == "bigint" ||
1229  r->Col(j).Type() == "long") {
1230  try {
1231  std::istringstream iss(value);
1232  uint64_t ulongValue;
1233  iss >> std::hex >> ulongValue;
1234  int64_t longValue = (int64_t) ulongValue;
1235  value = boost::lexical_cast<string>(longValue);
1236  }
1237  catch (...) {
1238  // simply let "value" remain unchanged
1239  }
1240  }
1241  else if (r->Col(j).Type() == "int") {
1242  try {
1243  std::istringstream iss(value);
1244  uint32_t uintValue;
1245  iss >> std::hex >> uintValue;
1246  int32_t intValue = (int32_t) uintValue;
1247  value = boost::lexical_cast<string>(intValue);
1248  }
1249  catch (...) {
1250  // simply let "value" remain unchanged
1251  }
1252  }
1253  else if (r->Col(j).Type() == "short") {
1254  try {
1255  std::istringstream iss(value);
1256  uint16_t ushortValue;
1257  iss >> std::hex >> ushortValue;
1258  int16_t shortValue = (int16_t) ushortValue;
1259  value = boost::lexical_cast<string>(shortValue);
1260  }
1261  catch (...) {
1262  // simply let "value" remain unchanged
1263  }
1264  }
1265  }
1266 
1267  bool isAutoIncr = (r->Col(j).Type() == "autoincr");
1268  if (value.empty()) {
1269  r->Col(j).SetNull(true);
1270  value = "NULL";
1271  }
1272  if (value == "\"")
1273  value = "";
1274 
1275  if (!update)
1276  r->Col(j).Set(value,isAutoIncr);
1277  else{
1278  string name = r->Col(j).Name();
1279  r->Update(name,value);
1280  }
1281  }
1282 
1283  bool isValid = true;
1284  if (! fValidityStart.empty()) {
1285  for (unsigned int i=0; i<fValidityStart.size(); ++i) {
1286  const Column* c = r->Col(fValidityStart[i].Name());
1287  if (fValidityStart[i].Type() == "bool")
1288  isValid = (c->Value() == fValidityStart[i].Value());
1289  else
1290  isValid = ((*c >= fValidityStart[i]) && (*c <= fValidityEnd[i]));
1291  if (!isValid) break;
1292  }
1293  }
1294 
1295  if (isValid) AddRow(r);
1296  delete r;
1297  }
1298  else {
1299  std::cerr << "Table::LoadFromCSV: ignoring line, wrong number of entries, "
1300  << "expected at least " << nReqCol << ", found " << sz << "."
1301  << std::endl;
1302  }
1303  }
1304 
1305  fin.close();
1306 
1307  return true;
1308 }
TString fin
Definition: Style.C:24
const XML_Char * name
Definition: expat.h:151
bool Set(const T &val, bool ignoreAutoIncr=false)
Definition: Column.h:70
std::vector< Column > fCol
Definition: Table.h:268
OStream cerr
Definition: OStream.cxx:7
string trim(string in)
Definition: rootgINukeVal.C:65
static bool findFile(const std::string &filePath, std::string &locationOfFoundInstance)
bool Update(std::string cname, T value)
Definition: Row.h:37
const XML_Char * s
Definition: expat.h:262
const XML_Char int const XML_Char * value
Definition: expat.h:331
std::vector< Column > fValidityEnd
Definition: Table.h:291
std::string Value() const
Definition: Column.h:37
const double j
Definition: BetheBloch.cxx:29
void AddRow(const Row *row)
Definition: Table.cpp:727
OStream cout
Definition: OStream.cxx:6
std::string Name() const
Definition: Column.h:35
TRandom3 r(0)
void SetNull(bool isNull)
Definition: Column.h:48
std::string Name()
Definition: Table.h:42
std::vector< Column > fValidityStart
Definition: Table.h:290
std::string Type() const
Definition: Column.h:36
Column & Col(int i)
Definition: Row.h:51
bool nova::database::Table::LoadFromCSV ( const char *  fname,
bool  update = false 
)
inline

Definition at line 122 of file Table.h.

References LoadFromCSV(), string, and UpdateFromCSV().

123  { return LoadFromCSV(std::string(fname)); }
bool LoadFromCSV(std::string fname, bool update=false)
Definition: Table.cpp:1177
enum BeamMode string
bool Table::LoadFromDB ( )

Definition at line 968 of file Table.cpp.

References AddRow(), om::cerr, CheckValidXMLDef(), nova::database::Row::Clear(), CloseConnection(), run_hadd::cmd, nova::database::Row::Col(), allTimeWatchdog::endl, ExistsInDB(), fCol, fConnection, fDescOrder, fDistinctCol, fHasConnection, fIgnoreDB, fOrderCol, fSchema, fSelectLimit, fSelectOffset, fValidityChanged, fValidityEnd, fValiditySQL, fValidityStart, fVerbosity, GetConnection(), MECModelEnuComparisons::i, calib::j, Name(), NewRow(), check_grl::row, Schema(), nova::database::Column::Set(), nova::database::Row::SetInDB(), nova::database::Column::SetNull(), and nova::database::Column::Type().

Referenced by comi::CosmicMetrics::endJob(), comi::DataCheck::endJob(), dbutils::hw::FEB::GetAllInstalled(), dbutils::hw::APD::GetAllInstalled(), dbutils::hw::FEB::Installed(), dbutils::hw::APD::Installed(), and UpdateFromSSV().

969 {
970  if (fIgnoreDB) return false;
971 
972  if (fSchema == "undef") {
973  std::cerr << "Table::LoadFromDB: Detector not set! Table::SetDetector()"
974  << " must be called first!" << std::endl;
975  return false;
976  }
977 
978  if (!fValidityChanged) return true;
979 
980  // make a connection to the dB if there isn't one already
981  bool hasConn = fHasConnection;
982  if (! fHasConnection) {
983  GetConnection();
984  hasConn = false;
985  }
986 
987  if (!fConnection) {
988  std::cerr << "Table::LoadFromDB: No connection to the database!" << std::endl;
989  return false;
990  }
991 
992  if (!ExistsInDB()) {
993  std::cerr << "Table::LoadFromDB: Table \"" << Name()
994  << "\" not found in database!" << std::endl;
995  CloseConnection();
996  return false;
997  }
998 
999  if (! CheckValidXMLDef()) {
1000  std::cerr << "Table::LoadFromDB: Found mismatch between XML table definition ";
1001  std::cerr << "file and table defintion from the database for table "
1002  << Name() << "! Aborting read..." << std::endl;
1003  return false;
1004  }
1005 
1006  string cmd;
1007  cmd.clear();
1008  PGresult* res;
1009 
1010  /*
1011  cmd = "SET search_path TO " + fSchema;
1012  res = PQexec(fConnection, cmd.c_str());
1013  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
1014  std::cerr << "\'" << cmd << "\' command failed" << std::endl;
1015  PQclear(res);
1016  CloseConnection();
1017  return false;
1018  }
1019  PQclear(res);
1020  cmd.clear();
1021  */
1022 
1023  std::ostringstream outs;
1024  outs << "BEGIN";
1025  res = PQexec(fConnection, outs.str().c_str());
1026  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
1027  std::cerr << "BEGIN command failed: " << PQerrorMessage(fConnection) << std::endl;
1028  PQclear(res);
1029  CloseConnection();
1030  return false;
1031  }
1032 
1033  PQclear(res);
1034 
1035  outs.str("");
1036  outs << "DECLARE myportal CURSOR FOR SELECT ";
1037  if (!fDistinctCol.empty()) {
1038  outs << "DISTINCT ON (";
1039  if (! fDistinctCol.empty()) {
1040  for (unsigned int i=0; i<fDistinctCol.size(); ++i) {
1041  outs << fDistinctCol[i]->Name();
1042  if (i<(fDistinctCol.size()-1)) outs << ", ";
1043  }
1044  }
1045  outs << ") ";
1046  }
1047 
1048  outs << "* from ";
1049  outs << Schema() << "." << Name();
1050 
1051  if (! fValidityStart.empty() || fValiditySQL != "" ) {
1052  outs << " WHERE " << fValiditySQL;
1053  if (fValiditySQL != "" && !fValidityStart.empty()) outs << " and ";
1054 
1055  for (unsigned int i=0; i<fValidityStart.size(); ++i) {
1056  bool isEqualTo = (fValidityStart[i].Value() == fValidityEnd[i].Value());
1057 
1058  outs << fValidityStart[i].Name();
1059  if (!isEqualTo)
1060  outs << ">=";
1061  else
1062  outs << "=";
1063 
1064  outs << fValidityStart[i];
1065 
1066  if (!isEqualTo) {
1067  outs << " and ";
1068  outs << fValidityEnd[i].Name() + "<=";
1069  outs << fValidityEnd[i];
1070  }
1071 
1072  if (i < (fValidityStart.size()-1)) outs << " and ";
1073  }
1074  }
1075 
1076  if (!fDistinctCol.empty() || !fOrderCol.empty()) {
1077  outs << " ORDER BY ";
1078 
1079  if (!fDistinctCol.empty()) {
1080  for (unsigned int i=0; i<fDistinctCol.size(); ++i) {
1081  outs << fDistinctCol[i]->Name();
1082  if (i<(fDistinctCol.size()-1)) outs << ", ";
1083  }
1084  }
1085 
1086  if (!fOrderCol.empty()) {
1087  for (unsigned int i=0; i<fOrderCol.size(); ++i) {
1088  outs << fOrderCol[i]->Name();
1089  if (i<(fOrderCol.size()-1)) outs << ", ";
1090  }
1091  }
1092 
1093  if (fDescOrder)
1094  outs << " DESC";
1095  else
1096  outs << " ASC";
1097  }
1098 
1099  if (fSelectLimit>0) {
1100  outs << " LIMIT " << boost::lexical_cast<string>(fSelectLimit);
1101  }
1102 
1103  if (fSelectOffset>0) {
1104  outs << " OFFSET " << boost::lexical_cast<string>(fSelectOffset);
1105  }
1106 
1107  if (fVerbosity > 0)
1108  std::cerr << "Table::LoadFromDB: Executing PGSQL command: \n\t" << outs.str() << std::endl;
1109 
1110  res = PQexec(fConnection,outs.str().c_str());
1111 
1112  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
1113  std::cerr << "DECLARE CURSOR failed: " << PQerrorMessage(fConnection) << std::endl;
1114  PQclear(res);
1115  CloseConnection();
1116  return false;
1117  }
1118  PQclear(res);
1119 
1120  res = PQexec(fConnection, "FETCH ALL in myportal");
1121  if (PQresultStatus(res) != PGRES_TUPLES_OK) {
1122  std::cerr << "FETCH ALL failed: %" << PQerrorMessage(fConnection) << std::endl;
1123  PQclear(res);
1124  CloseConnection();
1125  return false;
1126  }
1127 
1128  // now cache rows
1129  int nRow = PQntuples(res);
1130  vector<int> colMap(fCol.size());
1131 
1132  for (unsigned int i=0; i<fCol.size(); ++i) {
1133  colMap[i] = PQfnumber(res,fCol[i].Name().c_str());
1134  }
1135 
1136  int k;
1137  Row* row = NewRow();
1138  row->SetInDB();
1139  std::vector<std::string> hashVec;
1140  for (int i=0; i < nRow; i++) {
1141  row->Clear();
1142  for (unsigned int j=0; j < fCol.size(); j++) {
1143  k = colMap[j];
1144  if (k >= 0) {
1145  if (! PQgetisnull(res,i,k)) {
1146  string vstr = PQgetvalue(res,i,k);
1147  bool isAutoIncr = (row->Col(j).Type() == "autoincr");
1148  row->Col(j).Set(vstr,isAutoIncr);
1149  }
1150  else
1151  row->Col(j).SetNull(true);
1152  }
1153  }
1154  AddRow(row);
1155  }
1156  delete row;
1157 
1158  PQclear(res);
1159 
1160  /* close the portal ... we don't bother to check for errors ... */
1161  res = PQexec(fConnection, "CLOSE myportal");
1162  PQclear(res);
1163 
1164  /* end the transaction */
1165  res = PQexec(fConnection, "END");
1166  PQclear(res);
1167 
1168  // close connection to the dB if necessary
1169  if (! hasConn) CloseConnection();
1170 
1171  fValidityChanged = false;
1172 
1173  return true;
1174 }
PGconn * fConnection
Definition: Table.h:309
bool Set(const T &val, bool ignoreAutoIncr=false)
Definition: Column.h:70
std::vector< Column > fCol
Definition: Table.h:268
void Clear()
Definition: Row.cpp:61
OStream cerr
Definition: OStream.cxx:7
std::vector< const Column * > fDistinctCol
Definition: Table.h:293
pg_result PGresult
Definition: Table.h:25
string cmd
Definition: run_hadd.py:52
std::string Schema()
Definition: Table.h:203
bool CheckValidXMLDef()
Definition: Table.cpp:889
std::vector< Column > fValidityEnd
Definition: Table.h:291
const double j
Definition: BetheBloch.cxx:29
void SetInDB()
Definition: Row.h:44
void AddRow(const Row *row)
Definition: Table.cpp:727
std::vector< const Column * > fOrderCol
Definition: Table.h:294
void SetNull(bool isNull)
Definition: Column.h:48
std::string fValiditySQL
Definition: Table.h:288
std::string Name()
Definition: Table.h:42
std::vector< Column > fValidityStart
Definition: Table.h:290
std::string Type() const
Definition: Column.h:36
std::string fSchema
Definition: Table.h:284
Column & Col(int i)
Definition: Row.h:51
bool Table::LoadFromSSV ( std::string  fname,
bool  update = false 
)

Definition at line 1318 of file Table.cpp.

References AddRow(), plot_validation_datamc::c, om::cerr, nova::database::Row::Col(), om::cout, allTimeWatchdog::endl, fCol, fin, novadaq::LocationUtils::findFile(), fValidityEnd, fValidityStart, MECModelEnuComparisons::i, calib::j, nova::database::Column::Name(), Name(), NewRow(), r(), nova::database::Column::Set(), getGoodRuns4SAM::tname, trim(), nova::database::Column::Type(), nova::database::Row::Update(), and nova::database::Column::Value().

Referenced by LoadFromSSV(), UpdateFromCSV(), and UpdateFromSSV().

1319 {
1320  string tname;
1321  if (! novadaq::LocationUtils::findFile(fname, tname)) {
1322  std::cerr << "Cannot find " << fname << std::endl;
1323  return false;
1324  }
1325 
1326  std::cout << "Reading " << tname << std::endl;
1327 
1328  ifstream fin;
1329  fin.open(tname.c_str());
1330  if (!fin.is_open() || !fin.good()) {
1331  std::cerr << "Could not open " << tname << std::endl;
1332  return false;
1333  }
1334 
1335  using namespace boost;
1336 
1337  char_separator<char>ws(" ");
1338 
1339  string s;
1340  while (!fin.eof()) {
1341  getline(fin,s);
1342  if (fin.eof()) break;
1343 
1344  tokenizer<char_separator<char> > tok(s,ws);
1345  tokenizer<char_separator<char> >::iterator itr=tok.begin();
1346  tokenizer<char_separator<char> >::iterator itrEnd=tok.end();
1347 
1348  unsigned int sz = 0;
1349  for (; itr != itrEnd; ++itr) {
1350  ++sz;
1351  }
1352 
1353  itr = tok.begin();
1354 
1355  // require the correct number of commas in the line if we are to
1356  // insert it into the table
1357  if (sz == fCol.size()) {
1358  Row* r = NewRow();
1359  for (int j=0; itr != itrEnd; ++itr,++j) {
1360  string value = *itr;
1361  boost::trim(value);
1362  if (value.find("x") != std::string::npos) {
1363  if (r->Col(j).Type() == "bigint" ||
1364  r->Col(j).Type() == "long") {
1365  try {
1366  std::istringstream iss(value);
1367  uint64_t ulongValue;
1368  iss >> std::hex >> ulongValue;
1369  int64_t longValue = (int64_t) ulongValue;
1370  value = boost::lexical_cast<string>(longValue);
1371  }
1372  catch (...) {
1373  // simply let "value" remain unchanged
1374  }
1375  }
1376  else if (r->Col(j).Type() == "int") {
1377  try {
1378  std::istringstream iss(value);
1379  uint32_t uintValue;
1380  iss >> std::hex >> uintValue;
1381  int32_t intValue = (int32_t) uintValue;
1382  value = boost::lexical_cast<string>(intValue);
1383  }
1384  catch (...) {
1385  // simply let "value" remain unchanged
1386  }
1387  }
1388  else if (r->Col(j).Type() == "short") {
1389  try {
1390  std::istringstream iss(value);
1391  uint16_t ushortValue;
1392  iss >> std::hex >> ushortValue;
1393  int16_t shortValue = (int16_t) ushortValue;
1394  value = boost::lexical_cast<string>(shortValue);
1395  }
1396  catch (...) {
1397  // simply let "value" remain unchanged
1398  }
1399  }
1400  }
1401 
1402  bool isAutoIncr = (r->Col(j).Type() == "autoincr");
1403  if (!update)
1404  r->Col(j).Set(value,isAutoIncr);
1405  else{
1406  string name = r->Col(j).Name();
1407  r->Update(name,value);
1408  }
1409  }
1410 
1411  bool isValid = true;
1412  if (! fValidityStart.empty()) {
1413  for (unsigned int i=0; i<fValidityStart.size(); ++i) {
1414  const Column* c = r->Col(fValidityStart[i].Name());
1415  if (fValidityStart[i].Type() == "bool")
1416  isValid = (c->Value() == fValidityStart[i].Value());
1417  else
1418  isValid = ((*c >= fValidityStart[i]) && (*c <= fValidityEnd[i]));
1419  if (!isValid) break;
1420  }
1421  }
1422 
1423  if (isValid) AddRow(r);
1424  delete r;
1425  }
1426  else {
1427  std::cerr << "Table::LoadFromSSV: ignoring line, wrong number of entries, "
1428  << "expected " << fCol.size() << ", found " << sz << "."
1429  << std::endl;
1430  }
1431  }
1432 
1433  fin.close();
1434 
1435  return true;
1436 }
TString fin
Definition: Style.C:24
const XML_Char * name
Definition: expat.h:151
bool Set(const T &val, bool ignoreAutoIncr=false)
Definition: Column.h:70
std::vector< Column > fCol
Definition: Table.h:268
OStream cerr
Definition: OStream.cxx:7
string trim(string in)
Definition: rootgINukeVal.C:65
static bool findFile(const std::string &filePath, std::string &locationOfFoundInstance)
bool Update(std::string cname, T value)
Definition: Row.h:37
const XML_Char * s
Definition: expat.h:262
const XML_Char int const XML_Char * value
Definition: expat.h:331
std::vector< Column > fValidityEnd
Definition: Table.h:291
std::string Value() const
Definition: Column.h:37
const double j
Definition: BetheBloch.cxx:29
void AddRow(const Row *row)
Definition: Table.cpp:727
OStream cout
Definition: OStream.cxx:6
std::string Name() const
Definition: Column.h:35
TRandom3 r(0)
std::string Name()
Definition: Table.h:42
std::vector< Column > fValidityStart
Definition: Table.h:290
std::string Type() const
Definition: Column.h:36
Column & Col(int i)
Definition: Row.h:51
bool nova::database::Table::LoadFromSSV ( const char *  fname,
bool  update = false 
)
inline

Definition at line 129 of file Table.h.

References LoadFromSSV(), string, and UpdateFromSSV().

130  { return LoadFromSSV(std::string(fname)); }
bool LoadFromSSV(std::string fname, bool update=false)
Definition: Table.cpp:1318
enum BeamMode string
std::string nova::database::Table::Name ( )
inline
int nova::database::Table::NCol ( )
inline

Definition at line 80 of file Table.h.

References fCol.

Referenced by PrintColumns().

80 {return fCol.size();}
std::vector< Column > fCol
Definition: Table.h:268
Row* nova::database::Table::NewRow ( )
inline

Definition at line 102 of file Table.h.

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

Referenced by comi::CosmicMetrics::endJob(), comi::DataCheck::endJob(), LoadFromCSV(), LoadFromDB(), LoadFromSSV(), and SetValidityRange().

103  {Row* r = new Row(fCol); return r; }
std::vector< Column > fCol
Definition: Table.h:268
TRandom3 r(0)
int nova::database::Table::NRow ( )
inline
void Table::PrintColumns ( )

Definition at line 673 of file Table.cpp.

References om::cout, allTimeWatchdog::endl, GetCol(), MECModelEnuComparisons::i, calib::j, art::left(), demo0::length, nova::database::Column::Name(), NCol(), and nova::database::Column::Type().

Referenced by RemoveValidityRange().

674 {
675  std::cout << std::endl;
676  int i = 0;
677  vector<int> len(0);
678  int tlen;
679  int sumlen = 0;
680 
681  for ( ; i<this->NCol(); ++i) {
682  tlen = this->GetCol(i)->Name().length();
683  if ((int)this->GetCol(i)->Type().length() > tlen)
684  tlen = this->GetCol(i)->Type().length();
685  len.push_back(tlen);
686  sumlen += tlen;
687  }
688 
689  int nsp = 0;
690  i = 0;
691  int j = 0;
692  while (i < this->NCol()) {
693  for ( ; i<this->NCol() && nsp<78; ++i)
694  nsp += len[i] + 1;
695 
696  for (int k=0; k<nsp; ++k) {
697  std::cout << "_" ;
698  }
699  std::cout << std::endl;
700 
701  int j_save = j;
702  for (; j<i; ++j)
703  std::cout << "|" << setw(len[j]) << left << this->GetCol(j)->Name();
704  std::cout << "|" << std::endl;
705 
706  for (int k=0; k<nsp; ++k) {
707  std::cout << "-" ;
708  }
709  std::cout << std::endl;
710 
711  j = j_save;
712  for ( ; j<i; ++j)
713  std::cout << "|" << setw(len[j]) << left << this->GetCol(j)->Type();
714  std::cout << "|" << std::endl;
715 
716  for (int k=0; k<nsp; ++k) {
717  std::cout << "-" ;
718  }
719  std::cout << std::endl;
720 
721  nsp = 0;
722  }
723 
724 }
const XML_Char int len
Definition: expat.h:262
const Column * GetCol(int i)
Definition: Table.h:108
length
Definition: demo0.py:21
const double j
Definition: BetheBloch.cxx:29
OStream cout
Definition: OStream.cxx:6
constexpr auto const & left(const_AssnsIter< L, R, D, Dir > const &a, const_AssnsIter< L, R, D, Dir > const &b)
Definition: AssnsIter.h:104
std::string Name() const
Definition: Column.h:35
std::string Type() const
Definition: Column.h:36
void nova::database::Table::PrintPQErrorMsg ( )
inline

Definition at line 189 of file Table.h.

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

189  {
190  if (fConnection)
191  std::cerr << PQerrorMessage(fConnection) << std::endl;
192  }
PGconn * fConnection
Definition: Table.h:309
OStream cerr
Definition: OStream.cxx:7
bool Table::RemoveDistinctColumn ( unsigned int  i)

Definition at line 2277 of file Table.cpp.

References AddDistinctColumn(), plot_validation_datamc::c, fCol, fDistinctCol, GetCol(), MECModelEnuComparisons::i, and calib::j.

Referenced by AddDistinctColumn(), and RemoveDistinctColumn().

2278 {
2279  if (i >= fCol.size()) return false;
2280 
2281  const Column* c = &fCol[i];
2282  for (unsigned int j=0; j<fDistinctCol.size(); ++j)
2283  if (fDistinctCol[j] == c) {
2284  fDistinctCol.erase(fDistinctCol.begin() + j);
2285  return true;
2286  }
2287 
2288  return false;
2289 }
std::vector< Column > fCol
Definition: Table.h:268
std::vector< const Column * > fDistinctCol
Definition: Table.h:293
const double j
Definition: BetheBloch.cxx:29
bool nova::database::Table::RemoveDistinctColumn ( std::string  col)
bool nova::database::Table::RemoveDistinctColumn ( const char *  col)
inline

Definition at line 150 of file Table.h.

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

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

Definition at line 2349 of file Table.cpp.

References AddOrderColumn(), plot_validation_datamc::c, fCol, fOrderCol, GetCol(), MECModelEnuComparisons::i, and calib::j.

Referenced by AddOrderColumn(), and RemoveOrderColumn().

2350 {
2351  if (i >= fCol.size()) return false;
2352 
2353  const Column* c = &fCol[i];
2354  for (unsigned int j=0; j<fOrderCol.size(); ++j)
2355  if (fOrderCol[j] == c) {
2356  fOrderCol.erase(fOrderCol.begin() + j);
2357  return true;
2358  }
2359 
2360  return false;
2361 }
std::vector< Column > fCol
Definition: Table.h:268
const double j
Definition: BetheBloch.cxx:29
std::vector< const Column * > fOrderCol
Definition: Table.h:294
bool nova::database::Table::RemoveOrderColumn ( std::string  col)
bool nova::database::Table::RemoveOrderColumn ( const char *  col)
inline

Definition at line 159 of file Table.h.

References RemoveOrderColumn(), and string.

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

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

Definition at line 766 of file Table.cpp.

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

Referenced by ClearRows().

767 {
768  if (i < 0) return false;
769 
770  unsigned int j = i;
771 
772  if (j >= fRow.size()) return false;
773 
774  unsigned int kEnd = fNullList.size();
775  for (unsigned int k=0; k<kEnd; ++k)
776  if (fNullList[k].first == i) {
777  fNullList.erase(fNullList.begin()+k);
778  kEnd = fNullList.size();
779  }
780 
781  fRow.erase(fRow.begin()+j);
782 
783  return true;
784 }
std::vector< Row > fRow
Definition: Table.h:269
std::vector< std::pair< int, int > > fNullList
Definition: Table.h:295
const double j
Definition: BetheBloch.cxx:29
void Table::RemoveValidityRange ( std::string cname)

Definition at line 2246 of file Table.cpp.

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

Referenced by RemoveValidityRange(), and SetValidityRange().

2247 {
2248  unsigned int i=0;
2249  for (; i<fValidityStart.size(); ++i)
2250  if (fValidityStart[i].Name() == cname) {
2251  fValidityStart.erase(fValidityStart.begin()+i);
2252  fValidityEnd.erase(fValidityEnd.begin()+i);
2253  }
2254 }
std::vector< Column > fValidityEnd
Definition: Table.h:291
std::string Name()
Definition: Table.h:42
std::vector< Column > fValidityStart
Definition: Table.h:290
void nova::database::Table::RemoveValidityRange ( const char *  cname)
inline

Definition at line 243 of file Table.h.

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

244  { std::string cstr(cname); return RemoveValidityRange(cstr); fValidityChanged=true;}
void RemoveValidityRange(std::string &cname)
Definition: Table.cpp:2246
enum BeamMode string
void Table::Reset ( )
private

Definition at line 477 of file Table.cpp.

References ClearValidity(), fConnection, fDescOrder, fDistinctCol, fHasConnection, fPKeyList, fSelectLimit, fSelectOffset, fVerbosity, fXMLOverDB, and cet::getenv().

Referenced by CreateFromDB(), and Table().

478 {
479  fConnection = 0;
480  fHasConnection = 0;
481  fXMLOverDB = false;
482  /*
483  addInsertTime = false;
484  addInsertUser = false;
485  addUpdateTime = false;
486  addUpdateUser = false;
487  */
488  fPKeyList.clear();
489  fDistinctCol.clear();
490  fVerbosity = 0;
491  fDescOrder = true;
492  fSelectLimit = 0;
493  fSelectOffset = 0;
494  ClearValidity();
495 
496  char* tmpStr = getenv("NOVADBVERB");
497  if (tmpStr) {
498  fVerbosity = atoi(tmpStr);
499  }
500 
501 }
PGconn * fConnection
Definition: Table.h:309
std::vector< const Column * > fDistinctCol
Definition: Table.h:293
std::string getenv(std::string const &name)
std::vector< const Column * > fPKeyList
Definition: Table.h:292
bool nova::database::Table::ResetConnectionInfo ( )

Referenced by GetMaxConnAttempts().

void nova::database::Table::ResetRole ( )
inline

Definition at line 76 of file Table.h.

References fRole, and fUser.

76 { fRole = fUser; }
std::string fUser
Definition: Table.h:279
std::string fRole
Definition: Table.h:280
std::string nova::database::Table::Role ( )
inline

Definition at line 46 of file Table.h.

References fRole.

46 { return fRole; }
std::string fRole
Definition: Table.h:280
std::string nova::database::Table::Schema ( )
inline

Definition at line 203 of file Table.h.

References fSchema.

Referenced by BulkInsertInDB(), CheckValidXMLDef(), GetCurrSeqVal(), LoadFromDB(), and WriteToDB().

203 { return fSchema; }
std::string fSchema
Definition: Table.h:284
void nova::database::Table::SetDBHost ( std::string  dbhost)
inline

Definition at line 63 of file Table.h.

References fDBHost.

Referenced by CreateFromDB(), and SetDBInfo().

63 { fDBHost = dbhost; }
std::string fDBHost
Definition: Table.h:282
void nova::database::Table::SetDBHost ( const char *  dbhost)
inline

Definition at line 64 of file Table.h.

References fDBHost.

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

Definition at line 579 of file Table.cpp.

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

Referenced by SetDBPort().

581 {
582  SetDBName(name);
583  SetDBHost(host);
584  SetDBPort(port);
585  SetUser(user);
586 }
const XML_Char * name
Definition: expat.h:151
void SetDBName(std::string dbname)
Definition: Table.h:61
void SetDBPort(std::string p)
Definition: Table.h:65
void SetUser(std::string uname)
Definition: Table.h:57
void SetDBHost(std::string dbhost)
Definition: Table.h:63
int port
Definition: client_test.C:9
void Table::SetDBInfo ( const char *  name,
const char *  host,
const char *  port,
const char *  user 
)

Definition at line 589 of file Table.cpp.

References fCol, GetCol(), MECModelEnuComparisons::i, Name(), SetDBHost(), SetDBName(), SetDBPort(), and SetUser().

591 {
592  SetDBName(name);
593  SetDBHost(host);
594  SetDBPort(port);
595  SetUser(user);
596 }
const XML_Char * name
Definition: expat.h:151
void SetDBName(std::string dbname)
Definition: Table.h:61
void SetDBPort(std::string p)
Definition: Table.h:65
void SetUser(std::string uname)
Definition: Table.h:57
void SetDBHost(std::string dbhost)
Definition: Table.h:63
int port
Definition: client_test.C:9
void nova::database::Table::SetDBName ( std::string  dbname)
inline

Definition at line 61 of file Table.h.

References fDBName.

Referenced by CreateFromDB(), and SetDBInfo().

61 { fDBName = dbname; }
std::string fDBName
Definition: Table.h:283
void nova::database::Table::SetDBName ( const char *  dbname)
inline

Definition at line 62 of file Table.h.

References fDBName.

62 { fDBName = dbname; }
std::string fDBName
Definition: Table.h:283
void nova::database::Table::SetDBPort ( std::string  p)
inline

Definition at line 65 of file Table.h.

References fDBPort.

Referenced by CreateFromDB(), and SetDBInfo().

65 { fDBPort = p; }
const char * p
Definition: xmltok.h:285
std::string fDBPort
Definition: Table.h:281
void nova::database::Table::SetDBPort ( const char *  p)
inline

Definition at line 66 of file Table.h.

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

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

Definition at line 513 of file Table.cpp.

References om::cerr, fillBadChanDBTables::det, allTimeWatchdog::endl, fDetector, fSchema, fTableType, novadaq::cnv::DetInfo::GetId(), nova::database::kHardwareTable, and novadaq::cnv::kUNKNOWN_DET.

Referenced by comi::CosmicMetrics::endJob(), comi::DataCheck::endJob(), and SetValiditySQL().

514 {
515  // check that detector string is valid
517  std::cerr << "Error in Table::SetDetector: Unknown detector name \'"
518  << det << "\'" << std::endl;
519  return false;
520  }
521 
522  fDetector = det;
523 
524  if (fTableType != kHardwareTable)
525  fSchema = det;
526  else
527  fSchema = "public";
528 
529  boost::to_lower(fSchema);
530 
531  return true;
532 }
OStream cerr
Definition: OStream.cxx:7
static int GetId(std::string name)
std::string fDetector
Definition: Table.h:311
std::string fSchema
Definition: Table.h:284
bool Table::SetDetector ( int  idet)

Definition at line 535 of file Table.cpp.

References om::cerr, allTimeWatchdog::endl, fDetector, fSchema, fTableType, novadaq::cnv::DetInfo::GetName(), nova::database::kHardwareTable, and novadaq::cnv::kNDetector.

536 {
537  // check that detector id is valid
538  if (idet <= novadaq::cnv::kUNKNOWN_DET || idet >= novadaq::cnv::kNDetector) {
539  std::cerr << "Error in Table::SetDetector: Unknown detector id" << idet
540  << std::endl;
541  return false;
542  }
543 
545 
546  if (fTableType != kHardwareTable)
547  fSchema = fDetector;
548  else
549  fSchema = "public";
550 
551  boost::to_lower(fSchema);
552 
553  return true;
554 }
static std::string GetName(int id)
OStream cerr
Definition: OStream.cxx:7
std::string fDetector
Definition: Table.h:311
std::string fSchema
Definition: Table.h:284
void nova::database::Table::SetIgnoreEnvVar ( bool  f)
inline
void nova::database::Table::SetMaxConnAttempts ( int  n)
inline

Definition at line 250 of file Table.h.

References fMaxConnAttempts, and getGoodRuns4SAM::n.

void nova::database::Table::SetOrderAsc ( )
inline

Definition at line 166 of file Table.h.

References fDescOrder, GetCurrSeqVal(), and string.

166 { fDescOrder = false; }
void nova::database::Table::SetOrderDesc ( )
inline

Definition at line 165 of file Table.h.

References fDescOrder.

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

fname should be the name of the file

Definition at line 1633 of file Table.cpp.

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

Referenced by GetConnection(), and SetDBPort().

1634 {
1635  string fNameStr = "";
1636 
1637  if (fname == 0) {
1638  char* tmpStr = getenv("NOVADBPWDFILE");
1639  if (tmpStr)
1640  fNameStr = tmpStr;
1641  else {
1642  std::cerr << "NOVADBPWDFILE env. variable is not set, disabling "
1643  << "password-access to the dB." << std::endl;
1644  fPassword = "";
1645  return false;
1646  }
1647  }
1648  else
1649  fNameStr = fname;
1650 
1651  ifstream fin;
1652  fin.open(fNameStr.c_str());
1653  if (!fin.is_open() || !fin.good()) {
1654  std::cerr << "Could not open password file " << fNameStr
1655  << ". Disabling password-access to the dB." << std::endl;
1656  return false;
1657  }
1658  else {
1659  fin >> fPassword;
1660  fin.close();
1661  }
1662 
1663  return true;
1664 }
TString fin
Definition: Style.C:24
OStream cerr
Definition: OStream.cxx:7
std::string getenv(std::string const &name)
std::string fPassword
Definition: Table.h:287
bool nova::database::Table::SetRole ( std::string  role)

Referenced by CloseConnection(), and SetUser().

bool nova::database::Table::SetRole ( const char *  role)
void nova::database::Table::SetSchema ( std::string  s)
inline

Definition at line 202 of file Table.h.

References fSchema.

202 { fSchema = s; }
const XML_Char * s
Definition: expat.h:262
std::string fSchema
Definition: Table.h:284
void nova::database::Table::SetSelectLimit ( int  limit)
inline

Definition at line 162 of file Table.h.

References fSelectLimit, and PandAna.Demos.demo_prong::limit.

void nova::database::Table::SetSelectOffset ( int  offset)
inline
bool Table::SetTableType ( int  t)

Definition at line 469 of file Table.cpp.

References fTableType, nova::database::kNTableType, and confusionMatrixTree::t.

Referenced by CreateFromXML(), and TableType().

470 {
471  if (t < 0 || t >= kNTableType) return false;
472  fTableType = t;
473  return true;
474 }
void Table::SetTolerance ( std::string cname,
float  t 
)

Definition at line 622 of file Table.cpp.

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

Referenced by GetCol().

623 {
624  unsigned int i=0;
625  for ( ; i < fCol.size(); ++i)
626  if (fCol[i].Name() == cname) break;
627 
628  if (i >= fCol.size()) return;
629 
630  fCol[i].SetTolerance(t);
631 
632 }
std::vector< Column > fCol
Definition: Table.h:268
std::string Name()
Definition: Table.h:42
void Table::SetToleranceType ( std::string cname,
int  t 
)

Definition at line 634 of file Table.cpp.

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

Referenced by GetCol().

635 {
636  unsigned int i=0;
637  for ( ; i < fCol.size(); ++i)
638  if (fCol[i].Name() == cname) break;
639 
640  if (i >= fCol.size()) return;
641 
642  fCol[i].SetToleranceType(t);
643 
644 }
std::vector< Column > fCol
Definition: Table.h:268
std::string Name()
Definition: Table.h:42
void nova::database::Table::SetUser ( std::string  uname)
inline

Definition at line 57 of file Table.h.

References fUser.

Referenced by CreateFromDB(), and SetDBInfo().

57 { fUser = uname; }
std::string fUser
Definition: Table.h:279
void nova::database::Table::SetUser ( const char *  uname)
inline

Definition at line 58 of file Table.h.

References fUser, retire-remove-files::role, SetRole(), and string.

58 { fUser = uname; }
std::string fUser
Definition: Table.h:279
template<class T >
bool nova::database::Table::SetValidityRange ( std::string  cname,
T  start,
T  end 
)
inline

Definition at line 206 of file Table.h.

References plot_validation_datamc::c, nova::database::Row::Col(), fValidityChanged, fValidityEnd, fValidityStart, MECModelEnuComparisons::i, nova::database::Column::Name(), Name(), NewRow(), r(), and nova::database::Column::Type().

Referenced by comi::CosmicMetrics::endJob(), comi::DataCheck::endJob(), dbutils::hw::FEB::GetAllInstalled(), dbutils::hw::APD::GetAllInstalled(), dbutils::hw::FEB::Installed(), dbutils::hw::APD::Installed(), dbutils::hw::Util::PositionTable(), and SetValidityRange().

207  {
208  Row* r = NewRow();
209  Column* c = r->Col(cname);
210  if (c) {
211  // check to see if this makes sense for a boolean column.
212  if (c->Type() == "bool")
213  if (start != end) return false;
214 
215  // check to see that this column wasn't already added to valid.
216  // list if it is, overwrite it
217 
218  unsigned int i=0;
219  for (; i<fValidityStart.size(); ++i)
220  if (fValidityStart[i].Name() == c->Name()) break;
221 
222  if (i == fValidityStart.size()) {
223  fValidityStart.push_back(Column(*c));
224  fValidityEnd.push_back(Column(*c));
225  }
226  bool isAutoIncr = (c->Type() == "autoincr");
227  fValidityStart[i].Set(start,isAutoIncr);
228  fValidityEnd[i].Set(end,isAutoIncr);
229  fValidityChanged=true;
230  return true;
231  }
232 
233  delete r;
234  return false;
235  }
std::vector< Column > fValidityEnd
Definition: Table.h:291
TRandom3 r(0)
std::string Name()
Definition: Table.h:42
std::vector< Column > fValidityStart
Definition: Table.h:290
template<class T >
bool nova::database::Table::SetValidityRange ( std::string  cname,
T  start 
)
inline

Definition at line 238 of file Table.h.

References RemoveValidityRange(), SetValidityRange(), and string.

238  {
239  return (this->SetValidityRange(cname,start,start));
240  }
bool SetValidityRange(std::string cname, T start, T end)
Definition: Table.h:206
void nova::database::Table::SetValiditySQL ( std::string  cmd)
inline
void nova::database::Table::SetVerbosity ( int  i)
inline

Definition at line 78 of file Table.h.

References fVerbosity, and MECModelEnuComparisons::i.

int nova::database::Table::TableType ( )
inline

Definition at line 48 of file Table.h.

References CreateFromDB(), fTableType, SetTableType(), and confusionMatrixTree::t.

Referenced by CreateFromXML().

48 { return fTableType; }
float Table::Tolerance ( std::string cname)

Definition at line 647 of file Table.cpp.

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

Referenced by GetCol().

648 {
649  unsigned int i=0;
650  for ( ; i < fCol.size(); ++i)
651  if (fCol[i].Name() == cname) break;
652 
653  if (i >= fCol.size()) return 0.;
654 
655  return fCol[i].Tolerance();
656 
657 }
std::vector< Column > fCol
Definition: Table.h:268
std::string Name()
Definition: Table.h:42
float Table::ToleranceType ( std::string cname)

Definition at line 660 of file Table.cpp.

References fCol, MECModelEnuComparisons::i, nova::database::kUnknown, and Name().

Referenced by GetCol().

661 {
662  unsigned int i=0;
663  for ( ; i < fCol.size(); ++i)
664  if (fCol[i].Name() == cname) break;
665 
666  if (i >= fCol.size()) return kUnknown;
667 
668  return fCol[i].ToleranceType();
669 
670 }
std::vector< Column > fCol
Definition: Table.h:268
std::string Name()
Definition: Table.h:42
bool Table::UpdateFromCSV ( std::string  fname)

Definition at line 1311 of file Table.cpp.

References LoadFromCSV().

Referenced by LoadFromCSV(), and UpdateFromCSV().

1312 {
1313  return LoadFromCSV(fname,true);
1314 
1315 }
bool LoadFromCSV(std::string fname, bool update=false)
Definition: Table.cpp:1177
bool nova::database::Table::UpdateFromCSV ( const char *  fname)
inline

Definition at line 125 of file Table.h.

References LoadFromSSV(), string, and UpdateFromCSV().

126  { return UpdateFromCSV(std::string(fname)); }
bool UpdateFromCSV(std::string fname)
Definition: Table.cpp:1311
enum BeamMode string
bool Table::UpdateFromSSV ( std::string  fname)

Definition at line 1439 of file Table.cpp.

References LoadFromSSV().

Referenced by LoadFromSSV(), and UpdateFromSSV().

1440 {
1441  return LoadFromSSV(fname,true);
1442 
1443 }
bool LoadFromSSV(std::string fname, bool update=false)
Definition: Table.cpp:1318
bool nova::database::Table::UpdateFromSSV ( const char *  fname)
inline

Definition at line 132 of file Table.h.

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

133  { return UpdateFromSSV(std::string(fname)); }
bool UpdateFromSSV(std::string fname)
Definition: Table.cpp:1439
enum BeamMode string
std::string nova::database::Table::User ( )
inline

Definition at line 45 of file Table.h.

References fUser.

45 { return fUser; }
std::string fUser
Definition: Table.h:279
bool nova::database::Table::WriteToCSV ( std::string  fname,
bool  appendToFile = false 
)
bool nova::database::Table::WriteToCSV ( const char *  fname,
bool  appendToFile = false 
)
inline

Definition at line 139 of file Table.h.

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

140  { return WriteToCSV(std::string(fname)); }
bool WriteToCSV(std::string fname, bool appendToFile=false)
enum BeamMode string
bool Table::WriteToDB ( bool  commit = true)

use commit=false if just testing

Definition at line 1810 of file Table.cpp.

References addInsertTime, addInsertUser, addUpdateTime, addUpdateUser, CacheDBCommand(), om::cerr, CheckForNulls(), CheckValidXMLDef(), CloseConnection(), run_hadd::cmd, nova::database::Row::Col(), om::cout, e, allTimeWatchdog::endl, ExistsInDB(), fCol, fConnection, fHasConnection, fIgnoreDB, fPKeyList, fRow, fSchema, fUser, fVerbosity, GetConnection(), GetConnectionInfo(), nova::database::Util::GetCurrentTimeAsString(), GetCurrSeqVal(), MECModelEnuComparisons::i, calib::j, nova::database::Column::Modified(), nova::database::Column::Name(), Name(), nova::database::Row::NModified(), r(), Schema(), nova::database::Row::Set(), string, nova::database::Column::Type(), and nova::database::Row::Update().

Referenced by comi::CosmicMetrics::endJob(), comi::DataCheck::endJob(), and UpdateFromSSV().

1811 {
1812  if (! CheckForNulls()) return false;
1813 
1814  bool doWrite = ! fIgnoreDB;
1815  bool hasConn = fHasConnection;
1816 
1817  try {
1819  }
1820  catch (std::runtime_error& e) {
1821  std::cerr << e.what() << std::endl;
1822  return false;
1823  }
1824 
1825  // make a connection to the dB if one doesn't already exist
1826  if (doWrite) {
1827  if (! fHasConnection) {
1828  GetConnection();
1829  hasConn = false;
1830  }
1831 
1832  if (!fConnection) {
1833  std::cerr << "Table::WriteToDB: No connection to the database!" << std::endl;
1834  doWrite = false;
1835  }
1836  else {
1837  // now check that the table actually exists...
1838  if (!ExistsInDB()) {
1839  std::cerr << "Table::WriteToDB: Table does not exist in database!"
1840  << std::endl;
1841  doWrite = false;
1842  }
1843  else {
1844  if (! CheckValidXMLDef()) {
1845  std::cerr << "Table::WriteToDB: Mismatch between XML table definition ";
1846  std::cerr << "file and table defintion from the database! ";
1847  std::cerr << "Aborting write..." << std::endl;
1848  doWrite = false;
1849  }
1850  }
1851  }
1852  }
1853 
1854  bool retVal = true;
1855 
1856  // get current timestamp:
1857  string ts = Util::GetCurrentTimeAsString();
1858 
1859  PGresult* res = PQexec(fConnection, "BEGIN");
1860  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
1861  std::cerr << "BEGIN command failed: " << PQerrorMessage(fConnection) << std::endl;
1862  PQclear(res);
1863  CloseConnection();
1864  return false;
1865  }
1866 
1867  PQclear(res);
1868 
1869  std::string cmd = "SET search_path TO " + fSchema;
1870  res = PQexec(fConnection, cmd.c_str());
1871  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
1872  std::cerr << "\'" << cmd << "\' command failed" << std::endl;
1873  PQclear(res);
1874  CloseConnection();
1875  return false;
1876  }
1877  PQclear(res);
1878  cmd.clear();
1879 
1880  // lock the table if it is of type "timestamp" or "runsubrun"; abort
1881  // write if this fails
1882  /*
1883  if (TableType() == "timestamp" ||
1884  TableType() == "runsubrun") {
1885  if (fVerbosity>0)
1886  std::cerr << "Table::WriteToDB: locking " << Schema() << "." << Name()
1887  << " of type " << TableType() << std::endl;
1888  std::string cmd = "LOCK " + Schema() + "." + Name();
1889  res = PQexec(fConnection, cmd.c_str());
1890  bool isOk = true;
1891  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
1892  std::cerr << "LOCK command failed: " << PQerrorMessage(fConnection)
1893  << " Aborting write." << std::endl;
1894  isOk = false;
1895  }
1896  PQclear(res);
1897  if (!isOk) {
1898  CloseConnection();
1899  return false;
1900  }
1901  }
1902  */
1903 
1904  // now create the INSERT command
1905  for (unsigned int i=0; i<fRow.size(); ++i) {
1906  // do an INSERT only if this entry does not already exists in the dB
1907  if (! fRow[i].InDB()) {
1908  Row r(fRow[i]);
1909  if (addInsertTime) r.Set("inserttime",ts);
1910  if (addInsertUser) r.Set("insertuser",fUser);
1911 
1912  int nrowInsert = fCol.size();
1913  for (unsigned int j=0; j<fCol.size(); ++j) {
1914  if (r.Col(j).Name() == "updatetime")
1915  nrowInsert--;
1916  else if (r.Col(j).Name() == "updateuser")
1917  nrowInsert--;
1918  // else if (! r.Col(j).hasValue())
1919  // nrowInsert--;
1920  else if (r.Col(j).Type() == "autoincr")
1921  nrowInsert--;
1922  }
1923 
1924  ostringstream outs;
1925 
1926  int ic=0;
1927  outs << "INSERT INTO " << Schema() << "." << Name() << " (";
1928  for (unsigned int j=0; j<fCol.size(); ++j) {
1929  if (r.Col(j).Name() == "updatetime") continue;
1930  if (r.Col(j).Name() == "updateuser") continue;
1931  // if (! r.Col(j).hasValue()) continue;
1932  if (r.Col(j).Type() == "autoincr") continue;
1933 
1934  outs << r.Col(j).Name();
1935  if (ic < nrowInsert-1) outs << ",";
1936  ++ic;
1937  }
1938  outs << ") VALUES (";
1939 
1940  ic = 0;
1941  for (unsigned int j=0; j<fCol.size(); ++j) {
1942  if (r.Col(j).Name() == "updatetime") continue;
1943  if (r.Col(j).Name() == "updateuser") continue;
1944  // if (! r.Col(j).hasValue()) continue;
1945  if (r.Col(j).Type() == "autoincr") continue;
1946 
1947  outs << r.Col(j);
1948 
1949  if (ic < nrowInsert-1) outs << ",";
1950  ++ic;
1951  }
1952 
1953  outs << ")";
1954 
1955  if (fVerbosity > 0)
1956  std::cerr << "Table::WriteToDB: Executing PGSQL command: \n\t"
1957  << outs.str() << std::endl;
1958 
1959  if (!commit)
1960  std::cout << outs.str() << std::endl;
1961  else {
1962  if (doWrite) {
1963  res = PQexec(fConnection, outs.str().c_str());
1964  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
1965  CacheDBCommand(outs.str());
1966  std::cerr << "INSERT failed: " << PQerrorMessage(fConnection)
1967  << std::endl;
1968  retVal = false;
1969  }
1970  else {
1971  fRow[i].SetInDB();
1972  // set insert columns
1973  if (addInsertTime) fRow[i].Col("inserttime")->Set(ts);
1974  if (addInsertUser) fRow[i].Col("insertuser")->Set(fUser);
1975  // set autoincr columns
1976  int64_t iseq;
1977  string seqstr;
1978  for (unsigned int j=0; j<fCol.size(); ++j) {
1979  if (fCol[j].Type() == "autoincr") {
1980  if (this->GetCurrSeqVal(fCol[j].Name(),iseq)) {
1981  seqstr = boost::lexical_cast<string>(iseq);
1982  fRow[i].Col(j).Set(seqstr,true);
1983  }
1984  }
1985  }
1986  }
1987  PQclear(res);
1988  }
1989  else
1990  CacheDBCommand(outs.str());
1991  }
1992  }
1993  else {
1994  if ( fRow[i].NModified() > 0 ) {
1995  Row r(fRow[i]);
1996  if (addUpdateTime) r.Update("updatetime",ts);
1997  if (addUpdateUser) r.Update("updateuser",fUser);
1998  std::ostringstream outs;
1999  outs << "UPDATE " << Schema() << "." << Name() << " SET ";
2000  int im = 0;
2001  for (unsigned int j=0; j<fCol.size() && im < r.NModified(); ++j) {
2002  if (r.Col(j).Modified()) {
2003  outs << r.Col(j).Name() + "=";
2004  outs << r.Col(j);
2005  ++im;
2006  if (im < r.NModified()) outs << ",";
2007  }
2008  }
2009  outs << " WHERE ";
2010  // now print out all pkey values
2011  int nkey = fPKeyList.size();
2012  for (int j=0; j<nkey; ++j) {
2013  string pkey = fPKeyList[j]->Name();
2014  outs << pkey << "=" << *r.Col(pkey);
2015  if (j < (nkey-1)) outs << " and ";
2016  }
2017 
2018  if (fVerbosity > 0)
2019  std::cerr << "Table::WriteToDB: Executing PGSQL command: \n\t"
2020  << outs.str() << std::endl;
2021 
2022  if (!commit)
2023  std::cout << outs.str() << std::endl;
2024  else {
2025  if (doWrite) {
2026  res = PQexec(fConnection, outs.str().c_str());
2027  if (PQresultStatus(res) != PGRES_COMMAND_OK) {
2028  CacheDBCommand(outs.str());
2029  std::cerr << "UPDATE failed: " << PQerrorMessage(fConnection) << std::endl;
2030  retVal = false;
2031  }
2032  else {
2033  // set update columns
2034  if (addUpdateTime) fRow[i].Col("updatetime")->Set(ts);
2035  if (addUpdateUser) fRow[i].Col("updateuser")->Set(fUser);
2036  fRow[i].ClearModified();
2037  }
2038  PQclear(res);
2039  }
2040  else
2041  CacheDBCommand(outs.str());
2042  }
2043  }
2044  }
2045  }
2046 
2047  // PQclear(res);
2048  res = PQexec(fConnection, "END");
2049  PQclear(res);
2050 
2051  // close connection to the dB if necessary
2052  if (! hasConn) CloseConnection();
2053 
2054  return retVal;
2055 }
PGconn * fConnection
Definition: Table.h:309
std::vector< Column > fCol
Definition: Table.h:268
static std::string GetCurrentTimeAsString()
Definition: Util.cpp:34
std::vector< Row > fRow
Definition: Table.h:269
OStream cerr
Definition: OStream.cxx:7