3 final class ReleephBranchQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery
{
11 const STATUS_ALL
= 'status-all';
12 const STATUS_OPEN
= 'status-open';
13 private $status = self
::STATUS_ALL
;
15 private $needCutPointCommits;
17 public function withIDs(array $ids) {
22 public function withPHIDs(array $phids) {
23 $this->phids
= $phids;
27 public function needCutPointCommits($need_commits) {
28 $this->needCutPointCommits
= $need_commits;
32 public function withStatus($status) {
33 $this->status
= $status;
37 public function withProductPHIDs($product_phids) {
38 $this->productPHIDs
= $product_phids;
42 protected function loadPage() {
43 $table = new ReleephBranch();
44 $conn_r = $table->establishConnection('r');
48 'SELECT * FROM %T %Q %Q %Q',
49 $table->getTableName(),
50 $this->buildWhereClause($conn_r),
51 $this->buildOrderClause($conn_r),
52 $this->buildLimitClause($conn_r));
54 return $table->loadAllFromArray($data);
57 protected function willExecute() {
58 if ($this->productPHIDs
!== null) {
59 $products = id(new ReleephProductQuery())
60 ->setViewer($this->getViewer())
61 ->withPHIDs($this->productPHIDs
)
65 throw new PhabricatorEmptyQueryException();
68 $this->productIDs
= mpull($products, 'getID');
72 protected function willFilterPage(array $branches) {
73 $project_ids = mpull($branches, 'getReleephProjectID');
75 $projects = id(new ReleephProductQuery())
76 ->withIDs($project_ids)
77 ->setViewer($this->getViewer())
80 foreach ($branches as $key => $branch) {
81 $project_id = $project_ids[$key];
82 if (isset($projects[$project_id])) {
83 $branch->attachProject($projects[$project_id]);
85 unset($branches[$key]);
89 if ($this->needCutPointCommits
) {
90 $commit_phids = mpull($branches, 'getCutPointCommitPHID');
91 $commits = id(new DiffusionCommitQuery())
92 ->setViewer($this->getViewer())
93 ->withPHIDs($commit_phids)
95 $commits = mpull($commits, null, 'getPHID');
97 foreach ($branches as $branch) {
98 $commit = idx($commits, $branch->getCutPointCommitPHID());
99 $branch->attachCutPointCommit($commit);
106 protected function buildWhereClause(AphrontDatabaseConnection
$conn) {
109 if ($this->ids
!== null) {
116 if ($this->phids
!== null) {
123 if ($this->productIDs
!== null) {
126 'releephProjectID IN (%Ld)',
130 $status = $this->status
;
132 case self
::STATUS_ALL
:
134 case self
::STATUS_OPEN
:
140 throw new Exception(pht("Unknown status constant '%s'!", $status));
143 $where[] = $this->buildPagingClause($conn);
145 return $this->formatWhereClause($conn, $where);
148 public function getQueryApplicationClass() {
149 return 'PhabricatorReleephApplication';