1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-02-15 08:18:38 +01:00
phorge-phorge/src/applications/fact/storage/PhabricatorFactDimension.php
epriestley da40f80741 Update PhabricatorLiskDAO::chunkSQL() for new %Q semantics
Summary:
Ref T13217. This method is slightly tricky:

  - We can't safely return a string: return an array instead.
  - It no longer makes sense to accept glue. All callers use `', '` as glue anyway, so hard-code that.

Then convert all callsites.

Test Plan: Browsed around, saw fewer "unsafe" errors in error log.

Reviewers: amckinley

Reviewed By: amckinley

Subscribers: yelirekim, PHID-OPKG-gm6ozazyms6q6i22gyam

Maniphest Tasks: T13217

Differential Revision: https://secure.phabricator.com/D19784
2018-11-13 08:59:18 -08:00

110 lines
2.3 KiB
PHP

<?php
abstract class PhabricatorFactDimension extends PhabricatorFactDAO {
abstract protected function getDimensionColumnName();
final public function newDimensionID($key, $create = false) {
$map = $this->newDimensionMap(array($key), $create);
return idx($map, $key);
}
final public function newDimensionUnmap(array $ids) {
if (!$ids) {
return array();
}
$conn = $this->establishConnection('r');
$column = $this->getDimensionColumnName();
$rows = queryfx_all(
$conn,
'SELECT id, %C FROM %T WHERE id IN (%Ld)',
$column,
$this->getTableName(),
$ids);
$rows = ipull($rows, $column, 'id');
return $rows;
}
final public function newDimensionMap(array $keys, $create = false) {
if (!$keys) {
return array();
}
$conn = $this->establishConnection('r');
$column = $this->getDimensionColumnName();
$rows = queryfx_all(
$conn,
'SELECT id, %C FROM %T WHERE %C IN (%Ls)',
$column,
$this->getTableName(),
$column,
$keys);
$rows = ipull($rows, 'id', $column);
$map = array();
$need = array();
foreach ($keys as $key) {
if (isset($rows[$key])) {
$map[$key] = (int)$rows[$key];
} else {
$need[] = $key;
}
}
if (!$need) {
return $map;
}
if (!$create) {
return $map;
}
$sql = array();
foreach ($need as $key) {
$sql[] = qsprintf(
$conn,
'(%s)',
$key);
}
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) {
queryfx(
$conn,
'INSERT IGNORE INTO %T (%C) VALUES %LQ',
$this->getTableName(),
$column,
$chunk);
}
unset($unguarded);
$rows = queryfx_all(
$conn,
'SELECT id, %C FROM %T WHERE %C IN (%Ls)',
$column,
$this->getTableName(),
$column,
$need);
$rows = ipull($rows, 'id', $column);
foreach ($need as $key) {
if (isset($rows[$key])) {
$map[$key] = (int)$rows[$key];
} else {
throw new Exception(
pht(
'Failed to load or generate dimension ID ("%s") for dimension '.
'key "%s".',
get_class($this),
$key));
}
}
return $map;
}
}