From cba4e7318f74ef9fc9dcdf1e734a7bc0404d3f3d Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Wed, 12 Dec 2018 15:38:49 +0100 Subject: [PATCH] add method to record file language --- core/CMakeLists.txt | 2 +- core/include/DatabaseStorage.h | 1 + core/include/SourcetrailDBWriter.h | 1 + core/include/StorageFile.h | 13 ++++------ core/src/DatabaseStorage.cpp | 33 ++++++++++++++++++-------- core/src/SourcetrailDBWriter.cpp | 23 +++++++++++++++++- core/test/test.cpp | 18 ++++++++++++++ resources_swig/include/sourcetraildb.h | 2 ++ resources_swig/src/sourcetraildb.cpp | 5 ++++ 9 files changed, 77 insertions(+), 21 deletions(-) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 3041942..590ab15 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -3,7 +3,7 @@ cmake_minimum_required (VERSION 2.6) set(TEST_CORE "test_core") set(INTERFACE_VERSION 0) -set(DATABASE_VERSION 22) +set(DATABASE_VERSION 23) set(COMMIT_VERSION 1) set(VERSION_STRING "v${INTERFACE_VERSION}_db${DATABASE_VERSION}_c${COMMIT_VERSION}") diff --git a/core/include/DatabaseStorage.h b/core/include/DatabaseStorage.h index d337a44..ef89859 100644 --- a/core/include/DatabaseStorage.h +++ b/core/include/DatabaseStorage.h @@ -69,6 +69,7 @@ namespace sourcetrail int addError(const StorageErrorData& storageErrorData); void setNodeType(int nodeId, int nodeKind); + void setFileLanguage(int fileId, const std::string& languageIdentifier); template std::vector getAll() const diff --git a/core/include/SourcetrailDBWriter.h b/core/include/SourcetrailDBWriter.h index c18e48e..2743456 100644 --- a/core/include/SourcetrailDBWriter.h +++ b/core/include/SourcetrailDBWriter.h @@ -78,6 +78,7 @@ namespace sourcetrail bool recordReferenceLocation(int referenceId, const SourceRange& location); int recordFile(const std::string& filePath); + bool recordFileLanguage(int fileId, const std::string& languageIdentifier); int recordLocalSymbol(const std::string& name); bool recordLocalSymbolLocation(int localSymbolId, const SourceRange& location); diff --git a/core/include/StorageFile.h b/core/include/StorageFile.h index 9f4ddc8..1ee1585 100644 --- a/core/include/StorageFile.h +++ b/core/include/StorageFile.h @@ -26,22 +26,16 @@ namespace sourcetrail StorageFile() : id(0) , filePath("") + , languageIdentifier("") , modificationTime("") , indexed(true) , complete(true) {} - StorageFile(int id, std::string filePath, bool indexed, bool complete) - : id(id) - , filePath(std::move(filePath)) - , modificationTime("") - , indexed(indexed) - , complete(complete) - {} - - StorageFile(int id, std::string filePath, std::string modificationTime, bool indexed, bool complete) + StorageFile(int id, std::string filePath, std::string languageIdentifier, std::string modificationTime, bool indexed, bool complete) : id(id) , filePath(std::move(filePath)) + , languageIdentifier(std::move(languageIdentifier)) , modificationTime(std::move(modificationTime)) , indexed(indexed) , complete(complete) @@ -49,6 +43,7 @@ namespace sourcetrail int id; std::string filePath; + std::string languageIdentifier; std::string modificationTime; bool indexed; bool complete; diff --git a/core/src/DatabaseStorage.cpp b/core/src/DatabaseStorage.cpp index 1e25231..1211a62 100644 --- a/core/src/DatabaseStorage.cpp +++ b/core/src/DatabaseStorage.cpp @@ -116,6 +116,7 @@ namespace sourcetrail "CREATE TABLE IF NOT EXISTS file(" " id INTEGER NOT NULL, " " path TEXT, " + " language TEXT, " " modification_time TEXT, " " indexed INTEGER, " " complete INTEGER, " @@ -352,14 +353,15 @@ namespace sourcetrail { CppSQLite3Statement stmt = m_database.compileStatement( - "INSERT OR IGNORE INTO file(id, path, modification_time, indexed, complete, line_count) VALUES(?, ?, ?, ?, ?, ?);" + "INSERT OR IGNORE INTO file(id, path, language, modification_time, indexed, complete, line_count) VALUES(?, ?, ?, ?, ?, ?, ?);" ); stmt.bind(1, storageFile.id); stmt.bind(2, storageFile.filePath.c_str()); - stmt.bind(3, storageFile.modificationTime.c_str()); - stmt.bind(4, storageFile.indexed); - stmt.bind(5, storageFile.complete); - stmt.bind(6, lineCount); + stmt.bind(3, storageFile.languageIdentifier.c_str()); + stmt.bind(4, storageFile.modificationTime.c_str()); + stmt.bind(5, storageFile.indexed); + stmt.bind(6, storageFile.complete); + stmt.bind(7, lineCount); executeStatement(stmt); } @@ -561,6 +563,16 @@ namespace sourcetrail executeStatement(stmt); } + void DatabaseStorage::setFileLanguage(int fileId, const std::string& languageIdentifier) + { + CppSQLite3Statement stmt = m_database.compileStatement( + "UPDATE file SET language = ? WHERE id == ?;" + ); + stmt.bind(1, languageIdentifier.c_str()); + stmt.bind(2, fileId); + executeStatement(stmt); + } + // --- Private Interface --- void DatabaseStorage::insertOrUpdateMetaValue(const std::string& key, const std::string& value) @@ -701,7 +713,7 @@ namespace sourcetrail std::vector DatabaseStorage::doGetAll(const std::string& query) const { CppSQLite3Query q = executeQuery( - "SELECT id, path, modification_time, indexed, complete FROM file " + query + ";" + "SELECT id, path, language, modification_time, indexed, complete FROM file " + query + ";" ); std::vector files; @@ -709,13 +721,14 @@ namespace sourcetrail { const int id = q.getIntField(0, 0); const std::string filePath = q.getStringField(1, ""); - const std::string modificationTime = q.getStringField(2, ""); - const bool indexed = q.getIntField(3, 0); - const bool complete = q.getIntField(4, 0); + const std::string languageIdentifier = q.getStringField(2, ""); + const std::string modificationTime = q.getStringField(3, ""); + const bool indexed = q.getIntField(4, 0); + const bool complete = q.getIntField(5, 0); if (id != 0) { - files.emplace_back(StorageFile(id, filePath, modificationTime, indexed, complete)); + files.emplace_back(StorageFile(id, filePath, languageIdentifier, modificationTime, indexed, complete)); } q.nextRow(); } diff --git a/core/src/SourcetrailDBWriter.cpp b/core/src/SourcetrailDBWriter.cpp index 2ec0879..d955dd4 100644 --- a/core/src/SourcetrailDBWriter.cpp +++ b/core/src/SourcetrailDBWriter.cpp @@ -480,6 +480,27 @@ namespace sourcetrail } } + bool SourcetrailDBWriter::recordFileLanguage(int fileId, const std::string& languageIdentifier) + { + if (!m_storage) + { + m_lastError = "Unable to record file language, because no database is currently open."; + return false; + } + + try + { + m_storage->setFileLanguage(fileId, languageIdentifier); + } + catch (const SourcetrailException e) + { + m_lastError = e.getMessage(); + return false; + } + + return true; + } + int SourcetrailDBWriter::recordLocalSymbol(const std::string& name) { if (!m_storage) @@ -678,7 +699,7 @@ namespace sourcetrail const int nodeId = addNodeHierarchy(nameHierarchy); m_storage->setNodeType(nodeId, nodeKindToInt(NODE_FILE)); - m_storage->addFile(StorageFile(nodeId, filePath, utility::getDateTimeString(time(0)), true, true)); + m_storage->addFile(StorageFile(nodeId, filePath, "", utility::getDateTimeString(time(0)), true, true)); return nodeId; } diff --git a/core/test/test.cpp b/core/test/test.cpp index bb8c4d3..21da4ed 100644 --- a/core/test/test.cpp +++ b/core/test/test.cpp @@ -300,6 +300,7 @@ namespace sourcetrail REQUIRE(files.size() == 1); REQUIRE(files.front().id == idFile1); REQUIRE(files.front().filePath == filePath); + REQUIRE(files.front().languageIdentifier == ""); } SECTION("writer does not record file twice") @@ -312,6 +313,23 @@ namespace sourcetrail REQUIRE(files.size() == 1); } + SECTION("writer records file language") + { + const std::string languageIdentifier = "testlanguage"; + + const bool success = writer.recordFileLanguage( + idFile1, + languageIdentifier + ); + REQUIRE(success); + REQUIRE(writer.getLastError() == ""); + + const std::vector files = storage->getAll(); + REQUIRE(files.size() == 1); + REQUIRE(files.front().id == idFile1); + REQUIRE(files.front().languageIdentifier == languageIdentifier); + } + writer.close(); REQUIRE(writer.getLastError() == ""); } diff --git a/resources_swig/include/sourcetraildb.h b/resources_swig/include/sourcetraildb.h index 3fee721..5b487dd 100644 --- a/resources_swig/include/sourcetraildb.h +++ b/resources_swig/include/sourcetraildb.h @@ -95,6 +95,8 @@ bool recordReferenceLocation(int referenceId, std::string filePath, int startLin int recordFile(std::string filePath); +bool recordFileLanguage(int fileId, std::string languageIdentifier); + int recordLocalSymbol(std::string name); bool recordLocalSymbolLocation(int localSymbolId, std::string filePath, int startLine, int startColumn, int endLine, int endColumn); diff --git a/resources_swig/src/sourcetraildb.cpp b/resources_swig/src/sourcetraildb.cpp index 6e3dbc0..9afab7e 100644 --- a/resources_swig/src/sourcetraildb.cpp +++ b/resources_swig/src/sourcetraildb.cpp @@ -191,6 +191,11 @@ int recordFile(std::string filePath) return dbWriter.recordFile(filePath); } +bool recordFileLanguage(int fileId, std::string languageIdentifier) +{ + return dbWriter.recordFileLanguage(fileId, languageIdentifier); +} + int recordLocalSymbol(std::string name) { return dbWriter.recordLocalSymbol(name);