FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
databasehandler.cc
1 /*
2  * Copyright (C) 2014-2015, Siemens AG
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  */
17 
18 #include "databasehandler.hpp"
19 #include "libfossUtils.hpp"
20 
21 #include <iostream>
22 
23 using namespace fo;
24 using namespace std;
25 
26 NinkaDatabaseHandler::NinkaDatabaseHandler(DbManager dbManager) :
27  fo::AgentDatabaseHandler(dbManager)
28 {
29 }
30 
31 vector<unsigned long> NinkaDatabaseHandler::queryFileIdsForUpload(int uploadId)
32 {
33  return queryFileIdsVectorForUpload(uploadId);
34 }
35 
36 // TODO: see function saveToDb() from src/monk/agent/database.c
37 bool NinkaDatabaseHandler::saveLicenseMatch(int agentId, long pFileId, long licenseId, unsigned percentMatch)
38 {
39  return dbManager.execPrepared(
40  fo_dbManager_PrepareStamement(
41  dbManager.getStruct_dbManager(),
42  "saveLicenseMatch",
43  "INSERT INTO license_file (agent_fk, pfile_fk, rf_fk, rf_match_pct) VALUES ($1, $2, $3, $4)",
44  int, long, long, unsigned
45  ),
46  agentId,
47  pFileId,
48  licenseId,
49  percentMatch
50  );
51 }
52 
53 unsigned long NinkaDatabaseHandler::selectOrInsertLicenseIdForName(string rfShortName)
54 {
55  bool success = false;
56  unsigned long result = 0;
57 
58  unsigned count = 0;
59  while ((!success) && count++<3)
60  {
61  if (!dbManager.begin())
62  continue;
63 
64  dbManager.queryPrintf("LOCK TABLE license_ref");
65 
66  QueryResult queryResult = dbManager.execPrepared(
67  fo_dbManager_PrepareStamement(
68  dbManager.getStruct_dbManager(),
69  "selectOrInsertLicenseIdForName",
70  "WITH "
71  "selectExisting AS ("
72  "SELECT rf_pk FROM ONLY license_ref"
73  " WHERE rf_shortname = $1"
74  "),"
75  "insertNew AS ("
76  "INSERT INTO license_ref(rf_shortname, rf_text, rf_detector_type)"
77  " SELECT $1, $2, $3"
78  " WHERE NOT EXISTS(SELECT * FROM selectExisting)"
79  " RETURNING rf_pk"
80  ") "
81 
82  "SELECT rf_pk FROM insertNew "
83  "UNION "
84  "SELECT rf_pk FROM selectExisting",
85  char*, char*, int
86  ),
87  rfShortName.c_str(),
88  "License by Ninka.",
89  3
90  );
91 
92  success = queryResult && queryResult.getRowCount() > 0;
93 
94  if (success) {
95  success &= dbManager.commit();
96 
97  if (success) {
98  result = queryResult.getSimpleResults(0, fo::stringToUnsignedLong)[0];
99  }
100  } else {
101  dbManager.rollback();
102  }
103  }
104 
105  return result;
106 }
107 
108 NinkaDatabaseHandler NinkaDatabaseHandler::spawn() const
109 {
110  DbManager spawnedDbMan(dbManager.spawn());
111  return NinkaDatabaseHandler(spawnedDbMan);
112 }
113 
114 void NinkaDatabaseHandler::insertOrCacheLicenseIdForName(string const& rfShortName)
115 {
116  if (getCachedLicenseIdForName(rfShortName)==0)
117  {
118  unsigned long licenseId = selectOrInsertLicenseIdForName(rfShortName);
119 
120  if (licenseId > 0)
121  {
122  licenseRefCache.insert(std::make_pair(rfShortName, licenseId));
123  }
124  }
125 }
126 
127 unsigned long NinkaDatabaseHandler::getCachedLicenseIdForName(string const& rfShortName) const
128 {
129  std::unordered_map<string,long>::const_iterator findIterator = licenseRefCache.find(rfShortName);
130  if (findIterator != licenseRefCache.end())
131  {
132  return findIterator->second;
133  }
134  else
135  {
136  return 0;
137  }
138 }
DB wrapper for agents.
unsigned long stringToUnsignedLong(const char *string)
Definition: libfossUtils.cc:31
Database handler for agents.
Wrapper for DB result.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
General utility functions for CPP.
int getRowCount() const
fo namespace holds the FOSSology library functions.
std::vector< T > getSimpleResults(int columnN, T(functionP)(const char *)) const
Get vector of a single column from query result.