FOSSology  4.7.1
Open Source License Compliance by Open Source Software
scancode_dbhandler.hpp
1 /*
2  SPDX-FileCopyrightText: © 2021 Sarita Singh <saritasingh.0425@gmail.com>
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #ifndef SCANCODE_AGENT_DATABASE_HANDLER_HPP
8 #define SCANCODE_AGENT_DATABASE_HANDLER_HPP
9 
10 #include <algorithm>
11 #include <iostream>
12 #include <string>
13 #include <unordered_map>
14 #include <vector>
15 
17 #include "libfossUtils.hpp"
19 #include "match.hpp"
20 
21 extern "C" {
22 #include "libfossology.h"
23 }
24 
25 using namespace std;
26 
30 using namespace fo;
31 
32 #define RETURN_IF_FALSE(query) \
33  do {\
34  if (!(query)) {\
35  return false;\
36  }\
37  } while(0)
38 
39 
40 
45 class DatabaseEntry
46 {
47 public:
48  DatabaseEntry();
49  DatabaseEntry(Match match,unsigned long agentId, unsigned long pfileId);
50  unsigned long agent_fk;
51  unsigned long pfile_fk;
52  std::string content;
53  std::string hash;
63  std::string type;
64  unsigned copy_startbyte;
65  unsigned copy_endbyte;
66 };
67 
69 {
70 public:
73  ScancodeDatabaseHandler spawn() const;
74  long saveLicenseMatch(int agentId, long pFileId, long licenseId, int percentMatch);
75  bool insertNoResultInDatabase(int agentId, long pFileId, long licenseId);
76 
77  bool saveHighlightInfo(long licenseFileId, unsigned start, unsigned length);
78  void insertOrCacheLicenseIdForName(std::string const& rfShortName, std::string const& rfFullname, std::string const& rfTexturl);
79  unsigned long getCachedLicenseIdForName(std::string const& rfShortName) const;
80  bool insertInDatabase(DatabaseEntry& entry) const;
81  std::vector<unsigned long> queryFileIdsForUpload(int uploadId, bool ignoreFilesWithMimeType);
82  bool createTables() const;
83 private:
84  unsigned long selectOrInsertLicenseIdForName(std::string rfShortname, std::string rfFullname, std::string rfTexturl);
85  std::unordered_map<std::string,long> licenseRefCache;
90  struct ColumnDef
91  {
92  const char* name;
93  const char* type;
94  const char* creationFlags;
95  };
96 
97  static const ColumnDef columns_author[];
98  static const ColumnDef columns_copyright[];
99  bool createTableAgentEvents(string tablename) const;
100 
101  static const ColumnDef columns_author_event[];
102  static const ColumnDef columns_copyright_event[];
103  bool createTableAgentFindings(string tablename) const;
104  std::string getColumnCreationString(const ColumnDef in[], size_t size) const;
105 };
106 
107 #endif
Maps agent data to database schema.
Definition: database.hpp:25
unsigned long pfile_fk
unsigned long agent_fk
Database handler for agents.
DB wrapper for agents.
DB utility functions for agents.
General utility functions for CPP.
PGresult * queryFileIdsForUpload(fo_dbManager *dbManager, int uploadId, bool ignoreFilesWithMimeType)
Get all file IDs (pfile_fk) for a given upload.
Definition: libfossagent.c:61
DB wrapper for agents.
The main FOSSology C library.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
fo namespace holds the FOSSology library functions.
start($application)
start the application Assumes application is restartable via /etc/init.d/<script>....
Definition: pkgConfig.php:1214
Definition: match.h:20
Holds the column related data for table creation.
Store the results of a regex match.
Definition: scanners.hpp:28