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
mrpt::db::CSimpleDatabase::const_iterator
std::map< std::string, CSimpleDatabaseTable::Ptr >::const_iterator const_iterator
Definition: CSimpleDatabase.h:190
mrpt::db::CSimpleDatabaseTable::fieldIndex
size_t fieldIndex(const std::string &fieldName) const
Get the index for a given field name.
Definition: CSimpleDatabase.h:61
mrpt::db::CSimpleDatabaseTable::getFieldName
std::string getFieldName(size_t fieldIndex) const
Get the name of a field by its index.
Definition: CSimpleDatabase.cpp:216
const_iterator
const Scalar * const_iterator
Definition: eigen_plugins.h:27
mrpt::db::CSimpleDatabaseTable::addField
void addField(const std::string &fieldName)
Add a new field to the table.
Definition: CSimpleDatabase.h:47
mrpt::db::CSimpleDatabaseTable::fieldsCount
size_t fieldsCount() const
Get the count of fields.
Definition: CSimpleDatabase.cpp:203
mrpt::db::CSimpleDatabase::getTable
CSimpleDatabaseTable::Ptr getTable(const std::string &tableName)
Returns the table with the indicated name.
Definition: CSimpleDatabase.cpp:134
mrpt::db::CSimpleDatabaseTable::addField
void addField(const char *fieldName)
Add a new field to the table.
Definition: CSimpleDatabase.cpp:207
mrpt::db::CSimpleDatabaseTable::field_names
std::vector< std::string > field_names
Field names.
Definition: CSimpleDatabase.h:104
mrpt::db::CSimpleDatabase::~CSimpleDatabase
virtual ~CSimpleDatabase()
Destructor.
Definition: CSimpleDatabase.cpp:126
mrpt::db::CSimpleDatabaseTable::deleteRecord
void deleteRecord(size_t recordIndex)
Delete the record at the given index.
Definition: CSimpleDatabase.cpp:339
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::db::CSimpleDatabase::TTableList
std::map< std::string, CSimpleDatabaseTable::Ptr > TTableList
The tables of the DB indexed by their names:
Definition: CSimpleDatabase.h:187
mrpt::db::CSimpleDatabase::saveAsXML
bool saveAsXML(const std::string &fileName) const
Saves this database as a XML file.
Definition: CSimpleDatabase.cpp:353
mrpt::db::CSimpleDatabaseTable::data
std::vector< std::vector< std::string > > data
Data for each cell.
Definition: CSimpleDatabase.h:106
mrpt::db::CSimpleDatabase::tablesName
std::string tablesName(size_t tableIndex) const
Returns the tables names in the DB.
Definition: CSimpleDatabase.cpp:169
name
GLuint const GLchar * name
Definition: glext.h:4054
mrpt::db::CSimpleDatabaseTable::query
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,...
Definition: CSimpleDatabase.cpp:301
mrpt::db::CSimpleDatabaseTable::appendRecord
size_t appendRecord()
Append a new and empty record at the end of the table, and return the index of the newly added record...
Definition: CSimpleDatabase.cpp:326
mrpt::db::CSimpleDatabaseTable
This class implements the tables of databases.
Definition: CSimpleDatabase.h:21
mrpt::db::CSimpleDatabaseTable::CSimpleDatabaseTable
CSimpleDatabaseTable()
Default constructor.
Definition: CSimpleDatabase.cpp:195
mrpt::db::CSimpleDatabaseTable::getRecordCount
size_t getRecordCount() const
Get the records count in the table.
Definition: CSimpleDatabase.cpp:246
mrpt::db::CSimpleDatabaseTable::Ptr
std::shared_ptr< CSimpleDatabaseTable > Ptr
Definition: CSimpleDatabase.h:23
mrpt::db::CSimpleDatabaseTable::set
void set(size_t recordIndex, std::string field, std::string value)
Sets the cell content of the record indicates by its index, and the field indicated in "field".
Definition: CSimpleDatabase.cpp:273
mrpt::db::CSimpleDatabaseTable::~CSimpleDatabaseTable
virtual ~CSimpleDatabaseTable()
Destructor.
Definition: CSimpleDatabase.cpp:199
mrpt::db::CSimpleDatabase::m_tables
TTableList m_tables
Definition: CSimpleDatabase.h:192
mrpt::serialization::CSerializable
The virtual base class which provides a unified interface for all persistent objects in MRPT.
Definition: CSerializable.h:32
mrpt::db::CSimpleDatabase::tablesCount
size_t tablesCount() const
Returns the tables count in the DB.
Definition: CSimpleDatabase.cpp:165
mrpt::db::CSimpleDatabase::loadFromXML
bool loadFromXML(const std::string &fileName)
Loads the content of this database from a a XML file.
Definition: CSimpleDatabase.cpp:411
mrpt::db::CSimpleDatabase::renameTable
void renameTable(const std::string &tableName, const std::string &newTableName)
Changes the name of a given table.
Definition: CSimpleDatabase.cpp:509
mrpt::db::CSimpleDatabase::dropTable
void dropTable(const std::string &tableName)
Deletes the given table.
Definition: CSimpleDatabase.cpp:493
mrpt::db::CSimpleDatabase::iterator
std::map< std::string, CSimpleDatabaseTable::Ptr >::iterator iterator
Definition: CSimpleDatabase.h:188
DEFINE_SERIALIZABLE
#define DEFINE_SERIALIZABLE(class_name)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
Definition: CSerializable.h:102
value
GLsizei const GLfloat * value
Definition: glext.h:4117
mrpt::db::CSimpleDatabaseTable::get
std::string get(size_t recordIndex, std::string field) const
Returns the cell content of the record indicates by its index, and the field indicated in "field".
Definition: CSimpleDatabase.cpp:250
mrpt::db::CSimpleDatabase::CSimpleDatabase
CSimpleDatabase()
Default constructor.
Definition: CSimpleDatabase.cpp:122
mrpt::db::CSimpleDatabase
This class impements a very simple database system.
Definition: CSimpleDatabase.h:121
string
GLsizei const GLchar ** string
Definition: glext.h:4101
iterator
Scalar * iterator
Definition: eigen_plugins.h:26
CSerializable.h
mrpt::db::CSimpleDatabaseTable::fieldIndex
size_t fieldIndex(const char *fieldName) const
Get the index for a given field name.
Definition: CSimpleDatabase.cpp:229
mrpt::db::CSimpleDatabase::clear
void clear()
Clears the DB.
Definition: CSimpleDatabase.cpp:130
mrpt::db::CSimpleDatabase::createTable
CSimpleDatabaseTable::Ptr createTable(const std::string &name)
Creates a new table in the DB, initially empty.
Definition: CSimpleDatabase.cpp:184



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST