8 #include "database.hpp"
9 #include "identity.hpp"
16 #define RETURN_IF_FALSE(query) \
57 for (
size_t i = 0; i < size; ++i)
77 for (
size_t i = 0; i < size; ++i)
85 result += in[i].creationFlags;
102 int failedCounter = 0;
103 bool tablesChecked =
false;
106 while (!tablesChecked && failedCounter < MAX_TABLE_CREATION_RETRIES)
118 if (failedCounter < MAX_TABLE_CREATION_RETRIES)
119 std::cout <<
"WARNING: table creation failed: trying again"
120 " (" << failedCounter <<
"/" << MAX_TABLE_CREATION_RETRIES <<
")"
124 if (tablesChecked && (failedCounter > 0))
125 std::cout <<
"NOTICE: table creation succeded on try "
126 << failedCounter <<
"/" << MAX_TABLE_CREATION_RETRIES
130 return tablesChecked;
139 #define SEQUENCE_NAME IDENTITY"_pk_seq"
140 #define COLUMN_NAME_PK IDENTITY"_pk"
141 { COLUMN_NAME_PK,
"bigint",
"PRIMARY KEY DEFAULT nextval('" SEQUENCE_NAME
"'::regclass)"},
142 {
"agent_fk",
"bigint",
"NOT NULL"},
143 {
"pfile_fk",
"bigint",
"NOT NULL"},
144 {
"content",
"text",
""},
145 {
"hash",
"text",
""},
146 {
"type",
"text",
""},
147 {
"copy_startbyte",
"integer",
""},
148 {
"copy_endbyte",
"integer",
""},
149 {
"is_enabled",
"boolean",
"NOT NULL DEFAULT TRUE"},
178 "CREATE INDEX %s_agent_fk_index"
180 " USING BTREE (agent_fk)",
185 "CREATE INDEX %s_hash_index"
187 " USING BTREE (hash)",
192 "CREATE INDEX %s_pfile_fk_index"
194 " USING BTREE (pfile_fk)",
199 "ALTER TABLE ONLY %s"
200 " ADD CONSTRAINT agent_fk"
201 " FOREIGN KEY (agent_fk)"
202 " REFERENCES agent(agent_pk) ON DELETE CASCADE",
207 "ALTER TABLE ONLY %s"
208 " ADD CONSTRAINT pfile_fk"
209 " FOREIGN KEY (pfile_fk)"
210 " REFERENCES pfile(pfile_pk) ON DELETE CASCADE",
221 #define SEQUENCE_NAMEClearing IDENTITY"_decision_pk_seq"
222 {IDENTITY
"_decision_pk",
"bigint",
"PRIMARY KEY DEFAULT nextval('" SEQUENCE_NAMEClearing
"'::regclass)"},
223 {
"user_fk",
"bigint",
"NOT NULL"},
224 {
"pfile_fk",
"bigint",
"NOT NULL"},
225 {
"clearing_decision_type_fk",
"bigint",
"NOT NULL"},
226 {
"description",
"text",
""},
227 {
"textFinding",
"text",
""},
228 {
"comment",
"text",
""},
229 {
"is_enabled",
"boolean",
"NOT NULL DEFAULT TRUE"}
239 #define CLEARING_TABLE IDENTITY "_decision"
244 SEQUENCE_NAMEClearing
259 "CREATE INDEX %s_pfile_fk_index"
261 " USING BTREE (pfile_fk)",
262 CLEARING_TABLE, CLEARING_TABLE
266 "CREATE INDEX %s_user_fk_index"
268 " USING BTREE (user_fk)",
269 CLEARING_TABLE, CLEARING_TABLE
273 "CREATE INDEX %s_clearing_decision_type_fk_index"
275 " USING BTREE (clearing_decision_type_fk)",
276 CLEARING_TABLE, CLEARING_TABLE
280 "ALTER TABLE ONLY %s"
281 " ADD CONSTRAINT user_fk"
282 " FOREIGN KEY (user_fk)"
283 " REFERENCES users(user_pk) ON DELETE CASCADE",
288 "ALTER TABLE ONLY %s"
289 " ADD CONSTRAINT pfile_fk"
290 " FOREIGN KEY (pfile_fk)"
291 " REFERENCES pfile(pfile_pk) ON DELETE CASCADE",
310 std::string sql =
"SELECT pfile_pk"
312 " SELECT distinct(pfile_fk) AS PF"
313 " FROM " + uploadTreeTableName +
314 " WHERE upload_fk = $1 and (ufile_mode&x'3C000000'::int)=0"
316 "LEFT OUTER JOIN " IDENTITY
" ON (PF = pfile_fk AND agent_fk = $2) "
317 #ifdef IDENTITY_COPYRIGHT
318 "LEFT OUTER JOIN author AS au ON (PF = au.pfile_fk AND au.agent_fk = $2) "
320 "INNER JOIN pfile ON (PF = pfile_pk) "
321 #ifdef IDENTITY_COPYRIGHT
322 "WHERE copyright.copyright_pk IS NULL AND au.author_pk IS NULL"
324 "WHERE (" IDENTITY
"_pk IS NULL OR agent_fk <> $2)"
327 std::string statementName =
"queryFileIdsForUpload:" IDENTITY
"Agent" + uploadTreeTableName;
328 if (ignoreFilesWithMimeType)
330 sql = sql +
" AND (pfile_mimetypefk NOT IN ( "
331 "SELECT mimetype_pk FROM mimetype WHERE mimetype_name=ANY(string_to_array(( "
332 "SELECT conf_value FROM sysconfig WHERE variablename='SkipFiles'),','))));";
333 statementName = statementName +
"withMimetype";
337 statementName.c_str(),
355 std::string tableName = IDENTITY;
357 if(
"author" == entry.
type ||
358 "email" == entry.
type ||
359 "url" == entry.
type){
360 tableName =
"author";
364 fo_dbManager_PrepareStamement(
366 (
"insertInDatabaseFor" + tableName).c_str(),
367 (
"INSERT INTO "+ tableName +
368 "(agent_fk, pfile_fk, content, hash, type, copy_startbyte, copy_endbyte)" +
369 " VALUES($1,$2,$3,md5($3),$4,$5,$6)").c_str(),
370 long,
long,
char*,
char*,
int,
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 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.