Skip to content

Commit

Permalink
Dbl_Result: Gain fetch_ and close methods.
Browse files Browse the repository at this point in the history
And return a Dbl_Result instead of null or false.
And replace edb_row() with $result->fetch_row().
And replace edb_orow() with $result->fetch_object().
And replace edb_nrows() with $result->num_rows.
And add Dbl::is_error().
This wipes out a lot of typechecking errors.
  • Loading branch information
kohler committed May 6, 2020
1 parent cc212a8 commit f126941
Show file tree
Hide file tree
Showing 34 changed files with 150 additions and 100 deletions.
4 changes: 2 additions & 2 deletions assign.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,8 @@ function pcAssignments($qreq) {
where reviewType>=" . REVIEW_PC . " and timeSubmitted>=0
group by contactId) A using (contactId)
where ContactInfo.roles!=0 and (ContactInfo.roles&" . Contact::ROLE_PC . ")!=0");
$pcx = array();
while (($row = edb_orow($result))) {
$pcx = [];
while (($row = $result->fetch_object())) {
$pcx[$row->contactId] = $row;
}

Expand Down
2 changes: 1 addition & 1 deletion batch/fixdelegation.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function fix_one_delegation() {

$result = Dbl::qe("select l.* from ActionLog l where paperId=? order by logId asc", $pid);
$proposals = $confirmations = [];
while (($row = edb_orow($result))) {
while (($row = $result->fetch_object())) {
if ($row->contactId == $req_cid
&& preg_match('/\ALogged proposal for (\S+) to review/', $row->action, $m)
&& ($xid = $Conf->user_id_by_email($m[1]))) {
Expand Down
2 changes: 1 addition & 1 deletion batch/s3transfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

$result = $Conf->qe_raw("select paperStorageId, sha1 from PaperStorage where paperStorageId>1");
$sids = array();
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
if (!$match || $match->test_hash(Filer::hash_as_text($row[1])))
$sids[] = (int) $row[0];
}
Expand Down
4 changes: 2 additions & 2 deletions batch/updatecontactdb.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
$max_submitted = 0;
$pids = [];
$qv = [];
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
$qv[] = [$confid, $row[0], $row[1]];
$pids[] = $row[0];
$max_submitted = max($max_submitted, (int) $row[2]);
Expand All @@ -113,7 +113,7 @@

if ($collaborators) {
$result = Dbl::ql($Conf->dblink, "select email, collaborators, updateTime, lastLogin from ContactInfo where collaborators is not null and collaborators!=''");
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
$time = (int) $row[2] ? : (int) $row[3];
if ($time > 0) {
Dbl::ql($cdb, "update ContactInfo set collaborators=?, updateTime=? where email=? and (collaborators is null or collaborators='' or updateTime<?)", $row[1], $time, $row[0], $time);
Expand Down
51 changes: 41 additions & 10 deletions lib/dbl.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,36 @@
// Copyright (c) 2006-2020 Eddie Kohler; see LICENSE.

class Dbl_Result {
public $num_rows = 0;
public $affected_rows;
public $insert_id;
public $warning_count;

function __construct(mysqli $dblink) {
$this->affected_rows = $dblink->affected_rows;
$this->insert_id = $dblink->insert_id;
$this->warning_count = $dblink->warning_count;
public $errno;
public $query_string;

static function make(mysqli $dblink, $qstr = null) {
$r = new Dbl_Result;
$r->affected_rows = $dblink->affected_rows;
$r->insert_id = $dblink->insert_id;
$r->warning_count = $dblink->warning_count;
$r->errno = $dblink->errno;
$r->query_string = $qstr;
return $r;
}
static function make_empty() {
$r = new Dbl_Result;
$r->affected_rows = $r->warning_count = 0;
$r->insert_id = null;
$r->errno = 1002;
return $r;
}
function fetch_row() {
return null;
}
function fetch_object() {
return null;
}
function close() {
}
}

Expand Down Expand Up @@ -349,6 +371,7 @@ static function format_query_apply(/* [$dblink,] $qstr, [$argv] */) {
return self::format_query_args($dblink, $qstr, $argv);
}

/** @return mysqli_result|bool|null */
static private function call_query($dblink, $flags, $qfunc, $qstr) {
if ($flags & self::F_ECHO) {
error_log($qstr);
Expand All @@ -367,27 +390,27 @@ static private function call_query($dblink, $flags, $qfunc, $qstr) {
return $result;
}

/** @return mysqli_result|Dbl_Result */
static private function do_query_with($dblink, $qstr, $argv, $flags) {
if (!($flags & self::F_RAW)) {
$qstr = self::format_query_args($dblink, $qstr, $argv);
}
if (!$qstr) {
error_log(self::landmark() . ": empty query");
return false;
}
return self::do_result($dblink, $flags, $qstr, self::call_query($dblink, $flags, "query", $qstr));
}

/** @return mysqli_result|Dbl_Result */
static private function do_query($args, $flags) {
list($dblink, $qstr, $argv) = self::query_args($args, $flags, true);
return self::do_query_with($dblink, $qstr, $argv, $flags);
}

/** @return mysqli_result|Dbl_Result */
static function do_query_on($dblink, $args, $flags) {
list($ignored_dblink, $qstr, $argv) = self::query_args($args, $flags, true);
return self::do_query_with($dblink, $qstr, $argv, $flags);
}

/** @return mysqli_result|Dbl_Result */
static public function do_result($dblink, $flags, $qstr, $result) {
if ($result === false && $dblink->errno) {
if (!($flags & self::F_ALLOWERROR)) {
Expand All @@ -398,8 +421,11 @@ static public function do_result($dblink, $flags, $qstr, $result) {
} else if ($flags & self::F_LOG) {
error_log(self::landmark() . ": database error: " . $dblink->error . " in $qstr");
}
$result = Dbl_Result::make($dblink, $qstr);
} else if ($result === false || $result === true) {
$result = new Dbl_Result($dblink);
$result = Dbl_Result::make($dblink);
} else if ($result === null) {
$result = Dbl_Result::make_empty($dblink);
}
if (self::$check_warnings
&& !($flags & self::F_ALLOWERROR)
Expand Down Expand Up @@ -547,6 +573,11 @@ static function free($result) {
}
}

static function is_error($result) {
return !$result
|| ($result instanceof Dbl_Result && $result->errno);
}

// array of all first columns
static private function do_make_result($args, $flags = self::F_ERROR) {
if (count($args) == 1 && !is_string($args[0])) {
Expand Down
2 changes: 1 addition & 1 deletion log.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
$where = array();
if (count($ids)) {
$result = $Conf->qe("select contactId, email from ContactInfo where contactId?a union select contactId, email from DeletedContactInfo where contactId?a", $ids, $ids);
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
$where[] = "contactId=$row[0]";
$where[] = "destContactId=$row[0]";
$where[] = "action like " . Dbl::utf8ci("'% " . sqlq_for_like($row[1]) . "%'");
Expand Down
8 changes: 4 additions & 4 deletions mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
&& ctype_digit($Qreq->fromlog)
&& $Me->privChair) {
$result = $Conf->qe_raw("select * from MailLog where mailId=" . $Qreq->fromlog);
if (($row = edb_orow($result))) {
if (($row = $result->fetch_object())) {
foreach (["recipients", "q", "t", "cc", "replyto", "subject", "emailBody"] as $field) {
if (isset($row->$field) && !isset($Qreq[$field]))
$Qreq[$field] = $row->$field;
Expand Down Expand Up @@ -482,7 +482,7 @@ private function run() {
$fake_prep->fake = true;
$last_prep = $fake_prep;
$nrows_done = 0;
$nrows_total = edb_nrows($result);
$nrows_total = $result->num_rows;
$nwarnings = 0;
$preperrors = array();
$revinform = ($this->recipients == "newpcrev" ? array() : null);
Expand Down Expand Up @@ -722,11 +722,11 @@ private function run() {

if ($Me->privChair) {
$result = $Conf->qe_raw("select mailId, subject, emailBody from MailLog where fromNonChair=0 and status>=0 order by mailId desc limit 200");
if (edb_nrows($result)) {
if ($result->num_rows) {
echo '<div style="padding-top:12px;max-height:24em;overflow-y:auto">',
"<strong>Recent mails:</strong>\n";
$i = 1;
while (($row = edb_orow($result))) {
while (($row = $result->fetch_object())) {
echo '<div class="mhdd"><div style="position:relative;overflow:hidden">',
'<div style="position:absolute;white-space:nowrap"><span style="min-width:2em;text-align:right;display:inline-block" class="dim">', $i, '.</span> <a class="q" href="', hoturl("mail", "fromlog=" . $row->mailId), '">', htmlspecialchars($row->subject), ' &ndash; <span class="dim">', htmlspecialchars(UnicodeHelper::utf8_prefix($row->emailBody, 100)), "</span></a></div>",
"<br></div></div>\n";
Expand Down
4 changes: 2 additions & 2 deletions manualassign.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ function saveAssignments($qreq, $reviewer) {
left join PaperReview on (PaperReview.contactId=ContactInfo.contactId and PaperReview.reviewType>=" . REVIEW_SECONDARY . ")
where roles!=0 and (roles&" . Contact::ROLE_PC . ")!=0
group by ContactInfo.contactId");
$rev_count = array();
while (($row = edb_row($result))) {
$rev_count = [];
while (($row = $result->fetch_row())) {
$rev_count[$row[0]] = $row[1];
}

Expand Down
2 changes: 1 addition & 1 deletion reviewprefs.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ function parseUploadedPreferences($text, $filename, $apply) {

$prefcount = [];
$result = $Conf->qe_raw("select contactId, count(*) from PaperReviewPreference where preference!=0 or expertise is not null group by contactId");
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
$prefcount[$row[0]] = $row[1];
}

Expand Down
3 changes: 2 additions & 1 deletion src/assigners/a_follow.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ function load_state(AssignmentState $state) {
return;
}
$result = $state->conf->qe("select paperId, contactId, watch from PaperWatch where watch!=0 and paperId?a", $state->paper_ids());
while (($row = edb_row($result)))
while (($row = $result->fetch_row())) {
$state->load(["type" => "follow", "pid" => +$row[0], "cid" => +$row[1], "_watch" => +$row[2]]);
}
Dbl::free($result);
}
function allow_paper(PaperInfo $prow, AssignmentState $state) {
Expand Down
2 changes: 1 addition & 1 deletion src/assigners/a_preference.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function load_state(AssignmentState $state) {
return;
}
$result = $state->conf->qe("select paperId, contactId, preference, expertise from PaperReviewPreference where paperId?a", $state->paper_ids());
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
$state->load(["type" => "pref", "pid" => +$row[0], "cid" => +$row[1], "_pref" => +$row[2], "_exp" => self::make_exp($row[3])]);
}
Dbl::free($result);
Expand Down
2 changes: 1 addition & 1 deletion src/assigners/a_review.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function __construct(Conf $conf, $aj) {
static function load_review_state(AssignmentState $state) {
if ($state->mark_type("review", ["pid", "cid"], "Review_Assigner::make")) {
$result = $state->conf->qe("select paperId, contactId, reviewType, reviewRound, reviewSubmitted, timeApprovalRequested from PaperReview where paperId?a", $state->paper_ids());
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
$round = $state->conf->round_name($row[3]);
$state->load([
"type" => "review", "pid" => +$row[0], "cid" => +$row[1],
Expand Down
2 changes: 1 addition & 1 deletion src/assigners/a_tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static function load_tag_state(AssignmentState $state) {
return;
}
$result = $state->conf->qe("select paperId, tag, tagIndex from PaperTag where paperId?a", $state->paper_ids());
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
$state->load(["type" => "tag", "pid" => +$row[0], "ltag" => strtolower($row[1]), "_tag" => $row[1], "_index" => (float) $row[2]]);
}
Dbl::free($result);
Expand Down
4 changes: 2 additions & 2 deletions src/assignmentset.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ function load_rev() {
where p.timeWithdrawn<=0 and p.timeSubmitted>0
and u.roles!=0 and (u.roles&" . Contact::ROLE_PC . ")!=0
group by u.contactId");
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
$ct = $this->ensure($row[0]);
$ct->rev = strlen($row[1]);
$ct->meta = substr_count($row[1], REVIEW_META);
Expand All @@ -526,7 +526,7 @@ private function load_paperpc($type) {
$result = $this->conf->qe("select {$type}ContactId, count(paperId)
from Paper where timeWithdrawn<=0 and timeSubmitted>0
group by {$type}ContactId");
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
$ct = $this->ensure($row[0]);
$ct->$type = +$row[1];
}
Expand Down
18 changes: 9 additions & 9 deletions src/autoassigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function run_prefconflict($papertype) {
$papers = array_fill_keys($this->papersel, 1);
$result = $this->conf->preference_conflict_result($papertype, "");
$this->ass = ["paper,action,email"];
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
if (!isset($papers[$row[0]]) || !isset($this->pcm[$row[1]])) {
continue;
}
Expand Down Expand Up @@ -139,7 +139,7 @@ function run_clear($reviewtype) {
}
$this->ass = ["paper,action,email"];
$result = $this->conf->qe_raw($q);
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
if (isset($papers[$row[0]]) && isset($this->pcm[$row[1]])) {
$this->ass[] = "$row[0],$action," . $this->pcm[$row[1]]->email;
}
Expand All @@ -154,7 +154,7 @@ private function balance_reviews($reviewtype) {
$q .= " and reviewType={$reviewtype}";
}
$result = $this->conf->qe($q . " group by contactId", array_keys($this->pcm));
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
$this->load[(int) $row[0]] = (int) $row[1];
}
Dbl::free($result);
Expand All @@ -163,7 +163,7 @@ private function balance_reviews($reviewtype) {
private function balance_paperpc($action) {
$q = "select {$action}ContactId, count(paperId) from Paper where paperId ?A group by {$action}ContactId";
$result = $this->conf->qe($q, $this->papersel);
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
$this->load[(int) $row[0]] = (int) $row[1];
}
Dbl::free($result);
Expand All @@ -186,7 +186,7 @@ private function preferences_review($reviewtype) {

// first load refusals
$result = $this->conf->qe("select paperId, contactId from PaperReviewRefused where paperId ?a", $this->papersel);
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
$this->eass[(int) $row[1]][(int) $row[0]] = self::ENOASSIGN;
}

Expand Down Expand Up @@ -221,7 +221,7 @@ private function preferences_review($reviewtype) {
$missing_pcm = array_diff(array_keys($this->badpairs), array_keys($this->pcm));
if ($missing_pcm) {
$result = $this->conf->qe("select contactId, paperId from PaperReview where paperId?a and contactId?a", $this->papersel, array_values($missing_pcm));
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
$this->eass[$row[0]][$row[1]] = max($this->eass[$row[0]][$row[1]], self::ENOASSIGN);
}
Dbl::free($result);
Expand Down Expand Up @@ -703,7 +703,7 @@ function run_paperpc($action, $preference) {
$this->preferences_paperpc($preference);
$papers = array_fill_keys($this->papersel, 0);
$result = $this->conf->qe("select paperId from Paper where {$action}ContactId=0");
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
if (isset($papers[$row[0]]))
$papers[$row[0]] = 1;
}
Expand Down Expand Up @@ -743,7 +743,7 @@ function run_ensure_reviews($reviewtype, $round, $nass) {
$this->preferences_review($reviewtype);
$papers = array_fill_keys($this->papersel, $nass);
$result = $this->conf->qe("select paperId, count(reviewId) from PaperReview where reviewType={$reviewtype} group by paperId");
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
if (isset($papers[$row[0]]))
$papers[$row[0]] = max($nass - $row[1], 0);
}
Expand Down Expand Up @@ -833,7 +833,7 @@ function run_discussion_order($tag, $sequential = false) {
$cflt[$pid] = array();
}
$result = $this->conf->qe("select paperId, contactId from PaperConflict where paperId?a and contactId?a and conflictType>" . CONFLICT_MAXUNCONFLICTED, $this->papersel, array_keys($this->pcm));
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
$cflt[(int) $row[0]][] = (int) $row[1];
}
Dbl::free($result);
Expand Down
2 changes: 1 addition & 1 deletion src/commentinfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ function save($req, Contact $acting_contact) {
}

$result = $this->conf->qe_apply($q, $qv);
if (!$result) {
if (Dbl::is_error($result)) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/conference.php
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,7 @@ function user_by_email($email) {

function user_id_by_email($email) {
$result = $this->qe("select contactId from ContactInfo where email=?", trim($email));
$row = edb_row($result);
$row = $result->fetch_row();
Dbl::free($result);
return $row ? (int) $row[0] : false;
}
Expand Down Expand Up @@ -2820,7 +2820,7 @@ function count_submitted_accepted() {
$dlt = max($this->setting("sub_sub"), $this->setting("sub_close"));
$result = $this->qe("select outcome, count(paperId) from Paper where timeSubmitted>0 " . ($dlt ? "or (timeSubmitted=-100 and timeWithdrawn>=$dlt) " : "") . "group by outcome");
$n = $nyes = 0;
while (($row = edb_row($result))) {
while (($row = $result->fetch_row())) {
$n += $row[1];
if ($row[0] > 0) {
$nyes += $row[1];
Expand Down
Loading

0 comments on commit f126941

Please sign in to comment.