Belle II Software development
PostgreSQLInterface Class Reference
Inheritance diagram for PostgreSQLInterface:
DBInterface

Public Member Functions

 PostgreSQLInterface (const std::string &host, const std::string &database, const std::string &user, const std::string &password, int port)
 
void connect () override
 
bool isConnected () override
 
void execute_imp (const char *command) override
 
void close () override
 
void clear () override
 
DBRecordList loadRecords () override
 
bool checkTable (const std::string &tablename) override
 
DBFieldTypeList getTableContents (const std::string &tablename) override
 
void execute (const char *command,...)
 
void execute (const std::string &command)
 
void clearRecords ()
 
DBRecordList & getRecords ()
 
const std::string & getHostName () const
 
const std::string & getDatabase () const
 
const std::string & getUserName () const
 
const std::string & getPassword () const
 
int getPort () const
 

Protected Member Functions

void init (const std::string &host, const std::string &database, const std::string &user, const std::string &password, int port)
 

Protected Attributes

DBRecordList m_record_v
 
std::string m_host
 
std::string m_database
 
std::string m_user
 
std::string m_password
 
int m_port
 

Private Attributes

PGconn * m_sq_conn
 
PGresult * m_sq_result
 
Mutex m_mutex
 
char * m_buf
 

Static Private Attributes

static const int m_buf_size = 1024 * 1000
 

Detailed Description

Definition at line 23 of file PostgreSQLInterface.h.

Constructor & Destructor Documentation

◆ PostgreSQLInterface() [1/2]

PostgreSQLInterface ( const std::string &  host,
const std::string &  database,
const std::string &  user,
const std::string &  password,
int  port 
)

Definition at line 22 of file PostgreSQLInterface.cc.

27{
28 init(host, database, user, password, port);
29 m_sq_conn = NULL;
30 m_sq_result = NULL;
31}

◆ PostgreSQLInterface() [2/2]

Definition at line 33 of file PostgreSQLInterface.cc.

34{
35 ConfigFile config("slowcontrol");
36 init(config.get("database.host"),
37 config.get("database.dbname"),
38 config.get("database.user"),
39 config.get("database.password"),
40 config.getInt("database.port"));
41 m_sq_conn = NULL;
42 m_sq_result = NULL;
43}

◆ ~PostgreSQLInterface()

virtual ~PostgreSQLInterface ( )
inlinevirtual

Definition at line 32 of file PostgreSQLInterface.h.

32{}

Member Function Documentation

◆ checkTable()

bool checkTable ( const std::string &  tablename)
overridevirtual

Implements DBInterface.

Definition at line 164 of file PostgreSQLInterface.cc.

165{
166#ifndef NOT_USE_PSQL
167 execute("select relname from pg_stat_user_tables where relname='%s';",
168 tablename.c_str());
169 DBRecordList ret(loadRecords());
170 return ret.size() > 0;
171#else
172 throw (DBHandlerException("libpg is not available"));
173#endif
174}

◆ clear()

void clear ( )
overridevirtual

Implements DBInterface.

Definition at line 137 of file PostgreSQLInterface.cc.

138{
139#ifndef NOT_USE_PSQL
140 LockGuard lockGuard(m_mutex);
141 if (m_sq_result != NULL) {
142 PQclear(m_sq_result);
143 m_sq_result = NULL;
144 }
145#else
146 throw (DBHandlerException("libpg is not available"));
147#endif
148}
Lock Guard for a Mutex instance.
Definition: LockGuard.h:47

◆ clearRecords()

void clearRecords ( )
inlineinherited

Definition at line 47 of file DBInterface.h.

47{ m_record_v.resize(0); }

◆ close()

void close ( )
overridevirtual

Implements DBInterface.

Definition at line 150 of file PostgreSQLInterface.cc.

151{
152 clear();
153#ifndef NOT_USE_PSQL
154 LockGuard lockGuard(m_mutex);
155 if (m_sq_conn != NULL) {
156 PQfinish(m_sq_conn);
157 m_sq_conn = NULL;
158 }
159#else
160 throw (DBHandlerException("libpg is not available"));
161#endif
162}

