/* * MySQLBackend.hh * * Created on: Feb 26, 2016 * Author: nbarros */ #ifndef SRC_DB_PGSQLBACKEND_HH_ #define SRC_DB_PGSQLBACKEND_HH_ #include #include #include #include #include #include // Use the C-interface, as there is no native C++ interface // Adding an explicit C++ interface would only increse the number of dependencies extern "C" { #include } namespace RAT { class PgSQLBackend { public: PgSQLBackend(const std::string &conn_str = ""); virtual ~PgSQLBackend(); void SetConnOpts(const std::string &opts) {conn_options_ = opts;} const std::string& GetConnOpts() const {return conn_options_;} void SetConnURL(const std::string &url) {server_url_=url;} const std::string& GetConnURL() const {return server_url_;} void Disconnect(); void Connect(); void InsertObject(DBTable &tbl); std::set GetListIndexes(const std::string &type_name, const int &run = 0); std::set GetListTypes(const int run = 0); /** * Fetches the first object that matches the type and index requested. Useful for * cases where the run number is not known, or is irrelevant as all we care is the object structure. * @param tblname * @param index * @param found * @param size_bytes * @return */ json::Value FetchObjFast(const std::string &tblname, const std::string &index,bool &found, size_t &size_bytes); json::Value FetchObject(const std::string &tblname, const std::string &index, const int &runNumber, const int &passNumber, bool &found, size_t &size_bytes); //json::Value FetchObjectWithTag(const std::string &tblname, const std::string &index, const int &runNumber, const int &passNumber, const std::string &tag, bool &found, size_t &size_bytes); void SetReconnectRetries(const unsigned int &nretries) {num_reconn_ = nretries;}; const unsigned int& GetReconnRetries() const {return num_reconn_;}; // Defines that from now on, connections should be opened and closed as needed void SetBoRCompleted(bool status = true) {bor_done_ = status;}; bool GetBoRCompleted() const {return bor_done_;}; // bool GetLastQueryFail() {return last_query_fail_;} // const std::string &GetLastQueryMsg() {return last_query_msg_;} void SetDbTag(const std::string &tag); // {ratdb_tag_ = tag;} std::string GetDbTag() { return ratdb_tag_;} protected: void CheckAndResetConn(); void ClearResultset(); // void ParseConnectionStr(const std::string url); void ExecuteSelectQuery(const char* query); void FailQuery(const char* query); PGconn *pg_conn_; PGresult *pg_res_; PGnotify *pg_notify_; std::string server_url_; std::string conn_options_; unsigned int num_reconn_; unsigned int num_sec_wait_; bool bor_done_; bool bor_warn_given_; // bool last_query_fail_; // std::string last_query_msg_; std::string ratdb_tag_; std::string ratdb_header_tbl_; static const std::string ratdb_header_tbl_base_; }; inline void PgSQLBackend::ExecuteSelectQuery(const char* query) { pg_res_ = PQexec(pg_conn_, query); if (PQresultStatus(pg_res_) != PGRES_TUPLES_OK) { FailQuery(query); } #ifdef RATDEBUG else { detail << "PgSQLBackend::RATDB query executed successfully." << newline; } #endif /*RATDEBUG*/ // If the query executed fine, there is nothing else to do } inline void PgSQLBackend::ClearResultset() { PQclear(pg_res_); pg_res_ = NULL; } } /* namespace RAT */ #endif /* SRC_DB_PGSQLBACKEND_HH_ */