[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/drydock/query/ -> DrydockLeaseQuery.php (source)

   1  <?php
   2  
   3  final class DrydockLeaseQuery extends DrydockQuery {
   4  
   5    private $ids;
   6    private $phids;
   7    private $resourceIDs;
   8    private $statuses;
   9  
  10    public function withIDs(array $ids) {
  11      $this->ids = $ids;
  12      return $this;
  13    }
  14  
  15    public function withPHIDs(array $phids) {
  16      $this->phids = $phids;
  17      return $this;
  18    }
  19  
  20    public function withResourceIDs(array $ids) {
  21      $this->resourceIDs = $ids;
  22      return $this;
  23    }
  24  
  25    public function withStatuses(array $statuses) {
  26      $this->statuses = $statuses;
  27      return $this;
  28    }
  29  
  30    public function loadPage() {
  31      $table = new DrydockLease();
  32      $conn_r = $table->establishConnection('r');
  33  
  34      $data = queryfx_all(
  35        $conn_r,
  36        'SELECT lease.* FROM %T lease %Q %Q %Q',
  37        $table->getTableName(),
  38        $this->buildWhereClause($conn_r),
  39        $this->buildOrderClause($conn_r),
  40        $this->buildLimitClause($conn_r));
  41  
  42      return $table->loadAllFromArray($data);
  43    }
  44  
  45    public function willFilterPage(array $leases) {
  46      $resource_ids = array_filter(mpull($leases, 'getResourceID'));
  47      if ($resource_ids) {
  48        $resources = id(new DrydockResourceQuery())
  49          ->setParentQuery($this)
  50          ->setViewer($this->getViewer())
  51          ->withIDs($resource_ids)
  52          ->execute();
  53      } else {
  54        $resources = array();
  55      }
  56  
  57      foreach ($leases as $key => $lease) {
  58        $resource = null;
  59        if ($lease->getResourceID()) {
  60          $resource = idx($resources, $lease->getResourceID());
  61          if (!$resource) {
  62            unset($leases[$key]);
  63            continue;
  64          }
  65        }
  66        $lease->attachResource($resource);
  67      }
  68  
  69      return $leases;
  70    }
  71  
  72    private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
  73      $where = array();
  74  
  75      if ($this->resourceIDs) {
  76        $where[] = qsprintf(
  77          $conn_r,
  78          'resourceID IN (%Ld)',
  79          $this->resourceIDs);
  80      }
  81  
  82      if ($this->ids) {
  83        $where[] = qsprintf(
  84          $conn_r,
  85          'id IN (%Ld)',
  86          $this->ids);
  87      }
  88  
  89      if ($this->phids) {
  90        $where[] = qsprintf(
  91          $conn_r,
  92          'phid IN (%Ls)',
  93          $this->phids);
  94      }
  95  
  96      if ($this->statuses) {
  97        $where[] = qsprintf(
  98          $conn_r,
  99          'status IN (%Ld)',
 100          $this->statuses);
 101      }
 102  
 103      $where[] = $this->buildPagingClause($conn_r);
 104  
 105      return $this->formatWhereClause($where);
 106    }
 107  
 108  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1