diff --git a/core/Sequence.php b/core/Sequence.php index fa95b0b07c90c86ea426d1bed88c2c5310078e97..7a25c239591458560cb069a27b0b2436943dbaf7 100644 --- a/core/Sequence.php +++ b/core/Sequence.php @@ -22,6 +22,10 @@ use Piwik\Db\AdapterInterface; */ class Sequence { + const TABLE_NAME = 'sequence'; + /** + * @var string + */ private $name; /** @@ -29,21 +33,23 @@ class Sequence */ private $db; + /** + * @var string + */ + private $table; + /** * The name of the table or sequence you want to get an id for. * * @param string $name eg 'archive_numeric_2014_11' * @param AdapterInterface $db You can optionally pass a DB adapter to make it work against another database. + * @param string|null $tablePrefix */ - public function __construct($name, $db = null) + public function __construct($name, $db = null, $tablePrefix = null) { $this->name = $name; $this->db = $db ?: Db::get(); - } - - private function getTableName() - { - return Common::prefixTable('sequence'); + $this->table = $this->getTableName($tablePrefix); } /** @@ -58,9 +64,7 @@ class Sequence { $initialValue = (int) $initialValue; - $table = $this->getTableName(); - - $this->db->insert($table, array('name' => $this->name, 'value' => $initialValue)); + $this->db->insert($this->table, array('name' => $this->name, 'value' => $initialValue)); return $initialValue; } @@ -72,7 +76,7 @@ class Sequence */ public function exists() { - $query = $this->db->query('SELECT * FROM ' . $this->getTableName() . ' WHERE name = ?', $this->name); + $query = $this->db->query('SELECT * FROM ' . $this->table . ' WHERE name = ?', $this->name); return $query->rowCount() > 0; } @@ -86,8 +90,7 @@ class Sequence */ public function getNextId() { - $table = $this->getTableName(); - $sql = 'UPDATE ' . $table . ' SET value = LAST_INSERT_ID(value + 1) WHERE name = ?'; + $sql = 'UPDATE ' . $this->table . ' SET value = LAST_INSERT_ID(value + 1) WHERE name = ?'; $result = $this->db->query($sql, array($this->name)); $rowCount = $result->rowCount(); @@ -108,8 +111,7 @@ class Sequence */ public function getCurrentId() { - $table = $this->getTableName(); - $sql = 'SELECT value FROM ' . $table . ' WHERE name = ?'; + $sql = 'SELECT value FROM ' . $this->table . ' WHERE name = ?'; $id = $this->db->fetchOne($sql, array($this->name)); @@ -117,4 +119,9 @@ class Sequence return (int) $id; } } + + private function getTableName($prefix) + { + return ($prefix !== null) ? $prefix . self::TABLE_NAME : Common::prefixTable(self::TABLE_NAME); + } }