Skip to content

Commit

Permalink
Use Laravel query builder for DELETE statements when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
williamjallen committed Oct 4, 2023
1 parent f642d06 commit 7da9346
Show file tree
Hide file tree
Showing 36 changed files with 220 additions and 367 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function upgrade()
// We need to move the buildupdate build ids to the build2update table
$query = pdo_query('SELECT buildid FROM buildupdate');
while ($query_array = pdo_fetch_array($query)) {
pdo_query("INSERT INTO build2update (buildid,updateid) VALUES ('" . $query_array['buildid'] . "','" . $query_array['buildid'] . "')");
DB::insert("INSERT INTO build2update (buildid,updateid) VALUES ('" . $query_array['buildid'] . "','" . $query_array['buildid'] . "')");
}
RemoveTableIndex('buildupdate', 'buildid');
RenameTableField('buildupdate', 'buildid', 'id', 'int(11)', 'bigint', '0');
Expand Down Expand Up @@ -678,7 +678,7 @@ public function upgrade()
$buildgroup_array = pdo_fetch_array(pdo_query("SELECT id FROM buildgroup WHERE name='$buildtype' AND projectid='$projectid'"));

$groupid = $buildgroup_array['id'];
pdo_query("INSERT INTO build2group(buildid,groupid) VALUES ('$buildid','$groupid')");
DB::insert("INSERT INTO build2group(buildid,groupid) VALUES ('$buildid','$groupid')");
}

$xml .= add_XML_value('alert', 'Builds have been added to default groups successfully.');
Expand Down
5 changes: 2 additions & 3 deletions app/Http/Controllers/ManageProjectRolesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,8 @@ public function viewPage(): View|RedirectResponse

// Remove the user
if ($removeuser) {
$db->executePrepared('DELETE FROM user2project WHERE userid=? AND projectid=?', [$userid. $projectid]);
$db->executePrepared('DELETE FROM user2repository WHERE userid=? AND projectid=?', [$userid. $projectid]);
echo pdo_error();
DB::delete('DELETE FROM user2project WHERE userid=? AND projectid=?', [$userid, $projectid]);
DB::delete('DELETE FROM user2repository WHERE userid=? AND projectid=?', [$userid, $projectid]);
}

// Update the user
Expand Down
9 changes: 3 additions & 6 deletions app/Http/Controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public function editSite(): View|RedirectResponse
$db = Database::getInstance();

