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