2014-04-14 21:06:26 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class DifferentialHunkQuery
|
|
|
|
extends PhabricatorCursorPagedPolicyAwareQuery {
|
|
|
|
|
|
|
|
private $changesets;
|
|
|
|
private $shouldAttachToChangesets;
|
|
|
|
|
|
|
|
public function withChangesets(array $changesets) {
|
|
|
|
assert_instances_of($changesets, 'DifferentialChangeset');
|
|
|
|
$this->changesets = $changesets;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function needAttachToChangesets($attach) {
|
|
|
|
$this->shouldAttachToChangesets = $attach;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-01-13 20:56:07 +01:00
|
|
|
protected function willExecute() {
|
2014-04-23 23:22:10 +02:00
|
|
|
// If we fail to load any hunks at all (for example, because all of
|
|
|
|
// the requested changesets are directories or empty files and have no
|
|
|
|
// hunks) we'll never call didFilterPage(), and thus never have an
|
|
|
|
// opportunity to attach hunks. Attach empty hunk lists now so that we
|
|
|
|
// end up with the right result.
|
|
|
|
if ($this->shouldAttachToChangesets) {
|
|
|
|
foreach ($this->changesets as $changeset) {
|
|
|
|
$changeset->attachHunks(array());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-13 20:56:07 +01:00
|
|
|
protected function loadPage() {
|
2014-05-25 18:28:11 +02:00
|
|
|
$all_results = array();
|
|
|
|
|
|
|
|
// Load modern hunks.
|
2015-01-22 21:17:04 +01:00
|
|
|
$table = new DifferentialModernHunk();
|
2014-05-25 18:28:11 +02:00
|
|
|
$conn_r = $table->establishConnection('r');
|
|
|
|
|
|
|
|
$modern_data = queryfx_all(
|
|
|
|
$conn_r,
|
|
|
|
'SELECT * FROM %T %Q %Q %Q',
|
|
|
|
$table->getTableName(),
|
|
|
|
$this->buildWhereClause($conn_r),
|
|
|
|
$this->buildOrderClause($conn_r),
|
|
|
|
$this->buildLimitClause($conn_r));
|
|
|
|
$modern_results = $table->loadAllFromArray($modern_data);
|
|
|
|
|
|
|
|
|
|
|
|
// Now, load legacy hunks.
|
2015-01-22 21:17:04 +01:00
|
|
|
$table = new DifferentialLegacyHunk();
|
2014-04-14 21:06:26 +02:00
|
|
|
$conn_r = $table->establishConnection('r');
|
|
|
|
|
2014-05-25 18:28:11 +02:00
|
|
|
$legacy_data = queryfx_all(
|
2014-04-14 21:06:26 +02:00
|
|
|
$conn_r,
|
|
|
|
'SELECT * FROM %T %Q %Q %Q',
|
|
|
|
$table->getTableName(),
|
|
|
|
$this->buildWhereClause($conn_r),
|
|
|
|
$this->buildOrderClause($conn_r),
|
|
|
|
$this->buildLimitClause($conn_r));
|
2014-05-25 18:28:11 +02:00
|
|
|
$legacy_results = $table->loadAllFromArray($legacy_data);
|
2014-04-14 21:06:26 +02:00
|
|
|
|
2014-05-25 18:28:11 +02:00
|
|
|
// Strip all the IDs off since they're not unique and nothing should be
|
|
|
|
// using them.
|
|
|
|
return array_values(array_merge($legacy_results, $modern_results));
|
2014-04-14 21:06:26 +02:00
|
|
|
}
|
|
|
|
|
2015-01-13 20:56:07 +01:00
|
|
|
protected function willFilterPage(array $hunks) {
|
2014-04-14 21:06:26 +02:00
|
|
|
$changesets = mpull($this->changesets, null, 'getID');
|
|
|
|
foreach ($hunks as $key => $hunk) {
|
|
|
|
$changeset = idx($changesets, $hunk->getChangesetID());
|
|
|
|
if (!$changeset) {
|
|
|
|
unset($hunks[$key]);
|
|
|
|
}
|
|
|
|
$hunk->attachChangeset($changeset);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $hunks;
|
|
|
|
}
|
|
|
|
|
2015-01-13 20:56:07 +01:00
|
|
|
protected function didFilterPage(array $hunks) {
|
2014-04-14 21:06:26 +02:00
|
|
|
if ($this->shouldAttachToChangesets) {
|
|
|
|
$hunk_groups = mgroup($hunks, 'getChangesetID');
|
|
|
|
foreach ($this->changesets as $changeset) {
|
|
|
|
$hunks = idx($hunk_groups, $changeset->getID(), array());
|
|
|
|
$changeset->attachHunks($hunks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $hunks;
|
|
|
|
}
|
|
|
|
|
Make buildWhereClause() a method of AphrontCursorPagedPolicyAwareQuery
Summary:
Ref T4100. Ref T5595.
To support a unified "Projects:" query across all applications, a future diff is going to add a set of "Edge Logic" capabilities to `PolicyAwareQuery` which write the required SELECT, JOIN, WHERE, HAVING and GROUP clauses for you.
With the addition of "Edge Logic", we'll have three systems which may need to build components of query claues: ordering/paging, customfields/applicationsearch, and edge logic.
For most clauses, queries don't currently call into the parent explicitly to get default components. I want to move more query construction logic up the class tree so it can be shared.
For most methods, this isn't a problem, but many subclasses define a `buildWhereClause()`. Make all such definitions protected and consistent.
This causes no behavioral changes.
Test Plan: Ran `arc unit --everything`, which does a pretty through job of verifying this statically.
Reviewers: btrahan
Reviewed By: btrahan
Subscribers: yelirekim, hach-que, epriestley
Maniphest Tasks: T4100, T5595
Differential Revision: https://secure.phabricator.com/D12453
2015-04-18 16:08:30 +02:00
|
|
|
protected function buildWhereClause(AphrontDatabaseConnection $conn_r) {
|
2014-04-14 21:06:26 +02:00
|
|
|
$where = array();
|
|
|
|
|
|
|
|
if (!$this->changesets) {
|
|
|
|
throw new Exception(
|
|
|
|
pht('You must load hunks via changesets, with withChangesets()!'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'changesetID IN (%Ld)',
|
|
|
|
mpull($this->changesets, 'getID'));
|
|
|
|
|
|
|
|
$where[] = $this->buildPagingClause($conn_r);
|
|
|
|
|
|
|
|
return $this->formatWhereClause($where);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getQueryApplicationClass() {
|
2014-07-23 02:03:09 +02:00
|
|
|
return 'PhabricatorDifferentialApplication';
|
2014-04-14 21:06:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-12 22:11:42 +02:00
|
|
|
protected function getDefaultOrderVector() {
|
|
|
|
// TODO: Do we need this?
|
|
|
|
return array('-id');
|
2014-04-14 21:06:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|