예제 #1
0
        foreach ($this->schema['INHERITS'] as $table => $fromTable) {
            if (empty($table)) {
                continue;
            }
            if (!$this->dbman->existsTable($table) && $this->dbman->existsTable($fromTable)) {
                $sql = "CREATE TABLE \"{$table}\" () INHERITS (\"{$fromTable}\")";
                $this->applyOrEchoOnce($sql, $stmt = __METHOD__ . $table);
            }
        }
    }
}
if (empty($dbManager) || !$dbManager instanceof DbManager) {
    $logLevel = Logger::INFO;
    $logger = new Logger(__FILE__);
    $logger->pushHandler(new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, $logLevel));
    $dbManager = new ModernDbManager($logger);
    $dbManager->setDriver(new Postgres($PG_CONN));
}
/* simulate the old functions*/
$libschema = new fo_libschema($dbManager);
/**
 * @brief Make schema match $Filename.  This is a single transaction.
 * @param $Filename Schema file written by schema-export.php
 * @param $Debug Turn on debugging (echo sql as it is being executed)
 * @param $Catalog Optional database name
 * @return false=success, on error return string with error message.
 **/
function ApplySchema($Filename = NULL, $Debug = false, $Catalog = 'fossology')
{
    global $libschema;
    return $libschema->applySchema($Filename, $Debug, $Catalog);
 /**
  * \brief initialization
  */
 protected function setUp()
 {
     global $PG_CONN;
     global $upload_pk;
     global $pfile_pk_parent;
     global $pfile_pk_child;
     global $agent_pk;
     global $DB_COMMAND;
     global $DB_NAME;
     $DB_COMMAND = dirname(dirname(dirname(dirname(__FILE__)))) . "/testing/db/createTestDB.php";
     print "*** path to test db creation command: " . $DB_COMMAND;
     exec($DB_COMMAND, $dbout, $rc);
     if (!empty($rc)) {
         throw new Exception(implode("\n", $dbout));
     }
     preg_match("/(\\d+)/", $dbout[0], $matches);
     $test_name = $matches[1];
     $db_conf = $dbout[0];
     $DB_NAME = "fosstest" . $test_name;
     $PG_CONN = DBconnect($db_conf);
     $logger = new Monolog\Logger('default');
     $this->logFileName = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/db.sqlite.log';
     $logger->pushHandler(new Monolog\Handler\StreamHandler($this->logFileName, Monolog\Logger::ERROR));
     $this->dbManager = new ModernDbManager($logger);
     $this->dbManager->setDriver(new Fossology\Lib\Db\Driver\Postgres($PG_CONN));
     /** preparation, add uploadtree, upload, pfile, license_file record */
     $upload_filename = "license_file_test";
     /* upload file name */
     $this->dbManager->prepare($stmt = 'pfile.insert', $sql = "INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_size) VALUES (\$1,\$2,\$3)");
     $this->dbManager->freeResult($this->dbManager->execute($stmt, array('AF1DF2C4B32E4115DB5F272D9EFD0E674CF2A0BC', '2239AA7DAC291B6F8D0A56396B1B8530', '4560')));
     $this->dbManager->freeResult($this->dbManager->execute($stmt, array('B1938B14B9A573D59ABCBD3BF0F9200CE6E79FB6', '55EFE7F9B9D106047718F1CE9173B869', '1892')));
     /** add nomos agent record **/
     $this->dbManager->queryOnce($sql = "INSERT INTO agent (agent_name) VALUES('nomos')");
     /** add license_ref record */
     $this->dbManager->prepare($stmt = 'license_ref.insert', $sql = "INSERT INTO license_ref" . " (rf_pk, rf_shortname, rf_text, marydone, rf_active, rf_text_updatable, rf_detector_type)" . " VALUES (\$1,\$2,\$3,\$4,\$5,\$6,\$7)");
     $this->dbManager->freeResult($this->dbManager->execute($stmt, array(1, 'test_ref', 'test_ref', 'false', 'true', 'false', 1)));
     /** get pfile id */
     $this->dbManager->prepare($stmt = 'license_ref.select', $sql = "SELECT pfile_pk from pfile where pfile_sha1" . " IN ('AF1DF2C4B32E4115DB5F272D9EFD0E674CF2A0BC', 'B1938B14B9A573D59ABCBD3BF0F9200CE6E79FB6')");
     $result = $this->dbManager->execute($stmt);
     $row = $this->dbManager->fetchArray($result);
     $pfile_pk_parent = $row['pfile_pk'];
     $row = $this->dbManager->fetchArray($result);
     $pfile_pk_child = $row['pfile_pk'];
     $this->dbManager->freeResult($result);
     /** add a license_file record */
     $agent_nomos = $this->dbManager->getSingleRow("SELECT agent_pk from agent where agent_name = 'nomos'", array(), __METHOD__ . '.agent.select');
     $agent_pk = $agent_nomos['agent_pk'];
     $this->dbManager->prepare($stmt = 'license_file.insert', $sql = "INSERT INTO license_file(rf_fk, agent_fk, pfile_fk) VALUES (\$1,\$2,\$3)");
     $this->dbManager->freeResult($this->dbManager->execute($stmt, array(1, $agent_pk, $pfile_pk_parent)));
     $this->dbManager->freeResult($this->dbManager->execute($stmt, array(2, $agent_pk, $pfile_pk_child)));
     $this->dbManager->queryOnce("INSERT INTO upload (upload_filename,upload_mode,upload_ts, pfile_fk, uploadtree_tablename)" . " VALUES ('{$upload_filename}',40,now(), '{$pfile_pk_parent}', '{$this->uploadtree_tablename}')");
     $row = $this->dbManager->getSingleRow("SELECT upload_pk from upload where upload_filename = '{$upload_filename}'", array(), __METHOD__ . '.upload.select');
     $upload_pk = $row['upload_pk'];
     $this->dbManager->prepare($stmtIn = __METHOD__ . '.uploadtree.insert', "INSERT INTO uploadtree (parent, upload_fk, pfile_fk, ufile_mode, lft, rgt, ufile_name) VALUES (\$1,\$2,\$3,\$4,\$5,\$6,\$7)");
     $this->dbManager->freeResult($this->dbManager->execute($stmtIn, array(NULL, $upload_pk, $pfile_pk_parent, 33188, 1, 2, 'license_test.file.parent')));
     $this->dbManager->prepare($stmtOut = __METHOD__ . 'uploadtree.select', "SELECT uploadtree_pk from uploadtree where pfile_fk=\$1");
     $res = $this->dbManager->execute($stmtOut, array($pfile_pk_parent));
     $row = $this->dbManager->fetchArray($res);
     $this->dbManager->freeResult($res);
     $this->uploadtree_pk_parent = $row['uploadtree_pk'];
     /** add child uploadtree record */
     $this->dbManager->freeResult($this->dbManager->execute($stmtIn, array($this->uploadtree_pk_parent, $upload_pk, $pfile_pk_child, 33188, 1, 2, 'license_test.file.child')));
     $res = $this->dbManager->execute($stmtOut, array($pfile_pk_child));
     $row = $this->dbManager->fetchArray($res);
     $this->dbManager->freeResult($res);
     $this->uploadtree_pk_child = $row['uploadtree_pk'];
     $this->uploadtree_tablename = GetUploadtreeTableName($upload_pk);
     print '.';
 }