Skip to content
Extraits de code Groupes Projets
Valider 94303fb5 rédigé par Matthieu Napoli's avatar Matthieu Napoli
Parcourir les fichiers

Added dependency injection in Core\Sequence so that the SiteMigration plugin...

Added dependency injection in Core\Sequence so that the SiteMigration plugin can provide an alternative database
parent 055f9709
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
namespace Piwik; namespace Piwik;
use Exception; use Exception;
use Piwik\Db\AdapterInterface;
/** /**
* Used for generating auto increment ids. * Used for generating auto increment ids.
...@@ -23,14 +24,21 @@ class Sequence ...@@ -23,14 +24,21 @@ class Sequence
{ {
private $name; private $name;
/**
* @var AdapterInterface
*/
private $db;
/** /**
* The name of the table or sequence you want to get an id for. * The name of the table or sequence you want to get an id for.
* *
* @param string $name eg 'archive_numeric_2014_11' * @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.
*/ */
public function __construct($name) public function __construct($name, $db = null)
{ {
$this->name = $name; $this->name = $name;
$this->db = $db ?: Db::get();
} }
private function getTableName() private function getTableName()
...@@ -51,9 +59,8 @@ class Sequence ...@@ -51,9 +59,8 @@ class Sequence
$initialValue = (int) $initialValue; $initialValue = (int) $initialValue;
$table = $this->getTableName(); $table = $this->getTableName();
$db = $this->getDb();
$db->insert($table, array('name' => $this->name, 'value' => $initialValue)); $this->db->insert($table, array('name' => $this->name, 'value' => $initialValue));
return $initialValue; return $initialValue;
} }
...@@ -70,15 +77,14 @@ class Sequence ...@@ -70,15 +77,14 @@ class Sequence
$table = $this->getTableName(); $table = $this->getTableName();
$sql = 'UPDATE ' . $table . ' SET value = LAST_INSERT_ID(value + 1) WHERE name = ?'; $sql = 'UPDATE ' . $table . ' SET value = LAST_INSERT_ID(value + 1) WHERE name = ?';
$db = $this->getDb(); $result = $this->db->query($sql, array($this->name));
$result = $db->query($sql, array($this->name));
$rowCount = $result->rowCount(); $rowCount = $result->rowCount();
if (1 !== $rowCount) { if (1 !== $rowCount) {
throw new Exception("Sequence '" . $this->name . "' not found."); throw new Exception("Sequence '" . $this->name . "' not found.");
} }
$createdId = $db->lastInsertId(); $createdId = $this->db->lastInsertId();
return (int) $createdId; return (int) $createdId;
} }
...@@ -93,16 +99,10 @@ class Sequence ...@@ -93,16 +99,10 @@ class Sequence
$table = $this->getTableName(); $table = $this->getTableName();
$sql = 'SELECT value FROM ' . $table . ' WHERE name = ?'; $sql = 'SELECT value FROM ' . $table . ' WHERE name = ?';
$db = $this->getDb(); $id = $this->db->fetchOne($sql, array($this->name));
$id = $db->fetchOne($sql, array($this->name));
if (!empty($id) || '0' === $id || 0 === $id) { if (!empty($id) || '0' === $id || 0 === $id) {
return (int) $id; return (int) $id;
} }
} }
private function getDb()
{
return Db::get();
}
} }
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter