8 #include "database.hpp"
9 #include "identity.hpp"
16 #define RETURN_IF_FALSE(query) \
58 for (
size_t i = 0; i < size; ++i)
78 for (
size_t i = 0; i < size; ++i)
86 result += in[i].creationFlags;
103 int failedCounter = 0;
104 bool tablesChecked =
false;
107 while (!tablesChecked && failedCounter < MAX_TABLE_CREATION_RETRIES)
119 if (failedCounter < MAX_TABLE_CREATION_RETRIES)
120 std::cout <<
"WARNING: table creation failed: trying again"
121 " (" << failedCounter <<
"/" << MAX_TABLE_CREATION_RETRIES <<
")"
125 if (tablesChecked && (failedCounter > 0))
126 std::cout <<
"NOTICE: table creation succeded on try "
127 << failedCounter <<
"/" << MAX_TABLE_CREATION_RETRIES
131 return tablesChecked;
140 #define SEQUENCE_NAME IDENTITY"_pk_seq"
141 #define COLUMN_NAME_PK IDENTITY"_pk"
142 { COLUMN_NAME_PK,
"bigint",
"PRIMARY KEY DEFAULT nextval('" SEQUENCE_NAME
"'::regclass)"},
143 {
"agent_fk",
"bigint",
"NOT NULL"},
144 {
"pfile_fk",
"bigint",
"NOT NULL"},
145 {
"content",
"text",
""},
146 {
"hash",
"text",
""},
147 {
"type",
"text",
""},
148 {
"copy_startbyte",
"integer",
""},
149 {
"copy_endbyte",
"integer",
""},
150 {
"is_enabled",
"boolean",
"NOT NULL DEFAULT TRUE"},
179 "CREATE INDEX %s_agent_fk_index"
181 " USING BTREE (agent_fk)",
186 "CREATE INDEX %s_hash_index"
188 " USING BTREE (hash)",
193 "CREATE INDEX %s_pfile_fk_index"
195 " USING BTREE (pfile_fk)",
200 "ALTER TABLE ONLY %s"
201 " ADD CONSTRAINT agent_fk"
202 " FOREIGN KEY (agent_fk)"
203 " REFERENCES agent(agent_pk) ON DELETE CASCADE",
208 "ALTER TABLE ONLY %s"
209 " ADD CONSTRAINT pfile_fk"
210 " FOREIGN KEY (pfile_fk)"
211 " REFERENCES pfile(pfile_pk) ON DELETE CASCADE",
222 #define SEQUENCE_NAMEClearing IDENTITY"_decision_pk_seq"
223 {IDENTITY
"_decision_pk",
"bigint",
"PRIMARY KEY DEFAULT nextval('" SEQUENCE_NAMEClearing
"'::regclass)"},
224 {
"user_fk",
"bigint",
"NOT NULL"},
225 {
"pfile_fk",
"bigint",
"NOT NULL"},
226 {
"clearing_decision_type_fk",
"bigint",
"NOT NULL"},
227 {
"description",
"text",
""},
228 {
"textFinding",
"text",
""},
229 {
"comment",
"text",
""},
230 {
"is_enabled",
"boolean",
"NOT NULL DEFAULT TRUE"}
240 #define CLEARING_TABLE IDENTITY "_decision"
245 SEQUENCE_NAMEClearing
260 "CREATE INDEX %s_pfile_fk_index"
262 " USING BTREE (pfile_fk)",
263 CLEARING_TABLE, CLEARING_TABLE
267 "CREATE INDEX %s_user_fk_index"
269 " USING BTREE (user_fk)",
270 CLEARING_TABLE, CLEARING_TABLE
274 "CREATE INDEX %s_clearing_decision_type_fk_index"
276 " USING BTREE (clearing_decision_type_fk)",
277 CLEARING_TABLE, CLEARING_TABLE
281 "ALTER TABLE ONLY %s"
282 " ADD CONSTRAINT user_fk"
283 " FOREIGN KEY (user_fk)"
284 " REFERENCES users(user_pk) ON DELETE CASCADE",
289 "ALTER TABLE ONLY %s"
290 " ADD CONSTRAINT pfile_fk"
291 " FOREIGN KEY (pfile_fk)"
292 " REFERENCES pfile(pfile_pk) ON DELETE CASCADE",
311 std::string sql =
"SELECT pfile_pk"
313 " SELECT distinct(pfile_fk) AS PF"
314 " FROM " + uploadTreeTableName +
315 " WHERE upload_fk = $1 and (ufile_mode&x'3C000000'::int)=0"
317 "LEFT OUTER JOIN " IDENTITY
" ON (PF = pfile_fk AND agent_fk = $2) "
318 #ifdef IDENTITY_COPYRIGHT
319 "LEFT OUTER JOIN author AS au ON (PF = au.pfile_fk AND au.agent_fk = $2) "
321 "INNER JOIN pfile ON (PF = pfile_pk) "
322 #ifdef IDENTITY_COPYRIGHT
323 "WHERE copyright.copyright_pk IS NULL AND au.author_pk IS NULL"
325 "WHERE (" IDENTITY
"_pk IS NULL OR agent_fk <> $2)"
328 std::string statementName =
"queryFileIdsForUpload:" IDENTITY
"Agent" + uploadTreeTableName;
329 if (ignoreFilesWithMimeType)
331 sql = sql +
" AND (pfile_mimetypefk NOT IN ( "
332 "SELECT mimetype_pk FROM mimetype WHERE mimetype_name=ANY(string_to_array(( "
333 "SELECT conf_value FROM sysconfig WHERE variablename='SkipFiles'),','))));";
334 statementName = statementName +
"withMimetype";
338 statementName.c_str(),
356 std::string tableName = IDENTITY;
358 if(
"author" == entry.
type ||
359 "email" == entry.
type ||
360 "url" == entry.
type){
361 tableName =
"author";
365 fo_dbManager_PrepareStamement(
367 (
"insertInDatabaseFor" + tableName).c_str(),
368 (
"INSERT INTO "+ tableName +
369 "(agent_fk, pfile_fk, content, hash, type, copy_startbyte, copy_endbyte)" +
370 " VALUES($1,$2,$3,md5($3),$4,$5,$6)").c_str(),
371 long,
long,
char*,
char*,
int,
int
388 int uploadId,
const std::string& uploadTreeTableName)
const
392 "INSERT INTO copyright_event (upload_fk, copyright_fk, uploadtree_fk) "
393 "SELECT ut.upload_fk, cp.copyright_pk, ut.uploadtree_pk "
394 "FROM copyright AS cp "
395 "INNER JOIN " + uploadTreeTableName +
" AS ut ON cp.pfile_fk = ut.pfile_fk "
396 "WHERE cp.hash = md5($1) AND cp.agent_fk = $2 "
397 " AND cp.pfile_fk = $3 AND ut.upload_fk = $4";
400 fo_dbManager_PrepareStamement(
402 (
"insertDeactivatedEventsFor" + uploadTreeTableName).c_str(),
404 char*,
long,
long,
int
Manages database related requests for agent.
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.
static const ColumnDef columns[]
Columns required by agent in database.
bool createTableAgentFindings() const
Create table to store agent find data.
bool insertInDatabase(DatabaseEntry &entry) const
Insert a finding in database.
static const ColumnDef columnsDecision[]
Columns required to store user decisions in database.
std::string getColumnListString(const ColumnDef in[], size_t size) const
Given a list of ColumnDef, return a comma separated list of column names.
CopyrightDatabaseHandler(fo::DbManager manager)
Constructor to initialize database handler.
CopyrightDatabaseHandler spawn() const
Spawn/fork a new database handler and return it.
bool insertDeactivatedEvents(const DatabaseEntry &entry, int uploadId, const std::string &uploadTreeTableName) const
Insert one deactivated finding per uploadtree node for a given upload, for a given finding.
bool createTables() const
Create tables required by agent.
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...
bool createTableClearing() const
Create table to store user decisions.
Maps agent data to database schema.
std::string type
Type of statement found.
DatabaseEntry()
Default constructor for DatabaseEntry.
Database handler for agents.
std::string queryUploadTreeTableName(int uploadId)
Get the upload tree table name for a given upload id.
DbManager dbManager
DbManager to use.
QueryResult execPrepared(fo_dbManager_PreparedStatement *stmt,...) const
Execute a prepared statement with new parameters.
bool tableExists(const char *tableName) const
void ignoreWarnings(bool) const
QueryResult queryPrintf(const char *queryFormat,...) const
Execute a query in printf format.
bool sequenceExists(const char *name) const
fo_dbManager * getStruct_dbManager() const
std::vector< T > getSimpleResults(int columnN, T(functionP)(const char *)) const
Get vector of a single column from query result.
General utility functions for CPP.
fo namespace holds the FOSSology library functions.
unsigned long stringToUnsignedLong(const char *string)
Holds the column related data for table creation.