65 lines
1.8 KiB
PHP
65 lines
1.8 KiB
PHP
|
<?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;
|
||
|
}
|
||
|
}
|