FOSSology  4.4.0
Open Source License Compliance by Open Source Software
CompatibilityDatabaseHandler.cc
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2024 Siemens AG
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
12 
13 #include "libfossagentref.h"
14 
15 #include <iterator>
16 #include <sstream>
17 #include <utility>
18 
19 
20 using namespace fo;
21 using namespace std;
22 
29  fo::AgentDatabaseHandler(std::move(dbManager))
30 {
31 }
32 
39  int uploadId)
40 {
41  return queryFileIdsVectorForUpload(uploadId, false);
42 }
43 
52  int uploadId, int agentId)
53 {
54  string uploadtreeTableName = queryUploadTreeTableName(uploadId);
55 
56  QueryResult queryResult = dbManager.execPrepared(
57  fo_dbManager_PrepareStamement(
59  ("pfileForUploadFilterAgent" + uploadtreeTableName).c_str(),
60  ("SELECT distinct(ut.pfile_fk) FROM " + uploadtreeTableName +
61  " AS ut "
62  "INNER JOIN license_file AS lf ON ut.pfile_fk = lf.pfile_fk "
63  "LEFT JOIN comp_result AS cr ON ut.pfile_fk = cr.pfile_fk "
64  "AND cr.agent_fk = $2 WHERE cr.pfile_fk IS NULL "
65  "AND ut.upload_fk = $1 AND (ut.ufile_mode&x'3C000000'::int)=0;")
66  .c_str(), int, int),
67  uploadId, agentId);
68 
69  return queryResult.getSimpleResults(0, fo::stringToUnsignedLong);
70 }
71 
78  unsigned long pFileId,
79  vector<unsigned long> agentIds) // to get licId from pfile_id
80 {
81  std::stringstream ss;
82 
83  ss << "{";
84  for (size_t i = 0; i < agentIds.size() - 1; i++)
85  {
86  ss << agentIds[i] << ",";
87  }
88  ss << agentIds[agentIds.size() - 1] << "}";
89 
90  QueryResult queryResult = dbManager.execPrepared(
91  fo_dbManager_PrepareStamement(
92  dbManager.getStruct_dbManager(), "getLicenses",
93  ("SELECT DISTINCT rf_fk FROM license_file INNER JOIN license_ref "
94  "ON rf_fk = rf_pk AND rf_shortname NOT IN"
95  "('Dual-license', 'No_license_found', 'Void') WHERE pfile_fk = $1 "
96  "AND agent_fk = ANY($2::int[])"),
97  unsigned long, char*),
98  pFileId, ss.str().c_str());
99 
100  return queryResult.getSimpleResults(0, fo::stringToUnsignedLong);
101 }
102 
109 vector<tuple<unsigned long, string>> CompatibilityDatabaseHandler::
110  queryLicDetails(const vector<unsigned long>& licId)
111 {
112  vector<tuple<unsigned long, string>> vec;
113  for (auto i : licId)
114  {
115  QueryResult queryResult = dbManager.execPrepared(
116  fo_dbManager_PrepareStamement(
117  dbManager.getStruct_dbManager(), "agentCompGetLicDetails",
118  ("SELECT rf_licensetype FROM license_ref WHERE rf_pk = $1"),
119  unsigned long),
120  i);
121  vector<string> vec2 = queryResult.getRow(0);
122  vec.emplace_back(i, vec2[0]);
123  }
124  return vec;
125 }
126 
134  tuple<unsigned long, string> lic1, tuple<unsigned long, string> lic2) const
135 {
136  QueryResult queryResult = dbManager.execPrepared(
137  fo_dbManager_PrepareStamement(
138  dbManager.getStruct_dbManager(), "getCompatibilityOnLicenseId",
139  ("SELECT compatibility FROM license_rules WHERE (first_rf_fk = $1 AND "
140  "second_rf_fk = "
141  "$2) OR (second_rf_fk = $1 AND first_rf_fk = $2)"),
142  unsigned long, unsigned long),
143  get<0>(lic1), get<0>(lic2));
144  if (queryResult.getRowCount() == 0)
145  {
146  return UNKNOWN;
147  }
148  else
149  {
150  bool result = queryResult.getSimpleResults(0, fo::stringToBool)[0];
151  if (result)
152  {
153  return COMPATIBLE;
154  }
155  else
156  {
157  return NOTCOMPATIBLE;
158  }
159  }
160 }
161 
170  std::tuple<unsigned long, std::string> lic1,
171  std::tuple<unsigned long, std::string> lic2) const
172 {
173  QueryResult queryResult = dbManager.execPrepared(
174  fo_dbManager_PrepareStamement(dbManager.getStruct_dbManager(),
175  "getCompatibilityOnLicenseType",
176  ("SELECT compatibility FROM license_rules "
177  "WHERE (first_type = $1 AND second_type = $2) "
178  "OR (second_type = $1 AND first_type = $2)"),
179  char*, char*),
180  get<1>(lic1).c_str(), get<1>(lic2).c_str());
181  if (queryResult.getRowCount() == 0)
182  {
183  return UNKNOWN;
184  }
185  else
186  {
187  bool result = queryResult.getSimpleResults(0, fo::stringToBool)[0];
188  if (result)
189  {
190  return COMPATIBLE;
191  }
192  else
193  {
194  return NOTCOMPATIBLE;
195  }
196  }
197 }
198 
206  std::tuple<unsigned long, std::string> lic1,
207  std::tuple<unsigned long, std::string> lic2) const
208 {
209  unsigned long licenseId1, licenseId2;
210 
211  licenseId1 = get<0>(lic1);
212  licenseId2 = get<0>(lic2);
213  const char* licenseType1 = get<1>(lic1).c_str();
214  const char* licenseType2 = get<1>(lic2).c_str();
215 
216  QueryResult queryResult = dbManager.execPrepared(
217  fo_dbManager_PrepareStamement(
218  dbManager.getStruct_dbManager(), "getCompatibilityOnLicenseIdAndType",
219  ("SELECT compatibility FROM license_rules WHERE (first_rf_fk = $1 AND "
220  "second_type = $2) OR (first_rf_fk = $3 AND second_type = $4)"),
221  unsigned long, char*, unsigned long, char*),
222  licenseId1, licenseType2, licenseId2, licenseType1);
223 
224  if (queryResult.getRowCount() == 0)
225  {
226  return UNKNOWN;
227  }
228  else
229  {
230  bool result = queryResult.getSimpleResults(0, fo::stringToBool)[0];
231  if (result)
232  {
233  return COMPATIBLE;
234  }
235  else
236  {
237  return NOTCOMPATIBLE;
238  }
239  }
240 }
241 
249 bool CompatibilityDatabaseHandler::check(unsigned long id1, unsigned long id2,
250  unsigned long pFileId)
251 {
252  QueryResult queryResult = dbManager.execPrepared(
253  fo_dbManager_PrepareStamement(
254  dbManager.getStruct_dbManager(), "comp_resultExists",
255  ("SELECT exists(SELECT 1 FROM comp_result WHERE ((first_rf_fk= $1 "
256  "AND "
257  "second_rf_fk= $2) OR (second_rf_fk= $1 AND first_rf_fk= $2)) AND "
258  "pfile_fk = $3)"),
259  unsigned long, unsigned long, unsigned long),
260  id1, id2, pFileId);
261  return queryResult.getSimpleResults(0, fo::stringToBool)[0];
262 }
263 
274  int a_id,
275  unsigned long id1,
276  unsigned long id2,
277  const string& comp)
278 {
279  QueryResult queryResult = dbManager.execPrepared(
280  fo_dbManager_PrepareStamement(
281  dbManager.getStruct_dbManager(), "CompInsertResult",
282  "INSERT INTO comp_result"
283  "(pfile_fk, agent_fk, first_rf_fk, second_rf_fk, result)"
284  "VALUES($1, $2, $3, $4, $5)",
285  unsigned long, int, unsigned long, unsigned long, char*),
286  pFileId, a_id, id1, id2, comp.c_str());
287  return !queryResult.isFailed();
288 }
296 {
297  DbManager spawnedDbMan(dbManager.spawn());
298  return CompatibilityDatabaseHandler(spawnedDbMan);
299 }
300 
306 {
307  QueryResult queryResult = dbManager.execPrepared(
308  fo_dbManager_PrepareStamement(
309  dbManager.getStruct_dbManager(), "getDefaultRule",
310  "SELECT compatibility FROM license_rules "
311  "WHERE first_rf_fk IS NULL AND second_rf_fk IS NULL AND "
312  "first_type IS NULL AND second_type IS NULL;"));
313  if (queryResult.getRowCount() == 0)
314  {
315  return COMPATIBLE;
316  }
317  bool result = queryResult.getSimpleResults(0, fo::stringToBool)[0];
318  if (result)
319  {
320  return COMPATIBLE;
321  }
322  return NOTCOMPATIBLE;
323 }
324 
330 std::vector<unsigned long> CompatibilityDatabaseHandler::
331  queryScannerIdsForUpload(int uploadId)
332 {
333  QueryResult agentIdResult = dbManager.execPrepared(
334  fo_dbManager_PrepareStamement(
335  dbManager.getStruct_dbManager(), "latestScanner",
336  ("SELECT DISTINCT ON (agent_name) agent_pk FROM agent AS ag "
337  "INNER JOIN ars_master AS am "
338  " ON ag.agent_pk = am.agent_fk AND am.upload_fk = $1"
339  " AND am.ars_success = true "
340  "WHERE agent_name IN (" FOSSOLOGY_SCANNER_AGENT_NAME_ARRAY ") "
341  "ORDER BY agent_name, agent_ts DESC;"),
342  unsigned long),
343  uploadId);
344  return agentIdResult.getSimpleResults(0, fo::stringToUnsignedLong);
345 }
346 
352 std::vector<unsigned long> CompatibilityDatabaseHandler::
353  queryMainLicenseForUpload(int uploadId, int groupId)
354 {
355  QueryResult agentIdResult = dbManager.execPrepared(
356  fo_dbManager_PrepareStamement(
357  dbManager.getStruct_dbManager(), "mainLicense",
358  ("SELECT DISTINCT rf_fk FROM "
359  "upload_clearing_license "
360  "WHERE upload_fk = $1 AND group_fk = $2;"),
361  unsigned long, unsigned long),
362  uploadId, groupId);
363  return agentIdResult.getSimpleResults(0, fo::stringToUnsignedLong);
364 }
Database handler for COMPATIBILITY.
std::vector< unsigned long > queryScannerIdsForUpload(int uploadId)
Get the agent id of latest scanners run on the upload.
std::vector< unsigned long > queryMainLicenseForUpload(int uploadId, int groupId)
Get the main licenses for the upload.
bool queryInsertResult(unsigned long pFileId, int a_id, unsigned long id1, unsigned long id2, const string &comp)
insert the compatibility result in the comp_result table
bool check(unsigned long id1, unsigned long id2, unsigned long pFileId)
Checking whether the data is already present in database or not, to prevent redundancy.
std::vector< unsigned long > queryLicIdsFromPfile(unsigned long pFileId, vector< unsigned long > agentIds)
to get the license id from the file id
CompatibilityDatabaseHandler(fo::DbManager dbManager)
CompatibilityStatus queryRule3(std::tuple< unsigned long, std::string > lic1, std::tuple< unsigned long, std::string > lic2) const
This rule uses license id and license type to find the compatibility.
std::vector< std::tuple< unsigned long, std::string > > queryLicDetails(const std::vector< unsigned long > &licId)
store the id and type of that respective license in vector of tuple for all the licenses
CompatibilityStatus queryRule2(std::tuple< unsigned long, std::string > lic1, std::tuple< unsigned long, std::string > lic2) const
This rule uses license type of both the licenses to find the compatibility.
std::vector< unsigned long > queryFileIdsForScan(int uploadId, int agentId)
CompatibilityStatus queryRule1(tuple< unsigned long, string > lic1, tuple< unsigned long, string > lic2) const
This rule uses id of both the licenses to find the compatibility.
std::vector< unsigned long > queryFileIdsForUpload(int uploadId)
CompatibilityDatabaseHandler spawn() const
CompatibilityStatus getDefaultRule() const
Get the default rule from DB as CompatibilityStatus.
Database handler for agents.
std::vector< unsigned long > queryFileIdsVectorForUpload(int uploadId, bool ignoreFilesWithMimeType) const
Get pfile ids for a given upload id.
std::string queryUploadTreeTableName(int uploadId)
Get the upload tree table name for a given upload id.
DbManager dbManager
DbManager to use.
DB wrapper for agents.
QueryResult execPrepared(fo_dbManager_PreparedStatement *stmt,...) const
Execute a prepared statement with new parameters.
fo_dbManager * getStruct_dbManager() const
DbManager spawn() const
Wrapper for DB result.
std::vector< T > getSimpleResults(int columnN, T(functionP)(const char *)) const
Get vector of a single column from query result.
bool isFailed() const
Check if the query failed.
std::vector< std::string > getRow(int i) const
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
fo namespace holds the FOSSology library functions.
bool stringToBool(const char *string)
Definition: libfossUtils.cc:32
unsigned long stringToUnsignedLong(const char *string)
Definition: libfossUtils.cc:20