Introduce class Icinga\Repository\DbRepository

refs #8826
This commit is contained in:
Johannes Meyer 2015-05-04 11:39:12 +02:00
parent fa1906ee7d
commit 870e75c99c
1 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,64 @@
<?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Repository;
use Icinga\Exception\IcingaException;
use Icinga\Exception\ProgrammingError;
/**
* Abstract base class for concrete database repository implementations
*
* Additionally provided features:
* <ul>
* <li>Automatic table prefix handling</li>
* </ul>
*/
abstract class DbRepository extends Repository
{
/**
* Return the base table name this repository is responsible for
*
* This prepends the datasource's table prefix, if available and required.
*
* @return mixed
*
* @throws ProgrammingError In case no base table name has been set and
* $this->queryColumns does not provide one either
*/
public function getBaseTable()
{
return $this->prependTablePrefix(parent::getBaseTable());
}
/**
* Return the given table with the datasource's prefix being prepended
*
* @param array|string $table
*
* @return array|string
*
* @throws IcingaException In case $table is not of a supported type
*/
protected function prependTablePrefix($table)
{
$prefix = $this->ds->getTablePrefix();
if (! $prefix) {
return $table;
}
if (is_array($table)) {
foreach ($table as & $tableName) {
if (strpos($tableName, $prefix) === false) {
$tableName = $prefix . $tableName;
}
}
} elseif (is_string($table)) {
$table = (strpos($table, $prefix) === false ? $prefix : '') . $table;
} else {
throw new IcingaException('Table prefix handling for type "%s" is not supported', type($table));
}
return $table;
}
}