FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
database.c
1 /*
2 Author: Daniele Fognini, Andreas Wuerl
3 Copyright (C) 2013-2017, 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 #define DECISION_TYPE_FOR_IRRELEVANT 4
26 
27 PGresult* queryFileIdsForUploadAndLimits(fo_dbManager* dbManager, int uploadId, long left, long right, long groupId) {
29  fo_dbManager_PrepareStamement(
30  dbManager,
31  "queryFileIdsForUploadAndLimits"
32  ,
33  "SELECT distinct (pfile_fk) FROM ("
34  "SELECT distinct ON(ut.uploadtree_pk, ut.pfile_fk, scopesort) ut.pfile_fk pfile_fk, ut.uploadtree_pk, decision_type,"
35  " CASE cd.scope WHEN 1 THEN 1 ELSE 0 END AS scopesort"
36  " FROM uploadtree ut "
37  " LEFT JOIN clearing_decision cd ON cd.group_fk=$5 AND (ut.uploadtree_pk=cd.uploadtree_fk AND scope=0 OR ut.pfile_fk=cd.pfile_fk AND scope=1) "
38  " WHERE upload_fk=$1 and (ufile_mode&x'3C000000'::int)=0 AND (lft between $2 and $3) AND ut.pfile_fk != 0"
39  " ORDER BY ut.uploadtree_pk, scopesort, ut.pfile_fk, clearing_decision_pk DESC"
40  ") itemView WHERE decision_type!=$4 OR decision_type IS NULL"
41  ,
42  int, long, long, int, long),
43  uploadId, left, right, DECISION_TYPE_FOR_IRRELEVANT, groupId
44  );
45 }
46 
47 PGresult* queryAllLicenses(fo_dbManager* dbManager) {
48  return fo_dbManager_Exec_printf(
49  dbManager,
50  "select rf_pk, rf_shortname from " LICENSE_REF_TABLE " where rf_detector_type = 1 and rf_active = 'true'"
51  );
52 }
53 
54 char* getLicenseTextForLicenseRefId(fo_dbManager* dbManager, long refId) {
55  PGresult* licenseTextResult = fo_dbManager_ExecPrepared(
56  fo_dbManager_PrepareStamement(
57  dbManager,
58  "getLicenseTextForLicenseRefId",
59  "select rf_text from " LICENSE_REF_TABLE " where rf_pk = $1",
60  long),
61  refId
62  );
63 
64  if (PQntuples(licenseTextResult) != 1) {
65  printf("cannot find license text!\n");
66  PQclear(licenseTextResult);
67  return g_strdup("");
68  }
69 
70  char* result = g_strdup(PQgetvalue(licenseTextResult, 0, 0));
71  PQclear(licenseTextResult);
72  return result;
73 }
74 
75 int hasAlreadyResultsFor(fo_dbManager* dbManager, int agentId, long pFileId) {
76  PGresult* insertResult = fo_dbManager_ExecPrepared(
77  fo_dbManager_PrepareStamement(
78  dbManager,
79  "hasAlreadyResultsFor",
80  "SELECT 1 WHERE EXISTS (SELECT 1"
81  " FROM license_file WHERE agent_fk = $1 AND pfile_fk = $2"
82  ")",
83  int, long),
84  agentId, pFileId
85  );
86 
87  int exists = 0;
88  if (insertResult) {
89  exists = (PQntuples(insertResult) == 1);
90  PQclear(insertResult);
91  }
92 
93  return exists;
94 }
95 
96 int saveNoResultToDb(fo_dbManager* dbManager, int agentId, long pFileId) {
97  PGresult* insertResult = fo_dbManager_ExecPrepared(
98  fo_dbManager_PrepareStamement(
99  dbManager,
100  "saveNoResultToDb",
101  "insert into license_file(agent_fk, pfile_fk) values($1,$2)",
102  int, long),
103  agentId, pFileId
104  );
105 
106  int result = 0;
107  if (insertResult) {
108  result = 1;
109  PQclear(insertResult);
110  }
111 
112  return result;
113 }
114 
115 long saveToDb(fo_dbManager* dbManager, int agentId, long refId, long pFileId, unsigned percent) {
116  PGresult* insertResult = fo_dbManager_ExecPrepared(
117  fo_dbManager_PrepareStamement(
118  dbManager,
119  "saveToDb",
120  "insert into license_file(rf_fk, agent_fk, pfile_fk, rf_match_pct) values($1,$2,$3,$4) RETURNING fl_pk",
121  long, int, long, unsigned),
122  refId, agentId, pFileId, percent
123  );
124 
125  long licenseFilePk = -1;
126  if (insertResult) {
127  if (PQntuples(insertResult) == 1) {
128  licenseFilePk = atol(PQgetvalue(insertResult, 0, 0));
129  }
130  PQclear(insertResult);
131  }
132 
133  return licenseFilePk;
134 }
135 
136 int saveDiffHighlightToDb(fo_dbManager* dbManager, const DiffMatchInfo* diffInfo, long licenseFileId) {
137  PGresult* insertResult = fo_dbManager_ExecPrepared(
138  fo_dbManager_PrepareStamement(
139  dbManager,
140  "saveDiffHighlightToDb",
141  "insert into highlight(fl_fk, type, start, len, rf_start, rf_len) values($1,$2,$3,$4,$5,$6)",
142  long, char*, size_t, size_t, size_t, size_t),
143  licenseFileId,
144  diffInfo->diffType,
145  diffInfo->text.start, diffInfo->text.length,
146  diffInfo->search.start, diffInfo->search.length
147  );
148 
149  if (!insertResult)
150  return 0;
151 
152  PQclear(insertResult);
153 
154  return 1;
155 }
156 
157 int saveDiffHighlightsToDb(fo_dbManager* dbManager, const GArray* matchedInfo, long licenseFileId) {
158  size_t matchedInfoLen = matchedInfo->len ;
159  for (size_t i = 0; i < matchedInfoLen; i++) {
160  DiffMatchInfo* diffMatchInfo = &g_array_index(matchedInfo, DiffMatchInfo, i);
161  if (!saveDiffHighlightToDb(dbManager, diffMatchInfo, licenseFileId))
162  return 0;
163  }
164 
165  return 1;
166 }
PGresult * fo_dbManager_ExecPrepared(fo_dbManager_PreparedStatement *preparedStatement,...)
Execute a prepared statement.
Definition: standalone.c:31
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
int exists
Default not exists.
Definition: run_tests.c:31