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
440 #define CSEQUENCE_NAME "scancode_copyright_pk_seq"
441 #define CCOLUMN_NAME_PK "scancode_copyright_pk"
442 {CCOLUMN_NAME_PK,
"bigint",
443 "PRIMARY KEY DEFAULT nextval('" CSEQUENCE_NAME
"'::regclass)"},
444 {
"agent_fk",
"bigint",
"NOT NULL"},
445 {
"pfile_fk",
"bigint",
"NOT NULL"},
446 {
"content",
"text",
""},
447 {
"hash",
"text",
""},
448 {
"type",
"text",
""},
449 {
"copy_startbyte",
"integer",
""},
450 {
"copy_endbyte",
"integer",
""},
451 {
"is_enabled",
"boolean",
"NOT NULL DEFAULT TRUE"},
459 #define ASEQUENCE_NAME "scancode_author_pk_seq"
460 #define ACOLUMN_NAME_PK "scancode_author_pk"
461 {ACOLUMN_NAME_PK,
"bigint",
462 "PRIMARY KEY DEFAULT nextval('" ASEQUENCE_NAME
"'::regclass)"},
463 {
"agent_fk",
"bigint",
"NOT NULL"},
464 {
"pfile_fk",
"bigint",
"NOT NULL"},
465 {
"content",
"text",
""},
466 {
"hash",
"text",
""},
467 {
"type",
"text",
""},
468 {
"copy_startbyte",
"integer",
""},
469 {
"copy_endbyte",
"integer",
""},
470 {
"is_enabled",
"boolean",
"NOT NULL DEFAULT TRUE"},
480 if (tableName !=
"scancode_copyright" && tableName !=
"scancode_author") {
481 LOG_FATAL(
"createTableAgentFindings: unexpected table name '%s'", tableName.c_str());
485 const char *tablename =
"";
486 const char *sequencename =
"";
487 if (tableName ==
"scancode_copyright") {
488 tablename =
"scancode_copyright";
489 sequencename =
"scancode_copyright_pk_seq";
490 }
else if (tableName ==
"scancode_author") {
491 tablename =
"scancode_author";
492 sequencename =
"scancode_author_pk_seq";
500 " CACHE 1", sequencename));
502 if (tableName ==
"scancode_copyright") {
507 }
else if (tableName ==
"scancode_author") {
515 "CREATE INDEX IF NOT EXISTS %s_agent_fk_index"
517 " USING BTREE (agent_fk)",
522 "CREATE INDEX IF NOT EXISTS %s_hash_index"
524 " USING BTREE (hash)",
529 "CREATE INDEX IF NOT EXISTS %s_pfile_fk_index"
531 " USING BTREE (pfile_fk)",
537 "SELECT 1 FROM pg_constraint"
538 " WHERE conname = 'agent_fk' AND conrelid = '%s'::regclass",
540 RETURN_IF_FALSE(fkCheck);
544 " ALTER TABLE ONLY %s"
545 " ADD CONSTRAINT agent_fk"
546 " FOREIGN KEY (agent_fk)"
547 " REFERENCES agent(agent_pk) ON DELETE CASCADE;"
548 " EXCEPTION WHEN duplicate_object THEN NULL;"
557 "SELECT 1 FROM pg_constraint"
558 " WHERE conname = 'pfile_fk' AND conrelid = '%s'::regclass",
560 RETURN_IF_FALSE(fkCheck);
564 " ALTER TABLE ONLY %s"
565 " ADD CONSTRAINT pfile_fk"
566 " FOREIGN KEY (pfile_fk)"
567 " REFERENCES pfile(pfile_pk) ON DELETE CASCADE;"
568 " EXCEPTION WHEN duplicate_object THEN NULL;"
583 #define CESEQUENCE_NAME "scancode_copyright_event_pk_seq"
584 #define CECOLUMN_NAME_PK "scancode_copyright_event_pk"
585 {CECOLUMN_NAME_PK,
"bigint",
586 "PRIMARY KEY DEFAULT nextval('" CESEQUENCE_NAME
"'::regclass)"},
587 {
"upload_fk",
"bigint",
"NOT NULL"},
588 {
"uploadtree_fk",
"bigint",
"NOT NULL"},
589 {
"scancode_copyright_fk",
"bigint",
"NOT NULL"},
590 {
"content",
"text",
""},
591 {
"hash",
"text",
""},
592 {
"is_enabled",
"boolean",
"NOT NULL DEFAULT FALSE"},
593 {
"scope",
"int4",
"NOT NULL"},
601 #define AESEQUENCE_NAME "scancode_author_event_pk_seq"
602 #define AECOLUMN_NAME_PK "scancode_author_event_pk"
603 {AECOLUMN_NAME_PK,
"bigint",
604 "PRIMARY KEY DEFAULT nextval('" AESEQUENCE_NAME
"'::regclass)"},
605 {
"upload_fk",
"bigint",
"NOT NULL"},
606 {
"uploadtree_fk",
"bigint",
"NOT NULL"},
607 {
"scancode_author_fk",
"bigint",
"NOT NULL"},
608 {
"content",
"text",
""},
609 {
"hash",
"text",
""},
610 {
"is_enabled",
"boolean",
"NOT NULL DEFAULT FALSE"},
611 {
"scope",
"int4",
"NOT NULL"},
621 if (tableName !=
"scancode_copyright_event" && tableName !=
"scancode_author_event") {
622 LOG_FATAL(
"createTableAgentEvents: unexpected table name '%s'", tableName.c_str());
626 const char *tablename =
"";
627 const char *etablename =
"";
628 const char *esequencename =
"";
629 if (tableName ==
"scancode_copyright_event") {
630 etablename =
"scancode_copyright_event";
631 esequencename =
"scancode_copyright_event_pk_seq";
632 tablename =
"scancode_copyright";
633 }
else if (tableName ==
"scancode_author_event") {
634 etablename =
"scancode_author_event";
635 esequencename =
"scancode_author_event_pk_seq";
636 tablename =
"scancode_author";
644 " CACHE 1", esequencename));
646 if (tableName ==
"scancode_copyright_event") {
651 }
else if (tableName ==
"scancode_author_event") {
659 "CREATE INDEX IF NOT EXISTS %s_upload_fk_index"
661 " USING BTREE (upload_fk)",
662 etablename, etablename
666 "CREATE INDEX IF NOT EXISTS %s_uploadtree_fk_index"
668 " USING BTREE (uploadtree_fk)",
669 etablename, etablename
673 "CREATE INDEX IF NOT EXISTS %s_scancode_fk_index"
675 " USING BTREE (%s_fk)",
676 etablename, etablename, tablename
681 "SELECT 1 FROM pg_constraint"
682 " WHERE conname = 'upload_fk' AND conrelid = '%s'::regclass",
684 RETURN_IF_FALSE(fkCheck);
688 " ALTER TABLE ONLY %s"
689 " ADD CONSTRAINT upload_fk"
690 " FOREIGN KEY (upload_fk)"
691 " REFERENCES upload(upload_pk) ON DELETE CASCADE;"
692 " EXCEPTION WHEN duplicate_object THEN NULL;"
700 string contentFkName = string(tablename) +
"_fk";
702 "SELECT 1 FROM pg_constraint"
703 " WHERE conname = '%s' AND conrelid = '%s'::regclass",
704 contentFkName.c_str(), etablename);
705 RETURN_IF_FALSE(fkCheck);
709 " ALTER TABLE ONLY %s"
710 " ADD CONSTRAINT %s_fk"
711 " FOREIGN KEY (%s_fk)"
712 " REFERENCES %s(%s_pk) ON DELETE CASCADE;"
713 " EXCEPTION WHEN duplicate_object THEN NULL;"
715 etablename, tablename, tablename, tablename, tablename
722 "SELECT 1 FROM pg_attrdef ad"
723 " JOIN pg_attribute a ON a.attrelid = ad.adrelid AND a.attnum = ad.adnum"
724 " WHERE ad.adrelid = '%s'::regclass"
725 " AND a.attname = 'scope'"
726 " AND pg_get_expr(ad.adbin, ad.adrelid) = '1'",
728 RETURN_IF_FALSE(defaultCheck);
731 "ALTER TABLE %s ALTER COLUMN scope SET DEFAULT 1",
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.
void ignoreWarnings(bool) const
QueryResult queryPrintf(const char *queryFormat,...) const
Execute a query in printf format.
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.