data: saving file contents to sqlite db

This commit is contained in:
Eberhard Graether
2015-09-30 02:12:49 +02:00
parent 4782efddcb
commit de1af6ce7d
12 changed files with 76 additions and 17 deletions
+23 -3
View File
@@ -3,6 +3,7 @@
#include "data/graph/Node.h"
#include "data/location/TokenLocation.h"
#include "utility/logging/logging.h"
#include "utility/text/TextAccess.h"
#include "utility/utility.h"
#include "utility/utilityString.h"
@@ -80,12 +81,16 @@ Id SqliteStorage::addNode(int type, Id nameId)
Id SqliteStorage::addFile(Id nameId, const std::string& filePath, const std::string& modificationTime)
{
Id id = addNode(Node::NODE_FILE, nameId);
std::shared_ptr<TextAccess> content = TextAccess::createFromFile(filePath);
m_database.execDML((
"INSERT INTO file(id, path, modification_time) VALUES("
+ std::to_string(id) + ", '" + filePath + "', '" + modificationTime + "');"
CppSQLite3Statement stmt = m_database.compileStatement((
"INSERT INTO file(id, path, modification_time, content) VALUES("
+ std::to_string(id) + ", '" + filePath + "', '" + modificationTime + "', ?);"
).c_str());
stmt.bind(1, content->getText().c_str());
stmt.execDML();
return id;
}
@@ -449,6 +454,20 @@ std::vector<StorageFile> SqliteStorage::getAllFiles() const
return getAllFiles("SELECT file.id, node.name_id, file.path, file.modification_time FROM file INNER JOIN node ON file.id = node.id;");
}
std::shared_ptr<TextAccess> SqliteStorage::getFileContentByPath(const std::string& filePath) const
{
CppSQLite3Query q = m_database.execQuery((
"SELECT content FROM file WHERE path = '" + filePath + "';"
).c_str());
if (!q.eof())
{
return TextAccess::createFromString(q.getStringField(0, ""));
}
return TextAccess::createFromFile(filePath);
}
void SqliteStorage::setNodeType(int type, Id nodeId)
{
m_database.execDML((
@@ -721,6 +740,7 @@ void SqliteStorage::setupTables()
"id INTEGER NOT NULL, "
"path TEXT, "
"modification_time TEXT, "
"content TEXT, "
"PRIMARY KEY(id), "
"FOREIGN KEY(id) REFERENCES node(id) ON DELETE CASCADE);"
);
+3
View File
@@ -14,6 +14,8 @@
#include "data/location/TokenLocationCollection.h"
#include "data/StorageTypes.h"
class TextAccess;
class SqliteStorage
{
public:
@@ -71,6 +73,7 @@ public:
StorageFile getFileByName(const std::string& fileName) const;
StorageFile getFileByPath(const std::string& filePath) const;
std::vector<StorageFile> getAllFiles() const;
std::shared_ptr<TextAccess> getFileContentByPath(const std::string& filePath) const;
void setNodeType(int type, Id nodeId);
+6 -1
View File
@@ -1149,6 +1149,11 @@ std::shared_ptr<TokenLocationFile> Storage::getTokenLocationOfParentScope(const
return file;
}
std::shared_ptr<TextAccess> Storage::getFileContent(const FilePath& filePath) const
{
return m_sqliteStorage.getFileContentByPath(filePath.str());
}
const SearchIndex& Storage::getSearchIndex() const
{
return m_tokenIndex;
@@ -1293,7 +1298,7 @@ Id Storage::addEdge(Id sourceNodeId, Id targetNodeId, Edge::EdgeType type, Parse
return edgeId;
}
Id Storage::getFileNodeId(const FilePath& filePath)
Id Storage::getFileNodeId(const FilePath& filePath) const
{
std::map<FilePath, Id>::const_iterator it = m_fileNodeIds.find(filePath);
+4 -3
View File
@@ -152,9 +152,10 @@ public:
) const;
virtual TokenLocationCollection getErrorTokenLocations(std::vector<std::string>* errorMessages) const;
virtual std::shared_ptr<TokenLocationFile> getTokenLocationOfParentScope(const TokenLocation* child) const;
virtual std::shared_ptr<TextAccess> getFileContent(const FilePath& filePath) const;
const SearchIndex& getSearchIndex() const;
private:
@@ -166,7 +167,7 @@ private:
Id addEdge(Id sourceNodeId, Id targetNodeId, Edge::EdgeType type);
Id addEdge(Id sourceNodeId, Id targetNodeId, Edge::EdgeType type, ParseLocation location);
Id getFileNodeId(const FilePath& filePath);
Id getFileNodeId(const FilePath& filePath) const;
Id getLastVisibleParentNodeId(const Id nodeId) const;
std::vector<Id> getAllChildNodeIds(const Id nodeId) const;
@@ -191,7 +192,7 @@ private:
SearchIndex m_tokenIndex;
SqliteStorage m_sqliteStorage;
std::map <FilePath, Id> m_fileNodeIds;
mutable std::map <FilePath, Id> m_fileNodeIds;
HierarchyCache m_hierarchyCache;
TokenLocationCollection m_errorLocationCollection;
+3
View File
@@ -13,6 +13,7 @@
struct FileInfo;
class Graph;
class TextAccess;
class TokenLocation;
class TokenLocationCollection;
class TokenLocationFile;
@@ -51,6 +52,8 @@ public:
virtual TokenLocationCollection getErrorTokenLocations(std::vector<std::string>* errorMessages) const = 0;
virtual std::shared_ptr<TokenLocationFile> getTokenLocationOfParentScope(const TokenLocation* child) const = 0;
virtual std::shared_ptr<TextAccess> getFileContent(const FilePath& filePath) const = 0;
};
#endif // STORAGE_ACCESS_H
@@ -224,3 +224,13 @@ std::shared_ptr<TokenLocationFile> StorageAccessProxy::getTokenLocationOfParentS
return std::make_shared<TokenLocationFile>("");
}
std::shared_ptr<TextAccess> StorageAccessProxy::getFileContent(const FilePath& filePath) const
{
if (hasSubject())
{
return m_subject->getFileContent(filePath);
}
return nullptr;
}
+2 -1
View File
@@ -42,9 +42,10 @@ public:
) const;
virtual TokenLocationCollection getErrorTokenLocations(std::vector<std::string>* errorMessages) const;
virtual std::shared_ptr<TokenLocationFile> getTokenLocationOfParentScope(const TokenLocation* child) const;
virtual std::shared_ptr<TextAccess> getFileContent(const FilePath& filePath) const;
private:
StorageAccess* m_subject;
};