◆ connect()

void connect ( )
overridevirtual

Implements DBInterface.

Definition at line 45 of file PostgreSQLInterface.cc.

46{
47#ifndef NOT_USE_PSQL
48 if (isConnected()) return;
49
50 {
51 LockGuard lockGuard(m_mutex);
52 m_sq_conn = PQconnectdb(StringUtil::form("host=%s dbname=%s user=%s password=%s port=%d",
53 m_host.c_str(), m_database.c_str(),
54 m_user.c_str(), m_password.c_str(),
55 m_port).c_str());
56 }
57
58 if (PQstatus(m_sq_conn) == CONNECTION_BAD) {
59 DBHandlerException exception("Failed to connect to the database : %s",
60 PQerrorMessage(m_sq_conn));
61 close();
62 throw exception;
63 }
64#else
65 throw (DBHandlerException("PGLIB is not available"));
66#endif
67}

◆ execute() [1/2]

void execute ( const char *  command,
  ... 
)
inherited

Definition at line 45 of file DBInterface.cc.

46{
47 StringList s;
48
49 {
50 LockGuard lockGuard(m_mutex);
51
52 va_list ap;
53 va_start(ap, text);
54 vsnprintf(m_buf, m_buf_size, text, ap);
55 va_end(ap);
56 //std::cout << m_buf << std::endl;
57 //execute_imp(m_buf);
58 s = StringUtil::split(m_buf, ';');
59 }
60
61 for (size_t i = 0; i < s.size(); i++)
62 execute_imp(s[i].c_str());
63}

◆ execute() [2/2]

void execute ( const std::string &  command)
inherited

Definition at line 65 of file DBInterface.cc.

66{
67 //std::cout << text << std::endl;
68 StringList s = StringUtil::split(text, ';');
69 for (size_t i = 0; i < s.size(); i++)
70 execute_imp(s[i].c_str());
71}

◆ execute_imp()

void execute_imp ( const char *  command)
overridevirtual

Implements DBInterface.

Definition at line 80 of file PostgreSQLInterface.cc.

81{
82 clear();
83#ifndef NOT_USE_PSQL
84 LockGuard lockGuard(m_mutex);
85 m_sq_result = PQexec(m_sq_conn, command);
86 ExecStatusType status = PQresultStatus(m_sq_result);
87 if (status == PGRES_FATAL_ERROR) {
88 // Need to pre-generate exception to avoid data race
89 // on PQerrorMessage.
90 DBHandlerException exception("Failed to execute command : %s (%s)",
91 command, PQerrorMessage(m_sq_conn));
92 throw (exception);
93 }
94#else
95 throw (DBHandlerException("libpg is not available"));
96#endif
97}

◆ getDatabase()

const std::string & getDatabase ( ) const
inlineinherited

Definition at line 50 of file DBInterface.h.

50{ return m_database; }

◆ getHostName()

const std::string & getHostName ( ) const
inlineinherited

Definition at line 49 of file DBInterface.h.

49{ return m_host; }

◆ getPassword()

const std::string & getPassword ( ) const
inlineinherited

Definition at line 52 of file DBInterface.h.

52{ return m_password; }

◆ getPort()

int getPort ( ) const
inlineinherited

Definition at line 53 of file DBInterface.h.

53{ return m_port; }

◆ getRecords()

DBRecordList & getRecords ( )
inlineinherited

Definition at line 48 of file DBInterface.h.

48{ return m_record_v; }

◆ getTableContents()

DBFieldTypeList getTableContents ( const std::string &  tablename)
overridevirtual

Implements DBInterface.

Definition at line 176 of file PostgreSQLInterface.cc.

177{
178#ifndef NOT_USE_PSQL
179 DBFieldTypeList name_m;
180 execute("select attname, typname from pg_class, pg_attribute, pg_type "
181 "where relkind ='r'and relname = '%s' and attrelid = relfilenode "
182 "and attnum > 0 and pg_type.oid = atttypid;", tablename.c_str());
183 DBRecordList ret(loadRecords());
184 for (size_t i = 0; i < ret.size(); i++) {
185 name_m.insert(DBFieldTypeList::value_type(ret[i].get("attname"),
186 ret[i].get("typname")));
187 }
188 return name_m;
189#else
190 throw (DBHandlerException("libpg is not available"));
191#endif
192}

