44 for (
const_iterator i = m_tables.begin(); i != m_tables.end(); ++i)
74 mrpt::make_aligned_shared<CSimpleDatabaseTable>();
77 m_tables[aux] = newTb;
101 for (col = 0; col < nFie; col++) out << field_names[col];
104 for (col = 0; col < nFie; col++) out <<
data[
row][col];
122 field_names.resize(nFie);
124 for (col = 0; col < nFie; col++) in >> field_names[col];
130 for (col = 0; col < nFie; col++) in >>
data[
row][col];
159 iterator it = m_tables.find(tableName);
160 if (it != m_tables.end())
return it->second;
174 ASSERT_(tableIndex < tablesCount())
177 std::advance(it, tableIndex);
194 ASSERT_(tableIndex < tablesCount())
196 std::advance(it, tableIndex);
208 mrpt::make_aligned_shared<CSimpleDatabaseTable>();
230 field_names.push_back(
string(fieldName));
241 ASSERT_(fieldIndex < fieldsCount());
242 return field_names[fieldIndex];
254 size_t i,
n = field_names.size();
256 for (i = 0; i <
n; i++)
257 if (!
os::_strcmpi(fieldName, field_names[i].c_str()))
return (
int)i;
274 ASSERT_(recordIndex < getRecordCount());
275 return data[recordIndex][fieldIndex(field.c_str())];
285 ASSERT_(recordIndex < getRecordCount());
286 ASSERT_(fieldIndex < fieldsCount());
287 return data[recordIndex][fieldIndex];
298 ASSERT_(recordIndex < getRecordCount());
299 data[recordIndex][fieldIndex(field.c_str())] =
value;
308 size_t recordIndex,
size_t fieldIndex,
string value)
312 ASSERT_(recordIndex < getRecordCount());
313 ASSERT_(fieldIndex < fieldsCount());
324 int fieldInd, i,
n = (
uint32_t)getRecordCount();
328 fieldInd = (
uint32_t)fieldIndex(field.c_str());
335 for (i = 0; i <
n; i++)
351 new_rec.resize(fieldsCount());
352 data.push_back(new_rec);
354 return data.size() - 1;
363 ASSERT_(recordIndex < getRecordCount())
366 std::advance(it, recordIndex);
384 for (
const_iterator it = m_tables.begin(); it != m_tables.end(); ++it)
392 size_t nFields =
t->fieldsCount();
393 size_t nRecs =
t->getRecordCount();
396 for (
unsigned int i = 0; i < nFields; i++)
397 fNod.
addChild(
t->getFieldName(i).c_str());
401 for (
unsigned int i = 0; i < nRecs; i++)
404 for (
size_t j = 0; j < nFields; j++)
407 recNod.
addChild(
t->getFieldName(j).c_str());
408 recContent.
addText(
t->get(i, j).c_str());
420 cerr <<
"[CSimpleDatabase::saveAsXML] Exception ignored:" << endl
442 cerr <<
"[CSimpleDatabase::loadFromXML] Error loading XML file: "
448 root = root.
getChildNode(
"CSimpleDatabase-MRPT-Object");
451 cerr <<
"[CSimpleDatabase::loadFromXML] Loaded XML file does not "
452 "have a 'CSimpleDatabase-MRPT-Object' tag";
460 size_t i, j, nTables = root.
nChildNode(
"table");
461 for (i = 0; i < nTables; i++)
475 for (j = 0; j < nFields; j++)
482 for (
size_t k = 0; k < nRecs; k++)
484 size_t recIdx =
t->appendRecord();
489 for (j = 0; j < nFields; j++)
494 t->set(recIdx, j, str !=
nullptr ?
string(str) :
string());
505 cerr <<
"[CSimpleDatabase::loadFromXML] Exception ignored:" << endl
522 iterator it = m_tables.find(tableName);
523 if (it == m_tables.end())
539 if (tableName == newTableName)
return;
541 iterator it = m_tables.find(tableName);
542 if (it == m_tables.end())
546 iterator itNew = m_tables.find(newTableName);
547 if (itNew != m_tables.end())
549 "A table with the name '%s' already exists",
550 newTableName.c_str())
556 m_tables[newTableName] = tb;
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
The virtual base class which provides a unified interface for all persistent objects in MRPT.
This class impements a very simple database system.
virtual ~CSimpleDatabase()
Destructor.
void clear()
Clears the DB.
std::string tablesName(size_t tableIndex) const
Returns the tables names in the DB.
std::map< std::string, CSimpleDatabaseTable::Ptr >::const_iterator const_iterator
bool saveAsXML(const std::string &fileName) const
Saves this database as a XML file.
void renameTable(const std::string &tableName, const std::string &newTableName)
Changes the name of a given table.
std::map< std::string, CSimpleDatabaseTable::Ptr >::iterator iterator
bool loadFromXML(const std::string &fileName)
Loads the content of this database from a a XML file.
CSimpleDatabaseTable::Ptr getTable(const std::string &tableName)
Returns the table with the indicated name.
CSimpleDatabaseTable::Ptr createTable(const std::string &name)
Creates a new table in the DB, initially empty.
void dropTable(const std::string &tableName)
Deletes the given table.
CSimpleDatabase()
Default constructor.
size_t tablesCount() const
Returns the tables count in the DB.
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
This class implements the tables of databases.
size_t fieldIndex(const char *fieldName) const
Get the index for a given field name.
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
void addField(const char *fieldName)
Add a new field to the table.
size_t fieldsCount() const
Get the count of fields.
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".
void deleteRecord(size_t recordIndex)
Delete the record at the given index.
std::shared_ptr< CSimpleDatabaseTable > Ptr
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,...
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".
CSimpleDatabaseTable()
Default constructor.
size_t getRecordCount() const
Get the records count in the table.
std::string getFieldName(size_t fieldIndex) const
Get the name of a field by its index.
size_t appendRecord()
Append a new and empty record at the end of the table, and return the index of the newly added record...
void readFromStream(mrpt::utils::CStream &in, int version) override
Introduces a pure virtual method responsible for loading from a CStream This can not be used directly...
virtual ~CSimpleDatabaseTable()
Destructor.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
GLenum GLenum GLvoid * row
GLsizei GLsizei GLenum GLenum const GLvoid * data
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
GLenum GLsizei GLenum GLenum const GLvoid * table
GLuint const GLchar * name
GLsizei const GLfloat * value
GLsizei const GLchar ** string
XMLError writeToFile(XMLCSTR filename, const char *encoding=nullptr, char nFormat=1) const
Save the content of an xmlNode inside a file.
static XMLCSTR getError(XMLError error)
this gives you a
static XMLNode parseFile(XMLCSTR filename, XMLCSTR tag=nullptr, XMLResults *pResults=nullptr)
Parse an XML file and return the root of a XMLNode tree representing the file.
XMLCSTR addText(XMLCSTR lpszValue, XMLElementPosition pos=-1)
Add a new text content.
XMLNode addChild(XMLCSTR lpszName, char isDeclaration=FALSE, XMLElementPosition pos=-1)
Add a new child node.
static XMLNode createXMLTopNode(XMLCSTR lpszName, char isDeclaration=FALSE)
Create the top node of an XMLNode structure.
XMLAttribute * addAttribute(XMLCSTR lpszName, XMLCSTR lpszValuev)
it will be detached from it's parents before being attached to the current XMLNode
std::vector< std::string > vector_string
A type for passing a vector of strings.
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.
int nChildNode(XMLCSTR name) const
return the number of child node with specific name
XMLAttribute getAttribute(int i=0) const
return ith attribute
XMLCSTR getName() const
name of the node
char isEmpty() const
is this node Empty?
XMLNode getChildNode(int i=0) const
return ith child node
XMLCSTR getText(int i=0) const
return ith text field
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values,...
void clear()
Clear the contents of this container.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
unsigned __int32 uint32_t
Main Class representing a XML node.
Structure used to obtain error details if the parse fails.