/********************************************************* DBFindMime(): Find a mime type in the DBMime table. Returns mimetype ID or -1 if not found. *********************************************************/ int DBFindMime (char *Mimetype) { int i; if (!Mimetype || (Mimetype[0]=='\0')) return(-1); if (!DBMime) DBLoadMime(); for(i=0; i < MaxDBMime; i++) { if (!strcmp(Mimetype,DBgetvalue(DBMime,i,1))) { return(atoi(DBgetvalue(DBMime,i,0))); /* return mime type */ } } /* If it got here, then the mimetype is unknown. Add it! */ memset(SQL,'\0',sizeof(SQL)); snprintf(SQL,sizeof(SQL)-1,"INSERT INTO mimetype (mimetype_name) VALUES ('%s');",TaintString(Mimetype)); /* The insert will fail if it already exists. This is good. It will prevent multiple mimetype agents from inserting the same data at the same type. */ DBaccess(DB,SQL); /* Now reload the mimetype table */ DBLoadMime(); /* And re-process the request... */ return(DBFindMime(Mimetype)); } /* DBFindMime() */
/** * \brief for function DBLoadMime */ void testDBLoadMime() { char SQL[MAXCMD] = {0}; PGresult *result = NULL; char mimetype_name[] = "application/octet-stream"; /** delete the record mimetype_name is application/octet-stream in mimetype */ memset(SQL, '\0', MAXCMD); snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = '%s';", mimetype_name); result = PQexec(pgConn, SQL); if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__)) { PQfinish(pgConn); exit(-1); } PQclear(result); memset(SQL, '\0', MAXCMD); snprintf(SQL, MAXCMD, "INSERT INTO mimetype (mimetype_name) VALUES ('%s');", mimetype_name); result = PQexec(pgConn, SQL); if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__)) { PQfinish(pgConn); exit(-1); } PQclear(result); MaxDBMime = 0; /** exectue the tested function */ DBLoadMime(); /** select the record mimetype_name is application/octet-stream */ memset(SQL, '\0', MAXCMD); snprintf(SQL, MAXCMD, "SELECT mimetype_name from mimetype where mimetype_name = ('%s');", mimetype_name); result = PQexec(pgConn, SQL); if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__)) { PQfinish(pgConn); exit(-1); } int count = PQntuples(result); PQclear(result); CU_ASSERT_EQUAL(MaxDBMime, count); /** delete the record mimetype_name is application/octet-stream in mimetype */ memset(SQL, '\0', MAXCMD); snprintf(SQL, MAXCMD, "DELETE FROM mimetype where mimetype_name = '%s';", mimetype_name); result = PQexec(pgConn, SQL); /** reset the evn, that is clear all data in mimetype */ if (fo_checkPQcommand(pgConn, result, SQL, __FILE__, __LINE__)) { PQfinish(pgConn); exit(-1); } MaxDBMime = 0; PQclear(result); }