FOSSology  4.4.0
Open Source License Compliance by Open Source Software
database.c
1 /*
2  Author: Daniele Fognini, Andreas Wuerl
3  SPDX-FileCopyrightText: © 2013-2017, 2021 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 #define _GNU_SOURCE
9 #include <stdio.h>
10 
11 #include "database.h"
12 
13 #define LICENSE_REF_TABLE "ONLY license_ref"
14 
15 PGresult* queryFileIdsForUploadAndLimits(fo_dbManager* dbManager, int uploadId,
16  long left, long right, long groupId,
17  bool ignoreIrre, bool scanFindings) {
18  char* tablename = getUploadTreeTableName(dbManager, uploadId);
19  gchar* stmt;
20  gchar* sql;
21  PGresult* result;
22 
23  char* distinctPfile = "SELECT DISTINCT pfile_fk FROM %s"
24  " WHERE upload_fk = $1 AND (ufile_mode&x'3C000000'::int) = 0 "
25  " AND (lft BETWEEN $2 AND $3) AND pfile_fk != 0";
26  char* distinctPfileNoDec = "SELECT DISTINCT pfile_fk FROM ("
27  "SELECT distinct ON(ut.uploadtree_pk, ut.pfile_fk, scopesort) ut.pfile_fk pfile_fk, ut.uploadtree_pk, decision_type,"
28  " CASE cd.scope WHEN 1 THEN 1 ELSE 0 END AS scopesort"
29  " FROM %s AS ut "
30  " LEFT JOIN clearing_decision cd ON "
31  " ((ut.uploadtree_pk = cd.uploadtree_fk AND scope = 0 AND cd.group_fk = $5) "
32  " OR (ut.pfile_fk = cd.pfile_fk AND scope = 1)) "
33  " WHERE upload_fk=$1 AND (ufile_mode&x'3C000000'::int)=0 AND (lft BETWEEN $2 AND $3) AND ut.pfile_fk != 0"
34  " ORDER BY ut.uploadtree_pk, scopesort, ut.pfile_fk, clearing_decision_pk DESC"
35  ") itemView WHERE decision_type!=$4 OR decision_type IS NULL";
36  char* nonVoidPfile = "SELECT pfile_fk FROM allPfileData"
37  " WHERE pfile_fk NOT IN (SELECT pfile_fk FROM license_file WHERE rf_fk IN"
38  " (SELECT rf_pk FROM " LICENSE_REF_TABLE
39  " WHERE rf_shortname = ANY(VALUES('No_license_found'), ('Void'))))";
40 
41  if (!ignoreIrre && !scanFindings)
42  {
43  sql = g_strdup_printf(distinctPfile, tablename);
44  stmt = g_strdup_printf("queryFileIdsForUploadAndLimits.%s", tablename);
46  fo_dbManager_PrepareStamement(
47  dbManager,
48  stmt,
49  sql,
50  int, long, long),
51  uploadId, left, right
52  );
53  }
54  else if(!ignoreIrre && scanFindings)
55  {
56  sql = g_strdup_printf(
57  g_strconcat("WITH allPfileData AS (", distinctPfile, ") ", nonVoidPfile,
58  NULL), tablename);
59  stmt = g_strdup_printf("queryFileIdsForUploadAndLimitswithlicensefinding.%s", tablename);
61  fo_dbManager_PrepareStamement(
62  dbManager,
63  stmt,
64  sql,
65  int, long, long),
66  uploadId, left, right
67  );
68  }
69  else if(ignoreIrre && scanFindings)
70  {
71  sql = g_strdup_printf(
72  g_strconcat("WITH allPfileData AS (", distinctPfileNoDec, ") ",
73  nonVoidPfile, NULL), tablename);
74  stmt = g_strdup_printf("queryFileIdsForUploadAndLimits.%s.ignoreirreandscanFindings", tablename);
76  fo_dbManager_PrepareStamement(
77  dbManager,
78  stmt,
79  sql,
80  int, long, long, int, long),
81  uploadId, left, right, DECISION_TYPE_FOR_IRRELEVANT, groupId
82  );
83  }
84  else
85  {
86  sql = g_strdup_printf(distinctPfileNoDec, tablename);
87  stmt = g_strdup_printf("queryFileIdsForUploadAndLimits.%s.ignoreirre", tablename);
89  fo_dbManager_PrepareStamement(
90  dbManager,
91  stmt,
92  sql,
93  int, long, long, int, long),
94  uploadId, left, right, DECISION_TYPE_FOR_IRRELEVANT, groupId
95  );
96  }
97  g_free(sql);
98  g_free(stmt);
99  return result;
100 }
101 
102 PGresult* queryAllLicenses(fo_dbManager* dbManager) {
103  return fo_dbManager_Exec_printf(
104  dbManager,
105  "select rf_pk, rf_shortname from " LICENSE_REF_TABLE " where rf_detector_type = 1 and rf_active = 'true'"
106  );
107 }
108 
109 char* getLicenseTextForLicenseRefId(fo_dbManager* dbManager, long refId) {
110  PGresult* licenseTextResult = fo_dbManager_ExecPrepared(
111  fo_dbManager_PrepareStamement(
112  dbManager,
113  "getLicenseTextForLicenseRefId",
114  "select rf_text from " LICENSE_REF_TABLE " where rf_pk = $1",
115  long),
116  refId
117  );
118 
119  if (PQntuples(licenseTextResult) != 1) {
120  printf("cannot find license text!\n");
121  PQclear(licenseTextResult);
122  return g_strdup("");
123  }
124 
125  char* result = g_strdup(PQgetvalue(licenseTextResult, 0, 0));
126  PQclear(licenseTextResult);
127  return result;
128 }
129 
130 int hasAlreadyResultsFor(fo_dbManager* dbManager, int agentId, long pFileId) {
131  PGresult* insertResult = fo_dbManager_ExecPrepared(
132  fo_dbManager_PrepareStamement(
133  dbManager,
134  "hasAlreadyResultsFor",
135  "SELECT 1 WHERE EXISTS (SELECT 1"
136  " FROM license_file WHERE agent_fk = $1 AND pfile_fk = $2"
137  ")",
138  int, long),
139  agentId, pFileId
140  );
141 
142  int exists = 0;
143  if (insertResult) {
144  exists = (PQntuples(insertResult) == 1);
145  PQclear(insertResult);
146  }
147 
148  return exists;
149 }
150 
151 int saveNoResultToDb(fo_dbManager* dbManager, int agentId, long pFileId) {
152  PGresult* insertResult = fo_dbManager_ExecPrepared(
153  fo_dbManager_PrepareStamement(
154  dbManager,
155  "saveNoResultToDb",
156  "insert into license_file(agent_fk, pfile_fk) values($1,$2)",
157  int, long),
158  agentId, pFileId
159  );
160 
161  int result = 0;
162  if (insertResult) {
163  result = 1;
164  PQclear(insertResult);
165  }
166 
167  return result;
168 }
169 
170 long saveToDb(fo_dbManager* dbManager, int agentId, long refId, long pFileId, unsigned percent) {
171  PGresult* insertResult = fo_dbManager_ExecPrepared(
172  fo_dbManager_PrepareStamement(
173  dbManager,
174  "saveToDb",
175  "insert into license_file(rf_fk, agent_fk, pfile_fk, rf_match_pct) values($1,$2,$3,$4) RETURNING fl_pk",
176  long, int, long, unsigned),
177  refId, agentId, pFileId, percent
178  );
179 
180  long licenseFilePk = -1;
181  if (insertResult) {
182  if (PQntuples(insertResult) == 1) {
183  licenseFilePk = atol(PQgetvalue(insertResult, 0, 0));
184  }
185  PQclear(insertResult);
186  }
187 
188  return licenseFilePk;
189 }
190 
191 int saveDiffHighlightToDb(fo_dbManager* dbManager, const DiffMatchInfo* diffInfo, long licenseFileId) {
192  PGresult* insertResult = fo_dbManager_ExecPrepared(
193  fo_dbManager_PrepareStamement(
194  dbManager,
195  "saveDiffHighlightToDb",
196  "insert into highlight(fl_fk, type, start, len, rf_start, rf_len) values($1,$2,$3,$4,$5,$6)",
197  long, char*, size_t, size_t, size_t, size_t),
198  licenseFileId,
199  diffInfo->diffType,
200  diffInfo->text.start, diffInfo->text.length,
201  diffInfo->search.start, diffInfo->search.length
202  );
203 
204  if (!insertResult)
205  return 0;
206 
207  PQclear(insertResult);
208 
209  return 1;
210 }
211 
212 int saveDiffHighlightsToDb(fo_dbManager* dbManager, const GArray* matchedInfo, long licenseFileId) {
213  size_t matchedInfoLen = matchedInfo->len ;
214  for (size_t i = 0; i < matchedInfoLen; i++) {
215  DiffMatchInfo* diffMatchInfo = &g_array_index(matchedInfo, DiffMatchInfo, i);
216  if (!saveDiffHighlightToDb(dbManager, diffMatchInfo, licenseFileId))
217  return 0;
218  }
219 
220  return 1;
221 }
char * getUploadTreeTableName(fo_dbManager *dbManager, int uploadId)
Get the upload tree table name for a given upload.
Definition: libfossagent.c:25
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
PGresult * fo_dbManager_ExecPrepared(fo_dbManager_PreparedStatement *preparedStatement,...)
Execute a prepared statement.
Definition: standalone.c:36
int exists
Default not exists.
Definition: run_tests.c:20