37 #ifndef _POSTGRESQL_DATABASE_H_
38 #define _POSTGRESQL_DATABASE_H_
42 #include <boost/shared_ptr.hpp>
46 #include <yaml-cpp/yaml.h>
55 namespace database_interface {
84 node[
"user"] >> options.
user_;
85 node[
"host"] >> options.
host_;
86 node[
"port"] >> options.
port_;
87 node[
"dbname"] >> options.
dbname_;
93 void pgMDBconstruct(std::string host, std::string port, std::string user,
94 std::string password, std::string dbname );
106 bool getVariable(std::string name, std::string &value)
const;
119 bool getList(std::vector< boost::shared_ptr<T> > &vec,
const T& example, std::string where_clause)
const;
123 std::vector<int> &column_ids, std::string where_clause,
124 boost::shared_ptr<PGresultAutoPtr> &result,
int &num_tuples)
const;
128 const std::vector<const DBFieldBase*> &fields,
129 const std::vector<int> &column_ids)
const;
132 bool getSequence(std::string name, std::string &value);
135 bool insertIntoTable(std::string table_name,
const std::vector<const DBFieldBase*> &fields);
143 std::string password, std::string dbname);
159 bool getList(std::vector< boost::shared_ptr<T> > &vec)
const
162 return getList<T>(vec, example,
"");
168 return getList<T>(vec, example, clause.
clause_);
171 bool getList(std::vector< boost::shared_ptr<T> > &vec, std::string where_clause)
const
174 return getList<T>(vec, example, where_clause);
179 bool getList(std::vector< boost::shared_ptr<T> > &vec,
const T &example)
const
181 return getList<T>(vec, example,
"");
186 return getList<T>(vec, example, clause.
clause_);
190 bool countList(
const DBClass *example,
int &count, std::string where_clause)
const;
193 template <
typename T>
197 return countList(&example, count, clause.clause_);
236 const T &example, std::string where_clause)
const
239 std::vector<const DBFieldBase*> fields;
241 std::vector<int> column_ids;
242 boost::shared_ptr<PGresultAutoPtr> result;
246 if (!
getListRawResult(&example, fields, column_ids, where_clause, result, num_tuples))
258 for (
int i=0; i<num_tuples; i++)
260 boost::shared_ptr<T> entry(
new T);
263 vec.push_back(entry);
bool insertIntoTable(std::string table_name, const std::vector< const DBFieldBase * > &fields)
Helper function for inserting an instance into the database.
bool getList(std::vector< boost::shared_ptr< T > > &vec) const
bool countList(int &count, const FilterClause clause=FilterClause()) const
templated implementation of count list that works on filter clauses.
bool getList(std::vector< boost::shared_ptr< T > > &vec, std::string where_clause) const
std::string getPort() const
std::string getDBname() const
PostgresqlDatabaseConfig()
bool commit()
Issues the "commit" command to the database.
bool getSequence(std::string name, std::string &value)
Returns the 'currval' for the database sequence identified by name.
bool countList(const DBClass *example, int &count, std::string where_clause) const
Counts the number of instances of a certain type in the database.
bool getList(std::vector< boost::shared_ptr< T > > &vec, const T &example) const
friend void operator>>(const YAML::Node &node, PostgresqlDatabaseConfig &options)
Loads YAML doc into configuration params. Throws YAML::ParserException if keys missing.
bool populateListEntry(DBClass *entry, boost::shared_ptr< PGresultAutoPtr > result, int row_num, const std::vector< const DBFieldBase * > &fields, const std::vector< int > &column_ids) const
Helper function for getList, separates SQL from (templated) instantiation.
bool getListRawResult(const DBClass *example, std::vector< const DBFieldBase * > &fields, std::vector< int > &column_ids, std::string where_clause, boost::shared_ptr< PGresultAutoPtr > &result, int &num_tuples) const
Helper function for getList, separates SQL from (templated) instantiation.
PostgresqlDatabase(std::string host, std::string port, std::string user, std::string password, std::string dbname)
Attempts to connect to the specified database.
The base class for a field of a class stored in the database, corresponding to a column in a table...
The base class for all C++ classes that can be stored in the database.
std::string getPassword() const
bool getVariable(std::string name, std::string &value) const
Gets the text value of a given variable.
~PostgresqlDatabase()
Closes the connection to the database.
bool rollback()
Issues the "rollback" command to the database.
bool getList(std::vector< boost::shared_ptr< T > > &vec, const T &example, std::string where_clause) const
Retreives the list of objects of a certain type from the database.
bool begin()
Isses the "begin" command to the database.
PGconn * connection_
The PostgreSQL database connection we are using.
void pgMDBconstruct(std::string host, std::string port, std::string user, std::string password, std::string dbname)
bool getList(std::vector< boost::shared_ptr< T > > &vec, const T &example, const FilterClause clause) const
bool insertIntoDatabase(DBClass *instance)
Inserts a new instance of a DBClass into the database.
bool deleteFromDatabase(DBClass *instance)
Deletes an instance of a DBClass from the database.
bool saveToDatabase(const DBFieldBase *field)
Writes the value of one particular field of a DBClass to the database.
std::string getHost() const
bool loadFromDatabase(DBFieldBase *field) const
Reads the value of one particular fields of a DBClass from the database.
std::istream & operator>>(std::istream &iss, std::vector< V > &vec)
Streaming of a vector from a string in accordance to database formatting.
std::string getUser() const
bool getList(std::vector< boost::shared_ptr< T > > &vec, const FilterClause clause) const
bool deleteFromTable(std::string table_name, const DBFieldBase *key_field)
Helper function that deletes a row from a table based on the value of the specified field...
bool isConnected() const
Returns true if the interface is connected to the database and ready to go.