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-2018, 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 #pragma once
10 
12 #include <map>
13 
14 namespace mrpt
15 {
16 namespace db
17 {
18 /** This class implements the tables of databases.
19  * \sa CSimpleDatabase \ingroup mrpt_base_grp
20  */
22 {
24  public:
25  /** Default constructor
26  */
28 
29  /** Destructor
30  */
31  virtual ~CSimpleDatabaseTable();
32 
33  /** Get the count of fields.
34  */
35  size_t fieldsCount() const;
36 
37  /** Append a new and empty record at the end of the table, and return the
38  * index of the newly added record.
39  * \sa deleteRecord
40  */
41  size_t appendRecord();
42 
43  /** Add a new field to the table. The table is cleared in this operation. */
44  void addField(const char* fieldName);
45 
46  /** Add a new field to the table. The table is cleared in this operation. */
47  void addField(const std::string& fieldName) { addField(fieldName.c_str()); }
48  /** Get the name of a field by its index
49  * \exception std::exception On index out of bounds
50  */
51  std::string getFieldName(size_t fieldIndex) const;
52 
53  /** Get the index for a given field name
54  * \exception std::exception On field not found
55  */
56  size_t fieldIndex(const char* fieldName) const;
57 
58  /** Get the index for a given field name
59  * \exception std::exception On field not found
60  */
61  size_t fieldIndex(const std::string& fieldName) const
62  {
63  return fieldIndex(fieldName.c_str());
64  }
65 
66  /** Get the records count in the table
67  */
68  size_t getRecordCount() const;
69 
70  /** Returns the cell content of the record indicates by its index, and the
71  * field indicated in "field".
72  * \exception std::exception On field or record not found
73  */
74  std::string get(size_t recordIndex, std::string field) const;
75 
76  /** Returns the cell content of the record indicates by its index, and the
77  * field indicated by its index.
78  * \exception std::exception On field or record not found
79  */
80  std::string get(size_t recordIndex, size_t fieldIndex) const;
81 
82  /** Sets the cell content of the record indicates by its index, and the
83  * field indicated in "field".
84  * \exception std::exception On field or record not found
85  */
86  void set(size_t recordIndex, std::string field, std::string value);
87 
88  /** Sets the cell content of the record indicates by its index, and the
89  * field indicated by its index.
90  * \exception std::exception On field or record not found
91  */
92  void set(size_t recordIndex, size_t fieldIndex, std::string value);
93 
94  /** Executes a query in the table, returning the record index which a given
95  * field has a given value, case insensitive, or -1 if not found.
96  */
97  int query(std::string field, std::string value) const;
98 
99  /** Delete the record at the given index \sa appendRecord */
100  void deleteRecord(size_t recordIndex);
101 
102  private:
103  /** Field names */
104  std::vector<std::string> field_names;
105  /** Data for each cell */
106  std::vector<std::vector<std::string>> data;
107 
108 }; // end of class definition
109 
110 /** This class impements a very simple database system. A database is
111  * a collection of tables, each one being a CSimpleDatabaseTable object.
112  * Tables are
113  * a rectangular arrrangement of cells, organized as records of fields.
114  * There are XML export/import methods in saveAsXML, loadFromXML.
115  *
116  * \note This class is NOT safe for read/write access from different threads.
117  * If needed, use critical sections.
118  *
119  * \sa CSimpleDatabaseTable
120  */
122 {
124 
125  public:
126  /** Default constructor
127  */
128  CSimpleDatabase();
129 
130  /** Destructor
131  */
132  virtual ~CSimpleDatabase();
133 
134  /** Clears the DB.
135  */
136  void clear();
137 
138  /** Creates a new table in the DB, initially empty.
139  */
141 
142  /** Returns the table with the indicated name
143  * \exception std::exception On table not found.
144  */
146 
147  /** Deletes the given table.
148  * \exception std::exception On table not found.
149  */
150  void dropTable(const std::string& tableName);
151 
152  /** Changes the name of a given table
153  * \exception std::exception On table not found or new name already
154  * existed.
155  */
156  void renameTable(
157  const std::string& tableName, const std::string& newTableName);
158 
159  /** Returns the table by index.
160  * \exception std::exception On index out of bounds
161  */
162  CSimpleDatabaseTable::Ptr getTable(size_t tableIndex);
163 
164  /** Returns the tables count in the DB.
165  */
166  size_t tablesCount() const;
167 
168  /** Returns the tables names in the DB.
169  * \exception std::exception On index out of bounds
170  */
171  std::string tablesName(size_t tableIndex) const;
172 
173  /** Saves this database as a XML file.
174  * \return false on any error, true if successful.
175  * \sa loadFromXML
176  */
177  bool saveAsXML(const std::string& fileName) const;
178 
179  /** Loads the content of this database from a a XML file.
180  * \return false on any error, true if successful.
181  * \sa saveAsXML
182  */
183  bool loadFromXML(const std::string& fileName);
184 
185  private:
186  /** The tables of the DB indexed by their names: */
187  using TTableList = std::map<std::string, CSimpleDatabaseTable::Ptr>;
189  using const_iterator =
191 
193 
194 }; // end of class definition
195 } // namespace db
196 } // namespace mrpt
Scalar * iterator
Definition: eigen_plugins.h:26
CSimpleDatabaseTable()
Default constructor.
std::map< std::string, CSimpleDatabaseTable::Ptr >::const_iterator const_iterator
virtual ~CSimpleDatabase()
Destructor.
bool saveAsXML(const std::string &fileName) const
Saves this database as a XML file.
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...
size_t fieldsCount() const
Get the count of fields.
This class impements a very simple database system.
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.
CSimpleDatabaseTable::Ptr createTable(const std::string &name)
Creates a new table in the DB, initially empty.
size_t fieldIndex(const char *fieldName) const
Get the index for a given field name.
std::string tablesName(size_t tableIndex) const
Returns the tables names in the DB.
void addField(const char *fieldName)
Add a new field to the table.
std::vector< std::string > field_names
Field names.
std::map< std::string, CSimpleDatabaseTable::Ptr >::iterator iterator
void dropTable(const std::string &tableName)
Deletes the given table.
CSimpleDatabase()
Default constructor.
std::map< std::string, CSimpleDatabaseTable::Ptr > TTableList
The tables of the DB indexed by their names:
void clear()
Clears the DB.
size_t appendRecord()
Append a new and empty record at the end of the table, and return the index of the newly added record...
virtual ~CSimpleDatabaseTable()
Destructor.
void deleteRecord(size_t recordIndex)
Delete the record at the given index.
GLsizei const GLchar ** string
Definition: glext.h:4101
CSimpleDatabaseTable::Ptr getTable(const std::string &tableName)
Returns the table with the indicated name.
std::vector< std::vector< std::string > > data
Data for each cell.
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...
GLuint const GLchar * name
Definition: glext.h:4054
void addField(const std::string &fieldName)
Add a new field to the table.
The virtual base class which provides a unified interface for all persistent objects in MRPT...
Definition: CSerializable.h:32
This class implements the tables of databases.
GLsizei const GLfloat * value
Definition: glext.h:4117
void renameTable(const std::string &tableName, const std::string &newTableName)
Changes the name of a given table.
const Scalar * const_iterator
Definition: eigen_plugins.h:27
size_t getRecordCount() const
Get the records count in the table.
bool loadFromXML(const std::string &fileName)
Loads the content of this database from a a XML file.
size_t fieldIndex(const std::string &fieldName) const
Get the index for a given field name.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019