/* * Fetch data */ bool CDatabase::fetch(const RY_PDS::CObjectIndex& index, RY_PDS::CPData &data, bool fetchIndex) { H_AUTO(PDS_Database_fetch); if (!initialised()) { PDS_WARNING("set(): database not initialised"); return false; } CTable* table = getNonConstTable(index.table()); if (table == NULL) { PDS_WARNING("fetch(): unable to get table '%d'", index.table()); return false; } bool success = table->fetch(index.row(), data, fetchIndex); if (success) PDS_FULL_DEBUG("fetch '%s' successfully", index.toString().c_str()); return success; }
/* * Map a row in a table * \param index is the table/row to allocate * \param key is the 64 bits row key * Return true if succeded */ bool CDatabase::mapRow(const RY_PDS::CObjectIndex &index, uint64 key) { if (!initialised()) { PDS_WARNING("mapRow(): database not initialised"); return false; } if (!index.isValid()) { PDS_WARNING("mapRow(): index '%s' is not valid", index.toString().c_str()); return false; } CTable *table = getNonConstTable(index.table()); if (table == NULL || !table->initialised()) { PDS_WARNING("deallocate(): table '%d' is not initialised", index.table()); return false; } bool success = table->mapRow(index, key); if (success) PDS_FULL_DEBUG("mapped '%016"NL_I64"X' to '%s' successfully", key, index.toString().c_str()); return success; }
/* * Dump database content and info of an object to xml */ void CDatabase::dumpToXml(const RY_PDS::CObjectIndex& index, NLMISC::IStream& xml, sint expandDepth) { if (xml.isReading() || !initialised()) return; CTable* table = getNonConstTable(index.table()); if (table != NULL) table->dumpToXml(index.row(), xml, expandDepth); }
/* * Search object in database using its key * \param key is the 64 bits row key to search through all tables */ bool CDatabase::searchObjectIndex(uint64 key, std::set<RY_PDS::CObjectIndex>& indexes) const { indexes.clear(); uint i; for (i=0; i<_Tables.size(); ++i) { if (_Tables[i] == NULL || !_Tables[i]->initialised() || !_Tables[i]->isMapped()) continue; RY_PDS::CObjectIndex index = _Tables[i]->getMappedRow(key); if (index.isValid()) indexes.insert(index); } return !indexes.empty(); }
/* * Fetch data */ bool CDbManager::fetch(TDatabaseId id, RY_PDS::TTableIndex tableIndex, uint64 key, RY_PDS::CPData &data) { CHECK_DB_MGR_INIT(fetch, false); CDatabase* db = getDatabase(id); if (db == NULL) { nlwarning("Unable to fetch(), db '%d' not created yet", id); return false; } RY_PDS::CObjectIndex index = db->getMappedRow(tableIndex, key); if (!index.isValid()) { // row is not mapped return false; } return db->fetch(index, data); }
/** * Release a row in a table * \param index is the table/row to release * Return true if succeded */ bool CDatabase::release(const RY_PDS::CObjectIndex &index) { if (!initialised()) { PDS_WARNING("release(): database not initialised"); return false; } if (!index.isValid()) { PDS_WARNING("release(): index '%s' is not valid", index.toString().c_str()); return false; } CTable *table = getNonConstTable(index.table()); if (table == NULL || !table->initialised()) { PDS_WARNING("release(): table '%d' is not initialised", index.table()); return false; } bool success = table->release(index.row()); if (success) PDS_FULL_DEBUG("released '%s' successfully", index.toString().c_str()); return success; }
/* * Set value with human readable parameters */ bool CDatabase::set(RY_PDS::TTableIndex table, RY_PDS::TRowIndex row, RY_PDS::TColumnIndex column, const std::string& type, const std::string &value) { TDataType datatype = getDataTypeFromName(type); if (!checkDataType(datatype)) return false; switch (datatype) { case PDS_bool: case PDS_char: case PDS_uint8: case PDS_sint8: { uint8 data; NLMISC::fromString(value, data); return set(table, row, column, sizeof(data), &data); } break; case PDS_ucchar: case PDS_uint16: case PDS_sint16: { uint16 data; NLMISC::fromString(value, data); return set(table, row, column, sizeof(data), &data); } break; case PDS_uint32: case PDS_sint32: case PDS_enum: case PDS_CSheetId: case PDS_CNodeId: { uint32 data; NLMISC::fromString(value, data); return set(table, row, column, sizeof(data), &data); } break; case PDS_uint64: case PDS_sint64: { uint64 data; sscanf(value.c_str(), "%016"NL_I64"d", &data); return set(table, row, column, sizeof(data), &data); } break; case PDS_CEntityId: { CEntityId data; data.fromString(value.c_str()); return set(table, row, column, sizeof(data), &data); } break; case PDS_float: { float data = (float)atof(value.c_str()); return set(table, row, column, sizeof(data), &data); } break; case PDS_double: { double data = (double)atof(value.c_str()); return set(table, row, column, sizeof(data), &data); } break; case PDS_Index: { RY_PDS::CObjectIndex data; data.fromString(value.c_str()); return set(table, row, column, sizeof(data), &data); } break; case PDS_dimension: { uint32 data; NLMISC::fromString(value, data); return set(table, row, column, sizeof(data), &data); } break; } return false; }
/* * Tells if an object is allocated * \param object is the object index to test */ bool CDatabase::isAllocated(const RY_PDS::CObjectIndex &index) const { const CTable* table = getTable(index.table()); return table != NULL && table->isAllocated(index.row()); }
/* * Get value as a string * \param path is of the form '[tableindex|tablename].[$key|index].attrib1.attrib2' */ string CDatabase::getValue(const CLocatePath::TLocatePath &path) { if (path.size() < 3) { PDS_WARNING("getValue(): path is too short"); return ""; } uint node = 0; // select table const CTable* table; uint tableId; if (sscanf(path[node].Name.c_str(), "%d", &tableId) == 1) table = getTable(tableId); else table = getTable(path[node].Name); if (table == NULL) { PDS_WARNING("getValue(): unable to select table '%s'", path[node].Name.c_str()); return ""; } ++node; // select row RY_PDS::CObjectIndex object; if (path[node].Name[0] == '$') { uint64 key; if (sscanf(path[node].Name.c_str()+1, "%"NL_I64"X", &key) != 1) { PDS_WARNING("getValue(): unable to select mapped row '%s'", path[node].Name.c_str()); return ""; } object = table->getMappedRow(key); } else { RY_PDS::TRowIndex row; if (sscanf(path[node].Name.c_str(), "%u", &row) != 1) { PDS_WARNING("getValue(): unable to select row '%s'", path[node].Name.c_str()); return ""; } object = RY_PDS::CObjectIndex((RY_PDS::TTableIndex)table->getId(), row); } if (!object.isValid() || !isAllocated(object)) { PDS_WARNING("getValue(): object '%s' not accessible", path[node].Name.c_str()); return ""; } ++node; const CTable* subTable = table; // browse through row while (node < path.size()) { const CAttribute* attribute = subTable->getAttribute(path[node].Name); if (attribute == NULL) { PDS_WARNING("getValue(): '%s' is not an attribute of '%s'", path[node].Name.c_str(), subTable->getName().c_str()); return ""; } ++node; } return ""; }
/* * Map a row in a table * \param index is the table/row to allocate * \param key is the 64 bits row key * Return true if succeded */ bool CDbManager::mapRow(TDatabaseId id, const RY_PDS::CObjectIndex &index, uint64 key) { CHECK_DB_MGR_INIT(mapRow, false); CDatabase* db = getDatabase(id); if (db == NULL) { nlwarning("Unable to mapRow() '%016"NL_I64"X' to row '%d':'%d' in db '%d' , not created yet", key, index.table(), index.row(), id); return false; } return db->mapRow(index, key); }