diff --git a/src/lib/data/SqliteStorage.cpp b/src/lib/data/SqliteStorage.cpp index 1a810871..ea4e6a63 100644 --- a/src/lib/data/SqliteStorage.cpp +++ b/src/lib/data/SqliteStorage.cpp @@ -232,8 +232,11 @@ void SqliteStorage::removeElementsWithLocationInFiles(const std::vector& fil ).c_str()); m_database.execDML(( - "DELETE FROM element WHERE element.id IN (" + utility::join(utility::toStrings(elementIds), ',') + ") AND element.id NOT IN " - "( SELECT source_location.element_id FROM source_location WHERE source_location.element_id == element.id );" + "DELETE FROM element WHERE " + "element.id IN (" + utility::join(utility::toStrings(elementIds), ',') + ") " // skip all elements that dont have a matching id. + "AND element.id NOT IN (" + "SELECT source_location.element_id FROM source_location WHERE source_location.element_id == element.id LIMIT 1" + ");" // delete all elements that dont have a source location. This query is executed for each element that passed the first test. ).c_str()); } @@ -402,23 +405,14 @@ StorageNode SqliteStorage::getNodeById(Id id) const { if (id != 0) { - std::vector nodes = getAllNodes("WHERE id == " + std::to_string(id)); - if (nodes.size()) - { - return nodes[0]; - } + return getFirstNode("WHERE id == " + std::to_string(id)); } return StorageNode(0, 0, 0, definitionTypeToInt(DEFINITION_NONE)); } StorageNode SqliteStorage::getNodeBySerializedName(const std::string& serializedName) const { - std::vector nodes = getAllNodes("WHERE serialized_name == '" + serializedName + "'"); // Todo: use getfirstnode here - if (nodes.size() > 0) - { - return nodes[0]; - } - return StorageNode(0, 0, "", definitionTypeToInt(DEFINITION_NONE)); + return getFirstNode("WHERE serialized_name == '" + serializedName + "'"); } std::vector SqliteStorage::getNodesByIds(const std::vector& nodeIds) const @@ -973,3 +967,13 @@ std::vector SqliteStorage::getAllNodes(const std::string& query) co } return nodes; } + +StorageNode SqliteStorage::getFirstNode(const std::string& query) const +{ + std::vector nodes = getAllNodes(query + " LIMIT 1"); + if (nodes.size() > 0) + { + return nodes[0]; + } + return StorageNode(0, 0, "", definitionTypeToInt(DEFINITION_NONE)); +} diff --git a/src/lib/data/SqliteStorage.h b/src/lib/data/SqliteStorage.h index 32b2e477..91dc58c7 100644 --- a/src/lib/data/SqliteStorage.h +++ b/src/lib/data/SqliteStorage.h @@ -118,6 +118,7 @@ private: std::vector getAllEdges(const std::string& query) const; std::vector getAllNodes(const std::string& query) const; + StorageNode getFirstNode(const std::string& query) const; template ResultType getFirstResult(const std::string& query) const;