postgresql_database.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2009, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 // Author(s): Matei Ciocarlie
36 
37 #ifndef _POSTGRESQL_DATABASE_H_
38 #define _POSTGRESQL_DATABASE_H_
39 
40 #include <vector>
41 #include <string>
42 #include <boost/shared_ptr.hpp>
43 
44 //for ROS error messages
45 #include <ros/ros.h>
46 #include <yaml-cpp/yaml.h>
47 
50 
51 //A bit of an involved way to forward declare PGconn, which is a typedef
52 struct pg_conn;
53 typedef struct pg_conn PGconn;
54 
55 namespace database_interface {
56 
58 {
59 private:
60  std::string password_;
61  std::string user_;
62  std::string host_;
63  std::string port_;
64  std::string dbname_;
65 
66 public:
68 
69  std::string getPassword() const { return password_; }
70  std::string getUser() const { return user_; }
71  std::string getHost() const { return host_; }
72  std::string getPort() const { return port_; }
73  std::string getDBname() const { return dbname_; }
74 
75  friend void operator>>(const YAML::Node &node, PostgresqlDatabaseConfig &options);
76 };
77 
81 inline void operator>>(const YAML::Node& node, PostgresqlDatabaseConfig &options)
82 {
83  node["password"] >> options.password_;
84  node["user"] >> options.user_;
85  node["host"] >> options.host_;
86  node["port"] >> options.port_;
87  node["dbname"] >> options.dbname_;
88 }
89 
91 {
92  protected:
93  void pgMDBconstruct(std::string host, std::string port, std::string user,
94  std::string password, std::string dbname );
95 
98 
101 
102  // beginTransaction sets this flag. endTransaction clears it.
103  bool in_transaction_;
104 
106  bool getVariable(std::string name, std::string &value) const;
107 
109  bool rollback();
110 
112  bool begin();
113 
115  bool commit();
116 
118  template <class T>
119  bool getList(std::vector< boost::shared_ptr<T> > &vec, const T& example, std::string where_clause) const;
120 
122  bool getListRawResult(const DBClass *example, std::vector<const DBFieldBase*> &fields,
123  std::vector<int> &column_ids, std::string where_clause,
124  boost::shared_ptr<PGresultAutoPtr> &result, int &num_tuples) const;
125 
127  bool populateListEntry(DBClass *entry, boost::shared_ptr<PGresultAutoPtr> result, int row_num,
128  const std::vector<const DBFieldBase*> &fields,
129  const std::vector<int> &column_ids) const;
130 
132  bool getSequence(std::string name, std::string &value);
133 
135  bool insertIntoTable(std::string table_name, const std::vector<const DBFieldBase*> &fields);
136 
138  bool deleteFromTable(std::string table_name, const DBFieldBase *key_field);
139 
140  public:
142  PostgresqlDatabase(std::string host, std::string port, std::string user,
143  std::string password, std::string dbname);
144 
147 
148 
151 
153  bool isConnected() const;
154 
155  //------- general queries that should work regardless of the datatypes actually being used ------
156 
157  //------- retrieval without examples -------
158  template <class T>
159  bool getList(std::vector< boost::shared_ptr<T> > &vec) const
160  {
161  T example;
162  return getList<T>(vec, example, "");
163  }
164  template <class T>
165  bool getList(std::vector< boost::shared_ptr<T> > &vec, const FilterClause clause) const
166  {
167  T example;
168  return getList<T>(vec, example, clause.clause_);
169  }
170  template <class T>
171  bool getList(std::vector< boost::shared_ptr<T> > &vec, std::string where_clause) const
172  {
173  T example;
174  return getList<T>(vec, example, where_clause);
175  }
176 
177  //------- retrieval with examples -------
178  template <class T>
179  bool getList(std::vector< boost::shared_ptr<T> > &vec, const T &example) const
180  {
181  return getList<T>(vec, example, "");
182  }
183  template <class T>
184  bool getList(std::vector< boost::shared_ptr<T> > &vec, const T &example, const FilterClause clause) const
185  {
186  return getList<T>(vec, example, clause.clause_);
187  }
188 
190  bool countList(const DBClass *example, int &count, std::string where_clause) const;
191 
193  template <typename T>
194  bool countList(int &count, const FilterClause clause=FilterClause()) const
195  {
196  T example;
197  return countList(&example, count, clause.clause_);
198  }
199 
201  bool saveToDatabase(const DBFieldBase* field);
202 
204  bool loadFromDatabase(DBFieldBase* field) const;
205 
207  bool insertIntoDatabase(DBClass* instance);
208 
210  bool deleteFromDatabase(DBClass* instance);
211 
212 };
213 
234 template <class T>
235 bool PostgresqlDatabase::getList(std::vector< boost::shared_ptr<T> > &vec,
236  const T &example, std::string where_clause) const
237 {
238  //we will store here the fields to be retrieved retrieve from the database
239  std::vector<const DBFieldBase*> fields;
240  //we will store here their index in the result returned from the database
241  std::vector<int> column_ids;
242  boost::shared_ptr<PGresultAutoPtr> result;
243 
244  int num_tuples;
245  //do all the heavy lifting of querying the database and getting the raw result
246  if (!getListRawResult(&example, fields, column_ids, where_clause, result, num_tuples))
247  {
248  return false;
249  }
250 
251  vec.clear();
252  if (!num_tuples)
253  {
254  return true;
255  }
256 
257  //parse the raw result and populate the list
258  for (int i=0; i<num_tuples; i++)
259  {
260  boost::shared_ptr<T> entry(new T);
261  if (populateListEntry(entry.get(), result, i, fields, column_ids))
262  {
263  vec.push_back(entry);
264  }
265  }
266  return true;
267 }
268 
269 
270 }//namespace
271 
272 #endif
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
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...
Definition: db_field.h:72
The base class for all C++ classes that can be stored in the database.
Definition: db_class.h:64
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.
struct pg_conn PGconn
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.
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.
Definition: db_field.h:158
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.


database_interface
Author(s): Matei Ciocarlie and Lorenz Mosenlechner
autogenerated on Mon Mar 2 2015 01:31:34