Main MRPT website > C++ reference
MRPT logo
CSimpleDatabase.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CSimpleDatabase_H
29 #define CSimpleDatabase_H
30 
31 #include <mrpt/utils/utils_defs.h>
33 
34 namespace mrpt
35 {
36 namespace utils
37 {
38 
39  // This must be added to any CSerializable derived class:
41  // This must be added to any CSerializable derived class:
43 
44 /** This class implements the tables of databases.
45  * \sa CSimpleDatabase \ingroup mrpt_base_grp
46  */
47 class BASE_IMPEXP CSimpleDatabaseTable : public mrpt::utils::CSerializable
48 {
49  // This must be added to any CSerializable derived class:
50  DEFINE_SERIALIZABLE( CSimpleDatabaseTable )
51 public:
52  /** Default constructor
53  */
54  CSimpleDatabaseTable( );
55 
56  /** Destructor
57  */
58  virtual ~CSimpleDatabaseTable();
59 
60  /** Get the count of fields.
61  */
62  size_t fieldsCount() const;
63 
64  /** Append a new and empty record at the end of the table, and return the index of the newly added record.
65  * \sa deleteRecord
66  */
67  size_t appendRecord();
68 
69  /** Add a new field to the table. The table is cleared in this operation. */
70  void addField(const char *fieldName);
71 
72  /** Add a new field to the table. The table is cleared in this operation. */
73  void addField(const std::string &fieldName) {
74  addField(fieldName.c_str());
75  }
76 
77  /** Get the name of a field by its index
78  * \exception std::exception On index out of bounds
79  */
80  std::string getFieldName(size_t fieldIndex) const;
81 
82  /** Get the index for a given field name
83  * \exception std::exception On field not found
84  */
85  size_t fieldIndex(const char *fieldName) const;
86 
87  /** Get the index for a given field name
88  * \exception std::exception On field not found
89  */
90  size_t fieldIndex(const std::string &fieldName) const {
91  return fieldIndex(fieldName.c_str());
92  }
93 
94  /** Get the records count in the table
95  */
96  size_t getRecordCount() const;
97 
98  /** Returns the cell content of the record indicates by its index, and the field indicated in "field".
99  * \exception std::exception On field or record not found
100  */
101  std::string get(size_t recordIndex, std::string field) const;
102 
103  /** Returns the cell content of the record indicates by its index, and the field indicated by its index.
104  * \exception std::exception On field or record not found
105  */
106  std::string get(size_t recordIndex, size_t fieldIndex) const;
107 
108  /** Sets the cell content of the record indicates by its index, and the field indicated in "field".
109  * \exception std::exception On field or record not found
110  */
111  void set(size_t recordIndex, std::string field, std::string value);
112 
113  /** Sets the cell content of the record indicates by its index, and the field indicated by its index.
114  * \exception std::exception On field or record not found
115  */
116  void set(size_t recordIndex, size_t fieldIndex, std::string value);
117 
118  /** Executes a query in the table, returning the record index which a given field has a given value, case insensitive, or -1 if not found.
119  */
120  int query(std::string field, std::string value) const;
121 
122  /** Delete the record at the given index \sa appendRecord */
123  void deleteRecord(size_t recordIndex);
124 
125 private:
126  vector_string field_names; //!< Field names
127  std::vector<vector_string> data; //!< Data for each cell
128 
129 }; // end of class definition
130 
131 /** This class impements a very simple database system. A database is
132  * a collection of tables, each one being a CSimpleDatabaseTable object. Tables are
133  * a rectangular arrrangement of cells, organized as records of fields.
134  * There are XML export/import methods in saveAsXML, loadFromXML.
135  *
136  * \note This class is NOT safe for read/write access from different threads. If needed, use critical sections.
137  *
138  * \sa CSimpleDatabaseTable
139  */
141 {
142  // This must be added to any CSerializable derived class:
144 
145 public:
146  /** Default constructor
147  */
148  CSimpleDatabase( );
149 
150  /** Destructor
151  */
152  virtual ~CSimpleDatabase( );
153 
154  /** Clears the DB.
155  */
156  void clear();
157 
158  /** Creates a new table in the DB, initially empty.
159  */
160  CSimpleDatabaseTablePtr createTable(const std::string &name);
161 
162  /** Returns the table with the indicated name
163  * \exception std::exception On table not found.
164  */
165  CSimpleDatabaseTablePtr getTable(const std::string &tableName);
166 
167  /** Deletes the given table.
168  * \exception std::exception On table not found.
169  */
170  void dropTable(const std::string &tableName);
171 
172  /** Changes the name of a given table
173  * \exception std::exception On table not found or new name already existed.
174  */
175  void renameTable(
176  const std::string &tableName,
177  const std::string &newTableName );
178 
179  /** Returns the table by index.
180  * \exception std::exception On index out of bounds
181  */
182  CSimpleDatabaseTablePtr getTable(size_t tableIndex);
183 
184  /** Returns the tables count in the DB.
185  */
186  size_t tablesCount() const;
187 
188  /** Returns the tables names in the DB.
189  * \exception std::exception On index out of bounds
190  */
191  std::string tablesName(size_t tableIndex) const;
192 
193  /** Saves this database as a XML file.
194  * \return false on any error, true if successful.
195  * \sa loadFromXML
196  */
197  bool saveAsXML( const std::string &fileName ) const;
198 
199  /** Loads the content of this database from a a XML file.
200  * \return false on any error, true if successful.
201  * \sa saveAsXML
202  */
203  bool loadFromXML( const std::string &fileName );
204 
205 
206 private:
207 
208  /** The tables of the DB indexed by their names: */
209  typedef std::map<std::string, CSimpleDatabaseTablePtr> TTableList;
210  typedef std::map<std::string, CSimpleDatabaseTablePtr>::iterator iterator;
211  typedef std::map<std::string, CSimpleDatabaseTablePtr>::const_iterator const_iterator;
212 
213  TTableList m_tables;
214 
215 
216 }; // end of class definition
217 
218 
219 } // End of namespace
220 } // End of namespace
221 
222 
223 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013