◆ getUserName()

const std::string & getUserName ( ) const
inlineinherited

Definition at line 51 of file DBInterface.h.

51{ return m_user; }

◆ init()

void init ( const std::string &  host,
const std::string &  database,
const std::string &  user,
const std::string &  password,
int  port 
)
protectedinherited

Definition at line 32 of file DBInterface.cc.

36{
37 m_host = host;
38 m_database = database;
39 m_user = user;
40 m_password = password;
41 m_port = port;
42 m_buf = new char[m_buf_size];
43}

◆ isConnected()

bool isConnected ( )
overridevirtual

Implements DBInterface.

Definition at line 69 of file PostgreSQLInterface.cc.

70{
71#ifndef NOT_USE_PSQL
72 LockGuard lockGuard(m_mutex);
73 bool connected = m_sq_conn != NULL && PQstatus(m_sq_conn) == CONNECTION_OK;
74 return connected;
75#else
76 return false;
77#endif
78}

◆ loadRecords()

DBRecordList loadRecords ( )
overridevirtual

Implements DBInterface.

Definition at line 99 of file PostgreSQLInterface.cc.

100{
101#ifndef NOT_USE_PSQL
102 LockGuard lockGuard(m_mutex);
103 if (PQresultStatus(m_sq_result) != PGRES_TUPLES_OK) {
104 throw (DBHandlerException("DB records are not ready for reading"));
105 }
106 const size_t nrecords = PQntuples(m_sq_result);
107 const size_t nfields = PQnfields(m_sq_result);
108 m_record_v = DBRecordList();
109 std::vector<std::string> name_v;
110 for (size_t ifield = 0; ifield < nfields; ifield++) {
111 const char* name = PQfname(m_sq_result, ifield);
112 if (name != NULL) name_v.push_back(name);
113 }
114 for (size_t irecord = 0; irecord < nrecords; irecord++) {
115 DBRecord record;
116 for (size_t ifield = 0; ifield < nfields; ifield++) {
117 if (!PQgetisnull(m_sq_result, irecord, ifield)) {
118 const char* value = PQgetvalue(m_sq_result, irecord, ifield);
119 if (value != NULL) {
120 record.add(name_v[ifield], value);
121 }
122 }
123 }
124 m_record_v.push_back(record);
125 }
126
127 // Vector copy must be done before mutex release,
128 // otherwise data race is introduced.
129 DBRecordList ret(m_record_v);
130
131 return ret;
132#else
133 throw (DBHandlerException("libpg is not available"));
134#endif
135}

Member Data Documentation

◆ m_buf

char* m_buf
privateinherited

Definition at line 74 of file DBInterface.h.

◆ m_buf_size

const int m_buf_size = 1024 * 1000
staticprivateinherited

Definition at line 73 of file DBInterface.h.

◆ m_database

std::string m_database
protectedinherited

Definition at line 66 of file DBInterface.h.

◆ m_host

std::string m_host
protectedinherited

Definition at line 65 of file DBInterface.h.

◆ m_mutex

Mutex m_mutex
private

Definition at line 47 of file PostgreSQLInterface.h.

◆ m_password

std::string m_password
protectedinherited

Definition at line 68 of file DBInterface.h.

◆ m_port

int m_port
protectedinherited

Definition at line 69 of file DBInterface.h.

◆ m_record_v

DBRecordList m_record_v
protectedinherited

Definition at line 64 of file DBInterface.h.

◆ m_sq_conn

PGconn* m_sq_conn
private

Definition at line 45 of file PostgreSQLInterface.h.

◆ m_sq_result

PGresult* m_sq_result
private

Definition at line 46 of file PostgreSQLInterface.h.

◆ m_user

std::string m_user
protectedinherited

Definition at line 67 of file DBInterface.h.


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