19 #include "database.hpp" 20 #include "identity.hpp" 27 #define RETURN_IF_FALSE(query) \ 68 for (
size_t i = 0; i < size; ++i)
88 for (
size_t i = 0; i < size; ++i)
113 int failedCounter = 0;
114 bool tablesChecked =
false;
117 while (!tablesChecked && failedCounter < MAX_TABLE_CREATION_RETRIES)
121 tablesChecked = createTableAgentFindings() && createTableClearing();
129 if (failedCounter < MAX_TABLE_CREATION_RETRIES)
130 std::cout <<
"WARNING: table creation failed: trying again" 131 " (" << failedCounter <<
"/" << MAX_TABLE_CREATION_RETRIES <<
")" 135 if (tablesChecked && (failedCounter > 0))
136 std::cout <<
"NOTICE: table creation succeded on try " 137 << failedCounter <<
"/" << MAX_TABLE_CREATION_RETRIES
141 return tablesChecked;
150 #define SEQUENCE_NAME IDENTITY"_pk_seq" 151 #define COLUMN_NAME_PK IDENTITY"_pk" 152 { COLUMN_NAME_PK,
"bigint",
"PRIMARY KEY DEFAULT nextval('" SEQUENCE_NAME
"'::regclass)"},
153 {
"agent_fk",
"bigint",
"NOT NULL"},
154 {
"pfile_fk",
"bigint",
"NOT NULL"},
155 {
"content",
"text",
""},
156 {
"hash",
"text",
""},
157 {
"type",
"text",
""},
158 {
"copy_startbyte",
"integer",
""},
159 {
"copy_endbyte",
"integer",
""},
160 {
"is_enabled",
"boolean",
"NOT NULL DEFAULT TRUE"},
170 if (!
dbManager.sequenceExists(SEQUENCE_NAME))
172 RETURN_IF_FALSE(
dbManager.queryPrintf(
"CREATE SEQUENCE " 184 RETURN_IF_FALSE(
dbManager.queryPrintf(
"CREATE table %s(%s)", IDENTITY,
185 getColumnCreationString(CopyrightDatabaseHandler::columns, ncolumns).c_str()
189 "CREATE INDEX %s_agent_fk_index" 191 " USING BTREE (agent_fk)",
196 "CREATE INDEX %s_hash_index" 198 " USING BTREE (hash)",
203 "CREATE INDEX %s_pfile_fk_index" 205 " USING BTREE (pfile_fk)",
210 "ALTER TABLE ONLY %s" 211 " ADD CONSTRAINT agent_fk" 212 " FOREIGN KEY (agent_fk)" 213 " REFERENCES agent(agent_pk) ON DELETE CASCADE",
218 "ALTER TABLE ONLY %s" 219 " ADD CONSTRAINT pfile_fk" 220 " FOREIGN KEY (pfile_fk)" 221 " REFERENCES pfile(pfile_pk) ON DELETE CASCADE",
232 #define SEQUENCE_NAMEClearing IDENTITY"_decision_pk_seq" 233 {IDENTITY
"_decision_pk",
"bigint",
"PRIMARY KEY DEFAULT nextval('" SEQUENCE_NAMEClearing
"'::regclass)"},
234 {
"user_fk",
"bigint",
"NOT NULL"},
235 {
"pfile_fk",
"bigint",
"NOT NULL"},
236 {
"clearing_decision_type_fk",
"bigint",
"NOT NULL"},
237 {
"description",
"text",
""},
238 {
"textFinding",
"text",
""},
239 {
"comment",
"text",
""},
240 {
"is_enabled",
"boolean",
"NOT NULL DEFAULT TRUE"}
250 #define CLEARING_TABLE IDENTITY "_decision" 252 if (!
dbManager.sequenceExists(SEQUENCE_NAMEClearing))
254 RETURN_IF_FALSE(
dbManager.queryPrintf(
"CREATE SEQUENCE " 255 SEQUENCE_NAMEClearing
263 if (!
dbManager.tableExists(CLEARING_TABLE))
266 RETURN_IF_FALSE(
dbManager.queryPrintf(
"CREATE table %s(%s)", CLEARING_TABLE,
267 getColumnCreationString(CopyrightDatabaseHandler::columnsDecision, nDec).c_str()));
270 "CREATE INDEX %s_pfile_fk_index" 272 " USING BTREE (pfile_fk)",
273 CLEARING_TABLE, CLEARING_TABLE
277 "CREATE INDEX %s_user_fk_index" 279 " USING BTREE (user_fk)",
280 CLEARING_TABLE, CLEARING_TABLE
284 "CREATE INDEX %s_clearing_decision_type_fk_index" 286 " USING BTREE (clearing_decision_type_fk)",
287 CLEARING_TABLE, CLEARING_TABLE
291 "ALTER TABLE ONLY %s" 292 " ADD CONSTRAINT user_fk" 293 " FOREIGN KEY (user_fk)" 294 " REFERENCES users(user_pk) ON DELETE CASCADE",
299 "ALTER TABLE ONLY %s" 300 " ADD CONSTRAINT pfile_fk" 301 " FOREIGN KEY (pfile_fk)" 302 " REFERENCES pfile(pfile_pk) ON DELETE CASCADE",
319 std::string uploadTreeTableName = queryUploadTreeTableName(uploadId);
321 std::string sql =
"SELECT pfile_pk" 323 " SELECT distinct(pfile_fk) AS PF" 324 " FROM " + uploadTreeTableName +
325 " WHERE upload_fk = $1 and (ufile_mode&x'3C000000'::int)=0" 327 "LEFT OUTER JOIN " IDENTITY
" ON (PF = pfile_fk AND agent_fk = $2) " 328 #ifdef IDENTITY_COPYRIGHT 329 "LEFT OUTER JOIN author AS au ON (PF = au.pfile_fk AND au.agent_fk = $2) " 331 "INNER JOIN pfile ON (PF = pfile_pk) " 332 #ifdef IDENTITY_COPYRIGHT 333 "WHERE copyright.copyright_pk IS NULL AND au.author_pk IS NULL" 335 "WHERE (" IDENTITY
"_pk IS NULL OR agent_fk <> $2)" 338 std::string statementName =
"queryFileIdsForUpload:" IDENTITY
"Agent" + uploadTreeTableName;
339 if (ignoreFilesWithMimeType)
341 sql = sql +
" AND (pfile_mimetypefk NOT IN ( " 342 "SELECT mimetype_pk FROM mimetype WHERE mimetype_name=ANY(string_to_array(( " 343 "SELECT conf_value FROM sysconfig WHERE variablename='SkipFiles'),','))));";
344 statementName = statementName +
"withMimetype";
347 fo_dbManager_PrepareStamement(
dbManager.getStruct_dbManager(),
348 statementName.c_str(),
367 fo_dbManager_PrepareStamement(
369 "insertNoResultInDatabase",
372 "(agent_fk, pfile_fk) VALUES($1,$2)",
387 std::string tableName = IDENTITY;
389 if(
"author" == entry.
type ||
390 "email" == entry.
type ||
391 "url" == entry.
type){
392 tableName =
"author";
396 fo_dbManager_PrepareStamement(
398 (
"insertInDatabaseFor" + tableName).c_str(),
399 (
"INSERT INTO "+ tableName +
400 "(agent_fk, pfile_fk, content, hash, type, copy_startbyte, copy_endbyte)" +
401 " VALUES($1,$2,$3,md5($3),$4,$5,$6)").c_str(),
402 long, long,
char*,
char*, int, int
Holds the column related data for table creation.
std::string getColumnListString(const ColumnDef in[], size_t size) const
Given a list of ColumnDef, return a comma separated list of column names.
std::vector< unsigned long > queryFileIdsForUpload(int agentId, int uploadId, bool ignoreFilesWithMimeType)
Get the list of pfile ids on which the given agent has no findings for a given upload.
CopyrightDatabaseHandler(fo::DbManager manager)
Constructor to initialize database handler.
bool insertInDatabase(DatabaseEntry &entry) const
Insert a finding in database.
const char * creationFlags
Manages database related requests for agent.
static const ColumnDef columns[]
Columns required by agent in database.
unsigned long stringToUnsignedLong(const char *string)
Database handler for agents.
std::string type
Type of statement found.
DatabaseEntry()
Default constructor for DatabaseEntry.
bool createTables() const
Create tables required by agent.
fo_dbManager * dbManager
fo_dbManager object
General utility functions for CPP.
static const ColumnDef columnsDecision[]
Columns required to store user decisions in database.
bool insertNoResultInDatabase(long agentId, long pFileId) const
Insert empty findings in database to prevent scan on next upload.
fo namespace holds the FOSSology library functions.
std::string getColumnCreationString(const ColumnDef in[], size_t size) const
Return a comma delimited string with column elements separated by space. The string is used for datab...
Maps agent data to database schema.
bool createTableClearing() const
Create table to store user decisions.
CopyrightDatabaseHandler spawn() const
Spawn/fork a new database handler and return it.
bool createTableAgentFindings() const
Create table to store agent find data.