function Save() { if (!$this->Exists()) { $id = ""; $idvalue = ""; if ($this->Id) { $id = "id,"; $idvalue = qnum($this->Id) . ","; } if (strlen($this->Type) == 0) { $this->Type = extract_type_from_buildstamp($this->Stamp); } $this->Name = pdo_real_escape_string($this->Name); $this->Stamp = pdo_real_escape_string($this->Stamp); $this->Type = pdo_real_escape_string($this->Type); $this->Generator = pdo_real_escape_string($this->Generator); $this->StartTime = pdo_real_escape_string($this->StartTime); $this->EndTime = pdo_real_escape_string($this->EndTime); $this->SubmitTime = pdo_real_escape_string($this->SubmitTime); $this->Command = pdo_real_escape_string($this->Command); $this->Log = pdo_real_escape_string($this->Log); // Compute the number of errors and warnings (this speeds up the display of the main table) if ($this->InsertErrors) { $nbuilderrors = 0; $nbuildwarnings = 0; foreach ($this->Errors as $error) { if ($error->Type == 0) { $nbuilderrors++; } else { $nbuildwarnings++; } } } else { $nbuilderrors = -1; $nbuildwarnings = -1; } $parentId = 0; $justCreatedParent = false; if ($this->SubProjectName) { $parentId = $this->GetParentBuildId(); if ($parentId == 0) { // This is the first subproject to submit for a new build. // Create a new parent build for it. $parentId = $this->CreateParentBuild($nbuilderrors, $nbuildwarnings); $justCreatedParent = true; } } $this->ParentId = $parentId; $query = "INSERT INTO build (" . $id . "siteid,projectid,stamp,name,type,generator,starttime,endtime,submittime,command,log,builderrors,buildwarnings,parentid)\n VALUES (" . $idvalue . "'{$this->SiteId}','{$this->ProjectId}','{$this->Stamp}','{$this->Name}',\n '{$this->Type}','{$this->Generator}','{$this->StartTime}',\n '{$this->EndTime}','{$this->SubmitTime}','{$this->Command}','{$this->Log}',{$nbuilderrors},{$nbuildwarnings}, {$this->ParentId})"; if (!pdo_query($query)) { add_last_sql_error("Build Insert", $this->ProjectId, $this->Id); return false; } if (!$this->Id) { $this->Id = pdo_insert_id("build"); } // Add the groupid if ($this->GroupId) { $query = "INSERT INTO build2group (groupid,buildid) VALUES ('{$this->GroupId}','{$this->Id}')"; if (!pdo_query($query)) { add_last_sql_error("Build Insert", $this->ProjectId, $this->Id); return false; } // Associate the parent with this group too. if ($this->ParentId > 0) { $result = pdo_query("SELECT groupid FROM build2group WHERE buildid=" . qnum($this->ParentId)); if (pdo_num_rows($result) == 0) { $query = "INSERT INTO build2group (groupid,buildid)\n VALUES ('{$this->GroupId}','{$this->ParentId}')"; if (!pdo_query($query)) { add_last_sql_error("Build Insert", $this->ProjectId, $this->ParentId); return false; } } } } // Add the subproject2build relationship: if ($this->SubProjectId) { $query = "INSERT INTO subproject2build (subprojectid,buildid) VALUES ('{$this->SubProjectId}','{$this->Id}')"; if (!pdo_query($query)) { add_last_sql_error("Build Insert", $this->ProjectId, $this->Id); return false; } } // Add errors/warnings foreach ($this->Errors as $error) { $error->BuildId = $this->Id; $error->Insert(); } // Add ErrorDiff foreach ($this->ErrorDiffs as $diff) { $diff->BuildId = $this->Id; $diff->Insert(); } // Save the information if (!empty($this->Information)) { $this->Information->BuildId = $this->Id; $this->Information->Save(); } // Update parent's tally of total build errors & warnings. if (!$justCreatedParent) { $this->UpdateParentBuild($nbuilderrors, $nbuildwarnings); } } else { if ($this->Append) { $this->EndTime = pdo_real_escape_string($this->EndTime); $this->SubmitTime = pdo_real_escape_string($this->SubmitTime); $this->Command = pdo_real_escape_string(' ' . $this->Command); $this->Log = pdo_real_escape_string(' ' . $this->Log); // Compute the number of errors and warnings (this speeds up the display of the main table) if ($this->InsertErrors) { $nbuilderrors = $this->GetNumberOfErrors(); $nbuildwarnings = $this->GetNumberOfWarnings(); foreach ($this->Errors as $error) { if ($error->Type == 0) { $nbuilderrors++; } else { $nbuildwarnings++; } } } else { $nbuilderrors = -1; $nbuildwarnings = -1; } if ($this->SubProjectName) { $newErrors = 0; $newWarnings = 0; if ($nbuilderrors > 0 || $nbuildwarnings > 0) { // If we are adding errors or warnings to this build we need to know // how many builderrors & buildwarnings it had previously so we can // update the parent's tally properly. $priorResult = pdo_single_row_query("SELECT builderrors, buildwarnings FROM build\n WHERE id=" . qnum($this->Id)); if ($priorResult['builderrors'] == -1) { $priorResult['builderrors'] = 0; } if ($priorResult['buildwarnings'] == -1) { $priorResult['buildwarnings'] = 0; } $newErrors = $nbuilderrors - $priorResult['builderrors']; $newWarnings = $nbuildwarnings - $priorResult['buildwarnings']; } $this->ParentId = $this->GetParentBuildId(); $this->UpdateParentBuild($newErrors, $newWarnings); } include 'cdash/config.php'; if ($CDASH_DB_TYPE == 'pgsql') { $query = "UPDATE build SET\n endtime='{$this->EndTime}',submittime='{$this->SubmitTime}',\n builderrors='{$nbuilderrors}',buildwarnings='{$nbuildwarnings}'," . "command=command || '{$this->Command}',\n log=log || '{$this->Log}'" . "WHERE id=" . qnum($this->Id); } else { $query = "UPDATE build SET\n endtime='{$this->EndTime}',submittime='{$this->SubmitTime}',\n builderrors='{$nbuilderrors}',buildwarnings='{$nbuildwarnings}'," . "command=CONCAT(command, '{$this->Command}'),\n log=CONCAT(log, '{$this->Log}')" . "WHERE id=" . qnum($this->Id); } if (!pdo_query($query)) { add_last_sql_error("Build Insert (Append)", $this->ProjectId, $this->Id); return false; } // Add errors/warnings foreach ($this->Errors as $error) { $error->BuildId = $this->Id; $error->Insert(); } // Add ErrorDiff foreach ($this->ErrorDiffs as $diff) { $diff->BuildId = $this->Id; $diff->Insert(); } } else { //echo "info: nothing<br/>"; } } // Add label associations regardless of how Build::Save gets called: // $this->InsertLabelAssociations(); return true; }
public function Save() { $this->StartTime = pdo_real_escape_string($this->StartTime); $this->EndTime = pdo_real_escape_string($this->EndTime); $this->SubmitTime = pdo_real_escape_string($this->SubmitTime); $this->Command = pdo_real_escape_string(trim($this->Command)); $this->Log = pdo_real_escape_string(trim($this->Log)); // Compute the number of errors and warnings. // This speeds up the display of the main table. $nbuilderrors = -1; $nbuildwarnings = -1; if ($this->InsertErrors) { $nbuilderrors = 0; $nbuildwarnings = 0; foreach ($this->Errors as $error) { if ($error->Type == 0) { $nbuilderrors++; } else { $nbuildwarnings++; } } } if (!$this->Exists()) { $id = ''; $idvalue = ''; if ($this->Id) { $id = 'id,'; $idvalue = qnum($this->Id) . ','; } if (strlen($this->Type) == 0) { $this->Type = extract_type_from_buildstamp($this->Stamp); } $this->Name = pdo_real_escape_string($this->Name); $this->Stamp = pdo_real_escape_string($this->Stamp); $this->Type = pdo_real_escape_string($this->Type); $this->Generator = pdo_real_escape_string($this->Generator); $this->SetParentId(0); $justCreatedParent = false; if ($this->SubProjectName) { $this->SetParentId($this->LookupParentBuildId()); if ($this->ParentId == 0) { // This is the first subproject to submit for a new build. // Create a new parent build for it. $justCreatedParent = $this->CreateParentBuild($nbuilderrors, $nbuildwarnings); } } $this->Uuid = Build::GenerateUuid($this->Stamp, $this->Name, $this->SiteId, $this->ProjectId, $this->SubProjectName); $query = 'INSERT INTO build (' . $id . 'siteid, projectid, stamp, name, type, generator, starttime, endtime, submittime, command, log, builderrors, buildwarnings, parentid, uuid, changeid) VALUES (' . $idvalue . "'{$this->SiteId}', '{$this->ProjectId}',\n '{$this->Stamp}', '{$this->Name}', '{$this->Type}',\n '{$this->Generator}', '{$this->StartTime}', '{$this->EndTime}',\n '{$this->SubmitTime}', '{$this->Command}', '{$this->Log}',\n {$nbuilderrors}, {$nbuildwarnings}, {$this->ParentId},\n '{$this->Uuid}', '{$this->PullRequest}')"; if (!pdo_query($query)) { $error = pdo_error(null, false); // This error might be due to a unique constraint violation // for this UUID. Query for such a previously existing build. $existing_id_result = pdo_single_row_query("SELECT id FROM build WHERE uuid = '{$this->Uuid}'"); if ($existing_id_result && array_key_exists('id', $existing_id_result)) { $this->Id = $existing_id_result['id']; // If a previously existing build with this UUID was found // call UpdateBuild() on it. This also sets ParentId // if an existing parent was found. $this->UpdateBuild($this->Id, $nbuilderrors, $nbuildwarnings); // Does the parent still need to be created? if ($this->SubProjectName && $this->ParentId < 1) { if (!$this->CreateParentBuild($nbuilderrors, $nbuildwarnings)) { // Someone else created the parent after we called // UpdateBuild(this->Id,...). // In this case we also need to manually update // the parent as well. $this->UpdateBuild($this->ParentId, $nbuilderrors, $nbuildwarnings); } } // Now that the existing build and its parent (if any) have // been updated we can return early. return true; } add_log("SQL error: {$error}", 'Build Insert', LOG_ERR, $this->ProjectId, $this->Id); return false; } if (!$this->Id) { $this->Id = pdo_insert_id('build'); } // Add the groupid if ($this->GroupId) { $query = "INSERT INTO build2group (groupid,buildid) VALUES ('{$this->GroupId}','{$this->Id}')"; if (!pdo_query($query)) { add_last_sql_error('Build2Group Insert', $this->ProjectId, $this->Id); } // Associate the parent with this group too. if ($this->ParentId > 0) { $result = pdo_query('SELECT groupid FROM build2group WHERE buildid=' . qnum($this->ParentId)); if (pdo_num_rows($result) == 0) { global $CDASH_DB_TYPE; $duplicate_sql = ''; if ($CDASH_DB_TYPE !== 'pgsql') { $duplicate_sql = 'ON DUPLICATE KEY UPDATE groupid=groupid'; } $query = "INSERT INTO build2group (groupid,buildid)\n VALUES ('{$this->GroupId}','{$this->ParentId}')\n {$duplicate_sql}"; if (!pdo_query($query)) { add_last_sql_error('Parent Build2Group Insert', $this->ProjectId, $this->ParentId); } } } } // Add the subproject2build relationship: if ($this->SubProjectId) { $query = "INSERT INTO subproject2build (subprojectid,buildid) VALUES ('{$this->SubProjectId}','{$this->Id}')"; if (!pdo_query($query)) { add_last_sql_error('SubProject2Build Insert', $this->ProjectId, $this->Id); } } // Save the information if (!empty($this->Information)) { $this->Information->BuildId = $this->Id; $this->Information->Save(); } // Update parent's tally of total build errors & warnings. if (!$justCreatedParent) { $this->UpdateBuild($this->ParentId, $nbuilderrors, $nbuildwarnings); } elseif ($this->ParentId > 0) { // If we just created a child build, associate it with // the parent's updates (if any). require_once 'models/buildupdate.php'; BuildUpdate::AssignUpdateToChild($this->Id, $this->ParentId); } } else { // Build already exists. // Update this build and its parent (if necessary). $this->UpdateBuild($this->Id, $nbuilderrors, $nbuildwarnings); } // Add errors/warnings foreach ($this->Errors as $error) { $error->BuildId = $this->Id; $error->Insert(); } // Add ErrorDiff foreach ($this->ErrorDiffs as $diff) { $diff->BuildId = $this->Id; $diff->Insert(); } // Add label associations regardless of how Build::Save gets called: // $this->InsertLabelAssociations(); // Should we post build errors to a pull request? if (isset($this->PullRequest)) { $hasErrors = false; foreach ($this->Errors as $error) { if ($error->Type == 0) { $hasErrors = true; break; } } if ($hasErrors) { $message = 'This build experienced errors'; $url = get_server_URI(false) . "/viewBuildError.php?buildid={$this->Id}"; $this->NotifyPullRequest($message, $url); } } return true; }