1 /*
2 Author: Daniele Fognini, Andreas Wuerl
3 Copyright (C) 2013-2017, 2021, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 */
18 
19 #define _GNU_SOURCE
20 #include <stdio.h>
21 
22 #include "database.h"
23 
24 #define LICENSE_REF_TABLE "ONLY license_ref"
25 
queryFileIdsForUploadAndLimits(fo_dbManager * dbManager,int uploadId,long left,long right,long groupId,bool ignoreIrre)26 PGresult* queryFileIdsForUploadAndLimits(fo_dbManager* dbManager, int uploadId,
27                                          long left, long right, long groupId,
28                                          bool ignoreIrre) {
29   char* tablename = getUploadTreeTableName(dbManager, uploadId);
30   gchar* stmt;
31   gchar* sql;
32   PGresult* result;
33   if (!ignoreIrre)
34   {
35     sql = g_strdup_printf("SELECT DISTINCT pfile_fk FROM %s"
36       " WHERE upload_fk = $1 AND (ufile_mode&x'3C000000'::int) = 0 "
37       " AND (lft BETWEEN $2 AND $3) AND pfile_fk != 0;", tablename);
38     stmt = g_strdup_printf("queryFileIdsForUploadAndLimits.%s", tablename);
39     result = fo_dbManager_ExecPrepared(
40       fo_dbManager_PrepareStamement(
41         dbManager,
42         stmt,
43         sql,
44         int, long, long),
45       uploadId, left, right
46     );
47   }
48   else
49   {
50     sql = g_strdup_printf("SELECT distinct (pfile_fk) FROM ("
51       "SELECT distinct ON(ut.uploadtree_pk, ut.pfile_fk, scopesort) ut.pfile_fk pfile_fk, ut.uploadtree_pk, decision_type,"
52         " CASE cd.scope WHEN 1 THEN 1 ELSE 0 END AS scopesort"
53       " FROM %s AS ut "
54       " LEFT JOIN clearing_decision cd ON "
55       "  ((ut.uploadtree_pk = cd.uploadtree_fk AND scope = 0 AND cd.group_fk = $5) "
56       "  OR (ut.pfile_fk = cd.pfile_fk AND scope = 1)) "
57       " WHERE upload_fk=$1 AND (ufile_mode&x'3C000000'::int)=0 AND (lft BETWEEN $2 AND $3) AND ut.pfile_fk != 0"
58       " ORDER BY ut.uploadtree_pk, scopesort, ut.pfile_fk, clearing_decision_pk DESC"
59     ") itemView WHERE decision_type!=$4 OR decision_type IS NULL;", tablename);
60     stmt = g_strdup_printf("queryFileIdsForUploadAndLimits.%s.ignoreirre", tablename);
61     result = fo_dbManager_ExecPrepared(
62       fo_dbManager_PrepareStamement(
63         dbManager,
64         stmt,
65         sql,
66         int, long, long, int, long),
67       uploadId, left, right, DECISION_TYPE_FOR_IRRELEVANT, groupId
68     );
69   }
70   g_free(sql);
71   g_free(stmt);
72   return result;
73 }
74 
queryAllLicenses(fo_dbManager * dbManager)75 PGresult* queryAllLicenses(fo_dbManager* dbManager) {
76   return fo_dbManager_Exec_printf(
77     dbManager,
78     "select rf_pk, rf_shortname from " LICENSE_REF_TABLE " where rf_detector_type = 1 and rf_active = 'true'"
79   );
80 }
81 
getLicenseTextForLicenseRefId(fo_dbManager * dbManager,long refId)82 char* getLicenseTextForLicenseRefId(fo_dbManager* dbManager, long refId) {
83   PGresult* licenseTextResult = fo_dbManager_ExecPrepared(
84     fo_dbManager_PrepareStamement(
85       dbManager,
86       "getLicenseTextForLicenseRefId",
87       "select rf_text from " LICENSE_REF_TABLE " where rf_pk = $1",
88       long),
89     refId
90   );
91 
92   if (PQntuples(licenseTextResult) != 1) {
93     printf("cannot find license text!\n");
94     PQclear(licenseTextResult);
95     return g_strdup("");
96   }
97 
98   char* result = g_strdup(PQgetvalue(licenseTextResult, 0, 0));
99   PQclear(licenseTextResult);
100   return result;
101 }
102 
hasAlreadyResultsFor(fo_dbManager * dbManager,int agentId,long pFileId)103 int hasAlreadyResultsFor(fo_dbManager* dbManager, int agentId, long pFileId) {
104   PGresult* insertResult = fo_dbManager_ExecPrepared(
105     fo_dbManager_PrepareStamement(
106       dbManager,
107       "hasAlreadyResultsFor",
108       "SELECT 1 WHERE EXISTS (SELECT 1"
109       " FROM license_file WHERE agent_fk = $1 AND pfile_fk = $2"
110       ")",
111       int, long),
112     agentId, pFileId
113   );
114 
115   int exists = 0;
116   if (insertResult) {
117     exists = (PQntuples(insertResult) == 1);
118     PQclear(insertResult);
119   }
120 
121   return exists;
122 }
123 
saveNoResultToDb(fo_dbManager * dbManager,int agentId,long pFileId)124 int saveNoResultToDb(fo_dbManager* dbManager, int agentId, long pFileId) {
125   PGresult* insertResult = fo_dbManager_ExecPrepared(
126     fo_dbManager_PrepareStamement(
127       dbManager,
128       "saveNoResultToDb",
129       "insert into license_file(agent_fk, pfile_fk) values($1,$2)",
130       int, long),
131     agentId, pFileId
132   );
133 
134   int result = 0;
135   if (insertResult) {
136     result = 1;
137     PQclear(insertResult);
138   }
139 
140   return result;
141 }
142 
saveToDb(fo_dbManager * dbManager,int agentId,long refId,long pFileId,unsigned percent)143 long saveToDb(fo_dbManager* dbManager, int agentId, long refId, long pFileId, unsigned percent) {
144   PGresult* insertResult = fo_dbManager_ExecPrepared(
145     fo_dbManager_PrepareStamement(
146       dbManager,
147       "saveToDb",
148       "insert into license_file(rf_fk, agent_fk, pfile_fk, rf_match_pct) values($1,$2,$3,$4) RETURNING fl_pk",
149       long, int, long, unsigned),
150     refId, agentId, pFileId, percent
151   );
152 
153   long licenseFilePk = -1;
154   if (insertResult) {
155     if (PQntuples(insertResult) == 1) {
156       licenseFilePk = atol(PQgetvalue(insertResult, 0, 0));
157     }
158     PQclear(insertResult);
159   }
160 
161   return licenseFilePk;
162 }
163 
saveDiffHighlightToDb(fo_dbManager * dbManager,const DiffMatchInfo * diffInfo,long licenseFileId)164 int saveDiffHighlightToDb(fo_dbManager* dbManager, const DiffMatchInfo* diffInfo, long licenseFileId) {
165   PGresult* insertResult = fo_dbManager_ExecPrepared(
166     fo_dbManager_PrepareStamement(
167       dbManager,
168       "saveDiffHighlightToDb",
169       "insert into highlight(fl_fk, type, start, len, rf_start, rf_len) values($1,$2,$3,$4,$5,$6)",
170       long, char*, size_t, size_t, size_t, size_t),
171     licenseFileId,
172     diffInfo->diffType,
173     diffInfo->text.start, diffInfo->text.length,
174     diffInfo->search.start, diffInfo->search.length
175   );
176 
177   if (!insertResult)
178     return 0;
179 
180   PQclear(insertResult);
181 
182   return 1;
183 }
184 
saveDiffHighlightsToDb(fo_dbManager * dbManager,const GArray * matchedInfo,long licenseFileId)185 int saveDiffHighlightsToDb(fo_dbManager* dbManager, const GArray* matchedInfo, long licenseFileId) {
186   size_t matchedInfoLen = matchedInfo->len ;
187   for (size_t i = 0; i < matchedInfoLen; i++) {
188     DiffMatchInfo* diffMatchInfo = &g_array_index(matchedInfo, DiffMatchInfo, i);
189     if (!saveDiffHighlightToDb(dbManager, diffMatchInfo, licenseFileId))
190       return 0;
191   }
192 
193   return 1;
194 }
195