Changeset View
Changeset View
Standalone View
Standalone View
src/applications/almanac/query/AlmanacInterfaceQuery.php
Show All 28 Lines | public function withDevicePHIDs(array $phids) { | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withAddresses(array $addresses) { | public function withAddresses(array $addresses) { | ||||
$this->addresses = $addresses; | $this->addresses = $addresses; | ||||
return $this; | return $this; | ||||
} | } | ||||
protected function loadPage() { | public function newResultObject() { | ||||
$table = new AlmanacInterface(); | return new AlmanacInterface(); | ||||
$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); | protected function loadPage() { | ||||
return $this->loadStandardPage($this->newResultObject()); | |||||
} | } | ||||
protected function willFilterPage(array $interfaces) { | protected function willFilterPage(array $interfaces) { | ||||
$network_phids = mpull($interfaces, 'getNetworkPHID'); | $network_phids = mpull($interfaces, 'getNetworkPHID'); | ||||
$device_phids = mpull($interfaces, 'getDevicePHID'); | $device_phids = mpull($interfaces, 'getDevicePHID'); | ||||
$networks = id(new AlmanacNetworkQuery()) | $networks = id(new AlmanacNetworkQuery()) | ||||
->setParentQuery($this) | ->setParentQuery($this) | ||||
Show All 20 Lines | foreach ($interfaces as $key => $interface) { | ||||
$interface->attachNetwork($network); | $interface->attachNetwork($network); | ||||
$interface->attachDevice($device); | $interface->attachDevice($device); | ||||
} | } | ||||
return $interfaces; | return $interfaces; | ||||
} | } | ||||
protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { | protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { | ||||
$where = array(); | $where = parent::buildWhereClauseParts($conn); | ||||
if ($this->ids !== null) { | if ($this->ids !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn_r, | $conn, | ||||
'id IN (%Ld)', | 'interface.id IN (%Ld)', | ||||
$this->ids); | $this->ids); | ||||
} | } | ||||
if ($this->phids !== null) { | if ($this->phids !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn_r, | $conn, | ||||
'phid IN (%Ls)', | 'interface.phid IN (%Ls)', | ||||
$this->phids); | $this->phids); | ||||
} | } | ||||
if ($this->networkPHIDs !== null) { | if ($this->networkPHIDs !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn_r, | $conn, | ||||
'networkPHID IN (%Ls)', | 'interface.networkPHID IN (%Ls)', | ||||
$this->networkPHIDs); | $this->networkPHIDs); | ||||
} | } | ||||
if ($this->devicePHIDs !== null) { | if ($this->devicePHIDs !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn_r, | $conn, | ||||
'devicePHID IN (%Ls)', | 'interface.devicePHID IN (%Ls)', | ||||
$this->devicePHIDs); | $this->devicePHIDs); | ||||
} | } | ||||
if ($this->addresses !== null) { | if ($this->addresses !== null) { | ||||
$parts = array(); | $parts = array(); | ||||
foreach ($this->addresses as $address) { | foreach ($this->addresses as $address) { | ||||
$parts[] = qsprintf( | $parts[] = qsprintf( | ||||
$conn_r, | $conn, | ||||
'(networkPHID = %s AND address = %s AND port = %d)', | '(interface.networkPHID = %s '. | ||||
'AND interface.address = %s '. | |||||
'AND interface.port = %d)', | |||||
$address->getNetworkPHID(), | $address->getNetworkPHID(), | ||||
$address->getAddress(), | $address->getAddress(), | ||||
$address->getPort()); | $address->getPort()); | ||||
} | } | ||||
$where[] = implode(' OR ', $parts); | $where[] = implode(' OR ', $parts); | ||||
} | } | ||||
$where[] = $this->buildPagingClause($conn_r); | return $where; | ||||
} | |||||
protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) { | |||||
$joins = parent::buildJoinClauseParts($conn); | |||||
if ($this->shouldJoinDeviceTable()) { | |||||
$joins[] = qsprintf( | |||||
$conn, | |||||
'JOIN %T device ON device.phid = interface.devicePHID', | |||||
id(new AlmanacDevice())->getTableName()); | |||||
} | |||||
return $joins; | |||||
} | |||||
protected function shouldGroupQueryResultRows() { | |||||
if ($this->shouldJoinDeviceTable()) { | |||||
return true; | |||||
} | |||||
return parent::shouldGroupQueryResultRows(); | |||||
} | |||||
private function shouldJoinDeviceTable() { | |||||
$vector = $this->getOrderVector(); | |||||
if ($vector->containsKey('name')) { | |||||
return true; | |||||
} | |||||
return $this->formatWhereClause($where); | return false; | ||||
} | |||||
protected function getPrimaryTableAlias() { | |||||
return 'interface'; | |||||
} | } | ||||
public function getQueryApplicationClass() { | public function getQueryApplicationClass() { | ||||
return 'PhabricatorAlmanacApplication'; | return 'PhabricatorAlmanacApplication'; | ||||
} | } | ||||
public function getBuiltinOrders() { | |||||
return array( | |||||
'name' => array( | |||||
'vector' => array('name', 'id'), | |||||
'name' => pht('Device Name'), | |||||
), | |||||
) + parent::getBuiltinOrders(); | |||||
} | |||||
public function getOrderableColumns() { | |||||
return parent::getOrderableColumns() + array( | |||||
'name' => array( | |||||
'table' => 'device', | |||||
'column' => 'name', | |||||
'type' => 'string', | |||||
'reverse' => true, | |||||
), | |||||
); | |||||
} | |||||
protected function getPagingValueMap($cursor, array $keys) { | |||||
$interface = $this->loadCursorObject($cursor); | |||||
$map = array( | |||||
'id' => $interface->getID(), | |||||
'name' => $interface->getDevice()->getName(), | |||||
); | |||||
return $map; | |||||
} | |||||
} | } |