14 #define LICENSE_REF_TABLE "ONLY license_ref"
17 long left,
long right,
long groupId,
18 bool ignoreIrre,
bool scanFindings) {
24 char* distinctPfile =
"SELECT DISTINCT pfile_fk FROM %s"
25 " WHERE upload_fk = $1 AND (ufile_mode&x'3C000000'::int) = 0 "
26 " AND (lft BETWEEN $2 AND $3) AND pfile_fk != 0";
27 char* distinctPfileNoDec =
"SELECT DISTINCT pfile_fk FROM ("
28 "SELECT distinct ON(ut.uploadtree_pk, ut.pfile_fk, scopesort) ut.pfile_fk pfile_fk, ut.uploadtree_pk, decision_type,"
29 " CASE cd.scope WHEN 1 THEN 1 ELSE 0 END AS scopesort"
31 " LEFT JOIN clearing_decision cd ON ("
32 " (ut.uploadtree_pk = cd.uploadtree_fk AND cd.scope = 0 AND cd.group_fk = $5)"
34 " SELECT 1 FROM report_info ri WHERE ri.upload_fk=$1 AND ri.ri_globaldecision=1"
35 " ) AND ut.pfile_fk = cd.pfile_fk AND cd.scope = 1)"
37 " WHERE upload_fk=$1 AND (ufile_mode&x'3C000000'::int)=0 AND (lft BETWEEN $2 AND $3) AND ut.pfile_fk != 0"
38 " ORDER BY ut.uploadtree_pk, scopesort, ut.pfile_fk, clearing_decision_pk DESC"
39 ") itemView WHERE decision_type!=$4 OR decision_type IS NULL";
40 char* nonVoidPfile =
"SELECT a.pfile_fk FROM allPfileData a"
41 " WHERE EXISTS (SELECT 1 FROM license_file lf"
42 " JOIN license_ref lr ON lf.rf_fk = lr.rf_pk"
43 " WHERE lf.pfile_fk = a.pfile_fk"
44 " AND lr.rf_shortname NOT IN ('No_license_found', 'Void'))"
45 " OR NOT EXISTS (SELECT 1 FROM license_file lf"
46 " JOIN license_ref lr ON lf.rf_fk = lr.rf_pk"
47 " WHERE lf.pfile_fk = a.pfile_fk)";
49 if (!ignoreIrre && !scanFindings)
51 sql = g_strdup_printf(distinctPfile, tablename);
52 stmt = g_strdup_printf(
"queryFileIdsForUploadAndLimits.%s", tablename);
54 fo_dbManager_PrepareStamement(
62 else if(!ignoreIrre && scanFindings)
64 sql = g_strdup_printf(
65 g_strconcat(
"WITH allPfileData AS (", distinctPfile,
") ", nonVoidPfile,
67 stmt = g_strdup_printf(
"queryFileIdsForUploadAndLimitswithlicensefinding.%s", tablename);
69 fo_dbManager_PrepareStamement(
77 else if(ignoreIrre && scanFindings)
79 sql = g_strdup_printf(
80 g_strconcat(
"WITH allPfileData AS (", distinctPfileNoDec,
") ",
81 nonVoidPfile, NULL), tablename);
82 stmt = g_strdup_printf(
"queryFileIdsForUploadAndLimits.%s.ignoreirreandscanFindings", tablename);
84 fo_dbManager_PrepareStamement(
88 int,
long,
long,
int,
long),
89 uploadId, left, right, DECISION_TYPE_FOR_IRRELEVANT, groupId
94 sql = g_strdup_printf(distinctPfileNoDec, tablename);
95 stmt = g_strdup_printf(
"queryFileIdsForUploadAndLimits.%s.ignoreirre", tablename);
97 fo_dbManager_PrepareStamement(
101 int,
long,
long,
int,
long),
102 uploadId, left, right, DECISION_TYPE_FOR_IRRELEVANT, groupId
111 return fo_dbManager_Exec_printf(
113 "select rf_pk, rf_shortname from " LICENSE_REF_TABLE
" where rf_detector_type = 1 and rf_active = 'true'"
119 fo_dbManager_PrepareStamement(
121 "getLicenseTextForLicenseRefId",
122 "select rf_text from " LICENSE_REF_TABLE
" where rf_pk = $1",
127 if (PQntuples(licenseTextResult) != 1) {
128 printf(
"cannot find license text!\n");
129 PQclear(licenseTextResult);
133 char* result = g_strdup(PQgetvalue(licenseTextResult, 0, 0));
134 PQclear(licenseTextResult);
140 fo_dbManager_PrepareStamement(
142 "hasAlreadyResultsFor",
143 "SELECT 1 WHERE EXISTS (SELECT 1"
144 " FROM license_file WHERE agent_fk = $1 AND pfile_fk = $2"
152 exists = (PQntuples(insertResult) == 1);
153 PQclear(insertResult);
161 fo_dbManager_PrepareStamement(
164 "insert into license_file(agent_fk, pfile_fk) values($1,$2)",
172 PQclear(insertResult);
180 fo_dbManager_PrepareStamement(
183 "insert into license_file(rf_fk, agent_fk, pfile_fk, rf_match_pct) values($1,$2,$3,$4) RETURNING fl_pk",
184 long,
int,
long,
unsigned),
185 refId, agentId, pFileId, percent
188 long licenseFilePk = -1;
190 if (PQntuples(insertResult) == 1) {
191 licenseFilePk = atol(PQgetvalue(insertResult, 0, 0));
193 PQclear(insertResult);
196 return licenseFilePk;
201 fo_dbManager_PrepareStamement(
203 "saveDiffHighlightToDb",
204 "insert into highlight(fl_fk, type, start, len, rf_start, rf_len) values($1,$2,$3,$4,$5,$6)",
205 long,
char*,
size_t,
size_t,
size_t,
size_t),
208 diffInfo->text.start, diffInfo->text.length,
209 diffInfo->search.start, diffInfo->search.length
215 PQclear(insertResult);
221 size_t matchedInfoLen = matchedInfo->len ;
222 for (
size_t i = 0; i < matchedInfoLen; i++) {
224 if (!saveDiffHighlightToDb(
dbManager, diffMatchInfo, licenseFileId))
239 fo_dbManager_PrepareStamement(
241 "queryActiveCustomPhrasesWithMappings",
242 "SELECT cp.cp_pk, cp.text, cp.acknowledgement, cp.comments,"
243 " cplm.rf_fk, cplm.removing,"
244 " cplm.comment, cplm.reportinfo, cplm.acknowledgement"
245 " FROM custom_phrase cp"
246 " LEFT JOIN custom_phrase_license_map cplm ON cp.cp_pk = cplm.cp_fk"
247 " WHERE cp.is_active = true"
252 GArray* phrases = g_array_new(FALSE, FALSE,
sizeof(
Phrase*));
257 int numRows = PQntuples(result);
260 for (
int i = 0; i < numRows; i++) {
261 long cpId = atol(PQgetvalue(result, i, 0));
263 if (!current || current->cpId != cpId) {
266 phrase->text = g_strdup(PQgetvalue(result, i, 1));
267 phrase->acknowledgement = PQgetisnull(result, i, 2) ? NULL : g_strdup(PQgetvalue(result, i, 2));
268 phrase->comments = PQgetisnull(result, i, 3) ? NULL : g_strdup(PQgetvalue(result, i, 3));
269 phrase->licenseMappings = g_array_new(FALSE, FALSE,
sizeof(
LicenseMapping));
270 phrase->stmtName = NULL;
271 g_array_append_val(phrases, phrase);
276 if (!PQgetisnull(result, i, 4)) {
278 mapping.
rfPk = atol(PQgetvalue(result, i, 4));
279 mapping.
removing = (strcmp(PQgetvalue(result, i, 5),
"t") == 0) ? 1 : 0;
280 mapping.
comment = PQgetisnull(result, i, 6) ? NULL : g_strdup(PQgetvalue(result, i, 6));
281 mapping.
reportinfo = PQgetisnull(result, i, 7) ? NULL : g_strdup(PQgetvalue(result, i, 7));
282 mapping.
acknowledgement = PQgetisnull(result, i, 8) ? NULL : g_strdup(PQgetvalue(result, i, 8));
283 g_array_append_val(current->licenseMappings, mapping);
295 void phrase_free(
Phrase* phrase) {
298 g_free(phrase->text);
299 g_free(phrase->acknowledgement);
300 g_free(phrase->comments);
301 g_free(phrase->stmtName);
303 if (phrase->licenseMappings) {
304 for (guint i = 0; i < phrase->licenseMappings->len; i++) {
310 g_array_free(phrase->licenseMappings, TRUE);
320 void phrases_free(GArray* phrases) {
321 if (!phrases)
return;
323 for (guint i = 0; i < phrases->len; i++) {
328 g_array_free(phrases, TRUE);
char * getUploadTreeTableName(fo_dbManager *dbManager, int uploadId)
Get the upload tree table name for a given upload.
fo_dbManager * dbManager
fo_dbManager object
PGresult * fo_dbManager_ExecPrepared(fo_dbManager_PreparedStatement *preparedStatement,...)
Execute a prepared statement.
License mapping entry with per-mapping report metadata.
int removing
0 = add license, 1 = remove license
char * acknowledgement
nullable
long rfPk
rf_pk from license_ref table
char * reportinfo
nullable
Structure to hold a custom phrase and its mapped licenses.
int exists
Default not exists.