FOSSology  4.5.1
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 cd.scope = 0 AND cd.group_fk = $5)"
32  " OR (EXISTS ("
33  " SELECT 1 FROM report_info ri WHERE ri.upload_fk = ut.upload_fk AND ri.ri_globaldecision = 1"
34  " ) AND ut.pfile_fk = cd.pfile_fk AND cd.scope = 1)"
35  ")"
36  " WHERE upload_fk=$1 AND (ufile_mode&x'3C000000'::int)=0 AND (lft BETWEEN $2 AND $3) AND ut.pfile_fk != 0"
37  " ORDER BY ut.uploadtree_pk, scopesort, ut.pfile_fk, clearing_decision_pk DESC"
38  ") itemView WHERE decision_type!=$4 OR decision_type IS NULL";
39  char* nonVoidPfile = "SELECT pfile_fk FROM allPfileData"
40  " WHERE pfile_fk NOT IN (SELECT lf.pfile_fk"
41  " FROM license_file lf"
42  " JOIN ars_master am ON lf.agent_fk = am.agent_fk"
43  " JOIN " LICENSE_REF_TABLE " lr ON lf.rf_fk = lr.rf_pk"
44  " WHERE am.upload_fk = $1"
45  " AND lr.rf_shortname = ANY(VALUES('No_license_found'), ('Void')))";
46 
47  if (!ignoreIrre && !scanFindings)
48  {
49  sql = g_strdup_printf(distinctPfile, tablename);
50  stmt = g_strdup_printf("queryFileIdsForUploadAndLimits.%s", tablename);
52  fo_dbManager_PrepareStamement(
53  dbManager,
54  stmt,
55  sql,
56  int, long, long),
57  uploadId, left, right
58  );
59  }
60  else if(!ignoreIrre && scanFindings)
61  {
62  sql = g_strdup_printf(
63  g_strconcat("WITH allPfileData AS (", distinctPfile, ") ", nonVoidPfile,
64  NULL), tablename);
65  stmt = g_strdup_printf("queryFileIdsForUploadAndLimitswithlicensefinding.%s", tablename);
67  fo_dbManager_PrepareStamement(
68  dbManager,
69  stmt,
70  sql,
71  int, long, long),
72  uploadId, left, right
73  );
74  }
75  else if(ignoreIrre && scanFindings)
76  {
77  sql = g_strdup_printf(
78  g_strconcat("WITH allPfileData AS (", distinctPfileNoDec, ") ",
79  nonVoidPfile, NULL), tablename);
80  stmt = g_strdup_printf("queryFileIdsForUploadAndLimits.%s.ignoreirreandscanFindings", tablename);
82  fo_dbManager_PrepareStamement(
83  dbManager,
84  stmt,
85  sql,
86  int, long, long, int, long),
87  uploadId, left, right, DECISION_TYPE_FOR_IRRELEVANT, groupId
88  );
89  }
90  else
91  {
92  sql = g_strdup_printf(distinctPfileNoDec, tablename);
93  stmt = g_strdup_printf("queryFileIdsForUploadAndLimits.%s.ignoreirre", tablename);
95  fo_dbManager_PrepareStamement(
96  dbManager,
97  stmt,
98  sql,
99  int, long, long, int, long),
100  uploadId, left, right, DECISION_TYPE_FOR_IRRELEVANT, groupId
101  );
102  }
103  g_free(sql);
104  g_free(stmt);
105  return result;
106 }
107 
108 PGresult* queryAllLicenses(fo_dbManager* dbManager) {
109  return fo_dbManager_Exec_printf(
110  dbManager,
111  "select rf_pk, rf_shortname from " LICENSE_REF_TABLE " where rf_detector_type = 1 and rf_active = 'true'"
112  );
113 }
114 
115 char* getLicenseTextForLicenseRefId(fo_dbManager* dbManager, long refId) {
116  PGresult* licenseTextResult = fo_dbManager_ExecPrepared(
117  fo_dbManager_PrepareStamement(
118  dbManager,
119  "getLicenseTextForLicenseRefId",
120  "select rf_text from " LICENSE_REF_TABLE " where rf_pk = $1",
121  long),
122  refId
123  );
124 
125  if (PQntuples(licenseTextResult) != 1) {
126  printf("cannot find license text!\n");
127  PQclear(licenseTextResult);
128  return g_strdup("");
129  }
130 
131  char* result = g_strdup(PQgetvalue(licenseTextResult, 0, 0));
132  PQclear(licenseTextResult);
133  return result;
134 }
135 
136 int hasAlreadyResultsFor(fo_dbManager* dbManager, int agentId, long pFileId) {
137  PGresult* insertResult = fo_dbManager_ExecPrepared(
138  fo_dbManager_PrepareStamement(
139  dbManager,
140  "hasAlreadyResultsFor",
141  "SELECT 1 WHERE EXISTS (SELECT 1"
142  " FROM license_file WHERE agent_fk = $1 AND pfile_fk = $2"
143  ")",
144  int, long),
145  agentId, pFileId
146  );
147 
148  int exists = 0;
149  if (insertResult) {
150  exists = (PQntuples(insertResult) == 1);
151  PQclear(insertResult);
152  }
153 
154  return exists;
155 }
156 
157 int saveNoResultToDb(fo_dbManager* dbManager, int agentId, long pFileId) {
158  PGresult* insertResult = fo_dbManager_ExecPrepared(
159  fo_dbManager_PrepareStamement(
160  dbManager,
161  "saveNoResultToDb",
162  "insert into license_file(agent_fk, pfile_fk) values($1,$2)",
163  int, long),
164  agentId, pFileId
165  );
166 
167  int result = 0;
168  if (insertResult) {
169  result = 1;
170  PQclear(insertResult);
171  }
172 
173  return result;
174 }
175 
176 long saveToDb(fo_dbManager* dbManager, int agentId, long refId, long pFileId, unsigned percent) {
177  PGresult* insertResult = fo_dbManager_ExecPrepared(
178  fo_dbManager_PrepareStamement(
179  dbManager,
180  "saveToDb",
181  "insert into license_file(rf_fk, agent_fk, pfile_fk, rf_match_pct) values($1,$2,$3,$4) RETURNING fl_pk",
182  long, int, long, unsigned),
183  refId, agentId, pFileId, percent
184  );
185 
186  long licenseFilePk = -1;
187  if (insertResult) {
188  if (PQntuples(insertResult) == 1) {
189  licenseFilePk = atol(PQgetvalue(insertResult, 0, 0));
190  }
191  PQclear(insertResult);
192  }
193 
194  return licenseFilePk;
195 }
196 
197 int saveDiffHighlightToDb(fo_dbManager* dbManager, const DiffMatchInfo* diffInfo, long licenseFileId) {
198  PGresult* insertResult = fo_dbManager_ExecPrepared(
199  fo_dbManager_PrepareStamement(
200  dbManager,
201  "saveDiffHighlightToDb",
202  "insert into highlight(fl_fk, type, start, len, rf_start, rf_len) values($1,$2,$3,$4,$5,$6)",
203  long, char*, size_t, size_t, size_t, size_t),
204  licenseFileId,
205  diffInfo->diffType,
206  diffInfo->text.start, diffInfo->text.length,
207  diffInfo->search.start, diffInfo->search.length
208  );
209 
210  if (!insertResult)
211  return 0;
212 
213  PQclear(insertResult);
214 
215  return 1;
216 }
217 
218 int saveDiffHighlightsToDb(fo_dbManager* dbManager, const GArray* matchedInfo, long licenseFileId) {
219  size_t matchedInfoLen = matchedInfo->len ;
220  for (size_t i = 0; i < matchedInfoLen; i++) {
221  DiffMatchInfo* diffMatchInfo = &g_array_index(matchedInfo, DiffMatchInfo, i);
222  if (!saveDiffHighlightToDb(dbManager, diffMatchInfo, licenseFileId))
223  return 0;
224  }
225 
226  return 1;
227 }
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