Main MRPT website > C++ reference for MRPT 1.5.9
CSimpleDatabase.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef CSimpleDatabase_H
10 #define CSimpleDatabase_H
11 
12 #include <mrpt/utils/utils_defs.h>
14 #include <map>
15 
16 namespace mrpt
17 {
18 namespace utils
19 {
20 
21  // This must be added to any CSerializable derived class:
23  // This must be added to any CSerializable derived class:
25 
26 /** This class implements the tables of databases.
27  * \sa CSimpleDatabase \ingroup mrpt_base_grp
28  */
30 {
31  // This must be added to any CSerializable derived class:
33 public:
34  /** Default constructor
35  */
37 
38  /** Destructor
39  */
40  virtual ~CSimpleDatabaseTable();
41 
42  /** Get the count of fields.
43  */
44  size_t fieldsCount() const;
45 
46  /** Append a new and empty record at the end of the table, and return the index of the newly added record.
47  * \sa deleteRecord
48  */
49  size_t appendRecord();
50 
51  /** Add a new field to the table. The table is cleared in this operation. */
52  void addField(const char *fieldName);
53 
54  /** Add a new field to the table. The table is cleared in this operation. */
55  void addField(const std::string &fieldName) {
56  addField(fieldName.c_str());
57  }
58 
59  /** Get the name of a field by its index
60  * \exception std::exception On index out of bounds
61  */
62  std::string getFieldName(size_t fieldIndex) const;
63 
64  /** Get the index for a given field name
65  * \exception std::exception On field not found
66  */
67  size_t fieldIndex(const char *fieldName) const;
68 
69  /** Get the index for a given field name
70  * \exception std::exception On field not found
71  */
72  size_t fieldIndex(const std::string &fieldName) const {
73  return fieldIndex(fieldName.c_str());
74  }
75 
76  /** Get the records count in the table
77  */
78  size_t getRecordCount() const;
79 
80  /** Returns the cell content of the record indicates by its index, and the field indicated in "field".
81  * \exception std::exception On field or record not found
82  */
83  std::string get(size_t recordIndex, std::string field) const;
84 
85  /** Returns the cell content of the record indicates by its index, and the field indicated by its index.
86  * \exception std::exception On field or record not found
87  */
88  std::string get(size_t recordIndex, size_t fieldIndex) const;
89 
90  /** Sets the cell content of the record indicates by its index, and the field indicated in "field".
91  * \exception std::exception On field or record not found
92  */
93  void set(size_t recordIndex, std::string field, std::string value);
94 
95  /** Sets the cell content of the record indicates by its index, and the field indicated by its index.
96  * \exception std::exception On field or record not found
97  */
98  void set(size_t recordIndex, size_t fieldIndex, std::string value);
99 
100  /** 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.
101  */
102  int query(std::string field, std::string value) const;
103 
104  /** Delete the record at the given index \sa appendRecord */
105  void deleteRecord(size_t recordIndex);
106 
107 private:
108  vector_string field_names; //!< Field names
109  std::vector<vector_string> data; //!< Data for each cell
110 
111 }; // end of class definition
112 
113 /** This class impements a very simple database system. A database is
114  * a collection of tables, each one being a CSimpleDatabaseTable object. Tables are
115  * a rectangular arrrangement of cells, organized as records of fields.
116  * There are XML export/import methods in saveAsXML, loadFromXML.
117  *
118  * \note This class is NOT safe for read/write access from different threads. If needed, use critical sections.
119  *
120  * \sa CSimpleDatabaseTable
121  */
123 {
124  // This must be added to any CSerializable derived class:
126 
127 public:
128  /** Default constructor
129  */
130  CSimpleDatabase( );
131 
132  /** Destructor
133  */
134  virtual ~CSimpleDatabase( );
135 
136  /** Clears the DB.
137  */
138  void clear();
139 
140  /** Creates a new table in the DB, initially empty.
141  */
142  CSimpleDatabaseTablePtr createTable(const std::string &name);
143 
144  /** Returns the table with the indicated name
145  * \exception std::exception On table not found.
146  */
147  CSimpleDatabaseTablePtr getTable(const std::string &tableName);
148 
149  /** Deletes the given table.
150  * \exception std::exception On table not found.
151  */
152  void dropTable(const std::string &tableName);
153 
154  /** Changes the name of a given table
155  * \exception std::exception On table not found or new name already existed.
156  */
157  void renameTable(
158  const std::string &tableName,
159  const std::string &newTableName );
160 
161  /** Returns the table by index.
162  * \exception std::exception On index out of bounds
163  */
164  CSimpleDatabaseTablePtr getTable(size_t tableIndex);
165 
166  /** Returns the tables count in the DB.
167  */
168  size_t tablesCount() const;
169 
170  /** Returns the tables names in the DB.
171  * \exception std::exception On index out of bounds
172  */
173  std::string tablesName(size_t tableIndex) const;
174 
175  /** Saves this database as a XML file.
176  * \return false on any error, true if successful.
177  * \sa loadFromXML
178  */
179  bool saveAsXML( const std::string &fileName ) const;
180 
181  /** Loads the content of this database from a a XML file.
182  * \return false on any error, true if successful.
183  * \sa saveAsXML
184  */
185  bool loadFromXML( const std::string &fileName );
186 
187 
188 private:
189 
190  /** The tables of the DB indexed by their names: */
191  typedef std::map<std::string, CSimpleDatabaseTablePtr> TTableList;
194 
196 
197 
198 }; // end of class definition
199 
202 
203 
204 } // End of namespace
205 } // End of namespace
206 
207 
208 #endif
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:39
Scalar * iterator
Definition: eigen_plugins.h:23
vector_string field_names
Field names.
size_t fieldIndex(const std::string &fieldName) const
Get the index for a given field name.
const Scalar * const_iterator
Definition: eigen_plugins.h:24
std::map< std::string, CSimpleDatabaseTablePtr >::const_iterator const_iterator
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:113
std::vector< std::string > vector_string
A type for passing a vector of strings.
Definition: types_simple.h:30
This class implements the tables of databases.
std::map< std::string, CSimpleDatabaseTablePtr >::iterator iterator
#define DEFINE_SERIALIZABLE_PRE_CUSTOM_BASE(class_name, base_name)
This declaration must be inserted in all CSerializable classes definition, before the class declarati...
GLsizei const GLchar ** string
Definition: glext.h:3919
This class impements a very simple database system.
void addField(const std::string &fieldName)
Add a new field to the table.
std::map< std::string, CSimpleDatabaseTablePtr > TTableList
The tables of the DB indexed by their names:
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
#define DEFINE_SERIALIZABLE_POST_CUSTOM_BASE(class_name, base_name)
GLuint const GLchar * name
Definition: glext.h:3891
GLsizei const GLfloat * value
Definition: glext.h:3929
std::vector< vector_string > data
Data for each cell.



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020