20 #undef _UNICODE // JLBC 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++)
474 size_t nFields = fNod.nChildNode();
475 for (j = 0; j < nFields; j++)
477 t->addField(fNod.getChildNode((
int)j).getName());
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;
std::map< std::string, CSimpleDatabaseTable::Ptr >::const_iterator const_iterator
size_t tablesCount() const
Returns the tables count in the DB.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
std::string getFieldName(size_t fieldIndex) const
Get the name of a field by its index.
void writeToStream(mrpt::utils::CStream &out, int *getVersion) const override
Introduces a pure virtual method responsible for writing to a CStream.
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Main Class representing a XML node.
The virtual base class which provides a unified interface for all persistent objects in MRPT...
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"...
XMLError writeToFile(XMLCSTR filename, const char *encoding=nullptr, char nFormat=1) const
Save the content of an xmlNode inside a file.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
This must be inserted in all CSerializable classes implementation files.
void deleteRecord(size_t recordIndex)
Delete the record at the given index.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
bool loadFromXML(const std::string &fileName)
Loads the content of this database from a a XML file.
virtual ~CSimpleDatabase()
Destructor.
char isEmpty() const
is this node Empty?
GLenum GLsizei GLenum GLenum const GLvoid * table
size_t fieldsCount() const
Get the count of fields.
static XMLNode createXMLTopNode(XMLCSTR lpszName, char isDeclaration=FALSE)
Create the top node of an XMLNode structure.
CSimpleDatabase()
Default constructor.
bool saveAsXML(const std::string &fileName) const
Saves this database as a XML file.
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...
void clear()
Clear the contents of this container.
static XMLCSTR getError(XMLError error)
this gives you a
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.
void addField(const char *fieldName)
Add a new field to the table.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
This class implements the tables of databases.
XMLAttribute getAttribute(int i=0) const
return ith attribute
void dropTable(const std::string &tableName)
Deletes the given table.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
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
size_t getRecordCount() const
Get the records count in the table.
This class impements a very simple database system.
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.
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.
virtual ~CSimpleDatabaseTable()
Destructor.
GLenum GLenum GLvoid * row
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"...
GLuint const GLchar * name
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...
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
XMLNode addChild(XMLCSTR lpszName, char isDeclaration=FALSE, XMLElementPosition pos=-1)
Add a new child node.
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...
GLsizei const GLfloat * value
unsigned __int32 uint32_t
void clear()
Clears the DB.
GLsizei GLsizei GLenum GLenum const GLvoid * data
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.
int nChildNode(XMLCSTR name) const
return the number of child node with specific name
Structure used to obtain error details if the parse fails.
XMLNode getChildNode(int i=0) const
return ith child node
XMLAttribute * addAttribute(XMLCSTR lpszName, XMLCSTR lpszValuev)
it will be detached from it's parents before being attached to the current XMLNode ...
int _strcmpi(const char *str1, const char *str2) noexcept
An OS-independent version of strcmpi.