1 #include "daq/slc/psql/PostgreSQLInterface.h"
2 #include "daq/slc/base/ConfigFile.h"
5 #include <pgsql/libpq-fe.h>
8 #include <daq/slc/base/StringUtil.h>
9 #include <daq/slc/database/DBHandlerException.h>
11 #include <daq/slc/system/LockGuard.h>
15 PostgreSQLInterface::PostgreSQLInterface(
const std::string& host,
16 const std::string& database,
17 const std::string& user,
18 const std::string& password,
21 init(host, database, user, password, port);
26 PostgreSQLInterface::PostgreSQLInterface()
29 init(config.get(
"database.host"),
30 config.get(
"database.dbname"),
31 config.get(
"database.user"),
32 config.get(
"database.password"),
33 config.getInt(
"database.port"));
38 void PostgreSQLInterface::connect()
41 if (isConnected())
return;
45 m_sq_conn = PQconnectdb(StringUtil::form(
"host=%s dbname=%s user=%s password=%s port=%d",
46 m_host.c_str(), m_database.c_str(),
47 m_user.c_str(), m_password.c_str(),
51 if (PQstatus(m_sq_conn) == CONNECTION_BAD) {
53 PQerrorMessage(m_sq_conn));
62 bool PostgreSQLInterface::isConnected()
66 bool connected = m_sq_conn != NULL && PQstatus(m_sq_conn) == CONNECTION_OK;
73 void PostgreSQLInterface::execute_imp(
const char* command)
78 m_sq_result = PQexec(m_sq_conn, command);
79 ExecStatusType status = PQresultStatus(m_sq_result);
80 if (status == PGRES_FATAL_ERROR) {
84 command, PQerrorMessage(m_sq_conn));
92 DBRecordList PostgreSQLInterface::loadRecords()
96 if (PQresultStatus(m_sq_result) != PGRES_TUPLES_OK) {
99 const size_t nrecords = PQntuples(m_sq_result);
100 const size_t nfields = PQnfields(m_sq_result);
101 m_record_v = DBRecordList();
102 std::vector<std::string> name_v;
103 for (
size_t ifield = 0; ifield < nfields; ifield++) {
104 const char* name = PQfname(m_sq_result, ifield);
105 if (name != NULL) name_v.push_back(name);
107 for (
size_t irecord = 0; irecord < nrecords; irecord++) {
109 for (
size_t ifield = 0; ifield < nfields; ifield++) {
110 if (!PQgetisnull(m_sq_result, irecord, ifield)) {
111 const char* value = PQgetvalue(m_sq_result, irecord, ifield);
113 record.add(name_v[ifield], value);
117 m_record_v.push_back(record);
122 DBRecordList ret(m_record_v);
130 void PostgreSQLInterface::clear()
134 if (m_sq_result != NULL) {
135 PQclear(m_sq_result);
143 void PostgreSQLInterface::close()
148 if (m_sq_conn != NULL) {
157 bool PostgreSQLInterface::checkTable(
const std::string& tablename)
160 execute(
"select relname from pg_stat_user_tables where relname='%s';",
162 DBRecordList ret(loadRecords());
163 return ret.size() > 0;
169 DBFieldTypeList PostgreSQLInterface::getTableContents(
const std::string& tablename)
172 DBFieldTypeList name_m;
173 execute(
"select attname, typname from pg_class, pg_attribute, pg_type "
174 "where relkind ='r'and relname = '%s' and attrelid = relfilenode "
175 "and attnum > 0 and pg_type.oid = atttypid;", tablename.c_str());
176 DBRecordList ret(loadRecords());
177 for (
size_t i = 0; i < ret.size(); i++) {
178 name_m.insert(DBFieldTypeList::value_type(ret[i].get(
"attname"),
179 ret[i].get(
"typname")));