diff --git a/src/applications/diffusion/query/DiffusionCommitQuery.php b/src/applications/diffusion/query/DiffusionCommitQuery.php index 8abb67b99f..22331e8510 100644 --- a/src/applications/diffusion/query/DiffusionCommitQuery.php +++ b/src/applications/diffusion/query/DiffusionCommitQuery.php @@ -149,8 +149,8 @@ final class DiffusionCommitQuery return $this->identifierMap; } - protected function getPagingColumn() { - return 'commit.id'; + protected function getPrimaryTableAlias() { + return 'commit'; } protected function willExecute() { diff --git a/src/applications/phrequent/query/PhrequentUserTimeQuery.php b/src/applications/phrequent/query/PhrequentUserTimeQuery.php index 06a9028a13..ecf8656fe5 100644 --- a/src/applications/phrequent/query/PhrequentUserTimeQuery.php +++ b/src/applications/phrequent/query/PhrequentUserTimeQuery.php @@ -9,26 +9,29 @@ final class PhrequentUserTimeQuery const ORDER_STARTED_DESC = 3; const ORDER_ENDED_ASC = 4; const ORDER_ENDED_DESC = 5; - const ORDER_DURATION_ASC = 6; - const ORDER_DURATION_DESC = 7; const ENDED_YES = 0; const ENDED_NO = 1; const ENDED_ALL = 2; + private $ids; private $userPHIDs; private $objectPHIDs; - private $order = self::ORDER_ID_ASC; private $ended = self::ENDED_ALL; private $needPreemptingEvents; - public function withUserPHIDs($user_phids) { + public function withIDs(array $ids) { + $this->ids = $ids; + return $this; + } + + public function withUserPHIDs(array $user_phids) { $this->userPHIDs = $user_phids; return $this; } - public function withObjectPHIDs($object_phids) { + public function withObjectPHIDs(array $object_phids) { $this->objectPHIDs = $object_phids; return $this; } @@ -39,7 +42,29 @@ final class PhrequentUserTimeQuery } public function setOrder($order) { - $this->order = $order; + switch ($order) { + case self::ORDER_ID_ASC: + $this->setOrderVector(array('-id')); + break; + case self::ORDER_ID_DESC: + $this->setOrderVector(array('id')); + break; + case self::ORDER_STARTED_ASC: + $this->setOrderVector(array('-start', '-id')); + break; + case self::ORDER_STARTED_DESC: + $this->setOrderVector(array('start', 'id')); + break; + case self::ORDER_ENDED_ASC: + $this->setOrderVector(array('-ongoing', '-end', '-id')); + break; + case self::ORDER_ENDED_DESC: + $this->setOrderVector(array('ongoing', 'end', 'id')); + break; + default: + throw new Exception(pht('Unknown order "%s".', $order)); + } + return $this; } @@ -51,14 +76,21 @@ final class PhrequentUserTimeQuery private function buildWhereClause(AphrontDatabaseConnection $conn) { $where = array(); - if ($this->userPHIDs) { + if ($this->ids !== null) { + $where[] = qsprintf( + $conn, + 'id IN (%Ld)', + $this->ids); + } + + if ($this->userPHIDs !== null) { $where[] = qsprintf( $conn, 'userPHID IN (%Ls)', $this->userPHIDs); } - if ($this->objectPHIDs) { + if ($this->objectPHIDs !== null) { $where[] = qsprintf( $conn, 'objectPHID IN (%Ls)', @@ -87,59 +119,31 @@ final class PhrequentUserTimeQuery return $this->formatWhereClause($where); } - protected function getPagingColumn() { - switch ($this->order) { - case self::ORDER_ID_ASC: - case self::ORDER_ID_DESC: - return 'id'; - case self::ORDER_STARTED_ASC: - case self::ORDER_STARTED_DESC: - return 'dateStarted'; - case self::ORDER_ENDED_ASC: - case self::ORDER_ENDED_DESC: - return 'dateEnded'; - case self::ORDER_DURATION_ASC: - case self::ORDER_DURATION_DESC: - return 'COALESCE(dateEnded, UNIX_TIMESTAMP()) - dateStarted'; - default: - throw new Exception("Unknown order '{$this->order}'!"); - } + public function getOrderableColumns() { + return parent::getOrderableColumns() + array( + 'start' => array( + 'column' => 'dateStarted', + 'type' => 'int', + ), + 'ongoing' => array( + 'column' => 'dateEnded', + 'type' => 'null', + ), + 'end' => array( + 'column' => 'dateEnded', + 'type' => 'int', + ), + ); } - protected function getPagingValue($result) { - switch ($this->order) { - case self::ORDER_ID_ASC: - case self::ORDER_ID_DESC: - return $result->getID(); - case self::ORDER_STARTED_ASC: - case self::ORDER_STARTED_DESC: - return $result->getDateStarted(); - case self::ORDER_ENDED_ASC: - case self::ORDER_ENDED_DESC: - return $result->getDateEnded(); - case self::ORDER_DURATION_ASC: - case self::ORDER_DURATION_DESC: - return ($result->getDateEnded() || time()) - $result->getDateStarted(); - default: - throw new Exception("Unknown order '{$this->order}'!"); - } - } - - protected function getReversePaging() { - switch ($this->order) { - case self::ORDER_ID_ASC: - case self::ORDER_STARTED_ASC: - case self::ORDER_ENDED_ASC: - case self::ORDER_DURATION_ASC: - return true; - case self::ORDER_ID_DESC: - case self::ORDER_STARTED_DESC: - case self::ORDER_ENDED_DESC: - case self::ORDER_DURATION_DESC: - return false; - default: - throw new Exception("Unknown order '{$this->order}'!"); - } + protected function getPagingValueMap($cursor, array $keys) { + $usertime = $this->loadCursorObject($cursor); + return array( + 'id' => $usertime->getID(), + 'start' => $usertime->getDateStarted(), + 'ongoing' => $usertime->getDateEnded(), + 'end' => $usertime->getDateEnded(), + ); } protected function loadPage() { @@ -249,8 +253,6 @@ final class PhrequentUserTimeQuery self::ORDER_STARTED_DESC => pht('by nearest start date'), self::ORDER_ENDED_ASC => pht('by furthest end date'), self::ORDER_ENDED_DESC => pht('by nearest end date'), - self::ORDER_DURATION_ASC => pht('by smallest duration'), - self::ORDER_DURATION_DESC => pht('by largest duration'), ); } diff --git a/src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php b/src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php index fdeb3cd459..dceaf6c331 100644 --- a/src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php +++ b/src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php @@ -570,18 +570,20 @@ abstract class PhabricatorCursorPagedPolicyAwareQuery $table = idx($part, 'table'); $column = $part['column']; - if ($descending) { - if ($table !== null) { - $sql[] = qsprintf($conn, '%T.%T DESC', $table, $column); - } else { - $sql[] = qsprintf($conn, '%T DESC', $column); - } + if ($table !== null) { + $field = qsprintf($conn, '%T.%T', $table, $column); } else { - if ($table !== null) { - $sql[] = qsprintf($conn, '%T.%T ASC', $table, $column); - } else { - $sql[] = qsprintf($conn, '%T ASC', $column); - } + $field = qsprintf($conn, '%T', $column); + } + + if (idx($part, 'type') === 'null') { + $field = qsprintf($conn, '(%Q IS NULL)', $field); + } + + if ($descending) { + $sql[] = qsprintf($conn, '%Q DESC', $field); + } else { + $sql[] = qsprintf($conn, '%Q ASC', $field); } }