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