1 /*
2  * Copyright (C) 2014, Siemens AG
3  * Author: Daniele Fognini
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2
7  * 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.
12  * See the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #ifndef DATABASE_HPP
20 #define DATABASE_HPP
21 
22 #include <string>
23 #include <vector>
24 
25 #include "libfossdbmanagerclass.hpp"
26 #include "libfossAgentDatabaseHandler.hpp"
27 #include "cleanEntries.hpp"
28 
29 #define MAX_TABLE_CREATION_RETRIES 5
30 
31 /**
32  * \class DatabaseEntry
33  * \brief Maps agent data to database schema
34  */
35 class DatabaseEntry
36 {
37 public:
38   DatabaseEntry();
39 
40   long agent_fk;                    /**< Id of agent performed the scan */
41   long pfile_fk;                    /**< Id of pfile on which the scan was performed */
42   std::string content;              /**< Statement found during the scan */
43   std::string hash;                 /**< MD5 hash of the statement */
44   /**
45    * \brief Type of statement found.
46    *
47    * Can be
48    *   - statement for Copyright
49    *   - author for Author
50    *   - url for URL
51    *   - email for email
52    *   - ecc for ECC
53    */
54   std::string type;
55   int copy_startbyte;               /**< Statement start offset from start of pfile content */
56   int copy_endbyte;                 /**< Statement end offset from start of pfile content */
57 };
58 
59 /**
60  * \class CopyrightDatabaseHandler
61  * \brief Manages database related requests for agent
62  */
63 class CopyrightDatabaseHandler : public fo::AgentDatabaseHandler
64 {
65 public:
66   CopyrightDatabaseHandler(fo::DbManager manager);
67   CopyrightDatabaseHandler(const CopyrightDatabaseHandler&) = delete;
CopyrightDatabaseHandler(CopyrightDatabaseHandler && other)68   CopyrightDatabaseHandler(CopyrightDatabaseHandler&& other) : fo::AgentDatabaseHandler(std::move(other)) {}; // = default
69   CopyrightDatabaseHandler spawn() const;
70 
71   bool createTables() const;
72   bool insertInDatabase(DatabaseEntry& entry) const;
73   bool insertNoResultInDatabase(long agentId, long pFileId) const;
74   std::vector<unsigned long> queryFileIdsForUpload(int agentId, int uploadId, bool ignoreFilesWithMimeType);
75 
76 private:
77   /**
78    * \struct ColumnDef
79    * \brief Holds the column related data for table creation
80    * \see CopyrightDatabaseHandler::columns
81    */
82   typedef struct
83   {
84     const char* name;               /**< Name of the table column */
85     const char* type;               /**< Data type of the table column */
86     const char* creationFlags;      /**< Special flags of the table column */
87   } ColumnDef;
88 
89   static const ColumnDef columns[];
90   static const ColumnDef columnsDecision[];
91 
92   bool createTableAgentFindings() const;
93   bool createTableClearing() const;
94   std::string getColumnListString(const ColumnDef in[], size_t size) const;
95   std::string getColumnCreationString(const ColumnDef in[], size_t size) const;
96 };
97 
98 #endif // DATABASE_HPP
99