8#include "daq/slc/psql/PostgreSQLInterface.h"
9#include "daq/slc/base/ConfigFile.h"
12#include <pgsql/libpq-fe.h>
15#include <daq/slc/base/StringUtil.h>
16#include <daq/slc/database/DBHandlerException.h>
18#include <daq/slc/system/LockGuard.h>
22PostgreSQLInterface::PostgreSQLInterface(
const std::string& host,
23 const std::string& database,
24 const std::string& user,
25 const std::string& password,
28 init(host, database, user, password, port);
33PostgreSQLInterface::PostgreSQLInterface()
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"));
45void PostgreSQLInterface::connect()
48 if (isConnected())
return;
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(),
58 if (PQstatus(m_sq_conn) == CONNECTION_BAD) {
60 PQerrorMessage(m_sq_conn));
69bool PostgreSQLInterface::isConnected()
73 bool connected = m_sq_conn != NULL && PQstatus(m_sq_conn) == CONNECTION_OK;
80void PostgreSQLInterface::execute_imp(
const char* command)
85 m_sq_result = PQexec(m_sq_conn, command);
86 ExecStatusType status = PQresultStatus(m_sq_result);
87 if (status == PGRES_FATAL_ERROR) {
91 command, PQerrorMessage(m_sq_conn));
99DBRecordList PostgreSQLInterface::loadRecords()
103 if (PQresultStatus(m_sq_result) != PGRES_TUPLES_OK) {
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);
114 for (
size_t irecord = 0; irecord < nrecords; irecord++) {
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);
120 record.add(name_v[ifield], value);
124 m_record_v.push_back(record);
129 DBRecordList ret(m_record_v);
137void PostgreSQLInterface::clear()
141 if (m_sq_result != NULL) {
142 PQclear(m_sq_result);
150void PostgreSQLInterface::close()
155 if (m_sq_conn != NULL) {
164bool PostgreSQLInterface::checkTable(
const std::string& tablename)
167 execute(
"select relname from pg_stat_user_tables where relname='%s';",
169 DBRecordList ret(loadRecords());
170 return ret.size() > 0;
176DBFieldTypeList PostgreSQLInterface::getTableContents(
const std::string& tablename)
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")));
Lock Guard for a Mutex instance.
Abstract base class for different kinds of events.