From 870e75c99c811da73791582e3ab7129ee4b4ad20 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 4 May 2015 11:39:12 +0200 Subject: [PATCH] Introduce class Icinga\Repository\DbRepository refs #8826 --- library/Icinga/Repository/DbRepository.php | 64 ++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 library/Icinga/Repository/DbRepository.php diff --git a/library/Icinga/Repository/DbRepository.php b/library/Icinga/Repository/DbRepository.php new file mode 100644 index 000000000..8ba43efaf --- /dev/null +++ b/library/Icinga/Repository/DbRepository.php @@ -0,0 +1,64 @@ + + *
  • Automatic table prefix handling
  • + * + */ +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; + } +}