Static Public Member Functions | List of all members
nutools::dbi::Util Class Reference

#include <Util.h>

Static Public Member Functions

static bool CheckConnection (std::string dbname, std::string server, std::string user="", std::string port="")
 
static std::string GetFarPastTimeAsString ()
 
static std::string GetCurrentTimeAsString ()
 
static std::string GetCurrentDateAsString ()
 
static std::string GetFarFutureTimeAsString ()
 
static bool TimeAsStringToTime_t (std::string ts, time_t &t)
 
static bool DateAsStringToTime_t (std::string date, time_t &t)
 
static bool RunningOnGrid ()
 

Detailed Description

Generalized Database Table Interface Utilities

Author
Jonathan Paley
Version
Id
Util.h,v 1.9 2011/08/08 16:03:04 jpaley Exp

Definition at line 16 of file Util.h.

Member Function Documentation

bool nutools::dbi::Util::CheckConnection ( std::string  dbname,
std::string  server,
std::string  user = "",
std::string  port = "" 
)
static

Definition at line 13 of file Util.cpp.

15  {
16  bool retVal = true;
17 
18  std::string cmd = "dbname = " + dbname + " host = " + host;
19  if (user != "")
20  cmd += " user = " + user;
21  if (port != "")
22  cmd += " port = " + port;
23 
24  PGconn* conn = PQconnectdb(cmd.c_str());
25  if (conn) {
26  if (PQstatus(conn) != CONNECTION_OK) {
27  std::cerr << "Connection to " << host << ":" << dbname << " failed: "
28  << PQerrorMessage(conn) << std::endl;
29  retVal = false;
30  }
31  else
32  PQfinish(conn);
33  }
34  else
35  retVal = false;
36 
37  return retVal;
38 
39  }
std::string string
Definition: nybbler.cc:12
pg_conn PGconn
Definition: Table.h:17
conn
Definition: dbjson.py:16
list cmd
Definition: getreco.py:19
QTextStream & endl(QTextStream &s)
bool nutools::dbi::Util::DateAsStringToTime_t ( std::string  date,
time_t &  t 
)
static

Definition at line 106 of file Util.cpp.

107  {
108  bool isOk = false;
109  struct tm ta;
110  if ( (sscanf(ts.c_str(),"%d/%d/%d",
111  &ta.tm_year,&ta.tm_mon,&ta.tm_mday) == 3))
112  isOk = true;
113  if (!isOk)
114  if ( (sscanf(ts.c_str(),"%d-%d-%d",
115  &ta.tm_year,&ta.tm_mon,&ta.tm_mday) == 3))
116  isOk = true;
117 
118  if (isOk) {
119  ta.tm_year -= 1900;
120  ta.tm_mon -= 1;
121  ta.tm_hour = 0;
122  ta.tm_min = 0;
123  ta.tm_sec = 0;
124  t = timegm(&ta);
125  }
126 
127  return isOk;
128 
129  }
std::string nutools::dbi::Util::GetCurrentDateAsString ( )
static

Definition at line 56 of file Util.cpp.

57  {
58  time_t rawtime;
59  struct tm* timeinfo;
60  char buffer[256];
61  time(&rawtime);
62  timeinfo = gmtime(&rawtime);
63  strftime(buffer,256,"%Y-%m-%d",timeinfo);
64 
65  return std::string(buffer);
66 
67  }
std::string string
Definition: nybbler.cc:12
std::string nutools::dbi::Util::GetCurrentTimeAsString ( )
static

Definition at line 42 of file Util.cpp.

43  {
44  time_t rawtime;
45  struct tm* timeinfo;
46  char buffer[256];
47  time(&rawtime);
48  timeinfo = gmtime(&rawtime);
49  strftime(buffer,256,"%Y-%m-%d %H:%M:%S",timeinfo);
50 
51  return std::string(buffer);
52 
53  }
std::string string
Definition: nybbler.cc:12
std::string nutools::dbi::Util::GetFarFutureTimeAsString ( )
static

Definition at line 75 of file Util.cpp.

76  {
77  return "2099-12-31 23:59:59";
78  }
std::string nutools::dbi::Util::GetFarPastTimeAsString ( )
static

Definition at line 70 of file Util.cpp.

71  {
72  return "1900-01-01 00:00:00";
73  }
bool nutools::dbi::Util::RunningOnGrid ( )
static

Definition at line 132 of file Util.cpp.

133  {
134  char* tStr = getenv("_CONDOR_SCRATCH_DIR");
135  return (tStr != 0);
136  }
std::string getenv(std::string const &name)
Definition: getenv.cc:15
bool nutools::dbi::Util::TimeAsStringToTime_t ( std::string  ts,
time_t &  t 
)
static

Definition at line 81 of file Util.cpp.

82  {
83  bool isOk = false;
84  struct tm ta;
85  if ( (sscanf(ts.c_str(),"%d/%d/%d %d:%d:%d",
86  &ta.tm_year,&ta.tm_mon,&ta.tm_mday,
87  &ta.tm_hour,&ta.tm_min,&ta.tm_sec) == 6))
88  isOk = true;
89  if (!isOk)
90  if ( (sscanf(ts.c_str(),"%d-%d-%d %d:%d:%d",
91  &ta.tm_year,&ta.tm_mon,&ta.tm_mday,
92  &ta.tm_hour,&ta.tm_min,&ta.tm_sec) == 6))
93  isOk = true;
94 
95  if (isOk) {
96  ta.tm_year -= 1900;
97  ta.tm_mon -= 1;
98  t = timegm(&ta);
99  }
100 
101  return isOk;
102 
103  }

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