SearchSECODatabaseAPI
DatabaseHandler.h
1/*
2This program has been developed by students from the bachelor Computer Science at
3Utrecht University within the Software Project course.
4© Copyright Utrecht University (Department of Information and Computing Sciences)
5*/
6
7#pragma once
8#include "Types.h"
9
10#include <tuple>
11#include <cassandra.h>
12
13#define IP "cassandra"
14#define DBPORT 8002
15#define HASHES_INSERT_MAX 1000
16
17using namespace types;
18
23{
24public:
30 virtual void connect(std::string ip, int port);
31
37 virtual bool addProject(ProjectIn project);
38
44 virtual void addHashToProject(ProjectIn project, int index);
45
55 virtual ProjectOut searchForProject(ProjectID projectID, Version version);
56
65 virtual ProjectOut prevProject(ProjectID projectID);
66
76 virtual void addMethod(MethodIn method, ProjectIn project, long long prevVersion, long long parserVersion,
77 bool newProject);
78
87 virtual std::vector<Hash> updateUnchangedFiles(std::vector<Hash> hashes, std::vector<std::string> files,
88 ProjectIn project, long long prevVersion);
89
95 virtual std::vector<MethodOut> hashToMethods(std::string hash);
96
102 virtual Author idToAuthor(std::string id);
103
109 virtual std::vector<MethodID> authorToMethods(std::string authorID);
110
111private:
115 void addMethodByAuthor(CassUuid authorID, MethodIn method, ProjectIn project);
116
120 ProjectOut getProject(const CassRow *row);
121
125 MethodOut getMethod(const CassRow *row);
126
130 MethodID getMethodID(const CassRow *row);
131
138 void addNewMethod(MethodIn method, ProjectIn project, long long parserVersion);
139
147 void updateMethod(MethodIn method, ProjectIn project, long long startVersion);
148
158 void handleSelectMethodQueryResult(CassFuture *queryFuture, MethodIn method, ProjectIn project,
159 long long prevVersion, long long parserVersion, bool &newProject);
160
168 std::vector<Hash> handleSelectUnchangedMethodsResult(CassFuture *queryFuture, ProjectIn project,
169 long long prevVersion);
170
178 CassFuture *executeSelectUnchangedMethodsQuery(std::vector<Hash> hashes, std::vector<std::string> files,
179 ProjectIn project);
185 CassUuid createAuthorIfNotExists(Author author);
186
190 void setPreparedStatements();
191
195 CassSession *connection;
196
200 const CassPrepared *selectMethods;
201 const CassPrepared *selectProject;
202 const CassPrepared *selectPrevProject;
203 const CassPrepared *insertProject;
204 const CassPrepared *addHashesToProject;
205 const CassPrepared *insertMethod;
206 const CassPrepared *insertVulnMethod;
207 const CassPrepared *updateMethods;
208 const CassPrepared *updateUnchangedMethods;
209 const CassPrepared *selectMethod;
210 const CassPrepared *selectUnchangedMethods;
211 const CassPrepared *insertMethodByAuthor;
212 const CassPrepared *selectMethodByAuthor;
213 const CassPrepared *insertAuthorByID;
214 const CassPrepared *selectAuthorByID;
215};
Handles interaction with database.
Definition: DatabaseHandler.h:23
virtual void addHashToProject(ProjectIn project, int index)
Adds a number of hashes to a project in the database, starting at some index.
Definition: DatabaseHandlerSet.cpp:72
virtual ProjectOut searchForProject(ProjectID projectID, Version version)
Searches for a project in the database and returns it to the user if it exists.
Definition: DatabaseHandlerGet.cpp:15
virtual Author idToAuthor(std::string id)
Retrieves an author given its authorID.
Definition: DatabaseHandlerGet.cpp:186
virtual std::vector< MethodID > authorToMethods(std::string authorID)
Retrieves the methods created by an author given its authorID.
Definition: DatabaseHandlerGet.cpp:141
virtual std::vector< MethodOut > hashToMethods(std::string hash)
Retrieves all methods with a given hash.
Definition: DatabaseHandlerGet.cpp:58
virtual bool addProject(ProjectIn project)
Adds a project to database.
Definition: DatabaseHandlerSet.cpp:15
virtual std::vector< Hash > updateUnchangedFiles(std::vector< Hash > hashes, std::vector< std::string > files, ProjectIn project, long long prevVersion)
Updates the methods in the previous version of the project that are part of an unchanged file.
Definition: DatabaseHandlerSet.cpp:317
virtual void addMethod(MethodIn method, ProjectIn project, long long prevVersion, long long parserVersion, bool newProject)
Adds/updates a method to the tables methods and method_by_author. Takes in a method and a project and...
Definition: DatabaseHandlerSet.cpp:117
virtual void connect(std::string ip, int port)
Establishes a connection to the database.
Definition: DatabaseHandler.cpp:13
virtual ProjectOut prevProject(ProjectID projectID)
Retrieves the previous/latest version of the project present in the database.
Definition: DatabaseHandlerGet.cpp:104
Represents the data of an author.
Definition: Types.h:26
Definition: Types.h:82
Represents the relevant data of a method to be put in the database.
Definition: Types.h:50
Represents the data of a method to be returned to the user. Difference with MethodIn: authorIDs inste...
Definition: Types.h:65
Represents the relevant data of a project when it is put into the database.
Definition: Types.h:94
Represents the relevant data of a project when it is returned to user. Difference with ProjectIn: aut...
Definition: Types.h:112