7 #include "scancode_dbhandler.hpp"
30 unsigned long pfileId) :
31 agent_fk(agentId), pfile_fk(pfileId), hash(
"")
48 for (
size_t i = 0; i < size; ++i)
56 result += in[i].creationFlags;
102 "scancodeInsertNoLicense",
103 "INSERT INTO license_file"
104 "(agent_fk, pfile_fk, rf_fk)"
108 agentId, pFileId, licenseId);
127 fo_dbManager_PrepareStamement(
131 "selectExisting AS ("
132 "SELECT fl_pk FROM ONLY license_file"
133 " WHERE (agent_fk = $1 AND pfile_fk = $2 AND rf_fk = $3)"
136 "INSERT INTO license_file"
137 "(agent_fk, pfile_fk, rf_fk, rf_match_pct)"
138 " SELECT $1, $2, $3, $4"
139 " WHERE NOT EXISTS(SELECT * FROM license_file WHERE (agent_fk = $1 AND pfile_fk = $2 AND rf_fk = $3))"
142 "SELECT fl_pk FROM insertNew "
144 "SELECT fl_pk FROM selectExisting",
145 int,
long,
long,
unsigned
153 long licenseFilePK= -1;
154 if(!result.isFailed()){
156 vector<unsigned long> res = result.getSimpleResults<
unsigned long>(0,
159 licenseFilePK = res.at(0);
161 return licenseFilePK;
177 fo_dbManager_PrepareStamement(
180 "INSERT INTO highlight"
181 "(fl_fk, type, start, len)"
182 " SELECT $1, 'L', $2, $3 "
183 " WHERE NOT EXISTS(SELECT * FROM highlight WHERE (fl_fk = $1 AND start = $2 AND len = $3))",
184 long,
unsigned,
unsigned
205 licenseRefCache.insert(std::make_pair(rfShortName, licenseId));
217 auto findIterator = licenseRefCache.find(rfShortName);
218 if (findIterator != licenseRefCache.end())
220 return findIterator->second;
234 bool hasEnding(
string const &firstString,
string const &ending)
236 if (firstString.length() >= ending.length())
239 == firstString.compare(firstString.length() - ending.length(),
240 ending.length(), ending));
257 bool success =
false;
258 unsigned long result = 0;
264 unicodeCleanShortname.toUTF8String(rfShortName);
268 "selectLicenseIdWithOrScancode",
269 " SELECT rf_pk FROM ONLY license_ref"
270 " WHERE LOWER(rf_shortname) = LOWER($1)"
271 " OR LOWER(rf_shortname) = LOWER($2);",
276 string tempShortName(rfShortName);
278 std::transform(tempShortName.begin(), tempShortName.end(), tempShortName.begin(),
281 string orLater(
"-or-later");
283 unsigned long int plusLast = tempShortName.rfind(plus);
284 unsigned long int orLaterLast = tempShortName.rfind(orLater);
287 if (plusLast != string::npos)
289 tempShortName.erase(plusLast, string::npos);
291 if (orLaterLast != string::npos)
293 tempShortName.erase(orLaterLast, string::npos);
297 (tempShortName + plus).c_str(), (tempShortName + orLater).c_str());
299 success = queryResult && queryResult.
getRowCount() > 0;
307 string tempShortName(rfShortName);
309 std::transform(tempShortName.begin(), tempShortName.end(), tempShortName.begin(),
311 string only(
"-only");
313 unsigned long int onlyLast = tempShortName.rfind(only);
316 if (onlyLast != string::npos)
318 tempShortName.erase(onlyLast, string::npos);
322 tempShortName.c_str(), (tempShortName + only).c_str());
324 success = queryResult && queryResult.
getRowCount() > 0;
338 while ((!success) && count++<3)
345 fo_dbManager_PrepareStamement(
347 "selectOrInsertLicenseIdForName",
349 "selectExisting AS ("
350 "SELECT rf_pk FROM ONLY license_ref"
351 " WHERE rf_shortname = $1"
354 "INSERT INTO license_ref(rf_shortname, rf_text, rf_detector_type, rf_fullname, rf_url)"
355 " SELECT $1, $2, $3, $4, $5"
356 " WHERE NOT EXISTS(SELECT * FROM selectExisting)"
360 "SELECT rf_pk FROM insertNew "
362 "SELECT rf_pk FROM selectExisting",
363 char*,
char*,
int,
char* ,
char*
366 "License by Scancode.",
373 success = queryResult && queryResult.
getRowCount() > 0;
396 std::string tableName =
"scancode_author";
398 if(
"scancode_statement" == entry.
type ){
399 tableName =
"scancode_copyright";
403 fo_dbManager_PrepareStamement(
405 (
"insertInDatabaseFor " + tableName).c_str(),
406 (
"INSERT INTO "+ tableName +
407 "(agent_fk, pfile_fk, content, hash, type, copy_startbyte, copy_endbyte)" +
408 " SELECT $1, $2, $3, md5($3), $4, $5, $6 "
409 " WHERE NOT EXISTS(SELECT * FROM " + tableName +
410 " WHERE (agent_fk= $1 AND pfile_fk = $2 AND hash = md5($3)))").c_str(),
411 long,
long,
char*,
char*,
int,
int
426 int failedCounter = 0;
427 bool tablesChecked =
false;
430 while (!tablesChecked && failedCounter < MAX_TABLE_CREATION_RETRIES)
442 if (failedCounter < MAX_TABLE_CREATION_RETRIES)
443 LOG_WARNING(
"table creation failed: trying again (%d/%d) \n", failedCounter, MAX_TABLE_CREATION_RETRIES);
446 if (tablesChecked && (failedCounter > 0))
447 LOG_NOTICE(
"table creation succeeded on try %d/%d \n", failedCounter, MAX_TABLE_CREATION_RETRIES);
449 return tablesChecked;
457 #define CSEQUENCE_NAME "scancode_copyright_pk_seq"
458 #define CCOLUMN_NAME_PK "scancode_copyright_pk"
459 {CCOLUMN_NAME_PK,
"bigint",
460 "PRIMARY KEY DEFAULT nextval('" CSEQUENCE_NAME
"'::regclass)"},
461 {
"agent_fk",
"bigint",
"NOT NULL"},
462 {
"pfile_fk",
"bigint",
"NOT NULL"},
463 {
"content",
"text",
""},
464 {
"hash",
"text",
""},
465 {
"type",
"text",
""},
466 {
"copy_startbyte",
"integer",
""},
467 {
"copy_endbyte",
"integer",
""},
468 {
"is_enabled",
"boolean",
"NOT NULL DEFAULT TRUE"},
476 #define ASEQUENCE_NAME "scancode_author_pk_seq"
477 #define ACOLUMN_NAME_PK "scancode_author_pk"
478 {ACOLUMN_NAME_PK,
"bigint",
479 "PRIMARY KEY DEFAULT nextval('" ASEQUENCE_NAME
"'::regclass)"},
480 {
"agent_fk",
"bigint",
"NOT NULL"},
481 {
"pfile_fk",
"bigint",
"NOT NULL"},
482 {
"content",
"text",
""},
483 {
"hash",
"text",
""},
484 {
"type",
"text",
""},
485 {
"copy_startbyte",
"integer",
""},
486 {
"copy_endbyte",
"integer",
""},
487 {
"is_enabled",
"boolean",
"NOT NULL DEFAULT TRUE"},
497 const char *tablename =
"";
498 const char *sequencename =
"";
499 if (tableName ==
"scancode_copyright") {
500 tablename =
"scancode_copyright";
501 sequencename =
"scancode_copyright_pk_seq";
502 }
else if (tableName ==
"scancode_author") {
503 tablename =
"scancode_author";
504 sequencename =
"scancode_author_pk_seq";
512 " CACHE 1",sequencename));
517 if (tableName ==
"scancode_copyright") {
523 }
else if (tableName ==
"scancode_author") {
532 "CREATE INDEX %s_agent_fk_index"
534 " USING BTREE (agent_fk)",
539 "CREATE INDEX %s_hash_index"
541 " USING BTREE (hash)",
546 "CREATE INDEX %s_pfile_fk_index"
548 " USING BTREE (pfile_fk)",
553 "ALTER TABLE ONLY %s"
554 " ADD CONSTRAINT agent_fk"
555 " FOREIGN KEY (agent_fk)"
556 " REFERENCES agent(agent_pk) ON DELETE CASCADE",
561 "ALTER TABLE ONLY %s"
562 " ADD CONSTRAINT pfile_fk"
563 " FOREIGN KEY (pfile_fk)"
564 " REFERENCES pfile(pfile_pk) ON DELETE CASCADE",
576 #define CESEQUENCE_NAME "scancode_copyright_event_pk_seq"
577 #define CECOLUMN_NAME_PK "scancode_copyright_event_pk"
578 {CECOLUMN_NAME_PK,
"bigint",
579 "PRIMARY KEY DEFAULT nextval('" CESEQUENCE_NAME
"'::regclass)"},
580 {
"upload_fk",
"bigint",
"NOT NULL"},
581 {
"uploadtree_fk",
"bigint",
"NOT NULL"},
582 {
"scancode_copyright_fk",
"bigint",
"NOT NULL"},
583 {
"content",
"text",
""},
584 {
"hash",
"text",
""},
585 {
"is_enabled",
"boolean",
"NOT NULL DEFAULT FALSE"},
586 {
"scope",
"int4",
"NOT NULL"},
594 #define AESEQUENCE_NAME "scancode_author_event_pk_seq"
595 #define AECOLUMN_NAME_PK "scancode_author_event_pk"
596 {AECOLUMN_NAME_PK,
"bigint",
597 "PRIMARY KEY DEFAULT nextval('" AESEQUENCE_NAME
"'::regclass)"},
598 {
"upload_fk",
"bigint",
"NOT NULL"},
599 {
"uploadtree_fk",
"bigint",
"NOT NULL"},
600 {
"scancode_author_fk",
"bigint",
"NOT NULL"},
601 {
"content",
"text",
""},
602 {
"hash",
"text",
""},
603 {
"is_enabled",
"boolean",
"NOT NULL DEFAULT FALSE"},
604 {
"scope",
"int4",
"NOT NULL"},
614 const char *tablename =
"";
615 const char *etablename =
"";
616 const char *esequencename =
"";
617 if (tableName ==
"scancode_copyright_event") {
618 etablename =
"scancode_copyright_event";
619 esequencename =
"scancode_copyright_event_pk_seq";
620 tablename =
"scancode_copyright";
621 }
else if (tableName ==
"scancode_author_event") {
622 etablename =
"scancode_author_event";
623 esequencename =
"scancode_author_event_pk_seq";
624 tablename =
"scancode_author";
632 " CACHE 1",esequencename));
637 if (tableName ==
"scancode_copyright_event") {
643 }
else if (tableName ==
"scancode_author_event") {
651 "CREATE INDEX %s_upload_fk_index"
653 " USING BTREE (upload_fk)",
654 etablename, etablename
657 "CREATE INDEX %s_uploadtree_fk_index"
659 " USING BTREE (uploadtree_fk)",
660 etablename, etablename
663 "CREATE INDEX %s_scancode_fk_index"
665 " USING BTREE (%s_fk)",
666 etablename, etablename, tablename
670 "ALTER TABLE ONLY %s"
671 " ADD CONSTRAINT upload_fk"
672 " FOREIGN KEY (upload_fk)"
673 " REFERENCES upload(upload_pk) ON DELETE CASCADE",
679 "ALTER TABLE ONLY %s"
680 " ADD CONSTRAINT %s_fk"
681 " FOREIGN KEY (%s_fk)"
682 " REFERENCES %s(%s_pk) ON DELETE CASCADE",
683 etablename, tablename, tablename, tablename, tablename
687 " ALTER COLUMN scope"
bool hasEnding(string const &firstString, string const &ending)
Maps agent data to database schema.
std::string type
Type of statement found.
DatabaseEntry()
Default constructor for DatabaseEntry.
bool saveHighlightInfo(long licenseFileId, unsigned start, unsigned length)
save highlight information in the highlight table
static const ColumnDef columns_copyright_event[]
Columns required to store copyright deactivated statement.
unsigned long selectOrInsertLicenseIdForName(std::string rfShortname, std::string rfFullname, std::string rfTexturl)
insert license if not present in license_ref table and return rf_pk
bool createTables() const
create tables to save copyright and author informations
unsigned long getCachedLicenseIdForName(std::string const &rfShortName) const
for given short name search license
static const ColumnDef columns_copyright[]
Columns required to store copyright information by scancode agent.
long saveLicenseMatch(int agentId, long pFileId, long licenseId, int percentMatch)
save license match with license_ref table in license_file table Insert license if already not present...
static const ColumnDef columns_author_event[]
Columns required to store author deactivated statement.
bool insertInDatabase(DatabaseEntry &entry) const
insert copyright/author in scancode_copyright/scancode_author table
void insertOrCacheLicenseIdForName(std::string const &rfShortName, std::string const &rfFullname, std::string const &rfTexturl)
calling function for selectOrInsertLicenseIdForName
bool insertNoResultInDatabase(int agentId, long pFileId, long licenseId)
Insert null value of license for uploads having no licenses.
std::string getColumnCreationString(const ColumnDef in[], size_t size) const
get string of parameters for a column for table creation
std::vector< unsigned long > queryFileIdsForUpload(int uploadId, bool ignoreFilesWithMimeType)
Function to get pfile ID for uploads.
ScancodeDatabaseHandler(fo::DbManager dbManager)
Default constructor for ScanCode Database Handler class.
bool createTableAgentFindings(string tablename) const
create table to store agent findings
static const ColumnDef columns_author[]
Columns required to store author information by scancode agent.
bool createTableAgentEvents(string tablename) const
create table to store agent events
ScancodeDatabaseHandler spawn() const
Instantiate a new object spawn for ScanCode Database handler Used to create new objects for threads.
Database handler for agents.
std::vector< unsigned long > queryFileIdsVectorForUpload(int uploadId, bool ignoreFilesWithMimeType) const
Get pfile ids 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.
fo_dbManager * dbManager
fo_dbManager object
fo namespace holds the FOSSology library functions.
unsigned long stringToUnsignedLong(const char *string)
icu::UnicodeString recodeToUnicode(const std::string &input)
start($application)
start the application Assumes application is restartable via /etc/init.d/<script>....
Holds the column related data for table creation.
Store the results of a regex match.