1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-09-20 09:18:48 +02:00

Introduce PhabricatorNamedQueryQuery

Summary: Adds a first-class Query object for querying NamedQueries. xzibit would be proud.

Test Plan: Updated query edit interface to use this query, verified it works correctly.

Reviewers: btrahan, blc

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T2625

Differential Revision: https://secure.phabricator.com/D6051
This commit is contained in:
epriestley 2013-05-27 13:40:43 -07:00
parent 6551ea8245
commit 12e9350efb
4 changed files with 128 additions and 20 deletions

View file

@ -1147,6 +1147,7 @@ phutil_register_library_map(array(
'PhabricatorMySQLConfigOptions' => 'applications/config/option/PhabricatorMySQLConfigOptions.php',
'PhabricatorMySQLFileStorageEngine' => 'applications/files/engine/PhabricatorMySQLFileStorageEngine.php',
'PhabricatorNamedQuery' => 'applications/search/storage/PhabricatorNamedQuery.php',
'PhabricatorNamedQueryQuery' => 'applications/search/query/PhabricatorNamedQueryQuery.php',
'PhabricatorNoteExample' => 'applications/uiexample/examples/PhabricatorNoteExample.php',
'PhabricatorNotificationBuilder' => 'applications/notification/builder/PhabricatorNotificationBuilder.php',
'PhabricatorNotificationClearController' => 'applications/notification/controller/PhabricatorNotificationClearController.php',
@ -2923,7 +2924,12 @@ phutil_register_library_map(array(
'PhabricatorMustVerifyEmailController' => 'PhabricatorAuthController',
'PhabricatorMySQLConfigOptions' => 'PhabricatorApplicationConfigOptions',
'PhabricatorMySQLFileStorageEngine' => 'PhabricatorFileStorageEngine',
'PhabricatorNamedQuery' => 'PhabricatorSearchDAO',
'PhabricatorNamedQuery' =>
array(
0 => 'PhabricatorSearchDAO',
1 => 'PhabricatorPolicyInterface',
),
'PhabricatorNamedQueryQuery' => 'PhabricatorCursorPagedPolicyAwareQuery',
'PhabricatorNoteExample' => 'PhabricatorUIExample',
'PhabricatorNotificationClearController' => 'PhabricatorNotificationController',
'PhabricatorNotificationConfigOptions' => 'PhabricatorApplicationConfigOptions',

View file

@ -10,32 +10,28 @@ final class PhabricatorPasteQueriesController
$nav = $this->buildSideNavView("");
$filter = $nav->getSelectedFilter();
$table = new PhabricatorNamedQuery();
$conn = $table->establishConnection('r');
$data = queryfx_all(
$conn,
'SELECT * FROM %T WHERE userPHID=%s AND engineClassName=%s',
$table->getTableName(),
$user->getPHID(),
'PhabricatorPasteSearchEngine');
$named_queries = id(new PhabricatorNamedQueryQuery())
->setViewer($user)
->withUserPHIDs(array($user->getPHID()))
->withEngineClassNames(array('PhabricatorPasteSearchEngine'))
->execute();
$list = new PhabricatorObjectItemListView();
$list->setUser($user);
foreach ($data as $key => $saved_query) {
$date_created = phabricator_datetime($saved_query["dateCreated"], $user);
foreach ($named_queries as $named_query) {
$date_created = phabricator_datetime(
$named_query->getDateCreated(),
$user);
$item = id(new PhabricatorObjectItemView())
->setHeader($saved_query["queryName"])
->setHref('/paste/query/'.$saved_query["queryKey"].'/')
->addByline(pht('Date Created: ').$date_created);
->setHeader($named_query->getQueryName())
->setHref('/paste/query/'.$named_query->getQueryKey().'/')
->addIcon('none', $date_created);
$list->addItem($item);
}
$pager = new AphrontCursorPagerView();
$pager->readFromRequest($request);
$list->setPager($pager);
$list->setNoDataString(pht("No results found for this query."));
$nav->appendChild(

View file

@ -0,0 +1,84 @@
<?php
/**
* @group search
*/
final class PhabricatorNamedQueryQuery
extends PhabricatorCursorPagedPolicyAwareQuery {
private $ids;
private $engineClassNames;
private $userPHIDs;
private $queryKeys;
public function withIDs(array $ids) {
$this->ids = $ids;
return $this;
}
public function withUserPHIDs(array $user_phids) {
$this->userPHIDs = $user_phids;
return $this;
}
public function withEngineClassNames(array $engine_class_names) {
$this->engineClassNames = $engine_class_names;
return $this;
}
public function withQueryKeys(array $query_keys) {
$this->queryKeys = $query_keys;
return $this;
}
protected function loadPage() {
$table = new PhabricatorNamedQuery();
$conn_r = $table->establishConnection('r');
$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));
return $table->loadAllFromArray($data);
}
private function buildWhereClause($conn_r) {
$where = array();
if ($this->ids) {
$where[] = qsprintf(
$conn_r,
'id IN (%Ld)',
$this->ids);
}
if ($this->engineClassNames) {
$where[] = qsprintf(
$conn_r,
'engineClassName IN (%Ls)',
$this->engineClassNames);
}
if ($this->userPHIDs) {
$where[] = qsprintf(
$conn_r,
'userPHID IN (%Ls)',
$this->userPHIDs);
}
if ($this->queryKeys) {
$where[] = qsprintf(
$conn_r,
'queryKey IN (%Ls)',
$this->queryKeys);
}
$where[] = $this->buildPagingClause($conn_r);
return $this->formatWhereClause($where);
}
}

View file

@ -3,11 +3,33 @@
/**
* @group search
*/
final class PhabricatorNamedQuery extends PhabricatorSearchDAO {
final class PhabricatorNamedQuery extends PhabricatorSearchDAO
implements PhabricatorPolicyInterface {
protected $queryKey = "";
protected $queryName = "";
protected $userPHID = "";
protected $engineClassName = "";
/* -( PhabricatorPolicyInterface )----------------------------------------- */
public function getCapabilities() {
return array(
PhabricatorPolicyCapability::CAN_VIEW,
);
}
public function getPolicy($capability) {
return PhabricatorPolicies::POLICY_NOONE;
}
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
if ($viewer->getPHID() == $this->userPHID) {
return true;
}
return false;
}
}