FOSSology  4.4.0
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 #define MAX_TABLE_CREATION_RETRIES 5
41 
46 class DatabaseEntry
47 {
48 public:
49  DatabaseEntry();
50  DatabaseEntry(Match match,unsigned long agentId, unsigned long pfileId);
51  unsigned long agent_fk;
52  unsigned long pfile_fk;
53  std::string content;
54  std::string hash;
64  std::string type;
65  unsigned copy_startbyte;
66  unsigned copy_endbyte;
67 };
68 
70 {
71 public:
74  ScancodeDatabaseHandler spawn() const;
75  long saveLicenseMatch(int agentId, long pFileId, long licenseId, int percentMatch);
76  bool insertNoResultInDatabase(int agentId, long pFileId, long licenseId);
77 
78  bool saveHighlightInfo(long licenseFileId, unsigned start, unsigned length);
79  void insertOrCacheLicenseIdForName(std::string const& rfShortName, std::string const& rfFullname, std::string const& rfTexturl);
80  unsigned long getCachedLicenseIdForName(std::string const& rfShortName) const;
81  bool insertInDatabase(DatabaseEntry& entry) const;
82  std::vector<unsigned long> queryFileIdsForUpload(int uploadId, bool ignoreFilesWithMimeType);
83  bool createTables() const;
84 private:
85  unsigned long selectOrInsertLicenseIdForName(std::string rfShortname, std::string rfFullname, std::string rfTexturl);
86  std::unordered_map<std::string,long> licenseRefCache;
91  struct ColumnDef
92  {
93  const char* name;
94  const char* type;
95  const char* creationFlags;
96  };
97 
98  static const ColumnDef columns_author[];
99  static const ColumnDef columns_copyright[];
100  bool createTableAgentEvents(string tablename) const;
101 
102  static const ColumnDef columns_author_event[];
103  static const ColumnDef columns_copyright_event[];
104  bool createTableAgentFindings(string tablename) const;
105  std::string getColumnCreationString(const ColumnDef in[], size_t size) const;
106 };
107 
108 #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