20 #undef _UNICODE // JLBC 37 for (
const auto& m_table : m_tables)
40 out << *m_table.second;
64 std::make_shared<CSimpleDatabaseTable>();
67 m_tables[aux] = newTb;
84 for (col = 0; col < nFie; col++) out << field_names[col];
87 for (col = 0; col < nFie; col++) out <<
data[
row][col];
102 field_names.resize(nFie);
104 for (col = 0; col < nFie; col++) in >> field_names[col];
110 for (col = 0; col < nFie; col++) in >>
data[
row][col];
139 auto it = m_tables.find(tableName);
140 if (it != m_tables.end())
return it->second;
154 ASSERT_(tableIndex < tablesCount());
155 auto it = m_tables.begin();
156 std::advance(it, tableIndex);
173 ASSERT_(tableIndex < tablesCount());
174 auto it = m_tables.begin();
175 std::advance(it, tableIndex);
208 field_names.emplace_back(fieldName);
219 ASSERT_(fieldIndex < fieldsCount());
220 return field_names[fieldIndex];
232 size_t i,
n = field_names.size();
234 for (i = 0; i <
n; i++)
235 if (!
os::_strcmpi(fieldName, field_names[i].c_str()))
return (
int)i;
252 ASSERT_(recordIndex < getRecordCount());
253 return data[recordIndex][fieldIndex(field.c_str())];
263 ASSERT_(recordIndex < getRecordCount());
264 ASSERT_(fieldIndex < fieldsCount());
265 return data[recordIndex][fieldIndex];
276 ASSERT_(recordIndex < getRecordCount());
277 data[recordIndex][fieldIndex(field.c_str())] =
value;
286 size_t recordIndex,
size_t fieldIndex,
string value)
290 ASSERT_(recordIndex < getRecordCount());
291 ASSERT_(fieldIndex < fieldsCount());
302 int fieldInd, i,
n = (
uint32_t)getRecordCount();
306 fieldInd = (
uint32_t)fieldIndex(field.c_str());
313 for (i = 0; i <
n; i++)
327 std::vector<std::string> new_rec;
329 new_rec.resize(fieldsCount());
330 data.push_back(new_rec);
332 return data.size() - 1;
341 ASSERT_(recordIndex < getRecordCount());
342 auto it =
data.begin();
343 std::advance(it, recordIndex);
361 for (
const auto& m_table : m_tables)
369 size_t nFields =
t->fieldsCount();
370 size_t nRecs =
t->getRecordCount();
373 for (
unsigned int i = 0; i < nFields; i++)
374 fNod.
addChild(
t->getFieldName(i).c_str());
378 for (
unsigned int i = 0; i < nRecs; i++)
381 for (
size_t j = 0; j < nFields; j++)
384 recNod.
addChild(
t->getFieldName(j).c_str());
385 recContent.
addText(
t->get(i, j).c_str());
397 cerr <<
"[CSimpleDatabase::saveAsXML] Exception ignored:" << endl
419 cerr <<
"[CSimpleDatabase::loadFromXML] Error loading XML file: " 425 root = root.
getChildNode(
"CSimpleDatabase-MRPT-Object");
428 cerr <<
"[CSimpleDatabase::loadFromXML] Loaded XML file does not " 429 "have a 'CSimpleDatabase-MRPT-Object' tag";
437 size_t i, j, nTables = root.
nChildNode(
"table");
438 for (i = 0; i < nTables; i++)
449 size_t nFields = fNod.nChildNode();
450 for (j = 0; j < nFields; j++)
452 t->addField(fNod.getChildNode((
int)j).getName());
457 for (
size_t k = 0; k < nRecs; k++)
459 size_t recIdx =
t->appendRecord();
463 for (j = 0; j < nFields; j++)
468 t->set(recIdx, j, str !=
nullptr ?
string(str) :
string());
479 cerr <<
"[CSimpleDatabase::loadFromXML] Exception ignored:" << endl
496 auto it = m_tables.find(tableName);
497 if (it == m_tables.end())
513 if (tableName == newTableName)
return;
515 auto it = m_tables.find(tableName);
516 if (it == m_tables.end())
520 auto itNew = m_tables.find(newTableName);
521 if (itNew != m_tables.end())
523 "A table with the name '%s' already exists",
524 newTableName.c_str());
530 m_tables[newTableName] = tb;
CSimpleDatabaseTable()
Default constructor.
bool saveAsXML(const std::string &fileName) const
Saves this database as a XML file.
Main Class representing a XML node.
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
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.
XMLError writeToFile(XMLCSTR filename, const char *encoding=nullptr, char nFormat=1) const
Save the content of an xmlNode inside a file.
This class impements a very simple database system.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
size_t tablesCount() const
Returns the tables count in the DB.
char isEmpty() const
is this node Empty?
std::string getFieldName(size_t fieldIndex) const
Get the name of a field by its index.
GLenum GLsizei GLenum GLenum const GLvoid * table
static XMLNode createXMLTopNode(XMLCSTR lpszName, char isDeclaration=FALSE)
Create the top node of an XMLNode structure.
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.
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
~CSimpleDatabase() override
Destructor.
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.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
static XMLCSTR getError(XMLError error)
this gives you a
#define ASSERT_(f)
Defines an assertion mechanism.
XMLAttribute getAttribute(int i=0) const
return ith attribute
map< string, CVectorDouble > results
void dropTable(const std::string &tableName)
Deletes the given table.
CSimpleDatabase()
Default constructor.
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...
void deleteRecord(size_t recordIndex)
Delete the record at the given index.
GLsizei const GLchar ** string
CSimpleDatabaseTable::Ptr getTable(const std::string &tableName)
Returns the table with the indicated name.
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.
Virtual base class for "archives": classes abstracting I/O streams.
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"...
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
~CSimpleDatabaseTable() override
Destructor.
XMLNode addChild(XMLCSTR lpszName, char isDeclaration=FALSE, XMLElementPosition pos=-1)
Add a new child node.
This class implements the tables of databases.
GLsizei const GLfloat * value
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
void renameTable(const std::string &tableName, const std::string &newTableName)
Changes the name of a given table.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
unsigned __int32 uint32_t
GLsizei GLsizei GLenum GLenum const GLvoid * data
void clear()
Clear the contents of this container.
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
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.
int nChildNode(XMLCSTR name) const
return the number of child node with specific name
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
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.