if (isset($_POST['unclaimsite']) && isset($_GET['siteid'])) {
$db->executePrepared('
DB::delete('
DELETE FROM site2user
WHERE siteid=? AND userid=?
', [intval($_GET['siteid']), $userid]);
Expand Down Expand Up @@ -660,8 +660,7 @@ private static function add_site2user(int $siteid, int $userid): void
$db = Database::getInstance();
$site2user = $db->executePrepared('SELECT * FROM site2user WHERE siteid=? AND userid=?', [intval($siteid), intval($userid)]);
if (!empty($site2user)) {
$db->executePrepared('INSERT INTO site2user (siteid, userid) VALUES (?, ?)', [$siteid, $userid]);
add_last_sql_error('add_site2user');
DB::insert('INSERT INTO site2user (siteid, userid) VALUES (?, ?)', [$siteid, $userid]);
}
}

Expand All @@ -670,9 +669,7 @@ private static function add_site2user(int $siteid, int $userid): void
*/
private static function remove_site2user(int $siteid, int $userid): void
{
$db = Database::getInstance();
$db->executePrepared('DELETE FROM site2user WHERE siteid=? AND userid=?', [$siteid, $userid]);
add_last_sql_error('remove_site2user');
DB::delete('DELETE FROM site2user WHERE siteid=? AND userid=?', [$siteid, $userid]);
}

/**
Expand Down
63 changes: 32 additions & 31 deletions app/Http/Controllers/SubscribeProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use CDash\Model\UserProject;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\View\View;

final class SubscribeProjectController extends AbstractProjectController
Expand Down Expand Up @@ -92,11 +93,11 @@ public function subscribeProject(): View|RedirectResponse
$LabelEmail->UserId = $user->id;

if ($Unsubscribe) {
$db->executePrepared('DELETE FROM user2project WHERE userid=? AND projectid=?', [$user->id, $this->project->Id]);
$db->executePrepared('DELETE FROM user2repository WHERE userid=? AND projectid=?', [$user->id, $this->project->Id]);
DB::delete('DELETE FROM user2project WHERE userid=? AND projectid=?', [$user->id, $this->project->Id]);
DB::delete('DELETE FROM user2repository WHERE userid=? AND projectid=?', [$user->id, $this->project->Id]);

// Remove the claim sites for this project if they are only part of this project
$db->executePrepared('
DB::delete('
DELETE FROM site2user
WHERE
userid=?
Expand Down Expand Up @@ -150,20 +151,20 @@ public function subscribeProject(): View|RedirectResponse

if ($Role == 0) {
// Remove the claim sites for this project if they are only part of this project
$db->executePrepared('
DELETE FROM site2user
WHERE
userid=?
AND siteid NOT IN (
SELECT build.siteid
FROM build, user2project AS up
WHERE
up.projectid=build.projectid
AND up.userid=?
AND up.role>0
GROUP BY build.siteid
)
', [$user->id, $user->id]);
DB::delete('
DELETE FROM site2user
WHERE
userid=?
AND siteid NOT IN (
SELECT build.siteid
FROM build, user2project AS up
WHERE
up.projectid=build.projectid
AND up.userid=?
AND up.role>0
GROUP BY build.siteid
)
', [$user->id, $user->id]);
}
}

Expand Down Expand Up @@ -213,20 +214,20 @@ public function subscribeProject(): View|RedirectResponse

if ($Role == 0) {
// Remove the claim sites for this project if they are only part of this project
$db->executePrepared('
DELETE FROM site2user
WHERE
userid=?
AND siteid NOT IN (
SELECT build.siteid
FROM build, user2project AS up
WHERE
up.projectid0=build.projectid
AND up.userid=?
AND up.role>0
GROUP BY build.siteid
)
', [$user->id, $user->id]);
DB::delete('
DELETE FROM site2user
WHERE
userid=?
AND siteid NOT IN (
SELECT build.siteid
FROM build, user2project AS up
WHERE
up.projectid0=build.projectid
AND up.userid=?
AND up.role>0
GROUP BY build.siteid
)
', [$user->id, $user->id]);
}
} else {
$db->executePrepared('
Expand Down
10 changes: 3 additions & 7 deletions app/cdash/app/Model/BuildConfigure.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
=========================================================================*/
namespace CDash\Model;

use Illuminate\Support\Facades\DB;
use PDO;
use CDash\Database;

Expand Down Expand Up @@ -147,18 +148,13 @@ public function Delete()
pdo_execute($stmt, [$this->Id]);
$row = $stmt->fetch();
if ($row['c'] < 2) {
$stmt = $this->PDO->prepare('DELETE FROM configure WHERE id = ?');
pdo_execute($stmt, [$this->Id]);
DB::delete('DELETE FROM configure WHERE id = ?', [$this->Id]);
$retval = true;
}

if ($this->BuildId) {
// Delete the build2configure row for this build.
$stmt = $this->PDO->prepare(
'DELETE FROM build2configure WHERE buildid = ?');
if (!pdo_execute($stmt, [$this->BuildId])) {
return false;
}
DB::delete('DELETE FROM build2configure WHERE buildid = ?', [$this->BuildId]);
}

return $retval;
Expand Down
6 changes: 3 additions & 3 deletions app/cdash/app/Model/BuildGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,10 +486,10 @@ public function Delete(): bool
}

// We delete all the build2grouprule associated with the group
$this->PDO->executePrepared('DELETE FROM build2grouprule WHERE groupid=?', [$this->Id]);
DB::delete('DELETE FROM build2grouprule WHERE groupid=?', [$this->Id]);

// We delete the buildgroup
$this->PDO->executePrepared('DELETE FROM buildgroup WHERE id=?', [$this->Id]);
DB::delete('DELETE FROM buildgroup WHERE id=?', [$this->Id]);

// Restore the builds that were associated with this group
$oldbuilds = $this->PDO->executePrepared('
Expand Down Expand Up @@ -532,7 +532,7 @@ public function Delete(): bool

// Delete the buildgroupposition and update the position
// of the other groups.
$this->PDO->executePrepared('DELETE FROM buildgroupposition WHERE buildgroupid=?', [$this->Id]);
DB::delete('DELETE FROM buildgroupposition WHERE buildgroupid=?', [$this->Id]);
$buildgroupposition = $this->PDO->executePrepared('
SELECT bg.buildgroupid
FROM buildgroupposition AS bg, buildgroup AS g
Expand Down
27 changes: 12 additions & 15 deletions app/cdash/app/Model/BuildGroupRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace CDash\Model;

use CDash\Database;
use Illuminate\Support\Facades\DB;

class BuildGroupRule
{
Expand Down Expand Up @@ -220,7 +221,7 @@ private function HardDelete()
}

/** Soft delete all active previous versions of this rule. */
public function SoftDeleteExpiredRules($now)
public function SoftDeleteExpiredRules($now): void
{
$stmt = $this->PDO->prepare(
"UPDATE build2grouprule
Expand All @@ -240,7 +241,7 @@ public function SoftDeleteExpiredRules($now)
}

/** Change the group that this rule points to. */
public function ChangeGroup($newgroupid)
public function ChangeGroup($newgroupid): void
{
$stmt = $this->PDO->prepare(
"UPDATE build2grouprule SET groupid = :newgroupid
Expand Down Expand Up @@ -270,24 +271,20 @@ public function ChangeGroup($newgroupid)
$this->PDO->execute($stmt, $query_params);
}

public static function DeleteExpiredRulesForProject($projectid, $cutoff_date)
public static function DeleteExpiredRulesForProject($projectid, $cutoff_date): void
{
$db = Database::getInstance();
$stmt = $db->prepare(
"DELETE FROM build2grouprule
WHERE groupid IN
(SELECT id FROM buildgroup WHERE projectid = :projectid)
DB::delete("
DELETE FROM build2grouprule
WHERE groupid IN (
SELECT id FROM buildgroup WHERE projectid = ?
)
AND endtime != '1980-01-01 00:00:00'
AND endtime < :endtime");
$query_params = [
':projectid' => $projectid,
':endtime' => $cutoff_date,
];
$db->execute($stmt, $query_params);
AND endtime < ?
", [$projectid, $cutoff_date]);
}

// Populate this object with a row from the database and a projectid.
public function FillFromRow($row, $projectid)
public function FillFromRow($row, $projectid): void
{
$this->BuildName = $row['buildname'];
$this->BuildType = $row['buildtype'];
Expand Down
13 changes: 8 additions & 5 deletions app/cdash/app/Model/CoverageFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace CDash\Model;

use CDash\Database;
use Illuminate\Support\Facades\DB;
use PDO;

/** This class shouldn't be used externally */
Expand Down Expand Up @@ -110,11 +111,13 @@ public function Update($buildid)
}

// Remove the file if the crc32 is NULL
$stmt = $this->PDO->prepare(
'DELETE FROM coveragefile
WHERE id=:prevfileid AND file IS NULL AND crc32 IS NULL');
$stmt->bindParam(':prevfileid', $prevfileid);
pdo_execute($stmt);
DB::delete('
DELETE FROM coveragefile
WHERE
id = ?
AND file IS NULL
AND crc32 IS NULL
', [$prevfileid]);
}
} else {
// The file doesn't exist in the database
Expand Down
36 changes: 12 additions & 24 deletions app/cdash/app/Model/CoverageFile2User.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
namespace CDash\Model;

use CDash\Database;
use Illuminate\Support\Facades\DB;

/** Coverage file to users */
class CoverageFile2User
Expand Down Expand Up @@ -108,45 +109,32 @@ public function Insert(): bool
} // function Insert

/** Remove authors */
public function RemoveAuthors(): bool
public function RemoveAuthors(): void
{
if ($this->FullPath == '' || $this->ProjectId < 1) {
abort(500, 'CoverageFile2User:RemoveAuthors: FullPath or ProjectId not set');
}

$db = Database::getInstance();
$query_result = $db->executePrepared('DELETE FROM coveragefile2user WHERE fileid=?', [$this->GetId()]);
if (!$query_result) {
add_last_sql_error('CoverageFile2User:RemoveAuthors');
return false;
}
return true;
DB::delete('DELETE FROM coveragefile2user WHERE fileid = ?', [$this->GetId()]);
}

/** Remove the new user */
public function Remove(): bool
public function Remove(): void
{
if (!isset($this->UserId) || $this->UserId < 1) {
return false;
abort(500, 'Invalid UserId');
}
if (!isset($this->FileId) || $this->FileId < 1) {
return false;
abort(500, 'Invalid FileId');
}

$db = Database::getInstance();
$query_result = $db->executePrepared('
DELETE FROM coveragefile2user
WHERE
userid=?
AND fileid=?
', [$this->UserId, $this->FileId]);
if ($query_result === false) {
add_last_sql_error('CoverageFile2User:Remove');
return false;
}
DB::delete('
DELETE FROM coveragefile2user
WHERE
userid=?
AND fileid=?
', [$this->UserId, $this->FileId]);

$this->FixPosition();
return true;
}

/** Fix the position given a file */
Expand Down
Loading

0 comments on commit 7da9346

Please sign in to comment.