Main MRPT website > C++ reference for MRPT 1.9.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 /** This class implements the tables of databases.
21  * \sa CSimpleDatabase \ingroup mrpt_base_grp
22  */
24 {
26  public:
27  /** Default constructor
28  */
30 
31  /** Destructor
32  */
33  virtual ~CSimpleDatabaseTable();
34 
35  /** Get the count of fields.
36  */
37  size_t fieldsCount() const;
38 
39  /** Append a new and empty record at the end of the table, and return the
40  * index of the newly added record.
41  * \sa deleteRecord
42  */
43  size_t appendRecord();
44 
45  /** Add a new field to the table. The table is cleared in this operation. */
46  void addField(const char* fieldName);
47 
48  /** Add a new field to the table. The table is cleared in this operation. */
49  void addField(const std::string& fieldName) { addField(fieldName.c_str()); }
50  /** Get the name of a field by its index
51  * \exception std::exception On index out of bounds
52  */
53  std::string getFieldName(size_t fieldIndex) const;
54 
55  /** Get the index for a given field name
56  * \exception std::exception On field not found
57  */
58  size_t fieldIndex(const char* fieldName) const;
59 
60  /** Get the index for a given field name
61  * \exception std::exception On field not found
62  */
63  size_t fieldIndex(const std::string& fieldName) const
64  {
65  return fieldIndex(fieldName.c_str());
66  }
67 
68  /** Get the records count in the table
69  */
70  size_t getRecordCount() const;
71 
72  /** Returns the cell content of the record indicates by its index, and the
73  * field indicated in "field".
74  * \exception std::exception On field or record not found
75  */
76  std::string get(size_t recordIndex, std::string field) const;
77 
78  /** Returns the cell content of the record indicates by its index, and the
79  * field indicated by its index.
80  * \exception std::exception On field or record not found
81  */
82  std::string get(size_t recordIndex, size_t fieldIndex) const;
83 
84  /** Sets the cell content of the record indicates by its index, and the
85  * field indicated in "field".
86  * \exception std::exception On field or record not found
87  */
88  void set(size_t recordIndex, std::string field, std::string value);
89 
90  /** Sets the cell content of the record indicates by its index, and the
91  * field indicated by its index.
92  * \exception std::exception On field or record not found
93  */
94  void set(size_t recordIndex, size_t fieldIndex, std::string value);
95 
96  /** Executes a query in the table, returning the record index which a given
97  * field has a given value, case insensitive, or -1 if not found.
98  */
99  int query(std::string field, std::string value) const;
100 
101  /** Delete the record at the given index \sa appendRecord */
102  void deleteRecord(size_t recordIndex);
103 
104  private:
105  /** Field names */
107  /** Data for each cell */
108  std::vector<vector_string> data;
109 
110 }; // end of class definition
111 
112 /** This class impements a very simple database system. A database is
113  * a collection of tables, each one being a CSimpleDatabaseTable object.
114  * 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.
119  * If needed, use critical sections.
120  *
121  * \sa CSimpleDatabaseTable
122  */
124 {
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  */
143 
144  /** Returns the table with the indicated name
145  * \exception std::exception On table not found.
146  */
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
156  * existed.
157  */
158  void renameTable(
159  const std::string& tableName, const std::string& newTableName);
160 
161  /** Returns the table by index.
162  * \exception std::exception On index out of bounds
163  */
164  CSimpleDatabaseTable::Ptr 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  private:
188  /** The tables of the DB indexed by their names: */
189  typedef std::map<std::string, CSimpleDatabaseTable::Ptr> TTableList;
193 
195 
196 }; // end of class definition
197 } // End of namespace
198 } // End of namespace
199 
200 #endif
std::map< std::string, CSimpleDatabaseTable::Ptr >::const_iterator const_iterator
size_t tablesCount() const
Returns the tables count in the DB.
std::string getFieldName(size_t fieldIndex) const
Get the name of a field by its index.
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:44
void deleteRecord(size_t recordIndex)
Delete the record at the given index.
Scalar * iterator
Definition: eigen_plugins.h:26
bool loadFromXML(const std::string &fileName)
Loads the content of this database from a a XML file.
virtual ~CSimpleDatabase()
Destructor.
size_t fieldsCount() const
Get the count of fields.
vector_string field_names
Field names.
CSimpleDatabase()
Default constructor.
size_t fieldIndex(const std::string &fieldName) const
Get the index for a given field name.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
bool saveAsXML(const std::string &fileName) const
Saves this database as a XML file.
std::map< std::string, CSimpleDatabaseTable::Ptr > TTableList
The tables of the DB indexed by their names:
CSimpleDatabaseTable()
Default constructor.
std::map< std::string, CSimpleDatabaseTable::Ptr >::iterator iterator
void renameTable(const std::string &tableName, const std::string &newTableName)
Changes the name of a given table.
std::vector< std::string > vector_string
A type for passing a vector of strings.
Definition: types_simple.h:33
void addField(const char *fieldName)
Add a new field to the table.
This class implements the tables of databases.
void dropTable(const std::string &tableName)
Deletes the given table.
std::string tablesName(size_t tableIndex) const
Returns the tables names in the DB.
std::shared_ptr< CSimpleDatabaseTable > Ptr
CSimpleDatabaseTable::Ptr getTable(const std::string &tableName)
Returns the table with the indicated name.
GLsizei const GLchar ** string
Definition: glext.h:4101
size_t getRecordCount() const
Get the records count in the table.
This class impements a very simple database system.
void addField(const std::string &fieldName)
Add a new field to the table.
size_t appendRecord()
Append a new and empty record at the end of the table, and return the index of the newly added record...
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...
virtual ~CSimpleDatabaseTable()
Destructor.
GLuint const GLchar * name
Definition: glext.h:4054
int query(std::string field, std::string value) const
Executes a query in the table, returning the record index which a given field has a given value...
GLsizei const GLfloat * value
Definition: glext.h:4117
void clear()
Clears the DB.
CSimpleDatabaseTable::Ptr createTable(const std::string &name)
Creates a new table in the DB, initially empty.
std::vector< vector_string > data
Data for each cell.
size_t fieldIndex(const char *fieldName) const
Get the index for a given field name.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019