#ifndef SQLITE_STORAGE_H #define SQLITE_STORAGE_H #include #include #include #include "sqlite/CppSQLite3.h" #include "data/location/TokenLocationFile.h" #include "data/location/TokenLocationCollection.h" #include "data/name/NameHierarchy.h" #include "data/StorageTypes.h" #include "data/SqliteIndex.h" #include "utility/file/FilePath.h" #include "utility/types.h" #include "utility/utility.h" #include "utility/utilityString.h" class TextAccess; class Version; struct ParseLocation; class SqliteStorage { public: enum StorageModeType { STORAGE_MODE_UNKNOWN = 0, STORAGE_MODE_READ = 1, STORAGE_MODE_WRITE = 2, STORAGE_MODE_CLEAR = 4, }; SqliteStorage(const FilePath& dbFilePath); ~SqliteStorage(); void setup(); void clear(); void setMode(const StorageModeType mode); void beginTransaction(); void commitTransaction(); void rollbackTransaction(); void optimizeMemory() const; FilePath getDbFilePath() const; bool isEmpty() const; bool isIncompatible() const; std::string getProjectSettingsText() const; void setProjectSettingsText(std::string text); void setVersion(); Id addEdge(int type, Id sourceNodeId, Id targetNodeId); Id addNode(const int type, const std::string& serializedName); void addSymbol(const int id, int definitionKind); void addFile(const int id, const std::string& filePath, const std::string& modificationTime); Id addLocalSymbol(const std::string& name); Id addSourceLocation(Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol, int type); bool addOccurrence(Id elementId, Id sourceLocationId); Id addComponentAccess(Id nodeId, int type); Id addCommentLocation(Id fileNodeId, uint startLine, uint startCol, uint endLine, uint endCol); Id addError(const std::string& message, const FilePath& filePath, uint lineNumber, uint columnNumber, bool fatal, bool indexed); void removeElement(Id id); void removeElements(const std::vector& ids); void removeElementsWithLocationInFiles(const std::vector& fileIds); void removeErrorsInFiles(const std::vector& filePaths); bool isEdge(Id elementId) const; bool isNode(Id elementId) const; bool isFile(Id elementId) const; StorageEdge getEdgeBySourceTargetType(Id sourceId, Id targetId, int type) const; std::vector getEdgesBySourceId(Id sourceId) const; std::vector getEdgesBySourceIds(const std::vector& sourceIds) const; std::vector getEdgesByTargetId(Id targetId) const; std::vector getEdgesByTargetIds(const std::vector& targetIds) const; std::vector getEdgesBySourceOrTargetId(Id id) const; std::vector getEdgesByType(int type) const; std::vector getEdgesBySourceType(Id sourceId, int type) const; std::vector getEdgesBySourcesType(const std::vector& sourceIds, int type) const; std::vector getEdgesByTargetType(Id targetId, int type) const; std::vector getEdgesByTargetsType(const std::vector& targetIds, int type) const; StorageNode getNodeBySerializedName(const std::string& serializedName) const; StorageLocalSymbol getLocalSymbolByName(const std::string& name) const; StorageFile getFileByPath(const std::string& filePath) const; std::vector getFilesByPaths(const std::vector& filePaths) const; std::shared_ptr getFileContentByPath(const std::string& filePath) const; std::shared_ptr getFileContentById(Id fileId) const; void setNodeType(int type, Id nodeId); StorageSourceLocation getSourceLocationByAll(const Id fileNodeId, const uint startLine, const uint startCol, const uint endLine, const uint endCol, const int type) const; std::shared_ptr getTokenLocationsForFile(const FilePath& filePath) const; std::vector getOccurrencesForLocationId(Id locationId) const; std::vector getOccurrencesForLocationIds(const std::vector& locationIds) const; std::vector getOccurrencesForElementIds(const std::vector& elementIds) const; StorageComponentAccess getComponentAccessByNodeId(Id memberEdgeId) const; std::vector getComponentAccessesByNodeIds(const std::vector& memberEdgeIds) const; std::vector getCommentLocationsInFile(const FilePath& filePath) const; template std::vector getAll() const { return doGetAll(""); } template ResultType getFirstById(const Id id) const { if (id != 0) { return doGetFirst("WHERE id == " + std::to_string(id)); } return ResultType(); } template std::vector getAllByIds(const std::vector& ids) const { return doGetAll("WHERE id IN (" + utility::join(utility::toStrings(ids), ',') + ")"); } int getNodeCount() const; int getEdgeCount() const; int getFileCount() const; int getFileLineSum() const; int getSourceLocationCount() const; private: static const size_t STORAGE_VERSION; void clearTables(); void setupTables(); void executeStatement(const std::string& statement) const; void executeStatement(CppSQLite3Statement& statement) const; int executeScalar(const std::string& statement) const; CppSQLite3Query executeQuery(const std::string& statement) const; CppSQLite3Query executeQuery(CppSQLite3Statement& statement) const; bool hasTable(const std::string& tableName) const; std::string getMetaValue(const std::string& key) const; void insertOrUpdateMetaValue(const std::string& key, const std::string& value); size_t getStorageVersion() const; void setStorageVersion(); Version getApplicationVersion() const; void setApplicationVersion(); template std::vector doGetAll(const std::string& query) const; template ResultType doGetFirst(const std::string& query) const { std::vector results = doGetAll(query + " LIMIT 1"); if (results.size() > 0) { return results[0]; } return ResultType(); } mutable CppSQLite3DB m_database; FilePath m_dbFilePath; StorageModeType m_mode; std::vector> m_indices; }; template <> std::vector SqliteStorage::doGetAll(const std::string& query) const; template <> std::vector SqliteStorage::doGetAll(const std::string& query) const; template <> std::vector SqliteStorage::doGetAll(const std::string& query) const; template <> std::vector SqliteStorage::doGetAll(const std::string& query) const; template <> std::vector SqliteStorage::doGetAll(const std::string& query) const; template <> std::vector SqliteStorage::doGetAll(const std::string& query) const; template <> std::vector SqliteStorage::doGetAll(const std::string& query) const; template <> std::vector SqliteStorage::doGetAll(const std::string& query) const; template <> std::vector SqliteStorage::doGetAll(const std::string& query) const; template <> std::vector SqliteStorage::doGetAll(const std::string& query) const; #endif // SQLITE_STORAGE_H